all opcodes decoded

This commit is contained in:
Thiago Auler 2017-11-12 14:02:30 -02:00
parent 61e4896613
commit d942e682ae
3 changed files with 339 additions and 64 deletions

View File

@ -1,9 +1,10 @@
#include <stdio.h>
#include "inc/types.h" #include "inc/types.h"
#include "inc/memory.h" #include "inc/memory.h"
#include "inc/opcodes.h" #include "inc/opcodes.h"
oc1 opcode_decoded_1;
oc2 opcode_decoded_2;
void init() void init()
{ {
@ -38,11 +39,128 @@ void decode()
opcode_decoded_1 = aaacc; opcode_decoded_1 = aaacc;
opcode_decoded_2 = ir; 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() 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: case BRK:
return brk(); return brk();
@ -112,6 +230,8 @@ void execute()
return dex(); return dex();
case NOP: case NOP:
return nop(); return nop();
case XXX:
return xxx();
} }
switch (opcode_decoded_1) switch (opcode_decoded_1)
@ -162,15 +282,22 @@ void execute()
return dec(); return dec();
case INC: case INC:
return inc(); return inc();
case XXX:
return xxx();
} }
} }
void run() void run()
{ {
while (pc != 0) //while (pc != 0)
{ {
fetch(); //fetch();
decode(); for (int i = 0x00; i <= 0xFF; i++)
execute(); {
ir = i;
decode();
execute();
}
} }
} }

View File

@ -76,12 +76,34 @@ enum opcodes_comp
TAX = 0xAA, TAX = 0xAA,
TSX = 0xBA, TSX = 0xBA,
DEX = 0xCA, 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 oc1;
typedef enum opcodes_comp oc2; 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 adc(); // add memory to accumalator with carry
void and(); // and memory with accumulator void and(); // and memory with accumulator
void asl(); // shift left one bit (memory on accumulator) void asl(); // shift left one bit (memory on accumulator)

View File

@ -2,338 +2,464 @@
#include "inc/opcodes.h" #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() void adc()
{ {
// add memory to accumalator with carry // add memory to accumalator with carry
printf("ADC\n"); printf("%02X:[ADC", ir);
decode_addressing_mode();
} }
void and() void and()
{ {
// and memory with accumulator // and memory with accumulator
printf("AND\n"); printf("%02X:[AND", ir);
decode_addressing_mode();
} }
void asl() void asl()
{ {
// shift left one bit (memory on accumulator) // shift left one bit (memory on accumulator)
printf("ASL\n"); printf("%02X:[ASL", ir);
decode_addressing_mode();
} }
void bcc() void bcc()
{ {
// branch on carry clear // branch on carry clear
printf("BCC\n"); printf("%02X:[BCC", ir);
decode_addressing_mode();
} }
void bcs() void bcs()
{ {
// branch on carry set // branch on carry set
printf("BSC\n"); printf("%02X:[BSC", ir);
decode_addressing_mode();
} }
void beq() void beq()
{ {
// branch on result zero // branch on result zero
printf("BEQ\n"); printf("%02X:[BEQ", ir);
decode_addressing_mode();
} }
void bit() void bit()
{ {
// test bits in memory with accumulator // test bits in memory with accumulator
printf("BIT\n"); printf("%02X:[BIT", ir);
decode_addressing_mode();
} }
void bmi() void bmi()
{ {
// branch on result minus // branch on result minus
printf("BMI\n"); printf("%02X:[BMI", ir);
decode_addressing_mode();
} }
void bne() void bne()
{ {
// branch on result not zero // branch on result not zero
printf("BNE\n"); printf("%02X:[BNE", ir);
decode_addressing_mode();
} }
void bpl() void bpl()
{ {
// branch on result plus // branch on result plus
printf("BPL\n"); printf("%02X:[BPL", ir);
decode_addressing_mode();
} }
void brk() void brk()
{ {
// force break // force break
printf("BRK\n"); printf("%02X:[BRK", ir);
decode_addressing_mode();
} }
void bvc() void bvc()
{ {
// branch on overflow clear // branch on overflow clear
printf("BVC\n"); printf("%02X:[BVC", ir);
decode_addressing_mode();
} }
void bvs() void bvs()
{ {
// branch on overflow set // branch on overflow set
printf("BVS\n"); printf("%02X:[BVS", ir);
decode_addressing_mode();
} }
void clc() void clc()
{ {
// clear carry flag // clear carry flag
printf("CLC\n"); printf("%02X:[CLC", ir);
decode_addressing_mode();
} }
void cld() void cld()
{ {
// clear decimal mode // clear decimal mode
printf("CLD\n"); printf("%02X:[CLD", ir);
decode_addressing_mode();
} }
void cli() void cli()
{ {
// clear interrupt disable bit // clear interrupt disable bit
printf("CLI\n"); printf("%02X:[CLI", ir);
decode_addressing_mode();
} }
void clv() void clv()
{ {
// clear overflow flag // clear overflow flag
printf("CLV\n"); printf("%02X:[CLV", ir);
decode_addressing_mode();
} }
void cmp() void cmp()
{ {
// compare memory with accumulator // compare memory with accumulator
printf("CMP\n"); printf("%02X:[CMP", ir);
decode_addressing_mode();
} }
void cpx() void cpx()
{ {
// compare memory and index x // compare memory and index x
printf("CPX\n"); printf("%02X:[CPX", ir);
decode_addressing_mode();
} }
void cpy() void cpy()
{ {
// compare memory and index y // compare memory and index y
printf("CPY\n"); printf("%02X:[CPY", ir);
decode_addressing_mode();
} }
void dec() void dec()
{ {
// decrement memory by one // decrement memory by one
printf("DEC\n"); printf("%02X:[DEC", ir);
decode_addressing_mode();
} }
void dex() void dex()
{ {
// decrement index x by one // decrement index x by one
printf("DEX\n"); printf("%02X:[DEX", ir);
decode_addressing_mode();
} }
void dey() void dey()
{ {
// decrement index y by one // decrement index y by one
printf("DEY\n"); printf("%02X:[DEY", ir);
decode_addressing_mode();
} }
void eor() void eor()
{ {
// exclusive-or memory with accumulator // exclusive-or memory with accumulator
printf("EOR\n"); printf("%02X:[EOR", ir);
decode_addressing_mode();
} }
void inc() void inc()
{ {
// increment memory by one // increment memory by one
printf("INC\n"); printf("%02X:[INC", ir);
decode_addressing_mode();
} }
void inx() void inx()
{ {
// increment index x by one // increment index x by one
printf("INX\n"); printf("%02X:[INX", ir);
decode_addressing_mode();
} }
void iny() void iny()
{ {
// increment index y by one // increment index y by one
printf("INY\n"); printf("%02X:[INY", ir);
decode_addressing_mode();
} }
void jmp() void jmp()
{ {
// jump to new location // jump to new location
printf("JMP\n"); printf("%02X:[JMP", ir);
decode_addressing_mode();
} }
void jsr() void jsr()
{ {
// jump to new location saving return address // jump to new location saving return address
printf("JSR\n"); printf("%02X:[JSR", ir);
decode_addressing_mode();
} }
void lda() void lda()
{ {
// load accumulator with memory // load accumulator with memory
printf("LDA\n"); printf("%02X:[LDA", ir);
decode_addressing_mode();
} }
void ldx() void ldx()
{ {
// load index x with memory // load index x with memory
printf("LDX\n"); printf("%02X:[LDX", ir);
decode_addressing_mode();
} }
void ldy() void ldy()
{ {
// load index y with memory // load index y with memory
printf("LDY\n"); printf("%02X:[LDY", ir);
decode_addressing_mode();
} }
void lsr() void lsr()
{ {
// shift one bit right (memory or accumulator) // shift one bit right (memory or accumulator)
printf("LSR\n"); printf("%02X:[LSR", ir);
decode_addressing_mode();
} }
void nop() void nop()
{ {
// no operation // no operation
printf("NOP\n"); printf("%02X:[NOP", ir);
decode_addressing_mode();
} }
void ora() void ora()
{ {
// or memory with accumulator // or memory with accumulator
printf("ORA\n"); printf("%02X:[ORA", ir);
decode_addressing_mode();
} }
void pha() void pha()
{ {
// push accumulator on stack // push accumulator on stack
printf("PHA\n"); printf("%02X:[PHA", ir);
decode_addressing_mode();
} }
void php() void php()
{ {
// push processor status on stack // push processor status on stack
printf("PHP\n"); printf("%02X:[PHP", ir);
decode_addressing_mode();
} }
void pla() void pla()
{ {
// pull accumulator from stack // pull accumulator from stack
printf("PLA\n"); printf("%02X:[PLA", ir);
decode_addressing_mode();
} }
void plp() void plp()
{ {
// pull processor status from stack // pull processor status from stack
printf("PLP\n"); printf("%02X:[PLP", ir);
decode_addressing_mode();
} }
void rol() void rol()
{ {
// rotate on bit left (memory or accumulator) // rotate on bit left (memory or accumulator)
printf("ROL\n"); printf("%02X:[ROL", ir);
decode_addressing_mode();
} }
void ror() void ror()
{ {
// rotate on bit right (memory or accumulator) // rotate on bit right (memory or accumulator)
printf("ROR\n"); printf("%02X:[ROR", ir);
decode_addressing_mode();
} }
void rti() void rti()
{ {
// return from interrupt // return from interrupt
printf("RTI\n"); printf("%02X:[RTI", ir);
decode_addressing_mode();
} }
void rts() void rts()
{ {
// retrun from subroutine // retrun from subroutine
printf("RTS\n"); printf("%02X:[RTS", ir);
decode_addressing_mode();
} }
void sbc() void sbc()
{ {
// subtract memory from accumulator with borrow // subtract memory from accumulator with borrow
printf("SBC\n"); printf("%02X:[SBC", ir);
decode_addressing_mode();
} }
void sec() void sec()
{ {
// set carry flag // set carry flag
printf("SEC\n"); printf("%02X:[SEC", ir);
decode_addressing_mode();
} }
void sed() void sed()
{ {
// set decimal flag // set decimal flag
printf("SED\n"); printf("%02X:[SED", ir);
decode_addressing_mode();
} }
void sei() void sei()
{ {
// set interrupt disable status // set interrupt disable status
printf("SEI\n"); printf("%02X:[SEI", ir);
decode_addressing_mode();
} }
void sta() void sta()
{ {
// store accumulator in memory // store accumulator in memory
printf("STA\n"); printf("%02X:[STA", ir);
decode_addressing_mode();
} }
void stx() void stx()
{ {
// store index x in memory // store index x in memory
printf("STX\n"); printf("%02X:[STX", ir);
decode_addressing_mode();
} }
void sty() void sty()
{ {
// store index y in memory // store index y in memory
printf("STY\n"); printf("%02X:[STY", ir);
decode_addressing_mode();
} }
void tax() void tax()
{ {
// transfer accumulator to index x // transfer accumulator to index x
printf("TAX\n"); printf("%02X:[TAX", ir);
decode_addressing_mode();
} }
void tay() void tay()
{ {
// transfer accumulator to index y // transfer accumulator to index y
printf("TAY\n"); printf("%02X:[TAY", ir);
decode_addressing_mode();
} }
void tsx() void tsx()
{ {
// transfer stack pointer to index x // transfer stack pointer to index x
printf("TSX\n"); printf("%02X:[TSX", ir);
decode_addressing_mode();
} }
void txa() void txa()
{ {
// transfer index x to accumulator // transfer index x to accumulator
printf("TXA\n"); printf("%02X:[TXA", ir);
decode_addressing_mode();
} }
void txs() void txs()
{ {
// transfer index x to stack pointer // transfer index x to stack pointer
printf("TXS\n"); printf("%02X:[TXS", ir);
decode_addressing_mode();
} }
void tya() void tya()
{ {
// transfer index y to accumulator // transfer index y to accumulator
printf("TYA\n"); printf("%02X:[TYA", ir);
decode_addressing_mode();
} }