Added loader
This commit is contained in:
parent
b5690a7f57
commit
6507b601e7
33
src/simulator/loader.rs
Normal file
33
src/simulator/loader.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use crate::Machine;
|
||||
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::io::BufRead;
|
||||
|
||||
|
||||
|
||||
/// Load a file into a new machine
|
||||
///
|
||||
/// `panic!` when size is not 1, 2, 4 or 8
|
||||
/// `panic!` when the text does not represents instructions in hexadecimal
|
||||
///
|
||||
/// ### Parameters
|
||||
///
|
||||
/// - **path** the path of the file to load
|
||||
/// - **size** the number of bytes to write (1, 2, 4 or 8)
|
||||
pub fn load(path : &str, instruction_size: i32) -> Machine {
|
||||
let file = fs::File::open(path).expect("Wrong filename");
|
||||
let reader = io::BufReader::new(file);
|
||||
let mut machine = Machine::_init_machine();
|
||||
|
||||
for (i,line) in reader.lines().enumerate() {
|
||||
let res = u64::from_str_radix(&line.unwrap(), 16);
|
||||
match res {
|
||||
Ok(value) => {
|
||||
Machine::write_memory(&mut machine, instruction_size, i, value);
|
||||
},
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
machine
|
||||
}
|
@ -2,7 +2,7 @@ pub mod machine;
|
||||
pub mod decode;
|
||||
pub mod print;
|
||||
pub mod mem_cmp;
|
||||
|
||||
pub mod loader;
|
||||
|
||||
pub mod global {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user