Fix labels and jumps
This commit is contained in:
39
cpu/core.c
39
cpu/core.c
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user