From 5a90419ebb788da1f07d66af21afeff6cebca647 Mon Sep 17 00:00:00 2001 From: Baptiste Date: Wed, 1 Mar 2023 16:12:46 +0100 Subject: [PATCH] change memory to a vector, it can now be fill --- src/main.rs | 11 +++++++---- src/simulator/machine.rs | 24 +++++++++++++----------- src/simulator/mem_cmp.rs | 10 +++++----- test_file_section.txt | 19 ++----------------- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/main.rs b/src/main.rs index b245e37..6e4d2c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,14 @@ mod simulator; use simulator::machine::Machine; -use simulator::{mem_cmp, loader}; +use simulator::mem_cmp; fn main() { - let path = "test_file_section.txt".to_string(); - let mut m = loader::load(&path, 4); - Machine::print_memory(&mut m); + let mut m = Machine::_init_machine(); + let path = "memory.txt".to_string(); + let checker = mem_cmp::Mem_Checker::from(&path); + mem_cmp::Mem_Checker::fill_memory_from_Mem_Checker(&checker, &mut m); + mem_cmp::Mem_Checker::print_Mem_Checker(&checker); + Machine::print_memory(&mut m, 0x400000, 0x405000); Machine::run(m); } diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index e7af292..bd5617a 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -5,7 +5,7 @@ use super::global::*; use std::fs::File; /// doit disparaitre -const MEM_SIZE : usize = 4096; +const MEM_SIZE : usize = 0x500000; pub trait RegisterNum: Add + Sub + PartialEq + Copy {} @@ -65,7 +65,7 @@ pub struct Machine { pub sp: usize, pub int_reg : Register, pub fp_reg : Register, - pub main_memory : [u8 ; MEM_SIZE], + pub main_memory : Vec, pub shiftmask : [u64 ; 64] // futur taille à calculer int memSize = g_cfg->NumPhysPages * g_cfg->PageSize; //creer une struct cfg(configuration) qui s'initialise avec valeur dans un fichier cfg @@ -89,7 +89,7 @@ impl Machine { sp: 0, int_reg : Register::::init(), fp_reg : Register::::init(), - main_memory : [0 ; MEM_SIZE], + main_memory : vec![0; MEM_SIZE], shiftmask } @@ -180,17 +180,16 @@ impl Machine { uint64_t value;*/ if machine.main_memory.len() <= machine.pc as usize { - println!("ERROR : number max of instructions rushed"); - return ; + panic!("ERROR : number max of instructions rushed"); } - let mut val: [u8; 4] = [0; 4]; - for i in 0..4 { + let mut val: [u8; 8] = [0; 8]; + for i in 0..8 { val[i] = machine.main_memory[machine.pc as usize + i]; } - let val = u32::from_be_bytes(val); + let val = u64::from_be_bytes(val); println!("{:x}", val); - let inst : Instruction = decode(val as u64); + let inst : Instruction = decode(val); match inst.opcode { @@ -610,8 +609,11 @@ impl Machine { } - pub fn print_memory(machine : &mut Machine) { - for i in 0..MEM_SIZE { + /// print memory FOR DEBUG + /// + /// "@"adresse [16 bytes] + pub fn print_memory(machine : &mut Machine, from: usize, to: usize) { + for i in from..to { if i%16 == 0 { print!("\n@{:04x} ", i); } diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index 5c81029..e90fec7 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -106,7 +106,7 @@ impl Mem_Checker{ pub fn from(path: &String) -> Mem_Checker { - let file = fs::File::open("test_file_section.txt").expect("Wrong filename"); + let file = fs::File::open(path).expect("Wrong filename"); let reader = io::BufReader::new(file); let mut lines = reader.lines(); @@ -124,7 +124,7 @@ impl Mem_Checker{ let current_line = vector.get(i).unwrap_or(&default); //Lecture des sections - if current_line.contains(' ') { + if i % 2 == 0 { //lecture ligne ADDR LEN let next_word_index = current_line.find(' ').unwrap(); tmp_addr_str = String::from(¤t_line[0..next_word_index]); @@ -135,7 +135,7 @@ impl Mem_Checker{ let section_f = SectionFormat{ addr: tmp_addr_str.clone(), len: tmp_len_str.clone(), - content: current_line.clone(), + content: current_line.clone().replace(" ", ""), }; sections.push(Section::from(§ion_f)); } @@ -147,8 +147,8 @@ impl Mem_Checker{ pub fn print_Mem_Checker(m_c: &Mem_Checker){ - println!("PC :: {}", m_c.pc); - println!("SP :: {}", m_c.sp); + println!("PC :: {:x}", m_c.pc); + println!("SP :: {:x}", m_c.sp); for(i,s) in m_c.sections.iter().enumerate() { println!("\nSection {}\n", i); diff --git a/test_file_section.txt b/test_file_section.txt index 21571c8..81b6acd 100644 --- a/test_file_section.txt +++ b/test_file_section.txt @@ -1,17 +1,2 @@ -fe010113 -00813c23 -02010413 -fe042623 -00100793 -fef42423 -fec42783 -00078713 -fe842783 -00f707bb -fef42623 -00000013 -01813403 -02010113 -00008067 -0 -0 \ No newline at end of file +130101FE233C8100 +13040102232604FE