cpu65816.module
README.md
snes.cpu65816 — WDC 65C816 CPU
A reusable 65C816 core (Super Nintendo, Apple IIGS, SA-1 style machines) as a table of 256 closures over upvalue registers.
local Cpu = require("snes.cpu65816")
local cpu = Cpu.new({
read = function(adr24) return byte end, -- slow path (I/O, unmapped)
write = function(adr24, v) end,
-- fast path, per 8 KB page (2048 pages over 24 bits): buffer + offset (+ writable)
-- and the master clocks one access to the page costs
map = function(page) return buf, off, writable, clocks end,
})
cpu.reset()
local used = cpu.run(untilMasterClock) -- runs whole instructions until cycles() >= the limit
cpu.nmi() ; cpu.setIrq(true/false) ; cpu.remap(page?) ; cpu.addCycles(n) ; cpu.setLimit(n)
cpu.getState() / cpu.setState(t) ; cpu.regs() / cpu.setRegs(t)
- Registers: A/C (16-bit, B is the high byte while M=1), X, Y (high byte 0 while X=1), S (page 1 in emulation), D, DBR, PBR, PC, P, E.
- Every documented opcode incl. MVN/MVP (one byte per step, interruptible), PEA/PEI/PER, REP/SEP, XCE, XBA, COP, BRK, WDM, WAI (wakes on NMI or IRQ, even with I set), STP.
- Decimal ADC/SBC in 8 and 16 bit.
- Timing in master clocks: each bus access costs what
mapsays for its page, each internal operation 6; dp with D low byte set, index page crossing, 16-bit operands and RMW add the documented extra cycles. - Flags kept as
nf/zfvalues (N = bit 7 of nf, which holds the high byte of 16-bit results; Z = zf == 0).
Tests: snes.package/tests/cpu_tests.luau.