add emulation HAL for cramium target

this is where we'll try to aggregate all the shims that allow
the target to work on hosted mode
This commit is contained in:
bunnie 2025-02-18 23:22:09 +08:00
parent 14bfa42aec
commit 3afb01b04c
4 changed files with 97 additions and 0 deletions

View file

@ -0,0 +1,11 @@
[package]
name = "cramium-emu"
version = "0.1.0"
edition = "2021"
[dependencies]
xous = "0.9.64"
xous-names = { package = "xous-api-names", version = "0.9.65" }
"ux-api" = { path = "../ux-api" }
"rand" = "0.8"
rand_core = "0.6.4"

View file

@ -0,0 +1,29 @@
use ux_api::minigfx::{ColorNative, FrameBuffer, Point};
pub const COLUMN: isize = 128;
pub const ROW: isize = 128;
pub const PAGE: u8 = ROW as u8 / 8;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct MonoColor(ColorNative);
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Mono {
Black,
White,
}
impl From<ColorNative> for Mono {
fn from(value: ColorNative) -> Self {
match value.0 {
0 => Mono::Black,
_ => Mono::White,
}
}
}
impl Into<ColorNative> for Mono {
fn into(self) -> ColorNative {
match self {
Mono::Black => ColorNative::from(0),
Mono::White => ColorNative::from(1),
}
}
}

View file

@ -0,0 +1,2 @@
pub mod display;
pub mod trng;

View file

@ -0,0 +1,55 @@
use rand::rngs::ThreadRng;
use rand::{CryptoRng, Rng, RngCore};
#[derive(Debug)]
pub struct Trng {}
impl Trng {
pub fn new(_xns: &xous_names::XousNames) -> Result<Self, xous::Error> { Ok(Trng {}) }
fn reseed(&self) {}
pub fn get_u32(&self) -> Result<u32, xous::Error> { Ok(rand::thread_rng().gen()) }
pub fn get_u64(&self) -> Result<u64, xous::Error> { Ok(rand::thread_rng().gen()) }
pub fn fill_buf(&self, data: &mut [u32]) -> Result<(), xous::Error> {
for d in data.iter_mut() {
*d = rand::thread_rng().gen();
}
Ok(())
}
/// This is copied out of the 0.5 API for rand_core
pub fn fill_bytes_via_next(&mut self, dest: &mut [u8]) {
use core::mem::transmute;
let mut left = dest;
while left.len() >= 8 {
let (l, r) = { left }.split_at_mut(8);
left = r;
let chunk: [u8; 8] = unsafe { transmute(rand::thread_rng().next_u64().to_le()) };
l.copy_from_slice(&chunk);
}
let n = left.len();
if n > 4 {
let chunk: [u8; 8] = unsafe { transmute(rand::thread_rng().next_u64().to_le()) };
left.copy_from_slice(&chunk[..n]);
} else if n > 0 {
let chunk: [u8; 4] = unsafe { transmute(rand::thread_rng().next_u32().to_le()) };
left.copy_from_slice(&chunk[..n]);
}
}
}
impl RngCore for Trng {
fn next_u32(&mut self) -> u32 { rand::thread_rng().gen() }
fn next_u64(&mut self) -> u64 { rand::thread_rng().gen() }
fn fill_bytes(&mut self, dest: &mut [u8]) { self.fill_bytes_via_next(dest); }
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
Ok(self.fill_bytes(dest))
}
}
impl CryptoRng for Trng {}