diff options
author | David Li <li.davidm96@gmail.com> | 2016-01-04 11:09:51 -0700 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2016-01-04 11:09:51 -0700 |
commit | b9bd878207c6b784694c15675a09636d112242d7 (patch) | |
tree | 46ee80da19c49a951eef986562c068e147e0409f /src/memory.rs | |
parent | 1e93d4c727f632cd5d6cdc90e6d8a21883d6f409 (diff) |
Implement initializing memory from text/data segments
Diffstat (limited to 'src/memory.rs')
-rw-r--r-- | src/memory.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/memory.rs b/src/memory.rs index 92aefd5..db0dca4 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -157,6 +157,31 @@ pub struct DirectMappedCache<'a> { next_level: SharedMemory<'a>, } +fn copy_u8_into_u32(src: &[u8], dst: &mut [u32]) { + for (offset, word) in src.chunks(4).enumerate() { + let word = if word.len() == 4 { + (word[0] as u32) | + ((word[1] as u32) << 8) | + ((word[2] as u32) << 16) | + ((word[3] as u32) << 24) + } + else if word.len() == 3 { + (word[0] as u32) | + ((word[1] as u32) << 8) | + ((word[2] as u32) << 16) + } + else if word.len() == 2 { + (word[0] as u32) | + ((word[1] as u32) << 8) + } + else { + word[0] as u32 + }; + + dst[offset] = word; + } +} + impl Memory { pub fn new(size: isa::Address) -> Memory { Memory { @@ -175,6 +200,26 @@ impl Memory { memory: memory, } } + + pub fn new_from_text_and_data(size: usize, + text: &[u8], text_offset: usize, + data: &[u8], data_offset: usize) -> Memory { + let mut memory = vec![0; size]; + + { + let mut text_segment = &mut memory[(text_offset / 4)..size]; + copy_u8_into_u32(text, text_segment); + } + + { + let mut data_segment = &mut memory[(data_offset / 4)..size]; + copy_u8_into_u32(data, data_segment); + } + + Memory { + memory: memory, + } + } } impl MemoryInterface for Memory { |