cleaning decode source code

This commit is contained in:
Thiago Auler 2017-11-12 22:27:51 -02:00
parent d942e682ae
commit 10f9063872
4 changed files with 64 additions and 248 deletions

BIN
doc/6502 opcodes tables.pdf Normal file

Binary file not shown.

View File

@ -4,7 +4,8 @@
#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()
{ {
@ -75,6 +76,7 @@ void decode()
{ {
opcode_decoded_1 = XXX; opcode_decoded_1 = XXX;
opcode_decoded_2 = XXX; opcode_decoded_2 = XXX;
addressing_mode = XXX;
} }
if (ir == 0x04 || ir == 0x0C || ir == 0x14 || ir == 0x1A || ir == 0x1C || ir == 0x34 || ir == 0x3A || if (ir == 0x04 || ir == 0x0C || ir == 0x14 || ir == 0x1A || ir == 0x1C || ir == 0x34 || ir == 0x3A ||
@ -84,82 +86,12 @@ void decode()
{ {
opcode_decoded_1 = XXX; opcode_decoded_1 = XXX;
opcode_decoded_2 = XXX; opcode_decoded_2 = XXX;
addressing_mode = XXX;
} }
} }
void execute() void execute()
{ {
/*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) switch (opcode_decoded_2)
{ {
case BRK: case BRK:
@ -289,15 +221,11 @@ void execute()
void run() void run()
{ {
//while (pc != 0) while (pc != 0)
{ {
//fetch(); fetch();
for (int i = 0x00; i <= 0xFF; i++)
{
ir = i;
decode(); decode();
execute(); execute();
} }
}
} }

View File

@ -99,8 +99,6 @@ typedef enum opcodes oc1;
typedef enum opcodes_comp oc2; typedef enum opcodes_comp oc2;
typedef enum address_mode am; typedef enum address_mode am;
oc1 opcode_decoded_1;
oc2 opcode_decoded_2;
am addressing_mode; am addressing_mode;
void xxx(); // invalid opcode void xxx(); // invalid opcode

View File

@ -1,465 +1,355 @@
#include <stdio.h> #include <stdio.h>
#include "inc/opcodes.h" #include "inc/opcodes.h"
#include "inc/memory.h"
int zz = 0; db fetch_operand(dw address)
void decode_addressing_mode()
{ {
switch (opcode_decoded_2) mem.mar = address;
{ mem_read();
case XXX: return mem.mdr;
printf("/---] "); }
break;
case JSR: dw fetch_address()
printf("/abs] "); {
break; dw word;
case BPL: case BMI: case BVC: case BVS: db low_byte;
case BCC: case BCS: case BNE: case BEQ: db high_byte;
printf("/rel] ");
break; low_byte = fetch_operand(pc);
case TXA: case TXS: case TAX: case TSX: case DEX: pc = pc + 1;
case NOP: case CLC: case SEC: case CLI: case SEI:
case TYA: case CLV: case CLD: case SED: case PHP: high_byte = fetch_operand(pc);
case PLP: case PHA: case PLA: case DEY: case TAY: pc = pc + 1;
case INY: case INX: case BRK: case RTI: case RTS:
printf("/imp] "); word = high_byte << 8; // shifts high byte to its place
break; word = word | low_byte; // appends low byte to form the complete word
default:
return word;
}
db decode_operand()
{
db operand;
db byte;
dw word;
switch (addressing_mode) switch (addressing_mode)
{ {
case immediate: case immediate:
printf("/imm] "); operand = fetch_operand(pc);
pc = pc + 1;
break; break;
case zero_page: case zero_page:
printf("/zpg] "); byte = fetch_operand(pc);
break; word = 0x0000 | byte;
case accumulator: operand = fetch_operand(word);
printf("/acc] "); pc = pc + 1;
break;
case absolute:
printf("/abs] ");
break;
case indirect_y:
printf("/iny] ");
break; break;
case zero_page_x: case zero_page_x:
printf("/zpx] "); byte = fetch_operand(pc);
break; word = 0x0000 | byte;
case absolute_y: word = word + x;
printf("/aby] "); operand = fetch_operand(word);
break; pc = pc + 1;
case absolute_x:
printf("/abx] ");
break;
case indirect_x:
printf("/inx] ");
break; break;
case zero_page_y: case zero_page_y:
printf("/zpy] "); byte = fetch_operand(pc);
word = 0x0000 | byte;
word = word + y;
operand = fetch_operand(word);
pc = pc + 1;
break; break;
} case accumulator:
operand = ac;
break; break;
} }
if (++zz % 8 == 0) { printf("\n"); } return operand;
} }
void xxx() void xxx()
{ {
// invalid opcode // invalid opcode
printf("%02X:[---", ir); return nop();
decode_addressing_mode();
} }
void adc() void adc()
{ {
// add memory to accumalator with carry // add memory to accumalator with carry
printf("%02X:[ADC", ir);
decode_addressing_mode();
} }
void and() void and()
{ {
// and memory with accumulator // and memory with accumulator
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("%02X:[ASL", ir);
decode_addressing_mode();
} }
void bcc() void bcc()
{ {
// branch on carry clear // branch on carry clear
printf("%02X:[BCC", ir);
decode_addressing_mode();
} }
void bcs() void bcs()
{ {
// branch on carry set // branch on carry set
printf("%02X:[BSC", ir);
decode_addressing_mode();
} }
void beq() void beq()
{ {
// branch on result zero // branch on result zero
printf("%02X:[BEQ", ir);
decode_addressing_mode();
} }
void bit() void bit()
{ {
// test bits in memory with accumulator // test bits in memory with accumulator
printf("%02X:[BIT", ir);
decode_addressing_mode();
} }
void bmi() void bmi()
{ {
// branch on result minus // branch on result minus
printf("%02X:[BMI", ir);
decode_addressing_mode();
} }
void bne() void bne()
{ {
// branch on result not zero // branch on result not zero
printf("%02X:[BNE", ir);
decode_addressing_mode();
} }
void bpl() void bpl()
{ {
// branch on result plus // branch on result plus
printf("%02X:[BPL", ir);
decode_addressing_mode();
} }
void brk() void brk()
{ {
// force break // force break
printf("%02X:[BRK", ir);
decode_addressing_mode();
} }
void bvc() void bvc()
{ {
// branch on overflow clear // branch on overflow clear
printf("%02X:[BVC", ir);
decode_addressing_mode();
} }
void bvs() void bvs()
{ {
// branch on overflow set // branch on overflow set
printf("%02X:[BVS", ir);
decode_addressing_mode();
} }
void clc() void clc()
{ {
// clear carry flag // clear carry flag
printf("%02X:[CLC", ir);
decode_addressing_mode();
} }
void cld() void cld()
{ {
// clear decimal mode // clear decimal mode
printf("%02X:[CLD", ir);
decode_addressing_mode();
} }
void cli() void cli()
{ {
// clear interrupt disable bit // clear interrupt disable bit
printf("%02X:[CLI", ir);
decode_addressing_mode();
} }
void clv() void clv()
{ {
// clear overflow flag // clear overflow flag
printf("%02X:[CLV", ir);
decode_addressing_mode();
} }
void cmp() void cmp()
{ {
// compare memory with accumulator // compare memory with accumulator
printf("%02X:[CMP", ir);
decode_addressing_mode();
} }
void cpx() void cpx()
{ {
// compare memory and index x // compare memory and index x
printf("%02X:[CPX", ir);
decode_addressing_mode();
} }
void cpy() void cpy()
{ {
// compare memory and index y // compare memory and index y
printf("%02X:[CPY", ir);
decode_addressing_mode();
} }
void dec() void dec()
{ {
// decrement memory by one // decrement memory by one
printf("%02X:[DEC", ir);
decode_addressing_mode();
} }
void dex() void dex()
{ {
// decrement index x by one // decrement index x by one
printf("%02X:[DEX", ir);
decode_addressing_mode();
} }
void dey() void dey()
{ {
// decrement index y by one // decrement index y by one
printf("%02X:[DEY", ir);
decode_addressing_mode();
} }
void eor() void eor()
{ {
// exclusive-or memory with accumulator // exclusive-or memory with accumulator
printf("%02X:[EOR", ir);
decode_addressing_mode();
} }
void inc() void inc()
{ {
// increment memory by one // increment memory by one
printf("%02X:[INC", ir);
decode_addressing_mode();
} }
void inx() void inx()
{ {
// increment index x by one // increment index x by one
printf("%02X:[INX", ir);
decode_addressing_mode();
} }
void iny() void iny()
{ {
// increment index y by one // increment index y by one
printf("%02X:[INY", ir);
decode_addressing_mode();
} }
void jmp() void jmp()
{ {
// jump to new location // jump to new location
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("%02X:[JSR", ir);
decode_addressing_mode();
} }
void lda() void lda()
{ {
// load accumulator with memory // load accumulator with memory
printf("%02X:[LDA", ir);
decode_addressing_mode();
} }
void ldx() void ldx()
{ {
// load index x with memory // load index x with memory
printf("%02X:[LDX", ir);
decode_addressing_mode();
} }
void ldy() void ldy()
{ {
// load index y with memory // load index y with memory
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("%02X:[LSR", ir);
decode_addressing_mode();
} }
void nop() void nop()
{ {
// no operation // no operation
printf("%02X:[NOP", ir);
decode_addressing_mode();
} }
void ora() void ora()
{ {
// or memory with accumulator // or memory with accumulator
printf("%02X:[ORA", ir);
decode_addressing_mode();
} }
void pha() void pha()
{ {
// push accumulator on stack // push accumulator on stack
printf("%02X:[PHA", ir);
decode_addressing_mode();
} }
void php() void php()
{ {
// push processor status on stack // push processor status on stack
printf("%02X:[PHP", ir);
decode_addressing_mode();
} }
void pla() void pla()
{ {
// pull accumulator from stack // pull accumulator from stack
printf("%02X:[PLA", ir);
decode_addressing_mode();
} }
void plp() void plp()
{ {
// pull processor status from stack // pull processor status from stack
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("%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("%02X:[ROR", ir);
decode_addressing_mode();
} }
void rti() void rti()
{ {
// return from interrupt // return from interrupt
printf("%02X:[RTI", ir);
decode_addressing_mode();
} }
void rts() void rts()
{ {
// retrun from subroutine // retrun from subroutine
printf("%02X:[RTS", ir);
decode_addressing_mode();
} }
void sbc() void sbc()
{ {
// subtract memory from accumulator with borrow // subtract memory from accumulator with borrow
printf("%02X:[SBC", ir);
decode_addressing_mode();
} }
void sec() void sec()
{ {
// set carry flag // set carry flag
printf("%02X:[SEC", ir);
decode_addressing_mode();
} }
void sed() void sed()
{ {
// set decimal flag // set decimal flag
printf("%02X:[SED", ir);
decode_addressing_mode();
} }
void sei() void sei()
{ {
// set interrupt disable status // set interrupt disable status
printf("%02X:[SEI", ir);
decode_addressing_mode();
} }
void sta() void sta()
{ {
// store accumulator in memory // store accumulator in memory
printf("%02X:[STA", ir);
decode_addressing_mode();
} }
void stx() void stx()
{ {
// store index x in memory // store index x in memory
printf("%02X:[STX", ir);
decode_addressing_mode();
} }
void sty() void sty()
{ {
// store index y in memory // store index y in memory
printf("%02X:[STY", ir);
decode_addressing_mode();
} }
void tax() void tax()
{ {
// transfer accumulator to index x // transfer accumulator to index x
printf("%02X:[TAX", ir);
decode_addressing_mode();
} }
void tay() void tay()
{ {
// transfer accumulator to index y // transfer accumulator to index y
printf("%02X:[TAY", ir);
decode_addressing_mode();
} }
void tsx() void tsx()
{ {
// transfer stack pointer to index x // transfer stack pointer to index x
printf("%02X:[TSX", ir);
decode_addressing_mode();
} }
void txa() void txa()
{ {
// transfer index x to accumulator // transfer index x to accumulator
printf("%02X:[TXA", ir);
decode_addressing_mode();
} }
void txs() void txs()
{ {
// transfer index x to stack pointer // transfer index x to stack pointer
printf("%02X:[TXS", ir);
decode_addressing_mode();
} }
void tya() void tya()
{ {
// transfer index y to accumulator // transfer index y to accumulator
printf("%02X:[TYA", ir);
decode_addressing_mode();
} }