diff options
author | David Li <li.davidm96@gmail.com> | 2016-01-07 11:12:49 -0700 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2016-01-07 11:12:49 -0700 |
commit | 940a5ae77a1efc8f89fe5b56fe4a4ae34a0e480d (patch) | |
tree | 49978de652f60c0bacd4ac5e28dd1fe67e00f56c /src | |
parent | fbbb16d448a92251779eb6b2e255b05e69bb3868 (diff) |
Implement print system call
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index e91db5f..722efdb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,10 @@ use rustv::isa; use rustv::memory; use rustv::memory::MemoryInterface; use rustv::memory::Mmu; +use rustv::register_file; use rustv::simulator; +use rustv::syscall; +use rustv::trap; const USAGE: &'static str = " cacheracer - A F/OSS implementation of the CS 3410 CacheRacer @@ -96,6 +99,40 @@ impl memory::MemoryInterface for MemoryTracker { } } +struct SyscallHandler<'a> { + memory: memory::SharedMemory<'a>, +} + +impl<'a> syscall::SyscallHandler for SyscallHandler<'a> { + fn syscall(&mut self, registers: &mut register_file::RegisterFile) -> Option<trap::Trap> { + println!("Syscall number {}", registers.read_word(isa::Register::X10)); + let mut base = registers.read_word(isa::Register::X11); + let mut string = vec![]; + + loop { + let c = self.memory.borrow_mut().read_byte(base); + + if let Ok(0x00) = c { + break; + } + else if let Ok(c) = c { + string.push(c); + } + + base += 1; + } + + let result = std::str::from_utf8(&string); + if let Ok(string) = result { + println!("{}", string); + } + else { + println!("Error printing string: {:?}", result); + } + None + } +} + fn load_program<T: memory::Mmu>(memory: &mut memory::Memory, mmu: &T, path: &str) -> isa::Address { let mut f = File::open(path).unwrap(); @@ -106,23 +143,13 @@ fn load_program<T: memory::Mmu>(memory: &mut memory::Memory, let elf = elfloader::ElfBinary::new("test", &buffer).unwrap(); let start = elf.file_header().entry as isa::Address; - let mut text = None; - let mut data = None; for p in elf.section_headers() { - if p.name.0 == 0x1b { - text = Some((elf.section_data(p), p.addr)); - } - else if p.name.0 == 0x33 { - data = Some((elf.section_data(p), p.addr)); + let name = elf.section_name(p); + if name == ".text" || name == ".sdata" || name == ".rodata" { + memory.write_segment(mmu, elf.section_data(p), p.addr as usize); } } - let (text, text_offset) = text.unwrap(); - let (data, data_offset) = data.unwrap(); - - memory.write_segment(mmu, text, text_offset as usize); - memory.write_segment(mmu, data, data_offset as usize); - start } @@ -156,9 +183,9 @@ fn main() { start2, 0x1000, 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))); + let system = SyscallHandler { memory: memory_ref.clone(), }; + let mut simulator = simulator::Simulator::new( + cores, memory_ref.clone(), system); simulator.run(); |