mirror of
https://github.com/nobuh/napple1.git
synced 2024-12-21 23:29:35 +00:00
Fix errors in 6502 emulation
This commit fixes the following behaviors: Indirect X zero page addressing should always wrap and stay on zero page BRK should disable interrupts ADC in decimal mode should carry between nibbles based on adjusted, not original values. Writes of status register to the stack should always set M and should set B on BRK/PHP and ~B on IRQ/NMI Found and tested against https://github.com/Klaus2m5/6502_65C02_functional_tests/blob/master/6502_functional_test.a65
This commit is contained in:
parent
a6f12814f9
commit
032aacee39
14
src/m6502.c
14
src/m6502.c
@ -40,7 +40,7 @@
|
|||||||
#define Z 0x02
|
#define Z 0x02
|
||||||
#define C 0x01
|
#define C 0x01
|
||||||
|
|
||||||
static unsigned char accumulator, xRegister, yRegister, statusRegister = 0x24, stackPointer;
|
static unsigned char accumulator, xRegister, yRegister, statusRegister = I, stackPointer;
|
||||||
static int IRQ = 0, NMI = 0;
|
static int IRQ = 0, NMI = 0;
|
||||||
static unsigned short programCounter;
|
static unsigned short programCounter;
|
||||||
static unsigned char btmp;
|
static unsigned char btmp;
|
||||||
@ -101,7 +101,7 @@ static void popProgramCounter(void)
|
|||||||
static void handleIRQ(void)
|
static void handleIRQ(void)
|
||||||
{
|
{
|
||||||
pushProgramCounter();
|
pushProgramCounter();
|
||||||
memWrite((unsigned short)(0x100 + stackPointer), (unsigned char)(statusRegister & ~0x10));
|
memWrite((unsigned short)(0x100 + stackPointer), (unsigned char)((statusRegister & ~B) | M));
|
||||||
stackPointer--;
|
stackPointer--;
|
||||||
statusRegister |= I;
|
statusRegister |= I;
|
||||||
programCounter = memReadAbsolute(0xFFFE);
|
programCounter = memReadAbsolute(0xFFFE);
|
||||||
@ -111,7 +111,7 @@ static void handleIRQ(void)
|
|||||||
static void handleNMI(void)
|
static void handleNMI(void)
|
||||||
{
|
{
|
||||||
pushProgramCounter();
|
pushProgramCounter();
|
||||||
memWrite((unsigned short)(0x100 + stackPointer), (unsigned char)(statusRegister & ~0x10));
|
memWrite((unsigned short)(0x100 + stackPointer), (unsigned char)((statusRegister & ~B) | M));
|
||||||
stackPointer--;
|
stackPointer--;
|
||||||
statusRegister |= I;
|
statusRegister |= I;
|
||||||
NMI = 0;
|
NMI = 0;
|
||||||
@ -190,7 +190,7 @@ static void Ind(void)
|
|||||||
|
|
||||||
static void IndZeroX(void)
|
static void IndZeroX(void)
|
||||||
{
|
{
|
||||||
ptr = xRegister + memRead(programCounter++);
|
ptr = (xRegister + memRead(programCounter++)) & 0xFF;
|
||||||
op = memRead(ptr);
|
op = memRead(ptr);
|
||||||
op += memRead((unsigned short)((ptr + 1) & 0xFF)) << 8;
|
op += memRead((unsigned short)((ptr + 1) & 0xFF)) << 8;
|
||||||
cycles += 3;
|
cycles += 3;
|
||||||
@ -319,7 +319,7 @@ static void ADC(void)
|
|||||||
|
|
||||||
tmp = (Op1 & 0x0F) + (Op2 & 0x0F) + (statusRegister & C ? 1 : 0);
|
tmp = (Op1 & 0x0F) + (Op2 & 0x0F) + (statusRegister & C ? 1 : 0);
|
||||||
accumulator = tmp < 0x0A ? tmp : tmp + 6;
|
accumulator = tmp < 0x0A ? tmp : tmp + 6;
|
||||||
tmp = (Op1 & 0xF0) + (Op2 & 0xF0) + (tmp & 0xF0);
|
tmp = (Op1 & 0xF0) + (Op2 & 0xF0) + (accumulator & 0xF0);
|
||||||
|
|
||||||
if (tmp & 0x80)
|
if (tmp & 0x80)
|
||||||
statusRegister |= N;
|
statusRegister |= N;
|
||||||
@ -617,7 +617,7 @@ static void PHA(void)
|
|||||||
|
|
||||||
static void PHP(void)
|
static void PHP(void)
|
||||||
{
|
{
|
||||||
memWrite((unsigned short)(0x100 + stackPointer), statusRegister);
|
memWrite((unsigned short)(0x100 + stackPointer), statusRegister | B | M);
|
||||||
stackPointer--;
|
stackPointer--;
|
||||||
cycles++;
|
cycles++;
|
||||||
}
|
}
|
||||||
@ -641,7 +641,7 @@ static void BRK(void)
|
|||||||
{
|
{
|
||||||
pushProgramCounter();
|
pushProgramCounter();
|
||||||
PHP();
|
PHP();
|
||||||
statusRegister |= B;
|
statusRegister |= I;
|
||||||
programCounter = memReadAbsolute(0xFFFE);
|
programCounter = memReadAbsolute(0xFFFE);
|
||||||
cycles += 3;
|
cycles += 3;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user