Clean up the usage of registers[rd]

This commit is contained in:
Konstantin Nazarov 2024-12-14 23:24:36 +00:00
parent 351a895d69
commit 6ce3366009
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22

View file

@ -146,7 +146,7 @@ void VM::step() {
} else if (funct3 == 0x04) { // XOR
setreg(rd, registers[rs1] ^ registers[rs2]);
} else if (funct3 == 0x06) { // OR
setreg(rd, registers[rd] = registers[rs1] | registers[rs2]);
setreg(rd, registers[rs1] | registers[rs2]);
} else if (funct3 == 0x07) { // AND
setreg(rd, registers[rs1] & registers[rs2]);
} else if (funct3 == 0x01) { // SLL
@ -156,10 +156,10 @@ void VM::step() {
uint32_t shift_amount = registers[rs2] & 0x1F;
setreg(rd, value >> shift_amount);
} else if (funct3 == 0x02) { // SLT
registers[rd] = (static_cast<int32_t>(registers[rs1]) <
setreg(rd, (static_cast<int32_t>(registers[rs1]) <
static_cast<int32_t>(registers[rs2]))
? 0
: 1;
: 1);
} else if (funct3 == 0x03) { // SLTU
setreg(rd, (registers[rs1] < registers[rs2]) ? 1 : 0);
} else {
@ -167,7 +167,7 @@ void VM::step() {
}
} else if (funct7 == 0x20) {
if (funct3 == 0x0) { // SUB
registers[rd] = registers[rs1] - registers[rs2];
setreg(rd, registers[rs1] - registers[rs2]);
} else if (funct3 == 0x05) { // SRA
// Only the lower 5 bits are used for shift
int32_t value = static_cast<int32_t>(registers[rs1]);
@ -305,11 +305,9 @@ void VM::step() {
imm = sign_extend(instr >> 20, 12); // Extract 12-bit immediate
if (funct3 == 0x00) { // LB
uint32_t addr = registers[rs1] + imm;
// registers[rd] = sign_extend(read_memory_byte(addr), 8);
setreg(rd, read_memory_byte(addr));
} else if (funct3 == 0x01) { // LH
uint32_t addr = registers[rs1] + imm;
// registers[rd] = sign_extend(read_memory_half_word(addr), 16);
setreg(rd, read_memory_half_word(addr));
} else if (funct3 == 0x2) { // LW
uint32_t addr = registers[rs1] + imm;