From 4567214f23a63b4e4964433f4034240cc4fa6a4b Mon Sep 17 00:00:00 2001 From: David Li Date: Thu, 14 Jan 2016 15:56:26 -0700 Subject: Add as_bytes to IsaType --- src/isa/mod.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/isa') 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; } 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 { + 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 { + 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 + } } } } -- cgit v1.2.3