2024-05-20 10:09:19 -04:00
|
|
|
#include <stdint.h>
|
2024-05-24 02:50:04 -04:00
|
|
|
#include <stdio.h>
|
2024-06-08 08:11:06 -04:00
|
|
|
#include <stdlib.h>
|
2024-05-24 03:25:44 -04:00
|
|
|
#include <string.h>
|
2024-05-24 02:50:04 -04:00
|
|
|
|
2024-06-08 08:11:06 -04:00
|
|
|
#define MAX(a, b) ((a > b) ? a : b)
|
|
|
|
|
2024-05-24 02:50:04 -04:00
|
|
|
#define STATUS_UPDATE_ZERO(r) \
|
|
|
|
(regs.status.zero = r == 0)
|
|
|
|
#define STATUS_UPDATE_NEGATIVE(r) \
|
|
|
|
(regs.status.negative = ((r & (1 << 7)) > 0))
|
2024-05-20 10:09:19 -04:00
|
|
|
|
2024-06-10 13:26:06 -04:00
|
|
|
#define PUSH(b) \
|
|
|
|
(memwrite(0x0100 + regs.sp--, b))
|
|
|
|
#define PULL() \
|
|
|
|
(peek(0x0100 + regs.sp++))
|
|
|
|
|
2024-05-20 10:09:19 -04:00
|
|
|
struct cpu_flags {
|
|
|
|
uint8_t carry : 1;
|
|
|
|
uint8_t zero : 1;
|
|
|
|
uint8_t interrupt_disable : 1;
|
|
|
|
uint8_t decimal_mode : 1;
|
|
|
|
uint8_t brk : 1;
|
|
|
|
uint8_t unused : 1;
|
|
|
|
uint8_t overflow : 1;
|
|
|
|
uint8_t negative : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct registers {
|
2024-05-24 02:50:04 -04:00
|
|
|
uint8_t a, x, y, sp;
|
2024-05-20 10:09:19 -04:00
|
|
|
struct cpu_flags status;
|
|
|
|
uint16_t pc;
|
|
|
|
};
|
|
|
|
struct registers regs;
|
|
|
|
|
|
|
|
enum addressing_mode {
|
2024-06-04 06:46:11 -04:00
|
|
|
AM_ACC,
|
|
|
|
AM_IMM,
|
|
|
|
AM_ZP,
|
|
|
|
AM_ZP_X,
|
|
|
|
AM_ZP_Y,
|
|
|
|
AM_REL,
|
|
|
|
AM_ABS,
|
|
|
|
AM_ABS_X,
|
|
|
|
AM_ABS_Y,
|
|
|
|
AM_IND,
|
|
|
|
AM_IND_X,
|
|
|
|
AM_IND_Y,
|
2024-05-20 10:09:19 -04:00
|
|
|
};
|
|
|
|
|
2024-06-04 06:24:09 -04:00
|
|
|
/* 64K address space, 16bit words */
|
|
|
|
uint8_t memory[0x16000];
|
2024-05-20 10:09:19 -04:00
|
|
|
|
|
|
|
/* example program taken from https://bugzmanov.github.io/nes_ebook/chapter_3_1.html */
|
|
|
|
uint8_t program[] = { 0xa9, 0xc0, 0xaa, 0xe8, 0x00 };
|
|
|
|
|
2024-06-09 03:26:13 -04:00
|
|
|
uint32_t cycles = 0;
|
|
|
|
|
2024-06-09 06:37:09 -04:00
|
|
|
static uint8_t
|
2024-06-08 08:11:06 -04:00
|
|
|
peek(uint16_t addr)
|
2024-06-04 06:24:09 -04:00
|
|
|
{
|
|
|
|
return memory[addr];
|
|
|
|
}
|
|
|
|
|
2024-06-09 06:37:09 -04:00
|
|
|
static uint16_t
|
2024-06-04 06:24:09 -04:00
|
|
|
peek16(uint16_t addr)
|
|
|
|
{
|
|
|
|
/* bytes are stored in little-endian (low then high) */
|
|
|
|
return (uint16_t)memory[addr] | ((uint16_t)memory[addr + 1] << 8);
|
|
|
|
}
|
|
|
|
|
2024-06-09 06:37:09 -04:00
|
|
|
static void
|
|
|
|
memwrite(uint16_t addr, uint8_t byte)
|
|
|
|
{
|
2024-06-09 12:45:21 -04:00
|
|
|
memory[addr] = byte;
|
2024-06-09 06:37:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
memwrite16(uint16_t addr, uint16_t word)
|
|
|
|
{
|
|
|
|
/* bytes are stored in little-endian (low then high) */
|
|
|
|
memory[addr] = word & 0xFF;
|
|
|
|
memory[addr + 1] = (word & 0xFF00) >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
2024-06-09 03:26:13 -04:00
|
|
|
opcode_arg(enum addressing_mode mode)
|
2024-06-08 08:11:06 -04:00
|
|
|
{
|
|
|
|
uint8_t arg, val;
|
|
|
|
|
|
|
|
if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
|
|
|
|
arg = peek(regs.pc++);
|
|
|
|
else
|
2024-06-08 12:38:51 -04:00
|
|
|
arg = peek16(regs.pc), regs.pc += 2;
|
2024-06-08 08:11:06 -04:00
|
|
|
|
|
|
|
switch (mode) {
|
2024-06-08 12:38:51 -04:00
|
|
|
case AM_IMM:
|
2024-06-09 06:32:04 -04:00
|
|
|
case AM_REL:
|
2024-06-08 08:11:06 -04:00
|
|
|
val = arg;
|
|
|
|
break;
|
2024-06-08 12:38:51 -04:00
|
|
|
case AM_ZP:
|
2024-06-08 08:11:06 -04:00
|
|
|
val = peek(arg % 256);
|
|
|
|
break;
|
2024-06-08 12:38:51 -04:00
|
|
|
case AM_ZP_X:
|
2024-06-08 08:11:06 -04:00
|
|
|
val = peek((arg + regs.x) % 256);
|
|
|
|
break;
|
2024-06-08 12:38:51 -04:00
|
|
|
case AM_ZP_Y:
|
|
|
|
val = peek((arg + regs.y) % 256);
|
|
|
|
break;
|
|
|
|
case AM_ABS:
|
|
|
|
val = peek16(arg);
|
|
|
|
break;
|
|
|
|
case AM_ABS_X:
|
2024-06-08 08:11:06 -04:00
|
|
|
val = peek16(arg + regs.x);
|
|
|
|
break;
|
2024-06-08 12:38:51 -04:00
|
|
|
case AM_ABS_Y:
|
2024-06-08 08:11:06 -04:00
|
|
|
val = peek16(arg + regs.y);
|
|
|
|
break;
|
2024-06-08 12:38:51 -04:00
|
|
|
case AM_IND_X:
|
2024-06-08 08:11:06 -04:00
|
|
|
val = peek(peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256);
|
|
|
|
break;
|
2024-06-08 12:38:51 -04:00
|
|
|
case AM_IND_Y:
|
2024-06-08 08:11:06 -04:00
|
|
|
val = peek(peek(arg) + peek((arg + 1) % 256) * 256 + regs.y);
|
|
|
|
break;
|
|
|
|
default:
|
2024-06-08 12:38:51 -04:00
|
|
|
fprintf(stderr, "INVALID ADDRESSING MODE\n");
|
2024-06-08 08:11:06 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2024-06-08 12:38:51 -04:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2024-06-09 12:45:21 -04:00
|
|
|
static uint16_t
|
|
|
|
opcode_mem(enum addressing_mode mode)
|
|
|
|
{
|
|
|
|
uint8_t arg;
|
|
|
|
uint16_t val;
|
|
|
|
|
|
|
|
if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y)
|
|
|
|
arg = peek(regs.pc++);
|
|
|
|
else
|
|
|
|
arg = peek16(regs.pc), regs.pc += 2;
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case AM_ZP:
|
|
|
|
val = arg % 256;
|
|
|
|
break;
|
|
|
|
case AM_ZP_X:
|
|
|
|
val = (arg + regs.x) % 256;
|
|
|
|
break;
|
|
|
|
case AM_ZP_Y:
|
|
|
|
val = (arg + regs.y) % 256;
|
|
|
|
break;
|
|
|
|
case AM_ABS:
|
|
|
|
val = arg;
|
|
|
|
break;
|
|
|
|
case AM_ABS_X:
|
|
|
|
val = arg + regs.x;
|
|
|
|
break;
|
|
|
|
case AM_ABS_Y:
|
|
|
|
val = arg + regs.y;
|
|
|
|
break;
|
|
|
|
case AM_IND_X:
|
|
|
|
val = peek((arg + regs.x) % 256) + peek((arg + regs.x + 1) % 256) * 256;
|
|
|
|
break;
|
|
|
|
case AM_IND_Y:
|
|
|
|
val = peek(arg) + peek((arg + 1) % 256) * 256 + regs.y;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "INVALID ADDRESSING MODE\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
2024-06-08 12:38:51 -04:00
|
|
|
adc(uint8_t arg)
|
|
|
|
{
|
|
|
|
uint16_t sum; // 16-bit sum makes it easier to determine carry flag
|
|
|
|
|
|
|
|
sum = regs.a + arg + regs.status.carry;
|
2024-06-08 08:11:06 -04:00
|
|
|
regs.a = sum & 0xFF;
|
|
|
|
|
|
|
|
regs.status.carry = sum > 0xFF;
|
|
|
|
/* overflow flag formula: https://stackoverflow.com/a/29224684 */
|
2024-06-09 06:05:31 -04:00
|
|
|
regs.status.overflow = (~(regs.a ^ arg) & (regs.a ^ sum) & 0x80) > 0;
|
2024-06-08 08:11:06 -04:00
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
|
|
|
}
|
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
|
|
|
and(uint8_t arg)
|
2024-06-09 03:26:13 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
regs.a &= arg;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:26:13 -04:00
|
|
|
}
|
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
|
|
|
asl(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
uint16_t tmp;
|
|
|
|
|
|
|
|
tmp = regs.a << 1;
|
|
|
|
regs.a = tmp & 0xFF;
|
|
|
|
|
|
|
|
regs.status.carry = tmp > 0xFF;
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bcc(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.carry == 0)
|
|
|
|
regs.pc += arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bcs(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.carry == 1)
|
|
|
|
regs.pc += arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
beq(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.zero == 1)
|
|
|
|
regs.pc += arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bit(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
regs.status.zero = (regs.a & arg) == 0;
|
|
|
|
regs.status.overflow = (arg & (1 << 6)) > 0;
|
2024-06-09 13:20:33 -04:00
|
|
|
STATUS_UPDATE_NEGATIVE(arg);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bmi(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.negative == 1)
|
|
|
|
regs.pc += arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bne(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.zero == 0)
|
|
|
|
regs.pc += arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bpl(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.negative == 0)
|
|
|
|
regs.pc += arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 06:32:04 -04:00
|
|
|
brk(void)
|
2024-05-24 02:20:08 -04:00
|
|
|
{
|
|
|
|
/* TODO: push regs.pc and regs.status to stack and load IRQ vector */
|
|
|
|
regs.status.brk = 1;
|
2024-06-09 03:41:05 -04:00
|
|
|
exit(0);
|
2024-05-24 02:20:08 -04:00
|
|
|
}
|
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
|
|
|
bvc(uint8_t arg)
|
2024-05-24 02:50:04 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.overflow == 0)
|
|
|
|
regs.pc += arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
2024-05-24 02:50:04 -04:00
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
|
|
|
bvs(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
if (regs.status.overflow == 1)
|
|
|
|
regs.pc += arg;
|
2024-05-24 02:50:04 -04:00
|
|
|
}
|
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
2024-06-09 06:32:04 -04:00
|
|
|
clc(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
regs.status.carry = 0;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 06:32:04 -04:00
|
|
|
cld(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
regs.status.decimal_mode = 0;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 06:32:04 -04:00
|
|
|
cli(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
regs.status.interrupt_disable = 0;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 06:32:04 -04:00
|
|
|
clv(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
regs.status.overflow = 0;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmp(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
uint8_t tmp;
|
|
|
|
|
|
|
|
tmp = regs.a - arg;
|
|
|
|
|
|
|
|
regs.status.carry = regs.a >= arg;
|
|
|
|
regs.status.zero = regs.a == arg;
|
2024-06-09 13:20:33 -04:00
|
|
|
STATUS_UPDATE_NEGATIVE(tmp);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cpx(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
uint8_t tmp;
|
|
|
|
|
|
|
|
tmp = regs.x - arg;
|
|
|
|
|
|
|
|
regs.status.carry = regs.x >= arg;
|
|
|
|
regs.status.zero = regs.x == arg;
|
2024-06-09 13:20:33 -04:00
|
|
|
STATUS_UPDATE_NEGATIVE(tmp);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cpy(uint8_t arg)
|
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
uint8_t tmp;
|
|
|
|
|
|
|
|
tmp = regs.y - arg;
|
|
|
|
|
|
|
|
regs.status.carry = regs.y >= arg;
|
|
|
|
regs.status.zero = regs.y == arg;
|
2024-06-09 13:20:33 -04:00
|
|
|
STATUS_UPDATE_NEGATIVE(tmp);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 12:45:21 -04:00
|
|
|
dec(uint16_t mem)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 12:45:21 -04:00
|
|
|
memwrite(mem, peek(mem) - 1);
|
2024-06-09 13:20:33 -04:00
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(peek(mem));
|
|
|
|
STATUS_UPDATE_NEGATIVE(peek(mem));
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 13:20:33 -04:00
|
|
|
dex(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.x--;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.x);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 13:20:33 -04:00
|
|
|
dey(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.y--;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.y);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eor(uint8_t arg)
|
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.a ^= arg;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 13:20:33 -04:00
|
|
|
inc(uint16_t mem)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
memwrite(mem, peek(mem) + 1);
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(peek(mem));
|
|
|
|
STATUS_UPDATE_NEGATIVE(peek(mem));
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 06:32:04 -04:00
|
|
|
inx(void)
|
2024-05-24 02:50:04 -04:00
|
|
|
{
|
|
|
|
regs.x++;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.x);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.x);
|
|
|
|
}
|
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
|
|
|
iny(uint8_t arg)
|
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.y++;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.y);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:55:49 -04:00
|
|
|
jmp(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.pc = arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:55:49 -04:00
|
|
|
jsr(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:55:49 -04:00
|
|
|
uint16_t tmp = regs.pc + 2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* first push high-byte of return address then low-byte
|
|
|
|
* https://www.masswerk.at/6502/6502_instruction_set.html
|
|
|
|
*/
|
|
|
|
PUSH((tmp & 0xFF00) >> 8);
|
|
|
|
PUSH(tmp & 0xFF);
|
|
|
|
regs.pc = arg;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-08 12:38:51 -04:00
|
|
|
lda(uint8_t arg)
|
2024-05-24 02:20:08 -04:00
|
|
|
{
|
2024-06-08 12:38:51 -04:00
|
|
|
regs.a = arg;
|
2024-06-04 06:24:09 -04:00
|
|
|
|
2024-05-24 02:50:04 -04:00
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-05-24 02:20:08 -04:00
|
|
|
}
|
|
|
|
|
2024-06-09 03:41:05 -04:00
|
|
|
static void
|
|
|
|
ldx(uint8_t arg)
|
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.x = arg;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.x);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ldy(uint8_t arg)
|
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.y = arg;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.y);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lsr(uint8_t arg)
|
|
|
|
{
|
2024-06-10 04:57:53 -04:00
|
|
|
regs.status.carry = regs.a & 1; // bit 0 in carry
|
|
|
|
regs.a >>= 1;
|
|
|
|
regs.a &= ~(1 << 7); // bit 7 cleared
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
nop(uint8_t arg)
|
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
return;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ora(uint8_t arg)
|
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.a |= arg;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
pha(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:26:06 -04:00
|
|
|
PUSH(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
php(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:26:06 -04:00
|
|
|
uint8_t status;
|
2024-06-09 03:41:05 -04:00
|
|
|
|
2024-06-10 13:26:06 -04:00
|
|
|
status = (regs.status.carry << 7) | (regs.status.zero << 6)
|
|
|
|
| (regs.status.interrupt_disable << 5) | (regs.status.decimal_mode << 4)
|
|
|
|
| (regs.status.brk << 3) | (regs.status.unused << 2)
|
|
|
|
| (regs.status.overflow << 1) | regs.status.negative;
|
2024-06-09 03:41:05 -04:00
|
|
|
|
2024-06-10 13:26:06 -04:00
|
|
|
PUSH(status);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
pla(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:26:06 -04:00
|
|
|
regs.a = PULL();
|
2024-06-09 03:41:05 -04:00
|
|
|
|
2024-06-10 13:26:06 -04:00
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
plp(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:26:06 -04:00
|
|
|
uint8_t status;
|
2024-06-09 03:41:05 -04:00
|
|
|
|
2024-06-10 13:26:06 -04:00
|
|
|
status = PULL();
|
|
|
|
|
|
|
|
regs.status.carry = (status & (1 << 7)) > 0;
|
|
|
|
regs.status.zero = (status & (1 << 6)) > 0;
|
|
|
|
regs.status.interrupt_disable = (status & (1 << 5)) > 0;
|
|
|
|
regs.status.decimal_mode = (status & (1 << 4)) > 0;
|
|
|
|
regs.status.brk = (status & (1 << 3)) > 0;
|
|
|
|
regs.status.unused = (status & (1 << 2)) > 0;
|
|
|
|
regs.status.overflow = (status & (1 << 1)) > 0;
|
|
|
|
regs.status.negative = (status & 1) > 0;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rol(uint8_t arg)
|
|
|
|
{
|
2024-06-10 04:57:53 -04:00
|
|
|
uint8_t tmp;
|
|
|
|
tmp = (regs.a & (1 << 7)) > 0;
|
|
|
|
|
|
|
|
regs.a <<= 1;
|
|
|
|
regs.a |= regs.status.carry;
|
|
|
|
|
|
|
|
regs.status.carry = tmp;
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ror(uint8_t arg)
|
|
|
|
{
|
2024-06-10 04:57:53 -04:00
|
|
|
uint8_t tmp;
|
|
|
|
tmp = regs.a & 1;
|
|
|
|
|
|
|
|
regs.a >>= 1;
|
|
|
|
regs.a |= regs.status.carry << 7;
|
|
|
|
|
|
|
|
regs.status.carry = tmp;
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rti(uint8_t arg)
|
|
|
|
{
|
2024-06-10 13:55:49 -04:00
|
|
|
plp();
|
|
|
|
regs.pc = PULL();
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:55:49 -04:00
|
|
|
rts(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:55:49 -04:00
|
|
|
regs.pc = PULL() + 1;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sbc(uint8_t arg)
|
|
|
|
{
|
|
|
|
/* SBC is described online as ADC with argument as two's complement */
|
|
|
|
adc(~arg + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
sec(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:20:00 -04:00
|
|
|
regs.status.carry = 1;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
sed(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:20:00 -04:00
|
|
|
regs.status.decimal_mode = 1;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
sei(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:20:00 -04:00
|
|
|
regs.status.interrupt_disable = 1;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(uint16_t mem)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:24:05 -04:00
|
|
|
memwrite(mem, regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 03:24:05 -04:00
|
|
|
stx(uint16_t mem)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:24:05 -04:00
|
|
|
memwrite(mem, regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 03:24:05 -04:00
|
|
|
sty(uint16_t mem)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:24:05 -04:00
|
|
|
memwrite(mem, regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-09 06:32:04 -04:00
|
|
|
tax(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
|
|
|
regs.x = regs.a;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.x);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
tay(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:25:40 -04:00
|
|
|
regs.y = regs.a;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.y);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
tsx(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:26:06 -04:00
|
|
|
regs.x = PULL();
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.x);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
txa(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:25:40 -04:00
|
|
|
regs.a = regs.x;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
txs(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 13:26:06 -04:00
|
|
|
PUSH(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-06-10 13:26:06 -04:00
|
|
|
tya(void)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:25:40 -04:00
|
|
|
regs.a = regs.y;
|
|
|
|
|
|
|
|
STATUS_UPDATE_ZERO(regs.a);
|
|
|
|
STATUS_UPDATE_NEGATIVE(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-06-09 06:37:09 -04:00
|
|
|
static void
|
2024-05-24 02:50:04 -04:00
|
|
|
interpret(void)
|
2024-05-20 10:09:19 -04:00
|
|
|
{
|
|
|
|
uint8_t opcode;
|
|
|
|
|
|
|
|
for (;;) {
|
2024-06-04 06:24:09 -04:00
|
|
|
opcode = peek(regs.pc++);
|
2024-05-20 10:09:19 -04:00
|
|
|
|
2024-05-24 02:50:04 -04:00
|
|
|
printf("opcode: $%02X\n", opcode);
|
|
|
|
|
2024-05-20 10:09:19 -04:00
|
|
|
switch (opcode) {
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x69:
|
|
|
|
adc(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
|
|
|
case 0x65:
|
2024-06-09 03:26:13 -04:00
|
|
|
adc(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x75:
|
|
|
|
adc(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x6d:
|
|
|
|
adc(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x7d:
|
|
|
|
adc(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x79:
|
|
|
|
adc(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x72:
|
|
|
|
adc(opcode_arg(AM_IND));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x61:
|
|
|
|
adc(opcode_arg(AM_IND_X));
|
|
|
|
cycles += 6;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
|
|
|
case 0x71:
|
2024-06-09 03:26:13 -04:00
|
|
|
adc(opcode_arg(AM_IND_Y));
|
|
|
|
cycles += 5;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x29:
|
|
|
|
and(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x25:
|
|
|
|
and(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x35:
|
|
|
|
and(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
2024-06-08 08:11:06 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x2d:
|
|
|
|
and(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
2024-05-24 02:20:08 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0x3d:
|
|
|
|
and(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x39:
|
|
|
|
and(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x32:
|
|
|
|
and(opcode_arg(AM_IND));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x21:
|
|
|
|
and(opcode_arg(AM_IND_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x31:
|
|
|
|
and(opcode_arg(AM_IND_Y));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x0a:
|
|
|
|
asl(opcode_arg(AM_ACC));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x06:
|
|
|
|
asl(opcode_arg(AM_ZP));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x16:
|
|
|
|
asl(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x0e:
|
|
|
|
asl(opcode_arg(AM_ABS));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x1e:
|
|
|
|
asl(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x90:
|
|
|
|
bcc(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xb0:
|
|
|
|
bcs(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xf0:
|
|
|
|
beq(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x89:
|
|
|
|
bit(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x24:
|
|
|
|
bit(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0x34:
|
|
|
|
bit(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x2c:
|
|
|
|
bit(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x3c:
|
|
|
|
bit(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x30:
|
|
|
|
bmi(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xd0:
|
|
|
|
bne(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x10:
|
|
|
|
bpl(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x00:
|
|
|
|
brk();
|
|
|
|
cycles += 7;
|
|
|
|
break;
|
|
|
|
case 0x50:
|
|
|
|
bvc(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x70:
|
|
|
|
bvs(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x18:
|
2024-06-09 12:45:21 -04:00
|
|
|
clc();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xd8:
|
2024-06-09 12:45:21 -04:00
|
|
|
cld();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x58:
|
2024-06-09 12:45:21 -04:00
|
|
|
cli();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xb8:
|
2024-06-09 12:45:21 -04:00
|
|
|
clv();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xc9:
|
|
|
|
cmp(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xc5:
|
|
|
|
cmp(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0xd5:
|
|
|
|
cmp(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xcd:
|
|
|
|
cmp(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xdd:
|
|
|
|
cmp(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xd9:
|
|
|
|
cmp(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xd2:
|
|
|
|
cmp(opcode_arg(AM_IND));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0xc1:
|
|
|
|
cmp(opcode_arg(AM_IND_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xd1:
|
|
|
|
cmp(opcode_arg(AM_IND_Y));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0xe0:
|
|
|
|
cpx(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xe4:
|
|
|
|
cpx(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0xec:
|
|
|
|
cpx(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xc0:
|
|
|
|
cpy(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xc4:
|
|
|
|
cpy(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0xcc:
|
|
|
|
cpy(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xc6:
|
2024-06-09 12:45:21 -04:00
|
|
|
dec(opcode_mem(AM_ZP));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0xd6:
|
2024-06-09 12:45:21 -04:00
|
|
|
dec(opcode_mem(AM_ZP_X));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xce:
|
2024-06-09 12:45:21 -04:00
|
|
|
dec(opcode_mem(AM_ABS));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xde:
|
2024-06-09 12:45:21 -04:00
|
|
|
dec(opcode_mem(AM_ABS_X));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 7;
|
|
|
|
break;
|
|
|
|
case 0xca:
|
2024-06-09 13:20:33 -04:00
|
|
|
dex();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x88:
|
2024-06-09 13:20:33 -04:00
|
|
|
dey();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x49:
|
|
|
|
eor(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x45:
|
|
|
|
eor(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0x55:
|
|
|
|
eor(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x4d:
|
|
|
|
eor(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x5d:
|
|
|
|
eor(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x59:
|
|
|
|
eor(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x52:
|
|
|
|
eor(opcode_arg(AM_IND));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x41:
|
|
|
|
eor(opcode_arg(AM_IND_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x51:
|
|
|
|
eor(opcode_arg(AM_IND_Y));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x1a:
|
|
|
|
inc(opcode_arg(AM_ACC));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xe6:
|
|
|
|
inc(opcode_arg(AM_ZP));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0xf6:
|
|
|
|
inc(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xee:
|
|
|
|
inc(opcode_arg(AM_ABS));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xfe:
|
|
|
|
inc(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 7;
|
|
|
|
break;
|
|
|
|
case 0xe8:
|
|
|
|
inx();
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xc8:
|
|
|
|
iny(opcode_arg(AM_ACC));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x4c:
|
|
|
|
jmp(opcode_arg(AM_ABS));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0x6c:
|
|
|
|
jmp(opcode_arg(AM_IND));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x7c:
|
|
|
|
jmp(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x20:
|
|
|
|
jsr(opcode_arg(AM_ABS));
|
|
|
|
cycles += 6;
|
2024-05-24 02:20:08 -04:00
|
|
|
break;
|
2024-05-24 02:50:04 -04:00
|
|
|
case 0xa9:
|
2024-06-09 03:26:13 -04:00
|
|
|
lda(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
2024-05-24 02:50:04 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0xa5:
|
|
|
|
lda(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0xb5:
|
|
|
|
lda(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
2024-05-24 02:20:08 -04:00
|
|
|
break;
|
|
|
|
case 0xad:
|
2024-06-09 03:26:13 -04:00
|
|
|
lda(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xbd:
|
|
|
|
lda(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xb9:
|
|
|
|
lda(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xb2:
|
|
|
|
lda(opcode_arg(AM_IND));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0xa1:
|
|
|
|
lda(opcode_arg(AM_IND_X));
|
|
|
|
cycles += 6;
|
2024-05-24 02:20:08 -04:00
|
|
|
break;
|
2024-05-24 02:50:04 -04:00
|
|
|
case 0xb1:
|
2024-06-09 03:26:13 -04:00
|
|
|
lda(opcode_arg(AM_IND_Y));
|
|
|
|
cycles += 5;
|
2024-05-24 02:50:04 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0xa2:
|
|
|
|
ldx(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
2024-05-24 02:20:08 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0xa6:
|
|
|
|
ldx(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
2024-05-24 02:20:08 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0xb6:
|
|
|
|
ldx(opcode_arg(AM_ZP_Y));
|
|
|
|
cycles += 4;
|
2024-05-24 02:20:08 -04:00
|
|
|
break;
|
2024-06-09 03:26:13 -04:00
|
|
|
case 0xae:
|
|
|
|
ldx(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xbe:
|
|
|
|
ldx(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xa0:
|
|
|
|
ldy(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xa4:
|
|
|
|
ldy(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0xb4:
|
|
|
|
ldy(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xac:
|
|
|
|
ldy(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xbc:
|
|
|
|
ldy(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x4a:
|
|
|
|
lsr(opcode_arg(AM_ACC));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x46:
|
|
|
|
lsr(opcode_arg(AM_ZP));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x56:
|
|
|
|
lsr(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x4e:
|
|
|
|
lsr(opcode_arg(AM_ABS));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x5e:
|
|
|
|
lsr(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xea:
|
|
|
|
nop(opcode_arg(AM_ACC));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x09:
|
|
|
|
ora(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x05:
|
|
|
|
ora(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0x15:
|
|
|
|
ora(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x0d:
|
|
|
|
ora(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x1d:
|
|
|
|
ora(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x19:
|
|
|
|
ora(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x12:
|
|
|
|
ora(opcode_arg(AM_IND));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x01:
|
|
|
|
ora(opcode_arg(AM_IND_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x11:
|
|
|
|
ora(opcode_arg(AM_IND_Y));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x48:
|
2024-06-10 13:26:06 -04:00
|
|
|
pha();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0x08:
|
2024-06-10 13:26:06 -04:00
|
|
|
php();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0x68:
|
2024-06-10 13:26:06 -04:00
|
|
|
pla();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x28:
|
2024-06-10 13:26:06 -04:00
|
|
|
plp();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x2a:
|
|
|
|
rol(opcode_arg(AM_ACC));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x26:
|
|
|
|
rol(opcode_arg(AM_ZP));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x36:
|
|
|
|
rol(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x2e:
|
|
|
|
rol(opcode_arg(AM_ABS));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x3e:
|
|
|
|
rol(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x6a:
|
|
|
|
ror(opcode_arg(AM_ACC));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x66:
|
|
|
|
ror(opcode_arg(AM_ZP));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x76:
|
|
|
|
ror(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x6e:
|
|
|
|
ror(opcode_arg(AM_ABS));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x7e:
|
|
|
|
ror(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x40:
|
|
|
|
rti(opcode_arg(AM_ACC));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x60:
|
2024-06-10 13:55:49 -04:00
|
|
|
rts();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xe9:
|
|
|
|
sbc(opcode_arg(AM_IMM));
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xe5:
|
|
|
|
sbc(opcode_arg(AM_ZP));
|
|
|
|
cycles += 3;
|
|
|
|
break;
|
|
|
|
case 0xf5:
|
|
|
|
sbc(opcode_arg(AM_ZP_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xed:
|
|
|
|
sbc(opcode_arg(AM_ABS));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xfd:
|
|
|
|
sbc(opcode_arg(AM_ABS_X));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xf9:
|
|
|
|
sbc(opcode_arg(AM_ABS_Y));
|
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0xf2:
|
|
|
|
sbc(opcode_arg(AM_IND));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0xe1:
|
|
|
|
sbc(opcode_arg(AM_IND_X));
|
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0xf1:
|
|
|
|
sbc(opcode_arg(AM_IND_Y));
|
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x38:
|
2024-06-10 13:26:06 -04:00
|
|
|
sec();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xf8:
|
2024-06-10 13:26:06 -04:00
|
|
|
sed();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x78:
|
2024-06-10 13:26:06 -04:00
|
|
|
sei();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x85:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_ZP));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x95:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_ZP_X));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x8d:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_ABS));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x9d:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_ABS_X));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x99:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_ABS_Y));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x92:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_IND));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 6;
|
|
|
|
break;
|
|
|
|
case 0x81:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_IND_X));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 7;
|
|
|
|
break;
|
|
|
|
case 0x91:
|
2024-06-10 03:24:05 -04:00
|
|
|
sta(opcode_mem(AM_IND_Y));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 7;
|
|
|
|
break;
|
|
|
|
case 0x86:
|
2024-06-10 03:24:05 -04:00
|
|
|
stx(opcode_mem(AM_ZP));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x96:
|
2024-06-10 03:24:05 -04:00
|
|
|
stx(opcode_mem(AM_ZP_Y));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x8e:
|
2024-06-10 03:24:05 -04:00
|
|
|
stx(opcode_mem(AM_ABS));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x84:
|
2024-06-10 03:24:05 -04:00
|
|
|
sty(opcode_mem(AM_ZP));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 4;
|
|
|
|
break;
|
|
|
|
case 0x94:
|
2024-06-10 03:24:05 -04:00
|
|
|
sty(opcode_mem(AM_ZP_X));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0x8c:
|
2024-06-10 03:24:05 -04:00
|
|
|
sty(opcode_mem(AM_ABS));
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 5;
|
|
|
|
break;
|
|
|
|
case 0xaa:
|
|
|
|
tax();
|
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xa8:
|
2024-06-10 13:26:06 -04:00
|
|
|
tay();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0xba:
|
2024-06-10 13:26:06 -04:00
|
|
|
tsx();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x8a:
|
2024-06-10 13:26:06 -04:00
|
|
|
txa();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x9a:
|
2024-06-10 13:26:06 -04:00
|
|
|
txs();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
|
|
|
case 0x98:
|
2024-06-10 13:26:06 -04:00
|
|
|
tya();
|
2024-06-09 03:26:13 -04:00
|
|
|
cycles += 2;
|
|
|
|
break;
|
2024-05-20 10:09:19 -04:00
|
|
|
default:
|
2024-05-24 02:50:04 -04:00
|
|
|
printf("opcode $%02X not implemented\n", opcode);
|
2024-05-20 10:09:19 -04:00
|
|
|
break;
|
|
|
|
}
|
2024-05-24 02:50:04 -04:00
|
|
|
|
|
|
|
printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry,
|
|
|
|
regs.status.zero, regs.status.interrupt_disable,
|
|
|
|
regs.status.decimal_mode, regs.status.brk,
|
|
|
|
regs.status.unused, regs.status.overflow,
|
|
|
|
regs.status.negative);
|
|
|
|
printf("A: $%02X, X: $%02X, Y: $%02X, SP: $%02X, PC: $%02X\n",
|
|
|
|
regs.a, regs.x, regs.y, regs.sp, regs.pc);
|
2024-05-20 10:09:19 -04:00
|
|
|
}
|
|
|
|
}
|
2024-05-24 02:50:04 -04:00
|
|
|
|
2024-06-04 06:24:09 -04:00
|
|
|
/* https://www.nesdev.org/wiki/CPU_power_up_state */
|
|
|
|
void
|
|
|
|
cpu_init(void)
|
|
|
|
{
|
|
|
|
regs.a = regs.x = regs.y = 0;
|
|
|
|
regs.pc = 0xFFFC;
|
|
|
|
regs.sp = 0xFD;
|
|
|
|
|
|
|
|
memset(®s.status, 0, sizeof(regs.status));
|
|
|
|
regs.status.unused = 1;
|
2024-06-10 13:26:06 -04:00
|
|
|
|
|
|
|
cycles += 7;
|
2024-06-04 06:24:09 -04:00
|
|
|
}
|
|
|
|
|
2024-05-24 02:50:04 -04:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2024-06-04 06:24:09 -04:00
|
|
|
cpu_init();
|
|
|
|
|
2024-05-24 03:25:44 -04:00
|
|
|
if (sizeof(program) > (0x10000 - 0x8000)) {
|
|
|
|
fprintf(stderr, "program is too big for memory\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-06-08 08:11:06 -04:00
|
|
|
|
2024-05-24 03:25:44 -04:00
|
|
|
memcpy(memory + 0x8000, program, sizeof(program));
|
|
|
|
regs.pc = 0x8000;
|
|
|
|
|
|
|
|
printf("Initial State:\n");
|
|
|
|
printf("status: %i%i%i%i%i%i%i%i\n", regs.status.carry,
|
|
|
|
regs.status.zero, regs.status.interrupt_disable,
|
|
|
|
regs.status.decimal_mode, regs.status.brk,
|
|
|
|
regs.status.unused, regs.status.overflow,
|
|
|
|
regs.status.negative);
|
|
|
|
printf("A: $%02X, X: $%02X, Y: $%02X, SP: $%02X, PC: $%02X\n",
|
|
|
|
regs.a, regs.x, regs.y, regs.sp, regs.pc);
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
|
2024-05-24 02:50:04 -04:00
|
|
|
interpret();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|