mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
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:
parent
14bfa42aec
commit
3afb01b04c
4 changed files with 97 additions and 0 deletions
11
libs/cramium-emu/Cargo.toml
Normal file
11
libs/cramium-emu/Cargo.toml
Normal 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"
|
||||||
29
libs/cramium-emu/src/display.rs
Normal file
29
libs/cramium-emu/src/display.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2
libs/cramium-emu/src/lib.rs
Normal file
2
libs/cramium-emu/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod display;
|
||||||
|
pub mod trng;
|
||||||
55
libs/cramium-emu/src/trng.rs
Normal file
55
libs/cramium-emu/src/trng.rs
Normal 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 {}
|
||||||
Loading…
Reference in a new issue