rvemu

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

commit d0c2de1bcca990a4caf8c6e4a29a4ae1ed5d3f78
parent 4446e4fbe7569a37b01c06a28e26ef64c45c6e8c
Author: Michail Konstantinos Dimopoulos <mk@mcdim.xyz>
Date:   Tue, 17 Mar 2026 01:06:02 +0200

R-type done

Diffstat:
Mmain.c | 110++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
1 file changed, 72 insertions(+), 38 deletions(-)

diff --git a/main.c b/main.c @@ -40,7 +40,7 @@ R-TYPE INSTRUCTION DECODING +------------+--------+--------+--------+---------+--------+ opcode 0x33 0110011 */ -void exec_op(cpu_state_t *cpu, uint32_t inst) { +void exec_rtype(cpu_state_t *cpu, uint32_t inst) { printf("TYPE R\n"); rtype_t r; r.rd = (inst >> 7) & 31; @@ -49,7 +49,7 @@ void exec_op(cpu_state_t *cpu, uint32_t inst) { r.rs2 = (inst >> 20) & 31; r.funct7 = (inst >> 25) & 127; - /* print */ + /**print**/ printf("f7 rs2 rs1 f3 rd opcode\n"); print_binless(r.funct7, 7); printf(" "); @@ -61,56 +61,79 @@ void exec_op(cpu_state_t *cpu, uint32_t inst) { printf(" "); print_binless(r.rd, 5); printf("\n"); + /*********/ - /* */ - + uint32_t x; + uint32_t shamt; switch(r.funct3) { case 0x0: - printf("HI\n\n"); switch(r.funct7) { case 0x00: printf("ADD\n"); - printf("rs1:%d\nrs2:%d\nrd:%d\n",cpu->x[r.rs1],cpu->x[r.rs2],cpu->x[r.rd]); - - int x = cpu->x[r.rs1]+cpu->x[r.rs2]; - - printf("%d\n", x); - - if (r.rd != 0) { - cpu->x[r.rd] = x; - } - - printf("rd:%d\n", cpu->x[r.rd]); - + x = cpu->x[r.rs1]+cpu->x[r.rs2]; break; case 0x20: printf("SUB\n"); - printf("rs1:%d\nrs2:%d\nrd:%d\n",cpu->x[r.rs1],cpu->x[r.rs2],cpu->x[r.rd]); - x = cpu->x[r.rs1]-cpu->x[r.rs2]; - - if (r.rd != 0) { - cpu->x[r.rd] = x; - } - - printf("rd:%d\n", cpu->x[r.rd]); break; + default: + fprintf(stderr, "Illegal funct7\n"); + return; } break; + case 0x1: + printf("Shift logical left\n"); + shamt = cpu->x[r.rs2] & 31; + x = cpu->x[r.rs1] << shamt; + break; + case 0x2: + printf("Set less than\n"); + x = ((int32_t)cpu->x[r.rs1] < (int32_t)cpu->x[r.rs2]) ? 1 : 0; + break; + case 0x3: + printf("Set less than U\n"); + x = (cpu->x[r.rs1] < cpu->x[r.rs2]) ? 1 : 0; + break; case 0x4: printf("XOR\n"); + x = cpu->x[r.rs1]^cpu->x[r.rs2]; + break; + case 0x5: + switch(r.funct7) { + case 0x00: + printf("Shift logical right\n"); + shamt = cpu->x[r.rs2] & 31; + x = cpu->x[r.rs1] >> shamt; + break; + case 0x20: + printf("Shift arithm right\n"); + shamt = cpu->x[r.rs2] & 31; + x = (uint32_t)((int32_t)cpu->x[r.rs1] >> shamt); + break; + default: + fprintf(stderr, "Illegal funct7\n"); + return; + } break; case 0x6: printf("OR\n"); + x = cpu->x[r.rs1]|cpu->x[r.rs2]; break; case 0x7: printf("AND\n"); - printf("rs1:%d\nrs2:%d\nrd:%d",cpu->x[r.rs1],cpu->x[r.rs2],cpu->x[r.rd]); - - int x = r.rs1&r.rs2; - printf("%d\n", x); + x = cpu->x[r.rs1]&cpu->x[r.rs2]; break; + default: + fprintf(stderr, "Illegal funct3\n"); + return; } + + printf("rs1:%u rs2:%u rd:%u\n",cpu->x[r.rs1],cpu->x[r.rs2],cpu->x[r.rd]); + if (r.rd != 0) { + cpu->x[r.rd] = x; + } + + printf("rd:%u\n", cpu->x[r.rd]); } int main() { @@ -118,8 +141,8 @@ int main() { /* function pointer dispatch */ typedef void (*exec_fn)(cpu_state_t *, uint32_t inst); static exec_fn opcode_table[128]; - opcode_table[0x33] = exec_op; - /*opcode_table[0x13] = exec_op_imm; + opcode_table[0x33] = exec_rtype; + /*opcode_table[0x13] = exec_rtype_imm; opcode_table[0x03] = exec_load; opcode_table[0x23] = exec_store; opcode_table[0x63] = exec_branch; @@ -132,26 +155,37 @@ int main() { uint32_t inst = 0x01F00033; // f7 rs2 rs1 f3 rd opcode - // 0000000 00001 00010 000 00000 0110011 - inst = 0x00110033; + // 0000000 00001 00010 000 00011 0110011 + inst = 0x00110033; /* failed add because rd=0 */ + + // f7 rs2 rs1 f3 rd opcode + // 0000000 00001 00010 000 00011 0110011 + inst = 0x001101B3; /* successful add */ // f7 rs2 rs1 f3 rd opcode // 0100000 00001 00010 000 00011 0110011 - //inst = 0x401101B3; + inst = 0x401101B3; /* successful sub */ + + // f7 rs2 rs1 f3 rd opcode + // 0000000 00001 00010 011 00011 0110011 + inst = 0x1131B3; /* SLT */ + + // f7 rs2 rs1 f3 rd opcode + // 0000000 00001 00010 100 00011 0110011 + //inst = 0x1141B3; /* XOR */ print_bin32(inst); printf("\n"); /* decode instruction */ uint16_t opcode = inst & 127; - printf("opcode:"); - printf("\n"); - print_bin16(opcode); + printf("opcode: "); + print_binless(opcode,7); printf("\n"); exec_fn fn = opcode_table[opcode]; cpu_state_t cpu; - cpu.x[1] = 5; + cpu.x[1] = 51; cpu.x[2] = 14;