summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralekseiplusplus <alekseijeaves@protonmail.com>2023-08-05 05:32:54 +1000
committeralekseiplusplus <alekseijeaves@protonmail.com>2023-08-05 05:32:54 +1000
commita11980fe90fe62588fdf8103d9ef418283b3fd27 (patch)
tree954d2e327ff5190c8cdcdd977d9a73438ed62ca3 /src
parentb8599585fad704b2ec3bdde69dca4dd9c2a6f8fd (diff)
major refactor; doesn't compile (see ToDo)
Diffstat (limited to 'src')
-rw-r--r--src/apple.c23
-rw-r--r--src/apple.h33
-rw-r--r--src/cpu/6502.c1
-rw-r--r--src/cpu/6502.h5
-rw-r--r--src/cpu/addressing.c3
-rw-r--r--src/cpu/addressing.h27
-rw-r--r--src/cpu/core.h25
-rw-r--r--src/cpu/instructions.c86
-rw-r--r--src/cpu/instructions.h4
-rw-r--r--src/cpu/table.h7
-rw-r--r--src/debug.h7
-rw-r--r--src/include.h11
-rw-r--r--src/interpreter.c2
-rw-r--r--src/main.c3
14 files changed, 99 insertions, 138 deletions
diff --git a/src/apple.c b/src/apple.c
new file mode 100644
index 0000000..9886d2c
--- /dev/null
+++ b/src/apple.c
@@ -0,0 +1,23 @@
+#include"apple.h"
+
+
+void AppleOn(){
+ Memory = calloc(MEMORY_SIZE, sizeof(byte));
+ initInstructionTable();
+}
+
+void AppleReset(){
+ acc = 0; X = 0; Y = 0; P = 0; S = 0;
+ idata.cycles = 0; idata.length = 0; idata.add = 0; idata.value = 0;
+ free(Memory);
+ Memory = calloc(MEMORY_SIZE, sizeof(byte));
+}
+
+
+byte getMemory(address x){
+ return Memory[x];
+}
+
+void setMemory(address x, byte y){
+ Memory[x] = y;
+} \ No newline at end of file
diff --git a/src/apple.h b/src/apple.h
index 036f48c..01452cf 100644
--- a/src/apple.h
+++ b/src/apple.h
@@ -1,3 +1,13 @@
+#ifndef APPLE
+#define APPLE
+
+
+#include"cpu/6502.h"
+#include"cpu/addressing.h"
+#include"cpu/core.h"
+#include"cpu/instructions.h"
+#include"cpu/table.h"
+
#define MEMORY_SIZE 4096
#define XAML 0x24
@@ -14,25 +24,12 @@
#define DSP 0xD012
#define DSP_CR 0xD013
+void AppleOn();
-void AppleOn(){
- Memory = calloc(MEMORY_SIZE, sizeof(byte));
- initInstructionTable();
-}
-
-void AppleReset(){
- acc = 0; X = 0; Y = 0; P = 0; S = 0;
- idata.cycles = 0; idata.length = 0; idata.add = 0; idata.value = 0;
- free(Memory);
- Memory = calloc(MEMORY_SIZE, sizeof(byte));
-}
-
+void AppleReset();
-byte getMemory(address x){
- return Memory[x];
-}
+byte getMemory(address x);
-void setMemory(address x, byte y){
- Memory[x] = y;
-}
+void setMemory(address x, byte y);
+#endif \ No newline at end of file
diff --git a/src/cpu/6502.c b/src/cpu/6502.c
index d78cd7e..5aaffef 100644
--- a/src/cpu/6502.c
+++ b/src/cpu/6502.c
@@ -3,7 +3,6 @@
#include"6502.h"
-
byte getFlag(byte flag) {
return ((P & flag) == flag) ? 1 : 0;
}
diff --git a/src/cpu/6502.h b/src/cpu/6502.h
index c455e57..093fc3c 100644
--- a/src/cpu/6502.h
+++ b/src/cpu/6502.h
@@ -1,9 +1,10 @@
// 6502.h
// Main elements of the 6502 CPU
-#ifndef CPU_6502_H
-#define CPU_6502_H
+#ifndef CPU_H
+#define CPU_H
+#include"stdio.h"
#include"core.h"
byte acc, X, Y, P, S = 0x00;
diff --git a/src/cpu/addressing.c b/src/cpu/addressing.c
index 6e7d950..9a1694d 100644
--- a/src/cpu/addressing.c
+++ b/src/cpu/addressing.c
@@ -1,10 +1,9 @@
// addressing.h
// Contains definitions relevant to addressing, as well as fAddress() which returns time, length, value, and address for an instruction function call.
+// Would like to refactor the code into something better, such as switch-case statements for the cycles calculation.
#include"addressing.h"
-#define getInstructionLength(c) fAddressGetLength(*getInstructionTableAddressing(c))
-
int fAddressGetLength(Addressing addr){
switch(addr){
case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
diff --git a/src/cpu/addressing.h b/src/cpu/addressing.h
index d0320b8..c520535 100644
--- a/src/cpu/addressing.h
+++ b/src/cpu/addressing.h
@@ -5,32 +5,7 @@
#define ADDRESSING_H
#include"core.h"
-
-enum Addressing {
- eImmediate,
- eAccumulator,
- eZeroPage,
- eZeroPageIndexedX,
- eZeroPageIndexedY,
- eAbsolute,
- eAbsoluteIndexedX,
- eAbsoluteIndexedY,
- eIndexedIndirect,
- eIndirectIndexed,
- eImplied,
- eIndirectAbsolute,
- eRelative
-};
-
-typedef int Addressing;
-
-typedef struct AddData{
- int cycles;
- int length;
- address add;
- byte value;
-} AddData;
-
+#include"6502.h"
#include"instructions.h"
//Holds address of current instruction.
diff --git a/src/cpu/core.h b/src/cpu/core.h
index 4c338ab..71a420b 100644
--- a/src/cpu/core.h
+++ b/src/cpu/core.h
@@ -6,4 +6,29 @@ typedef unsigned char
typedef unsigned short
address;
+enum Addressing {
+ eImmediate,
+ eAccumulator,
+ eZeroPage,
+ eZeroPageIndexedX,
+ eZeroPageIndexedY,
+ eAbsolute,
+ eAbsoluteIndexedX,
+ eAbsoluteIndexedY,
+ eIndexedIndirect,
+ eIndirectIndexed,
+ eImplied,
+ eIndirectAbsolute,
+ eRelative
+};
+
+typedef int Addressing;
+
+typedef struct AddData{
+ int cycles;
+ int length;
+ address add;
+ byte value;
+} AddData;
+
#endif \ No newline at end of file
diff --git a/src/cpu/instructions.c b/src/cpu/instructions.c
index e61d082..6e80602 100644
--- a/src/cpu/instructions.c
+++ b/src/cpu/instructions.c
@@ -14,39 +14,32 @@ Fix all functions before performing testing
// Load and Store Instructions
void fLDA(Addressing addr, address val){
- idata = fAddress(addr, val);
acc = idata.value;
}
void fLDX(Addressing addr, address val){
- idata = fAddress(addr, val);
X = idata.value;
}
void fLDY(Addressing addr, address val){
- idata = fAddress(addr, val);
Y = idata.value;
}
void fSTA(Addressing addr, address val){
- idata = fAddress(addr, val);
setMemory(idata.add, acc);
}
void fSTX(Addressing addr, address val){
- idata = fAddress(addr, val);
setMemory(idata.add, X);
}
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);
int buffer = acc + idata.value;
setFlagV(buffer, acc);
@@ -61,7 +54,6 @@ void fADC(Addressing addr, address val){
}
void fSBC(Addressing addr, address val){
- idata = fAddress(addr, val);
int buffer = acc - idata.value;
setFlagV(buffer, acc);
@@ -78,7 +70,6 @@ void fSBC(Addressing addr, address val){
//Increment and Decrement Instructions
void fINC(Addressing addr, address val){
- idata = fAddress(addr, val);
byte a = getMemory(idata.add);
a++;
setMemory(idata.add, a);
@@ -87,21 +78,18 @@ void fINC(Addressing addr, address 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);
Y++;
setFlagN(Y);
setFlagZ(Y);
}
void fDEC(Addressing addr, address val){
- idata = fAddress(addr, val);
byte a = getMemory(idata.add);
a--;
setMemory(idata.add, a);
@@ -110,14 +98,12 @@ void fDEC(Addressing addr, address 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);
Y--;
setFlagN(Y);
setFlagZ(Y);
@@ -126,21 +112,18 @@ void fDEY(Addressing addr, address val){
// Logical Instructions
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);
acc |= idata.value;
setFlagN(acc);
setFlagZ(acc);
}
void fEOR(Addressing addr, address val){
- idata = fAddress(addr, val);
acc ^= idata.value;
setFlagN(acc);
setFlagZ(acc);
@@ -149,53 +132,43 @@ void fEOR(Addressing addr, address val){
// Jump, Branch, Compare, and Test Bits
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){ //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);
if (getFlag(flag_C) == 1) PC += 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);
if (getFlag(flag_Z) == 0) PC += 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);
if (getFlag(flag_N) == 0) PC += 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);
if (getFlag(flag_V) == 0) PC += val;
}
void fCMP(Addressing addr, address val){
- idata = fAddress(addr, val);
if (acc < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
}
@@ -208,7 +181,6 @@ void fCMP(Addressing addr, address val){
}
void fCPX(Addressing addr, address val){
- idata = fAddress(addr, val);
if (X < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
}
@@ -221,7 +193,6 @@ void fCPX(Addressing addr, address val){
}
void fCPY(Addressing addr, address val){
- idata = fAddress(addr, val);
if (Y < idata.value){
flagSet(flag_N); flagClear(flag_Z); flagClear(flag_C);
}
@@ -235,7 +206,6 @@ void fCPY(Addressing addr, address val){
//NEED TO DOUBLE CHECK THIS INSTRUCTION
void fBIT(Addressing addr, address val){
- idata = fAddress(addr, val);
setFlag(flag_N, (idata.value & flag_N));
setFlag(flag_V, (idata.value & flag_V));
@@ -249,7 +219,6 @@ void fBIT(Addressing addr, address val){
// Shift and Rotate Instructions
void fASL(Addressing addr, address val){
- idata = fAddress(addr, val);
setFlag(flag_C, (idata.value & 0x80));
acc = (idata.value << 1);
setFlagN(acc);
@@ -257,7 +226,6 @@ void fASL(Addressing addr, address val){
}
void fLSR(Addressing addr, address val){
- idata = fAddress(addr, val);
setFlag(flag_C, (idata.value & 0x01));
acc = (idata.value >> 1);
setFlagN(acc);
@@ -265,7 +233,6 @@ void fLSR(Addressing addr, address val){
}
void fROL(Addressing addr, address val){
- idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x80));
acc = (val << 1);
acc |= (getFlag(flag_C) * 0x01);
@@ -274,7 +241,6 @@ void fROL(Addressing addr, address val){
}
void fROR(Addressing addr, address val){
- idata = fAddress(addr, val);
setFlag(flag_C, (val & 0x01));
acc = (val >> 1);
acc |= (getFlag(flag_C) * 0x80);
@@ -285,28 +251,24 @@ void fROR(Addressing addr, address val){
// Transfer Instructions
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);
Y = acc;
//setFlagN(Y);
//setFlagZ(Y);
}
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);
acc = Y;
setFlagN(acc);
setFlagZ(acc);
@@ -315,119 +277,101 @@ void fTYA(Addressing addr, address val){
// Stack Instructions
void fTSX(Addressing addr, address val){
- idata = fAddress(addr, val);
X = S;
}
void fTXS(Addressing addr, address val){
- idata = fAddress(addr, val);
S = X;
}
void fPHA(Addressing addr, address val){
- idata = fAddress(addr, val);
- SET_STACK(acc);
+ setStack(acc);
S++;
}
void fPHP(Addressing addr, address val){
- idata = fAddress(addr, val);
- SET_STACK(P);
+ setStack(P);
S++;
}
void fPLA(Addressing addr, address val){
- idata = fAddress(addr, val);
S--;
- acc = GET_STACK;
+ acc = getStack();
}
void fPLP(Addressing addr, address val){
- idata = fAddress(addr, val);
S--;
- P = GET_STACK;
+ P = getStack();
}
// Subroutine Instructions
// NEED TO FINISH THESE
void fJSR(Addressing addr, address val){
- idata = fAddress(addr, val);
- SET_STACK(((PC-1) & 0xFF00) >> 8);
+ setStack(((PC-1) & 0xFF00) >> 8);
S++;
- SET_STACK((PC-1) & 0x00FF);
+ setStack((PC-1) & 0x00FF);
S++;
PC = idata.add;
}
void fRTS(Addressing addr, address val){
- idata = fAddress(addr, val);
S--;
- PC = (address)(GET_STACK + 1);
+ PC = (address)(getStack() + 1);
S--;
- PC += ((address)(GET_STACK)) << 8;
+ PC += ((address)(getStack())) << 8;
}
void fRTI(Addressing addr, address val){
- idata = fAddress(addr, val);
S--;
- P = GET_STACK; //NEED TO FIX
+ P = getStack(); //NEED TO FIX
S--;
- PC = (address)(GET_STACK);
+ PC = (address)(getStack());
S--;
- PC += (address)(GET_STACK << 8);
+ PC += (address)(getStack() << 8);
}
// Set/Reset Insutrctions
void fCLC(Addressing addr, address val){
- idata = fAddress(addr, val);
flagClear(flag_C);
}
void fCLD(Addressing addr, address val){
- idata = fAddress(addr, val);
flagClear(flag_D);
}
void fCLI(Addressing addr, address val){
- idata = fAddress(addr, val);
flagClear(flag_I);
}
void fCLV(Addressing addr, address val){
- idata = fAddress(addr, val);
flagClear(flag_V);
}
void fSEC(Addressing addr, address val){
- idata = fAddress(addr, val);
flagSet(flag_C);
}
void fSED(Addressing addr, address val){
- idata = fAddress(addr, val);
flagSet(flag_D);
}
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 fBRK(Addressing addr, address val){
- idata = fAddress(addr, val);
- SET_STACK((((PC+2) & 0xFF00) >> 8));
+ setStack((((PC+2) & 0xFF00) >> 8));
S++;
- SET_STACK((PC+2) & 0x00FF);
+ setStack((PC+2) & 0x00FF);
S++;
- SET_STACK(P);
+ setStack(P);
S++;
PC = (address)(getMemory(0xFFFE));
PC += ((address)(getMemory(0xFFFF)) << 8);
diff --git a/src/cpu/instructions.h b/src/cpu/instructions.h
index 082082f..6e8c1bf 100644
--- a/src/cpu/instructions.h
+++ b/src/cpu/instructions.h
@@ -2,6 +2,10 @@
#ifndef INSTRUCTIONS_H
#define INSTRUCTIONS_H
+#include"core.h"
+#include"6502.h"
+//#include"addressing.h"
+
AddData idata;
// Load and Store Instructions
diff --git a/src/cpu/table.h b/src/cpu/table.h
index ded8d63..e54776d 100644
--- a/src/cpu/table.h
+++ b/src/cpu/table.h
@@ -3,8 +3,9 @@
#ifndef TABLE_H
#define TABLE_H
-#include"cstdint"
-#include"string"
+#include"stdint.h"
+#include"stdlib.h"
+#include"string.h"
#include"addressing.h"
void* InstructionTable;
@@ -25,6 +26,4 @@ void setInstructionTable(int i, uintptr_t p, Addressing r);
void initInstructionTable();
// Initializes entirety of the instruction table in memory.
-
-
#endif \ No newline at end of file
diff --git a/src/debug.h b/src/debug.h
index 850b2d0..b36d187 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -1,6 +1,13 @@
// debug.h
// Various functions useful for use during development.
+#include"stdio.h"
+#include"cpu/6502.h"
+#include"cpu/addressing.h"
+#include"cpu/core.h"
+#include"cpu/instructions.h"
+#include"cpu/table.h"
+
// Converts a single character to hexadecimal
int dCharToNum(char c){
// 0x0 - 0x9
diff --git a/src/include.h b/src/include.h
deleted file mode 100644
index 3cd1502..0000000
--- a/src/include.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#include"stdio.h"
-#include"stdint.h"
-#include"stdlib.h"
-#include"string.h"
-#include"cpu/6502.h"
-#include"apple.h"
-
-#ifdef GRAPHICAL
-#include<SDL2/SDL.h>
-#include"signetics.h"
-#endif \ No newline at end of file
diff --git a/src/interpreter.c b/src/interpreter.c
index a51ade1..5d41522 100644
--- a/src/interpreter.c
+++ b/src/interpreter.c
@@ -2,8 +2,6 @@
// Useful for carrying out tests of the CPU instructions.
// Refer to interpreter.md for the manual
-
-#include"include.h"
#include"debug.h"
//Write a custom getc function here which ignores spaces
diff --git a/src/main.c b/src/main.c
index d5d5ded..fe81be5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,7 +6,8 @@ int main() {
-
+ // This line retrieves data about instruction used
+ idata = fAddress(addr, val);
return 0;