Merge conflicts

This commit is contained in:
Samy Solhi
2023-02-15 18:12:15 +01:00
4 changed files with 71 additions and 70 deletions

View File

@ -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);
}
}

View File

@ -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<u8>, // la donnée en question
@ -91,59 +93,60 @@ pub struct Mem_Checker{
impl 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 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<Section> = 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(&current_line);
}
else if i == length-1 {
//Lecture SP
sp = string_hex_to_usize(&current_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(&current_line[0..next_word_index]);
tmp_len_str = String::from(&current_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(&current_line[0..next_word_index]);
tmp_len_str = String::from(&current_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(&section_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(&section_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);
@ -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;