2024-06-17 05:16:00 -04:00
|
|
|
#include <libgen.h>
|
2024-07-03 20:40:57 -04:00
|
|
|
#include <stdbool.h>
|
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-30 19:10:29 -04:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "opcodes.h"
|
2024-06-17 05:16:00 -04:00
|
|
|
#include "rom.h"
|
|
|
|
|
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) \
|
2024-06-16 07:41:51 -04:00
|
|
|
(regs.status.negative = ((r & (1 << 7)) != 0))
|
2024-06-30 15:46:18 -04:00
|
|
|
#define STATUS_UPDATE_NZ(r) \
|
|
|
|
{STATUS_UPDATE_ZERO(r); \
|
|
|
|
STATUS_UPDATE_NEGATIVE(r);}
|
2024-06-28 09:51:55 -04:00
|
|
|
#define STATUS_TO_INT() \
|
2024-06-28 14:44:16 -04:00
|
|
|
(regs.status.carry | (regs.status.zero << 1) \
|
|
|
|
| (regs.status.interrupt_disable << 2) | (regs.status.decimal_mode << 3) \
|
|
|
|
| (regs.status.brk << 4) | (regs.status.unused << 5) \
|
|
|
|
| (regs.status.overflow << 6) | (regs.status.negative << 7))
|
2024-05-20 10:09:19 -04:00
|
|
|
|
2024-06-11 06:08:58 -04:00
|
|
|
#define MEMORY_MIRROR(addr) \
|
|
|
|
if (addr < 0x2000) \
|
|
|
|
addr &= 0x07FF; \
|
|
|
|
else if (addr < 0x4000) \
|
|
|
|
addr &= 0x2007;
|
|
|
|
|
2024-06-10 13:26:06 -04:00
|
|
|
#define PUSH(b) \
|
|
|
|
(memwrite(0x0100 + regs.sp--, b))
|
|
|
|
#define PULL() \
|
2024-06-28 10:18:01 -04:00
|
|
|
(peek(0x0100 + ++regs.sp))
|
2024-06-10 13:26:06 -04:00
|
|
|
|
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;
|
|
|
|
};
|
2024-06-17 05:16:00 -04:00
|
|
|
struct registers regs = {0};
|
|
|
|
|
|
|
|
struct Rom rom = {0};
|
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
|
|
|
|
2024-06-09 03:26:13 -04:00
|
|
|
uint32_t cycles = 0;
|
|
|
|
|
2024-07-04 11:18:38 -04:00
|
|
|
bool page_crossed = false;
|
|
|
|
|
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
|
|
|
{
|
2024-06-11 06:08:58 -04:00
|
|
|
MEMORY_MIRROR(addr);
|
|
|
|
|
2024-06-17 05:16:00 -04:00
|
|
|
if (addr > 0x7FFF) {
|
|
|
|
if (rom.prg_rom_size == 0x4000)
|
|
|
|
return rom.prg_rom[(addr - 0x8000) % 0x4000];
|
|
|
|
else if (rom.prg_rom_size == 0x8000)
|
|
|
|
return rom.prg_rom[addr - 0x8000];
|
|
|
|
} else {
|
|
|
|
return memory[addr];
|
|
|
|
}
|
2024-06-04 06:24:09 -04:00
|
|
|
}
|
|
|
|
|
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) */
|
2024-06-17 05:41:45 -04:00
|
|
|
return peek(addr) | (peek(addr + 1) << 8);
|
2024-06-04 06:24:09 -04:00
|
|
|
}
|
|
|
|
|
2024-06-09 06:37:09 -04:00
|
|
|
static void
|
|
|
|
memwrite(uint16_t addr, uint8_t byte)
|
|
|
|
{
|
2024-06-11 06:08:58 -04:00
|
|
|
MEMORY_MIRROR(addr);
|
|
|
|
|
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)
|
|
|
|
{
|
2024-06-11 06:08:58 -04:00
|
|
|
MEMORY_MIRROR(addr);
|
|
|
|
|
2024-06-09 06:37:09 -04:00
|
|
|
/* bytes are stored in little-endian (low then high) */
|
|
|
|
memory[addr] = word & 0xFF;
|
|
|
|
memory[addr + 1] = (word & 0xFF00) >> 8;
|
|
|
|
}
|
|
|
|
|
2024-06-09 12:45:21 -04:00
|
|
|
static uint16_t
|
|
|
|
opcode_mem(enum addressing_mode mode)
|
|
|
|
{
|
2024-06-17 05:41:45 -04:00
|
|
|
uint16_t arg, val;
|
2024-06-09 12:45:21 -04:00
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
if (mode == AM_ACC || mode == AM_NONE) {
|
|
|
|
return 0;
|
|
|
|
} else if (mode != AM_ABS && mode != AM_ABS_X && mode != AM_ABS_Y && mode != AM_IND && mode) {
|
2024-06-09 12:45:21 -04:00
|
|
|
arg = peek(regs.pc++);
|
2024-06-28 09:51:55 -04:00
|
|
|
} else {
|
|
|
|
arg = peek16(regs.pc);
|
|
|
|
regs.pc += 2;
|
|
|
|
}
|
2024-06-09 12:45:21 -04:00
|
|
|
|
|
|
|
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;
|
2024-07-03 18:52:15 -04:00
|
|
|
case AM_REL:
|
2024-07-04 15:06:15 -04:00
|
|
|
val = (int8_t)arg + regs.pc;
|
2024-07-04 11:18:38 -04:00
|
|
|
break;
|
|
|
|
case AM_IMM:
|
2024-06-09 12:45:21 -04:00
|
|
|
case AM_ABS:
|
|
|
|
val = arg;
|
|
|
|
break;
|
|
|
|
case AM_ABS_X:
|
|
|
|
val = arg + regs.x;
|
2024-07-04 11:18:38 -04:00
|
|
|
|
|
|
|
if ((arg & 0xFF00) != (val & 0xFF00))
|
|
|
|
page_crossed = true;
|
2024-06-09 12:45:21 -04:00
|
|
|
break;
|
|
|
|
case AM_ABS_Y:
|
|
|
|
val = arg + regs.y;
|
2024-07-04 11:18:38 -04:00
|
|
|
|
|
|
|
if ((arg & 0xFF00) != (val & 0xFF00))
|
|
|
|
page_crossed = true;
|
2024-06-09 12:45:21 -04:00
|
|
|
break;
|
2024-06-28 10:28:53 -04:00
|
|
|
case AM_IND:
|
2024-06-30 15:14:38 -04:00
|
|
|
val = peek(arg) | (peek(((arg + 1) & 0xFF) | (arg & 0xFF00)) << 8);
|
2024-06-28 10:28:53 -04:00
|
|
|
break;
|
2024-06-09 12:45:21 -04:00
|
|
|
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;
|
2024-07-04 11:18:38 -04:00
|
|
|
|
|
|
|
if (((uint16_t)(val - regs.y) & 0xFF00) != (val & 0xFF00))
|
|
|
|
page_crossed = true;
|
2024-06-09 12:45:21 -04:00
|
|
|
break;
|
|
|
|
default:
|
2024-07-03 19:49:21 -04:00
|
|
|
fprintf(stderr, "INVALID ADDRESSING MODE %i\n", mode);
|
2024-06-09 12:45:21 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2024-07-03 20:40:57 -04:00
|
|
|
static void
|
|
|
|
branch(uint16_t addr, bool cond)
|
|
|
|
{
|
|
|
|
if (!cond)
|
|
|
|
return;
|
|
|
|
cycles++;
|
|
|
|
|
2024-07-04 15:06:15 -04:00
|
|
|
if (((regs.pc + 1) & 0xFF00) != (addr & 0xFF00))
|
|
|
|
cycles++;
|
|
|
|
|
2024-07-03 20:40:57 -04:00
|
|
|
regs.pc = addr;
|
|
|
|
}
|
|
|
|
|
2024-07-04 12:11:53 -04:00
|
|
|
/* OFFICIAL OPCODES */
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ADC(uint16_t arg)
|
2024-06-08 12:38:51 -04:00
|
|
|
{
|
|
|
|
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.status.carry = sum > 0xFF;
|
|
|
|
/* overflow flag formula: https://stackoverflow.com/a/29224684 */
|
2024-06-16 07:41:51 -04:00
|
|
|
regs.status.overflow = (~(regs.a ^ arg) & (regs.a ^ sum) & 0x80) != 0;
|
2024-06-29 09:51:12 -04:00
|
|
|
regs.a = sum & 0xFF;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-08 08:11:06 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
AND(uint16_t arg)
|
2024-06-09 03:26:13 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
regs.a &= arg;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:26:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ASL_acc(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 06:32:04 -04:00
|
|
|
uint16_t tmp;
|
|
|
|
|
|
|
|
tmp = regs.a << 1;
|
|
|
|
regs.a = tmp & 0xFF;
|
|
|
|
|
|
|
|
regs.status.carry = tmp > 0xFF;
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ASL(uint16_t mem)
|
2024-06-11 05:38:53 -04:00
|
|
|
{
|
|
|
|
uint16_t tmp;
|
|
|
|
|
|
|
|
tmp = peek(mem) << 1;
|
|
|
|
memwrite(mem, tmp & 0xFF);
|
|
|
|
|
|
|
|
regs.status.carry = tmp > 0xFF;
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(tmp);
|
2024-06-11 05:38:53 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BCC(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, regs.status.carry == 0);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BCS(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, regs.status.carry == 1);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BEQ(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, regs.status.zero == 1);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BIT(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 19:49:21 -04:00
|
|
|
uint8_t tmp = arg;
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
regs.status.zero = (regs.a & tmp) == 0;
|
|
|
|
regs.status.overflow = (tmp & (1 << 6)) != 0;
|
|
|
|
STATUS_UPDATE_NEGATIVE(tmp);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BMI(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, regs.status.negative == 1);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BNE(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, regs.status.zero == 0);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BPL(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, regs.status.negative == 0);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BRK(uint16_t arg)
|
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-07-04 15:06:15 -04:00
|
|
|
exit(0);
|
2024-05-24 02:20:08 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BVC(uint16_t arg)
|
2024-05-24 02:50:04 -04:00
|
|
|
{
|
2024-06-29 09:51:49 -04:00
|
|
|
//regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) == 0;
|
|
|
|
//if (regs.status.overflow == 0)
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, (STATUS_TO_INT() & (1 << 6)) == 0);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
2024-05-24 02:50:04 -04:00
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
BVS(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-29 09:51:49 -04:00
|
|
|
regs.status.overflow = (STATUS_TO_INT() & (1 << 6)) != 0;
|
2024-07-03 20:40:57 -04:00
|
|
|
branch(arg, regs.status.overflow == 1);
|
2024-05-24 02:50:04 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
CLC(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
CLD(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
CLI(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
CLV(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
CMP(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
CPX(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
CPY(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
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
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(peek(mem));
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
DEX(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.x--;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
DEY(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.y--;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
EOR(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.a ^= arg;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
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);
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(peek(mem));
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
INX(uint16_t arg)
|
2024-05-24 02:50:04 -04:00
|
|
|
{
|
|
|
|
regs.x++;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.x);
|
2024-05-24 02:50:04 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
INY(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.y++;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
JSR(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-30 07:56:08 -04:00
|
|
|
uint16_t tmp = regs.pc - 1;
|
2024-06-10 13:55:49 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
LDA(uint16_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-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-05-24 02:20:08 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
LDX(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.x = arg;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
LDY(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.y = arg;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
LSR_acc(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
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
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
LSR(uint16_t mem)
|
2024-06-11 05:38:53 -04:00
|
|
|
{
|
|
|
|
uint8_t tmp;
|
|
|
|
|
|
|
|
tmp = peek(mem);
|
|
|
|
|
|
|
|
regs.status.carry = tmp & 1; // bit 0 in carry
|
|
|
|
tmp >>= 1;
|
|
|
|
tmp &= ~(1 << 7); // bit 7 cleared
|
|
|
|
|
|
|
|
memwrite(mem, tmp);
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(tmp);
|
2024-06-11 05:38:53 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
NOP(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
return;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ORA(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-09 13:20:33 -04:00
|
|
|
regs.a |= arg;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
PHA(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
PHP(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-29 09:51:49 -04:00
|
|
|
PUSH(STATUS_TO_INT() | (1 << 4));
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
PLA(uint16_t arg)
|
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-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
PLP(uint16_t arg)
|
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();
|
|
|
|
|
2024-06-29 09:51:49 -04:00
|
|
|
regs.status.carry = (status & 1) != 0;
|
|
|
|
regs.status.zero = (status & (1 << 1)) != 0;
|
|
|
|
regs.status.interrupt_disable = (status & (1 << 2)) != 0;
|
|
|
|
regs.status.decimal_mode = (status & (1 << 3)) != 0;
|
|
|
|
regs.status.brk = 0;
|
|
|
|
regs.status.unused = 1;
|
|
|
|
regs.status.overflow = (status & (1 << 6)) != 0;
|
|
|
|
regs.status.negative = (status & (1 << 7)) != 0;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ROL_acc(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-11 05:38:53 -04:00
|
|
|
uint8_t carry;
|
2024-06-16 07:41:51 -04:00
|
|
|
carry = (regs.a & (1 << 7)) != 0;
|
2024-06-10 04:57:53 -04:00
|
|
|
|
|
|
|
regs.a <<= 1;
|
|
|
|
regs.a |= regs.status.carry;
|
|
|
|
|
2024-06-11 05:38:53 -04:00
|
|
|
regs.status.carry = carry;
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ROL(uint16_t mem)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-11 05:38:53 -04:00
|
|
|
uint8_t carry, tmp;
|
2024-06-16 07:41:51 -04:00
|
|
|
carry = (peek(mem) & (1 << 7)) != 0;
|
2024-06-11 05:38:53 -04:00
|
|
|
|
|
|
|
tmp = (peek(mem) << 1) | regs.status.carry;
|
|
|
|
memwrite(mem, tmp);
|
|
|
|
|
|
|
|
regs.status.carry = carry;
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-11 05:38:53 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ROR_acc(uint16_t arg)
|
2024-06-11 05:38:53 -04:00
|
|
|
{
|
|
|
|
uint8_t carry;
|
|
|
|
carry = regs.a & 1;
|
2024-06-10 04:57:53 -04:00
|
|
|
|
|
|
|
regs.a >>= 1;
|
|
|
|
regs.a |= regs.status.carry << 7;
|
|
|
|
|
2024-06-11 05:38:53 -04:00
|
|
|
regs.status.carry = carry;
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
ROR(uint16_t mem)
|
2024-06-11 05:38:53 -04:00
|
|
|
{
|
|
|
|
uint8_t carry, tmp;
|
|
|
|
carry = peek(mem) & 1;
|
|
|
|
|
|
|
|
tmp = (peek(mem) >> 1) | (regs.status.carry << 7);
|
|
|
|
memwrite(mem, tmp);
|
|
|
|
|
|
|
|
regs.status.carry = carry;
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(tmp);
|
2024-06-11 05:38:53 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
RTI(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 18:52:15 -04:00
|
|
|
PLP(0);
|
2024-06-30 07:56:08 -04:00
|
|
|
regs.pc = PULL() | (PULL() << 8);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
RTS(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-30 07:56:08 -04:00
|
|
|
regs.pc = (PULL() | (PULL() << 8)) + 1;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
SBC(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-07-03 19:49:21 -04:00
|
|
|
uint8_t tmp = arg & 0xFF;
|
|
|
|
uint16_t diff;
|
|
|
|
|
|
|
|
diff = regs.a - tmp - !regs.status.carry;
|
|
|
|
|
|
|
|
regs.status.carry = diff > 0xFF;
|
|
|
|
regs.status.carry = !regs.status.carry;
|
|
|
|
/* overflow flag formula: https://stackoverflow.com/a/29224684 */
|
|
|
|
regs.status.overflow = ((regs.a ^ arg) & (regs.a ^ diff) & 0x80) != 0;
|
|
|
|
regs.a = diff & 0xFF;
|
|
|
|
|
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
SEC(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
SED(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
SEI(uint16_t arg)
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
TAX(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
|
|
|
regs.x = regs.a;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
TAY(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:25:40 -04:00
|
|
|
regs.y = regs.a;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.y);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
TSX(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-30 06:28:41 -04:00
|
|
|
// regs.x = PULL();
|
|
|
|
regs.x = regs.sp;
|
2024-06-10 13:26:06 -04:00
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.x);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
TXA(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:25:40 -04:00
|
|
|
regs.a = regs.x;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
TXS(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-30 06:28:41 -04:00
|
|
|
// PUSH(regs.x);
|
|
|
|
regs.sp = regs.x;
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
void
|
|
|
|
TYA(uint16_t arg)
|
2024-06-09 03:41:05 -04:00
|
|
|
{
|
2024-06-10 03:25:40 -04:00
|
|
|
regs.a = regs.y;
|
|
|
|
|
2024-06-30 15:46:18 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-06-09 03:41:05 -04:00
|
|
|
}
|
|
|
|
|
2024-07-04 12:11:53 -04:00
|
|
|
/* UNOFFICIAL OPCODES */
|
|
|
|
|
|
|
|
void
|
|
|
|
AAC(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
AND(arg);
|
|
|
|
|
|
|
|
regs.status.carry = regs.status.negative;
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-07-04 14:17:40 -04:00
|
|
|
SAX(uint16_t arg)
|
2024-07-04 12:11:53 -04:00
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
uint8_t tmp = regs.x & regs.a;
|
|
|
|
memwrite(arg, tmp);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ARR(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
uint8_t tmp = arg & regs.a;
|
|
|
|
|
|
|
|
AND(arg);
|
|
|
|
ROR_acc(0);
|
|
|
|
|
|
|
|
if ((tmp & (1 << 5)) != 0 && (tmp & (1 << 6)) != 0)
|
|
|
|
SEC(0), CLV(0);
|
|
|
|
else if ((tmp & (1 << 5)) == 0 && (tmp & (1 << 6)) == 0)
|
|
|
|
CLC(0), CLV(0);
|
|
|
|
else if ((tmp & (1 << 5)) != 0)
|
|
|
|
CLC(0), regs.status.overflow = 1;
|
|
|
|
else if ((tmp & (1 << 6)) != 0)
|
|
|
|
SEC(0), regs.status.overflow = 1;
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ASR(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
AND(arg);
|
|
|
|
LSR_acc(0);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ATX(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
AND(arg);
|
|
|
|
TAX(0);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AXA(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
uint8_t tmp = regs.x & regs.a & 7;
|
|
|
|
memwrite(arg, tmp);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AXS(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
regs.x &= regs.a;
|
|
|
|
regs.x -= arg;
|
|
|
|
|
|
|
|
regs.status.carry = arg <= regs.x;
|
|
|
|
STATUS_UPDATE_NZ(regs.x);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DCP(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
uint8_t tmp = peek(arg) - 1;
|
|
|
|
memwrite(arg, tmp);
|
2024-07-04 12:11:53 -04:00
|
|
|
|
2024-07-04 14:17:40 -04:00
|
|
|
regs.status.carry = tmp <= regs.a;
|
2024-07-04 15:06:15 -04:00
|
|
|
STATUS_UPDATE_NZ(regs.a - tmp);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-07-04 15:06:15 -04:00
|
|
|
ISB(uint16_t arg)
|
2024-07-04 12:11:53 -04:00
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
INC(arg);
|
|
|
|
SBC(peek(arg));
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
KIL(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
/* TODO: figure out how to stop interpret(), I guess with global bool */
|
2024-07-04 12:11:53 -04:00
|
|
|
NOP(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LAR(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
regs.a = regs.x = regs.sp = regs.a & regs.sp;
|
|
|
|
|
|
|
|
STATUS_UPDATE_NZ(regs.a);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LAX(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
LDA(arg);
|
|
|
|
LDX(arg);
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RLA(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
ROL(arg);
|
2024-07-04 15:06:15 -04:00
|
|
|
AND(peek(arg));
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RRA(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
ROR(arg);
|
|
|
|
ADC(peek(arg));
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SLO(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
ASL(arg);
|
2024-07-04 15:06:15 -04:00
|
|
|
ORA(peek(arg));
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SRE(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
LSR(arg);
|
2024-07-04 15:06:15 -04:00
|
|
|
EOR(peek(arg));
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SXA(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
memwrite(arg, regs.x & ((arg & 0xFF00) + 1));
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SYA(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
memwrite(arg, regs.x & ((arg & 0xFF00) + 1));
|
2024-07-04 12:11:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XAA(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
/* TODO: apparently the exact operation is unknown */
|
2024-07-04 12:11:53 -04:00
|
|
|
NOP(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XAS(uint16_t arg)
|
|
|
|
{
|
2024-07-04 14:17:40 -04:00
|
|
|
regs.sp = regs.a & regs.x;
|
|
|
|
memwrite(arg, regs.sp & ((arg & 0xFF00) + 1));
|
2024-07-04 12:11:53 -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
|
|
|
{
|
2024-07-03 18:52:15 -04:00
|
|
|
uint8_t op;
|
2024-06-28 14:33:47 -04:00
|
|
|
uint16_t arg, mem, deref;
|
|
|
|
enum addressing_mode mode;
|
2024-05-20 10:09:19 -04:00
|
|
|
|
|
|
|
for (;;) {
|
2024-07-03 18:52:15 -04:00
|
|
|
printf("%04X ", regs.pc);
|
|
|
|
|
|
|
|
op = peek(regs.pc++);
|
|
|
|
|
|
|
|
printf("%02X", op);
|
|
|
|
for (uint8_t i = 0; i < opcodes[op].bytes - 1; i++)
|
|
|
|
printf(" %02X", peek(regs.pc + i));
|
2024-07-04 12:11:53 -04:00
|
|
|
|
|
|
|
if (opcodes[op].unofficial) {
|
|
|
|
if (opcodes[op].bytes == 1)
|
|
|
|
printf(" ");
|
|
|
|
else if (opcodes[op].bytes == 2)
|
|
|
|
printf(" ");
|
|
|
|
else if (opcodes[op].bytes == 3)
|
|
|
|
putchar(' ');
|
|
|
|
putchar('*');
|
|
|
|
} else
|
|
|
|
putchar('\t');
|
|
|
|
printf("%s ", opcodes[op].name);
|
2024-07-03 18:52:15 -04:00
|
|
|
|
|
|
|
mode = opcodes[op].mode;
|
|
|
|
arg = opcode_mem(mode);
|
2024-05-24 02:50:04 -04:00
|
|
|
|
2024-06-28 14:33:47 -04:00
|
|
|
switch (mode) {
|
|
|
|
case AM_IMM:
|
2024-07-03 18:52:15 -04:00
|
|
|
printf("#$%02X", arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
|
|
|
case AM_ZP:
|
2024-07-03 18:52:15 -04:00
|
|
|
printf("$%02X", arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
|
|
|
case AM_ZP_X:
|
2024-07-03 18:52:15 -04:00
|
|
|
printf("$%02X,X", arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
|
|
|
case AM_ZP_Y:
|
2024-07-03 18:52:15 -04:00
|
|
|
printf("$%02X,Y", arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
2024-06-29 09:51:49 -04:00
|
|
|
case AM_REL:
|
2024-06-28 14:33:47 -04:00
|
|
|
case AM_ABS:
|
2024-07-03 18:52:15 -04:00
|
|
|
printf("$%04X", arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
|
|
|
case AM_ABS_X:
|
2024-07-03 21:02:34 -04:00
|
|
|
printf("$%04X,X @ %04X", (uint16_t)(arg - regs.x), arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
|
|
|
case AM_ABS_Y:
|
2024-07-03 21:02:34 -04:00
|
|
|
printf("$%04X,Y @ %04X", (uint16_t)(arg - regs.y), arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
|
|
|
case AM_IND:
|
2024-07-03 21:02:34 -04:00
|
|
|
printf("($%04X) = %04X", arg, peek16(arg));
|
|
|
|
break;
|
|
|
|
case AM_IND_X:
|
|
|
|
printf("($%02X,X) @ %02X = %04X", peek(regs.pc - 1), peek(regs.pc - 1) + regs.x, arg);
|
|
|
|
break;
|
|
|
|
case AM_IND_Y:
|
|
|
|
printf("($%02X),Y = %04X @ %04X", peek(regs.pc - 1), (uint16_t)(arg - regs.y), arg);
|
2024-06-28 14:33:47 -04:00
|
|
|
break;
|
|
|
|
case AM_ACC:
|
2024-07-03 18:52:15 -04:00
|
|
|
case AM_NONE:
|
|
|
|
putchar('\t');
|
|
|
|
break;
|
2024-06-28 14:33:47 -04:00
|
|
|
default:
|
|
|
|
printf("\t\t\t\t");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-07-03 19:49:21 -04:00
|
|
|
if (opcodes[op].memread || opcodes[op].memwrite)
|
|
|
|
printf(" = %02X", peek(arg));
|
|
|
|
|
2024-07-03 21:02:34 -04:00
|
|
|
if (mode != AM_IND && mode != AM_ABS_X && mode != AM_ABS_Y)
|
2024-07-03 19:49:21 -04:00
|
|
|
putchar('\t');
|
2024-07-03 21:02:34 -04:00
|
|
|
if (mode != AM_IND_X && mode != AM_IND_Y)
|
|
|
|
printf("\t\t");
|
2024-07-03 18:52:15 -04:00
|
|
|
|
2024-07-03 19:49:21 -04:00
|
|
|
printf("A:%02X X:%02X Y:%02X P:%02X SP:%02X CYC:%d\n",
|
|
|
|
regs.a, regs.x, regs.y, STATUS_TO_INT(), regs.sp, cycles);
|
2024-07-03 18:52:15 -04:00
|
|
|
|
2024-07-03 19:49:21 -04:00
|
|
|
if (opcodes[op].memread)
|
|
|
|
opcodes[op].instr(peek(arg));
|
|
|
|
else
|
|
|
|
opcodes[op].instr(arg);
|
2024-07-04 11:18:38 -04:00
|
|
|
|
2024-07-03 18:52:15 -04:00
|
|
|
cycles += opcodes[op].cycles;
|
2024-07-04 11:18:38 -04:00
|
|
|
if (page_crossed) {
|
|
|
|
if (opcodes[op].page_cross)
|
|
|
|
cycles++;
|
|
|
|
page_crossed = false;
|
|
|
|
}
|
2024-05-20 10:09:19 -04:00
|
|
|
}
|
2024-06-28 14:33:47 -04:00
|
|
|
loop_exit:
|
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;
|
|
|
|
|
2024-06-17 05:16:00 -04:00
|
|
|
//memset(®s.status, 0, sizeof(regs.status));
|
2024-06-28 14:44:16 -04:00
|
|
|
regs.status.interrupt_disable = 1;
|
2024-06-04 06:24:09 -04:00
|
|
|
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
|
2024-06-17 05:16:00 -04:00
|
|
|
main(int argc, char *argv[])
|
2024-05-24 02:50:04 -04:00
|
|
|
{
|
2024-06-17 05:16:00 -04:00
|
|
|
FILE *fp;
|
|
|
|
uint8_t *buf;
|
|
|
|
size_t buflen;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s rom.nes\n", basename(argv[0]));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(argv[1], "r");
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
buflen = ftell(fp);
|
|
|
|
buf = calloc(1, buflen);
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
if (fread(buf, 1, buflen, fp) != buflen && ferror(fp)) {
|
|
|
|
fprintf(stderr, "file %s was not read properly\n", argv[1]);
|
|
|
|
clearerr(fp);
|
|
|
|
fclose(fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
parse_rom(buf, buflen, &rom);
|
|
|
|
free(buf);
|
|
|
|
|
2024-06-04 06:24:09 -04:00
|
|
|
cpu_init();
|
|
|
|
|
2024-06-17 05:16:00 -04:00
|
|
|
/* TODO: move to separate file? */
|
|
|
|
if (rom.mapper != 0) {
|
|
|
|
fprintf(stderr, "Only iNES ROMs using Mapper 0 are supported for now.\n");
|
2024-05-24 03:25:44 -04:00
|
|
|
return 1;
|
|
|
|
}
|
2024-06-08 08:11:06 -04:00
|
|
|
|
2024-06-17 05:41:45 -04:00
|
|
|
memwrite16(0xFFFC, 0xC000);
|
|
|
|
regs.pc = 0xC000;
|
2024-05-24 03:25:44 -04:00
|
|
|
|
2024-05-24 02:50:04 -04:00
|
|
|
interpret();
|
|
|
|
|
2024-06-28 14:33:47 -04:00
|
|
|
printf("\n$02 = %02X\n", memory[0x02]);
|
|
|
|
printf("$03 = %02X\n", memory[0x03]);
|
|
|
|
|
2024-06-17 05:16:00 -04:00
|
|
|
free_rom(&rom);
|
|
|
|
|
2024-05-24 02:50:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|