Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gfx ioctl defines #17

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ authors = ["Jonathan 'theJPster' Pallant <[email protected]>"]
[dependencies]
neotron-ffi = "0.1"
neotron-api = "0.2"
neotron-common-bios = "0.12"

[target.'cfg(unix)'.dependencies]
crossterm = "0.26"
Expand Down
2 changes: 1 addition & 1 deletion samples/chello/neotron.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ typedef struct NeotronApi
*
* * You cannot rename a file if it is currently open.
* * You cannot rename a file where the `old_path` and the `new_path` are
* not on the same drive.
* not on the same drive.
* * Paths must confirm to the rules for the filesystem for the given drive.
*/
struct FfiResult_void (*rename)(struct FfiString old_path, struct FfiString new_path);
Expand Down
99 changes: 99 additions & 0 deletions src/ioctls/gfx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! Graphics ioctl constants

/// Clear the screen
///
/// The corresponding value is the fill colour, which is taken modulo the number of on-screen colours.
pub const COMMAND_CLEAR_SCREEN: u64 = 0;

/// Plot a chunky pixel (and move the cursor).
///
/// The command contains [ x | y | mode | colour ].
///
/// * `x` is 16 bits and marks the horizontal position (0 is left)
/// * `y` is 16 bits and marks the vertical position (0 is top)
/// * `mode` is 8 bits and is currently ignored
/// * `colour` is 24 bits, and is taken modulo the number of on-screen colours
///
/// Use [`chunky_plot_value`] to create a suitable value for this ioctl command.
pub const COMMAND_CHUNKY_PLOT: u64 = 1;

/// Change graphics mode
///
/// The command contains the video mode in the upper 32 bits and a pointer to a
/// framebuffer in the lower 32 bits.
///
/// The framebuffer pointer must point to a 32-bit aligned region of memory
/// that is large enough for the selected mode. If you pass `null`, then the OS
/// will attempt to allocate a framebuffer for you.
///
/// Use [`change_mode_value`] to construct a value.
pub const COMMAND_CHANGE_MODE: u64 = 2;

/// Move the cursor
///
/// The command contains [ x | y | <padding> ].
///
/// The graphics handle maintains a virtual cursor position. This is used for
/// plotting lines for example - you move the cursor to one end of the line, and
/// then issue a LINE_DRAW ioctl containing the other end of the line. This saves
/// having to try and pass two sets of co-ordinates within one ioctl.
///
/// Use [`move_cursor_value`] to construct a value.
pub const COMMAND_MOVE_CURSOR: u64 = 3;

/// Draw a line
///
/// The command contains [ x | y | mode | colour ].
///
/// * `x` is 16 bits and marks the final horizontal position (0 is left)
/// * `y` is 16 bits and marks the final vertical position (0 is top)
/// * `mode` is 8 bits and is currently ignored
/// * `colour` is 24 bits, and is taken modulo the number of on-screen colours
///
/// The start position is the cursor position. The cursor is updated to the final position.
///
/// Use [`draw_line_value`] to construct a value.
pub const COMMAND_DRAW_LINE: u64 = 4;

/// Set a palette entry
///
/// The command contains [ <padding> | II | RR | GG | BB ]
///
/// II, RR, GG and BB are 8-bit values where II is the index into the 256 long
/// palette, and RR, GG and BB are the 24-bit RGB colour for that index.
///
/// Use [`set_palette_value`] to construct a value.
pub const COMMAND_SET_PALETTE: u64 = 5;

/// Calculate a 64-bit value argument for the [`COMMAND_CHUNKY_PLOT`] gfx ioctl
pub fn chunky_plot_value(x: u16, y: u16, colour: u32) -> u64 {
(x as u64) << 48 | (y as u64) << 32 | (colour & 0xFFFFFF) as u64
}

/// Calculate a 64-bit value argument for the [`COMMAND_CHANGE_MODE`] gfx ioctl
pub fn change_mode_value(mode: crate::VideoMode, fb_ptr: *mut u32) -> u64 {
let fb_ptr = fb_ptr as usize as u64;
let mode = mode.as_u8() as u64;
mode << 32 | fb_ptr
}

/// Calculate a 64-bit value argument for the [`COMMAND_MOVE_CURSOR`] gfx ioctl
pub fn move_cursor_value(x: u16, y: u16) -> u64 {
(x as u64) << 48 | (y as u64) << 32
}

/// Calculate a 64-bit value argument for the [`COMMAND_DRAW_LINE`] gfx ioctl
pub fn draw_line_value(end_x: u16, end_y: u16, colour: u32) -> u64 {
(end_x as u64) << 48 | (end_y as u64) << 32 | (colour & 0xFFFFFF) as u64
}

/// Calculate a 64-bit value argument for the [`COMMAND_SET_PALETTE`] gfx ioctl
pub fn set_palette_value(index: u8, r: u8, g: u8, b: u8) -> u64 {
let mut result = (index as u64) << 24;
result |= (r as u64) << 16;
result |= (g as u64) << 8;
result |= b as u64;
result
}

// End of file
3 changes: 3 additions & 0 deletions src/ioctls/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! A collection of ioctl constants

pub mod gfx;
47 changes: 37 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@

pub use neotron_api::{file::Flags, path, Api, Error};

pub use neotron_common_bios::video::{Format as VideoFormat, Mode as VideoMode};

use neotron_api as api;

pub mod console;

pub mod ioctls;

#[cfg(not(target_os = "none"))]
mod fake_os_api;

Expand Down Expand Up @@ -66,7 +70,7 @@
static ARG_PTR: AtomicPtr<FfiString> = AtomicPtr::new(core::ptr::null_mut());

/// Random number generator state
static RAND_STATE: core::sync::atomic::AtomicU16 = core::sync::atomic::AtomicU16::new(0);
static RAND_STATE: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0);

// ============================================================================
// Types
Expand Down Expand Up @@ -189,7 +193,7 @@
///
/// * You cannot rename a file if it is currently open.
/// * You cannot rename a file where the `old_path` and the `new_path` are
/// not on the same drive.
/// not on the same drive.
/// * Paths must confirm to the rules for the filesystem for the given drive.
pub fn rename(old_path: path::Path, new_path: path::Path) -> Result<()> {
let api = get_api();
Expand All @@ -204,8 +208,14 @@

/// Perform a special I/O control operation.
///
/// The allowed values of `command` and `value` are TBD.
pub fn ioctl(&self, command: u64, value: u64) -> Result<u64> {
/// The allowed values of `command` and `value` are defined in the
/// [`ioctls`] module.
///
/// # Safety
///
/// Refer to the documentation for the ioctl you are using. Raw pointers may
/// be involved.
pub unsafe fn ioctl(&self, command: u64, value: u64) -> Result<u64> {
let api = get_api();
match (api.ioctl)(self.0, command, value) {
neotron_ffi::FfiResult::Ok(output) => Ok(output),
Expand Down Expand Up @@ -449,17 +459,34 @@
}

/// Seed the 16-bit psuedorandom number generator
pub fn srand(seed: u16) {
pub fn srand(seed: u32) {
RAND_STATE.store(seed, core::sync::atomic::Ordering::Relaxed);
}

/// Get a 16-bit psuedorandom number
pub fn rand() -> u16 {
/// Get a 32-bit psuedorandom number
pub fn rand() -> u32 {
let mut state = RAND_STATE.load(core::sync::atomic::Ordering::Relaxed);
let bit = (state ^ (state >> 2) ^ (state >> 3) ^ (state >> 5)) & 0x01;
state = (state >> 1) | (bit << 15);
state = state.wrapping_mul(1103515245).wrapping_add(12345);
let bits1 = state >> 16;
state = state.wrapping_mul(1103515245).wrapping_add(12345);
let bits2 = state >> 16;
RAND_STATE.store(state, core::sync::atomic::Ordering::Relaxed);
state
(bits1 << 16) | bits2
}

/// Get a random number from a range
pub fn random_in(range: core::ops::Range<u32>) -> u32 {
let count = range.end - range.start;
let bucket_size = u32::MAX / count;
let limit = bucket_size * count;
let rand_value = loop {
let temp = rand() as u32;

Check warning on line 483 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

casting to the same type is unnecessary (`u32` -> `u32`)

warning: casting to the same type is unnecessary (`u32` -> `u32`) --> src/lib.rs:483:20 | 483 | let temp = rand() as u32; | ^^^^^^^^^^^^^ help: try: `rand()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `#[warn(clippy::unnecessary_cast)]` on by default
if temp <= limit {
break temp;
}
};
let result = rand_value / bucket_size;
result + range.start
}

/// Get the API structure so we can call APIs manually.
Expand Down
Loading