corevm_guest/
rust.rs

1//! Rust wrappers around C API.
2
3#![allow(clippy::missing_safety_doc)]
4
5use crate::c;
6use core::mem::size_of_val;
7
8/// Get the current amount of gas.
9pub fn gas() -> u64 {
10	unsafe { c::gas() }
11}
12
13/// Allocate memory block of the specified size.
14///
15/// - The size is rounded up to the next multiple of page size.
16/// - The returned address is aligned at a page boundary.
17///
18/// Returns the pointer to the allocated memory region or [`null_mut()`](core::ptr::null_mut) if the
19/// allocation was unsuccessful.
20pub unsafe fn alloc(size: u64) -> *mut u8 {
21	c::alloc(size) as *mut u8
22}
23
24/// Deallocate the memory block of the specified size starting at `address`.
25///
26/// Deallocates *all* the pages that overlap the memory block.
27pub unsafe fn free(address: *mut u8, size: u64) {
28	c::free(address as u64, size)
29}
30
31/// Console stream type.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum ConsoleStream {
34	/// Standard output stream.
35	Stdout = 1,
36	/// Standard error stream.
37	Stderr = 2,
38}
39
40/// Append `text` to the specified _console_ output stream.
41pub fn yield_console_data(stream: ConsoleStream, text: &str) {
42	let bytes = text.as_bytes();
43	let len = bytes.len() as u64;
44	unsafe { c::yield_console_data(stream as u64, bytes.as_ptr() as u64, len) };
45}
46
47/// Append video frame to the _video_ output stream.
48///
49/// Frame format is defined by [`video_mode`](crate::video_mode).
50pub fn yield_video_frame<S: AsRef<[T]>, T: Sized>(frame_number: u64, frame: S) {
51	let frame = frame.as_ref();
52	let address = frame.as_ptr() as u64;
53	let len = size_of_val(frame) as u64;
54	unsafe { c::yield_video_frame(frame_number, address, len) };
55}
56
57/// Set video monitor output mode to `mode`.
58pub fn video_mode(mode: &VideoMode) {
59	unsafe {
60		c::video_mode(
61			mode.width as u64,
62			mode.height as u64,
63			mode.refresh_rate as u64,
64			mode.format as u64,
65		)
66	}
67}
68
69/// Video monitor output mode.
70pub struct VideoMode {
71	/// Frame width.
72	pub width: u32,
73	/// Frame height.
74	pub height: u32,
75	/// Monitor refresh rate (frames per second).
76	pub refresh_rate: u16,
77	/// Frame encoding.
78	pub format: VideoFrameFormat,
79}
80
81/// The format of the frames emitted via [`yield_video_frame`].
82#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
83pub enum VideoFrameFormat {
84	/// Indexed RGB colors.
85	///
86	/// Format: `[ has_palette(1) | palette(256*3) | frame(width*height) ]`.
87	///
88	/// Each frame starts with `has_palette` flag that defines whether the pallete is present or
89	/// not. `0` means the palette is not present and one should use the palette from the previous
90	/// frames. It is an error to transmit the first frame without a palette. `1` means the palette
91	/// is present.
92	///
93	/// The pallete has 256 colors and each colors has 3 components. Each color in the
94	/// palette is encoded as a sequence of red, green, blue components, each component occupies 8
95	/// bits.
96	///
97	/// The frame itself goes after the pallete. It is encoded as a sequence of 8-bit indices into
98	/// the palette in row-major order, there are `width * height` indices in total.
99	Rgb888Indexed8 = 1,
100}