Skip to content

Implement ebreak properly #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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