diff --git a/src/6502.c b/src/6502.c index 211d5a2..061d8fb 100644 --- a/src/6502.c +++ b/src/6502.c @@ -1,9 +1,10 @@ +#include + #include "inc/types.h" #include "inc/memory.h" #include "inc/opcodes.h" -oc1 opcode_decoded_1; -oc2 opcode_decoded_2; + void init() { @@ -38,11 +39,128 @@ void decode() opcode_decoded_1 = aaacc; opcode_decoded_2 = ir; + + addressing_mode = bbb; + + if (cc == 0b01) + { + // correct the addressing mode for '01' opcodetype + if (bbb == 0b000) + { + addressing_mode = indirect_x; + } + if (bbb == 0b010) + { + addressing_mode = immediate; + } + } + + if (cc == 0b10) + { + // adjust the addressing mode for STX and LDX + if ((opcode_decoded_1 == STX || opcode_decoded_1 == LDX) && addressing_mode == zero_page_x) + { + addressing_mode = zero_page_y; + } + if (opcode_decoded_1 == LDX && addressing_mode == absolute_x) + { + addressing_mode = absolute_y; + } + } + + // disable all incorrect opcodes + if (((ir & 0x0F) == 0x02 && ir != 0xA2) || + (ir & 0x0F) == 0x03 || (ir & 0x0F) == 0x07 || + (ir & 0x0F) == 0x0B || (ir & 0x0F) == 0x0F) + { + opcode_decoded_1 = XXX; + opcode_decoded_2 = XXX; + } + + if (ir == 0x04 || ir == 0x0C || ir == 0x14 || ir == 0x1A || ir == 0x1C || ir == 0x34 || ir == 0x3A || + ir == 0x3C || ir == 0x44 || ir == 0x54 || ir == 0x5A || ir == 0x5C || ir == 0x64 || ir == 0x74 || + ir == 0x7A || ir == 0x7C || ir == 0x80 || ir == 0x89 || ir == 0x9C || ir == 0x9E || + ir == 0xD4 || ir == 0xDA || ir == 0xDC || ir == 0xF4 || ir == 0xFA || ir == 0xFC) + { + opcode_decoded_1 = XXX; + opcode_decoded_2 = XXX; + } } void execute() { - switch(opcode_decoded_2) + /*switch (opcode_decoded_2) + { + case JSR: + printf("abs "); + pc++; + pc++; + break; + case BPL: case BMI: case BVC: case BVS: + case BCC: case BCS: case BNE: case BEQ: + printf("rel "); + pc++; + break; + case TXA: case TXS: case TAX: case TSX: case DEX: + case NOP: case CLC: case SEC: case CLI: case SEI: + case TYA: case CLV: case CLD: case SED: case PHP: + case PLP: case PHA: case PLA: case DEY: case TAY: + case INY: case INX: case BRK: case RTI: case RTS: + printf("imp "); + break; + default: + switch (addressing_mode) + { + case immediate: + printf("imm "); + pc++; + break; + case zero_page: + printf("zpg "); + pc++; + break; + case accumulator: + printf("acc "); + break; + case absolute: + printf("abs "); + pc++; + pc++; + break; + case indirect_y: + printf("iny "); + pc++; + break; + case zero_page_x: + printf("zpx "); + pc++; + break; + case absolute_y: + printf("aby "); + pc++; + pc++; + break; + case absolute_x: + printf("abx "); + pc++; + pc++; + break; + case indirect_x: + printf("inx "); + pc++; + break; + case zero_page_y: + printf("zpy "); + pc++; + break; + default: + + break; + } + break; + }*/ + + switch (opcode_decoded_2) { case BRK: return brk(); @@ -112,6 +230,8 @@ void execute() return dex(); case NOP: return nop(); + case XXX: + return xxx(); } switch (opcode_decoded_1) @@ -162,15 +282,22 @@ void execute() return dec(); case INC: return inc(); + case XXX: + return xxx(); } } void run() { - while (pc != 0) + //while (pc != 0) { - fetch(); - decode(); - execute(); + //fetch(); + for (int i = 0x00; i <= 0xFF; i++) + { + ir = i; + decode(); + execute(); + } } + } diff --git a/src/inc/opcodes.h b/src/inc/opcodes.h index c2252ea..c2296b0 100644 --- a/src/inc/opcodes.h +++ b/src/inc/opcodes.h @@ -76,12 +76,34 @@ enum opcodes_comp TAX = 0xAA, TSX = 0xBA, DEX = 0xCA, - NOP = 0xEA + NOP = 0xEA, + + XXX = 0xFF +}; + +enum address_mode +{ + immediate = 0b000, + zero_page = 0b001, + accumulator = 0b010, + absolute = 0b011, + indirect_y = 0b100, + zero_page_x = 0b101, + absolute_y = 0b110, + absolute_x = 0b111, + indirect_x, + zero_page_y }; typedef enum opcodes oc1; typedef enum opcodes_comp oc2; +typedef enum address_mode am; +oc1 opcode_decoded_1; +oc2 opcode_decoded_2; +am addressing_mode; + +void xxx(); // invalid opcode void adc(); // add memory to accumalator with carry void and(); // and memory with accumulator void asl(); // shift left one bit (memory on accumulator) diff --git a/src/opcodes.c b/src/opcodes.c index 4e01603..3ca717c 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -2,338 +2,464 @@ #include "inc/opcodes.h" +int zz = 0; + +void decode_addressing_mode() +{ + switch (opcode_decoded_2) + { + case XXX: + printf("/---] "); + break; + case JSR: + printf("/abs] "); + break; + case BPL: case BMI: case BVC: case BVS: + case BCC: case BCS: case BNE: case BEQ: + printf("/rel] "); + break; + case TXA: case TXS: case TAX: case TSX: case DEX: + case NOP: case CLC: case SEC: case CLI: case SEI: + case TYA: case CLV: case CLD: case SED: case PHP: + case PLP: case PHA: case PLA: case DEY: case TAY: + case INY: case INX: case BRK: case RTI: case RTS: + printf("/imp] "); + break; + default: + switch (addressing_mode) + { + case immediate: + printf("/imm] "); + break; + case zero_page: + printf("/zpg] "); + break; + case accumulator: + printf("/acc] "); + break; + case absolute: + printf("/abs] "); + break; + case indirect_y: + printf("/iny] "); + break; + case zero_page_x: + printf("/zpx] "); + break; + case absolute_y: + printf("/aby] "); + break; + case absolute_x: + printf("/abx] "); + break; + case indirect_x: + printf("/inx] "); + break; + case zero_page_y: + printf("/zpy] "); + break; + } + break; + } + + if (++zz % 8 == 0) { printf("\n"); } +} + +void xxx() +{ + // invalid opcode + printf("%02X:[---", ir); + decode_addressing_mode(); +} + void adc() { // add memory to accumalator with carry - printf("ADC\n"); + printf("%02X:[ADC", ir); + decode_addressing_mode(); } void and() { // and memory with accumulator - printf("AND\n"); + printf("%02X:[AND", ir); + decode_addressing_mode(); } void asl() { // shift left one bit (memory on accumulator) - printf("ASL\n"); + printf("%02X:[ASL", ir); + decode_addressing_mode(); } void bcc() { // branch on carry clear - printf("BCC\n"); + printf("%02X:[BCC", ir); + decode_addressing_mode(); } void bcs() { // branch on carry set - printf("BSC\n"); + printf("%02X:[BSC", ir); + decode_addressing_mode(); } void beq() { // branch on result zero - printf("BEQ\n"); + printf("%02X:[BEQ", ir); + decode_addressing_mode(); } void bit() { // test bits in memory with accumulator - printf("BIT\n"); + printf("%02X:[BIT", ir); + decode_addressing_mode(); } void bmi() { // branch on result minus - printf("BMI\n"); + printf("%02X:[BMI", ir); + decode_addressing_mode(); } void bne() { // branch on result not zero - printf("BNE\n"); + printf("%02X:[BNE", ir); + decode_addressing_mode(); } void bpl() { // branch on result plus - printf("BPL\n"); + printf("%02X:[BPL", ir); + decode_addressing_mode(); } void brk() { // force break - printf("BRK\n"); + printf("%02X:[BRK", ir); + decode_addressing_mode(); } void bvc() { // branch on overflow clear - printf("BVC\n"); + printf("%02X:[BVC", ir); + decode_addressing_mode(); } void bvs() { // branch on overflow set - printf("BVS\n"); + printf("%02X:[BVS", ir); + decode_addressing_mode(); } void clc() { // clear carry flag - printf("CLC\n"); + printf("%02X:[CLC", ir); + decode_addressing_mode(); } void cld() { // clear decimal mode - printf("CLD\n"); + printf("%02X:[CLD", ir); + decode_addressing_mode(); } void cli() { // clear interrupt disable bit - printf("CLI\n"); + printf("%02X:[CLI", ir); + decode_addressing_mode(); } void clv() { // clear overflow flag - printf("CLV\n"); + printf("%02X:[CLV", ir); + decode_addressing_mode(); } void cmp() { // compare memory with accumulator - printf("CMP\n"); + printf("%02X:[CMP", ir); + decode_addressing_mode(); } void cpx() { // compare memory and index x - printf("CPX\n"); + printf("%02X:[CPX", ir); + decode_addressing_mode(); } void cpy() { // compare memory and index y - printf("CPY\n"); + printf("%02X:[CPY", ir); + decode_addressing_mode(); } void dec() { // decrement memory by one - printf("DEC\n"); + printf("%02X:[DEC", ir); + decode_addressing_mode(); } void dex() { // decrement index x by one - printf("DEX\n"); + printf("%02X:[DEX", ir); + decode_addressing_mode(); } void dey() { // decrement index y by one - printf("DEY\n"); + printf("%02X:[DEY", ir); + decode_addressing_mode(); } void eor() { // exclusive-or memory with accumulator - printf("EOR\n"); + printf("%02X:[EOR", ir); + decode_addressing_mode(); } void inc() { // increment memory by one - printf("INC\n"); + printf("%02X:[INC", ir); + decode_addressing_mode(); } void inx() { // increment index x by one - printf("INX\n"); + printf("%02X:[INX", ir); + decode_addressing_mode(); } void iny() { // increment index y by one - printf("INY\n"); + printf("%02X:[INY", ir); + decode_addressing_mode(); } void jmp() { // jump to new location - printf("JMP\n"); + printf("%02X:[JMP", ir); + decode_addressing_mode(); } void jsr() { // jump to new location saving return address - printf("JSR\n"); + printf("%02X:[JSR", ir); + decode_addressing_mode(); } void lda() { // load accumulator with memory - printf("LDA\n"); + printf("%02X:[LDA", ir); + decode_addressing_mode(); } void ldx() { // load index x with memory - printf("LDX\n"); + printf("%02X:[LDX", ir); + decode_addressing_mode(); } void ldy() { // load index y with memory - printf("LDY\n"); + printf("%02X:[LDY", ir); + decode_addressing_mode(); } void lsr() { // shift one bit right (memory or accumulator) - printf("LSR\n"); + printf("%02X:[LSR", ir); + decode_addressing_mode(); } void nop() { // no operation - printf("NOP\n"); + printf("%02X:[NOP", ir); + decode_addressing_mode(); } void ora() { // or memory with accumulator - printf("ORA\n"); + printf("%02X:[ORA", ir); + decode_addressing_mode(); } void pha() { // push accumulator on stack - printf("PHA\n"); + printf("%02X:[PHA", ir); + decode_addressing_mode(); } void php() { // push processor status on stack - printf("PHP\n"); + printf("%02X:[PHP", ir); + decode_addressing_mode(); } void pla() { // pull accumulator from stack - printf("PLA\n"); + printf("%02X:[PLA", ir); + decode_addressing_mode(); } void plp() { // pull processor status from stack - printf("PLP\n"); + printf("%02X:[PLP", ir); + decode_addressing_mode(); } void rol() { // rotate on bit left (memory or accumulator) - printf("ROL\n"); + printf("%02X:[ROL", ir); + decode_addressing_mode(); } void ror() { // rotate on bit right (memory or accumulator) - printf("ROR\n"); + printf("%02X:[ROR", ir); + decode_addressing_mode(); } void rti() { // return from interrupt - printf("RTI\n"); + printf("%02X:[RTI", ir); + decode_addressing_mode(); } void rts() { // retrun from subroutine - printf("RTS\n"); + printf("%02X:[RTS", ir); + decode_addressing_mode(); } void sbc() { // subtract memory from accumulator with borrow - printf("SBC\n"); + printf("%02X:[SBC", ir); + decode_addressing_mode(); } void sec() { // set carry flag - printf("SEC\n"); + printf("%02X:[SEC", ir); + decode_addressing_mode(); } void sed() { // set decimal flag - printf("SED\n"); + printf("%02X:[SED", ir); + decode_addressing_mode(); } void sei() { // set interrupt disable status - printf("SEI\n"); + printf("%02X:[SEI", ir); + decode_addressing_mode(); } void sta() { // store accumulator in memory - printf("STA\n"); + printf("%02X:[STA", ir); + decode_addressing_mode(); } void stx() { // store index x in memory - printf("STX\n"); + printf("%02X:[STX", ir); + decode_addressing_mode(); } void sty() { // store index y in memory - printf("STY\n"); + printf("%02X:[STY", ir); + decode_addressing_mode(); } void tax() { // transfer accumulator to index x - printf("TAX\n"); + printf("%02X:[TAX", ir); + decode_addressing_mode(); } void tay() { // transfer accumulator to index y - printf("TAY\n"); + printf("%02X:[TAY", ir); + decode_addressing_mode(); } void tsx() { // transfer stack pointer to index x - printf("TSX\n"); + printf("%02X:[TSX", ir); + decode_addressing_mode(); } void txa() { // transfer index x to accumulator - printf("TXA\n"); + printf("%02X:[TXA", ir); + decode_addressing_mode(); } void txs() { // transfer index x to stack pointer - printf("TXS\n"); + printf("%02X:[TXS", ir); + decode_addressing_mode(); } void tya() { // transfer index y to accumulator - printf("TYA\n"); + printf("%02X:[TYA", ir); + decode_addressing_mode(); }