diff options
| author | David Li <li.davidm96@gmail.com> | 2015-12-15 16:25:58 -0500 | 
|---|---|---|
| committer | David Li <li.davidm96@gmail.com> | 2015-12-15 16:25:58 -0500 | 
| commit | 98475b71bcf5a89c8c1c4c59a0c9f9ade74494f5 (patch) | |
| tree | b97cf27b7e7ed29831992880db1378b876e80f87 /src | |
| parent | 391dceacc280d6a339b36a85e4f0665da992b91e (diff) | |
Load hexdump of ELF
Diffstat (limited to 'src')
| -rw-r--r-- | src/binary.rs | 79 | ||||
| -rw-r--r-- | src/lib.rs | 7 | ||||
| -rw-r--r-- | src/simulator.rs | 8 | 
3 files changed, 81 insertions, 13 deletions
diff --git a/src/binary.rs b/src/binary.rs index 2564637..e1e6d45 100644 --- a/src/binary.rs +++ b/src/binary.rs @@ -1,15 +1,76 @@ -pub struct Binary<'a> { -    words: &'a [u32], +use std::error::Error; +use std::fmt; +use std::fs::File; +use std::io::{self, BufRead}; +use std::num; +use std::path::Path; +use std::str; + +pub struct Binary { +    words: Vec<u32>,  } -impl<'a> Binary<'a> { -    pub fn new(words: &'a [u32]) -> Binary<'a> { -        Binary { -            words: words, +#[derive(Debug)] +pub enum BinaryError { +    Io(io::Error), +    Utf8(str::Utf8Error), +    ParseInt(num::ParseIntError), +} + +impl fmt::Display for BinaryError { +    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { +        match *self { +            BinaryError::Io(ref err) => err.fmt(f), +            BinaryError::Utf8(ref err) => err.fmt(f), +            BinaryError::ParseInt(ref err) => err.fmt(f), +        } +    } +} + +impl Error for BinaryError { +    fn description(&self) -> &str { +        match *self { +            BinaryError::Io(ref err) => err.description(), +            BinaryError::Utf8(ref err) => err.description(), +            BinaryError::ParseInt(ref err) => err.description(),          }      } +} + +impl From<io::Error> for BinaryError { +    fn from(err: io::Error) -> BinaryError { +        BinaryError::Io(err) +    } +} -    // pub fn new_from_hex_file() -> Binary<'a> { -         -    // } +impl From<str::Utf8Error> for BinaryError { +    fn from(err: str::Utf8Error) -> BinaryError { +        BinaryError::Utf8(err) +    } +} + +impl From<num::ParseIntError> for BinaryError { +    fn from(err: num::ParseIntError) -> BinaryError { +        BinaryError::ParseInt(err) +    } +} + +impl Binary { +    pub fn new_from_hex_file(path: &Path) -> Result<Binary, BinaryError> { +        let file = try!(File::open(path)); +        let file = io::BufReader::new(file); + +        let mut words: Vec<u32> = Vec::new(); +        for line in file.lines() { +            let line = try!(line); +            for bytes in line.as_bytes().chunks(8).rev() { +                let word = try!(str::from_utf8(bytes)); +                words.push(try!(u32::from_str_radix(word, 16))); +            } +        } + +        Ok(Binary { +            words: words, +        }) +    }  } @@ -1,3 +1,4 @@ +#![feature(as_slice)]  mod isa;  mod binary;  mod memory; @@ -6,4 +7,10 @@ mod simulator;  #[test]  fn it_works() { +    use std::path::Path; +    println!("Test"); +    match binary::Binary::new_from_hex_file(Path::new("../riscv/kernel.hex")) { +        Ok(_) => println!("Ok"), +        Err(err) => println!("Error: {:?}", err), +    }  } diff --git a/src/simulator.rs b/src/simulator.rs index e965686..86ddfab 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -2,8 +2,8 @@ use isa;  use binary::{Binary};  use memory::{Memory}; -pub struct Simulator<'a> { -    binary: Binary<'a>, +pub struct Simulator { +    binary: Binary,      num_cores: usize,      memory: Memory,  } @@ -17,8 +17,8 @@ struct RegisterFile {      registers: [u32; 32],  } -impl<'a> Simulator<'a> { -    pub fn new(num_cores: usize, binary: Binary<'a>) -> Simulator<'a> { +impl Simulator { +    pub fn new(num_cores: usize, binary: Binary) -> Simulator {          Simulator {              binary: binary,              num_cores: num_cores,  | 
