From b9bd878207c6b784694c15675a09636d112242d7 Mon Sep 17 00:00:00 2001
From: David Li
Date: Mon, 4 Jan 2016 11:09:51 -0700
Subject: Implement initializing memory from text/data segments
---
src/memory.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
(limited to 'src/memory.rs')
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 {
--
cgit v1.2.3