commit ed69a16140d8f2310eb22e84cd4856afc674e51e
parent 58dc93d516ee655d43250e283d455b3e59e7f12b
Author: Michail Konstantinos Dimopoulos <mk@mcdim.xyz>
Date: Fri, 24 Jul 2026 00:42:32 +0300
Crude debugger interface
Diffstat:
| M | main.c | | | 81 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
1 file changed, 43 insertions(+), 38 deletions(-)
diff --git a/main.c b/main.c
@@ -2,6 +2,7 @@
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#define MEMSIZE 2052
@@ -454,7 +455,8 @@ void exec_btype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) {
/**/uint32_t raw_imm = (imm_12 << 12) | (imm_11 << 11) | (imm_10_5 << 5) | (imm_4_1 << 1);
/**/if (raw_imm & (1U << 12))
/**/ raw_imm |= 0xffffe000U;
- b.imm = (int32_t)(0 | (imm_4_1 << 1) | (imm_10_5 << 5) | (imm_11 << 11) | (imm_12 << 12));
+ //b.imm = (int32_t)(0 | (imm_4_1 << 1) | (imm_10_5 << 5) | (imm_11 << 11) | (imm_12 << 12));
+ b.imm = (int32_t)raw_imm;
b.funct3 = (inst >> 12) & 7;
b.rs1 = (inst >> 15) & 31;
@@ -480,8 +482,6 @@ void exec_btype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) {
printf("\n");
/*********/
- //printf("PC:%d\n", cpu->pc);
- /**/printf("PC before: %" PRIu32 "\n", cpu->pc);
switch (b.funct3) {
case 0x0:
if (cpu->x[b.rs1] == cpu->x[b.rs2]) {
@@ -493,12 +493,8 @@ void exec_btype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) {
}
cpu->pc = target;
}
- case 0x0:
}
- //printf("PC:%d\n", cpu->pc);
- /**/printf("PC after: %" PRIu32 "\n", cpu->pc);
-
}
int main() {
@@ -538,46 +534,55 @@ int main() {
size_t len = sizeof(arr) / sizeof(arr[0]);
/* fetch & run */
- for (size_t i = 0; i < len; i++) {
- /*printf("\n>");
- scanf("%s\n", &a);
- char line[256];
- int i;
- if (fgets(line, sizeof(line), stdin)) {
+ size_t i = 0;
+ while (1) {
+ printf("\n>");
+ char line[256];
+ if (!fgets(line, sizeof(line), stdin)) {
+ fprintf(stderr, "Error reading interactive input\n");
+ }
- }*/
+ if (strncmp(line, "s", 1) == 0 || strncmp(line, "step", 4) == 0 ||
+ strncmp(line, "n", 1) == 0 || strncmp(line, "next", 4) == 0) {
+ if (i >= len) return 0;
- /* execute next instruction */
- uint32_t inst = arr[i];
+ uint32_t inst = arr[i];
- printf("------------INST%d------\n", i);
- print_bin32(inst);
- printf("\n");
+ printf("------------INST%d------\n", i);
+ print_bin32(inst);
+ printf("\n");
- /* decode opcode */
- uint16_t opcode = inst & 127;
- printf("opcode: ");
- print_binless(opcode,7);
- printf("\n");
+ /* decode opcode */
+ uint16_t opcode = inst & 127;
+ printf("opcode: ");
+ print_binless(opcode,7);
+ printf("\n");
- exec_fn fn = opcode_table[opcode];
+ exec_fn fn = opcode_table[opcode];
- if (!fn) {
- //illegal(NULL, inst);
- fprintf(stderr, "Illegal opcode\n");
- return EXIT_FAILURE;
- }
+ if (!fn) {
+ //illegal(NULL, inst);
+ fprintf(stderr, "Illegal opcode\n");
+ return EXIT_FAILURE;
+ }
- fn(&cpu,inst,mem);
- //printf("Memory:\n");
- //for (int i=0; i<32; i++) {
- // printf("0x%x: 0x%x\n", i, mem[i]);
- //}
+ fn(&cpu,inst,mem);
+ i++;
- printf("Registers:\n");
- for (int i=0; i<32; i++) {
- printf("0x%x: 0x%x\n", i, cpu.x[i]);
+ } else if (strncmp(line, "r", 1) == 0 || strncmp(line, "registers", 9) == 0) {
+ printf("Registers:\n");
+ for (int i=0; i<32; i++) {
+ printf("0x%x: 0x%x\n", i, cpu.x[i]);
+ }
+ } else if (strncmp(line, "m", 1) == 0 || strncmp(line, "memory", 6) == 0) {
+ printf("Memory:\n");
+ for (int i=0; i<32; i++) {
+ printf("0x%x: 0x%x\n", i, mem[i]);
+ }
+ } else if (strncmp(line, "ip", 2) == 0 || strncmp(line, "pc", 2) == 0) {
+ printf("Instruction poiter:\n");
+ printf("PC: %" PRIu32 "\n", cpu.pc);
}
}