burritos/src/simulator/loader.rs

34 lines
986 B
Rust
Raw Normal View History

2023-02-15 17:20:10 +01:00
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)
2023-03-10 11:03:54 +01:00
pub fn _load(path : &str, instruction_size: i32) -> Machine {
2023-02-15 17:20:10 +01:00
let file = fs::File::open(path).expect("Wrong filename");
let reader = io::BufReader::new(file);
let mut machine = Machine::init_machine();
2023-02-15 17:20:10 +01:00
for (i,line) in reader.lines().enumerate() {
let res = u64::from_str_radix(&line.unwrap(), 16);
match res {
Ok(value) => {
2023-03-01 15:11:35 +01:00
Machine::write_memory(&mut machine, instruction_size, i*instruction_size as usize, value);
2023-02-15 17:20:10 +01:00
},
_ => panic!()
}
}
2023-02-15 18:09:18 +01:00
println!("{:x}", Machine::read_memory(& mut machine, 4, 0));
2023-02-15 17:20:10 +01:00
machine
}