From 6507b601e7e2d94ffea055e90ec2292cd1fc11c0 Mon Sep 17 00:00:00 2001 From: Samy Solhi Date: Wed, 15 Feb 2023 17:20:10 +0100 Subject: [PATCH] Added loader --- src/simulator/loader.rs | 33 +++++++++++++++++++++++++++++++++ src/simulator/mod.rs | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/simulator/loader.rs diff --git a/src/simulator/loader.rs b/src/simulator/loader.rs new file mode 100644 index 0000000..0391aed --- /dev/null +++ b/src/simulator/loader.rs @@ -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 +} \ No newline at end of file diff --git a/src/simulator/mod.rs b/src/simulator/mod.rs index 54d271e..6ee59e2 100644 --- a/src/simulator/mod.rs +++ b/src/simulator/mod.rs @@ -2,7 +2,7 @@ pub mod machine; pub mod decode; pub mod print; pub mod mem_cmp; - +pub mod loader; pub mod global {