// table.c #include"table.h" void* InstructionTable; void (*func)(Addressing, address); 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){ val = 0; // TODO: Let the initial value of val be redundant for now so as to not break anything, but fix later // Setup to call the correct function. uintptr_t a = GetInstructionTableFunction(i); memcpy(&func, a, sizeof(uintptr_t)); // Find the correct addressing mode. Addressing* r = (InstructionTable + ((sizeof(uintptr_t)*256) + (sizeof(Addressing) * i))); // Set val if (fAddressGetLength(*r) >= 2) val += (address)GetMemory(PC+1); if (fAddressGetLength(*r) == 3) val += (address)GetMemory(PC+2) << 8; // Set idata idata = fAddress(*r, val); // Perform function func(*r, val); } void SetInstructionTable(int i, uintptr_t p, Addressing r){ uintptr_t* p1 = ( InstructionTable + (sizeof(uintptr_t) * i) ); *p1 = p; Addressing* r1 = ( InstructionTable + (sizeof(uintptr_t) * 256) + (sizeof(Addressing) * i) ); *r1 = r; } void InitInstructionTable(){ InstructionTable = malloc(InstructionTableSize); // Load and Store Instructions // fLDA(Addressing, address); 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); 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); 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); 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); SetInstructionTable(0x86, (uintptr_t)&fSTX, eZeroPage); SetInstructionTable(0x96, (uintptr_t)&fSTX, eZeroPageIndexedX); SetInstructionTable(0x8E, (uintptr_t)&fSTX, eAbsolute); // fSTY(Addressing, address); SetInstructionTable(0x84, (uintptr_t)&fSTY, eZeroPage); SetInstructionTable(0x94, (uintptr_t)&fSTY, eZeroPageIndexedY); SetInstructionTable(0x8C, (uintptr_t)&fSTY, eAbsolute); // Arithmetic Instructions // fADC(Addressing, address); 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); 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); 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); SetInstructionTable(0xE8, (uintptr_t)&fINX, eImplied); //INY(Addressing, address); SetInstructionTable(0xC8, (uintptr_t)&fINY, eImplied); //DEC(Addressing, address); 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); SetInstructionTable(0xCA, (uintptr_t)&fDEX, eImplied); //DEY(Addressing, address); SetInstructionTable(0x88, (uintptr_t)&fDEY, eImplied); // Logical Instructions //AND(Addressing, address); 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); 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); 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); SetInstructionTable(0x4C, (uintptr_t)&fJMP, eAbsolute); SetInstructionTable(0x6C, (uintptr_t)&fJMP, eIndirectAbsolute); //BCC(Addressing, address); SetInstructionTable(0x90, (uintptr_t)&fBCC, eRelative); //BCS(Addressing, address); SetInstructionTable(0xB0, (uintptr_t)&fBCS, eRelative); //BEQ(Addressing, address); SetInstructionTable(0xF0, (uintptr_t)&fBEQ, eRelative); //BNE(Addressing, address); SetInstructionTable(0xD0, (uintptr_t)&fBNE, eRelative); //BMI(Addressing, address); SetInstructionTable(0x30, (uintptr_t)&fBMI, eRelative); //BPL(Addressing, address); SetInstructionTable(0x10, (uintptr_t)&fBPL, eRelative); //BVS(Addressing, address); SetInstructionTable(0x70, (uintptr_t)&fBVS, eRelative); //BVC(Addressing, address); SetInstructionTable(0x50, (uintptr_t)&fBVC, eRelative); //CMP(Addressing, address); 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); SetInstructionTable(0xE0, (uintptr_t)&fCPX, eImmediate); SetInstructionTable(0xE4, (uintptr_t)&fCPX, eZeroPage); SetInstructionTable(0xEC, (uintptr_t)&fCPX, eAbsolute); //CPY(Addressing, address); SetInstructionTable(0xC0, (uintptr_t)&fCPY, eImmediate); SetInstructionTable(0xC4, (uintptr_t)&fCPY, eZeroPage); SetInstructionTable(0xCC, (uintptr_t)&fCPY, eAbsolute); //BIT(Addressing, address); SetInstructionTable(0x24, (uintptr_t)&fBIT, eZeroPage); SetInstructionTable(0x2C, (uintptr_t)&fBIT, eAbsolute); // Shift and Rotate Instructions //ASL(Addressing, address); 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); 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); 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); 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); SetInstructionTable(0xAA, (uintptr_t)&fTAX, eImplied); //TAY(Addressing, address); SetInstructionTable(0xA8, (uintptr_t)&fTAY, eImplied); //TXA(Addressing, address); SetInstructionTable(0x8A, (uintptr_t)&fTXA, eImplied); //TYA(Addressing, address); SetInstructionTable(0x98, (uintptr_t)&fTYA, eImplied); // Stack Instructions //TSX(Addressing, address); SetInstructionTable(0xBA, (uintptr_t)&fTSX, eImplied); //TXS(Addressing, address); SetInstructionTable(0x9A, (uintptr_t)&fTXS, eImplied); //PHA(Addressing, address); SetInstructionTable(0x48, (uintptr_t)&fPHA, eImplied); //PHP(Addressing, address); SetInstructionTable(0x08, (uintptr_t)&fPHP, eImplied); //PLA(Addressing, address); SetInstructionTable(0x68, (uintptr_t)&fPLA, eImplied); //PLP(Addressing, address); SetInstructionTable(0x28, (uintptr_t)&fPLP, eImplied); // Subroutine Instructions //JSR(Addressing, address); SetInstructionTable(0x20, (uintptr_t)&fJSR, eAbsolute); //RTS(Addressing, address); SetInstructionTable(0x60, (uintptr_t)&fRTS, eImplied); //RTI(Addressing, address); SetInstructionTable(0x40, (uintptr_t)&fRTI, eImplied); // Set/Reset Insutrctions //CLC(Addressing, address); SetInstructionTable(0x18, (uintptr_t)&fCLC, eImplied); //CLD(Addressing, address); SetInstructionTable(0xD8, (uintptr_t)&fCLD, eImplied); //CLI(Addressing, address); SetInstructionTable(0x58, (uintptr_t)&fCLI, eImplied); //CLV(Addressing, address); SetInstructionTable(0xB8, (uintptr_t)&fCLV, eImplied); //SEC(Addressing, address); SetInstructionTable(0x38, (uintptr_t)&fSEC, eImplied); //SED(Addressing, address); SetInstructionTable(0xF8, (uintptr_t)&fSED, eImplied); //SEI(Addressing, address); SetInstructionTable(0x78, (uintptr_t)&fSEI, eImplied); // NOP/BRK Instructions //NOP(Addressing, address); SetInstructionTable(0xEA, (uintptr_t)&fNOP, eImplied); //BRK(Addressing, address); SetInstructionTable(0x00, (uintptr_t)&fBRK, eImplied); }