Added loader

This commit is contained in:
Samy Solhi 2023-02-15 17:20:10 +01:00
parent b5690a7f57
commit 6507b601e7
2 changed files with 34 additions and 1 deletions

33
src/simulator/loader.rs Normal file
View 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
}

View File

@ -2,7 +2,7 @@ pub mod machine;
pub mod decode;
pub mod print;
pub mod mem_cmp;
pub mod loader;
pub mod global {