burritos/src/simulator/mem_cmp.rs

185 lines
3.8 KiB
Rust
Raw Normal View History

2023-02-07 15:19:38 +01:00
/* TRUCS MANQUANT
* Verifier qu'il y a un nombre pair de caractere hexa dans la ligne correspondante d'une section du fichier source
* Sinon on ne peut pas automatiquement remplir chaque octect car 2 hexa = 1 octet
*/
/* FORMAT FICHIER.TXT Représentant la mémoire apres éxecution d'un prog
* PC
* SP
* Section_1
* Section_2
* ...
* ...
* Section_n
*/
/* Chaque section se divise en 3 parties, sur 2 lignes de texte
* addr ESPACE len
* content
*/
//content est une suite hexadécimale
//Section dans le fichier, champ String car informations proviennent d'un fichier txt
struct SectionFormat{
addr: String,
len: String,
content: String,
}
//Section dans le programme
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
}
/*
* Voir si instanciation d'une structure deplace les valeurs "locales" à la méthode from, je sais plus ....
*/
impl Section{
fn from(section: &SectionFormat) -> Section {
let mut content: Vec<u8> = Vec::new();
let addr: usize = section.addr.parse().unwrap();
let len: usize = section.len.parse().unwrap();
let mut tmp_a: char = ' ';
let mut tmp_b: char = ' ';
for (i, c) in section.content.chars().enumerate(){
if i%2 == 0 {
tmp_a = c;
}
else {
tmp_b = c;
content.push(two_hex_to_u8(tmp_a,tmp_b));
}
}
2023-02-07 15:19:38 +01:00
Section{addr:addr, len:len, content:content}
}
}
2023-02-07 22:50:55 +01:00
/*
* Representation de l'etat de la mémoire (apres execution.... a confirmer), sous forme de sections
*/
struct Mem_Checker{
pc: usize,
sp: usize,
sections: Vec<Section>,
}
impl Mem_Checker{
/*fn from(path: &String) -> Mem_Checker{
}*/
}
/*
* c doit etre un caractère hexadécimale
*/
fn one_hex_to_dec(c: char) -> u8 {
match c {
'A' | 'a' => 10,
'B' | 'b' => 11,
'C' | 'c' => 12,
'D' | 'd' => 13,
'E' | 'e' => 14,
'F' | 'f' => 15,
_ => {
let ret : u8 = c.to_digit(10).unwrap() as u8;
return ret;
},
}
}
fn two_hex_to_u8(c1: char, c2: char) -> u8 {
let a = one_hex_to_dec(c1);
let b = one_hex_to_dec(c2);
16*a + b
}
#[cfg(test)]
mod tests {
use super::*;
#[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);
let mut expected_vec: Vec<u8> = Vec::new();
expected_vec.push(0u8);
expected_vec.push(255u8);
expected_vec.push(10u8);
expected_vec.push(160u8);
expected_vec.push(165u8);
//println!("Vec from created section {:?}", &section.content);
//println!("Expected vec {:?}", &expected_vec);
assert_eq!(section.content, expected_vec);
}
2023-02-07 22:50:55 +01:00
#[test]
fn test_mod(){
let cond = (0%2) == 0;
assert_eq!(true, cond);
}
#[test]
fn test_mod_2(){
let cond = (1%2) == 1;
assert_eq!(true, cond);
}
#[test]
fn test_hex_1(){
let b = two_hex_to_u8('0', '0');
assert_eq!(0u8, b);
}
#[test]
fn test_hex_2(){
let b = two_hex_to_u8('F', 'F');
assert_eq!(255u8, b);
}
#[test]
fn test_hex_3(){
let b = two_hex_to_u8('0', 'A');
assert_eq!(10u8, b);
}
#[test]
fn test_hex_4(){
let b = two_hex_to_u8('A', '0');
assert_eq!(160u8, b);
}
#[test]
fn test_hex_5(){
let b = two_hex_to_u8('A', '5');
assert_eq!(165u8, b);
}
}