diff --git a/6502-emu.c b/6502-emu.c index ebe09ed..911eecf 100644 --- a/6502-emu.c +++ b/6502-emu.c @@ -20,10 +20,12 @@ void step_delay() void run_cpu() { - for (;;) { - // CPU timing is currently very far from being cycle-accurate - for (int i = 0; i < (CPU_FREQ / (ONE_SECOND / STEP_DURATION)); i++) { - step_cpu(); + int cycles = 0; + int cycles_per_step = (CPU_FREQ / (ONE_SECOND / STEP_DURATION)); + + for (;;) { + for (cycles %= cycles_per_step; cycles < cycles_per_step;) { + cycles += step_cpu(); step_uart(); } step_delay(); // remove this for more speed diff --git a/6502.c b/6502.c index 9582c6a..7b584f6 100644 --- a/6502.c +++ b/6502.c @@ -575,262 +575,262 @@ void init_tables() // this is only done at runtime to improve code readability. /* Instructions */ - instructions[0x00] = (Instruction) {"BRK impl", inst_BRK, IMPL}; - instructions[0x01] = (Instruction) {"ORA X,ind", inst_ORA, XIND}; - instructions[0x02] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x03] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x04] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x05] = (Instruction) {"ORA zpg", inst_ORA, ZP}; - instructions[0x06] = (Instruction) {"ASL zpg", inst_ASL, ZP}; - instructions[0x07] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x08] = (Instruction) {"PHP impl", inst_PHP, IMPL}; - instructions[0x09] = (Instruction) {"ORA #", inst_ORA, IMM}; - instructions[0x0A] = (Instruction) {"ASL A", inst_ASL, ACC}; - instructions[0x0B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x0C] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x0D] = (Instruction) {"ORA abs", inst_ORA, ABS}; - instructions[0x0E] = (Instruction) {"ASL abs", inst_ASL, ABS}; - instructions[0x0F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x10] = (Instruction) {"BPL rel", inst_BPL, REL}; - instructions[0x11] = (Instruction) {"ORA ind,Y", inst_ORA, INDY}; - instructions[0x12] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x13] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x14] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x15] = (Instruction) {"ORA zpg,X", inst_ORA, ZPX}; - instructions[0x16] = (Instruction) {"ASL zpg,X", inst_ASL, ZPX}; - instructions[0x17] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x18] = (Instruction) {"CLC impl", inst_CLC, IMPL}; - instructions[0x19] = (Instruction) {"ORA abs,Y", inst_ORA, ABSY}; - instructions[0x1A] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x1B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x1C] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x1D] = (Instruction) {"ORA abs,X", inst_ORA, ABSX}; - instructions[0x1E] = (Instruction) {"ASL abs,X", inst_ASL, ABSX}; - instructions[0x1F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x20] = (Instruction) {"JSR abs", inst_JSR, ABS}; - instructions[0x21] = (Instruction) {"AND X,ind", inst_AND, XIND}; - instructions[0x22] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x23] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x24] = (Instruction) {"BIT zpg", inst_BIT, ZP}; - instructions[0x25] = (Instruction) {"AND zpg", inst_AND, ZP}; - instructions[0x26] = (Instruction) {"ROL zpg", inst_ROL, ZP}; - instructions[0x27] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x28] = (Instruction) {"PLP impl", inst_PLP, IMPL}; - instructions[0x29] = (Instruction) {"AND #", inst_AND, IMM}; - instructions[0x2A] = (Instruction) {"ROL A", inst_ROL, ACC}; - instructions[0x2B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x2C] = (Instruction) {"BIT abs", inst_BIT, ABS}; - instructions[0x2D] = (Instruction) {"AND abs", inst_AND, ABS}; - instructions[0x2E] = (Instruction) {"ROL abs", inst_ROL, ABS}; - instructions[0x2F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x30] = (Instruction) {"BMI rel", inst_BMI, REL}; - instructions[0x31] = (Instruction) {"AND ind,Y", inst_AND, INDY}; - instructions[0x32] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x33] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x34] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x35] = (Instruction) {"AND zpg,X", inst_AND, ZPX}; - instructions[0x36] = (Instruction) {"ROL zpg,X", inst_ROL, ZPX}; - instructions[0x37] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x38] = (Instruction) {"SEC impl", inst_SEC, IMPL}; - instructions[0x39] = (Instruction) {"AND abs,Y", inst_AND, ABSY}; - instructions[0x3A] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x3B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x3C] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x3D] = (Instruction) {"AND abs,X", inst_AND, ABSX}; - instructions[0x3E] = (Instruction) {"ROL abs,X", inst_ROL, ABSX}; - instructions[0x3F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x40] = (Instruction) {"RTI impl", inst_RTI, IMPL}; - instructions[0x41] = (Instruction) {"EOR X,ind", inst_EOR, XIND}; - instructions[0x42] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x43] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x44] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x45] = (Instruction) {"EOR zpg", inst_EOR, ZP}; - instructions[0x46] = (Instruction) {"LSR zpg", inst_LSR, ZP}; - instructions[0x47] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x48] = (Instruction) {"PHA impl", inst_PHA, IMPL}; - instructions[0x49] = (Instruction) {"EOR #", inst_EOR, IMM}; - instructions[0x4A] = (Instruction) {"LSR A", inst_LSR, ACC}; - instructions[0x4B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x4C] = (Instruction) {"JMP abs", inst_JMP, ABS}; - instructions[0x4D] = (Instruction) {"EOR abs", inst_EOR, ABS}; - instructions[0x4E] = (Instruction) {"LSR abs", inst_LSR, ABS}; - instructions[0x4F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x50] = (Instruction) {"BVC rel", inst_BVC, REL}; - instructions[0x51] = (Instruction) {"EOR ind,Y", inst_EOR, INDY}; - instructions[0x52] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x53] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x54] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x55] = (Instruction) {"EOR zpg,X", inst_EOR, ZPX}; - instructions[0x56] = (Instruction) {"LSR zpg,X", inst_LSR, ZPX}; - instructions[0x57] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x58] = (Instruction) {"CLI impl", inst_CLI, IMPL}; - instructions[0x59] = (Instruction) {"EOR abs,Y", inst_EOR, ABSY}; - instructions[0x5A] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x5B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x5C] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x5D] = (Instruction) {"EOR abs,X", inst_EOR, ABSX}; - instructions[0x5E] = (Instruction) {"LSR abs,X", inst_LSR, ABSX}; - instructions[0x5F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x60] = (Instruction) {"RTS impl", inst_RTS, IMPL}; - instructions[0x61] = (Instruction) {"ADC X,ind", inst_ADC, XIND}; - instructions[0x62] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x63] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x64] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x65] = (Instruction) {"ADC zpg", inst_ADC, ZP}; - instructions[0x66] = (Instruction) {"ROR zpg", inst_ROR, ZP}; - instructions[0x67] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x68] = (Instruction) {"PLA impl", inst_PLA, IMPL}; - instructions[0x69] = (Instruction) {"ADC #", inst_ADC, IMM}; - instructions[0x6A] = (Instruction) {"ROR A", inst_ROR, ACC}; - instructions[0x6B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x6C] = (Instruction) {"JMP ind", inst_JMP, IND}; - instructions[0x6D] = (Instruction) {"ADC abs", inst_ADC, ABS}; - instructions[0x6E] = (Instruction) {"ROR abs", inst_ROR, ABS}; - instructions[0x6F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x70] = (Instruction) {"BVS rel", inst_BVS, REL}; - instructions[0x71] = (Instruction) {"ADC ind,Y", inst_ADC, INDY}; - instructions[0x72] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x73] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x74] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x75] = (Instruction) {"ADC zpg,X", inst_ADC, ZPX}; - instructions[0x76] = (Instruction) {"ROR zpg,X", inst_ROR, ZPX}; - instructions[0x77] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x78] = (Instruction) {"SEI impl", inst_SEI, IMPL}; - instructions[0x79] = (Instruction) {"ADC abs,Y", inst_ADC, ABSY}; - instructions[0x7A] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x7B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x7C] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x7D] = (Instruction) {"ADC abs,X", inst_ADC, ABSX}; - instructions[0x7E] = (Instruction) {"ROR abs,X", inst_ROR, ABSX}; - instructions[0x7F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x80] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x81] = (Instruction) {"STA X,ind", inst_STA, XIND}; - instructions[0x82] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x83] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x84] = (Instruction) {"STY zpg", inst_STY, ZP}; - instructions[0x85] = (Instruction) {"STA zpg", inst_STA, ZP}; - instructions[0x86] = (Instruction) {"STX zpg", inst_STX, ZP}; - instructions[0x87] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x88] = (Instruction) {"DEY impl", inst_DEY, IMPL}; - instructions[0x89] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x8A] = (Instruction) {"TXA impl", inst_TXA, IMPL}; - instructions[0x8B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x8C] = (Instruction) {"STY abs", inst_STY, ABS}; - instructions[0x8D] = (Instruction) {"STA abs", inst_STA, ABS}; - instructions[0x8E] = (Instruction) {"STX abs", inst_STX, ABS}; - instructions[0x8F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x90] = (Instruction) {"BCC rel", inst_BCC, REL}; - instructions[0x91] = (Instruction) {"STA ind,Y", inst_STA, INDY}; - instructions[0x92] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x93] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x94] = (Instruction) {"STY zpg,X", inst_STY, ZPX}; - instructions[0x95] = (Instruction) {"STA zpg,X", inst_STA, ZPX}; - instructions[0x96] = (Instruction) {"STX zpg,Y", inst_STX, ZPY}; - instructions[0x97] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x98] = (Instruction) {"TYA impl", inst_TYA, IMPL}; - instructions[0x99] = (Instruction) {"STA abs,Y", inst_STA, ABSY}; - instructions[0x9A] = (Instruction) {"TXS impl", inst_TXS, IMPL}; - instructions[0x9B] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x9C] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x9D] = (Instruction) {"STA abs,X", inst_STA, ABSX}; - instructions[0x9E] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0x9F] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xA0] = (Instruction) {"LDY #", inst_LDY, IMM}; - instructions[0xA1] = (Instruction) {"LDA X,ind", inst_LDA, XIND}; - instructions[0xA2] = (Instruction) {"LDX #", inst_LDX, IMM}; - instructions[0xA3] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xA4] = (Instruction) {"LDY zpg", inst_LDY, ZP}; - instructions[0xA5] = (Instruction) {"LDA zpg", inst_LDA, ZP}; - instructions[0xA6] = (Instruction) {"LDX zpg", inst_LDX, ZP}; - instructions[0xA7] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xA8] = (Instruction) {"TAY impl", inst_TAY, IMPL}; - instructions[0xA9] = (Instruction) {"LDA #", inst_LDA, IMM}; - instructions[0xAA] = (Instruction) {"TAX impl", inst_TAX, IMPL}; - instructions[0xAB] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xAC] = (Instruction) {"LDY abs", inst_LDY, ABS}; - instructions[0xAD] = (Instruction) {"LDA abs", inst_LDA, ABS}; - instructions[0xAE] = (Instruction) {"LDX abs", inst_LDX, ABS}; - instructions[0xAF] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xB0] = (Instruction) {"BCS rel", inst_BCS, REL}; - instructions[0xB1] = (Instruction) {"LDA ind,Y", inst_LDA, INDY}; - instructions[0xB2] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xB3] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xB4] = (Instruction) {"LDY zpg,X", inst_LDY, ZPX}; - instructions[0xB5] = (Instruction) {"LDA zpg,X", inst_LDA, ZPX}; - instructions[0xB6] = (Instruction) {"LDX zpg,Y", inst_LDX, ZPY}; - instructions[0xB7] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xB8] = (Instruction) {"CLV impl", inst_CLV, IMPL}; - instructions[0xB9] = (Instruction) {"LDA abs,Y", inst_LDA, ABSY}; - instructions[0xBA] = (Instruction) {"TSX impl", inst_TSX, IMPL}; - instructions[0xBB] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xBC] = (Instruction) {"LDY abs,X", inst_LDY, ABSX}; - instructions[0xBD] = (Instruction) {"LDA abs,X", inst_LDA, ABSX}; - instructions[0xBE] = (Instruction) {"LDX abs,Y", inst_LDX, ABSY}; - instructions[0xBF] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xC0] = (Instruction) {"CPY #", inst_CPY, IMM}; - instructions[0xC1] = (Instruction) {"CMP X,ind", inst_CMP, XIND}; - instructions[0xC2] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xC3] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xC4] = (Instruction) {"CPY zpg", inst_CPY, ZP}; - instructions[0xC5] = (Instruction) {"CMP zpg", inst_CMP, ZP}; - instructions[0xC6] = (Instruction) {"DEC zpg", inst_DEC, ZP}; - instructions[0xC7] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xC8] = (Instruction) {"INY impl", inst_INY, IMPL}; - instructions[0xC9] = (Instruction) {"CMP #", inst_CMP, IMM}; - instructions[0xCA] = (Instruction) {"DEX impl", inst_DEX, IMPL}; - instructions[0xCB] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xCC] = (Instruction) {"CPY abs", inst_CPY, ABS}; - instructions[0xCD] = (Instruction) {"CMP abs", inst_CMP, ABS}; - instructions[0xCE] = (Instruction) {"DEC abs", inst_DEC, ABS}; - instructions[0xCF] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xD0] = (Instruction) {"BNE rel", inst_BNE, REL}; - instructions[0xD1] = (Instruction) {"CMP ind,Y", inst_CMP, INDY}; - instructions[0xD2] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xD3] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xD4] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xD5] = (Instruction) {"CMP zpg,X", inst_CMP, ZPX}; - instructions[0xD6] = (Instruction) {"DEC zpg,X", inst_DEC, ZPX}; - instructions[0xD7] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xD8] = (Instruction) {"CLD impl", inst_CLD, IMPL}; - instructions[0xD9] = (Instruction) {"CMP abs,Y", inst_CMP, ABSY}; - instructions[0xDA] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xDB] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xDC] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xDD] = (Instruction) {"CMP abs,X", inst_CMP, ABSX}; - instructions[0xDE] = (Instruction) {"DEC abs,X", inst_DEC, ABSX}; - instructions[0xDF] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xE0] = (Instruction) {"CPX #", inst_CPX, IMM}; - instructions[0xE1] = (Instruction) {"SBC X,ind", inst_SBC, XIND}; - instructions[0xE2] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xE3] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xE4] = (Instruction) {"CPX zpg", inst_CPX, ZP}; - instructions[0xE5] = (Instruction) {"SBC zpg", inst_SBC, ZP}; - instructions[0xE6] = (Instruction) {"INC zpg", inst_INC, ZP}; - instructions[0xE7] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xE8] = (Instruction) {"INX impl", inst_INX, IMPL}; - instructions[0xE9] = (Instruction) {"SBC #", inst_SBC, IMM}; - instructions[0xEA] = (Instruction) {"NOP impl", inst_NOP, IMPL}; - instructions[0xEB] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xEC] = (Instruction) {"CPX abs", inst_CPX, ABS}; - instructions[0xED] = (Instruction) {"SBC abs", inst_SBC, ABS}; - instructions[0xEE] = (Instruction) {"INC abs", inst_INC, ABS}; - instructions[0xEF] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xF0] = (Instruction) {"BEQ rel", inst_BEQ, REL}; - instructions[0xF1] = (Instruction) {"SBC ind,Y", inst_SBC, INDY}; - instructions[0xF2] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xF3] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xF4] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xF5] = (Instruction) {"SBC zpg,X", inst_SBC, ZPX}; - instructions[0xF6] = (Instruction) {"INC zpg,X", inst_INC, ZPX}; - instructions[0xF7] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xF8] = (Instruction) {"SED impl", inst_SED, IMPL}; - instructions[0xF9] = (Instruction) {"SBC abs,Y", inst_SBC, ABSY}; - instructions[0xFA] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xFB] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xFC] = (Instruction) {"???", inst_NOP, IMPL}; - instructions[0xFD] = (Instruction) {"SBC abs,X", inst_SBC, ABSX}; - instructions[0xFE] = (Instruction) {"INC abs,X", inst_INC, ABSX}; - instructions[0xFF] = (Instruction) {"???", inst_NOP, IMPL}; + instructions[0x00] = (Instruction) {"BRK impl", inst_BRK, IMPL, 7,}; + instructions[0x01] = (Instruction) {"ORA X,ind", inst_ORA, XIND, 6}; + instructions[0x02] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x03] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x04] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x05] = (Instruction) {"ORA zpg", inst_ORA, ZP, 5}; + instructions[0x06] = (Instruction) {"ASL zpg", inst_ASL, ZP, 1}; + instructions[0x07] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x08] = (Instruction) {"PHP impl", inst_PHP, IMPL, 1}; + instructions[0x09] = (Instruction) {"ORA #", inst_ORA, IMM, 1}; + instructions[0x0A] = (Instruction) {"ASL A", inst_ASL, ACC, 1}; + instructions[0x0B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x0C] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x0D] = (Instruction) {"ORA abs", inst_ORA, ABS, 1}; + instructions[0x0E] = (Instruction) {"ASL abs", inst_ASL, ABS, 1}; + instructions[0x0F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x10] = (Instruction) {"BPL rel", inst_BPL, REL, 1}; + instructions[0x11] = (Instruction) {"ORA ind,Y", inst_ORA, INDY, 1}; + instructions[0x12] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x13] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x14] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x15] = (Instruction) {"ORA zpg,X", inst_ORA, ZPX, 1}; + instructions[0x16] = (Instruction) {"ASL zpg,X", inst_ASL, ZPX, 1}; + instructions[0x17] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x18] = (Instruction) {"CLC impl", inst_CLC, IMPL, 1}; + instructions[0x19] = (Instruction) {"ORA abs,Y", inst_ORA, ABSY, 1}; + instructions[0x1A] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x1B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x1C] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x1D] = (Instruction) {"ORA abs,X", inst_ORA, ABSX, 1}; + instructions[0x1E] = (Instruction) {"ASL abs,X", inst_ASL, ABSX, 1}; + instructions[0x1F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x20] = (Instruction) {"JSR abs", inst_JSR, ABS, 1}; + instructions[0x21] = (Instruction) {"AND X,ind", inst_AND, XIND, 1}; + instructions[0x22] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x23] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x24] = (Instruction) {"BIT zpg", inst_BIT, ZP, 1}; + instructions[0x25] = (Instruction) {"AND zpg", inst_AND, ZP, 1}; + instructions[0x26] = (Instruction) {"ROL zpg", inst_ROL, ZP, 1}; + instructions[0x27] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x28] = (Instruction) {"PLP impl", inst_PLP, IMPL, 1}; + instructions[0x29] = (Instruction) {"AND #", inst_AND, IMM, 1}; + instructions[0x2A] = (Instruction) {"ROL A", inst_ROL, ACC, 1}; + instructions[0x2B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x2C] = (Instruction) {"BIT abs", inst_BIT, ABS, 1}; + instructions[0x2D] = (Instruction) {"AND abs", inst_AND, ABS, 1}; + instructions[0x2E] = (Instruction) {"ROL abs", inst_ROL, ABS, 1}; + instructions[0x2F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x30] = (Instruction) {"BMI rel", inst_BMI, REL, 1}; + instructions[0x31] = (Instruction) {"AND ind,Y", inst_AND, INDY, 1}; + instructions[0x32] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x33] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x34] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x35] = (Instruction) {"AND zpg,X", inst_AND, ZPX, 1}; + instructions[0x36] = (Instruction) {"ROL zpg,X", inst_ROL, ZPX, 1}; + instructions[0x37] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x38] = (Instruction) {"SEC impl", inst_SEC, IMPL, 1}; + instructions[0x39] = (Instruction) {"AND abs,Y", inst_AND, ABSY, 1}; + instructions[0x3A] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x3B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x3C] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x3D] = (Instruction) {"AND abs,X", inst_AND, ABSX, 1}; + instructions[0x3E] = (Instruction) {"ROL abs,X", inst_ROL, ABSX, 1}; + instructions[0x3F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x40] = (Instruction) {"RTI impl", inst_RTI, IMPL, 1}; + instructions[0x41] = (Instruction) {"EOR X,ind", inst_EOR, XIND, 1}; + instructions[0x42] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x43] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x44] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x45] = (Instruction) {"EOR zpg", inst_EOR, ZP, 1}; + instructions[0x46] = (Instruction) {"LSR zpg", inst_LSR, ZP, 1}; + instructions[0x47] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x48] = (Instruction) {"PHA impl", inst_PHA, IMPL, 1}; + instructions[0x49] = (Instruction) {"EOR #", inst_EOR, IMM, 1}; + instructions[0x4A] = (Instruction) {"LSR A", inst_LSR, ACC, 1}; + instructions[0x4B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x4C] = (Instruction) {"JMP abs", inst_JMP, ABS, 1}; + instructions[0x4D] = (Instruction) {"EOR abs", inst_EOR, ABS, 1}; + instructions[0x4E] = (Instruction) {"LSR abs", inst_LSR, ABS, 1}; + instructions[0x4F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x50] = (Instruction) {"BVC rel", inst_BVC, REL, 1}; + instructions[0x51] = (Instruction) {"EOR ind,Y", inst_EOR, INDY, 1}; + instructions[0x52] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x53] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x54] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x55] = (Instruction) {"EOR zpg,X", inst_EOR, ZPX, 1}; + instructions[0x56] = (Instruction) {"LSR zpg,X", inst_LSR, ZPX, 1}; + instructions[0x57] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x58] = (Instruction) {"CLI impl", inst_CLI, IMPL, 1}; + instructions[0x59] = (Instruction) {"EOR abs,Y", inst_EOR, ABSY, 1}; + instructions[0x5A] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x5B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x5C] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x5D] = (Instruction) {"EOR abs,X", inst_EOR, ABSX, 1}; + instructions[0x5E] = (Instruction) {"LSR abs,X", inst_LSR, ABSX, 1}; + instructions[0x5F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x60] = (Instruction) {"RTS impl", inst_RTS, IMPL, 1}; + instructions[0x61] = (Instruction) {"ADC X,ind", inst_ADC, XIND, 1}; + instructions[0x62] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x63] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x64] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x65] = (Instruction) {"ADC zpg", inst_ADC, ZP, 1}; + instructions[0x66] = (Instruction) {"ROR zpg", inst_ROR, ZP, 1}; + instructions[0x67] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x68] = (Instruction) {"PLA impl", inst_PLA, IMPL, 1}; + instructions[0x69] = (Instruction) {"ADC #", inst_ADC, IMM, 1}; + instructions[0x6A] = (Instruction) {"ROR A", inst_ROR, ACC, 1}; + instructions[0x6B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x6C] = (Instruction) {"JMP ind", inst_JMP, IND, 1}; + instructions[0x6D] = (Instruction) {"ADC abs", inst_ADC, ABS, 1}; + instructions[0x6E] = (Instruction) {"ROR abs", inst_ROR, ABS, 1}; + instructions[0x6F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x70] = (Instruction) {"BVS rel", inst_BVS, REL, 1}; + instructions[0x71] = (Instruction) {"ADC ind,Y", inst_ADC, INDY, 1}; + instructions[0x72] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x73] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x74] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x75] = (Instruction) {"ADC zpg,X", inst_ADC, ZPX, 1}; + instructions[0x76] = (Instruction) {"ROR zpg,X", inst_ROR, ZPX, 1}; + instructions[0x77] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x78] = (Instruction) {"SEI impl", inst_SEI, IMPL, 1}; + instructions[0x79] = (Instruction) {"ADC abs,Y", inst_ADC, ABSY, 1}; + instructions[0x7A] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x7B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x7C] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x7D] = (Instruction) {"ADC abs,X", inst_ADC, ABSX, 1}; + instructions[0x7E] = (Instruction) {"ROR abs,X", inst_ROR, ABSX, 1}; + instructions[0x7F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x80] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x81] = (Instruction) {"STA X,ind", inst_STA, XIND, 1}; + instructions[0x82] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x83] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x84] = (Instruction) {"STY zpg", inst_STY, ZP, 1}; + instructions[0x85] = (Instruction) {"STA zpg", inst_STA, ZP, 1}; + instructions[0x86] = (Instruction) {"STX zpg", inst_STX, ZP, 1}; + instructions[0x87] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x88] = (Instruction) {"DEY impl", inst_DEY, IMPL, 1}; + instructions[0x89] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x8A] = (Instruction) {"TXA impl", inst_TXA, IMPL, 1}; + instructions[0x8B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x8C] = (Instruction) {"STY abs", inst_STY, ABS, 1}; + instructions[0x8D] = (Instruction) {"STA abs", inst_STA, ABS, 1}; + instructions[0x8E] = (Instruction) {"STX abs", inst_STX, ABS, 1}; + instructions[0x8F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x90] = (Instruction) {"BCC rel", inst_BCC, REL, 1}; + instructions[0x91] = (Instruction) {"STA ind,Y", inst_STA, INDY, 1}; + instructions[0x92] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x93] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x94] = (Instruction) {"STY zpg,X", inst_STY, ZPX, 1}; + instructions[0x95] = (Instruction) {"STA zpg,X", inst_STA, ZPX, 1}; + instructions[0x96] = (Instruction) {"STX zpg,Y", inst_STX, ZPY, 1}; + instructions[0x97] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x98] = (Instruction) {"TYA impl", inst_TYA, IMPL, 1}; + instructions[0x99] = (Instruction) {"STA abs,Y", inst_STA, ABSY, 1}; + instructions[0x9A] = (Instruction) {"TXS impl", inst_TXS, IMPL, 1}; + instructions[0x9B] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x9C] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x9D] = (Instruction) {"STA abs,X", inst_STA, ABSX, 1}; + instructions[0x9E] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0x9F] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xA0] = (Instruction) {"LDY #", inst_LDY, IMM, 1}; + instructions[0xA1] = (Instruction) {"LDA X,ind", inst_LDA, XIND, 1}; + instructions[0xA2] = (Instruction) {"LDX #", inst_LDX, IMM, 1}; + instructions[0xA3] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xA4] = (Instruction) {"LDY zpg", inst_LDY, ZP, 1}; + instructions[0xA5] = (Instruction) {"LDA zpg", inst_LDA, ZP, 1}; + instructions[0xA6] = (Instruction) {"LDX zpg", inst_LDX, ZP, 1}; + instructions[0xA7] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xA8] = (Instruction) {"TAY impl", inst_TAY, IMPL, 1}; + instructions[0xA9] = (Instruction) {"LDA #", inst_LDA, IMM, 1}; + instructions[0xAA] = (Instruction) {"TAX impl", inst_TAX, IMPL, 1}; + instructions[0xAB] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xAC] = (Instruction) {"LDY abs", inst_LDY, ABS, 1}; + instructions[0xAD] = (Instruction) {"LDA abs", inst_LDA, ABS, 1}; + instructions[0xAE] = (Instruction) {"LDX abs", inst_LDX, ABS, 1}; + instructions[0xAF] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xB0] = (Instruction) {"BCS rel", inst_BCS, REL, 1}; + instructions[0xB1] = (Instruction) {"LDA ind,Y", inst_LDA, INDY, 1}; + instructions[0xB2] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xB3] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xB4] = (Instruction) {"LDY zpg,X", inst_LDY, ZPX, 1}; + instructions[0xB5] = (Instruction) {"LDA zpg,X", inst_LDA, ZPX, 1}; + instructions[0xB6] = (Instruction) {"LDX zpg,Y", inst_LDX, ZPY, 1}; + instructions[0xB7] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xB8] = (Instruction) {"CLV impl", inst_CLV, IMPL, 1}; + instructions[0xB9] = (Instruction) {"LDA abs,Y", inst_LDA, ABSY, 1}; + instructions[0xBA] = (Instruction) {"TSX impl", inst_TSX, IMPL, 1}; + instructions[0xBB] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xBC] = (Instruction) {"LDY abs,X", inst_LDY, ABSX, 1}; + instructions[0xBD] = (Instruction) {"LDA abs,X", inst_LDA, ABSX, 1}; + instructions[0xBE] = (Instruction) {"LDX abs,Y", inst_LDX, ABSY, 1}; + instructions[0xBF] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xC0] = (Instruction) {"CPY #", inst_CPY, IMM, 1}; + instructions[0xC1] = (Instruction) {"CMP X,ind", inst_CMP, XIND, 1}; + instructions[0xC2] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xC3] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xC4] = (Instruction) {"CPY zpg", inst_CPY, ZP, 1}; + instructions[0xC5] = (Instruction) {"CMP zpg", inst_CMP, ZP, 1}; + instructions[0xC6] = (Instruction) {"DEC zpg", inst_DEC, ZP, 1}; + instructions[0xC7] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xC8] = (Instruction) {"INY impl", inst_INY, IMPL, 1}; + instructions[0xC9] = (Instruction) {"CMP #", inst_CMP, IMM, 1}; + instructions[0xCA] = (Instruction) {"DEX impl", inst_DEX, IMPL, 1}; + instructions[0xCB] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xCC] = (Instruction) {"CPY abs", inst_CPY, ABS, 1}; + instructions[0xCD] = (Instruction) {"CMP abs", inst_CMP, ABS, 1}; + instructions[0xCE] = (Instruction) {"DEC abs", inst_DEC, ABS, 1}; + instructions[0xCF] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xD0] = (Instruction) {"BNE rel", inst_BNE, REL, 1}; + instructions[0xD1] = (Instruction) {"CMP ind,Y", inst_CMP, INDY, 1}; + instructions[0xD2] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xD3] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xD4] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xD5] = (Instruction) {"CMP zpg,X", inst_CMP, ZPX, 1}; + instructions[0xD6] = (Instruction) {"DEC zpg,X", inst_DEC, ZPX, 1}; + instructions[0xD7] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xD8] = (Instruction) {"CLD impl", inst_CLD, IMPL, 1}; + instructions[0xD9] = (Instruction) {"CMP abs,Y", inst_CMP, ABSY, 1}; + instructions[0xDA] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xDB] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xDC] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xDD] = (Instruction) {"CMP abs,X", inst_CMP, ABSX, 1}; + instructions[0xDE] = (Instruction) {"DEC abs,X", inst_DEC, ABSX, 1}; + instructions[0xDF] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xE0] = (Instruction) {"CPX #", inst_CPX, IMM, 1}; + instructions[0xE1] = (Instruction) {"SBC X,ind", inst_SBC, XIND, 1}; + instructions[0xE2] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xE3] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xE4] = (Instruction) {"CPX zpg", inst_CPX, ZP, 1}; + instructions[0xE5] = (Instruction) {"SBC zpg", inst_SBC, ZP, 1}; + instructions[0xE6] = (Instruction) {"INC zpg", inst_INC, ZP, 1}; + instructions[0xE7] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xE8] = (Instruction) {"INX impl", inst_INX, IMPL, 1}; + instructions[0xE9] = (Instruction) {"SBC #", inst_SBC, IMM, 1}; + instructions[0xEA] = (Instruction) {"NOP impl", inst_NOP, IMPL, 1}; + instructions[0xEB] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xEC] = (Instruction) {"CPX abs", inst_CPX, ABS, 1}; + instructions[0xED] = (Instruction) {"SBC abs", inst_SBC, ABS, 1}; + instructions[0xEE] = (Instruction) {"INC abs", inst_INC, ABS, 1}; + instructions[0xEF] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xF0] = (Instruction) {"BEQ rel", inst_BEQ, REL, 1}; + instructions[0xF1] = (Instruction) {"SBC ind,Y", inst_SBC, INDY, 1}; + instructions[0xF2] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xF3] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xF4] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xF5] = (Instruction) {"SBC zpg,X", inst_SBC, ZPX, 1}; + instructions[0xF6] = (Instruction) {"INC zpg,X", inst_INC, ZPX, 1}; + instructions[0xF7] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xF8] = (Instruction) {"SED impl", inst_SED, IMPL, 1}; + instructions[0xF9] = (Instruction) {"SBC abs,Y", inst_SBC, ABSY, 1}; + instructions[0xFA] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xFB] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xFC] = (Instruction) {"???", inst_NOP, IMPL, 1}; + instructions[0xFD] = (Instruction) {"SBC abs,X", inst_SBC, ABSX, 1}; + instructions[0xFE] = (Instruction) {"INC abs,X", inst_INC, ABSX, 1}; + instructions[0xFF] = (Instruction) {"???", inst_NOP, IMPL, 1}; } void reset_cpu() @@ -866,7 +866,7 @@ int load_rom(char * filename) return 0; } -void step_cpu() +int step_cpu() // returns cycle count { inst = instructions[memory[PC]]; @@ -884,4 +884,6 @@ void step_cpu() jumping = 0; inst.function(); if (jumping == 0) PC += lengths[inst.mode]; + + return inst.cycles; }