summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralekseiplusplus <alekseijeaves@protonmail.com>2023-07-27 11:36:01 +1000
committeralekseiplusplus <alekseijeaves@protonmail.com>2023-07-27 11:36:01 +1000
commit9a6188f821b11b69fff3d3a303dbfcce2e52e6f4 (patch)
treeb06aa0d876b19cca281cbc26db9086ab7241d6bd /src
parentb158eaeb489bce502198e844593b38a2f5f5b9ee (diff)
refactoring of src/cpu/
Diffstat (limited to 'src')
-rw-r--r--src/apple.h1
-rw-r--r--src/cpu/6502.c89
-rw-r--r--src/cpu/6502.h107
-rw-r--r--src/cpu/addressing.c184
-rw-r--r--src/cpu/addressing.h188
-rw-r--r--src/cpu/core.h9
-rw-r--r--src/cpu/instructions.c (renamed from src/cpu/instructions/definitions.h)197
-rw-r--r--src/cpu/instructions.h (renamed from src/cpu/instructions/init.h)24
-rw-r--r--src/cpu/instructions/illegal/definitions.h0
-rw-r--r--src/cpu/instructions/illegal/init.h0
-rw-r--r--src/cpu/instructions/illegal/table-append.h0
-rw-r--r--src/cpu/table.c (renamed from src/cpu/instructions/table.h)17
-rw-r--r--src/cpu/table.h30
-rw-r--r--src/main.c23
-rw-r--r--src/signetics.h13
15 files changed, 524 insertions, 358 deletions
diff --git a/src/apple.h b/src/apple.h
index 5946374..036f48c 100644
--- a/src/apple.h
+++ b/src/apple.h
@@ -28,7 +28,6 @@ void AppleReset(){
}
-
byte getMemory(address x){
return Memory[x];
}
diff --git a/src/cpu/6502.c b/src/cpu/6502.c
new file mode 100644
index 0000000..d78cd7e
--- /dev/null
+++ b/src/cpu/6502.c
@@ -0,0 +1,89 @@
+// 6502.h
+// Main elements of the 6502 CPU
+
+#include"6502.h"
+
+
+byte getFlag(byte flag) {
+ return ((P & flag) == 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");
+ }
+}
+
+void flagSet(byte flag){
+ P |= flag;
+}
+
+void flagClear(byte flag){
+ P &= ~flag;
+}
+
+// Functions which perform reusable routines for finding if a specific flag should be set.
+
+void setFlagN(byte x){
+ if (x & flag_N == flag_N)
+ setFlag(flag_N, 1);
+ else
+ setFlag(flag_N, 0);
+}
+
+//Perform prior to any changes
+void setFlagV(byte x, byte y){
+ if ((x & flag_N) == (y & flag_N)){
+ if (((x + y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1);
+ else setFlag(flag_V, 0);
+ }else{
+ if (((x - y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1);
+ else setFlag(flag_V, 0);
+ }
+}
+
+/*void setFlagB(){ //WORK ON
+ setFlag(flag_B, 1);
+}*/
+
+/*void setFlagD(){
+ setFlag(flag_D, 1);
+}*/
+
+/*void setFlagI(){ //WORK ON
+ setFlag(flag_Z, 1);
+}*/
+
+void setFlagZ(int x){
+ if (x == 0)
+ setFlag(flag_Z, 1);
+ else
+ setFlag(flag_Z, 0);
+}
+
+/*void setFlagC(){
+ setFlag(flag_Z, 1);
+}*/
+
+
+/*byte getMemory(address x){
+ return Memory[x];
+}
+
+void setMemory(address x, byte y){
+ Memory[x] = y;
+}*/
+
+
+byte getStack() {
+ return getMemory(0x01FF - S);
+}
+
+void setStack(byte z) {
+ setMemory(0x01FF - S, z);
+} \ No newline at end of file
diff --git a/src/cpu/6502.h b/src/cpu/6502.h
index 66526fd..c455e57 100644
--- a/src/cpu/6502.h
+++ b/src/cpu/6502.h
@@ -1,10 +1,10 @@
// 6502.h
-// Core elements of the 6502 CPU
+// Main elements of the 6502 CPU
-typedef unsigned char
- byte;
-typedef unsigned short
- address;
+#ifndef CPU_6502_H
+#define CPU_6502_H
+
+#include"core.h"
byte acc, X, Y, P, S = 0x00;
address PC = 0x0000;
@@ -20,89 +20,52 @@ byte* ROM;
#define flag_Z 0x02 // Zero
#define flag_C 0x01 // Carry
-byte getFlag(byte flag) {
- return ((P & flag) == 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");
- }
-}
+byte getFlag(byte flag);
+// Get the value of a flag.
-void flagSet(byte flag){
- P |= flag;
-}
+void setFlag(byte flag, int x);
+// Set a flag with some value.
-void flagClear(byte flag){
- P &= ~flag;
-}
+void flagSet(byte flag);
+// Sets some flag.
+void flagClear(byte flag);
+// Clears some flag.
// Functions which perform reusable routines for finding if a specific flag should be set.
-void setFlagN(byte x){
- if (x & flag_N == flag_N)
- setFlag(flag_N, 1);
- else
- setFlag(flag_N, 0);
-}
+void setFlagN(byte x);
//Perform prior to any changes
-void setFlagV(byte x, byte y){
- if ((x & flag_N) == (y & flag_N)){
- if (((x + y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1);
- else setFlag(flag_V, 0);
- }else{
- if (((x - y) & (flag_N ^ 0xFF)) > 0x7F) setFlag(flag_V, 1);
- else setFlag(flag_V, 0);
- }
-}
-
-/*void setFlagB(){ //WORK ON
- setFlag(flag_B, 1);
-}*/ //Dont really need since its dependent on the BRK insturction
-
-/*void setFlagD(){
- setFlag(flag_D, 1);
-}*/
-
-/*void setFlagI(){ //WORK ON
- setFlag(flag_Z, 1);
-}*/
-
-void setFlagZ(int x){
- if (x == 0)
- setFlag(flag_Z, 1);
- else
- setFlag(flag_Z, 0);
-}
+void setFlagV(byte x, byte y);
-//Only 6 instructions, 2 not including stack instructions, use the carry flag.
-// Need to look further into implementation details for this.
-/*void setFlagC(){
- setFlag(flag_Z, 1);
-}*/
+//void setFlagB();
+//May not need since its dependent on the BRK insturction
+//void setFlagD();
+//Might not be necessary.
-// Memory Manipulation
+//void setFlagI();
+//Need to work on.
-// Are to be defined by the system to handle special cases.
-
-byte getMemory(address x);
-void setMemory(address x, byte y);
+void setFlagZ(int x);
+//void setFlagC();
+//Only 6 instructions, 2 not including stack instructions, use the carry flag.
+// The following two may be better defined within apple.c
+//byte getMemory(address x);
+// Retrieve value from the computers address space.
+// It is important not to directly access the memory array because some value hold special meaning.
+//void setMemory(address x, byte y);
+// Set a piece of memory to some value.
+byte getStack();
+// Get top value of the stack.
+void setStack(byte z);
+// Set top value of the stack.
-#include"addressing.h"
-#include"instructions/definitions.h"
-#include"instructions/table.h" \ No newline at end of file
+#endif \ No newline at end of file
diff --git a/src/cpu/addressing.c b/src/cpu/addressing.c
new file mode 100644
index 0000000..6e7d950
--- /dev/null
+++ b/src/cpu/addressing.c
@@ -0,0 +1,184 @@
+// addressing.h
+// Contains definitions relevant to addressing, as well as fAddress() which returns time, length, value, and address for an instruction function call.
+
+#include"addressing.h"
+
+#define getInstructionLength(c) fAddressGetLength(*getInstructionTableAddressing(c))
+
+int fAddressGetLength(Addressing addr){
+ switch(addr){
+ case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
+ return 3;
+ case eAccumulator: case eImplied:
+ return 1;
+ default:
+ return 2;
+ }
+}
+
+AddData fAddress(Addressing addr, short x) {
+ AddData ret;
+
+ // ADDRESS
+
+ switch(addr){
+ case eImplied:
+ case eIndirectAbsolute:
+ case eRelative:
+ case eImmediate:
+ case eAccumulator:
+ ret.add = 0x0000;
+ break;
+
+ case eAbsolute:
+ ret.add = x;
+ break;
+ case eAbsoluteIndexedX:
+ ret.add = (x + X);
+ break;
+ case eAbsoluteIndexedY:
+ ret.add = (x + Y);
+ break;
+
+ case eZeroPage:
+ ret.add = (x & 0x00FF);
+ break;
+ case eZeroPageIndexedX:
+ ret.add = ((x + X) & 0x00FF);
+ break;
+ case eZeroPageIndexedY:
+ ret.add = ((x + Y) & 0x00FF);
+ break;
+
+ case eIndexedIndirect:
+ ret.add = ((getMemory(x+X+1))<<8) + (getMemory(x+X));
+ break;
+ case eIndirectIndexed:
+ ret.add = ((getMemory(x+1))<<8) + (getMemory(x)) + Y;
+ break;
+ }
+
+ // VALUE
+
+ switch(addr){
+ case eImplied:
+ case eIndirectAbsolute:
+ case eRelative:
+ break;
+
+ case eImmediate:
+ ret.value = x;
+ break;
+
+ case eAccumulator:
+ ret.value = acc;
+ break;
+
+ default:
+ ret.value = getMemory(ret.add);
+ }
+
+ // LENGTH
+
+ ret.length = fAddressGetLength(addr);
+
+ // CYCLES
+
+ //case &fADC: case &fAND: case &fBIT: case &fCMP: case &fCPX: case &fCPY: case &fEOR: case &fLDA:
+ //case &fLDX: case &fLDY: case &fORA: case &fSBC: case &fSTX: case &fSTY:
+
+ if ( current_instruction == &fADC || current_instruction == &fAND || current_instruction == &fBIT || current_instruction == &fCMP || current_instruction == &fCPX
+ || current_instruction == &fCPY || current_instruction == &fEOR || current_instruction == &fLDA || current_instruction == &fLDX || current_instruction == &fLDY
+ || current_instruction == &fORA || current_instruction == &fSBC || current_instruction == &fSTX || current_instruction == &fSTY ){
+ switch(addr){
+ case eImmediate:
+ ret.cycles = 2; break;
+ case eZeroPage:
+ ret.cycles = 3; break;
+ case eZeroPageIndexedX: case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
+ ret.cycles = 4; break;
+ case eIndexedIndirect:
+ ret.cycles = 6; break;
+ case eIndirectIndexed:
+ ret.cycles = 5; break;
+ }
+ }
+
+ //case &fASL: case &fDEC: case &fINC: case &fLSR: case &fROL: case &fROR:
+ else if( current_instruction == &fASL || current_instruction == &fDEC || current_instruction == &fINC
+ || current_instruction == &fLSR || current_instruction == &fROL || current_instruction == &fROR ){
+ switch(addr){
+ case eAccumulator:
+ ret.cycles = 2; break;
+ case eZeroPage:
+ ret.cycles = 5; break;
+ case eZeroPageIndexedX: case eAbsolute:
+ ret.cycles = 6; break;
+ case eAbsoluteIndexedX:
+ ret.cycles = 7; break;
+ }
+ }
+
+ //case &fSTA:
+ else if (current_instruction == &fSTA){
+ switch(addr){
+ case eZeroPage:
+ ret.cycles = 3; break;
+ case eZeroPageIndexedX: case eAbsolute:
+ ret.cycles = 4; break;
+ case eAbsoluteIndexedX: case eAbsoluteIndexedY:
+ ret.cycles = 5; break;
+ case eIndexedIndirect: case eIndirectIndexed:
+ ret.cycles = 6; break;
+ }
+ }
+
+
+ //case &fBRK:
+ else if (current_instruction == &fBRK){
+ ret.cycles = 7;
+ }
+
+
+ //case &fRTI: case &fRTS: case &fJSR:
+ else if (current_instruction == &fRTI || current_instruction == &fRTS || current_instruction == &fJSR){
+ ret.cycles = 6;
+ }
+
+ //case &fJMP:
+ else if (current_instruction == &fJMP){
+ ret.cycles = 5;
+ }
+
+ //case &fPLA: case &fPLP:
+ else if (current_instruction == &fPLA || current_instruction == &fPLP){
+ ret.cycles = 4;
+ }
+
+ //case &fPHA: case &fPHP:
+ else if (current_instruction == &fPHA || current_instruction == &fPHP){
+ ret.cycles = 3;
+ }
+
+ else {
+ ret.cycles = 2;
+ }
+
+
+ // Page Boundary
+
+ //case &fADC: case &fSBC: case &fLDA: case &fLDX: case &fLDY: case &fEOR: case &fAND: case &fORA: case &fCMP:
+ if ( current_instruction == &fADC || current_instruction == &fSBC || current_instruction == &fLDA || current_instruction == &fLDX || current_instruction == &fLDY
+ || current_instruction == &fEOR || current_instruction == &fAND || current_instruction == &fORA || current_instruction == &fCMP ){
+ switch(addr){
+ case eAbsoluteIndexedX:
+ if ((x & 0xFF00) != ((x + X) & 0xFF00)) ret.cycles++; break;
+ case eAbsoluteIndexedY:
+ if ((x & 0xFF00) != ((x + Y) & 0xFF00)) ret.cycles++; break;
+ case eIndirectIndexed:
+ if ((ret.add & 0xFF00) != (ret.add - Y & 0xFF00)) ret.cycles++; break;
+ }
+ }
+
+ return ret;
+} \ No newline at end of file
diff --git a/src/cpu/addressing.h b/src/cpu/addressing.h
index 29fa87c..d0320b8 100644
--- a/src/cpu/addressing.h
+++ b/src/cpu/addressing.h
@@ -1,6 +1,11 @@
// addressing.h
// Contains definitions relevant to addressing, as well as fAddress() which returns time, length, value, and address for an instruction function call.
+#ifndef ADDRESSING_H
+#define ADDRESSING_H
+
+#include"core.h"
+
enum Addressing {
eImmediate,
eAccumulator,
@@ -26,190 +31,15 @@ typedef struct AddData{
byte value;
} AddData;
-//typedef struct AddData AddData;
-
-#include"instructions/init.h"
+#include"instructions.h"
//Holds address of current instruction.
void* current_instruction;
#define getInstructionLength(c) fAddressGetLength(*getInstructionTableAddressing(c))
-int fAddressGetLength(Addressing addr){
- switch(addr){
- case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
- return 3;
- case eAccumulator: case eImplied:
- return 1;
- default:
- return 2;
- }
-}
-
-AddData fAddress(Addressing addr, short x) {
- AddData ret;
-
- // ADDRESS
-
- switch(addr){
- case eImplied:
- case eIndirectAbsolute:
- case eRelative:
- case eImmediate:
- case eAccumulator:
- ret.add = 0x0000;
- break;
-
- case eAbsolute:
- ret.add = x;
- break;
- case eAbsoluteIndexedX:
- ret.add = (x + X);
- break;
- case eAbsoluteIndexedY:
- ret.add = (x + Y);
- break;
-
- case eZeroPage:
- ret.add = (x & 0x00FF);
- break;
- case eZeroPageIndexedX:
- ret.add = ((x + X) & 0x00FF);
- break;
- case eZeroPageIndexedY:
- ret.add = ((x + Y) & 0x00FF);
- break;
-
- case eIndexedIndirect:
- ret.add = ((getMemory(x+X+1))<<8) + (getMemory(x+X));
- break;
- case eIndirectIndexed:
- ret.add = ((getMemory(x+1))<<8) + (getMemory(x)) + Y;
- break;
- }
-
- // VALUE
-
- switch(addr){
- case eImplied:
- case eIndirectAbsolute:
- case eRelative:
- break;
-
- case eImmediate:
- ret.value = x;
- break;
-
- case eAccumulator:
- ret.value = acc;
- break;
-
- default:
- ret.value = getMemory(ret.add);
- }
-
- // LENGTH
-
- ret.length = fAddressGetLength(addr);
-
- // CYCLES
-
- //case &fADC: case &fAND: case &fBIT: case &fCMP: case &fCPX: case &fCPY: case &fEOR: case &fLDA:
- //case &fLDX: case &fLDY: case &fORA: case &fSBC: case &fSTX: case &fSTY:
-
- if ( current_instruction == &fADC || current_instruction == &fAND || current_instruction == &fBIT || current_instruction == &fCMP || current_instruction == &fCPX
- || current_instruction == &fCPY || current_instruction == &fEOR || current_instruction == &fLDA || current_instruction == &fLDX || current_instruction == &fLDY
- || current_instruction == &fORA || current_instruction == &fSBC || current_instruction == &fSTX || current_instruction == &fSTY ){
- switch(addr){
- case eImmediate:
- ret.cycles = 2; break;
- case eZeroPage:
- ret.cycles = 3; break;
- case eZeroPageIndexedX: case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
- ret.cycles = 4; break;
- case eIndexedIndirect:
- ret.cycles = 6; break;
- case eIndirectIndexed:
- ret.cycles = 5; break;
- }
- }
-
- //case &fASL: case &fDEC: case &fINC: case &fLSR: case &fROL: case &fROR:
- else if( current_instruction == &fASL || current_instruction == &fDEC || current_instruction == &fINC
- || current_instruction == &fLSR || current_instruction == &fROL || current_instruction == &fROR ){
- switch(addr){
- case eAccumulator:
- ret.cycles = 2; break;
- case eZeroPage:
- ret.cycles = 5; break;
- case eZeroPageIndexedX: case eAbsolute:
- ret.cycles = 6; break;
- case eAbsoluteIndexedX:
- ret.cycles = 7; break;
- }
- }
-
- //case &fSTA:
- else if (current_instruction == &fSTA){
- switch(addr){
- case eZeroPage:
- ret.cycles = 3; break;
- case eZeroPageIndexedX: case eAbsolute:
- ret.cycles = 4; break;
- case eAbsoluteIndexedX: case eAbsoluteIndexedY:
- ret.cycles = 5; break;
- case eIndexedIndirect: case eIndirectIndexed:
- ret.cycles = 6; break;
- }
- }
-
-
- //case &fBRK:
- else if (current_instruction == &fBRK){
- ret.cycles = 7;
- }
-
-
- //case &fRTI: case &fRTS: case &fJSR:
- else if (current_instruction == &fRTI || current_instruction == &fRTS || current_instruction == &fJSR){
- ret.cycles = 6;
- }
-
- //case &fJMP:
- else if (current_instruction == &fJMP){
- ret.cycles = 5;
- }
-
- //case &fPLA: case &fPLP:
- else if (current_instruction == &fPLA || current_instruction == &fPLP){
- ret.cycles = 4;
- }
-
- //case &fPHA: case &fPHP:
- else if (current_instruction == &fPHA || current_instruction == &fPHP){
- ret.cycles = 3;
- }
-
- else {
- ret.cycles = 2;
- }
-
-
- // Page Boundary
-
- //case &fADC: case &fSBC: case &fLDA: case &fLDX: case &fLDY: case &fEOR: case &fAND: case &fORA: case &fCMP:
- if ( current_instruction == &fADC || current_instruction == &fSBC || current_instruction == &fLDA || current_instruction == &fLDX || current_instruction == &fLDY
- || current_instruction == &fEOR || current_instruction == &fAND || current_instruction == &fORA || current_instruction == &fCMP ){
- switch(addr){
- case eAbsoluteIndexedX:
- if ((x & 0xFF00) != ((x + X) & 0xFF00)) ret.cycles++; break;
- case eAbsoluteIndexedY:
- if ((x & 0xFF00) != ((x + Y) & 0xFF00)) ret.cycles++; break;
- case eIndirectIndexed:
- if ((ret.add & 0xFF00) != (ret.add - Y & 0xFF00)) ret.cycles++; break;
- }
- }
+int fAddressGetLength(Addressing addr);
- return ret;
-}
+AddData fAddress(Addressing addr, short x);
+#endif \ No newline at end of file
diff --git a/src/cpu/core.h b/src/cpu/core.h
new file mode 100644
index 0000000..4c338ab
--- /dev/null
+++ b/src/cpu/core.h
@@ -0,0 +1,9 @@
+#ifndef CPU_CORE_H
+#define CPU_CORE_H
+
+typedef unsigned char
+ byte;
+typedef unsigned short
+ address;
+
+#endif \ No newline at end of file
diff --git a/src/cpu/instructions/definitions.h b/src/cpu/instructions.c
index 98345c5..e61d082 100644
--- a/src/cpu/instructions/definitions.h
+++ b/src/cpu/instructions.c
@@ -1,9 +1,7 @@
-// instructions.h
+// instructions.c
// Definition of all instruction functions, handling effect of instruction and flags.
-AddData idata;
-#define GET_STACK getMemory(0x01FF - S)
-#define SET_STACK(z) setMemory(0x01FF - S, z)
+#include"instructions.h"
/* TO DO
@@ -15,33 +13,40 @@ Fix all functions before performing testing
// Load and Store Instructions
-void fLDA(Addressing addr, address val){ idata = fAddress(addr, val);
+void fLDA(Addressing addr, address val){
+ idata = fAddress(addr, val);
acc = idata.value;
}
-void fLDX(Addressing addr, address val){ idata = fAddress(addr, val);
+void fLDX(Addressing addr, address val){
+ idata = fAddress(addr, val);
X = idata.value;
}
-void fLDY(Addressing addr, address val){ idata = fAddress(addr, val);
+void fLDY(Addressing addr, address val){
+ idata = fAddress(addr, val);
Y = idata.value;
}
-void fSTA(Addressing addr, address val){ idata = fAddress(addr, val);
+void fSTA(Addressing addr, address val){
+ idata = fAddress(addr, val);
setMemory(idata.add, acc);
}
-void fSTX(Addressing addr, address val){ idata = fAddress(addr, val);
+void fSTX(Addressing addr, address val){
+ idata = fAddress(addr, val);
setMemory(idata.add, X);
}
-void fSTY(Addressing addr, address val){ idata = fAddress(addr, val);
+void fSTY(Addressing addr, address val){
+ idata = fAddress(addr, val);
setMemory(idata.add, Y);
}
// Arithmetic Instructions
-void fADC(Addressing addr, address val){ idata = fAddress(addr, val);
+void fADC(Addressing addr, address val){
+ idata = fAddress(addr, val);
int buffer = acc + idata.value;
setFlagV(buffer, acc);
@@ -55,7 +60,8 @@ void fADC(Addressing addr, address val){ idata = fAddress(addr, val);
setFlagZ(acc);
}
-void fSBC(Addressing addr, address val){ idata = fAddress(addr, val);
+void fSBC(Addressing addr, address val){
+ idata = fAddress(addr, val);
int buffer = acc - idata.value;
setFlagV(buffer, acc);
@@ -71,7 +77,8 @@ void fSBC(Addressing addr, address val){ idata = fAddress(addr, val);
//Increment and Decrement Instructions
-void fINC(Addressing addr, address val){ idata = fAddress(addr, val);
+void fINC(Addressing addr, address val){
+ idata = fAddress(addr, val);
byte a = getMemory(idata.add);
a++;
setMemory(idata.add, a);
@@ -79,19 +86,22 @@ void fINC(Addressing addr, address val){ idata = fAddress(addr, val);
setFlagZ(Memory[idata.add]);
}
-void fINX(Addressing addr, address val){ idata = fAddress(addr, val);
+void fINX(Addressing addr, address val){
+ idata = fAddress(addr, val);
X++;
setFlagN(X);
setFlagZ(X);
}
-void fINY(Addressing addr, address val){ idata = fAddress(addr, val);
+void fINY(Addressing addr, address val){
+ idata = fAddress(addr, val);
Y++;
setFlagN(Y);
setFlagZ(Y);
}
-void fDEC(Addressing addr, address val){ idata = fAddress(addr, val);
+void fDEC(Addressing addr, address val){
+ idata = fAddress(addr, val);
byte a = getMemory(idata.add);
a--;
setMemory(idata.add, a);
@@ -99,13 +109,15 @@ void fDEC(Addressing addr, address val){ idata = fAddress(addr, val);
setFlagZ(Memory[idata.add]);
}
-void fDEX(Addressing addr, address val){ idata = fAddress(addr, val);
+void fDEX(Addressing addr, address val){
+ idata = fAddress(addr, val);
X--;
setFlagN(X);
setFlagZ(X);
}
-void fDEY(Addressing addr, address val){ idata = fAddress(addr, val);
+void fDEY(Addressing addr, address val){
+ idata = fAddress(addr, val);
Y--;
setFlagN(Y);
setFlagZ(Y);
@@ -113,19 +125,22 @@ void fDEY(Addressing addr, address val){ idata = fAddress(addr, val);
// Logical Instructions
-void fAND(Addressing addr, address val){ idata = fAddress(addr, val);
+void fAND(Addressing addr, address val){
+ idata = fAddress(addr, val);
acc &= idata.value;
setFlagN(acc);
setFlagZ(acc);
}
-void fORA(Addressing addr, address val){ idata = fAddress(addr, val);
+void fORA(Addressing addr, address val){
+ idata = fAddress(addr, val);
acc |= idata.value;
setFlagN(acc);
setFlagZ(acc);
}
-void fEOR(Addressing addr, address val){ idata = fAddress(addr, val);
+void fEOR(Addressing addr, address val){
+ idata = fAddress(addr, val);
acc ^= idata.value;
setFlagN(acc);
setFlagZ(acc);
@@ -133,75 +148,94 @@ void fEOR(Addressing addr, address val){ idata = fAddress(addr, val);
// Jump, Branch, Compare, and Test Bits
-void fJMP(Addressing addr, address val){ idata = fAddress(addr, val);
+void fJMP(Addressing addr, address val){
+ idata = fAddress(addr, val);
PC = val;
}
-void fBCC(Addressing addr, address val){ idata = fAddress(addr, val); //FINISH ALL BRANCH INSTRUCTIONS
+void fBCC(Addressing addr, address val){
+ idata = fAddress(addr, val); //FINISH ALL BRANCH INSTRUCTIONS
//signed char val down to BVC
if (getFlag(flag_C) == 0) PC += val;
}
-void fBCS(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBCS(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (getFlag(flag_C) == 1) PC += val;
}
-void fBEQ(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBEQ(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (getFlag(flag_Z) == 1) PC += val;
}
-void fBNE(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBNE(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (getFlag(flag_Z) == 0) PC += val;
}
-void fBMI(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBMI(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (getFlag(flag_N) == 1) PC += val;
}
-void fBPL(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBPL(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (getFlag(flag_N) == 0) PC += val;
}
-void fBVS(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBVS(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (getFlag(flag_V) == 1) PC += val;
}
-void fBVC(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBVC(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (getFlag(flag_V) == 0) PC += val;
}
-void fCMP(Addressing addr, address val){ idata = fAddress(addr, val);
+void fCMP(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (acc < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
- }if (acc == idata.value){
+ }
+ else if (acc == idata.value){
flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C);
- }if (acc > idata.value){
+ }
+ else if (acc > idata.value){
flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C);
}
}
-void fCPX(Addressing addr, address val){ idata = fAddress(addr, val);
+void fCPX(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (X < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
- }if (X == idata.value){
+ }
+ else if (X == idata.value){
flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C);
- }if (X > idata.value){
+ }
+ else if (X > idata.value){
flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C);
}
}
-void fCPY(Addressing addr, address val){ idata = fAddress(addr, val);
+void fCPY(Addressing addr, address val){
+ idata = fAddress(addr, val);
if (Y < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
- }if (Y == idata.value){
+ }
+ else if (Y == idata.value){
flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C);
- }if (Y > idata.value){
+ }
+ else if (Y > idata.value){
flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C);
}
}
//NEED TO DOUBLE CHECK THIS INSTRUCTION
-void fBIT(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBIT(Addressing addr, address val){
+ idata = fAddress(addr, val);
setFlag(flag_N, (idata.value & flag_N));
setFlag(flag_V, (idata.value & flag_V));
@@ -214,21 +248,24 @@ void fBIT(Addressing addr, address val){ idata = fAddress(addr, val);
// Shift and Rotate Instructions
-void fASL(Addressing addr, address val){ idata = fAddress(addr, val);
+void fASL(Addressing addr, address val){
+ idata = fAddress(addr, val);
setFlag(flag_C, (idata.value & 0x80));
acc = (idata.value << 1);
setFlagN(acc);
setFlagZ(acc);
}
-void fLSR(Addressing addr, address val){ idata = fAddress(addr, val);
+void fLSR(Addressing addr, address val){
+ idata = fAddress(addr, val);
setFlag(flag_C, (idata.value & 0x01));
acc = (idata.value >> 1);
setFlagN(acc);
setFlagZ(acc);
}
-void fROL(Addressing addr, address val){ idata = fAddress(addr, val);
+void fROL(Addressing addr, address val){
+ idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x80));
acc = (val << 1);
acc |= (getFlag(flag_C) * 0x01);
@@ -236,7 +273,8 @@ void fROL(Addressing addr, address val){ idata = fAddress(addr, val);
setFlagZ(acc);
}
-void fROR(Addressing addr, address val){ idata = fAddress(addr, val);
+void fROR(Addressing addr, address val){
+ idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x01));
acc = (val >> 1);
acc |= (getFlag(flag_C) * 0x80);
@@ -246,25 +284,29 @@ void fROR(Addressing addr, address val){ idata = fAddress(addr, val);
// Transfer Instructions
-void fTAX(Addressing addr, address val){ idata = fAddress(addr, val);
+void fTAX(Addressing addr, address val){
+ idata = fAddress(addr, val);
X = acc;
//setFlagN(X);
//setFlagZ(X);
}
-void fTAY(Addressing addr, address val){ idata = fAddress(addr, val);
+void fTAY(Addressing addr, address val){
+ idata = fAddress(addr, val);
Y = acc;
//setFlagN(Y);
//setFlagZ(Y);
}
-void fTXA(Addressing addr, address val){ idata = fAddress(addr, val);
+void fTXA(Addressing addr, address val){
+ idata = fAddress(addr, val);
acc = X;
setFlagN(acc);
setFlagZ(acc);
}
-void fTYA(Addressing addr, address val){ idata = fAddress(addr, val);
+void fTYA(Addressing addr, address val){
+ idata = fAddress(addr, val);
acc = Y;
setFlagN(acc);
setFlagZ(acc);
@@ -272,30 +314,36 @@ void fTYA(Addressing addr, address val){ idata = fAddress(addr, val);
// Stack Instructions
-void fTSX(Addressing addr, address val){ idata = fAddress(addr, val);
+void fTSX(Addressing addr, address val){
+ idata = fAddress(addr, val);
X = S;
}
-void fTXS(Addressing addr, address val){ idata = fAddress(addr, val);
+void fTXS(Addressing addr, address val){
+ idata = fAddress(addr, val);
S = X;
}
-void fPHA(Addressing addr, address val){ idata = fAddress(addr, val);
+void fPHA(Addressing addr, address val){
+ idata = fAddress(addr, val);
SET_STACK(acc);
S++;
}
-void fPHP(Addressing addr, address val){ idata = fAddress(addr, val);
+void fPHP(Addressing addr, address val){
+ idata = fAddress(addr, val);
SET_STACK(P);
S++;
}
-void fPLA(Addressing addr, address val){ idata = fAddress(addr, val);
+void fPLA(Addressing addr, address val){
+ idata = fAddress(addr, val);
S--;
acc = GET_STACK;
}
-void fPLP(Addressing addr, address val){ idata = fAddress(addr, val);
+void fPLP(Addressing addr, address val){
+ idata = fAddress(addr, val);
S--;
P = GET_STACK;
}
@@ -303,7 +351,8 @@ void fPLP(Addressing addr, address val){ idata = fAddress(addr, val);
// Subroutine Instructions
// NEED TO FINISH THESE
-void fJSR(Addressing addr, address val){ idata = fAddress(addr, val);
+void fJSR(Addressing addr, address val){
+ idata = fAddress(addr, val);
SET_STACK(((PC-1) & 0xFF00) >> 8);
S++;
SET_STACK((PC-1) & 0x00FF);
@@ -311,14 +360,16 @@ void fJSR(Addressing addr, address val){ idata = fAddress(addr, val);
PC = idata.add;
}
-void fRTS(Addressing addr, address val){ idata = fAddress(addr, val);
+void fRTS(Addressing addr, address val){
+ idata = fAddress(addr, val);
S--;
PC = (address)(GET_STACK + 1);
S--;
PC += ((address)(GET_STACK)) << 8;
}
-void fRTI(Addressing addr, address val){ idata = fAddress(addr, val);
+void fRTI(Addressing addr, address val){
+ idata = fAddress(addr, val);
S--;
P = GET_STACK; //NEED TO FIX
S--;
@@ -329,40 +380,49 @@ void fRTI(Addressing addr, address val){ idata = fAddress(addr, val);
// Set/Reset Insutrctions
-void fCLC(Addressing addr, address val){ idata = fAddress(addr, val);
+void fCLC(Addressing addr, address val){
+ idata = fAddress(addr, val);
flagClear(flag_C);
}
-void fCLD(Addressing addr, address val){ idata = fAddress(addr, val);
+void fCLD(Addressing addr, address val){
+ idata = fAddress(addr, val);
flagClear(flag_D);
}
-void fCLI(Addressing addr, address val){ idata = fAddress(addr, val);
+void fCLI(Addressing addr, address val){
+ idata = fAddress(addr, val);
flagClear(flag_I);
}
-void fCLV(Addressing addr, address val){ idata = fAddress(addr, val);
+void fCLV(Addressing addr, address val){
+ idata = fAddress(addr, val);
flagClear(flag_V);
}
-void fSEC(Addressing addr, address val){ idata = fAddress(addr, val);
+void fSEC(Addressing addr, address val){
+ idata = fAddress(addr, val);
flagSet(flag_C);
}
-void fSED(Addressing addr, address val){ idata = fAddress(addr, val);
+void fSED(Addressing addr, address val){
+ idata = fAddress(addr, val);
flagSet(flag_D);
}
-void fSEI(Addressing addr, address val){ idata = fAddress(addr, val);
+void fSEI(Addressing addr, address val){
+ idata = fAddress(addr, val);
flagSet(flag_I);
}
// NOP/BRK Instructions
-void fNOP(Addressing addr, address val){ idata = fAddress(addr, val);
+void fNOP(Addressing addr, address val){
+ idata = fAddress(addr, val);
}
-void fBRK(Addressing addr, address val){ idata = fAddress(addr, val);
+void fBRK(Addressing addr, address val){
+ idata = fAddress(addr, val);
SET_STACK((((PC+2) & 0xFF00) >> 8));
S++;
SET_STACK((PC+2) & 0x00FF);
@@ -373,6 +433,7 @@ void fBRK(Addressing addr, address val){ idata = fAddress(addr, val);
PC += ((address)(getMemory(0xFFFF)) << 8);
}
-#ifdef ILLEGAL
-#include"illegal/definitions.h"
+#ifdef ILLEGAL_INSTRUCTIONS
+
+
#endif \ No newline at end of file
diff --git a/src/cpu/instructions/init.h b/src/cpu/instructions.h
index dd2d477..082082f 100644
--- a/src/cpu/instructions/init.h
+++ b/src/cpu/instructions.h
@@ -1,5 +1,8 @@
-// instruction-init.h
-// Initializes every instruction function prior to addressing.h
+// instructions.h
+#ifndef INSTRUCTIONS_H
+#define INSTRUCTIONS_H
+
+AddData idata;
// Load and Store Instructions
void fLDA(Addressing, address);
@@ -8,9 +11,11 @@ void fLDY(Addressing, address);
void fSTA(Addressing, address);
void fSTX(Addressing, address);
void fSTY(Addressing, address);
+
// Arithmetic Instructions
void fADC(Addressing, address);
void fSBC(Addressing, address);
+
//Increment and Decrement Instructions
void fINC(Addressing, address);
void fINX(Addressing, address);
@@ -18,10 +23,12 @@ void fINY(Addressing, address);
void fDEC(Addressing, address);
void fDEX(Addressing, address);
void fDEY(Addressing, address);
+
// Logical Instructions
void fAND(Addressing, address);
void fORA(Addressing, address);
void fEOR(Addressing, address);
+
// Jump, Branch, Compare, and Test Bits
void fJMP(Addressing, address);
void fBCC(Addressing, address);
@@ -36,16 +43,19 @@ void fCMP(Addressing, address);
void fCPX(Addressing, address);
void fCPY(Addressing, address);
void fBIT(Addressing, address);
+
// Shift and Rotate Instructions
void fASL(Addressing, address);
void fLSR(Addressing, address);
void fROL(Addressing, address);
void fROR(Addressing, address);
+
// Transfer Instructions
void fTAX(Addressing, address);
void fTAY(Addressing, address);
void fTXA(Addressing, address);
void fTYA(Addressing, address);
+
// Stack Instructions
void fTSX(Addressing, address);
void fTXS(Addressing, address);
@@ -53,10 +63,12 @@ void fPHA(Addressing, address);
void fPHP(Addressing, address);
void fPLA(Addressing, address);
void fPLP(Addressing, address);
+
// Subroutine Instructions
void fJSR(Addressing, address);
void fRTS(Addressing, address);
void fRTI(Addressing, address);
+
// Set/Reset Insutrctions
void fCLC(Addressing, address);
void fCLD(Addressing, address);
@@ -65,10 +77,14 @@ void fCLV(Addressing, address);
void fSEC(Addressing, address);
void fSED(Addressing, address);
void fSEI(Addressing, address);
+
// NOP/BRK Instructions
void fNOP(Addressing, address);
void fBRK(Addressing, address);
-#ifdef ILLEGAL
-#include"illegal/init.h"
+#endif
+
+#ifdef ILLEGAL_INSTRUCTIONS
+
+
#endif \ No newline at end of file
diff --git a/src/cpu/instructions/illegal/definitions.h b/src/cpu/instructions/illegal/definitions.h
deleted file mode 100644
index e69de29..0000000
--- a/src/cpu/instructions/illegal/definitions.h
+++ /dev/null
diff --git a/src/cpu/instructions/illegal/init.h b/src/cpu/instructions/illegal/init.h
deleted file mode 100644
index e69de29..0000000
--- a/src/cpu/instructions/illegal/init.h
+++ /dev/null
diff --git a/src/cpu/instructions/illegal/table-append.h b/src/cpu/instructions/illegal/table-append.h
deleted file mode 100644
index e69de29..0000000
--- a/src/cpu/instructions/illegal/table-append.h
+++ /dev/null
diff --git a/src/cpu/instructions/table.h b/src/cpu/table.c
index f37814b..0f70889 100644
--- a/src/cpu/instructions/table.h
+++ b/src/cpu/table.c
@@ -1,10 +1,6 @@
-// instruction-table.h
-// Defines the instruction table, and the functions to access it.
+// table.c
-void* InstructionTable;
-void (*func)(Addressing, address);
-
-#define InstructionTableSize (256 * (sizeof(uintptr_t) + sizeof(Addressing)))
+#include"table.h"
uintptr_t* getInstructionTableFunction(int i){ //Segmentation fault is occurring here, likely in next one too
uintptr_t* r = (InstructionTable + (sizeof(uintptr_t)*i));
@@ -24,10 +20,10 @@ void callInstructionTable(int i, address val){
}
void setInstructionTable(int i, uintptr_t p, Addressing r){
- uintptr_t* p1 = (InstructionTable + (sizeof(uintptr_t)*i));
+ uintptr_t* p1 = ( InstructionTable + (sizeof(uintptr_t) * i) );
*p1 = p;
- Addressing* r1 = (InstructionTable + (sizeof(uintptr_t)*256) + (sizeof(Addressing)*i));
+ Addressing* r1 = ( InstructionTable + (sizeof(uintptr_t) * 256) + (sizeof(Addressing) * i) );
*r1 = r;
}
@@ -261,10 +257,5 @@ void initInstructionTable(){
setInstructionTable(0xEA, (uintptr_t)&fNOP, eImplied);
//BRK(Addressing, address);
setInstructionTable(0x00, (uintptr_t)&fBRK, eImplied);
-
- #ifdef ILLEGAL
- #include"illegal/table-append.h"
- #endif
-
}
diff --git a/src/cpu/table.h b/src/cpu/table.h
new file mode 100644
index 0000000..ded8d63
--- /dev/null
+++ b/src/cpu/table.h
@@ -0,0 +1,30 @@
+// table.h
+// Defines the instruction table, and the functions to access it.
+#ifndef TABLE_H
+#define TABLE_H
+
+#include"cstdint"
+#include"string"
+#include"addressing.h"
+
+void* InstructionTable;
+
+void (*func)(Addressing, address);
+
+#define InstructionTableSize (256 * (sizeof(uintptr_t) + sizeof(Addressing)))
+
+uintptr_t* getInstructionTableFunction(int i);
+
+Addressing* getInstructionTableAddressing(int i);
+
+void callInstructionTable(int i, address val);
+
+void setInstructionTable(int i, uintptr_t p, Addressing r);
+// Sets an individual portion of an instruction set
+
+void initInstructionTable();
+// Initializes entirety of the instruction table in memory.
+
+
+
+#endif \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index dd7f306..b3c6dbb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,9 +6,9 @@
#define CHR_WIDTH 5
#define CHR_HEIGHT 8
-#define WIDTH_SPACE 1
+#define WIDTH_SPACE 1 * SCALE
-#define MIN_WIDTH (40 * CHR_WIDTH)
+#define MIN_WIDTH (40 * CHR_WIDTH) + 39*WIDTH_SPACE
#define MIN_HEIGHT (24 * CHR_HEIGHT)
int main(){
@@ -43,25 +43,12 @@ int main(){
SDL_SetRenderDrawColor (render, 0, 0, 0, 255);
SDL_RenderClear (render);
-
- for(int i = 0; i < 500; i++){
-
- SDL_RenderCopy (render, font_texture, &character, &draw_character);
- SDL_RenderPresent (render);
-
-
- character.x = (i % 0x10) * CHR_WIDTH;
- draw_character.x += draw_character.w;
- if(i % 2 == 1) draw_character.y += draw_character.h;
- if(draw_character.x > MIN_WIDTH*SCALE) draw_character.x -= MIN_WIDTH*SCALE;
- if(draw_character.y > MIN_HEIGHT*SCALE) draw_character.y -= MIN_HEIGHT*SCALE;
- SDL_Delay(40);
- }
-
+ SDL_RenderCopy (render, font_texture, &character, &draw_character);
+ SDL_RenderPresent (render);
// Just delay before it dies
- //SDL_Delay(5000);
+ SDL_Delay(1000);
SDL_Quit();
return 0;
diff --git a/src/signetics.h b/src/signetics.h
index 28498e0..8442f11 100644
--- a/src/signetics.h
+++ b/src/signetics.h
@@ -2,13 +2,20 @@
// The Apple I came with its own terminal on-board, with a Signetics 2513 character ROM.
-/*byte CharacterROM[0x40] = {
+byte CharacterROM[0x40] = {
'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?'
-};*/
-//In case it comes in handy, which it probably wont.
+};
+
+byte* TerminalShiftRegister;
+int TerminalShiftRegisterOffset = 0;
+
+void InitVideo(){
+ TerminalShiftRegister = malloc(1024); //maybe 960
+}
+
byte ToAppleASCII(char x){
if (x < 32 || x > 95)