diff options
-rw-r--r-- | src/isa/mod.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/isa/mod.rs b/src/isa/mod.rs index 8d86610..6c4bfbc 100644 --- a/src/isa/mod.rs +++ b/src/isa/mod.rs @@ -115,6 +115,9 @@ pub trait IsaType { fn as_half_word(self) -> HalfWord; fn as_byte(self) -> Byte; fn as_address(self) -> Address; + + /// Converts the type into bytes, LSB-first. + fn as_bytes(self) -> Vec<Byte>; } macro_rules! isa_utype { @@ -148,6 +151,17 @@ macro_rules! isa_utype { fn as_address(self) -> Address { self.as_word() } + + fn as_bytes(self) -> Vec<Byte> { + use std::mem; + + let mut bytes = vec![]; + for offset in 0..mem::size_of::<$utype>() { + bytes.push(Byte(((self.0 >> (8 * offset)) & 0xFF) as u8)); + } + + bytes + } } impl IsaType for $signed { @@ -177,6 +191,17 @@ macro_rules! isa_utype { fn as_address(self) -> Address { self.as_word() } + + fn as_bytes(self) -> Vec<Byte> { + use std::mem; + + let mut bytes = vec![]; + for offset in 0..mem::size_of::<$utype>() { + bytes.push(Byte(((self.0 >> (8 * offset)) & 0xFF) as u8)); + } + + bytes + } } } } |