fix status flag ordering shenanigans

This commit is contained in:
Vineet K 2024-06-29 09:51:49 -04:00
parent 5d36aeb885
commit 4b7fae0010

56
cpu.c
View File

@ -326,13 +326,16 @@ brk(void)
static void static void
bvc(uint8_t arg) bvc(uint8_t arg)
{ {
if (regs.status.overflow == 0) //regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) == 0;
//if (regs.status.overflow == 0)
if ((STATUS_TO_INT() & (1 << 6)) == 0)
regs.pc += arg; regs.pc += arg;
} }
static void static void
bvs(uint8_t arg) bvs(uint8_t arg)
{ {
regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) != 0;
if (regs.status.overflow == 1) if (regs.status.overflow == 1)
regs.pc += arg; regs.pc += arg;
} }
@ -559,11 +562,7 @@ pha(void)
static void static void
php(void) php(void)
{ {
uint8_t status; PUSH(STATUS_TO_INT() | (1 << 4));
status = STATUS_TO_INT();
PUSH(status);
} }
static void static void
@ -582,14 +581,14 @@ plp(void)
status = PULL(); status = PULL();
regs.status.carry = (status & (1 << 7)) != 0; regs.status.carry = (status & 1) != 0;
regs.status.zero = (status & (1 << 6)) != 0; regs.status.zero = (status & (1 << 1)) != 0;
regs.status.interrupt_disable = (status & (1 << 5)) != 0; regs.status.interrupt_disable = (status & (1 << 2)) != 0;
regs.status.decimal_mode = (status & (1 << 4)) != 0; regs.status.decimal_mode = (status & (1 << 3)) != 0;
regs.status.brk = (status & (1 << 3)) != 0; regs.status.brk = 0;
regs.status.unused = (status & (1 << 2)) != 0; regs.status.unused = 1;
regs.status.overflow = (status & (1 << 1)) != 0; regs.status.overflow = (status & (1 << 6)) != 0;
regs.status.negative = (status & 1) != 0; regs.status.negative = (status & (1 << 7)) != 0;
} }
static void static void
@ -771,12 +770,14 @@ interpret(void)
uint16_t arg, mem, deref; uint16_t arg, mem, deref;
uint32_t cycles_; uint32_t cycles_;
enum addressing_mode mode; enum addressing_mode mode;
struct registers regs_;
for (;;) { for (;;) {
opcode = peek(regs.pc++); opcode = peek(regs.pc++);
cycles_ = cycles; cycles_ = cycles;
did_memwrite = 0; did_memwrite = 0;
status = STATUS_TO_INT(); status = STATUS_TO_INT();
regs_ = regs;
printf("%04X %02X", regs.pc - 1, opcode); printf("%04X %02X", regs.pc - 1, opcode);
@ -928,23 +929,26 @@ interpret(void)
printf("ASL"); printf("ASL");
break; break;
case 0x90: case 0x90:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
bcc(arg); bcc(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BCC"); printf("BCC");
break; break;
case 0xb0: case 0xb0:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
bcs(arg); bcs(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BCS"); printf("BCS");
break; break;
case 0xf0: case 0xf0:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
beq(arg); beq(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BEQ"); printf("BEQ");
break; break;
@ -989,23 +993,26 @@ interpret(void)
printf("BIT"); printf("BIT");
break; break;
case 0x30: case 0x30:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
bmi(arg); bmi(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BMI"); printf("BMI");
break; break;
case 0xd0: case 0xd0:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
bne(arg); bne(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BNE"); printf("BNE");
break; break;
case 0x10: case 0x10:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
bpl(arg); bpl(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BPL"); printf("BPL");
break; break;
@ -1016,16 +1023,18 @@ interpret(void)
printf("\tBRK"); printf("\tBRK");
goto loop_exit; goto loop_exit;
case 0x50: case 0x50:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
bvc(arg); bvc(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BVC"); printf("BVC");
break; break;
case 0x70: case 0x70:
mode = AM_IMM; mode = AM_REL;
arg = opcode_arg(mode); arg = opcode_arg(mode);
bvs(arg); bvs(arg);
arg += regs_.pc + 1;
cycles += 2; cycles += 2;
printf("BVS"); printf("BVS");
break; break;
@ -1884,6 +1893,7 @@ interpret(void)
if (did_memwrite) printf(" = %02X\t\t\t", ret); if (did_memwrite) printf(" = %02X\t\t\t", ret);
else printf("\t\t\t\t"); else printf("\t\t\t\t");
break; break;
case AM_REL:
case AM_ABS: case AM_ABS:
printf(" $%04X", arg); printf(" $%04X", arg);
if (did_memwrite) printf(" = %02X\t\t\t", ret); if (did_memwrite) printf(" = %02X\t\t\t", ret);
@ -1910,7 +1920,7 @@ interpret(void)
} }
printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X CYC:%d\n", printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X CYC:%d\n",
regs.a, regs.x, regs.y, status, regs.sp, cycles_); regs_.a, regs_.x, regs_.y, status, regs_.sp, cycles_);
} }
loop_exit: loop_exit:
} }