diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 85 |
1 files changed, 81 insertions, 4 deletions
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); } |