Skip to content

Commit

Permalink
Implement c.ebreak properly
Browse files Browse the repository at this point in the history
Add the breakpoint exception handler which triggers
a breakpoint exception when the c.ebreak instruction
is executed.
  • Loading branch information
Risheng1128 committed Sep 30, 2022
1 parent 09f4e65 commit 331ce72
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ Current progress of this emulator in riscv-arch-test(RV32):
* Passed Tests
- `I`: Base Integer Instruction Set
- `M`: Standard Extension for Integer Multiplication and Division
- `C`: Standard Extension for Compressed Instruction
- `Zifencei`: Instruction-Fetch Fence
* Failed Tests
- `C`: Standard Extension for Compressed Instruction
+ `cebreak`
- `privilege`: RISCV Privileged Specification
+ 2 system calls
* `ebreak`
Expand Down
37 changes: 37 additions & 0 deletions src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,37 @@ static void rv_except_illegal_insn(struct riscv_t *rv, uint32_t insn)
rv->csr_mcause = code;
}

static void rv_except_breakpoint(struct riscv_t *rv, uint32_t old_pc)
{
/* mtvec (Machine Trap-Vector Base Address Register)
* mtvec[MXLEN-1:2]: vector base address
* mtvec[1:0] : vector mode
*/
const uint32_t base = rv->csr_mtvec & ~0x3;
const uint32_t mode = rv->csr_mtvec & 0x3;

/* Exception Code: Breakpoint */
const uint32_t code = 3;

/* mepc (Machine Exception Program Counter)
* mtval(Machine Trap Value Register) : Breakpoint
*/
rv->csr_mepc = old_pc;
rv->csr_mtval = old_pc;

switch (mode) {
case 0: /* DIRECT: All exceptions set PC to base */
rv->PC = base;
break;
case 1: /* VECTORED: Asynchronous interrupts set PC to base + 4 * code */
rv->PC = base + 4 * code;
break;
}

/* mcause (Machine Cause Register): store exception code */
rv->csr_mcause = code;
}

/* RV32I Base Instruction Set
*
* bits 0-6: opcode
Expand Down Expand Up @@ -2189,3 +2220,9 @@ void rv_reset(struct riscv_t *rv, riscv_word_t pc)

rv->halt = false;
}

void ebreak_handler(struct riscv_t *rv)
{
assert(rv);
rv_except_breakpoint(rv, rv->PC);
}
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ int main(int argc, char **args)

/* system */
.on_ecall = syscall_handler,
.on_ebreak = rv_halt,
.on_ebreak = ebreak_handler,
};

state_t *state = state_new();
Expand Down
3 changes: 3 additions & 0 deletions src/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ riscv_word_t rv_get_reg(struct riscv_t *, uint32_t reg);
/* system call handler */
void syscall_handler(struct riscv_t *rv);

/* breakpoint exception handler */
void ebreak_handler(struct riscv_t *rv);

/* halt the core */
void rv_halt(struct riscv_t *);

Expand Down

0 comments on commit 331ce72

Please sign in to comment.