From 6117c5669cee4602c4ac90d2aef48c04a8be280c Mon Sep 17 00:00:00 2001 From: tjh <14987462+thomhayward@users.noreply.github.com> Date: Thu, 30 Jan 2025 10:51:18 +0000 Subject: [PATCH] move `Page` to `reg` module & create conversions module --- src/conv.rs | 19 +++++++++++++++++++ src/lib.rs | 47 +++-------------------------------------------- src/reg.rs | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 44 deletions(-) create mode 100644 src/conv.rs diff --git a/src/conv.rs b/src/conv.rs new file mode 100644 index 0000000..9d9eca1 --- /dev/null +++ b/src/conv.rs @@ -0,0 +1,19 @@ + +const WORD_LEN: usize = std::mem::size_of::(); + +#[derive(Debug)] +pub struct InvalidProgram; + +/// Converts a byte slice to a program. +/// +/// Returns `None` if the byte slice is not a multiple of 4 bytes in length. +pub fn bytes_to_program(bytes: &[u8]) -> Result, InvalidProgram> { + if bytes.len().rem_euclid(WORD_LEN) != 0 { + return Err(InvalidProgram); + } + + Ok(bytes + .chunks_exact(WORD_LEN) + .map(|word| u32::from_be_bytes(word.try_into().unwrap())) + .collect()) +} diff --git a/src/lib.rs b/src/lib.rs index 68218f7..c991d27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,55 +2,14 @@ use smallvec::SmallVec; use std::io::{Read, Write}; pub mod asm; +mod conv; pub mod ops; pub mod reg; use ops::Operation; -use reg::Register; +use reg::{Page, Register}; -const WORD_LEN: usize = std::mem::size_of::(); - -/// A set of registers. -#[derive(Debug, Default)] -struct Page([u32; 8]); - -impl std::ops::Index for Page { - type Output = u32; - #[inline(always)] - fn index(&self, index: Register) -> &Self::Output { - &self.0[index as usize] - } -} - -impl std::ops::IndexMut for Page { - #[inline(always)] - fn index_mut(&mut self, index: Register) -> &mut Self::Output { - &mut self.0[index as usize] - } -} - -impl From<[u32; 8]> for Page { - fn from(value: [u32; 8]) -> Self { - Self(value) - } -} - -#[derive(Debug)] -pub struct InvalidProgram; - -/// Converts a byte slice to a program. -/// -/// Returns `None` if the byte slice is not a multiple of 4 bytes in length. -pub fn bytes_to_program(bytes: &[u8]) -> Result, InvalidProgram> { - if bytes.len().rem_euclid(WORD_LEN) != 0 { - return Err(InvalidProgram); - } - - Ok(bytes - .chunks_exact(WORD_LEN) - .map(|word| u32::from_be_bytes(word.try_into().unwrap())) - .collect()) -} +pub use conv::*; const SMALLVEC_SIZE: usize = 24; diff --git a/src/reg.rs b/src/reg.rs index 8c508f9..ad48994 100644 --- a/src/reg.rs +++ b/src/reg.rs @@ -59,3 +59,28 @@ impl Register { } } } + +/// A set of registers. +#[derive(Debug, Default)] +pub struct Page([u32; 8]); + +impl std::ops::Index for Page { + type Output = u32; + #[inline(always)] + fn index(&self, index: Register) -> &Self::Output { + &self.0[index as usize] + } +} + +impl std::ops::IndexMut for Page { + #[inline(always)] + fn index_mut(&mut self, index: Register) -> &mut Self::Output { + &mut self.0[index as usize] + } +} + +impl From<[u32; 8]> for Page { + fn from(value: [u32; 8]) -> Self { + Self(value) + } +} -- 2.51.2