diff --git a/src/cpu/rv32cpu.c b/src/cpu/rv32cpu.c index 44ecbec..6594fe7 100644 --- a/src/cpu/rv32cpu.c +++ b/src/cpu/rv32cpu.c @@ -140,10 +140,12 @@ static void cpu_execute(rv32_cpu_t* cpu, instruction_t* instruction) case OPCODE_JALR: { // 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 uint32_t immediate = (instruction->immediate & 0xFFF) | (instruction->immediate & 0x800 ? 0xFFFFF000 : 0); cpu->pc = ((cpu->regs.x[instruction->rs1] + immediate) & 0xFFFFFFFE) - 4; + // Save old pc + cpu->regs.x[instruction->rd] = old_pc + 4; break; } case OPCODE_BRANCH: