fix more bugs

This commit is contained in:
Vineet K 2024-06-28 10:28:53 -04:00
parent 09692f45f6
commit c1a42045e6

53
cpu.c
View File

@ -138,15 +138,12 @@ opcode_arg(enum addressing_mode mode)
break; break;
case AM_ABS: case AM_ABS:
val = peek16(arg); val = peek16(arg);
printf("$%04X", arg);
break; break;
case AM_ABS_X: case AM_ABS_X:
val = peek16(arg + regs.x); val = peek16(arg + regs.x);
printf("$%04X", arg);
break; break;
case AM_ABS_Y: case AM_ABS_Y:
val = peek16(arg + regs.y); val = peek16(arg + regs.y);
printf("$%04X", arg);
break; break;
case AM_IND_X: case AM_IND_X:
val = peek(peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256); val = peek(peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256);
@ -155,7 +152,7 @@ opcode_arg(enum addressing_mode mode)
val = peek(peek(arg) + peek((arg + 1) % 256) * 256 + regs.y); val = peek(peek(arg) + peek((arg + 1) % 256) * 256 + regs.y);
break; break;
default: default:
fprintf(stderr, "INVALID ADDRESSING MODE\n"); fprintf(stderr, "INVALID ADDRESSING MODE %i\n", mode);
abort(); abort();
} }
@ -196,6 +193,9 @@ opcode_mem(enum addressing_mode mode)
case AM_ABS_Y: case AM_ABS_Y:
val = arg + regs.y; val = arg + regs.y;
break; break;
case AM_IND:
val = peek16(arg);
break;
case AM_IND_X: case AM_IND_X:
val = peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256; val = peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256;
break; break;
@ -754,7 +754,8 @@ tya(void)
static void static void
interpret(void) interpret(void)
{ {
uint8_t opcode, cycles_; uint8_t opcode;
uint32_t cycles_;
for (;;) { for (;;) {
opcode = peek(regs.pc++); opcode = peek(regs.pc++);
@ -793,11 +794,6 @@ interpret(void)
cycles += 4; cycles += 4;
printf("ADC"); printf("ADC");
break; break;
case 0x72:
adc(opcode_arg(AM_IND));
cycles += 5;
printf("ADC");
break;
case 0x61: case 0x61:
adc(opcode_arg(AM_IND_X)); adc(opcode_arg(AM_IND_X));
cycles += 6; cycles += 6;
@ -838,11 +834,6 @@ interpret(void)
cycles += 4; cycles += 4;
printf("AND"); printf("AND");
break; break;
case 0x32:
and(opcode_arg(AM_IND));
cycles += 5;
printf("AND");
break;
case 0x21: case 0x21:
and(opcode_arg(AM_IND_X)); and(opcode_arg(AM_IND_X));
cycles += 6; cycles += 6;
@ -998,11 +989,6 @@ interpret(void)
cycles += 4; cycles += 4;
printf("CMP"); printf("CMP");
break; break;
case 0xd2:
cmp(opcode_arg(AM_IND));
cycles += 5;
printf("CMP");
break;
case 0xc1: case 0xc1:
cmp(opcode_arg(AM_IND_X)); cmp(opcode_arg(AM_IND_X));
cycles += 6; cycles += 6;
@ -1103,11 +1089,6 @@ interpret(void)
cycles += 4; cycles += 4;
printf("EOR"); printf("EOR");
break; break;
case 0x52:
eor(opcode_arg(AM_IND));
cycles += 5;
printf("EOR");
break;
case 0x41: case 0x41:
eor(opcode_arg(AM_IND_X)); eor(opcode_arg(AM_IND_X));
cycles += 6; cycles += 6;
@ -1154,7 +1135,7 @@ interpret(void)
printf("JMP"); printf("JMP");
break; break;
case 0x6c: case 0x6c:
jmp(opcode_arg(AM_IND)); jmp(opcode_mem(AM_IND));
cycles += 6; cycles += 6;
printf("JMP"); printf("JMP");
break; break;
@ -1198,11 +1179,6 @@ interpret(void)
cycles += 4; cycles += 4;
printf("LDA"); printf("LDA");
break; break;
case 0xb2:
lda(opcode_arg(AM_IND));
cycles += 5;
printf("LDA");
break;
case 0xa1: case 0xa1:
lda(opcode_arg(AM_IND_X)); lda(opcode_arg(AM_IND_X));
cycles += 6; cycles += 6;
@ -1323,11 +1299,6 @@ interpret(void)
cycles += 4; cycles += 4;
printf("ORA"); printf("ORA");
break; break;
case 0x12:
ora(opcode_arg(AM_IND));
cycles += 5;
printf("ORA");
break;
case 0x01: case 0x01:
ora(opcode_arg(AM_IND_X)); ora(opcode_arg(AM_IND_X));
cycles += 6; cycles += 6;
@ -1448,11 +1419,6 @@ interpret(void)
cycles += 4; cycles += 4;
printf("SBC"); printf("SBC");
break; break;
case 0xf2:
sbc(opcode_arg(AM_IND));
cycles += 5;
printf("SBC");
break;
case 0xe1: case 0xe1:
sbc(opcode_arg(AM_IND_X)); sbc(opcode_arg(AM_IND_X));
cycles += 6; cycles += 6;
@ -1503,11 +1469,6 @@ interpret(void)
cycles += 6; cycles += 6;
printf("STA"); printf("STA");
break; break;
case 0x92:
sta(opcode_mem(AM_IND));
cycles += 6;
printf("STA");
break;
case 0x81: case 0x81:
sta(opcode_mem(AM_IND_X)); sta(opcode_mem(AM_IND_X));
cycles += 7; cycles += 7;