33 lines
895 B
Rust
33 lines
895 B
Rust
|
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
|
||
|
}
|