78b8838cc7
- Complete project skeleton per AGENTS.md (src/, bin/, samples/, attic/ with 1994 code, docs/) - High-performance C core (cpu.c) with most 6809 instructions + selected 6309 - Table-driven disassembler (D) and minimal built-in assembler (A) - Full C64-monitor inspired debugger (R ; M : D A G S B BC F L S ! X ?) - Single step, breakpoints (exec), register/memory view+edit - Robust S-Record, Intel HEX, raw binary load/save (tolerant of CRLF) - 6850 ACIA and 6821 PIA emulation (memory-mapped, registered devices) - CLI: -l load, -g go, -i interactive, --cpu 6309, --acia/--pia bases - Designed for tight integration with cc09 (WINTEK re-creation) - Optimized Makefile for macOS/Linux (clang/gcc, -O3 -flto -march=native) - AGENTS.md with mission, structure, coding rules, monitor reference Verified: builds release+debug, loads provided samples (140A.HEX 2299.HEX 2299.MIK), monitor commands, A/D/S/B, roundtrip save/load. Remote: http://gitea.binary-systems.com/vincente/mimic
1275 lines
47 KiB
C
1275 lines
47 KiB
C
/* cpu.c — mimic09 6809/6309 core interpreter
|
|
*
|
|
* Designed for speed on macOS/Linux.
|
|
* Single-instruction semantics first, then speed tuning.
|
|
*/
|
|
|
|
#include "cpu.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
/* One global CPU + 64KB flat memory */
|
|
cpu_t cpu;
|
|
uint8_t memory[65536];
|
|
|
|
/* Breakpoint maps (simple, fast enough for 64K) */
|
|
static uint8_t bp_exec[65536];
|
|
static uint8_t bp_watch[65536]; /* bit 0=read, 1=write */
|
|
|
|
/* Device hooks — filled in by acia.c / pia.c at init */
|
|
static uint8_t (*dev_read[4])(uint16_t off) = {0};
|
|
static void (*dev_write[4])(uint16_t off, uint8_t v) = {0};
|
|
static uint16_t dev_base[4] = {0};
|
|
static int dev_count = 0;
|
|
|
|
void cpu_register_device(uint16_t base, uint8_t (*r)(uint16_t), void (*w)(uint16_t,uint8_t))
|
|
{
|
|
if (dev_count < 4) {
|
|
dev_base[dev_count] = base;
|
|
dev_read[dev_count] = r;
|
|
dev_write[dev_count] = w;
|
|
dev_count++;
|
|
}
|
|
}
|
|
|
|
/* --- memory accessors ---------------------------------------------------- */
|
|
|
|
uint8_t cpu_read8(uint16_t addr)
|
|
{
|
|
/* watchpoint check (cheap) */
|
|
if (bp_watch[addr] & BP_READ) {
|
|
/* monitor will see this via a global or callback; for now just note */
|
|
cpu.stop_requested = true;
|
|
}
|
|
/* device check — small number, fast linear scan is fine */
|
|
for (int i = 0; i < dev_count; i++) {
|
|
uint16_t b = dev_base[i];
|
|
if (addr >= b && addr < (uint16_t)(b + 4)) { /* ACIA=2, PIA=4 typical */
|
|
if (dev_read[i]) return dev_read[i](addr - b);
|
|
return 0xFF;
|
|
}
|
|
}
|
|
return memory[addr];
|
|
}
|
|
|
|
void cpu_write8(uint16_t addr, uint8_t val)
|
|
{
|
|
if (bp_watch[addr] & BP_WRITE) {
|
|
cpu.stop_requested = true;
|
|
}
|
|
for (int i = 0; i < dev_count; i++) {
|
|
uint16_t b = dev_base[i];
|
|
if (addr >= b && addr < (uint16_t)(b + 4)) {
|
|
if (dev_write[i]) { dev_write[i](addr - b, val); return; }
|
|
}
|
|
}
|
|
memory[addr] = val;
|
|
}
|
|
|
|
uint16_t cpu_read16(uint16_t addr)
|
|
{
|
|
/* 16-bit read may straddle a device; be correct even if slow */
|
|
uint8_t hi = cpu_read8(addr);
|
|
uint8_t lo = cpu_read8((uint16_t)(addr + 1));
|
|
return (uint16_t)((hi << 8) | lo);
|
|
}
|
|
|
|
void cpu_write16(uint16_t addr, uint16_t val)
|
|
{
|
|
cpu_write8(addr, val >> 8);
|
|
cpu_write8((uint16_t)(addr + 1), val & 0xFF);
|
|
}
|
|
|
|
/* --- breakpoints --------------------------------------------------------- */
|
|
|
|
void bp_set_exec(uint16_t addr) { bp_exec[addr] = 1; }
|
|
void bp_clear_exec(uint16_t addr) { bp_exec[addr] = 0; }
|
|
void bp_clear_all(void)
|
|
{
|
|
memset(bp_exec, 0, sizeof(bp_exec));
|
|
memset(bp_watch, 0, sizeof(bp_watch));
|
|
}
|
|
bool bp_check_exec(uint16_t addr) { return bp_exec[addr] != 0; }
|
|
|
|
void bp_set_watch(uint16_t addr, int flags) { bp_watch[addr] |= (uint8_t)flags; }
|
|
void bp_clear_watch(uint16_t addr) { bp_watch[addr] = 0; }
|
|
|
|
/* --- interrupts (stubs for v1 — NMI/IRQ/FIRQ) --------------------------- */
|
|
|
|
void cpu_assert_irq(void) { /* TODO: set pending, check CC.I on entry */ }
|
|
void cpu_assert_firq(void) { /* TODO */ }
|
|
void cpu_assert_nmi(void) { /* TODO */ }
|
|
|
|
/* --- helpers ------------------------------------------------------------- */
|
|
|
|
static inline void push8(uint16_t *sp, uint8_t v) {
|
|
*sp = (*sp - 1) & 0xFFFF;
|
|
cpu_write8(*sp, v);
|
|
}
|
|
static inline uint8_t pull8(uint16_t *sp) {
|
|
uint8_t v = cpu_read8(*sp);
|
|
*sp = (*sp + 1) & 0xFFFF;
|
|
return v;
|
|
}
|
|
static inline void push16(uint16_t *sp, uint16_t v) {
|
|
push8(sp, v & 0xFF);
|
|
push8(sp, v >> 8);
|
|
}
|
|
static inline uint16_t pull16(uint16_t *sp) {
|
|
uint16_t hi = pull8(sp);
|
|
uint16_t lo = pull8(sp);
|
|
return (hi << 8) | lo;
|
|
}
|
|
|
|
/* Effective address calculation for indexed mode.
|
|
* Postbyte format documented in 6809 datasheet.
|
|
* Returns the EA; may auto-increment the register and consume extra bytes.
|
|
*/
|
|
static uint16_t ea_indexed(void)
|
|
{
|
|
uint8_t post = cpu_read8(cpu.pc++);
|
|
uint16_t ea = 0;
|
|
int reg = (post >> 5) & 3; /* 00=X, 01=Y, 10=U, 11=S */
|
|
uint16_t *rptr[4] = { &cpu.x, &cpu.y, &cpu.u, &cpu.s };
|
|
uint16_t *r = rptr[reg];
|
|
|
|
if (post & 0x80) {
|
|
/* indirect or special */
|
|
int indir = (post & 0x10) != 0;
|
|
switch (post & 0x0F) {
|
|
case 0x00: /* ,R+ */
|
|
ea = *r; *r = (*r + 1) & 0xFFFF;
|
|
break;
|
|
case 0x01: /* ,R++ */
|
|
ea = *r; *r = (*r + 2) & 0xFFFF;
|
|
break;
|
|
case 0x02: /* ,-R */
|
|
*r = (*r - 1) & 0xFFFF; ea = *r;
|
|
break;
|
|
case 0x03: /* ,--R */
|
|
*r = (*r - 2) & 0xFFFF; ea = *r;
|
|
break;
|
|
case 0x04: /* ,R */
|
|
ea = *r;
|
|
break;
|
|
case 0x05: /* B,R */
|
|
ea = (*r + (int8_t)cpu.d.ab.b) & 0xFFFF;
|
|
break;
|
|
case 0x06: /* A,R */
|
|
ea = (*r + (int8_t)cpu.d.ab.a) & 0xFFFF;
|
|
break;
|
|
case 0x07: /* E,R (6309) or illegal on 6809 */
|
|
if (cpu.model == CPU_6309) ea = (*r + (int8_t)cpu.e) & 0xFFFF;
|
|
else ea = *r; /* treat as ,R on 6809 for tolerance */
|
|
break;
|
|
case 0x08: /* n,R (8-bit signed) */
|
|
ea = (*r + (int8_t)cpu_read8(cpu.pc++)) & 0xFFFF;
|
|
break;
|
|
case 0x09: /* n,R (16-bit) */
|
|
ea = (*r + (int16_t)cpu_read16(cpu.pc)) & 0xFFFF;
|
|
cpu.pc += 2;
|
|
break;
|
|
case 0x0A: /* F,R (6309) */
|
|
if (cpu.model == CPU_6309) ea = (*r + (int8_t)cpu.f) & 0xFFFF;
|
|
else ea = *r;
|
|
break;
|
|
case 0x0B: /* D,R */
|
|
ea = (*r + cpu.d.d) & 0xFFFF;
|
|
break;
|
|
case 0x0C: /* n,PCR (8-bit relative to PC after postbyte) */
|
|
ea = (cpu.pc + (int8_t)cpu_read8(cpu.pc++)) & 0xFFFF;
|
|
break;
|
|
case 0x0D: /* n,PCR (16-bit) */
|
|
ea = (cpu.pc + (int16_t)cpu_read16(cpu.pc)) & 0xFFFF;
|
|
cpu.pc += 2;
|
|
break;
|
|
case 0x0E: /* W,R (6309) or ? */
|
|
if (cpu.model == CPU_6309) {
|
|
uint16_t w = (uint16_t)((cpu.e << 8) | cpu.f);
|
|
ea = (*r + w) & 0xFFFF;
|
|
} else ea = *r;
|
|
break;
|
|
case 0x0F: /* [n] extended indirect (postbyte 0x9F on 6809/6309) handled below */
|
|
if ((post & 0x1F) == 0x9F) {
|
|
ea = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
indir = 1; /* force indirect */
|
|
} else {
|
|
ea = cpu.pc; /* fallback */
|
|
}
|
|
break;
|
|
default:
|
|
ea = *r;
|
|
break;
|
|
}
|
|
if (indir) {
|
|
ea = cpu_read16(ea);
|
|
}
|
|
} else {
|
|
/* 5-bit signed offset ,R */
|
|
int off = post & 0x1F;
|
|
if (off & 0x10) off |= 0xFFFFFFE0; /* sign extend 5->32 */
|
|
ea = (*r + off) & 0xFFFF;
|
|
}
|
|
return ea;
|
|
}
|
|
|
|
/* --- condition evaluation for branches ---------------------------------- */
|
|
|
|
static inline bool cond(unsigned op)
|
|
{
|
|
/* op is the low nibble of the branch opcode */
|
|
bool n = cc_test(CC_N);
|
|
bool v = cc_test(CC_V);
|
|
bool z = cc_test(CC_Z);
|
|
bool c = cc_test(CC_C);
|
|
switch (op & 0x0F) {
|
|
case 0x0: return true; /* BRA / LBRA */
|
|
case 0x1: return false; /* BRN / LBRN */
|
|
case 0x2: return !(c | z); /* BHI */
|
|
case 0x3: return (c | z); /* BLS */
|
|
case 0x4: return !c; /* BCC / BHS */
|
|
case 0x5: return c; /* BCS / BLO */
|
|
case 0x6: return !z; /* BNE */
|
|
case 0x7: return z; /* BEQ */
|
|
case 0x8: return !v; /* BVC */
|
|
case 0x9: return v; /* BVS */
|
|
case 0xA: return !n; /* BPL */
|
|
case 0xB: return n; /* BMI */
|
|
case 0xC: return (n == v); /* BGE */
|
|
case 0xD: return (n != v); /* BLT */
|
|
case 0xE: return (n == v) && !z; /* BGT */
|
|
case 0xF: return (n != v) || z; /* BLE */
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/* --- illegal instruction ------------------------------------------------- */
|
|
|
|
static void illegal(uint8_t op)
|
|
{
|
|
(void)op;
|
|
cpu.halted = true;
|
|
cpu.stop_requested = true;
|
|
}
|
|
|
|
/* --- the big step function ---------------------------------------------- */
|
|
|
|
int cpu_step(void)
|
|
{
|
|
if (cpu.halted) return 0;
|
|
|
|
uint16_t pc0 = cpu.pc;
|
|
if (cpu.debug_mode && bp_check_exec(pc0)) {
|
|
cpu.stop_requested = true;
|
|
return 0;
|
|
}
|
|
|
|
uint8_t op = cpu_read8(cpu.pc++);
|
|
int cycles = 1; /* base; most are higher — we approximate */
|
|
|
|
switch (op) {
|
|
|
|
/* ---------------- $00-$0F direct / inherent mostly ------------------ */
|
|
case 0x00: { /* NEG direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
cpu.cc &= ~(CC_C | CC_V | CC_N | CC_Z);
|
|
if (m == 0x80) cc_set(CC_V);
|
|
if (m == 0x00) cc_set(CC_C);
|
|
m = (~m + 1) & 0xFF;
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
if (m == 0) cc_set(CC_Z);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x03: { /* COM direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = ~cpu_read8(ea);
|
|
cpu_write8(ea, m);
|
|
cc_clear(CC_V);
|
|
cc_set(CC_C);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x04: { /* LSR direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
cc_clear(CC_N);
|
|
bool c = m & 1; if (c) cc_set(CC_C); else cc_clear(CC_C);
|
|
m >>= 1;
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x06: { /* ROR direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
bool oldc = cc_test(CC_C);
|
|
bool newc = m & 1; if (newc) cc_set(CC_C); else cc_clear(CC_C);
|
|
m = (m >> 1) | (oldc ? 0x80 : 0);
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x07: { /* ASR direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
bool c = m & 1; if (c) cc_set(CC_C); else cc_clear(CC_C);
|
|
m = (uint8_t)((int8_t)m >> 1);
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x08: { /* ASL / LSL direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
bool c = (m & 0x80) != 0; if (c) cc_set(CC_C); else cc_clear(CC_C);
|
|
bool v = ((m & 0xC0) == 0xC0) || ((m & 0xC0) == 0x00);
|
|
if (v) cc_set(CC_V); else cc_clear(CC_V); /* V = b7 xor b6 before shift */
|
|
m <<= 1;
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x09: { /* ROL direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
bool oldc = cc_test(CC_C);
|
|
bool c = (m & 0x80) != 0; if (c) cc_set(CC_C); else cc_clear(CC_C);
|
|
bool v = ((m & 0xC0) == 0xC0) || ((m & 0xC0) == 0x00);
|
|
if (v) cc_set(CC_V); else cc_clear(CC_V);
|
|
m = (m << 1) | (oldc ? 1 : 0);
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x0A: { /* DEC direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
bool v = (m == 0x80); if (v) cc_set(CC_V); else cc_clear(CC_V);
|
|
m = (m - 1) & 0xFF;
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x0C: { /* INC direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ea = ad;
|
|
uint8_t m = cpu_read8(ea);
|
|
bool v = (m == 0x7F); if (v) cc_set(CC_V); else cc_clear(CC_V);
|
|
m = (m + 1) & 0xFF;
|
|
cpu_write8(ea, m);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x0D: { /* TST direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint8_t m = cpu_read8(ad);
|
|
cc_clear(CC_V);
|
|
set_nz8(m);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
case 0x0E: { /* JMP direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
cpu.pc = ad;
|
|
cycles = 3;
|
|
break;
|
|
}
|
|
case 0x0F: { /* CLR direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
cpu_write8(ad, 0);
|
|
cc_clear(CC_N | CC_V | CC_C);
|
|
cc_set(CC_Z);
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
|
|
/* ---------------- inherent / immediate ------------------------------ */
|
|
case 0x12: /* NOP */ cycles = 2; break;
|
|
case 0x13: /* SYNC */ cpu.halted = true; cycles = 2; break; /* simplistic */
|
|
case 0x16: { /* LBRA */
|
|
int16_t off = (int16_t)cpu_read16(cpu.pc); cpu.pc += 2;
|
|
cpu.pc = (cpu.pc + off) & 0xFFFF;
|
|
cycles = 5;
|
|
break;
|
|
}
|
|
case 0x17: { /* LBSR */
|
|
int16_t off = (int16_t)cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint16_t ret = cpu.pc;
|
|
push16(&cpu.s, ret);
|
|
cpu.pc = (cpu.pc + off) & 0xFFFF;
|
|
cycles = 9;
|
|
break;
|
|
}
|
|
case 0x19: { /* DAA */
|
|
uint8_t a = cpu.d.ab.a;
|
|
uint8_t correction = 0;
|
|
bool c = cc_test(CC_C);
|
|
if ((a & 0x0F) > 9 || cc_test(CC_H)) correction |= 0x06;
|
|
if (a > 0x99 || c) { correction |= 0x60; cc_set(CC_C); }
|
|
a = (a + correction) & 0xFF;
|
|
cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2;
|
|
break;
|
|
}
|
|
case 0x1A: { /* ORCC #imm */
|
|
uint8_t m = cpu_read8(cpu.pc++);
|
|
cpu.cc |= m;
|
|
cycles = 3;
|
|
break;
|
|
}
|
|
case 0x1C: { /* ANDCC #imm */
|
|
uint8_t m = cpu_read8(cpu.pc++);
|
|
cpu.cc &= m;
|
|
cycles = 3;
|
|
break;
|
|
}
|
|
case 0x1D: { /* SEX */
|
|
cpu.d.ab.a = (cpu.d.ab.b & 0x80) ? 0xFF : 0x00;
|
|
set_nz16(cpu.d.d);
|
|
cycles = 2;
|
|
break;
|
|
}
|
|
case 0x1E: { /* EXG post */
|
|
uint8_t p = cpu_read8(cpu.pc++);
|
|
int r1 = (p >> 4) & 0xF;
|
|
int r2 = p & 0xF;
|
|
/* simplified: only common pairs */
|
|
uint16_t t16;
|
|
uint8_t t8;
|
|
/* 8<->8 or 16<->16 only for now (full list is larger) */
|
|
if ((r1 <= 5 && r2 <= 5) || (r1 >=8 && r2 >=8)) {
|
|
/* treat as 16-bit pairs for X/Y/U/S/D/PC etc. */
|
|
uint16_t *map16[16] = { &cpu.d.d, &cpu.x, &cpu.y, &cpu.u, &cpu.s, &cpu.pc, 0,0, 0,0,0,0,0,0,0,0 };
|
|
if (map16[r1] && map16[r2]) {
|
|
t16 = *map16[r1]; *map16[r1] = *map16[r2]; *map16[r2] = t16;
|
|
}
|
|
} else {
|
|
/* 8-bit A/B/CC/DP etc. — minimal */
|
|
uint8_t *map8[16] = { &cpu.d.ab.a, &cpu.d.ab.b, &cpu.cc, 0,0,0,0,0, 0,0,0,0,0,0,0,0 };
|
|
if (map8[r1] && map8[r2]) {
|
|
t8 = *map8[r1]; *map8[r1] = *map8[r2]; *map8[r2] = t8;
|
|
}
|
|
}
|
|
cycles = 8;
|
|
break;
|
|
}
|
|
case 0x1F: { /* TFR post */
|
|
uint8_t p = cpu_read8(cpu.pc++);
|
|
int src = (p >> 4) & 0xF;
|
|
int dst = p & 0xF;
|
|
uint16_t *m16[6] = { &cpu.d.d, &cpu.x, &cpu.y, &cpu.u, &cpu.s, &cpu.pc };
|
|
uint8_t *m8[3] = { &cpu.d.ab.a, &cpu.d.ab.b, &cpu.cc };
|
|
if (src < 6 && dst < 6 && m16[src] && m16[dst]) *m16[dst] = *m16[src];
|
|
else if (src < 3 && dst < 3 && m8[src] && m8[dst]) *m8[dst] = *m8[src];
|
|
else if (src < 6 && dst < 3 && m16[src] && m8[dst]) *m8[dst] = (uint8_t)*m16[src];
|
|
else if (src < 3 && dst < 6 && m8[src] && m16[dst]) *m16[dst] = *m8[src];
|
|
cycles = 6;
|
|
break;
|
|
}
|
|
|
|
/* short branches */
|
|
case 0x20: case 0x21: case 0x22: case 0x23:
|
|
case 0x24: case 0x25: case 0x26: case 0x27:
|
|
case 0x28: case 0x29: case 0x2A: case 0x2B:
|
|
case 0x2C: case 0x2D: case 0x2E: case 0x2F: {
|
|
int8_t off = (int8_t)cpu_read8(cpu.pc++);
|
|
if (cond(op)) cpu.pc = (cpu.pc + off) & 0xFFFF;
|
|
cycles = 3;
|
|
break;
|
|
}
|
|
|
|
/* ---------------- stack / rts / jsr etc ----------------------------- */
|
|
case 0x34: { /* PSHS post */
|
|
uint8_t post = cpu_read8(cpu.pc++);
|
|
if (post & 0x80) push16(&cpu.s, cpu.pc);
|
|
if (post & 0x40) push16(&cpu.s, cpu.u);
|
|
if (post & 0x20) push16(&cpu.s, cpu.y);
|
|
if (post & 0x10) push16(&cpu.s, cpu.x);
|
|
if (post & 0x08) push8(&cpu.s, cpu.d.ab.a);
|
|
if (post & 0x04) push8(&cpu.s, cpu.d.ab.b);
|
|
if (post & 0x02) push8(&cpu.s, cpu.cc);
|
|
if (post & 0x01) push8(&cpu.s, 0 /* DP not modeled */);
|
|
cycles = 5 + __builtin_popcount(post); /* rough */
|
|
break;
|
|
}
|
|
case 0x35: { /* PULS post */
|
|
uint8_t post = cpu_read8(cpu.pc++);
|
|
if (post & 0x01) (void)pull8(&cpu.s);
|
|
if (post & 0x02) cpu.cc = pull8(&cpu.s);
|
|
if (post & 0x04) cpu.d.ab.b = pull8(&cpu.s);
|
|
if (post & 0x08) cpu.d.ab.a = pull8(&cpu.s);
|
|
if (post & 0x10) cpu.x = pull16(&cpu.s);
|
|
if (post & 0x20) cpu.y = pull16(&cpu.s);
|
|
if (post & 0x40) cpu.u = pull16(&cpu.s);
|
|
if (post & 0x80) cpu.pc = pull16(&cpu.s);
|
|
cycles = 5 + __builtin_popcount(post);
|
|
break;
|
|
}
|
|
case 0x36: { /* PSHU */
|
|
uint8_t post = cpu_read8(cpu.pc++);
|
|
if (post & 0x80) push16(&cpu.u, cpu.pc);
|
|
if (post & 0x40) push16(&cpu.u, cpu.s);
|
|
if (post & 0x20) push16(&cpu.u, cpu.y);
|
|
if (post & 0x10) push16(&cpu.u, cpu.x);
|
|
if (post & 0x08) push8(&cpu.u, cpu.d.ab.a);
|
|
if (post & 0x04) push8(&cpu.u, cpu.d.ab.b);
|
|
if (post & 0x02) push8(&cpu.u, cpu.cc);
|
|
cycles = 5 + __builtin_popcount(post);
|
|
break;
|
|
}
|
|
case 0x37: { /* PULU */
|
|
uint8_t post = cpu_read8(cpu.pc++);
|
|
if (post & 0x01) (void)pull8(&cpu.u);
|
|
if (post & 0x02) cpu.cc = pull8(&cpu.u);
|
|
if (post & 0x04) cpu.d.ab.b = pull8(&cpu.u);
|
|
if (post & 0x08) cpu.d.ab.a = pull8(&cpu.u);
|
|
if (post & 0x10) cpu.x = pull16(&cpu.u);
|
|
if (post & 0x20) cpu.y = pull16(&cpu.u);
|
|
if (post & 0x40) cpu.s = pull16(&cpu.u);
|
|
if (post & 0x80) cpu.pc = pull16(&cpu.u);
|
|
cycles = 5 + __builtin_popcount(post);
|
|
break;
|
|
}
|
|
case 0x39: /* RTS */
|
|
cpu.pc = pull16(&cpu.s);
|
|
cycles = 5;
|
|
break;
|
|
case 0x3A: /* ABX */
|
|
cpu.x = (cpu.x + cpu.d.ab.b) & 0xFFFF;
|
|
cycles = 3;
|
|
break;
|
|
case 0x3B: /* RTI */
|
|
cpu.cc = pull8(&cpu.s);
|
|
if (cpu.cc & CC_E) {
|
|
cpu.d.ab.b = pull8(&cpu.s);
|
|
cpu.d.ab.a = pull8(&cpu.s);
|
|
cpu.x = pull16(&cpu.s);
|
|
cpu.y = pull16(&cpu.s);
|
|
cpu.u = pull16(&cpu.s);
|
|
cpu.pc = pull16(&cpu.s);
|
|
} else {
|
|
cpu.pc = pull16(&cpu.s);
|
|
}
|
|
cycles = 6;
|
|
break;
|
|
case 0x3D: /* MUL */
|
|
cpu.d.d = (uint16_t)cpu.d.ab.a * cpu.d.ab.b;
|
|
if (cpu.d.ab.b & 0x80) cc_set(CC_C); else cc_clear(CC_C);
|
|
if (cpu.d.d == 0) cc_set(CC_Z); else cc_clear(CC_Z);
|
|
cycles = 11;
|
|
break;
|
|
case 0x3F: /* SWI */
|
|
cc_set(CC_E);
|
|
push16(&cpu.s, cpu.pc);
|
|
push16(&cpu.s, cpu.u);
|
|
push16(&cpu.s, cpu.y);
|
|
push16(&cpu.s, cpu.x);
|
|
push8(&cpu.s, cpu.d.ab.a);
|
|
push8(&cpu.s, cpu.d.ab.b);
|
|
push8(&cpu.s, cpu.cc);
|
|
cc_set(CC_I | CC_F);
|
|
cpu.pc = cpu_read16(0xFFFA);
|
|
cycles = 19;
|
|
break;
|
|
|
|
/* ---------------- accumulator inherent ------------------------------ */
|
|
case 0x40: /* NEGA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
cc_clear(CC_C | CC_V);
|
|
if (a == 0x80) cc_set(CC_V);
|
|
if (a == 0) cc_set(CC_C);
|
|
a = (~a + 1) & 0xFF;
|
|
cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x43: /* COMA */ {
|
|
cpu.d.ab.a = ~cpu.d.ab.a;
|
|
cc_clear(CC_V); cc_set(CC_C);
|
|
set_nz8(cpu.d.ab.a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x44: /* LSRA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
cc_clear(CC_N);
|
|
if (a & 1) cc_set(CC_C); else cc_clear(CC_C);
|
|
a >>= 1; cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x46: /* RORA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
bool oldc = cc_test(CC_C);
|
|
if (a & 1) cc_set(CC_C); else cc_clear(CC_C);
|
|
a = (a >> 1) | (oldc ? 0x80 : 0);
|
|
cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x47: /* ASRA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
if (a & 1) cc_set(CC_C); else cc_clear(CC_C);
|
|
a = (uint8_t)((int8_t)a >> 1);
|
|
cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x48: /* ASLA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
if (a & 0x80) cc_set(CC_C); else cc_clear(CC_C);
|
|
bool v = ((a & 0xC0) == 0xC0) || ((a & 0xC0) == 0x00);
|
|
if (v) cc_set(CC_V); else cc_clear(CC_V);
|
|
a <<= 1; cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x49: /* ROLA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
bool oldc = cc_test(CC_C);
|
|
if (a & 0x80) cc_set(CC_C); else cc_clear(CC_C);
|
|
bool v = ((a & 0xC0) == 0xC0) || ((a & 0xC0) == 0x00);
|
|
if (v) cc_set(CC_V); else cc_clear(CC_V);
|
|
a = (a << 1) | (oldc ? 1 : 0);
|
|
cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x4A: /* DECA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
if (a == 0x80) cc_set(CC_V); else cc_clear(CC_V);
|
|
a = (a - 1) & 0xFF; cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x4C: /* INCA */ {
|
|
uint8_t a = cpu.d.ab.a;
|
|
if (a == 0x7F) cc_set(CC_V); else cc_clear(CC_V);
|
|
a = (a + 1) & 0xFF; cpu.d.ab.a = a;
|
|
set_nz8(a);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x4D: /* TSTA */ cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles = 2; break;
|
|
case 0x4F: /* CLRA */ cpu.d.ab.a = 0; cc_clear(CC_N|CC_V|CC_C); cc_set(CC_Z); cycles = 2; break;
|
|
|
|
/* B accumulator */
|
|
case 0x50: /* NEGB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
cc_clear(CC_C | CC_V);
|
|
if (b == 0x80) cc_set(CC_V);
|
|
if (b == 0) cc_set(CC_C);
|
|
b = (~b + 1) & 0xFF; cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x53: /* COMB */ {
|
|
cpu.d.ab.b = ~cpu.d.ab.b;
|
|
cc_clear(CC_V); cc_set(CC_C);
|
|
set_nz8(cpu.d.ab.b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x54: /* LSRB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
cc_clear(CC_N);
|
|
if (b & 1) cc_set(CC_C); else cc_clear(CC_C);
|
|
b >>= 1; cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x56: /* RORB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
bool oldc = cc_test(CC_C);
|
|
if (b & 1) cc_set(CC_C); else cc_clear(CC_C);
|
|
b = (b >> 1) | (oldc ? 0x80 : 0);
|
|
cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x57: /* ASRB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
if (b & 1) cc_set(CC_C); else cc_clear(CC_C);
|
|
b = (uint8_t)((int8_t)b >> 1);
|
|
cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x58: /* ASLB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
if (b & 0x80) cc_set(CC_C); else cc_clear(CC_C);
|
|
bool v = ((b & 0xC0) == 0xC0) || ((b & 0xC0) == 0x00);
|
|
if (v) cc_set(CC_V); else cc_clear(CC_V);
|
|
b <<= 1; cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x59: /* ROLB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
bool oldc = cc_test(CC_C);
|
|
if (b & 0x80) cc_set(CC_C); else cc_clear(CC_C);
|
|
bool v = ((b & 0xC0) == 0xC0) || ((b & 0xC0) == 0x00);
|
|
if (v) cc_set(CC_V); else cc_clear(CC_V);
|
|
b = (b << 1) | (oldc ? 1 : 0);
|
|
cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x5A: /* DECB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
if (b == 0x80) cc_set(CC_V); else cc_clear(CC_V);
|
|
b = (b - 1) & 0xFF; cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x5C: /* INCB */ {
|
|
uint8_t b = cpu.d.ab.b;
|
|
if (b == 0x7F) cc_set(CC_V); else cc_clear(CC_V);
|
|
b = (b + 1) & 0xFF; cpu.d.ab.b = b;
|
|
set_nz8(b);
|
|
cycles = 2; break;
|
|
}
|
|
case 0x5D: /* TSTB */ cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles = 2; break;
|
|
case 0x5F: /* CLRB */ cpu.d.ab.b = 0; cc_clear(CC_N|CC_V|CC_C); cc_set(CC_Z); cycles = 2; break;
|
|
|
|
/* ---------------- indexed (0x6x,0xAx,0xEx) -------------------------- */
|
|
case 0x60: case 0x63: case 0x64: case 0x66: case 0x67:
|
|
case 0x68: case 0x69: case 0x6A: case 0x6C: case 0x6D: case 0x6E: case 0x6F: {
|
|
uint16_t ea = ea_indexed();
|
|
uint8_t m = cpu_read8(ea);
|
|
switch (op) {
|
|
case 0x60: { /* NEG indexed */
|
|
cc_clear(CC_C|CC_V); if (m==0x80)cc_set(CC_V); if (m==0)cc_set(CC_C);
|
|
m = (~m + 1)&0xFF; cpu_write8(ea, m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x63: { /* COM indexed */
|
|
m = ~m; cpu_write8(ea,m); cc_clear(CC_V); cc_set(CC_C); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x64: { /* LSR indexed */
|
|
cc_clear(CC_N); if (m&1)cc_set(CC_C); m>>=1; cpu_write8(ea,m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x66: { /* ROR indexed */
|
|
bool oldc=cc_test(CC_C); if (m&1)cc_set(CC_C);else cc_clear(CC_C);
|
|
m=(m>>1)|(oldc?0x80:0); cpu_write8(ea,m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x67: { /* ASR indexed */
|
|
if (m&1)cc_set(CC_C);else cc_clear(CC_C); m=(uint8_t)((int8_t)m>>1);
|
|
cpu_write8(ea,m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x68: { /* ASL indexed */
|
|
if (m&0x80)cc_set(CC_C);else cc_clear(CC_C);
|
|
bool v=((m&0xC0)==0xC0)||((m&0xC0)==0x00); if(v)cc_set(CC_V);else cc_clear(CC_V);
|
|
m<<=1; cpu_write8(ea,m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x69: { /* ROL indexed */
|
|
bool oldc=cc_test(CC_C); if (m&0x80)cc_set(CC_C);else cc_clear(CC_C);
|
|
bool v=((m&0xC0)==0xC0)||((m&0xC0)==0x00); if(v)cc_set(CC_V);else cc_clear(CC_V);
|
|
m=(m<<1)|(oldc?1:0); cpu_write8(ea,m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x6A: { /* DEC indexed */
|
|
if (m==0x80)cc_set(CC_V);else cc_clear(CC_V); m=(m-1)&0xFF; cpu_write8(ea,m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x6C: { /* INC indexed */
|
|
if (m==0x7F)cc_set(CC_V);else cc_clear(CC_V); m=(m+1)&0xFF; cpu_write8(ea,m); set_nz8(m); cycles=6; break;
|
|
}
|
|
case 0x6D: /* TST indexed */ cc_clear(CC_V); set_nz8(m); cycles=6; break;
|
|
case 0x6E: /* JMP indexed */ cpu.pc = ea; cycles=3; break;
|
|
case 0x6F: /* CLR indexed */ cpu_write8(ea,0); cc_clear(CC_N|CC_V|CC_C); cc_set(CC_Z); cycles=6; break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* ---------------- extended (0x7x, 0xBx, 0xFx) ----------------------- */
|
|
case 0x70: case 0x73: case 0x74: case 0x76: case 0x77: case 0x78:
|
|
case 0x79: case 0x7A: case 0x7C: case 0x7D: case 0x7E: case 0x7F: {
|
|
uint16_t ea = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint8_t m = cpu_read8(ea);
|
|
switch (op) {
|
|
case 0x70: { /* NEG extended */
|
|
cc_clear(CC_C|CC_V); if (m==0x80)cc_set(CC_V); if (m==0)cc_set(CC_C);
|
|
m=(~m+1)&0xFF; cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x73: { /* COM ext */
|
|
m=~m; cpu_write8(ea,m); cc_clear(CC_V); cc_set(CC_C); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x74: { /* LSR ext */
|
|
cc_clear(CC_N); if (m&1)cc_set(CC_C); m>>=1; cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x76: { /* ROR ext */ bool oldc=cc_test(CC_C); if (m&1)cc_set(CC_C);else cc_clear(CC_C);
|
|
m=(m>>1)|(oldc?0x80:0); cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x77: { /* ASR ext */ if (m&1)cc_set(CC_C);else cc_clear(CC_C); m=(uint8_t)((int8_t)m>>1);
|
|
cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x78: { /* ASL ext */ if (m&0x80)cc_set(CC_C);else cc_clear(CC_C);
|
|
bool v=((m&0xC0)==0xC0)||((m&0xC0)==0x00); if(v)cc_set(CC_V);else cc_clear(CC_V);
|
|
m<<=1; cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x79: { /* ROL ext */ bool oldc=cc_test(CC_C); if (m&0x80)cc_set(CC_C);else cc_clear(CC_C);
|
|
bool v=((m&0xC0)==0xC0)||((m&0xC0)==0x00); if(v)cc_set(CC_V);else cc_clear(CC_V);
|
|
m=(m<<1)|(oldc?1:0); cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x7A: { /* DEC ext */ if (m==0x80)cc_set(CC_V);else cc_clear(CC_V); m=(m-1)&0xFF; cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x7C: { /* INC ext */ if (m==0x7F)cc_set(CC_V);else cc_clear(CC_V); m=(m+1)&0xFF; cpu_write8(ea,m); set_nz8(m); cycles=7; break;
|
|
}
|
|
case 0x7D: /* TST ext */ cc_clear(CC_V); set_nz8(m); cycles=7; break;
|
|
case 0x7E: /* JMP ext */ cpu.pc = ea; cycles=4; break;
|
|
case 0x7F: /* CLR ext */ cpu_write8(ea,0); cc_clear(CC_N|CC_V|CC_C); cc_set(CC_Z); cycles=7; break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* ---------------- $8x immediate 8-bit ops --------------------------- */
|
|
case 0x80: { /* SUBA # */
|
|
uint8_t m = cpu_read8(cpu.pc++); uint8_t a = cpu.d.ab.a;
|
|
uint16_t r = a - m; cpu.d.ab.a = r & 0xFF;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x100) cc_set(CC_C);
|
|
if (((a ^ m ^ r) & 0x80) && ((a ^ r) & 0x80)) cc_set(CC_V);
|
|
set_nz8(cpu.d.ab.a);
|
|
cycles=2; break;
|
|
}
|
|
case 0x81: { /* CMPA # */
|
|
uint8_t m = cpu_read8(cpu.pc++); uint8_t a = cpu.d.ab.a;
|
|
uint16_t r = a - m;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x100) cc_set(CC_C);
|
|
if (((a ^ m ^ r) & 0x80) && ((a ^ r) & 0x80)) cc_set(CC_V);
|
|
set_nz8(r & 0xFF);
|
|
cycles=2; break;
|
|
}
|
|
case 0x82: { /* SBCA # */
|
|
uint8_t m = cpu_read8(cpu.pc++); uint8_t a = cpu.d.ab.a;
|
|
uint16_t r = a - m - (cc_test(CC_C)?1:0);
|
|
cpu.d.ab.a = r & 0xFF;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x100) cc_set(CC_C);
|
|
if (((a ^ m ^ r) & 0x80) && ((a ^ r) & 0x80)) cc_set(CC_V);
|
|
set_nz8(cpu.d.ab.a);
|
|
cycles=2; break;
|
|
}
|
|
case 0x83: { /* SUBD # */
|
|
uint16_t m = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint32_t r = cpu.d.d - m;
|
|
cpu.d.d = r & 0xFFFF;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x10000) cc_set(CC_C);
|
|
if (((cpu.d.d ^ m ^ (r&0xFFFF)) & 0x8000) && ((cpu.d.d ^ (r&0xFFFF)) & 0x8000)) cc_set(CC_V);
|
|
set_nz16(cpu.d.d);
|
|
cycles=4; break;
|
|
}
|
|
case 0x84: { /* ANDA # */
|
|
cpu.d.ab.a &= cpu_read8(cpu.pc++);
|
|
cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=2; break;
|
|
}
|
|
case 0x85: { /* BITA # */
|
|
uint8_t t = cpu.d.ab.a & cpu_read8(cpu.pc++);
|
|
cc_clear(CC_V); set_nz8(t); cycles=2; break;
|
|
}
|
|
case 0x86: /* LDA # */
|
|
cpu.d.ab.a = cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=2; break;
|
|
case 0x88: { /* EORA # */
|
|
cpu.d.ab.a ^= cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=2; break;
|
|
}
|
|
case 0x89: { /* ADCA # */
|
|
uint8_t m = cpu_read8(cpu.pc++); uint8_t a = cpu.d.ab.a; uint8_t c = cc_test(CC_C)?1:0;
|
|
uint16_t r = a + m + c;
|
|
cpu.d.ab.a = r & 0xFF;
|
|
cc_clear(CC_C|CC_H|CC_V|CC_N|CC_Z);
|
|
if (r & 0x100) cc_set(CC_C);
|
|
if ((a ^ m ^ r) & 0x10) cc_set(CC_H);
|
|
if (((a ^ m ^ r) & 0x80) && ~((a ^ r) & 0x80)) cc_set(CC_V); /* approx */
|
|
set_nz8(cpu.d.ab.a);
|
|
cycles=2; break;
|
|
}
|
|
case 0x8A: { /* ORA # */
|
|
cpu.d.ab.a |= cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=2; break;
|
|
}
|
|
case 0x8B: { /* ADDA # */
|
|
uint8_t m = cpu_read8(cpu.pc++); uint8_t a = cpu.d.ab.a;
|
|
uint16_t r = a + m;
|
|
cpu.d.ab.a = r & 0xFF;
|
|
cc_clear(CC_C|CC_H|CC_V|CC_N|CC_Z);
|
|
if (r & 0x100) cc_set(CC_C);
|
|
if ((a ^ m ^ r) & 0x10) cc_set(CC_H);
|
|
if (((a ^ m ^ r) & 0x80) && ~((a ^ r) & 0x80)) cc_set(CC_V);
|
|
set_nz8(cpu.d.ab.a);
|
|
cycles=2; break;
|
|
}
|
|
case 0x8C: { /* CMPX # */
|
|
uint16_t m = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint32_t r = cpu.x - m;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x10000) cc_set(CC_C);
|
|
if (((cpu.x ^ m ^ (r&0xFFFF)) & 0x8000) && ((cpu.x ^ (r&0xFFFF)) & 0x8000)) cc_set(CC_V);
|
|
set_nz16((uint16_t)r);
|
|
cycles=4; break;
|
|
}
|
|
case 0x8D: { /* BSR */
|
|
int8_t off = (int8_t)cpu_read8(cpu.pc++);
|
|
uint16_t ret = cpu.pc;
|
|
push16(&cpu.s, ret);
|
|
cpu.pc = (cpu.pc + off) & 0xFFFF;
|
|
cycles = 7; break;
|
|
}
|
|
case 0x8E: /* LDX # */
|
|
cpu.x = cpu_read16(cpu.pc); cpu.pc += 2; cc_clear(CC_V); set_nz16(cpu.x); cycles=3; break;
|
|
|
|
/* ---------------- $9x direct --------------------------------------- */
|
|
case 0x96: { /* LDA direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu.d.ab.a = cpu_read8(ad);
|
|
cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=4; break;
|
|
}
|
|
case 0x97: { /* STA direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu_write8(ad, cpu.d.ab.a);
|
|
cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=4; break;
|
|
}
|
|
case 0x9E: { /* LDX direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu.x = cpu_read16(ad);
|
|
cc_clear(CC_V); set_nz16(cpu.x); cycles=5; break;
|
|
}
|
|
case 0x9F: { /* STX direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu_write16(ad, cpu.x);
|
|
cc_clear(CC_V); set_nz16(cpu.x); cycles=5; break;
|
|
}
|
|
case 0x9D: { /* JSR direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++);
|
|
uint16_t ret = cpu.pc; push16(&cpu.s, ret);
|
|
cpu.pc = ad; cycles = 7; break;
|
|
}
|
|
case 0xBD: { /* JSR extended */
|
|
uint16_t ea = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint16_t ret = cpu.pc; push16(&cpu.s, ret);
|
|
cpu.pc = ea; cycles = 8; break;
|
|
}
|
|
|
|
/* ---------------- Ax indexed 8-bit --------------------------------- */
|
|
case 0xA0: case 0xA1: case 0xA2: case 0xA3: case 0xA4: case 0xA5:
|
|
case 0xA6: case 0xA7: case 0xA8: case 0xA9: case 0xAA: case 0xAB:
|
|
case 0xAC: case 0xAD: case 0xAE: case 0xAF: {
|
|
uint16_t ea = ea_indexed();
|
|
switch (op) {
|
|
case 0xA6: cpu.d.ab.a = cpu_read8(ea); cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=4; break;
|
|
case 0xA7: cpu_write8(ea, cpu.d.ab.a); cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=4; break;
|
|
case 0xAE: cpu.x = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.x); cycles=5; break;
|
|
case 0xAF: cpu_write16(ea, cpu.x); cc_clear(CC_V); set_nz16(cpu.x); cycles=5; break;
|
|
case 0xAD: { uint16_t ret=cpu.pc; push16(&cpu.s, ret); cpu.pc = ea; cycles=7; break; } /* JSR indexed */
|
|
default:
|
|
/* many ALU indexed left as exercise or fall to illegal for now */
|
|
illegal(op); cycles=1; break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* ---------------- Bx extended 8-bit -------------------------------- */
|
|
case 0xB6: { /* LDA ext */
|
|
uint16_t ea = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
cpu.d.ab.a = cpu_read8(ea); cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=5; break;
|
|
}
|
|
case 0xB7: { /* STA ext */
|
|
uint16_t ea = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
cpu_write8(ea, cpu.d.ab.a); cc_clear(CC_V); set_nz8(cpu.d.ab.a); cycles=5; break;
|
|
}
|
|
case 0xBE: { /* LDX ext */
|
|
uint16_t ea = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
cpu.x = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.x); cycles=6; break;
|
|
}
|
|
case 0xBF: { /* STX ext */
|
|
uint16_t ea = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
cpu_write16(ea, cpu.x); cc_clear(CC_V); set_nz16(cpu.x); cycles=6; break;
|
|
}
|
|
|
|
/* ---------------- $Cx immediate B-side ---------------------------- */
|
|
case 0xC0: { /* SUBB # */
|
|
uint8_t m = cpu_read8(cpu.pc++); uint8_t b = cpu.d.ab.b;
|
|
uint16_t r = b - m; cpu.d.ab.b = r & 0xFF;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x100) cc_set(CC_C);
|
|
if (((b ^ m ^ r) & 0x80) && ((b ^ r) & 0x80)) cc_set(CC_V);
|
|
set_nz8(cpu.d.ab.b);
|
|
cycles=2; break;
|
|
}
|
|
case 0xC1: { /* CMPB # */
|
|
uint8_t m = cpu_read8(cpu.pc++); uint8_t b = cpu.d.ab.b;
|
|
uint16_t r = b - m;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x100) cc_set(CC_C);
|
|
if (((b ^ m ^ r) & 0x80) && ((b ^ r) & 0x80)) cc_set(CC_V);
|
|
set_nz8(r & 0xFF);
|
|
cycles=2; break;
|
|
}
|
|
case 0xC4: { /* ANDB # */ cpu.d.ab.b &= cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=2; break; }
|
|
case 0xC5: { /* BITB # */ { uint8_t t = cpu.d.ab.b & cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(t); } cycles=2; break; }
|
|
case 0xC6: /* LDB # */ cpu.d.ab.b = cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=2; break;
|
|
case 0xC8: { /* EORB # */ cpu.d.ab.b ^= cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=2; break; }
|
|
case 0xCA: { /* ORB # */ cpu.d.ab.b |= cpu_read8(cpu.pc++); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=2; break; }
|
|
case 0xCC: /* LDD # */
|
|
cpu.d.ab.a = cpu_read8(cpu.pc++); cpu.d.ab.b = cpu_read8(cpu.pc++);
|
|
cc_clear(CC_V); set_nz16(cpu.d.d); cycles=3; break;
|
|
case 0xCE: /* LDU # */
|
|
cpu.u = cpu_read16(cpu.pc); cpu.pc += 2; cc_clear(CC_V); set_nz16(cpu.u); cycles=3; break;
|
|
|
|
/* ---------------- $Dx direct B/D/U -------------------------------- */
|
|
case 0xD6: { /* LDB direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu.d.ab.b = cpu_read8(ad);
|
|
cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=4; break;
|
|
}
|
|
case 0xD7: { /* STB direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu_write8(ad, cpu.d.ab.b);
|
|
cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=4; break;
|
|
}
|
|
case 0xDC: { /* LDD direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu.d.ab.a = cpu_read8(ad); cpu.d.ab.b = cpu_read8((uint16_t)(ad+1));
|
|
cc_clear(CC_V); set_nz16(cpu.d.d); cycles=5; break;
|
|
}
|
|
case 0xDD: { /* STD direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu_write16(ad, cpu.d.d);
|
|
cc_clear(CC_V); set_nz16(cpu.d.d); cycles=5; break;
|
|
}
|
|
case 0xDE: { /* LDU direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu.u = cpu_read16(ad);
|
|
cc_clear(CC_V); set_nz16(cpu.u); cycles=5; break;
|
|
}
|
|
case 0xDF: { /* STU direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu_write16(ad, cpu.u);
|
|
cc_clear(CC_V); set_nz16(cpu.u); cycles=5; break;
|
|
}
|
|
|
|
/* ---------------- $Ex indexed B/D/U ------------------------------- */
|
|
case 0xE6: { uint16_t ea=ea_indexed(); cpu.d.ab.b = cpu_read8(ea); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=4; break; }
|
|
case 0xE7: { uint16_t ea=ea_indexed(); cpu_write8(ea, cpu.d.ab.b); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=4; break; }
|
|
case 0xEC: { uint16_t ea=ea_indexed(); cpu.d.d = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.d.d); cycles=5; break; }
|
|
case 0xED: { uint16_t ea=ea_indexed(); cpu_write16(ea, cpu.d.d); cc_clear(CC_V); set_nz16(cpu.d.d); cycles=5; break; }
|
|
case 0xEE: { uint16_t ea=ea_indexed(); cpu.u = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.u); cycles=5; break; }
|
|
case 0xEF: { uint16_t ea=ea_indexed(); cpu_write16(ea, cpu.u); cc_clear(CC_V); set_nz16(cpu.u); cycles=5; break; }
|
|
|
|
/* ---------------- $Fx extended B/D/U ------------------------------ */
|
|
case 0xF6: { uint16_t ea=cpu_read16(cpu.pc); cpu.pc+=2; cpu.d.ab.b=cpu_read8(ea); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=5; break; }
|
|
case 0xF7: { uint16_t ea=cpu_read16(cpu.pc); cpu.pc+=2; cpu_write8(ea, cpu.d.ab.b); cc_clear(CC_V); set_nz8(cpu.d.ab.b); cycles=5; break; }
|
|
case 0xFC: { uint16_t ea=cpu_read16(cpu.pc); cpu.pc+=2; cpu.d.d = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.d.d); cycles=6; break; }
|
|
case 0xFD: { uint16_t ea=cpu_read16(cpu.pc); cpu.pc+=2; cpu_write16(ea, cpu.d.d); cc_clear(CC_V); set_nz16(cpu.d.d); cycles=6; break; }
|
|
case 0xFE: { uint16_t ea=cpu_read16(cpu.pc); cpu.pc+=2; cpu.u = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.u); cycles=6; break; }
|
|
case 0xFF: { uint16_t ea=cpu_read16(cpu.pc); cpu.pc+=2; cpu_write16(ea, cpu.u); cc_clear(CC_V); set_nz16(cpu.u); cycles=6; break; }
|
|
|
|
/* ---------------- $10 and $11 prefix pages ------------------------ */
|
|
case 0x10: {
|
|
uint8_t op2 = cpu_read8(cpu.pc++);
|
|
switch (op2) {
|
|
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25:
|
|
case 0x26: case 0x27: case 0x28: case 0x29: case 0x2A: case 0x2B:
|
|
case 0x2C: case 0x2D: case 0x2E: case 0x2F: {
|
|
/* long conditional branches */
|
|
int16_t off = (int16_t)cpu_read16(cpu.pc); cpu.pc += 2;
|
|
if (cond(op2)) cpu.pc = (cpu.pc + off) & 0xFFFF;
|
|
cycles = 5; /* approx */
|
|
break;
|
|
}
|
|
case 0x83: { /* CMPD # */
|
|
uint16_t m = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint32_t r = cpu.d.d - m;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x10000) cc_set(CC_C);
|
|
if (((cpu.d.d ^ m ^ (r&0xFFFF)) & 0x8000) && ((cpu.d.d ^ (r&0xFFFF)) & 0x8000)) cc_set(CC_V);
|
|
set_nz16((uint16_t)r);
|
|
cycles = 5; break;
|
|
}
|
|
case 0x8C: { /* CMPY # */
|
|
uint16_t m = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint32_t r = cpu.y - m;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x10000) cc_set(CC_C);
|
|
if (((cpu.y ^ m ^ (r&0xFFFF)) & 0x8000) && ((cpu.y ^ (r&0xFFFF)) & 0x8000)) cc_set(CC_V);
|
|
set_nz16((uint16_t)r);
|
|
cycles=5; break;
|
|
}
|
|
case 0x8E: /* LDY # */
|
|
cpu.y = cpu_read16(cpu.pc); cpu.pc += 2; cc_clear(CC_V); set_nz16(cpu.y); cycles=4; break;
|
|
case 0x9E: { /* LDY direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu.y = cpu_read16(ad);
|
|
cc_clear(CC_V); set_nz16(cpu.y); cycles=6; break;
|
|
}
|
|
case 0x9F: { /* STY direct */
|
|
uint8_t ad = cpu_read8(cpu.pc++); cpu_write16(ad, cpu.y);
|
|
cc_clear(CC_V); set_nz16(cpu.y); cycles=6; break;
|
|
}
|
|
case 0xAE: { uint16_t ea = ea_indexed(); cpu.y = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.y); cycles=6; break; }
|
|
case 0xAF: { uint16_t ea = ea_indexed(); cpu_write16(ea, cpu.y); cc_clear(CC_V); set_nz16(cpu.y); cycles=6; break; }
|
|
case 0xBE: { uint16_t ea = cpu_read16(cpu.pc); cpu.pc+=2; cpu.y = cpu_read16(ea); cc_clear(CC_V); set_nz16(cpu.y); cycles=7; break; }
|
|
case 0xBF: { uint16_t ea = cpu_read16(cpu.pc); cpu.pc+=2; cpu_write16(ea, cpu.y); cc_clear(CC_V); set_nz16(cpu.y); cycles=7; break; }
|
|
case 0xCE: /* LDS # */
|
|
cpu.s = cpu_read16(cpu.pc); cpu.pc += 2; cc_clear(CC_V); set_nz16(cpu.s); cycles=4; break;
|
|
default:
|
|
illegal(op2); cycles=1; break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case 0x11: {
|
|
uint8_t op2 = cpu_read8(cpu.pc++);
|
|
switch (op2) {
|
|
case 0x83: { /* CMPU # */
|
|
uint16_t m = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint32_t r = cpu.u - m;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x10000) cc_set(CC_C);
|
|
if (((cpu.u ^ m ^ (r&0xFFFF)) & 0x8000) && ((cpu.u ^ (r&0xFFFF)) & 0x8000)) cc_set(CC_V);
|
|
set_nz16((uint16_t)r);
|
|
cycles=5; break;
|
|
}
|
|
case 0x8C: { /* CMPS # */
|
|
uint16_t m = cpu_read16(cpu.pc); cpu.pc += 2;
|
|
uint32_t r = cpu.s - m;
|
|
cc_clear(CC_C|CC_V|CC_N|CC_Z);
|
|
if (r & 0x10000) cc_set(CC_C);
|
|
if (((cpu.s ^ m ^ (r&0xFFFF)) & 0x8000) && ((cpu.s ^ (r&0xFFFF)) & 0x8000)) cc_set(CC_V);
|
|
set_nz16((uint16_t)r);
|
|
cycles=5; break;
|
|
}
|
|
default:
|
|
illegal(op2); cycles=1; break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* ---------------- 6309 extras (selected) --------------------------- */
|
|
/* We implement a few useful ones so cc09 code using 6309 can advance. */
|
|
case 0x14: /* SEXW (6309) — sign extend W to Q (very simplified) */
|
|
if (cpu.model == CPU_6309) {
|
|
uint16_t w = (uint16_t)((cpu.e << 8) | cpu.f);
|
|
cpu.q = (w & 0x8000) ? (0xFFFF0000u | w) : w;
|
|
set_nz16(w);
|
|
cycles = 4;
|
|
} else illegal(op);
|
|
break;
|
|
|
|
case 0xCD: /* LDQ # (6309 immediate 32-bit) */
|
|
if (cpu.model == CPU_6309) {
|
|
cpu.q = ((uint32_t)cpu_read16(cpu.pc) << 16) | cpu_read16((uint16_t)(cpu.pc+2));
|
|
cpu.pc += 4;
|
|
/* set flags from low 16? or whole — keep simple */
|
|
set_nz16(cpu.q & 0xFFFF);
|
|
cycles = 5;
|
|
} else illegal(op);
|
|
break;
|
|
|
|
default:
|
|
illegal(op);
|
|
cycles = 1;
|
|
break;
|
|
}
|
|
|
|
cpu.total_cycles += (uint64_t)cycles;
|
|
|
|
/* If a device or watchpoint asked for a stop, honor it */
|
|
if (cpu.stop_requested) {
|
|
cpu.stop_requested = false;
|
|
return cycles;
|
|
}
|
|
return cycles;
|
|
}
|
|
|
|
/* --- run loop ----------------------------------------------------------- */
|
|
|
|
int cpu_run(void)
|
|
{
|
|
cpu.stop_requested = false;
|
|
int steps = 0;
|
|
while (!cpu.halted && !cpu.stop_requested) {
|
|
if (cpu.debug_mode) {
|
|
/* In debug mode we still run fast but let monitor control via step or bp */
|
|
if (bp_check_exec(cpu.pc)) {
|
|
cpu.stop_requested = true;
|
|
break;
|
|
}
|
|
}
|
|
cpu_step();
|
|
steps++;
|
|
/* Occasional yield / host input poll opportunity for ACIA etc.
|
|
In a real fast run we would batch many steps. */
|
|
if ((steps & 0x3FFF) == 0) {
|
|
/* allow host to inject SIGINT or kb chars */
|
|
/* In practice the monitor will use single steps or set a temp bp */
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* --- init / reset ------------------------------------------------------- */
|
|
|
|
void cpu_init(cpu_model_t model)
|
|
{
|
|
memset(&cpu, 0, sizeof(cpu));
|
|
memset(memory, 0, sizeof(memory));
|
|
memset(bp_exec, 0, sizeof(bp_exec));
|
|
memset(bp_watch, 0, sizeof(bp_watch));
|
|
cpu.model = model;
|
|
cpu.native6309 = (model == CPU_6309);
|
|
cpu.md = (model == CPU_6309) ? 0x03 : 0x00; /* example: native + FIRQ on 6309 */
|
|
dev_count = 0;
|
|
}
|
|
|
|
void cpu_reset(void)
|
|
{
|
|
cpu.halted = false;
|
|
cpu.stop_requested = false;
|
|
cpu.cc = CC_I | CC_F; /* typical after reset */
|
|
cpu.pc = mem_read16(0xFFFE); /* reset vector (bypass devices) */
|
|
}
|
|
|
|
void cpu_set_model(cpu_model_t m)
|
|
{
|
|
cpu.model = m;
|
|
cpu.native6309 = (m == CPU_6309);
|
|
}
|
|
|
|
void cpu_dump_regs(char *buf, size_t buflen)
|
|
{
|
|
/* Format similar to C64 monitors + 6809 flavor */
|
|
char flags[9];
|
|
flags[0] = (cpu.cc & CC_E) ? 'E' : '.';
|
|
flags[1] = (cpu.cc & CC_F) ? 'F' : '.';
|
|
flags[2] = (cpu.cc & CC_H) ? 'H' : '.';
|
|
flags[3] = (cpu.cc & CC_I) ? 'I' : '.';
|
|
flags[4] = (cpu.cc & CC_N) ? 'N' : '.';
|
|
flags[5] = (cpu.cc & CC_Z) ? 'Z' : '.';
|
|
flags[6] = (cpu.cc & CC_V) ? 'V' : '.';
|
|
flags[7] = (cpu.cc & CC_C) ? 'C' : '.';
|
|
flags[8] = 0;
|
|
|
|
uint16_t w = (cpu.model == CPU_6309) ? (uint16_t)((cpu.e << 8) | cpu.f) : 0;
|
|
|
|
snprintf(buf, buflen,
|
|
"PC=%04X A=%02X B=%02X D=%04X X=%04X Y=%04X U=%04X S=%04X\n"
|
|
"CC=%02X [%s] %s%s E=%02X F=%02X W=%04X Q=%08X MD=%02X cycles=%llu\n",
|
|
cpu.pc,
|
|
cpu.d.ab.a, cpu.d.ab.b, cpu.d.d,
|
|
cpu.x, cpu.y, cpu.u, cpu.s,
|
|
cpu.cc, flags,
|
|
(cpu.model==CPU_6309?"6309":"6809"),
|
|
(cpu.native6309?" native":""),
|
|
cpu.e, cpu.f, w, cpu.q, cpu.md,
|
|
(unsigned long long)cpu.total_cycles);
|
|
}
|
|
|
|
/* convenience for monitor: allow direct vector poke if needed */
|
|
void cpu_set_pc(uint16_t pc) { cpu.pc = pc; }
|