diff options
author | David Li <li.davidm96@gmail.com> | 2016-01-14 15:56:26 -0700 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2016-01-14 15:56:26 -0700 |
commit | 4567214f23a63b4e4964433f4034240cc4fa6a4b (patch) | |
tree | d9aa350fb95922abfad85e3345ee28c90d438aaa | |
parent | e324acc29f839eccf976906720c1e7796a9adb49 (diff) |
Add as_bytes to IsaType
-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 + } } } } |