From 78d3f650b2ca507e3d5376d3cad4d93df1901569 Mon Sep 17 00:00:00 2001 From: alekseiplusplus Date: Sun, 3 Dec 2023 21:03:44 +1100 Subject: making CPU more correct, STILL not working --- src/cpu/instructions.c | 94 +++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 50 deletions(-) (limited to 'src/cpu/instructions.c') diff --git a/src/cpu/instructions.c b/src/cpu/instructions.c index 2a2f97f..5f4000a 100644 --- a/src/cpu/instructions.c +++ b/src/cpu/instructions.c @@ -19,14 +19,19 @@ AddData idata; void fLDA(Addressing addr, address val){ acc = idata.value; SetFlagN(acc); + SetFlagZ(acc); } void fLDX(Addressing addr, address val){ X = idata.value; + SetFlagN(X); + SetFlagZ(X); } void fLDY(Addressing addr, address val){ Y = idata.value; + SetFlagN(Y); + SetFlagZ(Y); } void fSTA(Addressing addr, address val){ @@ -44,29 +49,25 @@ void fSTY(Addressing addr, address val){ // Arithmetic Instructions void fADC(Addressing addr, address val){ - int buffer = (int)acc + idata.value; - SetFlagV(buffer, acc); + byte buffer = acc + idata.value + getFlag(flag_C); - if (buffer > 255) - flagSet(flag_C); - else - flagClear(flag_C); + SetFlagV(buffer, acc); - acc += idata.value; + SetFlag(flag_C, (buffer < acc) ? 1 : 0); + + acc = buffer; SetFlagN(acc); SetFlagZ(acc); } void fSBC(Addressing addr, address val){ - int buffer = acc - idata.value; + byte buffer = acc - idata.value - !getFlag(flag_C); + SetFlagV(buffer, acc); - if (buffer < 0) - flagSet(flag_C); - else - flagClear(flag_C); + SetFlag(flag_C, (buffer > acc) ? 1 : 0); - acc -= idata.value; + acc = buffer; SetFlagN(acc); SetFlagZ(acc); } @@ -75,10 +76,9 @@ void fSBC(Addressing addr, address val){ void fINC(Addressing addr, address val){ byte a = GetMemory(idata.add); - a++; - SetMemory(idata.add, a); - SetFlagN(Memory[idata.add]); - SetFlagZ(Memory[idata.add]); + SetMemory(idata.add, ++a); + SetFlagN(a); + SetFlagZ(a); } void fINX(Addressing addr, address val){ @@ -95,10 +95,9 @@ void fINY(Addressing addr, address val){ void fDEC(Addressing addr, address val){ byte a = GetMemory(idata.add); - a--; - SetMemory(idata.add, a); - SetFlagN(Memory[idata.add]); - SetFlagZ(Memory[idata.add]); + SetMemory(idata.add, --a); + SetFlagN(a); + SetFlagZ(a); } void fDEX(Addressing addr, address val){ @@ -136,7 +135,7 @@ void fEOR(Addressing addr, address val){ // Jump, Branch, Compare, and Test Bits void fJMP(Addressing addr, address val){ - PC = val; PC -= 2; + PC = idata.add - idata.length; } void fBCC(Addressing addr, address val){ @@ -172,38 +171,33 @@ void fBVC(Addressing addr, address val){ } void fCMP(Addressing addr, address val){ - SetFlag(flag_C, (acc >= val) ? 1 : 0); - SetFlag(flag_Z, (acc == val) ? 1 : 0); - SetFlag(flag_N, (acc & flag_N ) ? 1 : 0); + SetFlag(flag_C, (acc >= idata.value) ? 1 : 0); + SetFlag(flag_Z, (acc == idata.value) ? 1 : 0); + SetFlag(flag_N, (acc < idata.value) ? 1 : 0); } void fCPX(Addressing addr, address val){ - SetFlag(flag_C, (X >= val) ? 1 : 0); - SetFlag(flag_Z, (X == val) ? 1 : 0); - SetFlag(flag_N, (X & flag_N ) ? 1 : 0); + SetFlag(flag_C, (X >= idata.value) ? 1 : 0); + SetFlag(flag_Z, (X == idata.value) ? 1 : 0); + SetFlag(flag_N, (X < idata.value) ? 1 : 0); } void fCPY(Addressing addr, address val){ - SetFlag(flag_C, (Y >= val) ? 1 : 0); - SetFlag(flag_Z, (Y == val) ? 1 : 0); - SetFlag(flag_N, (Y & flag_N ) ? 1 : 0); + SetFlag(flag_C, (Y >= idata.value) ? 1 : 0); + SetFlag(flag_Z, (Y == idata.value) ? 1 : 0); + SetFlag(flag_N, (Y < idata.value) ? 1 : 0); } void fBIT(Addressing addr, address val){ - SetFlag(flag_N, (idata.value & flag_N)?1:0); - SetFlag(flag_V, (idata.value & flag_V)?1:0); - - if (((idata.value & flag_N) & (idata.value & flag_V)) == 0) { - flagSet(flag_Z); - } else { - flagSet(flag_Z); - } + SetFlag(flag_N, (idata.value & flag_N) ? 1 : 0); + SetFlag(flag_V, (idata.value & flag_V) ? 1 : 0); + SetFlag(flag_Z, (idata.value & acc) ? 0 : 1); } // Shift and Rotate Instructions void fASL(Addressing addr, address val){ - SetFlag(flag_C, (idata.value & 0x80)?1:0); + SetFlag(flag_C, (idata.value & 0x80)); acc = (idata.value << 1); SetFlagN(acc); SetFlagZ(acc); @@ -217,16 +211,16 @@ void fLSR(Addressing addr, address val){ } void fROL(Addressing addr, address val){ - SetFlag(flag_C, (val & 0x80)?1:0); - acc = (val << 1); + SetFlag(flag_C, (idata.value & 0x80)); + acc = (idata.value << 1); acc |= (getFlag(flag_C)?1:0); SetFlagN(acc); SetFlagZ(acc); } void fROR(Addressing addr, address val){ - SetFlag(flag_C, (val & 0x01)); - acc = (val >> 1); + SetFlag(flag_C, (idata.value & 0x01)); + acc = (idata.value >> 1); acc |= (getFlag(flag_C)?0x80:0); SetFlagN(acc); SetFlagZ(acc); @@ -236,14 +230,14 @@ void fROR(Addressing addr, address val){ void fTAX(Addressing addr, address val){ X = acc; - //SetFlagN(X); - //SetFlagZ(X); + SetFlagN(X); + SetFlagZ(X); } void fTAY(Addressing addr, address val){ Y = acc; - //SetFlagN(Y); - //SetFlagZ(Y); + SetFlagN(Y); + SetFlagZ(Y); } void fTXA(Addressing addr, address val){ @@ -285,7 +279,6 @@ void fPLP(Addressing addr, address val){ } // Subroutine Instructions -// NEED TO FINISH THESE void fJSR(Addressing addr, address val){ SetStack((PC+3) >> 8); @@ -294,8 +287,9 @@ void fJSR(Addressing addr, address val){ } void fRTS(Addressing addr, address val){ - PC = (address)(GetStack()) - 1; + PC = (address)(GetStack()); PC += ((address)(GetStack())) << 8; + PC -= 1; } void fRTI(Addressing addr, address val){ -- cgit v1.2.3