From ee742e6020248f9695cc9ce5bbace5f42814383e Mon Sep 17 00:00:00 2001 From: alekseiplusplus Date: Fri, 21 Apr 2023 03:44:47 +1000 Subject: have to call it a night. --- .gitignore | 4 + ToDo | 2 + addressing.h | 222 ----------------------------- applesystem.h | 113 --------------- assembler.c | 2 - debug.h | 41 ------ headers/addressing.h | 213 ++++++++++++++++++++++++++++ headers/applesystem.h | 113 +++++++++++++++ headers/debug.h | 41 ++++++ headers/include.h | 7 + headers/instruction-init.h | 112 +++++++++++++++ headers/instructions.h | 347 +++++++++++++++++++++++++++++++++++++++++++++ include.h | 7 - instruction-bin.c | 323 ++++++++++++++++++++--------------------- instruction-init.h | 116 --------------- instruction.h | 347 --------------------------------------------- interpreter.c | 4 +- main.c | 6 - makefile | 3 +- test.c | 45 +++--- 20 files changed, 1027 insertions(+), 1041 deletions(-) create mode 100644 .gitignore delete mode 100644 addressing.h delete mode 100644 applesystem.h delete mode 100644 debug.h create mode 100644 headers/addressing.h create mode 100644 headers/applesystem.h create mode 100644 headers/debug.h create mode 100644 headers/include.h create mode 100644 headers/instruction-init.h create mode 100644 headers/instructions.h delete mode 100644 include.h delete mode 100644 instruction-init.h delete mode 100644 instruction.h delete mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9656d78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +main +instructions.bin +instruction-dump +.vscode/settings.json diff --git a/ToDo b/ToDo index b5c1cc3..74d2dfa 100644 --- a/ToDo +++ b/ToDo @@ -1,5 +1,7 @@ Before the next commit, this should all be completed. +First and foremost, make sure reading is possible. + Create the makefile. Fix directory structures and header files before it gets even messier. Delete unnecessary files. diff --git a/addressing.h b/addressing.h deleted file mode 100644 index d764e8f..0000000 --- a/addressing.h +++ /dev/null @@ -1,222 +0,0 @@ -// addressing.h -// Contains definitions relevant to addressing, as well as fAddress() which returns time, length, value, and address for an instruction function call. - -enum Addressing { - eImmediate, - eAccumulator, - eZeroPage, - eZeroPageIndexedX, - eZeroPageIndexedY, - eAbsolute, - eAbsoluteIndexedX, - eAbsoluteIndexedY, - eIndexedIndirect, - eIndirectIndexed, - eImplied, - eIndirectAbsolute, - eRelative -}; - -typedef int Addressing; - -struct AddData{ - int cycles; - int length; - address add; - byte value; -}; - -typedef struct AddData AddData; - - - -#include"instruction-init.h" - - -//Holds address of current instruction. -void* current_instruction; - - - -Addressing fAddressGetLength(Addressing addr){ - switch(addr){ - case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY: - return 3; - case eAccumulator: - return 1; - default: - return 2; - } -} - - -AddData fAddress(Addressing addr, short x) { - AddData ret; - - // might seperate the rest of these out? - - // 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 = (((address)Memory[x+X+1])<<8) + (Memory[x+X]); - break; - case eIndirectIndexed: - ret.add = (((address)Memory[x+1])<<8) + (Memory[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 = Memory[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 & 0xFFFC) != ((x + X) & 0xFFFC)) ret.cycles++; break; - case eAbsoluteIndexedY: - if ((x & 0xFFFC) != ((x + Y) & 0xFFFC)) ret.cycles++; break; - case eIndirectIndexed: - if ((ret.add & 0xFFFC) != (ret.add - Y & 0xFFFC)) ret.cycles++; break; - } - } - - return ret; -} - diff --git a/applesystem.h b/applesystem.h deleted file mode 100644 index 6b2f818..0000000 --- a/applesystem.h +++ /dev/null @@ -1,113 +0,0 @@ -// applesystem.h -// Core elements of the 6502 CPU, and flag manipulation. - -typedef unsigned char byte; -typedef unsigned short address; -byte acc, X, Y, P, S = 0x00; -address PC = 0x0000; -byte Memory[4096]; // TO DO. Add expansion capability to memory. - -// FLAGS -const byte flag_N = 0x80; // Negative -const byte flag_V = 0x40; // Overflow -const byte flag_B = 0x10; // BRK command -const byte flag_D = 0x08; // Decimal mode -const byte flag_I = 0x04; // IRQ disable -const byte flag_Z = 0x02; // Zero -const byte flag_C = 0x01; // Carry - -byte getFlag(byte flag) { - return ((P & flag) == flag) ? 1 : 0; -} - -void setFlag(byte flag, int x) { //OVERLOAD TO ACCEPT INT AS WELL - 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; -} - - -// BCD -// need to actually look into BCD on the 6502 -byte toBCD(byte x){ - if (x < 100){ - byte a = ((x / 10) << 4); - byte b = (x % 10); - return (a + b); - } - else{ - fprintf(stderr, "Number greater than 99 passed to toBCD()"); - } -} - -byte fromBCD(byte x){ - byte a = ((x >> 4) * 10); - byte b = (x & 0xF); - return (a + b); -} - - -// 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); -}*/ //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); -} - -//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); -}*/ - - - - - - - diff --git a/assembler.c b/assembler.c index b20d13b..1797d9b 100644 --- a/assembler.c +++ b/assembler.c @@ -2,8 +2,6 @@ // A minimalistic assembler for creating binary files. #include"stdio.h" -//syscall library - int main(int argc; char* argv[]){ int output = 0; diff --git a/debug.h b/debug.h deleted file mode 100644 index bb6ba40..0000000 --- a/debug.h +++ /dev/null @@ -1,41 +0,0 @@ -// debug.h -// Various functions useful for use during development. - -// Converts a single character to hexadecimal -int dCharToNum(char c){ - // 0x0 - 0x9 - if (c != 0x20 && (c >= 0x30 && c <= 0x39)){ - return (c - 0x30); - } - // 0xA - 0xF - else if (c != 0x20 && (c >= 0x41 && c <= 0x46)){ - return (c - 0x37); - // 0xa - 0xf - }else if (c != 0x20 && (c >= 0x61 && c <= 0x66)){ - return (c - 0x57); - // Invalid - }else{ - return -1; - } -} - -// Dump a particular page in memory. -void dPageDump(short m){ - m <<= 8; - for(int i = 0; i < 256; i+=16){ - printf("\t"); - for(int j = 0; j < 16; j+=1){ - printf("%2x ", Memory[(m+(i+j))]); - } - printf("\n"); - } -} - -// Dump CPU values -void dStatusDump(void){ - printf(" acc:\t%x\n X:\t%x\n Y:\t%x\nstack:\t%x\nflags:\t%x\n", acc, X, Y, S, P); -} - -void dIdataDump(void){ - printf("cycles:\t%d\nlength:\t%d\n add:\t%x\n value:\t%x\n", idata.cycles, idata.length, idata.add, idata.value); -} \ No newline at end of file diff --git a/headers/addressing.h b/headers/addressing.h new file mode 100644 index 0000000..d9d8e98 --- /dev/null +++ b/headers/addressing.h @@ -0,0 +1,213 @@ +// addressing.h +// Contains definitions relevant to addressing, as well as fAddress() which returns time, length, value, and address for an instruction function call. + +enum Addressing { + eImmediate, + eAccumulator, + eZeroPage, + eZeroPageIndexedX, + eZeroPageIndexedY, + eAbsolute, + eAbsoluteIndexedX, + eAbsoluteIndexedY, + eIndexedIndirect, + eIndirectIndexed, + eImplied, + eIndirectAbsolute, + eRelative +}; + +typedef int Addressing; + +struct AddData{ + int cycles; + int length; + address add; + byte value; +}; + +typedef struct AddData AddData; + +#include"instruction-init.h" + +//Holds address of current instruction. +void* current_instruction; + +Addressing fAddressGetLength(Addressing addr){ + switch(addr){ + case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY: + return 3; + case eAccumulator: + 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 = (((address)Memory[x+X+1])<<8) + (Memory[x+X]); + break; + case eIndirectIndexed: + ret.add = (((address)Memory[x+1])<<8) + (Memory[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 = Memory[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 & 0xFFFC) != ((x + X) & 0xFFFC)) ret.cycles++; break; + case eAbsoluteIndexedY: + if ((x & 0xFFFC) != ((x + Y) & 0xFFFC)) ret.cycles++; break; + case eIndirectIndexed: + if ((ret.add & 0xFFFC) != (ret.add - Y & 0xFFFC)) ret.cycles++; break; + } + } + + return ret; +} + diff --git a/headers/applesystem.h b/headers/applesystem.h new file mode 100644 index 0000000..6b2f818 --- /dev/null +++ b/headers/applesystem.h @@ -0,0 +1,113 @@ +// applesystem.h +// Core elements of the 6502 CPU, and flag manipulation. + +typedef unsigned char byte; +typedef unsigned short address; +byte acc, X, Y, P, S = 0x00; +address PC = 0x0000; +byte Memory[4096]; // TO DO. Add expansion capability to memory. + +// FLAGS +const byte flag_N = 0x80; // Negative +const byte flag_V = 0x40; // Overflow +const byte flag_B = 0x10; // BRK command +const byte flag_D = 0x08; // Decimal mode +const byte flag_I = 0x04; // IRQ disable +const byte flag_Z = 0x02; // Zero +const byte flag_C = 0x01; // Carry + +byte getFlag(byte flag) { + return ((P & flag) == flag) ? 1 : 0; +} + +void setFlag(byte flag, int x) { //OVERLOAD TO ACCEPT INT AS WELL + 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; +} + + +// BCD +// need to actually look into BCD on the 6502 +byte toBCD(byte x){ + if (x < 100){ + byte a = ((x / 10) << 4); + byte b = (x % 10); + return (a + b); + } + else{ + fprintf(stderr, "Number greater than 99 passed to toBCD()"); + } +} + +byte fromBCD(byte x){ + byte a = ((x >> 4) * 10); + byte b = (x & 0xF); + return (a + b); +} + + +// 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); +}*/ //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); +} + +//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); +}*/ + + + + + + + diff --git a/headers/debug.h b/headers/debug.h new file mode 100644 index 0000000..bb6ba40 --- /dev/null +++ b/headers/debug.h @@ -0,0 +1,41 @@ +// debug.h +// Various functions useful for use during development. + +// Converts a single character to hexadecimal +int dCharToNum(char c){ + // 0x0 - 0x9 + if (c != 0x20 && (c >= 0x30 && c <= 0x39)){ + return (c - 0x30); + } + // 0xA - 0xF + else if (c != 0x20 && (c >= 0x41 && c <= 0x46)){ + return (c - 0x37); + // 0xa - 0xf + }else if (c != 0x20 && (c >= 0x61 && c <= 0x66)){ + return (c - 0x57); + // Invalid + }else{ + return -1; + } +} + +// Dump a particular page in memory. +void dPageDump(short m){ + m <<= 8; + for(int i = 0; i < 256; i+=16){ + printf("\t"); + for(int j = 0; j < 16; j+=1){ + printf("%2x ", Memory[(m+(i+j))]); + } + printf("\n"); + } +} + +// Dump CPU values +void dStatusDump(void){ + printf(" acc:\t%x\n X:\t%x\n Y:\t%x\nstack:\t%x\nflags:\t%x\n", acc, X, Y, S, P); +} + +void dIdataDump(void){ + printf("cycles:\t%d\nlength:\t%d\n add:\t%x\n value:\t%x\n", idata.cycles, idata.length, idata.add, idata.value); +} \ No newline at end of file diff --git a/headers/include.h b/headers/include.h new file mode 100644 index 0000000..9a537c4 --- /dev/null +++ b/headers/include.h @@ -0,0 +1,7 @@ +#include"stdio.h" +#include"stdint.h" +#include"stdlib.h" +#include"string.h" +#include"applesystem.h" +#include"addressing.h" +#include"instructions.h" \ No newline at end of file diff --git a/headers/instruction-init.h b/headers/instruction-init.h new file mode 100644 index 0000000..488f73c --- /dev/null +++ b/headers/instruction-init.h @@ -0,0 +1,112 @@ +// instruction-init.h +// Initializes every instruction function prior to addressing.h so that function addresses are accessible +// also defines the array used to refer to functions + +//InstructionTable +void* InstructionTable; +void (*func)(Addressing, address); + +#define InstructionTableSize (256 * (sizeof(uintptr_t) + sizeof(Addressing))) + +/* +uintptr_t getInstructionTableFunction(int i){ //Segmentation fault is occurring here, likely in next one too + uintptr_t r = (InstructionTable + (sizeof(uintptr_t)*i)); + return r; +} + +Addressing getInstructionTableAddressing(int i){ + Addressing r = (InstructionTable + (sizeof(uintptr_t)*256) + (sizeof(Addressing)*i)); + return r; +} +*/ + +void callInstructionTable(int i, address val){ + printf("Table:\t%x\n", InstructionTable); + uintptr_t* a = (InstructionTable + (sizeof(uintptr_t) * i)); + printf("A Val:\t%x\n", a); + printf("Before:\t%x\n", func); func = *a; printf("After:\t%x\n", func); + //printf("Before:\t%x\n", func); memcpy(&func, a, sizeof(uintptr_t)); printf("After:\t%x\n", func); + Addressing* r = (InstructionTable + ((sizeof(uintptr_t)*256) + (sizeof(Addressing) * i))); + func(*r, val); printf("Statement OK"); +} + +void initInstructionTable(){ + InstructionTable = malloc(InstructionTableSize); + FILE* fs = fopen("instructions.bin", "r"); + + for(byte* c = InstructionTable; c < (InstructionTable + InstructionTableSize); c++){ //NEED TO MAKE (InstructionTable + (256*(sizeof(uintptr_t) + sizeof(Addressing)))) a macro + *c = (byte)fgetc(fs); + } //Investigate whether doing word sized units would increase speed + fclose(fs); + //read in the instruction data binary +} + + + +// Load and Store Instructions +void fLDA(Addressing, address); +void fLDX(Addressing, address); +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); +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); +void fBCS(Addressing, address); +void fBEQ(Addressing, address); +void fBNE(Addressing, address); +void fBMI(Addressing, address); +void fBPL(Addressing, address); +void fBVS(Addressing, address); +void fBVC(Addressing, address); +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); +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); +void fCLI(Addressing, address); +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); \ No newline at end of file diff --git a/headers/instructions.h b/headers/instructions.h new file mode 100644 index 0000000..96f5719 --- /dev/null +++ b/headers/instructions.h @@ -0,0 +1,347 @@ +// instruction.h +// Definition of all instruction functions, handling effect of instruction and flags. + +// array/map of pointers which all point +// to the functions which the index corresponds to. +// use that like a sort of map + + +//Instruction Data +AddData idata; + +// 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); + Memory[idata.add] = acc; +} + +void fSTX(Addressing addr, address val){ idata = fAddress(addr, val); + Memory[idata.add] = X; +} + +void fSTY(Addressing addr, address val){ idata = fAddress(addr, val); + Memory[idata.add] = Y; +} + +// Arithmetic Instructions + +void fADC(Addressing addr, address val){ idata = fAddress(addr, val); + int buffer = acc + idata.value; + setFlagV(buffer, acc); + + if (buffer > 255) + flagSet(flag_C); + else + flagClear(flag_C); + + acc += idata.value; + setFlagN(acc); + setFlagZ(acc); +} + +void fSBC(Addressing addr, address val){ idata = fAddress(addr, val); + int buffer = acc - idata.value; + setFlagV(buffer, acc); + + if (buffer < 0) + flagSet(flag_C); + else + flagClear(flag_C); + + acc -= idata.value; + setFlagN(acc); + setFlagZ(acc); +} + +//Increment and Decrement Instructions + +void fINC(Addressing addr, address val){ idata = fAddress(addr, val); + Memory[idata.add]++; + setFlagN(Memory[idata.add]); + setFlagZ(Memory[idata.add]); +} + +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); + Memory[idata.add]--; + setFlagN(Memory[idata.add]); + setFlagZ(Memory[idata.add]); +} + +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); +} + +// 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); +} + +// 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 + //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); + }if (acc == idata.value){ + flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C); + }if (acc > idata.value){ + flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C); + } +} + +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){ + flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C); + }if (X > idata.value){ + flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C); + } +} + +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){ + flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C); + }if (Y > idata.value){ + flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C); + } +} + +//Need to double check the function of this instruction +void fBIT(Addressing addr, address val){ idata = fAddress(addr, val); + setFlag(flag_N, (Memory[val] & flag_N)); + setFlag(flag_V, (Memory[val] & flag_V)); + if (((Memory[val] & flag_N) & (Memory[val] & flag_V)) == 0) { + flagSet(flag_Z); + } else { + flagSet(flag_Z); + } +} + +// Shift and Rotate Instructions + +void fASL(Addressing addr, address val){ idata = fAddress(addr, val); + setFlag(flag_C, (val & 0x80)); + acc = (val << 1); + setFlagN(acc); + setFlagZ(acc); +} + +void fLSR(Addressing addr, address val){ idata = fAddress(addr, val); + setFlag(flag_C, (val & 0x01)); + acc = (val >> 1); + setFlagN(acc); + setFlagZ(acc); +} + +void fROL(Addressing addr, address val){ idata = fAddress(addr, val); + setFlag(flag_C, (val & 0x80)); + acc = (val << 1); + acc |= (getFlag(flag_C) * 0x01); + setFlagN(acc); + setFlagZ(acc); +} + +void fROR(Addressing addr, address val){ idata = fAddress(addr, val); + setFlag(flag_C, (val & 0x01)); + acc = (val >> 1); + acc |= (getFlag(flag_C) * 0x80); + setFlagN(acc); + setFlagZ(acc); +} + +// 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); +} + +// 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); + Memory[0x01FF-S] = acc; + S++; +} + +void fPHP(Addressing addr, address val){ idata = fAddress(addr, val); + Memory[0x01FF-S] = P; + S++; +} + +void fPLA(Addressing addr, address val){ idata = fAddress(addr, val); + S--; + acc = Memory[0x01FF-S]; +} + +void fPLP(Addressing addr, address val){ idata = fAddress(addr, val); + S--; + P = Memory[0x01FF-S]; +} + +// Subroutine Instructions + +void fJSR(Addressing addr, address val){ idata = fAddress(addr, val); + Memory[0x01FF-S] = (idata.add-1); + S++; + PC = idata.add; +} + +void fRTS(Addressing addr, address val){ idata = fAddress(addr, val); + +} + +void fRTI(Addressing addr, address val){ idata = fAddress(addr, val); + +} + +// 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); + flagSet(flag_B); +} \ No newline at end of file diff --git a/include.h b/include.h deleted file mode 100644 index 4bffe1e..0000000 --- a/include.h +++ /dev/null @@ -1,7 +0,0 @@ -#include"stdio.h" -#include"stdint.h" -#include"stdlib.h" -#include"string.h" -#include"applesystem.h" -#include"addressing.h" -#include"instruction.h" \ No newline at end of file diff --git a/instruction-bin.c b/instruction-bin.c index d470c74..074a1e4 100644 --- a/instruction-bin.c +++ b/instruction-bin.c @@ -1,256 +1,257 @@ -#include"include.h" +#include"headers/include.h" -void setIT(int i, uintptr_t p, Addressing r); -void setAllIT(); +void setInstructionTable(int i, uintptr_t p, Addressing r); + +void setAllInstructionTable(); int main(){ - setAllIT(); + setAllInstructionTable(); // here, do calls to dump all instructions into a bin file FILE* fs = fopen("instructions.bin", "w"); - for(char* c = IT; c < (IT + InstructionTableSize); c++){ + for(char* c = InstructionTable; c < (InstructionTable + InstructionTableSize); c++){ fputc(*c, fs); } fclose(fs); } -void setIT(int i, uintptr_t p, Addressing r){ - uintptr_t* p1 = (IT + (sizeof(uintptr_t)*i)); +void setInstructionTable(int i, uintptr_t p, Addressing r){ + uintptr_t* p1 = (InstructionTable + (sizeof(uintptr_t)*i)); *p1 = p; - Addressing* r1 = (IT + (sizeof(uintptr_t)*256) + (sizeof(Addressing)*i)); + Addressing* r1 = (InstructionTable + (sizeof(uintptr_t)*256) + (sizeof(Addressing)*i)); *r1 = r; } -void setAllIT(){ - IT = malloc(256 * (sizeof(uintptr_t) + sizeof(Addressing))); +void setAllInstructionTable(){ + InstructionTable = malloc(256 * (sizeof(uintptr_t) + sizeof(Addressing))); // Load and Store Instructions // fLDA(Addressing, address); - setIT(0xA9, (uintptr_t)&fLDA, eImmediate); - setIT(0xA5, (uintptr_t)&fLDA, eZeroPage); - setIT(0xB5, (uintptr_t)&fLDA, eZeroPageIndexedX); - setIT(0xAD, (uintptr_t)&fLDA, eAbsolute); - setIT(0xBD, (uintptr_t)&fLDA, eAbsoluteIndexedX); - setIT(0xB9, (uintptr_t)&fLDA, eAbsoluteIndexedY); - setIT(0xA1, (uintptr_t)&fLDA, eIndexedIndirect); - setIT(0xB1, (uintptr_t)&fLDA, eIndirectIndexed); + setInstructionTable(0xA9, (uintptr_t)&fLDA, eImmediate); + setInstructionTable(0xA5, (uintptr_t)&fLDA, eZeroPage); + setInstructionTable(0xB5, (uintptr_t)&fLDA, eZeroPageIndexedX); + setInstructionTable(0xAD, (uintptr_t)&fLDA, eAbsolute); + setInstructionTable(0xBD, (uintptr_t)&fLDA, eAbsoluteIndexedX); + setInstructionTable(0xB9, (uintptr_t)&fLDA, eAbsoluteIndexedY); + setInstructionTable(0xA1, (uintptr_t)&fLDA, eIndexedIndirect); + setInstructionTable(0xB1, (uintptr_t)&fLDA, eIndirectIndexed); // fLDX(Addressing, address); - setIT(0xA2, (uintptr_t)&fLDX, eImmediate); - setIT(0xA6, (uintptr_t)&fLDX, eZeroPage); - setIT(0xB6, (uintptr_t)&fLDX, eZeroPageIndexedY); - setIT(0xAE, (uintptr_t)&fLDX, eAbsolute); - setIT(0xBE, (uintptr_t)&fLDX, eAbsoluteIndexedY); + setInstructionTable(0xA2, (uintptr_t)&fLDX, eImmediate); + setInstructionTable(0xA6, (uintptr_t)&fLDX, eZeroPage); + setInstructionTable(0xB6, (uintptr_t)&fLDX, eZeroPageIndexedY); + setInstructionTable(0xAE, (uintptr_t)&fLDX, eAbsolute); + setInstructionTable(0xBE, (uintptr_t)&fLDX, eAbsoluteIndexedY); // fLDY(Addressing, address); - setIT(0xA0, (uintptr_t)&fLDY, eImmediate); - setIT(0xA4, (uintptr_t)&fLDY, eZeroPage); - setIT(0xB4, (uintptr_t)&fLDY, eZeroPageIndexedX); - setIT(0xAC, (uintptr_t)&fLDY, eAbsolute); - setIT(0xBC, (uintptr_t)&fLDY, eAbsoluteIndexedX); + setInstructionTable(0xA0, (uintptr_t)&fLDY, eImmediate); + setInstructionTable(0xA4, (uintptr_t)&fLDY, eZeroPage); + setInstructionTable(0xB4, (uintptr_t)&fLDY, eZeroPageIndexedX); + setInstructionTable(0xAC, (uintptr_t)&fLDY, eAbsolute); + setInstructionTable(0xBC, (uintptr_t)&fLDY, eAbsoluteIndexedX); // fSTA(Addressing, address); - setIT(0x85, (uintptr_t)&fSTA, eZeroPage); - setIT(0x95, (uintptr_t)&fSTA, eZeroPageIndexedX); - setIT(0x8D, (uintptr_t)&fSTA, eAbsolute); - setIT(0x9D, (uintptr_t)&fSTA, eAbsoluteIndexedX); - setIT(0x99, (uintptr_t)&fSTA, eAbsoluteIndexedY); - setIT(0x81, (uintptr_t)&fSTA, eIndexedIndirect); - setIT(0x91, (uintptr_t)&fSTA, eIndirectIndexed); + setInstructionTable(0x85, (uintptr_t)&fSTA, eZeroPage); + setInstructionTable(0x95, (uintptr_t)&fSTA, eZeroPageIndexedX); + setInstructionTable(0x8D, (uintptr_t)&fSTA, eAbsolute); + setInstructionTable(0x9D, (uintptr_t)&fSTA, eAbsoluteIndexedX); + setInstructionTable(0x99, (uintptr_t)&fSTA, eAbsoluteIndexedY); + setInstructionTable(0x81, (uintptr_t)&fSTA, eIndexedIndirect); + setInstructionTable(0x91, (uintptr_t)&fSTA, eIndirectIndexed); // fSTX(Addressing, address); - setIT(0x86, (uintptr_t)&fSTX, eZeroPage); - setIT(0x96, (uintptr_t)&fSTX, eZeroPageIndexedX); - setIT(0x8E, (uintptr_t)&fSTX, eAbsolute); + setInstructionTable(0x86, (uintptr_t)&fSTX, eZeroPage); + setInstructionTable(0x96, (uintptr_t)&fSTX, eZeroPageIndexedX); + setInstructionTable(0x8E, (uintptr_t)&fSTX, eAbsolute); // fSTY(Addressing, address); - setIT(0x84, (uintptr_t)&fSTY, eZeroPage); - setIT(0x94, (uintptr_t)&fSTY, eZeroPageIndexedY); - setIT(0x8C, (uintptr_t)&fSTY, eAbsolute); + setInstructionTable(0x84, (uintptr_t)&fSTY, eZeroPage); + setInstructionTable(0x94, (uintptr_t)&fSTY, eZeroPageIndexedY); + setInstructionTable(0x8C, (uintptr_t)&fSTY, eAbsolute); // Arithmetic Instructions // fADC(Addressing, address); - setIT(0x69, (uintptr_t)&fADC, eImmediate); - setIT(0x65, (uintptr_t)&fADC, eZeroPage); - setIT(0x75, (uintptr_t)&fADC, eZeroPageIndexedX); - setIT(0x6D, (uintptr_t)&fADC, eAbsolute); - setIT(0x7D, (uintptr_t)&fADC, eAbsoluteIndexedX); - setIT(0x79, (uintptr_t)&fADC, eAbsoluteIndexedY); - setIT(0x61, (uintptr_t)&fADC, eIndexedIndirect); - setIT(0x71, (uintptr_t)&fADC, eIndirectIndexed); + setInstructionTable(0x69, (uintptr_t)&fADC, eImmediate); + setInstructionTable(0x65, (uintptr_t)&fADC, eZeroPage); + setInstructionTable(0x75, (uintptr_t)&fADC, eZeroPageIndexedX); + setInstructionTable(0x6D, (uintptr_t)&fADC, eAbsolute); + setInstructionTable(0x7D, (uintptr_t)&fADC, eAbsoluteIndexedX); + setInstructionTable(0x79, (uintptr_t)&fADC, eAbsoluteIndexedY); + setInstructionTable(0x61, (uintptr_t)&fADC, eIndexedIndirect); + setInstructionTable(0x71, (uintptr_t)&fADC, eIndirectIndexed); // fSBC(Addressing, address); - setIT(0xE9, (uintptr_t)&fSBC, eImmediate); - setIT(0xE5, (uintptr_t)&fSBC, eZeroPage); - setIT(0xF5, (uintptr_t)&fSBC, eZeroPageIndexedX); - setIT(0xED, (uintptr_t)&fSBC, eAbsolute); - setIT(0xFD, (uintptr_t)&fSBC, eAbsoluteIndexedX); - setIT(0xF9, (uintptr_t)&fSBC, eAbsoluteIndexedY); - setIT(0xE1, (uintptr_t)&fSBC, eIndexedIndirect); - setIT(0xF1, (uintptr_t)&fSBC, eIndirectIndexed); + setInstructionTable(0xE9, (uintptr_t)&fSBC, eImmediate); + setInstructionTable(0xE5, (uintptr_t)&fSBC, eZeroPage); + setInstructionTable(0xF5, (uintptr_t)&fSBC, eZeroPageIndexedX); + setInstructionTable(0xED, (uintptr_t)&fSBC, eAbsolute); + setInstructionTable(0xFD, (uintptr_t)&fSBC, eAbsoluteIndexedX); + setInstructionTable(0xF9, (uintptr_t)&fSBC, eAbsoluteIndexedY); + setInstructionTable(0xE1, (uintptr_t)&fSBC, eIndexedIndirect); + setInstructionTable(0xF1, (uintptr_t)&fSBC, eIndirectIndexed); //Increment and Decrement Instructions //INC(Addressing, address); - setIT(0xE6, (uintptr_t)&fINC, eZeroPage); - setIT(0xF6, (uintptr_t)&fINC, eZeroPageIndexedX); - setIT(0xEE, (uintptr_t)&fINC, eAbsolute); - setIT(0xFE, (uintptr_t)&fINC, eAbsoluteIndexedX); + setInstructionTable(0xE6, (uintptr_t)&fINC, eZeroPage); + setInstructionTable(0xF6, (uintptr_t)&fINC, eZeroPageIndexedX); + setInstructionTable(0xEE, (uintptr_t)&fINC, eAbsolute); + setInstructionTable(0xFE, (uintptr_t)&fINC, eAbsoluteIndexedX); //INX(Addressing, address); - setIT(0xE8, (uintptr_t)&fINX, eImplied); + setInstructionTable(0xE8, (uintptr_t)&fINX, eImplied); //INY(Addressing, address); - setIT(0xC8, (uintptr_t)&fINY, eImplied); + setInstructionTable(0xC8, (uintptr_t)&fINY, eImplied); //DEC(Addressing, address); - setIT(0xC6, (uintptr_t)&fDEC, eZeroPage); - setIT(0xD6, (uintptr_t)&fDEC, eZeroPageIndexedX); - setIT(0xCE, (uintptr_t)&fDEC, eAbsolute); - setIT(0xDE, (uintptr_t)&fDEC, eAbsoluteIndexedX); + setInstructionTable(0xC6, (uintptr_t)&fDEC, eZeroPage); + setInstructionTable(0xD6, (uintptr_t)&fDEC, eZeroPageIndexedX); + setInstructionTable(0xCE, (uintptr_t)&fDEC, eAbsolute); + setInstructionTable(0xDE, (uintptr_t)&fDEC, eAbsoluteIndexedX); //DEX(Addressing, address); - setIT(0xCA, (uintptr_t)&fDEX, eImplied); + setInstructionTable(0xCA, (uintptr_t)&fDEX, eImplied); //DEY(Addressing, address); - setIT(0x88, (uintptr_t)&fDEY, eImplied); + setInstructionTable(0x88, (uintptr_t)&fDEY, eImplied); // Logical Instructions //AND(Addressing, address); - setIT(0x29, (uintptr_t)&fAND, eImmediate); - setIT(0x25, (uintptr_t)&fAND, eZeroPage); - setIT(0x35, (uintptr_t)&fAND, eZeroPageIndexedX); - setIT(0x2D, (uintptr_t)&fAND, eAbsolute); - setIT(0x3D, (uintptr_t)&fAND, eAbsoluteIndexedX); - setIT(0x39, (uintptr_t)&fAND, eAbsoluteIndexedY); - setIT(0x21, (uintptr_t)&fAND, eIndexedIndirect); - setIT(0x31, (uintptr_t)&fAND, eIndirectIndexed); + setInstructionTable(0x29, (uintptr_t)&fAND, eImmediate); + setInstructionTable(0x25, (uintptr_t)&fAND, eZeroPage); + setInstructionTable(0x35, (uintptr_t)&fAND, eZeroPageIndexedX); + setInstructionTable(0x2D, (uintptr_t)&fAND, eAbsolute); + setInstructionTable(0x3D, (uintptr_t)&fAND, eAbsoluteIndexedX); + setInstructionTable(0x39, (uintptr_t)&fAND, eAbsoluteIndexedY); + setInstructionTable(0x21, (uintptr_t)&fAND, eIndexedIndirect); + setInstructionTable(0x31, (uintptr_t)&fAND, eIndirectIndexed); //ORA(Addressing, address); - setIT(0x09, (uintptr_t)&fORA, eImmediate); - setIT(0x05, (uintptr_t)&fORA, eZeroPage); - setIT(0x15, (uintptr_t)&fORA, eZeroPageIndexedX); - setIT(0x0D, (uintptr_t)&fORA, eAbsolute); - setIT(0x1D, (uintptr_t)&fORA, eAbsoluteIndexedX); - setIT(0x19, (uintptr_t)&fORA, eAbsoluteIndexedY); - setIT(0x01, (uintptr_t)&fORA, eIndexedIndirect); - setIT(0x11, (uintptr_t)&fORA, eIndirectIndexed); + setInstructionTable(0x09, (uintptr_t)&fORA, eImmediate); + setInstructionTable(0x05, (uintptr_t)&fORA, eZeroPage); + setInstructionTable(0x15, (uintptr_t)&fORA, eZeroPageIndexedX); + setInstructionTable(0x0D, (uintptr_t)&fORA, eAbsolute); + setInstructionTable(0x1D, (uintptr_t)&fORA, eAbsoluteIndexedX); + setInstructionTable(0x19, (uintptr_t)&fORA, eAbsoluteIndexedY); + setInstructionTable(0x01, (uintptr_t)&fORA, eIndexedIndirect); + setInstructionTable(0x11, (uintptr_t)&fORA, eIndirectIndexed); //EOR(Addressing, address); - setIT(0x49, (uintptr_t)&fEOR, eImmediate); - setIT(0x45, (uintptr_t)&fEOR, eZeroPage); - setIT(0x55, (uintptr_t)&fEOR, eZeroPageIndexedX); - setIT(0x4D, (uintptr_t)&fEOR, eAbsolute); - setIT(0x5D, (uintptr_t)&fEOR, eAbsoluteIndexedX); - setIT(0x59, (uintptr_t)&fEOR, eAbsoluteIndexedY); - setIT(0x41, (uintptr_t)&fEOR, eIndexedIndirect); - setIT(0x51, (uintptr_t)&fEOR, eIndirectIndexed); + setInstructionTable(0x49, (uintptr_t)&fEOR, eImmediate); + setInstructionTable(0x45, (uintptr_t)&fEOR, eZeroPage); + setInstructionTable(0x55, (uintptr_t)&fEOR, eZeroPageIndexedX); + setInstructionTable(0x4D, (uintptr_t)&fEOR, eAbsolute); + setInstructionTable(0x5D, (uintptr_t)&fEOR, eAbsoluteIndexedX); + setInstructionTable(0x59, (uintptr_t)&fEOR, eAbsoluteIndexedY); + setInstructionTable(0x41, (uintptr_t)&fEOR, eIndexedIndirect); + setInstructionTable(0x51, (uintptr_t)&fEOR, eIndirectIndexed); // Jump, Branch, Compare, and Test Bits //JMP(Addressing, address); - setIT(0x4C, (uintptr_t)&fJMP, eAbsolute); - setIT(0x6C, (uintptr_t)&fJMP, eIndirectAbsolute); + setInstructionTable(0x4C, (uintptr_t)&fJMP, eAbsolute); + setInstructionTable(0x6C, (uintptr_t)&fJMP, eIndirectAbsolute); //BCC(Addressing, address); - setIT(0x90, (uintptr_t)&fBCC, eRelative); + setInstructionTable(0x90, (uintptr_t)&fBCC, eRelative); //BCS(Addressing, address); - setIT(0xB0, (uintptr_t)&fBCS, eRelative); + setInstructionTable(0xB0, (uintptr_t)&fBCS, eRelative); //BEQ(Addressing, address); - setIT(0xF0, (uintptr_t)&fBEQ, eRelative); + setInstructionTable(0xF0, (uintptr_t)&fBEQ, eRelative); //BNE(Addressing, address); - setIT(0xD0, (uintptr_t)&fBNE, eRelative); + setInstructionTable(0xD0, (uintptr_t)&fBNE, eRelative); //BMI(Addressing, address); - setIT(0x30, (uintptr_t)&fBMI, eRelative); + setInstructionTable(0x30, (uintptr_t)&fBMI, eRelative); //BPL(Addressing, address); - setIT(0x10, (uintptr_t)&fBPL, eRelative); + setInstructionTable(0x10, (uintptr_t)&fBPL, eRelative); //BVS(Addressing, address); - setIT(0x70, (uintptr_t)&fBVS, eRelative); + setInstructionTable(0x70, (uintptr_t)&fBVS, eRelative); //BVC(Addressing, address); - setIT(0x50, (uintptr_t)&fBVC, eRelative); + setInstructionTable(0x50, (uintptr_t)&fBVC, eRelative); //CMP(Addressing, address); - setIT(0xC9, (uintptr_t)&fCMP, eImmediate); - setIT(0xC5, (uintptr_t)&fCMP, eZeroPage); - setIT(0xD5, (uintptr_t)&fCMP, eZeroPageIndexedX); - setIT(0xCD, (uintptr_t)&fCMP, eAbsolute); - setIT(0xDD, (uintptr_t)&fCMP, eAbsoluteIndexedX); - setIT(0xD9, (uintptr_t)&fCMP, eAbsoluteIndexedY); - setIT(0xC1, (uintptr_t)&fCMP, eIndexedIndirect); - setIT(0xD1, (uintptr_t)&fCMP, eIndirectIndexed); + setInstructionTable(0xC9, (uintptr_t)&fCMP, eImmediate); + setInstructionTable(0xC5, (uintptr_t)&fCMP, eZeroPage); + setInstructionTable(0xD5, (uintptr_t)&fCMP, eZeroPageIndexedX); + setInstructionTable(0xCD, (uintptr_t)&fCMP, eAbsolute); + setInstructionTable(0xDD, (uintptr_t)&fCMP, eAbsoluteIndexedX); + setInstructionTable(0xD9, (uintptr_t)&fCMP, eAbsoluteIndexedY); + setInstructionTable(0xC1, (uintptr_t)&fCMP, eIndexedIndirect); + setInstructionTable(0xD1, (uintptr_t)&fCMP, eIndirectIndexed); //CPX(Addressing, address); - setIT(0xE0, (uintptr_t)&fCPX, eImmediate); - setIT(0xE4, (uintptr_t)&fCPX, eZeroPage); - setIT(0xEC, (uintptr_t)&fCPX, eAbsolute); + setInstructionTable(0xE0, (uintptr_t)&fCPX, eImmediate); + setInstructionTable(0xE4, (uintptr_t)&fCPX, eZeroPage); + setInstructionTable(0xEC, (uintptr_t)&fCPX, eAbsolute); //CPY(Addressing, address); - setIT(0xC0, (uintptr_t)&fCPY, eImmediate); - setIT(0xC4, (uintptr_t)&fCPY, eZeroPage); - setIT(0xCC, (uintptr_t)&fCPY, eAbsolute); + setInstructionTable(0xC0, (uintptr_t)&fCPY, eImmediate); + setInstructionTable(0xC4, (uintptr_t)&fCPY, eZeroPage); + setInstructionTable(0xCC, (uintptr_t)&fCPY, eAbsolute); //BIT(Addressing, address); - setIT(0x4C, (uintptr_t)&fBIT, eZeroPage); - setIT(0x6C, (uintptr_t)&fBIT, eAbsolute); + setInstructionTable(0x4C, (uintptr_t)&fBIT, eZeroPage); + setInstructionTable(0x6C, (uintptr_t)&fBIT, eAbsolute); // Shift and Rotate Instructions //ASL(Addressing, address); - setIT(0x0A, (uintptr_t)&fASL, eAccumulator); - setIT(0x06, (uintptr_t)&fASL, eZeroPage); - setIT(0x16, (uintptr_t)&fASL, eZeroPageIndexedX); - setIT(0x0E, (uintptr_t)&fASL, eAbsolute); - setIT(0x1E, (uintptr_t)&fASL, eAbsoluteIndexedX); + setInstructionTable(0x0A, (uintptr_t)&fASL, eAccumulator); + setInstructionTable(0x06, (uintptr_t)&fASL, eZeroPage); + setInstructionTable(0x16, (uintptr_t)&fASL, eZeroPageIndexedX); + setInstructionTable(0x0E, (uintptr_t)&fASL, eAbsolute); + setInstructionTable(0x1E, (uintptr_t)&fASL, eAbsoluteIndexedX); //LSR(Addressing, address); - setIT(0x4A, (uintptr_t)&fLSR, eAccumulator); - setIT(0x46, (uintptr_t)&fLSR, eZeroPage); - setIT(0x56, (uintptr_t)&fLSR, eZeroPageIndexedX); - setIT(0x4E, (uintptr_t)&fLSR, eAbsolute); - setIT(0x5E, (uintptr_t)&fLSR, eAbsoluteIndexedX); + setInstructionTable(0x4A, (uintptr_t)&fLSR, eAccumulator); + setInstructionTable(0x46, (uintptr_t)&fLSR, eZeroPage); + setInstructionTable(0x56, (uintptr_t)&fLSR, eZeroPageIndexedX); + setInstructionTable(0x4E, (uintptr_t)&fLSR, eAbsolute); + setInstructionTable(0x5E, (uintptr_t)&fLSR, eAbsoluteIndexedX); //ROL(Addressing, address); - setIT(0x2A, (uintptr_t)&fROL, eAccumulator); - setIT(0x26, (uintptr_t)&fROL, eZeroPage); - setIT(0x36, (uintptr_t)&fROL, eZeroPageIndexedX); - setIT(0x2E, (uintptr_t)&fROL, eAbsolute); - setIT(0x3E, (uintptr_t)&fROL, eAbsoluteIndexedX); + setInstructionTable(0x2A, (uintptr_t)&fROL, eAccumulator); + setInstructionTable(0x26, (uintptr_t)&fROL, eZeroPage); + setInstructionTable(0x36, (uintptr_t)&fROL, eZeroPageIndexedX); + setInstructionTable(0x2E, (uintptr_t)&fROL, eAbsolute); + setInstructionTable(0x3E, (uintptr_t)&fROL, eAbsoluteIndexedX); //ROR(Addressing, address); - setIT(0x6A, (uintptr_t)&fROR, eAccumulator); - setIT(0x66, (uintptr_t)&fROR, eZeroPage); - setIT(0x76, (uintptr_t)&fROR, eZeroPageIndexedX); - setIT(0x6E, (uintptr_t)&fROR, eAbsolute); - setIT(0x7E, (uintptr_t)&fROR, eAbsoluteIndexedX); + setInstructionTable(0x6A, (uintptr_t)&fROR, eAccumulator); + setInstructionTable(0x66, (uintptr_t)&fROR, eZeroPage); + setInstructionTable(0x76, (uintptr_t)&fROR, eZeroPageIndexedX); + setInstructionTable(0x6E, (uintptr_t)&fROR, eAbsolute); + setInstructionTable(0x7E, (uintptr_t)&fROR, eAbsoluteIndexedX); // Transfer Instructions //TAX(Addressing, address); - setIT(0xAA, (uintptr_t)&fTAX, eImplied); + setInstructionTable(0xAA, (uintptr_t)&fTAX, eImplied); //TAY(Addressing, address); - setIT(0xA8, (uintptr_t)&fTAY, eImplied); + setInstructionTable(0xA8, (uintptr_t)&fTAY, eImplied); //TXA(Addressing, address); - setIT(0x8A, (uintptr_t)&fTXA, eImplied); + setInstructionTable(0x8A, (uintptr_t)&fTXA, eImplied); //TYA(Addressing, address); - setIT(0x98, (uintptr_t)&fTYA, eImplied); + setInstructionTable(0x98, (uintptr_t)&fTYA, eImplied); // Stack Instructions //TSX(Addressing, address); - setIT(0xBA, (uintptr_t)&fTSX, eImplied); + setInstructionTable(0xBA, (uintptr_t)&fTSX, eImplied); //TXS(Addressing, address); - setIT(0x9A, (uintptr_t)&fTXS, eImplied); + setInstructionTable(0x9A, (uintptr_t)&fTXS, eImplied); //PHA(Addressing, address); - setIT(0x48, (uintptr_t)&fPHA, eImplied); + setInstructionTable(0x48, (uintptr_t)&fPHA, eImplied); //PHP(Addressing, address); - setIT(0x08, (uintptr_t)&fPHP, eImplied); + setInstructionTable(0x08, (uintptr_t)&fPHP, eImplied); //PLA(Addressing, address); - setIT(0x68, (uintptr_t)&fPLA, eImplied); + setInstructionTable(0x68, (uintptr_t)&fPLA, eImplied); //PLP(Addressing, address); - setIT(0x28, (uintptr_t)&fPLP, eImplied); + setInstructionTable(0x28, (uintptr_t)&fPLP, eImplied); // Subroutine Instructions //JSR(Addressing, address); - setIT(0x20, (uintptr_t)&fJSR, eAbsolute); + setInstructionTable(0x20, (uintptr_t)&fJSR, eAbsolute); //RTS(Addressing, address); - setIT(0x60, (uintptr_t)&fRTS, eImplied); + setInstructionTable(0x60, (uintptr_t)&fRTS, eImplied); //RTI(Addressing, address); - setIT(0x40, (uintptr_t)&fRTI, eImplied); + setInstructionTable(0x40, (uintptr_t)&fRTI, eImplied); // Set/Reset Insutrctions //CLC(Addressing, address); - setIT(0x18, (uintptr_t)&fCLC, eImplied); + setInstructionTable(0x18, (uintptr_t)&fCLC, eImplied); //CLD(Addressing, address); - setIT(0xD8, (uintptr_t)&fCLD, eImplied); + setInstructionTable(0xD8, (uintptr_t)&fCLD, eImplied); //CLI(Addressing, address); - setIT(0x58, (uintptr_t)&fCLI, eImplied); + setInstructionTable(0x58, (uintptr_t)&fCLI, eImplied); //CLV(Addressing, address); - setIT(0xB8, (uintptr_t)&fCLV, eImplied); + setInstructionTable(0xB8, (uintptr_t)&fCLV, eImplied); //SEC(Addressing, address); - setIT(0x38, (uintptr_t)&fSEC, eImplied); + setInstructionTable(0x38, (uintptr_t)&fSEC, eImplied); //SED(Addressing, address); - setIT(0xF8, (uintptr_t)&fSED, eImplied); + setInstructionTable(0xF8, (uintptr_t)&fSED, eImplied); //SEI(Addressing, address); - setIT(0x78, (uintptr_t)&fSEI, eImplied); + setInstructionTable(0x78, (uintptr_t)&fSEI, eImplied); // NOP/BRK Instructions //NOP(Addressing, address); - setIT(0xEA, (uintptr_t)&fNOP, eImplied); + setInstructionTable(0xEA, (uintptr_t)&fNOP, eImplied); //BRK(Addressing, address); - setIT(0x00, (uintptr_t)&fBRK, eImplied); + setInstructionTable(0x00, (uintptr_t)&fBRK, eImplied); } diff --git a/instruction-init.h b/instruction-init.h deleted file mode 100644 index 12cdfa4..0000000 --- a/instruction-init.h +++ /dev/null @@ -1,116 +0,0 @@ -// instruction-init.h -// Initializes every instruction function prior to addressing.h so that function addresses are accessible -// also defines the array used to refer to functions - -//InstructionTable -void* IT; -void (*func)(Addressing, address); - -#define InstructionTableSize (256*(sizeof(uintptr_t) + sizeof(Addressing))) - -/* -uintptr_t getITFunction(int i){ //Segmentation fault is occurring here, likely in next one too - uintptr_t r = (IT + (sizeof(uintptr_t)*i)); - return r; -} - -Addressing getITAddressing(int i){ - Addressing r = (IT + (sizeof(uintptr_t)*256) + (sizeof(Addressing)*i)); - return r; -} -*/ - - -void callIT(int i, address val){ - uintptr_t a = (IT + (sizeof(uintptr_t) * i)); - memcpy(&func, a, sizeof(uintptr_t)); - Addressing* r = (IT + ((sizeof(uintptr_t)*256) + (sizeof(Addressing) * i))); - func(*r, val); -} - -void initIT(){ - IT = malloc(InstructionTableSize); - FILE* fs = fopen("instructions.bin", "r"); - - /*if (fs == NULL){ - system("instruction-dump"); - }*/ - for(char* c = IT; c < (IT + InstructionTableSize); c++){ //NEED TO MAKE (IT + (256*(sizeof(uintptr_t) + sizeof(Addressing)))) a macro - *c = fgetc(fs); - } //Investigate whether doing word sized units would increase speed - fclose(fs); - //read in the instruction data binary -} - - - -// Load and Store Instructions -void fLDA(Addressing, address); -void fLDX(Addressing, address); -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); -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); -void fBCS(Addressing, address); -void fBEQ(Addressing, address); -void fBNE(Addressing, address); -void fBMI(Addressing, address); -void fBPL(Addressing, address); -void fBVS(Addressing, address); -void fBVC(Addressing, address); -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); -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); -void fCLI(Addressing, address); -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); - - - diff --git a/instruction.h b/instruction.h deleted file mode 100644 index 96f5719..0000000 --- a/instruction.h +++ /dev/null @@ -1,347 +0,0 @@ -// instruction.h -// Definition of all instruction functions, handling effect of instruction and flags. - -// array/map of pointers which all point -// to the functions which the index corresponds to. -// use that like a sort of map - - -//Instruction Data -AddData idata; - -// 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); - Memory[idata.add] = acc; -} - -void fSTX(Addressing addr, address val){ idata = fAddress(addr, val); - Memory[idata.add] = X; -} - -void fSTY(Addressing addr, address val){ idata = fAddress(addr, val); - Memory[idata.add] = Y; -} - -// Arithmetic Instructions - -void fADC(Addressing addr, address val){ idata = fAddress(addr, val); - int buffer = acc + idata.value; - setFlagV(buffer, acc); - - if (buffer > 255) - flagSet(flag_C); - else - flagClear(flag_C); - - acc += idata.value; - setFlagN(acc); - setFlagZ(acc); -} - -void fSBC(Addressing addr, address val){ idata = fAddress(addr, val); - int buffer = acc - idata.value; - setFlagV(buffer, acc); - - if (buffer < 0) - flagSet(flag_C); - else - flagClear(flag_C); - - acc -= idata.value; - setFlagN(acc); - setFlagZ(acc); -} - -//Increment and Decrement Instructions - -void fINC(Addressing addr, address val){ idata = fAddress(addr, val); - Memory[idata.add]++; - setFlagN(Memory[idata.add]); - setFlagZ(Memory[idata.add]); -} - -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); - Memory[idata.add]--; - setFlagN(Memory[idata.add]); - setFlagZ(Memory[idata.add]); -} - -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); -} - -// 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); -} - -// 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 - //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); - }if (acc == idata.value){ - flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C); - }if (acc > idata.value){ - flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C); - } -} - -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){ - flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C); - }if (X > idata.value){ - flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C); - } -} - -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){ - flagClear(flag_N); flagSet(flag_Z); flagClear(flag_C); - }if (Y > idata.value){ - flagClear(flag_N); flagClear(flag_Z); flagSet(flag_C); - } -} - -//Need to double check the function of this instruction -void fBIT(Addressing addr, address val){ idata = fAddress(addr, val); - setFlag(flag_N, (Memory[val] & flag_N)); - setFlag(flag_V, (Memory[val] & flag_V)); - if (((Memory[val] & flag_N) & (Memory[val] & flag_V)) == 0) { - flagSet(flag_Z); - } else { - flagSet(flag_Z); - } -} - -// Shift and Rotate Instructions - -void fASL(Addressing addr, address val){ idata = fAddress(addr, val); - setFlag(flag_C, (val & 0x80)); - acc = (val << 1); - setFlagN(acc); - setFlagZ(acc); -} - -void fLSR(Addressing addr, address val){ idata = fAddress(addr, val); - setFlag(flag_C, (val & 0x01)); - acc = (val >> 1); - setFlagN(acc); - setFlagZ(acc); -} - -void fROL(Addressing addr, address val){ idata = fAddress(addr, val); - setFlag(flag_C, (val & 0x80)); - acc = (val << 1); - acc |= (getFlag(flag_C) * 0x01); - setFlagN(acc); - setFlagZ(acc); -} - -void fROR(Addressing addr, address val){ idata = fAddress(addr, val); - setFlag(flag_C, (val & 0x01)); - acc = (val >> 1); - acc |= (getFlag(flag_C) * 0x80); - setFlagN(acc); - setFlagZ(acc); -} - -// 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); -} - -// 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); - Memory[0x01FF-S] = acc; - S++; -} - -void fPHP(Addressing addr, address val){ idata = fAddress(addr, val); - Memory[0x01FF-S] = P; - S++; -} - -void fPLA(Addressing addr, address val){ idata = fAddress(addr, val); - S--; - acc = Memory[0x01FF-S]; -} - -void fPLP(Addressing addr, address val){ idata = fAddress(addr, val); - S--; - P = Memory[0x01FF-S]; -} - -// Subroutine Instructions - -void fJSR(Addressing addr, address val){ idata = fAddress(addr, val); - Memory[0x01FF-S] = (idata.add-1); - S++; - PC = idata.add; -} - -void fRTS(Addressing addr, address val){ idata = fAddress(addr, val); - -} - -void fRTI(Addressing addr, address val){ idata = fAddress(addr, val); - -} - -// 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); - flagSet(flag_B); -} \ No newline at end of file diff --git a/interpreter.c b/interpreter.c index 6fea22e..e49c77a 100644 --- a/interpreter.c +++ b/interpreter.c @@ -7,8 +7,8 @@ M - Dump a page of memory */ -#include"include.h" -#include"debug.h" +#include"headers/include.h" +#include"headers/debug.h" int main(){ char c; diff --git a/main.c b/main.c deleted file mode 100644 index 1df62a0..0000000 --- a/main.c +++ /dev/null @@ -1,6 +0,0 @@ -#include"include.h" -#include"debug.h" - -int main(int argc, char *argv[]){ - -} \ No newline at end of file diff --git a/makefile b/makefile index 41cddbc..636b454 100644 --- a/makefile +++ b/makefile @@ -1,3 +1,2 @@ main: - gcc instruction-bin.c -o instruction-dump - gcc test.c -o main + gcc test.c -o main \ No newline at end of file diff --git a/test.c b/test.c index 29b327b..7da793c 100644 --- a/test.c +++ b/test.c @@ -1,23 +1,24 @@ -#include"include.h" -#include"debug.h" - -int main(){ - char c; - unsigned char a, b; - - initIT(); - - printf("\n\n"); - - dStatusDump(); dIdataDump(); printf("\n"); - - //func(*(IT + ((sizeof(uintptr_t)*256)) + (0xA9 * sizeof(Addressing))), *(IT + ((sizeof(uintptr_t)*0xA9)))); - - callIT(0xA9, 0x01); - - dStatusDump(); dIdataDump(); - - printf("%x\n", (IT + 31)); - - return 0; +#include"headers/include.h" +#include"headers/debug.h" + +int main(int argc, char *argv[]){ + initInstructionTable(); + + uintptr_t* a; + Addressing* b; + for(int i = 0; i < 256; i++){ + a = InstructionTable + (i * sizeof(uintptr_t)); + printf("\t%x", *a); if(*a < 0x10) printf("\t"); + if ((i % 4) == 3) printf("\n"); + } + for(int i = 0; i < 256; i++){ + b = InstructionTable + (256 * sizeof(uintptr_t)) + (i * sizeof(Addressing)); + printf("\t%x", *b); if(*b < 0x10) printf("\t"); + if ((i % 4) == 3) printf("\n"); + } + printf("\n"); + + dStatusDump(); dIdataDump(); printf("\n"); + callInstructionTable(0x00, 0x01); + dStatusDump(); dIdataDump(); printf("\n"); } \ No newline at end of file -- cgit v1.2.3