2017-01-17 13:29:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "bus.h"
|
2017-01-18 16:18:19 +00:00
|
|
|
#include "rk65c02.h"
|
2017-01-17 13:29:20 +00:00
|
|
|
#include "65c02isa.h"
|
|
|
|
#include "instruction.h"
|
|
|
|
|
|
|
|
instruction_t
|
|
|
|
instruction_fetch(bus_t *b, uint16_t addr)
|
|
|
|
{
|
|
|
|
instruction_t i;
|
2017-01-18 16:18:19 +00:00
|
|
|
instrdef_t id;
|
2017-01-17 13:29:20 +00:00
|
|
|
|
2017-01-18 16:18:19 +00:00
|
|
|
i.opcode = bus_read_1(b, addr);
|
|
|
|
id = instruction_decode(i.opcode);
|
2017-01-17 13:29:20 +00:00
|
|
|
|
2017-01-18 13:37:24 +00:00
|
|
|
//assert(i.def.opcode != OP_UNIMPL);
|
2017-01-17 13:29:20 +00:00
|
|
|
|
|
|
|
/* handle operands */
|
2017-01-18 16:18:19 +00:00
|
|
|
switch (id.mode) {
|
2017-01-17 13:29:20 +00:00
|
|
|
case IMMEDIATE:
|
|
|
|
case ZP:
|
|
|
|
case ZPX:
|
|
|
|
case ZPY:
|
|
|
|
case IZP:
|
|
|
|
case IZPX:
|
|
|
|
case IZPY:
|
|
|
|
case RELATIVE:
|
|
|
|
i.op1 = bus_read_1(b, addr+1);
|
|
|
|
break;
|
|
|
|
case ABSOLUTE:
|
|
|
|
case ABSOLUTEX:
|
|
|
|
case ABSOLUTEY:
|
|
|
|
case IABSOLUTE:
|
|
|
|
case IABSOLUTEX:
|
|
|
|
i.op1 = bus_read_1(b, addr+1);
|
|
|
|
i.op2 = bus_read_1(b, addr+2);
|
|
|
|
break;
|
|
|
|
case IMPLIED:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
instruction_print(instruction_t *i)
|
|
|
|
{
|
2017-01-18 16:18:19 +00:00
|
|
|
instrdef_t id;
|
|
|
|
|
|
|
|
id = instruction_decode(i->opcode);
|
|
|
|
switch (id.mode) {
|
2017-01-17 13:29:20 +00:00
|
|
|
case IMPLIED:
|
2017-01-18 16:18:19 +00:00
|
|
|
printf("%s", id.mnemonic);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
2017-01-18 10:12:37 +00:00
|
|
|
case ACCUMULATOR:
|
2017-01-18 16:18:19 +00:00
|
|
|
printf("%s A", id.mnemonic);
|
2017-01-18 10:12:37 +00:00
|
|
|
break;
|
2017-01-17 13:29:20 +00:00
|
|
|
case IMMEDIATE:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s #%#02x", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case ZP:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s %#02x", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case ZPX:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s %#02x,X", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case ZPY:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s %#02x,Y", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case IZP:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s (%#02x)", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case IZPX:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s (%#02x,X)", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case IZPY:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s (%#02x),Y", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
2017-01-23 11:17:06 +00:00
|
|
|
case ZPR:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s %#02x,%#02x", id.mnemonic, i->op1, i->op2);
|
2017-01-23 11:17:06 +00:00
|
|
|
break;
|
2017-01-17 13:29:20 +00:00
|
|
|
case ABSOLUTE:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s %#02x%02x", id.mnemonic, i->op2, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case ABSOLUTEX:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s %#02x%02x,X", id.mnemonic, i->op2, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case ABSOLUTEY:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s %#02x%02x,Y", id.mnemonic, i->op2, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case IABSOLUTE:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s (%#02x%02x)", id.mnemonic, i->op2, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case IABSOLUTEX:
|
2017-01-29 12:08:32 +00:00
|
|
|
printf("%s (%#02x%02x,X)", id.mnemonic, i->op2, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
case RELATIVE:
|
2017-01-29 12:55:53 +00:00
|
|
|
printf("%s %#02x", id.mnemonic, i->op1);
|
2017-01-17 13:29:20 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
disassemble(bus_t *b, uint16_t addr)
|
|
|
|
{
|
|
|
|
instruction_t i;
|
2017-01-18 16:18:19 +00:00
|
|
|
instrdef_t id;
|
2017-01-17 13:29:20 +00:00
|
|
|
|
|
|
|
i = instruction_fetch(b, addr);
|
2017-01-18 16:18:19 +00:00
|
|
|
id = instruction_decode(i.opcode);
|
2017-01-17 13:29:20 +00:00
|
|
|
|
|
|
|
printf("%X:\t", addr);
|
|
|
|
instruction_print(&i);
|
2017-01-20 08:46:33 +00:00
|
|
|
printf("\t\t// ");
|
|
|
|
|
|
|
|
if (id.size == 1)
|
|
|
|
printf("%X", id.opcode);
|
|
|
|
else if (id.size == 2)
|
|
|
|
printf("%X %X", id.opcode, i.op1);
|
|
|
|
else if (id.size == 3)
|
|
|
|
printf("%X %X %X", id.opcode, i.op1, i.op2);
|
2017-01-17 13:29:20 +00:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-01-18 11:09:14 +00:00
|
|
|
instrdef_t
|
2017-01-18 16:18:19 +00:00
|
|
|
instruction_decode(uint8_t opcode)
|
2017-01-18 11:09:14 +00:00
|
|
|
{
|
|
|
|
instrdef_t id;
|
|
|
|
|
|
|
|
id = instrs[opcode];
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-01-20 09:25:19 +00:00
|
|
|
void
|
|
|
|
instruction_status_adjust_zero(rk65c02emu_t *e, uint8_t regval)
|
|
|
|
{
|
|
|
|
if (regval == 0)
|
|
|
|
e->regs.P |= P_ZERO;
|
|
|
|
else
|
|
|
|
e->regs.P &= ~P_ZERO;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
instruction_status_adjust_negative(rk65c02emu_t *e, uint8_t regval)
|
|
|
|
{
|
|
|
|
if (regval & NEGATIVE)
|
|
|
|
e->regs.P |= P_NEGATIVE;
|
|
|
|
else
|
|
|
|
e->regs.P &= ~P_NEGATIVE;
|
|
|
|
}
|
|
|
|
|
2017-01-21 20:43:31 +00:00
|
|
|
void
|
|
|
|
instruction_data_write_1(rk65c02emu_t *e, instrdef_t *id, instruction_t *i, uint8_t val)
|
|
|
|
{
|
|
|
|
uint16_t iaddr;
|
|
|
|
|
|
|
|
switch (id->mode) {
|
|
|
|
case ZP:
|
|
|
|
bus_write_1(e->bus, i->op1, val);
|
|
|
|
break;
|
|
|
|
case ZPX:
|
|
|
|
/* XXX: wraps around zero page? */
|
|
|
|
bus_write_1(e->bus, i->op1 + e->regs.X, val);
|
|
|
|
break;
|
|
|
|
case ZPY:
|
|
|
|
bus_write_1(e->bus, i->op1 + e->regs.Y, val);
|
|
|
|
break;
|
|
|
|
case IZP:
|
|
|
|
iaddr = bus_read_1(e->bus, i->op1);
|
|
|
|
iaddr |= (bus_read_1(e->bus, i->op1 + 1) << 8);
|
|
|
|
bus_write_1(e->bus, iaddr, val);
|
|
|
|
break;
|
2017-01-22 23:00:45 +00:00
|
|
|
case ABSOLUTE:
|
|
|
|
bus_write_1(e->bus, i->op1 + (i->op2 << 8), val);
|
|
|
|
break;
|
2017-01-21 20:43:31 +00:00
|
|
|
case IZPX:
|
2017-01-23 09:48:37 +00:00
|
|
|
/* XXX */
|
2017-01-23 09:29:19 +00:00
|
|
|
iaddr = bus_read_1(e->bus, i->op1 + e->regs.X);
|
|
|
|
iaddr |= (bus_read_1(e->bus, i->op1 + e->regs.X + 1) << 8);
|
|
|
|
bus_write_1(e->bus, iaddr, val);
|
2017-01-24 21:18:02 +00:00
|
|
|
break;
|
2017-01-21 20:43:31 +00:00
|
|
|
case IZPY:
|
2017-01-23 09:48:37 +00:00
|
|
|
/* XXX */
|
2017-01-24 15:37:10 +00:00
|
|
|
iaddr = bus_read_1(e->bus, i->op1);
|
|
|
|
iaddr |= (bus_read_1(e->bus, i->op1 + 1) << 8);
|
|
|
|
bus_write_1(e->bus, iaddr, val + e->regs.Y);
|
2017-01-24 21:18:02 +00:00
|
|
|
break;
|
2017-01-21 20:43:31 +00:00
|
|
|
case ABSOLUTEX:
|
2017-01-23 09:48:37 +00:00
|
|
|
bus_write_1(e->bus, (i->op1 + (i->op2 << 8)) + e->regs.X, val);
|
|
|
|
break;
|
2017-01-21 20:43:31 +00:00
|
|
|
case ABSOLUTEY:
|
2017-01-23 09:48:37 +00:00
|
|
|
bus_write_1(e->bus, (i->op1 + (i->op2 << 8)) + e->regs.Y, val);
|
|
|
|
break;
|
2017-01-27 10:03:50 +00:00
|
|
|
case ACCUMULATOR:
|
|
|
|
e->regs.A = val;
|
|
|
|
break;
|
2017-01-23 11:17:06 +00:00
|
|
|
case ZPR:
|
2017-01-30 20:24:45 +00:00
|
|
|
/*
|
|
|
|
* This mode is special as both operands have separate meaning.
|
|
|
|
* Handled withing emulation, as it is used only by BBS and BBR.
|
|
|
|
*/
|
|
|
|
assert(false);
|
2017-01-27 10:03:50 +00:00
|
|
|
break;
|
2017-01-24 21:18:02 +00:00
|
|
|
case IMMEDIATE:
|
2017-01-23 09:48:37 +00:00
|
|
|
case RELATIVE:
|
2017-01-21 20:43:31 +00:00
|
|
|
case IABSOLUTE:
|
|
|
|
case IABSOLUTEX:
|
2017-01-23 09:48:37 +00:00
|
|
|
/*
|
|
|
|
* IABSOLUTE, IABSOLUTEX, RELATIVE are only for branches
|
|
|
|
* and jumps. They do not read or write anything, only modify
|
|
|
|
* PC which is handled within emulation of a given opcode.
|
|
|
|
*/
|
2017-01-21 20:43:31 +00:00
|
|
|
default:
|
|
|
|
printf("unhandled addressing mode for opcode %x\n",
|
|
|
|
i->opcode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 09:59:35 +00:00
|
|
|
uint8_t
|
|
|
|
instruction_data_read_1(rk65c02emu_t *e, instrdef_t *id, instruction_t *i)
|
|
|
|
{
|
|
|
|
uint8_t rv; /* data read from the bus */
|
|
|
|
uint16_t iaddr; /* indirect address */
|
|
|
|
|
|
|
|
rv = 0;
|
|
|
|
|
|
|
|
switch (id->mode) {
|
|
|
|
case ACCUMULATOR:
|
|
|
|
rv = e->regs.A;
|
|
|
|
break;
|
|
|
|
case IMMEDIATE:
|
|
|
|
rv = i->op1;
|
|
|
|
break;
|
|
|
|
case ZP:
|
|
|
|
rv = bus_read_1(e->bus, i->op1);
|
|
|
|
break;
|
|
|
|
case ZPX:
|
|
|
|
/* XXX: wraps around zero page? */
|
|
|
|
rv = bus_read_1(e->bus, i->op1 + e->regs.X);
|
|
|
|
break;
|
|
|
|
case ZPY:
|
|
|
|
rv = bus_read_1(e->bus, i->op1 + e->regs.Y);
|
|
|
|
break;
|
|
|
|
case IZP:
|
|
|
|
iaddr = bus_read_1(e->bus, i->op1);
|
|
|
|
iaddr |= (bus_read_1(e->bus, i->op1 + 1) << 8);
|
|
|
|
rv = bus_read_1(e->bus, iaddr);
|
|
|
|
break;
|
|
|
|
case IZPX:
|
2017-01-23 09:29:19 +00:00
|
|
|
/* XXX: what about page wraps / roll over */
|
|
|
|
iaddr = bus_read_1(e->bus, i->op1 + e->regs.X);
|
|
|
|
iaddr |= (bus_read_1(e->bus, i->op1 + e->regs.X + 1) << 8);
|
|
|
|
rv = bus_read_1(e->bus, iaddr);
|
2017-01-22 23:00:45 +00:00
|
|
|
break;
|
2017-01-19 09:59:35 +00:00
|
|
|
case IZPY:
|
2017-01-23 09:29:19 +00:00
|
|
|
/* XXX: what about page wraps / roll over */
|
2017-01-24 15:37:10 +00:00
|
|
|
iaddr = bus_read_1(e->bus, i->op1);
|
|
|
|
iaddr |= (bus_read_1(e->bus, i->op1 + 1) << 8);
|
|
|
|
rv = bus_read_1(e->bus, iaddr) + e->regs.Y;
|
2017-01-22 23:00:45 +00:00
|
|
|
break;
|
2017-01-19 09:59:35 +00:00
|
|
|
case ABSOLUTE:
|
2017-01-22 23:00:45 +00:00
|
|
|
rv = bus_read_1(e->bus, i->op1 + (i->op2 << 8));
|
|
|
|
break;
|
2017-01-19 09:59:35 +00:00
|
|
|
case ABSOLUTEX:
|
2017-01-23 09:48:37 +00:00
|
|
|
rv = bus_read_1(e->bus, (i->op1 + (i->op2 << 8)) + e->regs.X);
|
|
|
|
break;
|
2017-01-19 09:59:35 +00:00
|
|
|
case ABSOLUTEY:
|
2017-01-23 09:48:37 +00:00
|
|
|
rv = bus_read_1(e->bus, (i->op1 + (i->op2 << 8)) + e->regs.Y);
|
|
|
|
break;
|
2017-01-23 11:17:06 +00:00
|
|
|
case ZPR:
|
2017-01-30 20:24:45 +00:00
|
|
|
/*
|
|
|
|
* This mode is special as both operands have separate meaning.
|
|
|
|
* Handled withing emulation, as it is used only by BBS and BBR.
|
|
|
|
*/
|
|
|
|
assert(false);
|
|
|
|
break;
|
2017-01-19 09:59:35 +00:00
|
|
|
case IABSOLUTE:
|
|
|
|
case IABSOLUTEX:
|
2017-01-22 23:00:45 +00:00
|
|
|
case RELATIVE:
|
2017-01-23 09:48:37 +00:00
|
|
|
/*
|
|
|
|
* IABSOLUTE, IABSOLUTEX, RELATIVE are only for branches
|
|
|
|
* and jumps. They do not read or write anything, only modify
|
|
|
|
* PC which is handled within emulation of a given opcode.
|
|
|
|
*/
|
2017-01-19 09:59:35 +00:00
|
|
|
default:
|
|
|
|
printf("unhandled addressing mode for opcode %x\n",
|
|
|
|
i->opcode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-01-20 21:03:03 +00:00
|
|
|
/* put value onto the stack */
|
|
|
|
void
|
|
|
|
stack_push(rk65c02emu_t *e, uint8_t val)
|
|
|
|
{
|
|
|
|
bus_write_1(e->bus, STACK_START+e->regs.SP, val);
|
|
|
|
e->regs.SP--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pull/pop value from the stack */
|
|
|
|
uint8_t
|
|
|
|
stack_pop(rk65c02emu_t *e)
|
|
|
|
{
|
|
|
|
uint8_t val;
|
|
|
|
|
|
|
|
e->regs.SP++;
|
2017-01-20 22:16:02 +00:00
|
|
|
val = bus_read_1(e->bus, STACK_START+e->regs.SP);
|
2017-01-20 21:03:03 +00:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-01-22 10:07:19 +00:00
|
|
|
/* increment program counter based on instruction size (opcode + operands) */
|
|
|
|
void
|
|
|
|
program_counter_increment(rk65c02emu_t *e, instrdef_t *id)
|
|
|
|
{
|
|
|
|
e->regs.PC += id->size;
|
|
|
|
}
|
|
|
|
|
2017-01-29 12:29:17 +00:00
|
|
|
void
|
|
|
|
program_counter_branch(rk65c02emu_t *e, int8_t boffset)
|
|
|
|
{
|
|
|
|
e->regs.PC += boffset + 2;
|
|
|
|
}
|
|
|
|
|
2017-01-27 19:43:08 +00:00
|
|
|
/* check whether given instruction modify program counter */
|
|
|
|
bool
|
|
|
|
instruction_modify_pc(instrdef_t *id)
|
|
|
|
{
|
|
|
|
return id->modify_pc;
|
|
|
|
}
|
|
|
|
|