From e6f241bcae2bace6f81086e820348ff68d4860bc Mon Sep 17 00:00:00 2001 From: bruno Date: Tue, 4 Feb 2025 15:14:56 +0100 Subject: [PATCH] Sync changes from laptop --- cpu/core.c | 28 ++++++++++++++++------------ cpu/core.h | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cpu/core.c b/cpu/core.c index 8b9a048..a712bd8 100644 --- a/cpu/core.c +++ b/cpu/core.c @@ -31,8 +31,8 @@ void step(CPU *cpu) { cpu->mode = Done; //terminate } uint8_t opcode = read_mem(cpu, cpu->pc++); - uint8_t reg1, reg2, imm; - uint32_t temp, newPC; + uint8_t reg1, reg2, imm, temp; + uint32_t newPC, addrTemp; int32_t cmpResult; switch (opcode) { case NOP: @@ -47,6 +47,9 @@ void step(CPU *cpu) { case INC_RN: cpu->pc++; reg1 = read_mem(cpu, cpu->pc++); + if (reg1 >= REG_COUNT) { + reg1 = REG_COUNT - 1; + } cpu->regs[reg1]++; case INC_ADDR: @@ -313,9 +316,9 @@ void step(CPU *cpu) { case JE_BIT_ADDR: { // Jump if bit in register set - temp = read_mem32(cpu, cpu->pc); - if (temp >= MEM_SIZE) { - temp = MEM_SIZE - 1; + addrTemp = read_mem32(cpu, cpu->pc); + if (addrTemp >= MEM_SIZE) { + addrTemp = MEM_SIZE - 1; } uint8_t bit = read_mem(cpu, cpu->pc++); if (bit > 7) { @@ -323,7 +326,7 @@ void step(CPU *cpu) { } newPC = read_mem32(cpu, cpu->pc); cpu->pc += 4; - if (cpu->memory[temp] & (1 << bit)) + if (cpu->memory[addrTemp] & (1 << bit)) cpu->pc = newPC; break; } @@ -357,9 +360,9 @@ void step(CPU *cpu) { case JNE_BIT_ADDR: { // Jump if bit in register set - temp = read_mem32(cpu, cpu->pc); - if (temp >= MEM_SIZE) { - temp = MEM_SIZE - 1; + addrTemp = read_mem32(cpu, cpu->pc); + if (addrTemp >= MEM_SIZE) { + addrTemp = MEM_SIZE - 1; } uint8_t bit = read_mem(cpu, cpu->pc++); if (bit > 7) { @@ -367,7 +370,7 @@ void step(CPU *cpu) { } newPC = read_mem32(cpu, cpu->pc); cpu->pc += 4; - if (!(cpu->memory[temp] & (1 << bit))) + if (!(cpu->memory[addrTemp] & (1 << bit))) cpu->pc = newPC; break; } @@ -422,7 +425,8 @@ void step(CPU *cpu) { newPC = read_mem32(cpu, cpu->pc); cpu->pc += 4; // Push return address (current PC) onto the stack. - write_mem(cpu, cpu->sp--, (uint8_t) (cpu->pc & 0xFF)); + write_mem32(cpu, cpu->sp, cpu->pc); + cpu->sp -= 4; cpu->pc = newPC; break; } @@ -435,7 +439,7 @@ void step(CPU *cpu) { case PUSH: { // Push register value onto the stack. reg1 = read_mem(cpu, cpu->pc++); - write_mem(cpu, cpu->sp--, (uint8_t) (cpu->regs[reg1] & 0xFF)); + write_mem(cpu, cpu->sp--, (uint8_t) cpu->regs[reg1]); break; } diff --git a/cpu/core.h b/cpu/core.h index 6fa30cb..8392151 100644 --- a/cpu/core.h +++ b/cpu/core.h @@ -23,7 +23,7 @@ enum Mode { // CPU state typedef struct { - uint32_t regs[REG_COUNT]; + uint8_t regs[REG_COUNT]; uint8_t memory[MEM_SIZE]; uint32_t pc; // Program counter uint32_t sp; // Stack pointer