From 3f3574efb1e1c27d2f471c9b4a87a9786326e773 Mon Sep 17 00:00:00 2001 From: alekseiplusplus Date: Tue, 11 Apr 2023 11:55:26 +1000 Subject: work on instruction table data and interpreter --- instruction-init.h | 413 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 245 insertions(+), 168 deletions(-) (limited to 'instruction-init.h') diff --git a/instruction-init.h b/instruction-init.h index aa05d8b..01b3fd8 100644 --- a/instruction-init.h +++ b/instruction-init.h @@ -3,254 +3,331 @@ // also defines the array used to refer to functions //InstructionTable -const IT = malloc(256 * (sizeof(uintptr_t) + sizeof(Addressing))); +void* IT; + + void setIT(int i, uintptr_t p, Addressing r){ uintptr_t* p1 = (IT + (i * sizeof(uintptr_t))); *p1 = p; - Addressing* r1 = (IT + ((i * sizeof(Addressing)) + (sizeof(uintptr_t)*256))); + Addressing* r1 = (IT + ((sizeof(uintptr_t)*256)) + (i * sizeof(Addressing))); *r1 = r; } +Addressing getITAddressing(int i){ + Addressing* r = IT + (sizeof(uintptr_t)*256)) + (sizeof(Addressing)*i) + return *r; +} + void callIT(int i, address val){ void (*func)(Addressing, address); func = (IT + (sizeof(uintptr_t) * i)); Addressing r = (IT + ((i * sizeof(Addressing)) + (sizeof(uintptr_t)*256))); - func(*r, val); + func(r, val); } -// Load and Store Instructions -void fLDA(Addressing, address); - setIT(0xA9, &fLDA, eImmediate); -// For the sake of testing, only Immediate LDA will be trialled initially to make compiling and solving easier. -/* IT[0xA5] = {&fLDA, eZeroPage}; - IT[0xB5] = {&fLDA, eZeroPageIndexedX}; - IT[0xAD] = {&fLDA, eAbsolute}; - IT[0xBD] = {&fLDA, eAbsoluteIndexedX}; - IT[0xB9] = {&fLDA, eAbsoluteIndexedY}; - IT[0xA1] = {&fLDA, eIndexedIndirect}; - IT[0xB1] = {&fLDA, eIndirectIndexed}; +// Load and Store Instructions +void fLDA(Addressing, address); void fLDX(Addressing, address); - IT[0xA2] = {&fLDX, eImmediate}; - IT[0xA6] = {&fLDX, eZeroPage}; - IT[0xB6] = {&fLDX, eZeroPageIndexedY}; - IT[0xAE] = {&fLDX, eAbsolute}; - IT[0xBE] = {&fLDX, eAbsoluteIndexedY}; void fLDY(Addressing, address); - IT[0xA0] = {&fLDY, eImmediate}; - IT[0xA4] = {&fLDY, eZeroPage}; - IT[0xB4] = {&fLDY, eZeroPageIndexedX}; - IT[0xAC] = {&fLDY, eAbsolute}; - IT[0xBC] = {&fLDY, eAbsoluteIndexedX}; void fSTA(Addressing, address); - IT[0x85] = {&fSTA, eZeroPage}; - IT[0x95] = {&fSTA, eZeroPageIndexedX}; - IT[0x8D] = {&fSTA, eAbsolute}; - IT[0x9D] = {&fSTA, eAbsoluteIndexedX}; - IT[0x99] = {&fSTA, eAbsoluteIndexedY}; - IT[0x81] = {&fSTA, eIndexedIndirect}; - IT[0x91] = {&fSTA, eIndirectIndexed}; void fSTX(Addressing, address); - IT[0x86] = {&fSTX, eZeroPage}; - IT[0x96] = {&fSTX, eZeroPageIndexedX}; - IT[0x8E] = {&fSTX, eAbsolute}; void fSTY(Addressing, address); - IT[0x84] = {&fSTY, eZeroPage}; - IT[0x94] = {&fSTY, eZeroPageIndexedY}; - IT[0x8C] = {&fSTY, eAbsolute}; - // Arithmetic Instructions void fADC(Addressing, address); - IT[0x69] = {&fADC, eImmediate}; - IT[0x65] = {&fADC, eZeroPage}; - IT[0x75] = {&fADC, eZeroPageIndexedX}; - IT[0x6D] = {&fADC, eAbsolute}; - IT[0x7D] = {&fADC, eAbsoluteIndexedX}; - IT[0x79] = {&fADC, eAbsoluteIndexedY}; - IT[0x61] = {&fADC, eIndexedIndirect}; - IT[0x71] = {&fADC, eIndirectIndexed}; void fSBC(Addressing, address); - IT[0xE9] = {&fSBC, eImmediate}; - IT[0xE5] = {&fSBC, eZeroPage}; - IT[0xF5] = {&fSBC, eZeroPageIndexedX}; - IT[0xED] = {&fSBC, eAbsolute}; - IT[0xFD] = {&fSBC, eAbsoluteIndexedX}; - IT[0xF9] = {&fSBC, eAbsoluteIndexedY}; - IT[0xE1] = {&fSBC, eIndexedIndirect}; - IT[0xF1] = {&fSBC, eIndirectIndexed}; - //Increment and Decrement Instructions void fINC(Addressing, address); - IT[0xE6] = {&fINC, eZeroPage}; - IT[0xF6] = {&fINC, eZeroPageIndexedX}; - IT[0xEE] = {&fINC, eAbsolute}; - IT[0xFE] = {&fINC, eAbsoluteIndexedX}; void fINX(Addressing, address); - IT[0xE8] = {&fINX, eImplied}; void fINY(Addressing, address); - IT[0xC8] = {&fINY, eImplied}; void fDEC(Addressing, address); - IT[0xC6] = {&fDEC, eZeroPage}; - IT[0xD6] = {&fDEC, eZeroPageIndexedX}; - IT[0xCE] = {&fDEC, eAbsolute}; - IT[0xDE] = {&fDEC, eAbsoluteIndexedX}; void fDEX(Addressing, address); - IT[0xCA] = {&fDEX, eImplied}; void fDEY(Addressing, address); - IT[0x88] = {&fDEY, eImplied}; - // Logical Instructions void fAND(Addressing, address); - IT[0x29] = {&fAND, eImmediate}; - IT[0x25] = {&fAND, eZeroPage}; - IT[0x35] = {&fAND, eZeroPageIndexedX}; - IT[0x2D] = {&fAND, eAbsolute}; - IT[0x3D] = {&fAND, eAbsoluteIndexedX}; - IT[0x39] = {&fAND, eAbsoluteIndexedY}; - IT[0x21] = {&fAND, eIndexedIndirect}; - IT[0x31] = {&fAND, eIndirectIndexed}; void fORA(Addressing, address); - IT[0x09] = {&fORA, eImmediate}; - IT[0x05] = {&fORA, eZeroPage}; - IT[0x15] = {&fORA, eZeroPageIndexedX}; - IT[0x0D] = {&fORA, eAbsolute}; - IT[0x1D] = {&fORA, eAbsoluteIndexedX}; - IT[0x19] = {&fORA, eAbsoluteIndexedY}; - IT[0x01] = {&fORA, eIndexedIndirect}; - IT[0x11] = {&fORA, eIndirectIndexed}; void fEOR(Addressing, address); - IT[0x49] = {&fEOR, eImmediate}; - IT[0x45] = {&fEOR, eZeroPage}; - IT[0x55] = {&fEOR, eZeroPageIndexedX}; - IT[0x4D] = {&fEOR, eAbsolute}; - IT[0x5D] = {&fEOR, eAbsoluteIndexedX}; - IT[0x59] = {&fEOR, eAbsoluteIndexedY}; - IT[0x41] = {&fEOR, eIndexedIndirect}; - IT[0x51] = {&fEOR, eIndirectIndexed}; - // Jump, Branch, Compare, and Test Bits void fJMP(Addressing, address); - IT[0x4C] = {&fJMP, eAbsolute}; - IT[0x6C] = {&fJMP, eIndirectAbsolute}; void fBCC(Addressing, address); - IT[0x90] = {&fBCC, eRelative}; void fBCS(Addressing, address); - IT[0xB0] = {&fBCS, eRelative}; void fBEQ(Addressing, address); - IT[0xF0] = {&fBEQ, eRelative}; void fBNE(Addressing, address); - IT[0xD0] = {&fBNE, eRelative}; void fBMI(Addressing, address); - IT[0x30] = {&fBMI, eRelative}; void fBPL(Addressing, address); - IT[0x10] = {&fBPL, eRelative}; void fBVS(Addressing, address); - IT[0x70] = {&fBVS, eRelative}; void fBVC(Addressing, address); - IT[0x50] = {&fBVC, eRelative}; void fCMP(Addressing, address); - IT[0xC9] = {&fCMP, eImmediate}; - IT[0xC5] = {&fCMP, eZeroPage}; - IT[0xD5] = {&fCMP, eZeroPageIndexedX}; - IT[0xCD] = {&fCMP, eAbsolute}; - IT[0xDD] = {&fCMP, eAbsoluteIndexedX}; - IT[0xD9] = {&fCMP, eAbsoluteIndexedY}; - IT[0xC1] = {&fCMP, eIndexedIndirect}; - IT[0xD1] = {&fCMP, eIndirectIndexed}; void fCPX(Addressing, address); - IT[0xE0] = {&fCPX, eImmediate}; - IT[0xE4] = {&fCPX, eZeroPage}; - IT[0xEC] = {&fCPX, eAbsolute}; void fCPY(Addressing, address); - IT[0xC0] = {&fCPY, eImmediate}; - IT[0xC4] = {&fCPY, eZeroPage}; - IT[0xCC] = {&fCPY, eAbsolute}; void fBIT(Addressing, address); - IT[0x4C] = {&fBIT, eZeroPage}; - IT[0x6C] = {&fBIT, eAbsolute}; - // Shift and Rotate Instructions void fASL(Addressing, address); - IT[0x0A] = {&fASL, eAccumulator}; - IT[0x06] = {&fASL, eZeroPage}; - IT[0x16] = {&fASL, eZeroPageIndexedX}; - IT[0x0E] = {&fASL, eAbsolute}; - IT[0x1E] = {&fASL, eAbsoluteIndexedX}; void fLSR(Addressing, address); - IT[0x4A] = {&fLSR, eAccumulator}; - IT[0x46] = {&fLSR, eZeroPage}; - IT[0x56] = {&fLSR, eZeroPageIndexedX}; - IT[0x4E] = {&fLSR, eAbsolute}; - IT[0x5E] = {&fLSR, eAbsoluteIndexedX}; void fROL(Addressing, address); - IT[0x2A] = {&fROL, eAccumulator}; - IT[0x26] = {&fROL, eZeroPage}; - IT[0x36] = {&fROL, eZeroPageIndexedX}; - IT[0x2E] = {&fROL, eAbsolute}; - IT[0x3E] = {&fROL, eAbsoluteIndexedX}; void fROR(Addressing, address); - IT[0x6A] = {&fROR, eAccumulator}; - IT[0x66] = {&fROR, eZeroPage}; - IT[0x76] = {&fROR, eZeroPageIndexedX}; - IT[0x6E] = {&fROR, eAbsolute}; - IT[0x7E] = {&fROR, eAbsoluteIndexedX}; - // Transfer Instructions void fTAX(Addressing, address); - IT[0xAA] = {&fTAX, eImplied}; void fTAY(Addressing, address); - IT[0xA8] = {&fTAY, eImplied}; void fTXA(Addressing, address); - IT[0x8A] = {&fTXA, eImplied}; void fTYA(Addressing, address); - IT[0x98] = {&fTYA, eImplied}; - // Stack Instructions void fTSX(Addressing, address); - IT[0xBA] = {&fTSX, eImplied}; void fTXS(Addressing, address); - IT[0x9A] = {&fTXS, eImplied}; void fPHA(Addressing, address); - IT[0x48] = {&fPHA, eImplied}; void fPHP(Addressing, address); - IT[0x08] = {&fPHP, eImplied}; void fPLA(Addressing, address); - IT[0x68] = {&fPLA, eImplied}; void fPLP(Addressing, address); - IT[0x28] = {&fPLP, eImplied}; - // Subroutine Instructions void fJSR(Addressing, address); - IT[0x20] = {&fJSR, eAbsolute}; void fRTS(Addressing, address); - IT[0x60] = {&fRTS, eImplied}; void fRTI(Addressing, address); - IT[0x40] = {&fRTI, eImplied}; - // Set/Reset Insutrctions void fCLC(Addressing, address); - IT[0x18] = {&fCLC, eImplied}; void fCLD(Addressing, address); - IT[0xD8] = {&fCLD, eImplied}; void fCLI(Addressing, address); - IT[0x58] = {&fCLI, eImplied}; void fCLV(Addressing, address); - IT[0xB8] = {&fCLV, eImplied}; void fSEC(Addressing, address); - IT[0x38] = {&fSEC, eImplied}; void fSED(Addressing, address); - IT[0xF8] = {&fSED, eImplied}; void fSEI(Addressing, address); - IT[0x78] = {&fSEI, eImplied}; - // NOP/BRK Instructions void fNOP(Addressing, address); - IT[0xEA] = {&fNOP, eImplied}; void fBRK(Addressing, address); - IT[0x00] = {&fBRK, eImplied}; -*/ \ No newline at end of file + +void initIT(){ + IT = malloc(256 * (sizeof(uintptr_t) + sizeof(Addressing))); + + // Load and Store Instructions + // fLDA(Addressing, address); + setIT(0xA9, &fLDA, eImmediate); + setIT(0xA5, &fLDA, eZeroPage); + setIT(0xB5, &fLDA, eZeroPageIndexedX); + setIT(0xAD, &fLDA, eAbsolute); + setIT(0xBD, &fLDA, eAbsoluteIndexedX); + setIT(0xB9, &fLDA, eAbsoluteIndexedY); + setIT(0xA1, &fLDA, eIndexedIndirect); + setIT(0xB1, &fLDA, eIndirectIndexed); + // fLDX(Addressing, address); + setIT(0xA2, &fLDX, eImmediate); + setIT(0xA6, &fLDX, eZeroPage); + setIT(0xB6, &fLDX, eZeroPageIndexedY); + setIT(0xAE, &fLDX, eAbsolute); + setIT(0xBE, &fLDX, eAbsoluteIndexedY); + // fLDY(Addressing, address); + setIT(0xA0, &fLDY, eImmediate); + setIT(0xA4, &fLDY, eZeroPage); + setIT(0xB4, &fLDY, eZeroPageIndexedX); + setIT(0xAC, &fLDY, eAbsolute); + setIT(0xBC, &fLDY, eAbsoluteIndexedX); + // fSTA(Addressing, address); + setIT(0x85, &fSTA, eZeroPage); + setIT(0x95, &fSTA, eZeroPageIndexedX); + setIT(0x8D, &fSTA, eAbsolute); + setIT(0x9D, &fSTA, eAbsoluteIndexedX); + setIT(0x99, &fSTA, eAbsoluteIndexedY); + setIT(0x81, &fSTA, eIndexedIndirect); + setIT(0x91, &fSTA, eIndirectIndexed); + // fSTX(Addressing, address); + setIT(0x86, &fSTX, eZeroPage); + setIT(0x96, &fSTX, eZeroPageIndexedX); + setIT(0x8E, &fSTX, eAbsolute); + // fSTY(Addressing, address); + setIT(0x84, &fSTY, eZeroPage); + setIT(0x94, &fSTY, eZeroPageIndexedY); + setIT(0x8C, &fSTY, eAbsolute); + + // Arithmetic Instructions + // fADC(Addressing, address); + setIT(0x69, &fADC, eImmediate); + setIT(0x65, &fADC, eZeroPage); + setIT(0x75, &fADC, eZeroPageIndexedX); + setIT(0x6D, &fADC, eAbsolute); + setIT(0x7D, &fADC, eAbsoluteIndexedX); + setIT(0x79, &fADC, eAbsoluteIndexedY); + setIT(0x61, &fADC, eIndexedIndirect); + setIT(0x71, &fADC, eIndirectIndexed); + // fSBC(Addressing, address); + setIT(0xE9, &fSBC, eImmediate); + setIT(0xE5, &fSBC, eZeroPage); + setIT(0xF5, &fSBC, eZeroPageIndexedX); + setIT(0xED, &fSBC, eAbsolute); + setIT(0xFD, &fSBC, eAbsoluteIndexedX); + setIT(0xF9, &fSBC, eAbsoluteIndexedY); + setIT(0xE1, &fSBC, eIndexedIndirect); + setIT(0xF1, &fSBC, eIndirectIndexed); + + //Increment and Decrement Instructions + //INC(Addressing, address); + setIT(0xE6, &fINC, eZeroPage); + setIT(0xF6, &fINC, eZeroPageIndexedX); + setIT(0xEE, &fINC, eAbsolute); + setIT(0xFE, &fINC, eAbsoluteIndexedX); + //INX(Addressing, address); + setIT(0xE8, &fINX, eImplied); + //INY(Addressing, address); + setIT(0xC8, &fINY, eImplied); + //DEC(Addressing, address); + setIT(0xC6, &fDEC, eZeroPage); + setIT(0xD6, &fDEC, eZeroPageIndexedX); + setIT(0xCE, &fDEC, eAbsolute); + setIT(0xDE, &fDEC, eAbsoluteIndexedX); + //DEX(Addressing, address); + setIT(0xCA, &fDEX, eImplied); + //DEY(Addressing, address); + setIT(0x88, &fDEY, eImplied); + + // Logical Instructions + //AND(Addressing, address); + setIT(0x29, &fAND, eImmediate); + setIT(0x25, &fAND, eZeroPage); + setIT(0x35, &fAND, eZeroPageIndexedX); + setIT(0x2D, &fAND, eAbsolute); + setIT(0x3D, &fAND, eAbsoluteIndexedX); + setIT(0x39, &fAND, eAbsoluteIndexedY); + setIT(0x21, &fAND, eIndexedIndirect); + setIT(0x31, &fAND, eIndirectIndexed); + //ORA(Addressing, address); + setIT(0x09, &fORA, eImmediate); + setIT(0x05, &fORA, eZeroPage); + setIT(0x15, &fORA, eZeroPageIndexedX); + setIT(0x0D, &fORA, eAbsolute); + setIT(0x1D, &fORA, eAbsoluteIndexedX); + setIT(0x19, &fORA, eAbsoluteIndexedY); + setIT(0x01, &fORA, eIndexedIndirect); + setIT(0x11, &fORA, eIndirectIndexed); + //EOR(Addressing, address); + setIT(0x49, &fEOR, eImmediate); + setIT(0x45, &fEOR, eZeroPage); + setIT(0x55, &fEOR, eZeroPageIndexedX); + setIT(0x4D, &fEOR, eAbsolute); + setIT(0x5D, &fEOR, eAbsoluteIndexedX); + setIT(0x59, &fEOR, eAbsoluteIndexedY); + setIT(0x41, &fEOR, eIndexedIndirect); + setIT(0x51, &fEOR, eIndirectIndexed); + + // Jump, Branch, Compare, and Test Bits + //JMP(Addressing, address); + setIT(0x4C, &fJMP, eAbsolute); + setIT(0x6C, &fJMP, eIndirectAbsolute); + //BCC(Addressing, address); + setIT(0x90, &fBCC, eRelative); + //BCS(Addressing, address); + setIT(0xB0, &fBCS, eRelative); + //BEQ(Addressing, address); + setIT(0xF0, &fBEQ, eRelative); + //BNE(Addressing, address); + setIT(0xD0, &fBNE, eRelative); + //BMI(Addressing, address); + setIT(0x30, &fBMI, eRelative); + //BPL(Addressing, address); + setIT(0x10, &fBPL, eRelative); + //BVS(Addressing, address); + setIT(0x70, &fBVS, eRelative); + //BVC(Addressing, address); + setIT(0x50, &fBVC, eRelative); + //CMP(Addressing, address); + setIT(0xC9, &fCMP, eImmediate); + setIT(0xC5, &fCMP, eZeroPage); + setIT(0xD5, &fCMP, eZeroPageIndexedX); + setIT(0xCD, &fCMP, eAbsolute); + setIT(0xDD, &fCMP, eAbsoluteIndexedX); + setIT(0xD9, &fCMP, eAbsoluteIndexedY); + setIT(0xC1, &fCMP, eIndexedIndirect); + setIT(0xD1, &fCMP, eIndirectIndexed); + //CPX(Addressing, address); + setIT(0xE0, &fCPX, eImmediate); + setIT(0xE4, &fCPX, eZeroPage); + setIT(0xEC, &fCPX, eAbsolute); + //CPY(Addressing, address); + setIT(0xC0, &fCPY, eImmediate); + setIT(0xC4, &fCPY, eZeroPage); + setIT(0xCC, &fCPY, eAbsolute); + //BIT(Addressing, address); + setIT(0x4C, &fBIT, eZeroPage); + setIT(0x6C, &fBIT, eAbsolute); + + // Shift and Rotate Instructions + //ASL(Addressing, address); + setIT(0x0A, &fASL, eAccumulator); + setIT(0x06, &fASL, eZeroPage); + setIT(0x16, &fASL, eZeroPageIndexedX); + setIT(0x0E, &fASL, eAbsolute); + setIT(0x1E, &fASL, eAbsoluteIndexedX); + //LSR(Addressing, address); + setIT(0x4A, &fLSR, eAccumulator); + setIT(0x46, &fLSR, eZeroPage); + setIT(0x56, &fLSR, eZeroPageIndexedX); + setIT(0x4E, &fLSR, eAbsolute); + setIT(0x5E, &fLSR, eAbsoluteIndexedX); + //ROL(Addressing, address); + setIT(0x2A, &fROL, eAccumulator); + setIT(0x26, &fROL, eZeroPage); + setIT(0x36, &fROL, eZeroPageIndexedX); + setIT(0x2E, &fROL, eAbsolute); + setIT(0x3E, &fROL, eAbsoluteIndexedX); + //ROR(Addressing, address); + setIT(0x6A, &fROR, eAccumulator); + setIT(0x66, &fROR, eZeroPage); + setIT(0x76, &fROR, eZeroPageIndexedX); + setIT(0x6E, &fROR, eAbsolute); + setIT(0x7E, &fROR, eAbsoluteIndexedX); + + // Transfer Instructions + //TAX(Addressing, address); + setIT(0xAA, &fTAX, eImplied); + //TAY(Addressing, address); + setIT(0xA8, &fTAY, eImplied); + //TXA(Addressing, address); + setIT(0x8A, &fTXA, eImplied); + //TYA(Addressing, address); + setIT(0x98, &fTYA, eImplied); + + // Stack Instructions + //TSX(Addressing, address); + setIT(0xBA, &fTSX, eImplied); + //TXS(Addressing, address); + setIT(0x9A, &fTXS, eImplied); + //PHA(Addressing, address); + setIT(0x48, &fPHA, eImplied); + //PHP(Addressing, address); + setIT(0x08, &fPHP, eImplied); + //PLA(Addressing, address); + setIT(0x68, &fPLA, eImplied); + //PLP(Addressing, address); + setIT(0x28, &fPLP, eImplied); + + // Subroutine Instructions + //JSR(Addressing, address); + setIT(0x20, &fJSR, eAbsolute); + //RTS(Addressing, address); + setIT(0x60, &fRTS, eImplied); + //RTI(Addressing, address); + setIT(0x40, &fRTI, eImplied); + + // Set/Reset Insutrctions + //CLC(Addressing, address); + setIT(0x18, &fCLC, eImplied); + //CLD(Addressing, address); + setIT(0xD8, &fCLD, eImplied); + //CLI(Addressing, address); + setIT(0x58, &fCLI, eImplied); + //CLV(Addressing, address); + setIT(0xB8, &fCLV, eImplied); + //SEC(Addressing, address); + setIT(0x38, &fSEC, eImplied); + //SED(Addressing, address); + setIT(0xF8, &fSED, eImplied); + //SEI(Addressing, address); + setIT(0x78, &fSEI, eImplied); + + // NOP/BRK Instructions + //NOP(Addressing, address); + setIT(0xEA, &fNOP, eImplied); + //BRK(Addressing, address); + setIT(0x00, &fBRK, eImplied); +} \ No newline at end of file -- cgit v1.2.3