From 9fab99e31f39d73df97ddd4f5e4ef4a9e58051da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Rativel?= Date: Wed, 15 Feb 2023 16:14:27 +0100 Subject: [PATCH 1/2] make struct and function public --- src/simulator/mem_cmp.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index abddae9..3e2ccd6 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -82,7 +82,7 @@ impl Section{ /* * Representation de l'etat de la mémoire (apres execution.... a confirmer), sous forme de sections */ -struct Mem_Checker{ +pub struct Mem_Checker{ pc: usize, sp: usize, sections: Vec
, @@ -91,7 +91,7 @@ struct Mem_Checker{ impl Mem_Checker{ - fn from(path: &String) -> Mem_Checker { + pub fn from(path: &String) -> Mem_Checker { let file = fs::File::open("test_file_section.txt").expect("Wrong filename"); let reader = io::BufReader::new(file); @@ -151,7 +151,7 @@ impl Mem_Checker{ } - fn fill_memory_from_Mem_Checker(m_c: &Mem_Checker, machine: &mut Machine){ + pub fn fill_memory_from_Mem_Checker(m_c: &Mem_Checker, machine: &mut Machine){ machine.sp = m_c.sp; machine.pc = m_c.pc as u64; From 6507b601e7e2d94ffea055e90ec2292cd1fc11c0 Mon Sep 17 00:00:00 2001 From: Samy Solhi Date: Wed, 15 Feb 2023 17:20:10 +0100 Subject: [PATCH 2/2] 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 {