Implementation of the UM-32 "Universal Machine" as described by the Cult of the Bound Variable

move `Page` to `reg` module & create conversions module

tjh 6117c566 3ed9dee0

Changed files
+47 -44
src
+19
src/conv.rs
···
··· 1 + 2 + const WORD_LEN: usize = std::mem::size_of::<u32>(); 3 + 4 + #[derive(Debug)] 5 + pub struct InvalidProgram; 6 + 7 + /// Converts a byte slice to a program. 8 + /// 9 + /// Returns `None` if the byte slice is not a multiple of 4 bytes in length. 10 + pub fn bytes_to_program(bytes: &[u8]) -> Result<Vec<u32>, InvalidProgram> { 11 + if bytes.len().rem_euclid(WORD_LEN) != 0 { 12 + return Err(InvalidProgram); 13 + } 14 + 15 + Ok(bytes 16 + .chunks_exact(WORD_LEN) 17 + .map(|word| u32::from_be_bytes(word.try_into().unwrap())) 18 + .collect()) 19 + }
+3 -44
src/lib.rs
··· 2 use std::io::{Read, Write}; 3 4 pub mod asm; 5 pub mod ops; 6 pub mod reg; 7 8 use ops::Operation; 9 - use reg::Register; 10 - 11 - const WORD_LEN: usize = std::mem::size_of::<u32>(); 12 - 13 - /// A set of registers. 14 - #[derive(Debug, Default)] 15 - struct Page([u32; 8]); 16 - 17 - impl std::ops::Index<Register> for Page { 18 - type Output = u32; 19 - #[inline(always)] 20 - fn index(&self, index: Register) -> &Self::Output { 21 - &self.0[index as usize] 22 - } 23 - } 24 25 - impl std::ops::IndexMut<Register> for Page { 26 - #[inline(always)] 27 - fn index_mut(&mut self, index: Register) -> &mut Self::Output { 28 - &mut self.0[index as usize] 29 - } 30 - } 31 - 32 - impl From<[u32; 8]> for Page { 33 - fn from(value: [u32; 8]) -> Self { 34 - Self(value) 35 - } 36 - } 37 - 38 - #[derive(Debug)] 39 - pub struct InvalidProgram; 40 - 41 - /// Converts a byte slice to a program. 42 - /// 43 - /// Returns `None` if the byte slice is not a multiple of 4 bytes in length. 44 - pub fn bytes_to_program(bytes: &[u8]) -> Result<Vec<u32>, InvalidProgram> { 45 - if bytes.len().rem_euclid(WORD_LEN) != 0 { 46 - return Err(InvalidProgram); 47 - } 48 - 49 - Ok(bytes 50 - .chunks_exact(WORD_LEN) 51 - .map(|word| u32::from_be_bytes(word.try_into().unwrap())) 52 - .collect()) 53 - } 54 55 const SMALLVEC_SIZE: usize = 24; 56
··· 2 use std::io::{Read, Write}; 3 4 pub mod asm; 5 + mod conv; 6 pub mod ops; 7 pub mod reg; 8 9 use ops::Operation; 10 + use reg::{Page, Register}; 11 12 + pub use conv::*; 13 14 const SMALLVEC_SIZE: usize = 24; 15
+25
src/reg.rs
··· 59 } 60 } 61 }
··· 59 } 60 } 61 } 62 + 63 + /// A set of registers. 64 + #[derive(Debug, Default)] 65 + pub struct Page([u32; 8]); 66 + 67 + impl std::ops::Index<Register> for Page { 68 + type Output = u32; 69 + #[inline(always)] 70 + fn index(&self, index: Register) -> &Self::Output { 71 + &self.0[index as usize] 72 + } 73 + } 74 + 75 + impl std::ops::IndexMut<Register> for Page { 76 + #[inline(always)] 77 + fn index_mut(&mut self, index: Register) -> &mut Self::Output { 78 + &mut self.0[index as usize] 79 + } 80 + } 81 + 82 + impl From<[u32; 8]> for Page { 83 + fn from(value: [u32; 8]) -> Self { 84 + Self(value) 85 + } 86 + }