Add a bin loader

This commit is contained in:
Quentin Legot
2023-03-27 18:10:11 +02:00
parent a223c14f36
commit 08ba0154f7
5 changed files with 69 additions and 20 deletions

View File

@ -146,7 +146,7 @@ impl Machine {
/// - **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 {
pub fn read_memory(&mut self, size : i32, address : usize) -> u64 {
if ![1, 2, 4, 8].contains(&size) {
panic!("ERROR read_memory : wrong size parameter {size}, must be (1, 2, 4 or 8)");
}
@ -154,7 +154,7 @@ impl Machine {
let mut ret: u64 = 0;
for i in 0..size {
ret <<= 8;
ret += machine.main_memory[address + i as usize] as u64;
ret += self.main_memory[address + i as usize] as u64;
}
ret
}
@ -169,13 +169,13 @@ impl Machine {
/// - **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) {
pub fn write_memory(&mut self, size: i32, address: usize, value: u64) {
if ![1, 2, 4, 8].contains(&size) {
panic!("ERROR write_memory: WRONG `size` PARAMETER ({size}), must be 1, 2, 4 or 8")
}
for i in 0..size as usize {
let inv_i = size as usize - i - 1;
machine.main_memory[address + i] = ((value & 0xff << (8 * inv_i)) >> (inv_i * 8)) as u8;
self.main_memory[address + i] = ((value & 0xff << (8 * inv_i)) >> (inv_i * 8)) as u8;
}
}
@ -185,11 +185,11 @@ impl Machine {
/// ### Parameters
///
/// - **machine** contains the memory
pub fn _extract_memory(machine: &mut Machine){
pub fn _extract_memory(&mut self){
let file_path = "burritos_memory.txt";
let write_to_file = |path| -> std::io::Result<File> {
let mut file = File::create(path)?;
file.write_all(&machine.main_memory)?;
file.write_all(&self.main_memory)?;
Ok(file)
};
match write_to_file(file_path) {
@ -198,20 +198,20 @@ impl Machine {
};
}
pub fn print_machine_status(machine: &mut Machine) {
pub fn print_machine_status(&mut self) {
println!("######### Machine status #########");
for i in (0..32).step_by(3) {
print!(">{0: <4} : {1:<16x} ", print::REG_X[i], machine.int_reg.get_reg(i));
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+1], machine.int_reg.get_reg(i+1));
print!(">{0: <4} : {1:<16x} ", print::REG_X[i], self.int_reg.get_reg(i));
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+1], self.int_reg.get_reg(i+1));
if i+2 < 32 {
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+2], machine.int_reg.get_reg(i+2));
print!(">{0: <4} : {1:<16x} ", print::REG_X[i+2], self.int_reg.get_reg(i+2));
}
println!();
}
println!("________________SP________________");
let sp_index = machine.int_reg.get_reg(2);
let sp_index = self.int_reg.get_reg(2);
for i in 0..5 {
println!("SP+{:<2} : {:16x}", i*8, Self::read_memory(machine, 8, (sp_index + i*8) as usize));
println!("SP+{:<2} : {:16x}", i*8, Self::read_memory(self, 8, (sp_index + i*8) as usize));
}
println!("##################################");
}
@ -229,9 +229,9 @@ impl Machine {
/// ### Parameters
///
/// - **machine** which contains a table of instructions
pub fn run(machine : &mut Machine){
while Machine::one_instruction(machine) == 0 {}
println!("trace : \n{}", machine.registers_trace);
pub fn run(&mut self){
while Machine::one_instruction(self) == 0 {}
println!("trace : \n{}", self.registers_trace);
}
/// execute the current instruction