rvemu

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit b6f38b53eb40e2eab52f93ca2b5bd2a5f1a27aa9
parent 50e9877a99f750e157f402ab7cfefb35f06514a9
Author: Michail Konstantinos Dimopoulos <mk@mcdim.xyz>
Date:   Thu, 27 Aug 2026 05:43:30 +0300

B-type complete

Diffstat:
Mmain.c | 96++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 81 insertions(+), 15 deletions(-)

diff --git a/main.c b/main.c @@ -488,19 +488,49 @@ void exec_btype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) { printf("\n"); /*********/ + int jump = 0; switch (b.funct3) { case 0x0: - if (cpu->x[b.rs1] == cpu->x[b.rs2]) { - uint32_t target; - if (get_valid_branch_target(cpu->pc, b.imm, &target) != 0) { - //fprintf(stderr, "Illegal branch target\n"); - /**/fprintf(stderr, "Illegal BEQ target: pc=%" PRIu32 ", offset=%" PRId32 "\n", cpu->pc, b.imm); - return; - } - cpu->pc = target; - } else { - cpu->pc += 4; + if (cpu->x[b.rs1] == cpu->x[b.rs2]) + jump = 1; + break; + case 0x1: + if (cpu->x[b.rs1] != cpu->x[b.rs2]) + jump = 1; + break; + case 0x4: + if ((int32_t)cpu->x[b.rs1] < (int32_t)cpu->x[b.rs2]) + jump = 1; + break; + case 0x5: + if ((int32_t)cpu->x[b.rs1] >= (int32_t)cpu->x[b.rs2]) + jump = 1; + break; + case 0x6: + if (cpu->x[b.rs1] < cpu->x[b.rs2]) + jump = 1; + break; + case 0x7: + if (cpu->x[b.rs1] >= cpu->x[b.rs2]) + jump = 1; + break; + default: + fprintf(stderr, "Illegal funct3\n"); + return; + } + + if (jump == 1) { + uint32_t target; + if (get_valid_branch_target(cpu->pc, b.imm, &target) != 0) + { + fprintf(stderr, "Illegal branch target: pc=%" + PRIu32 ", offset=%" PRId32 "\n", + cpu->pc, b.imm); + return; } + cpu->pc = target; + } else { + cpu->pc += 4; } } @@ -509,11 +539,19 @@ void load_program(uint8_t *mem) { /* PROGRAM */ + + /*uint32_t arr[] = { + 0x500193, // addi x3, x0, 5 + 0x600093, // addi x1, x0, 6 + 0x118263, // beq x3, x1. +4 + 0x728263 // beq x5, x7, +4 + }; */ + uint32_t arr[] = { - 0x500193, // addi x3, x0, 5 - 0x600093, // addi x1, x0, 6 - 0x118263, // beq x3, x1. +4 - 0x728263 // beq x5, x7, +4 + 0x00500193, // addi x3, x0, 5 + 0x00600093, // addi x1, x0, 6 + 0x00118263, // beq x3, x1. +4 + 0xfe728ae3 // beq x5, x7, -12 -> addr 0 }; size_t len = sizeof(arr) / sizeof(arr[0]); @@ -580,7 +618,6 @@ int main() { strncmp(line, "step", 4) == 0 || strncmp(line, "n", 1) == 0 || strncmp(line, "next", 4) == 0) { - //if (i >= len) return 0; uint32_t inst = make_instruction(mem, cpu.pc); @@ -625,3 +662,32 @@ int main() { return 0; } + +/* +int main() { + init_opcode_table(); + + cpu_state_t cpu = {0}; + + uint8_t mem[MEMSIZE] = {0}; + + load_program(mem); + + while (1) { + uint32_t inst = make_instruction(mem, cpu.pc); + + uint16_t opcode = inst & 127; + exec_fn fn = opcode_table[opcode]; + + if (!fn) { + //illegal(NULL, inst); + fprintf(stderr, "Illegal opcode\n"); + return EXIT_FAILURE; + } + + fn(&cpu,inst,mem); + } + + return 0; +} +*/