Sync changes from laptop

This commit is contained in:
2025-02-04 15:14:56 +01:00
parent 0a0f953f09
commit e6f241bcae
2 changed files with 17 additions and 13 deletions

View File

@@ -31,8 +31,8 @@ void step(CPU *cpu) {
cpu->mode = Done; //terminate cpu->mode = Done; //terminate
} }
uint8_t opcode = read_mem(cpu, cpu->pc++); uint8_t opcode = read_mem(cpu, cpu->pc++);
uint8_t reg1, reg2, imm; uint8_t reg1, reg2, imm, temp;
uint32_t temp, newPC; uint32_t newPC, addrTemp;
int32_t cmpResult; int32_t cmpResult;
switch (opcode) { switch (opcode) {
case NOP: case NOP:
@@ -47,6 +47,9 @@ void step(CPU *cpu) {
case INC_RN: case INC_RN:
cpu->pc++; cpu->pc++;
reg1 = read_mem(cpu, cpu->pc++); reg1 = read_mem(cpu, cpu->pc++);
if (reg1 >= REG_COUNT) {
reg1 = REG_COUNT - 1;
}
cpu->regs[reg1]++; cpu->regs[reg1]++;
case INC_ADDR: case INC_ADDR:
@@ -313,9 +316,9 @@ void step(CPU *cpu) {
case JE_BIT_ADDR: { case JE_BIT_ADDR: {
// Jump if bit in register set // Jump if bit in register set
temp = read_mem32(cpu, cpu->pc); addrTemp = read_mem32(cpu, cpu->pc);
if (temp >= MEM_SIZE) { if (addrTemp >= MEM_SIZE) {
temp = MEM_SIZE - 1; addrTemp = MEM_SIZE - 1;
} }
uint8_t bit = read_mem(cpu, cpu->pc++); uint8_t bit = read_mem(cpu, cpu->pc++);
if (bit > 7) { if (bit > 7) {
@@ -323,7 +326,7 @@ void step(CPU *cpu) {
} }
newPC = read_mem32(cpu, cpu->pc); newPC = read_mem32(cpu, cpu->pc);
cpu->pc += 4; cpu->pc += 4;
if (cpu->memory[temp] & (1 << bit)) if (cpu->memory[addrTemp] & (1 << bit))
cpu->pc = newPC; cpu->pc = newPC;
break; break;
} }
@@ -357,9 +360,9 @@ void step(CPU *cpu) {
case JNE_BIT_ADDR: { case JNE_BIT_ADDR: {
// Jump if bit in register set // Jump if bit in register set
temp = read_mem32(cpu, cpu->pc); addrTemp = read_mem32(cpu, cpu->pc);
if (temp >= MEM_SIZE) { if (addrTemp >= MEM_SIZE) {
temp = MEM_SIZE - 1; addrTemp = MEM_SIZE - 1;
} }
uint8_t bit = read_mem(cpu, cpu->pc++); uint8_t bit = read_mem(cpu, cpu->pc++);
if (bit > 7) { if (bit > 7) {
@@ -367,7 +370,7 @@ void step(CPU *cpu) {
} }
newPC = read_mem32(cpu, cpu->pc); newPC = read_mem32(cpu, cpu->pc);
cpu->pc += 4; cpu->pc += 4;
if (!(cpu->memory[temp] & (1 << bit))) if (!(cpu->memory[addrTemp] & (1 << bit)))
cpu->pc = newPC; cpu->pc = newPC;
break; break;
} }
@@ -422,7 +425,8 @@ void step(CPU *cpu) {
newPC = read_mem32(cpu, cpu->pc); newPC = read_mem32(cpu, cpu->pc);
cpu->pc += 4; cpu->pc += 4;
// Push return address (current PC) onto the stack. // 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; cpu->pc = newPC;
break; break;
} }
@@ -435,7 +439,7 @@ void step(CPU *cpu) {
case PUSH: { case PUSH: {
// Push register value onto the stack. // Push register value onto the stack.
reg1 = read_mem(cpu, cpu->pc++); 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; break;
} }

View File

@@ -23,7 +23,7 @@ enum Mode {
// CPU state // CPU state
typedef struct { typedef struct {
uint32_t regs[REG_COUNT]; uint8_t regs[REG_COUNT];
uint8_t memory[MEM_SIZE]; uint8_t memory[MEM_SIZE];
uint32_t pc; // Program counter uint32_t pc; // Program counter
uint32_t sp; // Stack pointer uint32_t sp; // Stack pointer