Fix labels and jumps

This commit is contained in:
2025-02-11 12:29:11 +01:00
parent f8b0307949
commit 0fc4040ad7
4 changed files with 57 additions and 139 deletions

View File

@@ -9,7 +9,7 @@
// Initialize CPU
void init_cpu(CPU *cpu) {
memset(cpu, 0, sizeof(CPU));
cpu->mode = CPU_MODE_HALTED;
cpu->mode = CPU_MODE_HALTED | CPU_MODE_SECOND;
}
// Helper function for setting flags in the CPU (here we assume bit0 is the Zero flag,
@@ -27,7 +27,7 @@ void step(CPU *cpu) {
if (cpu->mode & (CPU_MODE_HALTED | CPU_MODE_PAUSED | CPU_MODE_ERROR)) {
return;
}
if (cpu->pc >= MEM_SIZE) {
if (cpu->pc >= MEM_SIZE || cpu->pc >= cpu->programEnd) {
if (cpu->mode | CPU_MODE_LOOP) {
cpu->pc = 0;
} else {
@@ -385,8 +385,11 @@ void step(CPU *cpu) {
if (bit > 7) {
bit = 7;
}
if (temp & (1 << bit))
if (temp & (1 << bit)) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
case JMP_BIT_SET_ADDR: {
@@ -400,8 +403,11 @@ void step(CPU *cpu) {
bit = 7;
}
newPC = read_address_argument(cpu);
if (read_mem(cpu, addrTemp) & (1 << bit))
if (read_mem(cpu, addrTemp) & (1 << bit)) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -409,8 +415,11 @@ void step(CPU *cpu) {
case JE: {
// Jump if equal (Zero flag set)
newPC = read_address_argument(cpu);
if (cpu->flags & CPU_FLAG_ZERO)
if (cpu->flags & CPU_FLAG_ZERO) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -423,8 +432,11 @@ void step(CPU *cpu) {
bit = 7;
}
newPC = read_address_argument(cpu);
if (!(temp & (1 << bit)))
if (!(temp & (1 << bit))) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -436,8 +448,11 @@ void step(CPU *cpu) {
bit = 7;
}
newPC = read_address_argument(cpu);
if (!(read_mem(cpu, addrTemp) & (1 << bit)))
if (!(read_mem(cpu, addrTemp) & (1 << bit))) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -446,7 +461,9 @@ void step(CPU *cpu) {
newPC = read_address_argument(cpu);
if (!(cpu->flags & CPU_FLAG_ZERO)) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -455,7 +472,9 @@ void step(CPU *cpu) {
newPC = read_address_argument(cpu);
if (!(cpu->flags & CPU_FLAG_NEGATIVE) && !(cpu->flags & CPU_FLAG_ZERO)) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -464,7 +483,9 @@ void step(CPU *cpu) {
newPC = read_address_argument(cpu);
if (cpu->flags & CPU_FLAG_NEGATIVE) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -473,7 +494,9 @@ void step(CPU *cpu) {
newPC = read_address_argument(cpu);
if ((cpu->flags & CPU_FLAG_ZERO) || !(cpu->flags & CPU_FLAG_NEGATIVE)) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}
@@ -482,7 +505,9 @@ void step(CPU *cpu) {
newPC = read_address_argument(cpu);
if ((cpu->flags & CPU_FLAG_NEGATIVE) || (cpu->flags & CPU_FLAG_ZERO)) {
cpu->pc = newPC;
break;
}
cpu->pc += CPU_INSTRUCTION_SIZE;
break;
}

View File

@@ -35,6 +35,7 @@ typedef struct {
uint32_t pc; // Program counter
uint32_t stack_ptr; // Stack pointer
uint8_t flags; // Status flags
uint32_t programEnd;
uint8_t mode;
uint32_t cycle;
} CPU;