Fix JALR implementation (rd=rs1 edge case)

master
vhaudiquet 1 year ago
parent 57dfd9cb76
commit bb1427f77b
  1. 4
      src/cpu/rv32cpu.c

@ -140,10 +140,12 @@ static void cpu_execute(rv32_cpu_t* cpu, instruction_t* instruction)
case OPCODE_JALR: case OPCODE_JALR:
{ {
// Jump And Link Register // Jump And Link Register
cpu->regs.x[instruction->rd] = cpu->pc + 4; uint32_t old_pc = cpu->pc;
// Sign extend immediate from 12 bits to 32 bits // Sign extend immediate from 12 bits to 32 bits
uint32_t immediate = (instruction->immediate & 0xFFF) | (instruction->immediate & 0x800 ? 0xFFFFF000 : 0); uint32_t immediate = (instruction->immediate & 0xFFF) | (instruction->immediate & 0x800 ? 0xFFFFF000 : 0);
cpu->pc = ((cpu->regs.x[instruction->rs1] + immediate) & 0xFFFFFFFE) - 4; cpu->pc = ((cpu->regs.x[instruction->rs1] + immediate) & 0xFFFFFFFE) - 4;
// Save old pc
cpu->regs.x[instruction->rd] = old_pc + 4;
break; break;
} }
case OPCODE_BRANCH: case OPCODE_BRANCH:

Loading…
Cancel
Save