rvemu

RISC-V RV32I execution engine
git clone git://mcdim.xyz/revemu.git
Log | Files | Refs

commit 1b0c03d3ec03d206e74c17c7308f581f44332411
parent bcb8d4b9f6383baa8d150ef73f79e67280049a29
Author: Michail Konstantinos Dimopoulos <mk@mcdim.xyz>
Date:   Thu,  3 Sep 2026 23:38:06 +0300

Added fence

Diffstat:
Mdisasm.c | 16++++++++++++++++
Mmain.c | 30++++++++++++++++++++++++++----
2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/disasm.c b/disasm.c @@ -261,6 +261,21 @@ disasm_itype_control(uint32_t inst) } +static void +disasm_itype_fence(uint32_t inst) +{ + itype_t i; + decode_itype(&i, inst); + + if (i.funct3 != 0x0) { + puts("Illegal instruction"); + return; + } + + puts("fence"); + return; +} + typedef void (*disasm_fn)(uint32_t inst); static disasm_fn opcode_table[128]; @@ -273,6 +288,7 @@ init_opcode_table(void) opcode_table[0x03] = disasm_itype_loads; opcode_table[0x67] = disasm_itype_jalr; opcode_table[0x73] = disasm_itype_control; + opcode_table[0x0f] = disasm_itype_fence; opcode_table[0x23] = disasm_stype; opcode_table[0x63] = disasm_btype; opcode_table[0x6f] = disasm_jtype; diff --git a/main.c b/main.c @@ -410,7 +410,9 @@ static void exec_jtype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) { } } -static void exec_utype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) { +static void +exec_utype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) +{ (void)mem; /* suppressing unused parameters warning */ utype_t u; decode_utype(&u, inst); @@ -422,7 +424,8 @@ static void exec_utype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) { } static void -exec_utype_auipc(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) { +exec_utype_auipc(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) +{ (void)mem; /* suppressing unused parameters warning */ utype_t u; decode_utype(&u, inst); @@ -459,6 +462,21 @@ exec_itype_control(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) } static void +exec_itype_fence(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) +{ + (void)mem; /* suppressing unused parameters warning */ + itype_t i; + decode_itype(&i, inst); + + if (i.funct3 != 0x0) { + raise_trap(cpu, TRAP_ILLEGAL_INSTRUCTION, inst); + return; + } + + cpu->pc += 4; +} + +static void load_program(uint8_t *mem) { /* PROGRAM */ @@ -467,6 +485,8 @@ load_program(uint8_t *mem) 0x00600093, // addi x1, x0, 6 0xaaaaa3b7, // lui x7, 0xaaaaa -> rd:aaaaa000 0x00001397, // auipc x7 0x1 -> rd 1008 + 0x00000013, // nop + 0x0ff0000f, // fence 0x00100073, // ebreak }; size_t len = sizeof(arr) / sizeof(arr[0]); @@ -508,6 +528,7 @@ init_opcode_table(void) opcode_table[0x03] = exec_itype_loads; opcode_table[0x67] = exec_itype_jalr; opcode_table[0x73] = exec_itype_control; + opcode_table[0x0f] = exec_itype_fence; opcode_table[0x23] = exec_stype; opcode_table[0x63] = exec_btype; opcode_table[0x6f] = exec_jtype; @@ -516,7 +537,9 @@ init_opcode_table(void) } /***/ -int main() { +int +main() +{ init_opcode_table(); cpu_state_t cpu = {0}; @@ -574,7 +597,6 @@ int main() { case TRAP_NONE: printf("No trap cause\n"); break; - } return EXIT_FAILURE; }