summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralekseiplusplus <alekseijeaves@protonmail.com>2023-12-03 21:03:44 +1100
committeralekseiplusplus <alekseijeaves@protonmail.com>2023-12-03 21:03:44 +1100
commit78d3f650b2ca507e3d5376d3cad4d93df1901569 (patch)
tree997f7fae4c137af410e38f6acf790e2c7e20cebc /src
parent09288876a8080b2a643a8c644eb116c021f0c397 (diff)
making CPU more correct, STILL not working
Diffstat (limited to 'src')
-rw-r--r--src/apple.c12
-rw-r--r--src/cpu/6502.c11
-rw-r--r--src/cpu/addressing.c3
-rw-r--r--src/cpu/instructions.c94
4 files changed, 56 insertions, 64 deletions
diff --git a/src/apple.c b/src/apple.c
index 5ef87f6..5877bc8 100644
--- a/src/apple.c
+++ b/src/apple.c
@@ -62,18 +62,20 @@ void AppleReset(){
Memory = calloc(MEMORY_SIZE, sizeof(byte));
}
+
+//byte KBD_CR_val;
+
byte GetMemory(address x){
switch(x)
{
- // The keyboard does not act precisely as the real hardware, because there is no need to wait for the keyboard.
- // So KBD_CR and DSP always return a high high-order bit, and when the keyboard data is requested it will wait for UserInput() to finish.
+ // The keyboard does not act precisely as the real hardware, because there is no need to wait for the keyboard.
case KBD:
char c = -1;
while(c == -1)
c = UserInput();
return 0b10000000 | c;
case KBD_CR:
- return 0b10000000;
+ return 0b10100111;
case DSP:
return 0b00000000;
}
@@ -84,13 +86,15 @@ byte GetMemory(address x){
if (x < MEMORY_SIZE)
return Memory[x];
-
+
return -1;
}
void SetMemory(address x, byte y){
switch(x)
{
+ //case KBD_CR:
+ //KBD_CR_val = x;
case DSP:
if (y & 0x80)
DisplayInput(y);
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){