commit cae55a983faec0c7b20e685d5b6bb4fc3a58e88b
parent ed69a16140d8f2310eb22e84cd4856afc674e51e
Author: Michail Konstantinos Dimopoulos <mk@mcdim.xyz>
Date: Wed, 26 Aug 2026 18:28:40 +0300
Simplified main, some style changes
Diffstat:
| M | main.c | | | 60 | +++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 31 insertions(+), 29 deletions(-)
diff --git a/main.c b/main.c
@@ -497,32 +497,28 @@ void exec_btype(cpu_state_t *cpu, uint32_t inst, uint8_t *mem) {
}
+/* Function pointer dispatch */
+typedef void (*exec_fn)(cpu_state_t *, uint32_t inst, uint8_t *mem);
+
+static exec_fn opcode_table[128];
+
+static void
+init_opcode_table(void)
+{
+ opcode_table[0x33] = exec_rtype;
+ opcode_table[0x13] = exec_itype;
+ opcode_table[0x03] = exec_ltype;
+ opcode_table[0x23] = exec_stype;
+ opcode_table[0x63] = exec_btype;
+}
+/***/
+
int main() {
+ init_opcode_table();
- /* function pointer dispatch */
- typedef void (*exec_fn)(cpu_state_t *, uint32_t inst,
- uint8_t *mem);
- static exec_fn opcode_table[128];
- opcode_table[0x33] = exec_rtype;
- 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[0x73] = exec_system;*/
-
- /* init cpu state */
- cpu_state_t cpu;
- for (int i=0; i<32; i++) {
- cpu.x[i] = 0;
- }
- cpu.pc = 0;
- uint8_t mem[MEMSIZE] = {0};
+ cpu_state_t cpu = {0};
- /* set mem for test */
- for (int i=0; i<2056; i++) {
- mem[i] = (uint8_t)0; //i;
- }
+ uint8_t mem[MEMSIZE] = {0};
/* PROGRAM */
uint32_t arr[] = {
@@ -540,11 +536,14 @@ int main() {
printf("\n>");
char line[256];
if (!fgets(line, sizeof(line), stdin)) {
- fprintf(stderr, "Error reading interactive input\n");
+ 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 (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;
uint32_t inst = arr[i];
@@ -570,17 +569,20 @@ int main() {
fn(&cpu,inst,mem);
i++;
- } else if (strncmp(line, "r", 1) == 0 || strncmp(line, "registers", 9) == 0) {
+ } 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) {
+ } 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) {
+ } else if (strncmp(line, "ip", 2) == 0 ||
+ strncmp(line, "pc", 2) == 0) {
printf("Instruction poiter:\n");
printf("PC: %" PRIu32 "\n", cpu.pc);
}