diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index e8562a0..306c084 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -3,12 +3,6 @@ use crate::Machine; const MEM_SIZE : usize = 4096; - -/* TRUCS MANQUANTS -* 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 @@ -26,14 +20,20 @@ const MEM_SIZE : usize = 4096; //content est une suite hexadécimale -//Section dans le fichier, champ String car informations proviennent d'un fichier txt +/// File section pub struct SectionFormat{ addr: String, len: String, content: String, } -//Section dans le programme +/// # Memory section +/// +/// Representation of a section of memory from BurritOS or NachOS +/// +/// - addr: Memory address of the section +/// - len: The size of data in bytes +/// - content: the data itself pub struct Section{ addr: usize, // adresse dans la mémoire len: usize, // nombre d'octets de la donnée à addr @@ -45,32 +45,19 @@ pub struct Section{ */ impl Section{ + /// Creates a memory section from a SectionFormat fn from(section: &SectionFormat) -> Section { - - let mut content: Vec = Vec::new(); - let addr: usize = string_hex_to_usize(§ion.addr); - let len: usize = string_hex_to_usize(§ion.len); - - let mut tmp_a: char = ' '; - - for (i, c) in section.content.chars().enumerate(){ - - if i%2 == 0 { - tmp_a = c; - } - else { - content.push(two_hex_to_u8(tmp_a,c)); - } - } - + let addr = usize::from_str_radix(§ion.addr, 16).unwrap_or_default(); + let len = usize::from_str_radix(§ion.len, 16).unwrap_or_default(); + let content: Vec = 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(); Section{addr, len, content} } - + /// Pretty prints a memory section fn print_section(s: &Section){ - println!("ADDR :: {:x}", s.addr); - println!("LEN :: {:x}", s.len); - println!("CONTENT :: {:?}", s.content); + println!("ADDR :: {:x}\nLEN :: {:x}\nCONTENT :: {:?}", s.addr, s.len, s.content); } } @@ -101,8 +88,8 @@ impl MemChecker{ 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")); + *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(); vector } @@ -229,56 +216,6 @@ impl MemChecker{ } - - -fn string_hex_to_usize(s: &String) -> usize { - - if s.is_empty() { - return 0; - } - - let max_pow = (s.len()-1) as u32; - let mut ret_value: usize = 0; - let base: usize = 16; - - for (i,c )in s.chars().enumerate(){ - //println!("Current char :: {} :: Current pow :: {} ::", c, max_pow - (i as u32)); - let tmp: usize = one_hex_to_dec(c) as usize; - ret_value += base.pow(max_pow - (i as u32))*tmp; - } - - ret_value -} - - - -/* -* 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, - _ => { - c.to_digit(10).unwrap() as u8 - }, - } -} - - - -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::*; @@ -306,17 +243,6 @@ mod tests { MemChecker::print_mem_checker(&m_c); } - - #[test] - fn test_string_hex_to_usize(){ - let s = String::from("AE1F20"); - //println!("taille de string : {}", s.len()); - let expected: usize = 11411232; - let result = string_hex_to_usize(&s); - - assert_eq!(expected,result); - } - #[test] fn test_create_section_content(){ let section_format = SectionFormat{ @@ -324,43 +250,9 @@ mod tests { len: "0".to_string(), content: "00FF0AA0A5".to_string(), }; - let section = Section::from(§ion_format); let expected_vec: Vec = vec![0u8, 255u8, 10u8, 160u8, 165u8]; - - //println!("Vec from created section {:?}", §ion.content); - //println!("Expected vec {:?}", &expected_vec); - assert_eq!(section.content, expected_vec); } - #[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); - } }