From 39b7db864a64671119d62d9fdefbe607b82461af Mon Sep 17 00:00:00 2001 From: Baptiste Date: Wed, 15 Feb 2023 18:01:50 +0100 Subject: [PATCH] calling mem_checker in main --- src/main.rs | 13 +++--- src/simulator/machine.rs | 5 ++- src/simulator/mem_cmp.rs | 91 +++++++++++++++++++++------------------- test_file_section.txt | 32 +++++++------- 4 files changed, 75 insertions(+), 66 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4acaaf7..41ec194 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,13 @@ mod simulator; use simulator::machine::Machine; +use simulator::mem_cmp; fn main() { let mut m = Machine::_init_machine(); - m.main_memory[4] = 43; - m.main_memory[5] = 150; - let a : u8 = 128; - let b : i8 = a as i8; - let c : u8 = b as u8; - println!("aaa {c}"); - println!("read_memory : {}", Machine::read_memory(&mut m, 2, 4)); + let path = "test_file_section.txt".to_string(); + let checker = mem_cmp::Mem_Checker::from(&path); + mem_cmp::Mem_Checker::fill_memory_from_Mem_Checker(&checker, &mut m); + Machine::run(m); + mem_cmp::Mem_Checker::print_Mem_Checker(&checker); } diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index f3f635b..4b80ffd 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -155,7 +155,7 @@ impl Machine { /// - **machine** which contains a table of instructions pub fn run(machine : Machine){ let mut m = machine; - loop{ + for i in 0..MEM_SIZE{ Machine::one_instruction(&mut m); } } @@ -190,7 +190,8 @@ impl Machine { let val = u64::from_le_bytes(val); let inst : Instruction = decode(val); - + print!("executing instruction {}\n", val); + print!("executing instruction {}\n", inst.opcode); match inst.opcode { RISCV_LUI => { diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index c4a8a5a..8b3bed4 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -1,6 +1,8 @@ use std::fs; use std::io; use std::io::BufRead; +use std::io::BufReader; +use std::io::Lines; use crate::Machine; const MEM_SIZE : usize = 4096; @@ -30,14 +32,14 @@ const MEM_SIZE : usize = 4096; //content est une suite hexadécimale //Section dans le fichier, champ String car informations proviennent d'un fichier txt -struct SectionFormat{ +pub struct SectionFormat{ addr: String, len: String, content: String, } //Section dans le programme -struct Section{ +pub struct Section{ addr: usize, // adresse dans la mémoire len: usize, // nombre d'octets de la donnée à addr content: Vec, // la donnée en question @@ -82,7 +84,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,59 +93,60 @@ struct Mem_Checker{ impl Mem_Checker{ - fn from(path: &String) -> Mem_Checker { + fn vect_from_lines(lines: &mut Lines>, pc: &mut usize, sp: &mut usize) -> Vec{ + let mut vector = Vec::new(); + for (i,line) in lines.enumerate() { + vector.push(line.unwrap()); + } + let size = vector.len(); + *pc = string_hex_to_usize(vector.get(size - 2).expect("0")); + *sp = string_hex_to_usize(vector.get(size - 1).expect("0")); + vector + } + + 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); - let reader2 = io::BufReader::new(&file); - let lines = reader.lines(); - let length = reader2.lines().count(); - + + let reader = io::BufReader::new(file); + let mut lines = reader.lines(); + let mut pc: usize = 0; let mut sp: usize = 0; + let vector = Mem_Checker::vect_from_lines(&mut lines, &mut pc, &mut sp); + let mut sections: Vec
= Vec::new(); - let mut tmp_addr_str: String = String::new(); let mut tmp_len_str: String = String::new(); + + let default = String::new(); + for i in 0..vector.len()-2 { + let current_line = vector.get(i).unwrap_or(&default); - for (i,line) in lines.enumerate() { - - let current_line = line.unwrap(); - - if i == length-2 { - //Lecture de PC - pc = string_hex_to_usize(¤t_line); - } - else if i == length-1 { - //Lecture SP - sp = string_hex_to_usize(¤t_line); + //Lecture des sections + if current_line.contains(' ') { + //lecture ligne ADDR LEN + let next_word_index = current_line.find(' ').unwrap(); + tmp_addr_str = String::from(¤t_line[0..next_word_index]); + tmp_len_str = String::from(¤t_line[next_word_index+1..]); } else { - //Lecture des sections - if current_line.contains(' ') { - //lecture ligne ADDR LEN - let next_word_index = current_line.find(' ').unwrap(); - tmp_addr_str = String::from(¤t_line[0..next_word_index]); - tmp_len_str = String::from(¤t_line[next_word_index+1..]); - } - else { - //lecture ligne CONTENT - let section_f = SectionFormat{ - addr: tmp_addr_str.clone(), - len: tmp_len_str.clone(), - content: current_line, - }; - sections.push(Section::from(§ion_f)); - } - + //lecture ligne CONTENT + let section_f = SectionFormat{ + addr: tmp_addr_str.clone(), + len: tmp_len_str.clone(), + content: current_line.clone(), + }; + sections.push(Section::from(§ion_f)); } - } + } + Mem_Checker{pc:pc, sp:sp, sections:sections} } - fn print_Mem_Checker(m_c: &Mem_Checker){ + pub fn print_Mem_Checker(m_c: &Mem_Checker){ println!("PC :: {}", m_c.pc); println!("SP :: {}", m_c.sp); @@ -154,7 +157,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; @@ -195,7 +198,11 @@ impl Mem_Checker{ -fn string_hex_to_usize(s: &String) -> usize{ +fn string_hex_to_usize(s: &String) -> usize { + + if s.len() == 0 { + return 0; + } let max_pow = (s.len()-1) as u32; let mut ret_value: usize = 0; diff --git a/test_file_section.txt b/test_file_section.txt index df1b0d2..21571c8 100644 --- a/test_file_section.txt +++ b/test_file_section.txt @@ -1,15 +1,17 @@ -addi sp,sp,-32 -sd s0,24(sp) -addi s0,sp,32 -sw zero,-20(s0) -li a5,1 -sw a5,-24(s0) -lw a5,-20(s0) -mv a4,a5 -lw a5,-24(s0) -addw a5,a4,a5 -sw a5,-20(s0) -nop -ld s0,24(sp) -addi sp,sp,32 -ret \ No newline at end of file +fe010113 +00813c23 +02010413 +fe042623 +00100793 +fef42423 +fec42783 +00078713 +fe842783 +00f707bb +fef42623 +00000013 +01813403 +02010113 +00008067 +0 +0 \ No newline at end of file