aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2016-01-04 13:43:41 -0700
committerDavid Li <li.davidm96@gmail.com>2016-01-04 13:43:41 -0700
commitf71937d4bb74ad0ba8937da94643ddfe403ea346 (patch)
treee28b08a9a3bdf0d356f13e4cfdab9de67e98a37d /src
parentb9bd878207c6b784694c15675a09636d112242d7 (diff)
Load ELF directly
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs55
-rw-r--r--src/rust-elfloader/src/lib.rs4
-rw-r--r--src/simulator.rs8
3 files changed, 29 insertions, 38 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 1a176fd..c3fa51b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -21,29 +21,11 @@ pub mod memory;
pub mod simulator;
#[test]
-fn it_works() {
- use std::rc::Rc;
- use std::cell::RefCell;
-
- use std::path::Path;
- match binary::Binary::new_from_hex_file(Path::new("../riscv/kernel.hex")) {
- Ok(b) => {
- let mmu = memory::IdentityMmu::new();
- let memory = memory::Memory::new_from_binary(0x2000, b);
- let memory_ref = Rc::new(RefCell::new(Box::new(memory) as Box<memory::MemoryInterface>));
- let cache = Rc::new( RefCell::new( Box::new( memory::DirectMappedCache::new(4, 4, memory_ref.clone())) as Box<memory::MemoryInterface>) );
- let core = simulator::Core::new(cache.clone(), Box::new(mmu));
- let mut simulator = simulator::Simulator::new(vec![core], memory_ref.clone());
- simulator.run();
- },
- Err(err) => println!("Error: {:?}", err),
- }
-}
-
-#[test]
fn test_elfloader() {
use std::io::prelude::*;
use std::fs::File;
+ use std::rc::Rc;
+ use std::cell::RefCell;
extern crate elfloader;
let mut f = File::open("../riscv/kernel").unwrap();
@@ -52,23 +34,32 @@ fn test_elfloader() {
f.read_to_end(&mut buffer).unwrap();
let elf = elfloader::ElfBinary::new("test", &buffer).unwrap();
- println!("HEADERS");
- for p in elf.program_headers() {
- println!("{}", p);
- }
+ let start = elf.file_header().entry as isa::Address;
+
+ let mut text = None;
+ let mut data = None;
for p in elf.section_headers() {
- println!("{}", p);
if p.name.0 == 0x1b {
- let data = elf.section_data(p);
- print!("\t");
- for x in data[0..8].iter() {
- print!("{:02x}", x);
- }
- println!("");
+ text = Some((elf.section_data(p), p.addr));
+ }
+ else if p.name.0 == 0x33 {
+ data = Some((elf.section_data(p), p.addr));
}
}
- println!("{:?}", elf);
+ let (text, text_offset) = text.unwrap();
+ let (data, data_offset) = data.unwrap();
+
+ let mmu = memory::IdentityMmu::new();
+ let memory = memory::Memory::new_from_text_and_data(
+ 0x8000,
+ text, text_offset as usize,
+ data, data_offset as usize);
+ let memory_ref = Rc::new(RefCell::new(Box::new(memory) as Box<memory::MemoryInterface>));
+ let cache = Rc::new( RefCell::new( Box::new( memory::DirectMappedCache::new(4, 4, memory_ref.clone())) as Box<memory::MemoryInterface>) );
+ let core = simulator::Core::new(start, cache.clone(), Box::new(mmu));
+ let mut simulator = simulator::Simulator::new(vec![core], memory_ref.clone());
+ simulator.run();
}
#[cfg(test)]
diff --git a/src/rust-elfloader/src/lib.rs b/src/rust-elfloader/src/lib.rs
index e344bcb..92cae24 100644
--- a/src/rust-elfloader/src/lib.rs
+++ b/src/rust-elfloader/src/lib.rs
@@ -58,6 +58,10 @@ impl<'s> ElfBinary<'s> {
None
}
+ pub fn file_header(&self) -> &'s elf::FileHeader {
+ self.header
+ }
+
/// Print the program headers.
pub fn print_program_headers(&self) {
for p in self.program_headers() {
diff --git a/src/simulator.rs b/src/simulator.rs
index f92d3d2..571fafe 100644
--- a/src/simulator.rs
+++ b/src/simulator.rs
@@ -75,9 +75,9 @@ impl RegisterFile {
impl<'a> Core<'a> {
// TODO: take Rc<RefCell<>> to Memory as well?
- pub fn new(cache: SharedMemory<'a>, mmu: Box<Mmu + 'a>) -> Core<'a> {
+ pub fn new(entry: isa::Address, cache: SharedMemory<'a>, mmu: Box<Mmu + 'a>) -> Core<'a> {
Core {
- pc: 0x1002c, // TODO: hardcoded: fix later
+ pc: entry,
registers: RegisterFile::new(),
stall: 0,
running: true,
@@ -361,10 +361,6 @@ impl<'a> Simulator<'a> {
}
pub fn run(&mut self) {
- // hardcode _start
- self.cores[0].pc = 0x1002C;
- // hardcode GP
- self.cores[0].registers.write_word(isa::Register::X3, 0x108D0);
// hardcode SP
self.cores[0].registers.write_word(isa::Register::X2, 0x7FFC);
let mut total_cycles = 0;