Fix read_memory and implemented write_memory

This commit is contained in:
Quentin Legot 2023-01-18 15:03:58 +01:00
parent 547abd001b
commit 89cc9423bd

View File

@ -37,43 +37,45 @@ impl Machine {
/// Read from main memory of the machine
///
/// `panic!` when size is not 1, 2, 4 or 8
///
/// ### Parameters
///
/// - **machine** which contains the main memory
/// - **size** the number of bytes to read (1, 2, 4, 8)
/// - **address** in the memory to read
pub fn read_memory(machine : &mut Machine, size : i32, address : usize) -> u64 {
if size != 1 && size != 2 && size != 4 && size != 8 {
if ![1, 2, 4, 8].contains(&size) {
panic!("ERROR read_memory : wrong size parameter {}, must be (1, 2, 4 or 8)", size);
}
let mut ret : u64 = machine.main_memory[address] as u64;
if size == 2 || size == 4 || size == 8 {
let mut ret: u64 = 0;
for i in 0..size {
ret <<= 8;
ret += machine.main_memory[address + 1] as u64;
}
if size == 4 || size == 8 {
ret <<= 8;
ret += machine.main_memory[address + 2] as u64;
}
if size == 8 {
ret <<= 8;
ret += machine.main_memory[address + 3] as u64;
ret += machine.main_memory[address + i as usize] as u64;
}
ret
}
/// Write to the main memory of the machine
///
/// **machine** contains the memory
/// **size** the number of bytes to write (1, 2, 4 or 8)
/// **address** the address to write to
/// **value** data to be written
pub fn write_memory(machine: &mut Machine, size: i32, address: usize, value: i64) {
if ![1, 2, 3, 4].contains(&size) {
/// `panic!` when size is not 1, 2, 4 or 8
///
/// ### Parameters
///
/// - **machine** contains the memory
/// - **size** the number of bytes to write (1, 2, 4 or 8)
/// - **address** the address to write to
/// - **value** data to be written
pub fn write_memory(machine: &mut Machine, size: i32, address: usize, value: u64) {
if ![1, 2, 4, 8].contains(&size) {
panic!("ERROR write_memory: WRONG `size` PARAMETER ({}), must be 1, 2, 4 or 8", size)
}
todo!("Write memory not implemented yet");
let map: [u64; 8] = [0xff000000_00000000, 0x00ff0000_00000000, 0x0000ff00_00000000, 0x000000ff_00000000,
0x00000000_ff00000000, 0x00000000_00ff0000, 0x00000000_0000ff00, 0x00000000_000000ff];
for i in 0..size as usize {
machine.main_memory[address + i as usize] = (value & map[i]) as u8;
}
}
/// Execute the instructions table of a machine putted in param
@ -209,6 +211,7 @@ impl Machine {
RISCV_ST => {
match inst.funct3 {
RISCV_ST_STB => {
todo!("Write memory here");
},
RISCV_ST_STH => {