2017-11-12 02:31:55 +00:00
|
|
|
#include "inc/opcodes.h"
|
2017-11-13 00:27:51 +00:00
|
|
|
#include "inc/memory.h"
|
2017-11-12 02:31:55 +00:00
|
|
|
|
2017-11-13 19:39:10 +00:00
|
|
|
dw address;
|
|
|
|
db operand;
|
2017-11-13 00:27:51 +00:00
|
|
|
|
2017-11-13 19:39:10 +00:00
|
|
|
void fetch_operand()
|
|
|
|
{
|
2017-11-13 03:02:14 +00:00
|
|
|
switch (address_mode)
|
2017-11-12 16:02:30 +00:00
|
|
|
{
|
2017-11-13 00:27:51 +00:00
|
|
|
case immediate:
|
2017-11-13 19:39:10 +00:00
|
|
|
address = pc;
|
|
|
|
operand = read_byte(address);
|
2017-11-13 00:27:51 +00:00
|
|
|
pc = pc + 1;
|
2017-11-12 16:02:30 +00:00
|
|
|
break;
|
2017-11-13 00:27:51 +00:00
|
|
|
case zero_page:
|
2017-11-13 19:39:10 +00:00
|
|
|
address = read_byte(pc);
|
|
|
|
operand = read_byte(address);
|
2017-11-13 00:27:51 +00:00
|
|
|
pc = pc + 1;
|
2017-11-12 16:02:30 +00:00
|
|
|
break;
|
2017-11-13 00:27:51 +00:00
|
|
|
case zero_page_x:
|
2017-11-13 19:39:10 +00:00
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + x;
|
|
|
|
operand = read_byte(address);
|
2017-11-13 00:27:51 +00:00
|
|
|
pc = pc + 1;
|
2017-11-12 16:02:30 +00:00
|
|
|
break;
|
2017-11-13 00:27:51 +00:00
|
|
|
case zero_page_y:
|
2017-11-13 19:39:10 +00:00
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + y;
|
|
|
|
operand = read_byte(address);
|
2017-11-13 00:27:51 +00:00
|
|
|
pc = pc + 1;
|
2017-11-12 16:02:30 +00:00
|
|
|
break;
|
2017-11-13 00:27:51 +00:00
|
|
|
case accumulator:
|
2017-11-13 19:39:10 +00:00
|
|
|
address = pc;
|
2017-11-13 00:27:51 +00:00
|
|
|
operand = ac;
|
2017-11-12 16:02:30 +00:00
|
|
|
break;
|
2017-11-13 19:39:10 +00:00
|
|
|
case absolute:
|
2017-11-13 19:40:03 +00:00
|
|
|
address = read_word(pc);
|
2017-11-13 19:39:10 +00:00
|
|
|
operand = read_byte(address);
|
|
|
|
pc = pc + 2;
|
|
|
|
case absolute_x:
|
|
|
|
address = read_word(pc);
|
|
|
|
address = address + x;
|
|
|
|
operand = read_byte(address);
|
|
|
|
pc = pc + 2;
|
|
|
|
break;
|
|
|
|
case absolute_y:
|
|
|
|
address = read_word(pc);
|
|
|
|
address = address + y;
|
|
|
|
operand = read_byte(address);
|
|
|
|
pc = pc + 2;
|
|
|
|
break;
|
|
|
|
case indirect_x:
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + x;
|
|
|
|
address = read_word(address);
|
|
|
|
operand = read_byte(address);
|
|
|
|
pc = pc + 1;
|
|
|
|
break;
|
|
|
|
case indirect_y:
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = read_word(address);
|
|
|
|
address = address + y;
|
|
|
|
operand = read_byte(address);
|
|
|
|
pc = pc + 1;
|
|
|
|
break;
|
2017-11-12 16:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 17:54:56 +00:00
|
|
|
void adjustNZ(db a)
|
|
|
|
{
|
|
|
|
if (a == 0) { Z_SET; } else { Z_UNSET;}
|
|
|
|
a = a >> 7;
|
|
|
|
if (a == 0) { N_SET; } else { N_UNSET; }
|
|
|
|
}
|
|
|
|
|
|
|
|
db adder(db a, db b)
|
|
|
|
{
|
|
|
|
db r = a + b;
|
|
|
|
|
|
|
|
// todo: adjust carry flag
|
|
|
|
// todo: adjust overflow flag
|
|
|
|
adjustNZ(r);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
db subtractor(db a, db b)
|
|
|
|
{
|
|
|
|
db r = a - b;
|
|
|
|
|
|
|
|
// todo: adjust carry flag
|
|
|
|
// todo: adjust overflow flag
|
|
|
|
adjustNZ(r);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-11-12 02:31:55 +00:00
|
|
|
void adc()
|
|
|
|
{
|
|
|
|
// add memory to accumalator with carry
|
2017-11-13 23:13:20 +00:00
|
|
|
fetch_operand();
|
2017-11-14 18:06:51 +00:00
|
|
|
ac = adder(ac, operand + C_IS_SET);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void and()
|
|
|
|
{
|
|
|
|
// and memory with accumulator
|
2017-11-13 23:13:20 +00:00
|
|
|
fetch_operand();
|
|
|
|
ac = ac & operand;
|
2017-11-14 18:06:51 +00:00
|
|
|
adjustNZ(ac);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void asl()
|
|
|
|
{
|
2017-11-13 23:13:20 +00:00
|
|
|
// shift left one bit (memory or accumulator)
|
|
|
|
fetch_operand();
|
|
|
|
operand = operand << 1;
|
2017-11-14 18:06:51 +00:00
|
|
|
adjustNZ(operand);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bcc()
|
|
|
|
{
|
|
|
|
// branch on carry clear
|
2017-11-13 23:13:20 +00:00
|
|
|
if(!C_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bcs()
|
|
|
|
{
|
|
|
|
// branch on carry set
|
2017-11-13 23:13:20 +00:00
|
|
|
if(C_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void beq()
|
|
|
|
{
|
|
|
|
// branch on result zero
|
2017-11-13 23:13:20 +00:00
|
|
|
if(Z_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bit()
|
|
|
|
{
|
|
|
|
// test bits in memory with accumulator
|
|
|
|
}
|
|
|
|
|
|
|
|
void bmi()
|
|
|
|
{
|
|
|
|
// branch on result minus
|
2017-11-13 23:13:20 +00:00
|
|
|
if(N_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bne()
|
|
|
|
{
|
|
|
|
// branch on result not zero
|
2017-11-13 23:13:20 +00:00
|
|
|
if(!Z_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bpl()
|
|
|
|
{
|
|
|
|
// branch on result plus
|
2017-11-13 23:13:20 +00:00
|
|
|
if(!N_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void brk()
|
|
|
|
{
|
|
|
|
// force break
|
|
|
|
}
|
|
|
|
|
|
|
|
void bvc()
|
|
|
|
{
|
|
|
|
// branch on overflow clear
|
2017-11-13 23:13:20 +00:00
|
|
|
if(!V_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void bvs()
|
|
|
|
{
|
|
|
|
// branch on overflow set
|
2017-11-13 23:13:20 +00:00
|
|
|
if(V_IS_SET)
|
|
|
|
{
|
|
|
|
address = read_byte(pc);
|
|
|
|
address = address + pc;
|
|
|
|
pc = address;
|
|
|
|
}
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clc()
|
|
|
|
{
|
|
|
|
// clear carry flag
|
2017-11-13 23:13:20 +00:00
|
|
|
C_UNSET;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cld()
|
|
|
|
{
|
|
|
|
// clear decimal mode
|
2017-11-13 23:13:20 +00:00
|
|
|
D_UNSET;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cli()
|
|
|
|
{
|
|
|
|
// clear interrupt disable bit
|
2017-11-13 23:13:20 +00:00
|
|
|
I_UNSET;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clv()
|
|
|
|
{
|
|
|
|
// clear overflow flag
|
2017-11-13 23:13:20 +00:00
|
|
|
V_UNSET;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmp()
|
|
|
|
{
|
|
|
|
// compare memory with accumulator
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpx()
|
|
|
|
{
|
|
|
|
// compare memory and index x
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpy()
|
|
|
|
{
|
|
|
|
// compare memory and index y
|
|
|
|
}
|
|
|
|
|
|
|
|
void dec()
|
|
|
|
{
|
|
|
|
// decrement memory by one
|
2017-11-14 18:06:51 +00:00
|
|
|
operand = operand - 1;
|
|
|
|
write_mem(address, operand);
|
|
|
|
adjustNZ(operand);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dex()
|
|
|
|
{
|
|
|
|
// decrement index x by one
|
2017-11-14 18:06:51 +00:00
|
|
|
x = x - 1;
|
|
|
|
adjustNZ(x);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dey()
|
|
|
|
{
|
|
|
|
// decrement index y by one
|
2017-11-14 18:06:51 +00:00
|
|
|
y = y - 1;
|
|
|
|
adjustNZ(y);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void eor()
|
|
|
|
{
|
|
|
|
// exclusive-or memory with accumulator
|
|
|
|
}
|
|
|
|
|
|
|
|
void inc()
|
|
|
|
{
|
|
|
|
// increment memory by one
|
2017-11-13 23:13:20 +00:00
|
|
|
operand = operand + 1;
|
|
|
|
write_mem(address, operand);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void inx()
|
|
|
|
{
|
|
|
|
// increment index x by one
|
2017-11-13 23:13:20 +00:00
|
|
|
x = x + 1;
|
2017-11-14 18:06:51 +00:00
|
|
|
adjustNZ(x);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void iny()
|
|
|
|
{
|
|
|
|
// increment index y by one
|
2017-11-13 23:13:20 +00:00
|
|
|
y = y + 1;
|
2017-11-14 18:06:51 +00:00
|
|
|
adjustNZ(y);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void jmp()
|
|
|
|
{
|
2017-11-13 23:13:20 +00:00
|
|
|
// jump to new location (indirect)
|
|
|
|
address = read_word(pc);
|
|
|
|
address = read_word(address);
|
|
|
|
pc = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
void jpa()
|
|
|
|
{
|
|
|
|
// jump to new location (absolute)
|
|
|
|
address = read_word(pc);
|
|
|
|
pc = address;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void jsr()
|
|
|
|
{
|
|
|
|
// jump to new location saving return address
|
|
|
|
}
|
|
|
|
|
|
|
|
void lda()
|
|
|
|
{
|
|
|
|
// load accumulator with memory
|
2017-11-14 18:06:51 +00:00
|
|
|
ac = operand;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ldx()
|
|
|
|
{
|
|
|
|
// load index x with memory
|
2017-11-14 18:06:51 +00:00
|
|
|
x = operand;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ldy()
|
|
|
|
{
|
|
|
|
// load index y with memory
|
2017-11-14 18:06:51 +00:00
|
|
|
y = operand;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void lsr()
|
|
|
|
{
|
|
|
|
// shift one bit right (memory or accumulator)
|
|
|
|
}
|
|
|
|
|
|
|
|
void nop()
|
|
|
|
{
|
|
|
|
// no operation
|
|
|
|
}
|
|
|
|
|
|
|
|
void ora()
|
|
|
|
{
|
|
|
|
// or memory with accumulator
|
|
|
|
}
|
|
|
|
|
|
|
|
void pha()
|
|
|
|
{
|
|
|
|
// push accumulator on stack
|
|
|
|
}
|
|
|
|
|
|
|
|
void php()
|
|
|
|
{
|
|
|
|
// push processor status on stack
|
|
|
|
}
|
|
|
|
|
|
|
|
void pla()
|
|
|
|
{
|
|
|
|
// pull accumulator from stack
|
|
|
|
}
|
|
|
|
|
|
|
|
void plp()
|
|
|
|
{
|
|
|
|
// pull processor status from stack
|
|
|
|
}
|
|
|
|
|
|
|
|
void rol()
|
|
|
|
{
|
|
|
|
// rotate on bit left (memory or accumulator)
|
|
|
|
}
|
|
|
|
|
|
|
|
void ror()
|
|
|
|
{
|
|
|
|
// rotate on bit right (memory or accumulator)
|
|
|
|
}
|
|
|
|
|
|
|
|
void rti()
|
|
|
|
{
|
|
|
|
// return from interrupt
|
|
|
|
}
|
|
|
|
|
|
|
|
void rts()
|
|
|
|
{
|
|
|
|
// retrun from subroutine
|
|
|
|
}
|
|
|
|
|
|
|
|
void sbc()
|
|
|
|
{
|
|
|
|
// subtract memory from accumulator with borrow
|
|
|
|
}
|
|
|
|
|
|
|
|
void sec()
|
|
|
|
{
|
|
|
|
// set carry flag
|
2017-11-14 18:06:51 +00:00
|
|
|
C_SET;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sed()
|
|
|
|
{
|
|
|
|
// set decimal flag
|
2017-11-14 18:06:51 +00:00
|
|
|
D_SET;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sei()
|
|
|
|
{
|
|
|
|
// set interrupt disable status
|
2017-11-14 18:06:51 +00:00
|
|
|
I_SET;
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sta()
|
|
|
|
{
|
|
|
|
// store accumulator in memory
|
2017-11-14 18:06:51 +00:00
|
|
|
write_mem(address, ac);
|
|
|
|
adjustNZ(y);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void stx()
|
|
|
|
{
|
|
|
|
// store index x in memory
|
2017-11-14 18:06:51 +00:00
|
|
|
write_mem(address, x);
|
|
|
|
adjustNZ(x);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void sty()
|
|
|
|
{
|
|
|
|
// store index y in memory
|
2017-11-14 18:06:51 +00:00
|
|
|
write_mem(address, y);
|
|
|
|
adjustNZ(y);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tax()
|
|
|
|
{
|
|
|
|
// transfer accumulator to index x
|
2017-11-14 18:06:51 +00:00
|
|
|
x = ac;
|
|
|
|
adjustNZ(x);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tay()
|
|
|
|
{
|
|
|
|
// transfer accumulator to index y
|
2017-11-14 18:06:51 +00:00
|
|
|
y = ac;
|
|
|
|
adjustNZ(y);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tsx()
|
|
|
|
{
|
|
|
|
// transfer stack pointer to index x
|
2017-11-14 18:06:51 +00:00
|
|
|
x = sp;
|
|
|
|
adjustNZ(x);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void txa()
|
|
|
|
{
|
|
|
|
// transfer index x to accumulator
|
2017-11-14 18:06:51 +00:00
|
|
|
ac = x;
|
|
|
|
adjustNZ(x);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void txs()
|
|
|
|
{
|
|
|
|
// transfer index x to stack pointer
|
2017-11-14 18:06:51 +00:00
|
|
|
sp = x;
|
|
|
|
adjustNZ(x);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tya()
|
|
|
|
{
|
|
|
|
// transfer index y to accumulator
|
2017-11-14 18:06:51 +00:00
|
|
|
ac = y;
|
|
|
|
adjustNZ(y);
|
2017-11-12 02:31:55 +00:00
|
|
|
}
|