Skip to content

Commit

Permalink
Add the instruction c.ebreak
Browse files Browse the repository at this point in the history
  • Loading branch information
Risheng1128 committed Sep 28, 2022
1 parent 09f4e65 commit a6efc6b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
31 changes: 31 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;
}

void rv_except_breakpoint(struct riscv_t *rv)
{
/* 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 = rv->PC;
rv->csr_mtval = rv->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
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 = rv_except_breakpoint,
};

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);

/* trigger a breakpoint exception */
void rv_except_breakpoint(struct riscv_t *rv);

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

Expand Down

0 comments on commit a6efc6b

Please sign in to comment.