Line data Source code
1 : //! Definitions for components that can preform memory read or write operations 2 : //! from an memory address. 3 : 4 : /// A component that can either read or write a byte from a memory address. 5 : pub trait MemoryAccessByte<T> { 6 : /// Returns a byte from the memory address. 7 : fn read_byte(addr: u32, state: &T) -> u8; 8 : /// Writes a byte to the memory address. 9 : fn write_byte(addr: u32, data: u8, state: &mut T); 10 : } 11 : 12 : /// A component that can either read or write a halfword from a memory address. 13 : pub trait MemoryAccessHalfword<T> { 14 : /// Returns a halfword from the memory address. 15 : fn read_halfword(addr: u32, state: &T) -> u16; 16 : /// Writes a halfword to the memory address. 17 : fn write_halfword(addr: u32, data: u16, state: &mut T); 18 : } 19 : 20 : /// A component that can either read or write a word from a memory address. 21 : pub trait MemoryAccessWord<T> { 22 : /// Returns a word from the memory address. 23 : fn read_word(addr: u32, state: &T) -> u32; 24 : /// Writes a word to the memory address. 25 : fn write_word(addr: u32, data: u32, state: &mut T); 26 : } 27 : 28 : /// Writes a halfword to the memory array at the specified offset in little 29 : /// endian order. 30 0 : pub fn write_little_endian_halfword(mem: &mut [u8], offset: usize, data: u16) { 31 0 : let byte_0 = (data & 0xFF) as u8; 32 0 : let byte_1 = ((data >> 8) & 0xFF) as u8; 33 0 : 34 0 : mem[offset] = byte_0; 35 0 : mem[offset + 1] = byte_1; 36 0 : } 37 : 38 : /// Writes a word to the memory array at the specified offset in little endian 39 : /// order. 40 0 : pub fn write_little_endian_word(mem: &mut [u8], offset: usize, data: u32) { 41 0 : let byte_0 = (data & 0xFF) as u8; 42 0 : let byte_1 = ((data >> 8) & 0xFF) as u8; 43 0 : let byte_2 = ((data >> 16) & 0xFF) as u8; 44 0 : let byte_3 = ((data >> 24) & 0xFF) as u8; 45 0 : 46 0 : mem[offset] = byte_0; 47 0 : mem[offset + 1] = byte_1; 48 0 : mem[offset + 2] = byte_2; 49 0 : mem[offset + 3] = byte_3; 50 0 : }