summaryrefslogtreecommitdiff
path: root/src/cpu
diff options
context:
space:
mode:
Diffstat (limited to 'src/cpu')
-rw-r--r--src/cpu/6502.c11
-rw-r--r--src/cpu/addressing.c3
-rw-r--r--src/cpu/instructions.c94
3 files changed, 48 insertions, 60 deletions
diff --git a/src/cpu/6502.c b/src/cpu/6502.c
index 366882f..e7c1669 100644
--- a/src/cpu/6502.c
+++ b/src/cpu/6502.c
@@ -8,18 +8,11 @@ address PC = 0x0000;
byte* Memory;
byte getFlag(byte flag) {
- return ((P & flag) == flag) ? 1 : 0;
+ return (P & flag) ? 1 : 0;
}
void SetFlag(byte flag, int x) {
- if (x == 1){
- if ((P & flag) == 0x0) P += flag;
- }else if (x == 0){
- if ((P & flag) == flag) P -= flag;
- }
- else{
- fprintf(stderr, "SetFlag() passed arg neither 0 or 1");
- }
+ P = (P & ~flag) | ((x != 0) ? flag : 0);
}
void flagSet(byte flag){
diff --git a/src/cpu/addressing.c b/src/cpu/addressing.c
index f381e51..fd9bf7e 100644
--- a/src/cpu/addressing.c
+++ b/src/cpu/addressing.c
@@ -11,13 +11,14 @@ void* current_instruction;
address fAddressGetAddress(Addressing mode, short x) {
switch(mode){
case eImplied:
- case eIndirectAbsolute:
case eRelative:
case eImmediate:
case eAccumulator:
return 0x0000;
case eAbsolute:
return x;
+ case eIndirectAbsolute:
+ return GetMemory(x) + ((address)GetMemory(x+1) << 8);
case eAbsoluteIndexedX:
return x + X;
case eAbsoluteIndexedY:
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){