burritos/src/simulator/mem_cmp.rs

248 lines
7.3 KiB
Rust
Raw Normal View History

2023-03-14 14:49:58 +01:00
///! FILE.TXT FORMAT Representing machine memory memory
/// - PC
/// - SP
/// - Section_1
/// - Section_2
/// - ...
/// - Section_n
///
/// Each section is divided in 3 parts, on two lines of text
/// addr SPACE len
/// content
use std::{fs, io::{BufRead, BufReader, Lines, Error}};
2023-02-08 15:50:14 +01:00
use crate::Machine;
2023-03-14 00:33:19 +01:00
/// File section
2023-02-15 18:01:50 +01:00
pub struct SectionFormat{
2023-03-14 14:49:58 +01:00
/// Memory address of the section
2023-02-07 15:19:38 +01:00
addr: String,
2023-03-14 14:49:58 +01:00
/// The size of data in bytes
2023-02-07 15:19:38 +01:00
len: String,
2023-03-14 14:49:58 +01:00
/// The data itself in Hexadecimal format
2023-02-07 15:19:38 +01:00
content: String,
}
2023-03-14 00:33:19 +01:00
/// # Memory section
///
/// Representation of a section of memory from BurritOS or NachOS
2023-02-15 18:01:50 +01:00
pub struct Section{
2023-03-14 14:49:58 +01:00
/// Memory address of the section
addr: usize,
/// The size of data in bytes
len: usize,
/// The data itself in Hexadecimal format
content: Vec<u8>
2023-02-07 15:19:38 +01:00
}
2023-03-14 14:49:58 +01:00
impl Section {
2023-03-14 00:33:19 +01:00
/// Creates a memory section from a SectionFormat
2023-02-07 15:19:38 +01:00
fn from(section: &SectionFormat) -> Section {
let addr = usize::from_str_radix(&section.addr, 10).unwrap_or_default();
let len = usize::from_str_radix(&section.len, 10).unwrap_or_default();
2023-03-14 00:33:19 +01:00
let content: Vec<u8> = section.content.as_bytes().chunks(2).map(|x| {
u8::from_str_radix(std::str::from_utf8(x).unwrap_or_default(), 16).unwrap_or_default()
}).collect();
2023-03-08 13:04:03 +01:00
Section{addr, len, content}
2023-02-07 15:19:38 +01:00
}
2023-02-08 12:37:21 +01:00
2023-03-14 00:33:19 +01:00
/// Pretty prints a memory section
2023-03-08 13:04:03 +01:00
fn print_section(s: &Section){
2023-03-14 00:33:19 +01:00
println!("ADDR :: {:x}\nLEN :: {:x}\nCONTENT :: {:?}", s.addr, s.len, s.content);
2023-02-08 12:37:21 +01:00
}
2023-02-07 15:19:38 +01:00
}
2023-03-14 14:49:58 +01:00
/// # Representation of the state of machine memory
///
/// Could represent memory at any point in time, before, during, or after execution.
/// The memory is split into sections.
pub struct MemChecker {
/// Value of the program counter
2023-02-07 22:50:55 +01:00
pc: usize,
2023-03-14 14:49:58 +01:00
/// Value of the stack pointer
2023-02-07 22:50:55 +01:00
sp: usize,
2023-03-14 14:49:58 +01:00
/// Sections
2023-02-07 22:50:55 +01:00
sections: Vec<Section>,
}
2023-03-08 13:04:03 +01:00
impl MemChecker{
2023-02-08 12:37:21 +01:00
2023-03-08 11:15:13 +01:00
///Translate lines of a file in e Vector of String
///We need this method to parse the memory we received
///
/// ### Parameters
///
/// - **Lines** The file to parse
///
/// ### Return
/// - A vector of String where each line of the file os an element of the vector
2023-02-15 18:01:50 +01:00
fn vect_from_lines(lines: &mut Lines<BufReader<fs::File>>, pc: &mut usize, sp: &mut usize) -> Vec<String>{
let mut vector = Vec::new();
2023-03-08 13:04:03 +01:00
for (_,line) in lines.enumerate() {
2023-02-15 18:01:50 +01:00
vector.push(line.unwrap());
}
let size = vector.len();
2023-03-14 00:33:19 +01:00
*pc = usize::from_str_radix(vector.get(size - 2).expect("0"), 16).unwrap_or_default();
*sp = usize::from_str_radix(vector.get(size - 1).expect("0"), 16).unwrap_or_default();
2023-02-15 18:01:50 +01:00
vector
}
2023-03-08 11:15:13 +01:00
/// Fill a mem checker from a file (here the mock memory)
/// Extract the values of pc, sp and sections
///
/// ### Parameter
/// -**path** addr to the file
///
/// ### Return
/// Mem-checker filled
pub fn from(path: &str) -> Result<MemChecker, Error> {
2023-02-08 12:37:21 +01:00
let file = fs::File::open(path)?;
2023-02-15 18:01:50 +01:00
2023-03-08 13:04:03 +01:00
let reader = BufReader::new(file);
2023-02-15 18:01:50 +01:00
let mut lines = reader.lines();
let mut pc: usize = 0;
let mut sp: usize = 0;
2023-03-08 13:04:03 +01:00
let vector = MemChecker::vect_from_lines(&mut lines, &mut pc, &mut sp);
2023-02-15 18:01:50 +01:00
2023-02-08 12:37:21 +01:00
let mut sections: Vec<Section> = Vec::new();
let mut tmp_addr_str: String = String::new();
let mut tmp_len_str: String = String::new();
2023-02-15 18:01:50 +01:00
let default = String::new();
for i in 0..vector.len()-2 {
let current_line = vector.get(i).unwrap_or(&default);
//Lecture des sections
if i % 2 == 0 {
2023-02-15 18:01:50 +01:00
//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 {
2023-02-15 18:01:50 +01:00
//lecture ligne CONTENT
let section_f = SectionFormat{
addr: tmp_addr_str.clone(),
len: tmp_len_str.clone(),
2023-03-10 11:03:54 +01:00
content: current_line.clone().replace(' ', ""),
2023-02-15 18:01:50 +01:00
};
sections.push(Section::from(&section_f));
}
2023-02-08 12:37:21 +01:00
2023-02-15 18:01:50 +01:00
}
Ok(MemChecker{pc, sp, sections})
2023-02-08 12:37:21 +01:00
}
2023-03-08 11:15:13 +01:00
/// Print the content of a Mem_Checker
///
/// ### Parameter
///
/// - **m_c** Contains the data we want to print
2023-03-08 13:04:03 +01:00
pub fn print_mem_checker(m_c: &MemChecker){
println!("PC :: {:x}", m_c.pc);
println!("SP :: {:x}", m_c.sp);
2023-02-08 12:37:21 +01:00
for(i,s) in m_c.sections.iter().enumerate() {
println!("\nSection {}\n", i);
2023-03-10 11:03:54 +01:00
Section::print_section(s);
2023-02-08 12:37:21 +01:00
}
}
2023-02-08 14:34:09 +01:00
2023-03-08 11:15:13 +01:00
/// Fill a machine's memory from a Mem Chacker
///
/// ### Parameters
///
/// - **m_c** contains the data
/// - **machine** contains the memry to fill
2023-03-08 13:04:03 +01:00
pub fn fill_memory_from_mem_checker(m_c: &MemChecker, machine: &mut Machine){
2023-02-08 15:50:14 +01:00
machine.sp = m_c.sp;
2023-03-08 17:58:38 +01:00
machine.int_reg.set_reg(2, m_c.sp as i64);
2023-02-08 15:50:14 +01:00
machine.pc = m_c.pc as u64;
2023-02-08 14:34:09 +01:00
2023-02-08 15:50:14 +01:00
for section in m_c.sections.iter() {
for (i,b) in section.content.iter().enumerate() {
machine.main_memory[section.addr + i] = *b;
}
}
}
2023-02-08 14:34:09 +01:00
2023-03-14 14:49:58 +01:00
/// For debug
2023-03-08 13:04:03 +01:00
fn compare_print_m_c_machine(m_c: &MemChecker, machine: &mut Machine){
MemChecker::print_mem_checker(m_c);
2023-02-08 15:50:14 +01:00
for section in m_c.sections.iter() {
print!("\n\n");
println!("Content addr : {}", section.addr);
println!("Content len (number of bytes) : {}", section.len);
for i in 0..section.len {
println!("mem[{}] = {}", section.addr + i, machine.main_memory[section.addr + i]);
}
}
2023-02-08 14:34:09 +01:00
}
2023-02-08 15:50:14 +01:00
2023-03-08 13:34:12 +01:00
/// Compare sections of a memChecker and a machine memory
///
/// ### Parameters
///
/// - **m_c** contains section of the memory checker
/// - **machine** contains the main memory
2023-03-08 13:04:03 +01:00
pub fn compare_machine_memory(m_c: &MemChecker, machine: &Machine) -> bool {
2023-03-08 13:34:12 +01:00
m_c.sections.iter().map(|section| {
2023-03-08 17:58:38 +01:00
(0..section.len).into_iter().all(|i| machine.main_memory[section.addr + i] == section.content[i])
}).all(|e| e)
2023-03-07 17:32:59 +01:00
}
}
#[cfg(test)]
mod tests {
use super::*;
2023-02-08 12:37:21 +01:00
#[test]
2023-03-15 10:53:22 +01:00
#[ignore]
2023-02-08 15:50:14 +01:00
fn test_fill_memory(){
2023-03-13 23:55:35 +01:00
let m_c = MemChecker::from("test/machine/memory.txt").unwrap();
let mut machine = Machine::init_machine();
2023-03-08 13:04:03 +01:00
MemChecker::fill_memory_from_mem_checker(&m_c, &mut machine);
MemChecker::compare_print_m_c_machine(&m_c, &mut machine);
2023-02-08 15:50:14 +01:00
}
#[test]
fn test_enum_start_at_zero(){
let v = vec![1,2,3];
for (i,val) in v.iter().enumerate() {
println!("i = {} :: v[i] = {}", i, val);
}
}
#[test]
2023-03-08 13:04:03 +01:00
fn test_create_mem_checker(){
2023-03-13 23:55:35 +01:00
let m_c = MemChecker::from("test/machine/memory.txt").unwrap();
2023-03-08 13:04:03 +01:00
MemChecker::print_mem_checker(&m_c);
2023-02-08 12:37:21 +01:00
}
#[test]
fn test_create_section_content(){
let section_format = SectionFormat{
addr: "0".to_string(),
len: "0".to_string(),
content: "00FF0AA0A5".to_string(),
};
let section = Section::from(&section_format);
2023-03-10 10:38:58 +01:00
let expected_vec: Vec<u8> = vec![0u8, 255u8, 10u8, 160u8, 165u8];
assert_eq!(section.content, expected_vec);
}
2023-02-08 15:51:55 +01:00
}