2022-11-09 16:47:26 +01:00
|
|
|
use crate::decode::*;
|
|
|
|
use crate::print::*;
|
2022-11-09 15:59:05 +01:00
|
|
|
|
2022-11-16 15:48:46 +01:00
|
|
|
// doit disparaitre
|
2022-11-15 21:21:24 +01:00
|
|
|
const MEM_SIZE : usize= 4096;
|
|
|
|
|
|
|
|
|
2022-11-09 15:59:05 +01:00
|
|
|
pub struct Machine {
|
2022-11-09 16:47:26 +01:00
|
|
|
pub pc : u32,
|
|
|
|
pub int_reg : [u32 ; 32],
|
2022-11-15 21:21:24 +01:00
|
|
|
pub instructions : [u32 ; 100],
|
2022-11-16 15:48:46 +01:00
|
|
|
pub main_memory : [u8 ; MEM_SIZE]
|
2022-11-15 21:21:24 +01:00
|
|
|
// futur taille à calculer int memSize = g_cfg->NumPhysPages * g_cfg->PageSize;
|
|
|
|
//creer une struct cfg(configuration) qui s'initialise avec valeur dans un fichier cfg
|
2022-11-09 15:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Machine {
|
2022-11-09 16:47:26 +01:00
|
|
|
|
|
|
|
pub fn _init_machine() -> Machine {
|
2022-11-09 15:59:05 +01:00
|
|
|
|
|
|
|
Machine {
|
2022-11-09 16:47:26 +01:00
|
|
|
pc : 0,
|
|
|
|
instructions : [0 ; 100],
|
2022-11-15 21:21:24 +01:00
|
|
|
int_reg : [0 ; 32],
|
2022-11-16 15:48:46 +01:00
|
|
|
main_memory : [0 ; MEM_SIZE]
|
2022-11-09 15:59:05 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-09 16:47:26 +01:00
|
|
|
|
2022-11-16 15:48:46 +01:00
|
|
|
pub fn one_instruction(mut machine : Machine) -> Machine {
|
2022-11-15 21:21:24 +01:00
|
|
|
|
2022-11-16 15:48:46 +01:00
|
|
|
let mut unsigned_reg1 : u64 = 0;
|
|
|
|
let mut unsigned_reg2 : u64 = 0;
|
2022-11-15 21:21:24 +01:00
|
|
|
|
2022-11-16 15:48:46 +01:00
|
|
|
if machine.instructions.len() <= machine.pc as usize {
|
2022-11-09 16:47:26 +01:00
|
|
|
println!("ERROR : number max of instructions rushed");
|
|
|
|
return machine;
|
|
|
|
}
|
2022-11-09 17:35:16 +01:00
|
|
|
|
2022-11-09 16:47:26 +01:00
|
|
|
let inst : Instruction = decode(machine.instructions[machine.pc as usize]);
|
2022-11-15 21:21:24 +01:00
|
|
|
|
|
|
|
machine.pc += 4;
|
2022-11-09 17:35:16 +01:00
|
|
|
|
2022-11-15 21:21:24 +01:00
|
|
|
match inst.opcode {
|
2022-11-09 16:47:26 +01:00
|
|
|
RISCV_LUI => {
|
|
|
|
machine.int_reg[inst.rd as usize] = inst.imm31_12;
|
|
|
|
},
|
2022-11-09 17:35:16 +01:00
|
|
|
|
|
|
|
//******************************************************************************************
|
|
|
|
// Treatment for: OPI INSTRUCTIONS
|
|
|
|
RISCV_OPI => {
|
2022-11-15 21:21:24 +01:00
|
|
|
match inst.funct3 {
|
2022-11-09 17:35:16 +01:00
|
|
|
RISCV_OPI_ADDI => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] + inst.imm12_I_signed as u32;
|
|
|
|
},
|
|
|
|
RISCV_OPI_SLTI => {
|
|
|
|
machine.int_reg[inst.rd as usize] =
|
|
|
|
if machine.int_reg[inst.rs1 as usize] < inst.imm12_I_signed as u32 { 1 } else { 0 };
|
|
|
|
},
|
|
|
|
RISCV_OPI_XORI => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] ^ inst.imm12_I_signed as u32;
|
|
|
|
},
|
|
|
|
RISCV_OPI_ORI => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] | inst.imm12_I_signed as u32;
|
|
|
|
},
|
|
|
|
RISCV_OPI_ANDI => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] & inst.imm12_I_signed as u32;
|
|
|
|
},
|
|
|
|
RISCV_OPI_SLLI => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] << inst.shamt;
|
|
|
|
}
|
2022-11-16 15:48:46 +01:00
|
|
|
_ => { println!("{} inconnu", inst.funct3); }
|
2022-11-09 17:35:16 +01:00
|
|
|
}
|
|
|
|
},
|
2022-11-15 21:21:24 +01:00
|
|
|
|
|
|
|
RISCV_JAL => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.pc;
|
|
|
|
machine.pc = machine.pc - 4 + (inst.imm21_1_signed as u32);
|
|
|
|
},
|
|
|
|
|
|
|
|
RISCV_OP => {
|
|
|
|
match inst.funct3 {
|
|
|
|
RISCV_OP_ADD => {
|
|
|
|
// RISCV_OP_ADD_ADD inaccessible
|
|
|
|
/*if (inst.funct7 == RISCV_OP_ADD_ADD) {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] + machine.int_reg[inst.rs2 as usize];*/
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] - machine.int_reg[inst.rs2 as usize];
|
|
|
|
//}
|
|
|
|
},
|
|
|
|
RISCV_OP_SLL => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] << (machine.int_reg[inst.rs2 as usize] & 0x3f);
|
|
|
|
},
|
|
|
|
RISCV_OP_SLT => {
|
2022-11-16 15:48:46 +01:00
|
|
|
if machine.int_reg[inst.rs1 as usize] < machine.int_reg[inst.rs2 as usize] {
|
2022-11-15 21:21:24 +01:00
|
|
|
machine.int_reg[inst.rd as usize] = 1;
|
|
|
|
} else {
|
|
|
|
machine.int_reg[inst.rd as usize] = 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_OP_SLTU => {
|
2022-11-16 15:48:46 +01:00
|
|
|
unsigned_reg1 = machine.int_reg[inst.rs1 as usize] as u64;
|
|
|
|
unsigned_reg2 = machine.int_reg[inst.rs2 as usize] as u64;
|
|
|
|
if unsigned_reg1 < unsigned_reg2 {
|
2022-11-15 21:21:24 +01:00
|
|
|
machine.int_reg[inst.rd as usize] = 1;
|
|
|
|
} else {
|
|
|
|
machine.int_reg[inst.rd as usize] = 0;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RISCV_OP_XOR => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] ^ machine.int_reg[inst.rs2 as usize];
|
|
|
|
},
|
|
|
|
RISCV_OP_SR => {
|
|
|
|
// RISCV_OP_SR_SRL inaccessible
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] >> (machine.int_reg[inst.rs2 as usize] & 0x3f);
|
|
|
|
},
|
|
|
|
RISCV_OP_OR => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] | machine.int_reg[inst.rs2 as usize];
|
|
|
|
},
|
|
|
|
RISCV_OP_AND => {
|
|
|
|
machine.int_reg[inst.rd as usize] = machine.int_reg[inst.rs1 as usize] & machine.int_reg[inst.rs2 as usize];
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
println!("RISCV_OP undefined case\n");
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 15:48:46 +01:00
|
|
|
}
|
|
|
|
_ => { println!("{} opcode non géré", inst.opcode)},
|
2022-11-09 16:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
machine
|
|
|
|
}
|
2022-11-09 15:59:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2022-11-09 16:45:00 +01:00
|
|
|
use super::Machine;
|
2022-11-09 15:59:05 +01:00
|
|
|
|
|
|
|
}
|