aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2016-01-06 12:16:13 -0700
committerDavid Li <li.davidm96@gmail.com>2016-01-06 12:16:13 -0700
commitfbbb16d448a92251779eb6b2e255b05e69bb3868 (patch)
tree303e66a2406e84ffa06becd55c7886a15607e43e
parent32428a6bbdb43d8c8948ddcfe66db637219f3d03 (diff)
Keep track of bytes written with proxy memory
-rw-r--r--Cargo.lock4
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs85
3 files changed, 84 insertions, 7 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5ad6d68..c98941d 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,7 +4,7 @@ version = "0.1.0"
dependencies = [
"docopt 0.6.78 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
- "rustv 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rustv 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -65,7 +65,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustv"
-version = "0.2.1"
+version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"elfloader32 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index f22ebb2..e9c924a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,6 @@ version = "0.1.0"
authors = ["David Li <li.davidm96@gmail.com>"]
[dependencies]
-rustv = "0.2.1"
+rustv = "0.2.4"
docopt = "0.6"
rustc-serialize = "0.3"
diff --git a/src/main.rs b/src/main.rs
index 15b4fba..e91db5f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,6 +12,8 @@ use docopt::Docopt;
use rustv::elfloader;
use rustv::isa;
use rustv::memory;
+use rustv::memory::MemoryInterface;
+use rustv::memory::Mmu;
use rustv::simulator;
const USAGE: &'static str = "
@@ -31,6 +33,69 @@ struct Args {
arg_program2: String,
}
+struct MemoryTracker {
+ memory: memory::Memory,
+ program1_byte: u8,
+ program2_byte: u8,
+ program1: usize,
+ program2: usize,
+}
+
+impl memory::MemoryInterface for MemoryTracker {
+ fn latency(&self) -> u32 {
+ self.memory.latency()
+ }
+
+ fn step(&mut self) {
+ self.memory.step();
+ }
+
+ fn read_word(&mut self, address: isa::Address) -> memory::Result<isa::Word> {
+ self.memory.read_word(address)
+ }
+
+ fn write_word(&mut self, address: isa::Address, value: isa::Word) -> memory::Result<()> {
+ let original = self.memory.read_word(address);
+ let result = self.memory.write_word(address, value);
+
+ if let Ok(original) = original {
+ if let Ok(()) = result {
+ let p1b = self.program1_byte as u32;
+ let mut p1orig = 0;
+ let mut p1new = 0;
+ if original & 0xFF == p1b { p1orig += 1; }
+ if (original >> 8) & 0xFF == p1b { p1orig += 1; }
+ if (original >> 8) & 0xFF == p1b { p1orig += 1; }
+ if (original >> 8) & 0xFF == p1b { p1orig += 1; }
+
+ if value & 0xFF == p1b { p1new += 1; }
+ if (value >> 8) & 0xFF == p1b { p1new += 1; }
+ if (value >> 8) & 0xFF == p1b { p1new += 1; }
+ if (value >> 8) & 0xFF == p1b { p1new += 1; }
+
+ self.program1 += p1new - p1orig;
+
+ let p2b = self.program2_byte as u32;
+ let mut p2orig = 0;
+ let mut p2new = 0;
+ if original & 0xFF == p2b { p2orig += 1; }
+ if (original >> 8) & 0xFF == p2b { p2orig += 1; }
+ if (original >> 8) & 0xFF == p2b { p2orig += 1; }
+ if (original >> 8) & 0xFF == p2b { p2orig += 1; }
+
+ if value & 0xFF == p2b { p2new += 1; }
+ if (value >> 8) & 0xFF == p2b { p2new += 1; }
+ if (value >> 8) & 0xFF == p2b { p2new += 1; }
+ if (value >> 8) & 0xFF == p2b { p2new += 1; }
+
+ self.program2 += p2new - p2orig;
+ }
+ }
+
+ result
+ }
+}
+
fn load_program<T: memory::Mmu>(memory: &mut memory::Memory,
mmu: &T, path: &str) -> isa::Address {
let mut f = File::open(path).unwrap();
@@ -73,11 +138,17 @@ fn main() {
let start1 = load_program(&mut memory, &mmu, &args.arg_program1);
let start2 = load_program(&mut memory, &mmu2, &args.arg_program2);
- let memory_box = Box::new(memory) as Box<memory::MemoryInterface>;
- let memory_ref = Rc::new(RefCell::new(memory_box));
+ let mut memory = MemoryTracker {
+ memory: memory,
+ program1_byte: 0x43,
+ program2_byte: 0x42,
+ program1: 0,
+ program2: 0,
+ };
+
+ let memory_ref = Rc::new(RefCell::new(memory));
let cache = memory::DirectMappedCache::new(4, 4, memory_ref.clone());
- let cache_box = Box::new(cache) as Box<memory::MemoryInterface>;
- let cache_ref = Rc::new(RefCell::new(cache_box));
+ let cache_ref = Rc::new(RefCell::new(cache));
let core = simulator::Core::new(
start1, 0x1000,
cache_ref.clone(), Box::new(mmu));
@@ -86,5 +157,11 @@ fn main() {
cache_ref.clone(), Box::new(mmu2));
let cores = vec![core, core2];
let mut simulator = simulator::Simulator::new(cores, memory_ref.clone());
+
+ println!("{:?}", memory_ref.borrow_mut().read_instruction((start1)));
+
simulator.run();
+
+ println!("Program 1 bytes written: {}", memory_ref.borrow().program1);
+ println!("Program 2 bytes written: {}", memory_ref.borrow().program2);
}