calling mem_checker in main

This commit is contained in:
Baptiste 2023-02-15 18:01:50 +01:00
parent 4425ac747e
commit 39b7db864a
4 changed files with 75 additions and 66 deletions

View File

@ -1,14 +1,13 @@
mod simulator; mod simulator;
use simulator::machine::Machine; use simulator::machine::Machine;
use simulator::mem_cmp;
fn main() { fn main() {
let mut m = Machine::_init_machine(); let mut m = Machine::_init_machine();
m.main_memory[4] = 43; let path = "test_file_section.txt".to_string();
m.main_memory[5] = 150; let checker = mem_cmp::Mem_Checker::from(&path);
let a : u8 = 128; mem_cmp::Mem_Checker::fill_memory_from_Mem_Checker(&checker, &mut m);
let b : i8 = a as i8; Machine::run(m);
let c : u8 = b as u8; mem_cmp::Mem_Checker::print_Mem_Checker(&checker);
println!("aaa {c}");
println!("read_memory : {}", Machine::read_memory(&mut m, 2, 4));
} }

View File

@ -155,7 +155,7 @@ impl Machine {
/// - **machine** which contains a table of instructions /// - **machine** which contains a table of instructions
pub fn run(machine : Machine){ pub fn run(machine : Machine){
let mut m = machine; let mut m = machine;
loop{ for i in 0..MEM_SIZE{
Machine::one_instruction(&mut m); Machine::one_instruction(&mut m);
} }
} }
@ -190,7 +190,8 @@ impl Machine {
let val = u64::from_le_bytes(val); let val = u64::from_le_bytes(val);
let inst : Instruction = decode(val); let inst : Instruction = decode(val);
print!("executing instruction {}\n", val);
print!("executing instruction {}\n", inst.opcode);
match inst.opcode { match inst.opcode {
RISCV_LUI => { RISCV_LUI => {

View File

@ -1,6 +1,8 @@
use std::fs; use std::fs;
use std::io; use std::io;
use std::io::BufRead; use std::io::BufRead;
use std::io::BufReader;
use std::io::Lines;
use crate::Machine; use crate::Machine;
const MEM_SIZE : usize = 4096; const MEM_SIZE : usize = 4096;
@ -30,14 +32,14 @@ const MEM_SIZE : usize = 4096;
//content est une suite hexadécimale //content est une suite hexadécimale
//Section dans le fichier, champ String car informations proviennent d'un fichier txt //Section dans le fichier, champ String car informations proviennent d'un fichier txt
struct SectionFormat{ pub struct SectionFormat{
addr: String, addr: String,
len: String, len: String,
content: String, content: String,
} }
//Section dans le programme //Section dans le programme
struct Section{ pub struct Section{
addr: usize, // adresse dans la mémoire addr: usize, // adresse dans la mémoire
len: usize, // nombre d'octets de la donnée à addr len: usize, // nombre d'octets de la donnée à addr
content: Vec<u8>, // la donnée en question content: Vec<u8>, // 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 * 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, pc: usize,
sp: usize, sp: usize,
sections: Vec<Section>, sections: Vec<Section>,
@ -91,59 +93,60 @@ struct Mem_Checker{
impl Mem_Checker{ impl Mem_Checker{
fn from(path: &String) -> Mem_Checker { fn vect_from_lines(lines: &mut Lines<BufReader<fs::File>>, pc: &mut usize, sp: &mut usize) -> Vec<String>{
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 file = fs::File::open("test_file_section.txt").expect("Wrong filename");
let reader = io::BufReader::new(&file);
let reader2 = io::BufReader::new(&file); let reader = io::BufReader::new(file);
let lines = reader.lines(); let mut lines = reader.lines();
let length = reader2.lines().count();
let mut pc: usize = 0; let mut pc: usize = 0;
let mut sp: usize = 0; let mut sp: usize = 0;
let vector = Mem_Checker::vect_from_lines(&mut lines, &mut pc, &mut sp);
let mut sections: Vec<Section> = Vec::new(); let mut sections: Vec<Section> = Vec::new();
let mut tmp_addr_str: String = String::new(); let mut tmp_addr_str: String = String::new();
let mut tmp_len_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() { //Lecture des sections
if current_line.contains(' ') {
let current_line = line.unwrap(); //lecture ligne ADDR LEN
let next_word_index = current_line.find(' ').unwrap();
if i == length-2 { tmp_addr_str = String::from(&current_line[0..next_word_index]);
//Lecture de PC tmp_len_str = String::from(&current_line[next_word_index+1..]);
pc = string_hex_to_usize(&current_line);
}
else if i == length-1 {
//Lecture SP
sp = string_hex_to_usize(&current_line);
} }
else { else {
//Lecture des sections //lecture ligne CONTENT
if current_line.contains(' ') { let section_f = SectionFormat{
//lecture ligne ADDR LEN addr: tmp_addr_str.clone(),
let next_word_index = current_line.find(' ').unwrap(); len: tmp_len_str.clone(),
tmp_addr_str = String::from(&current_line[0..next_word_index]); content: current_line.clone(),
tmp_len_str = String::from(&current_line[next_word_index+1..]); };
} sections.push(Section::from(&section_f));
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(&section_f));
}
} }
}
}
Mem_Checker{pc:pc, sp:sp, sections:sections} 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!("PC :: {}", m_c.pc);
println!("SP :: {}", m_c.sp); 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.sp = m_c.sp;
machine.pc = m_c.pc as u64; 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 max_pow = (s.len()-1) as u32;
let mut ret_value: usize = 0; let mut ret_value: usize = 0;

View File

@ -1,15 +1,17 @@
addi sp,sp,-32 fe010113
sd s0,24(sp) 00813c23
addi s0,sp,32 02010413
sw zero,-20(s0) fe042623
li a5,1 00100793
sw a5,-24(s0) fef42423
lw a5,-20(s0) fec42783
mv a4,a5 00078713
lw a5,-24(s0) fe842783
addw a5,a4,a5 00f707bb
sw a5,-20(s0) fef42623
nop 00000013
ld s0,24(sp) 01813403
addi sp,sp,32 02010113
ret 00008067
0
0