rvemu

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

commit 57eaef5ec609fa7e243fca6494754866004db66b
parent e4f8168882930df9b5947a9ac146cb176c4fad53
Author: Michail Konstantinos Dimopoulos <mk@mcdim.xyz>
Date:   Mon, 23 Mar 2026 21:30:21 +0200

Added btype decoding and beq instruction

Diffstat:
Mmain.c | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 67 insertions(+), 4 deletions(-)

diff --git a/main.c b/main.c @@ -408,6 +408,62 @@ void exec_stype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) { } } +/* +B-TYPE INSTRUCTION FORMAT +31 25 24 20 19 15 14 12 11 7 6 0 ++---------------+--------+--------+--------+------------+--------+ +| imm[12|10:5] | rs2 | rs1 | funct3 |imm[4:1|11] | opcode | ++---------------+--------+--------+--------+------------+--------+ +opcode 0x63 1100010 +*/ +void exec_btype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) { + printf("TYPE B\n"); + stype_t b; + + uint32_t imm_10_5 = (inst >> 25) & 63; + uint32_t imm_11 = (inst >> 7) & 1; + uint32_t imm_4_1 = (inst >> 8) & 15; + uint32_t imm_12 = (inst >> 31) & 1; + b.imm = 0 | (imm_4_1 << 1) | (imm_10_5 << 5) | (imm_11 << 11) | (imm_12 << 12); + + b.funct3 = (inst >> 12) & 7; + b.rs1 = (inst >> 15) & 31; + b.rs2 = (inst >> 20) & 31; + + printf("\nimm:%x\n", b.imm); + + /**print**/ + printf("imm rs2 rs1 f3 imm opcode\n"); + printf("[12|10:5] [4:1|11]\n"); + print_binless(imm_12, 1); + print_binless(imm_10_5, 6); + printf(" "); + print_binless(b.rs2, 5); + printf(" "); + print_binless(b.rs1, 5); + printf(" "); + print_binless(b.funct3, 3); + printf(" "); + print_binless(imm_4_1, 4); + print_binless(imm_11, 1); + printf("\n"); + /*********/ + + printf("PC:%d\n", cpu->pc); + switch (b.funct3) { + case 0x0: + if (cpu->x[b.rs1] == cpu->x[b.rs2]) { + if (b.imm >= MEMSIZE) { + fprintf(stderr, "Illegal address\n"); + return; + } + cpu->pc += b.imm; + } + } + + printf("PC:%d\n", cpu->pc); +} + int main() { /* function pointer dispatch */ @@ -418,10 +474,8 @@ int main() { opcode_table[0x13] = exec_itype; opcode_table[0x03] = exec_ltype; opcode_table[0x23] = exec_stype; + opcode_table[0x63] = exec_btype; /*opcode_table[0x13] = exec_rtype_imm; - opcode_table[0x03] = exec_load; - opcode_table[0x23] = exec_store; - opcode_table[0x63] = exec_branch; opcode_table[0x73] = exec_system;*/ @@ -494,6 +548,11 @@ int main() { // 0000000 00000 00000 010 00011 0100011 inst = 0x21A3; /* STORE rs2 contents to mem 0x03, FAIL (word)*/ + // imm rs2 rs1 f3 imm opcode + // [12|10:5] [4:1|11] + // 0000000 00000 00000 000 00010 1100011 + inst = 0x163; /* branch to 2 if rs1 == rs2*/ + print_bin32(inst); printf("\n"); @@ -504,11 +563,15 @@ int main() { printf("\n"); exec_fn fn = opcode_table[opcode]; - cpu_state_t cpu; + /* init cpu state */ + cpu_state_t cpu; for (int i=0; i<32; i++) { cpu.x[i] = 0; } + cpu.pc = 0; + /* */ + cpu.x[1] = 51; cpu.x[2] = 00;