// A board around Breaknes's PPUSim (gate-level RP2C02G or RP2C07-0, CC0) for co-simulation // with famicom-python's CPU: see scripts/ppusim_board.py and docs/validation.md // ("PPUSim as a judge"). Our code; PPUSim itself is fetched, never committed. // // The shim owns what sits on the PPU's side of the NES-001: the octal latch that // holds AD0-7 on ALE, the cartridge's CHR (ROM or RAM) on PA13 = 0, and the // 2 KiB CIRAM on PA13 = 1 with A10 from PA10 (vertical) or PA11 (horizontal). // The CPU side is driven one master half-cycle at a time: R/W, RS0-2, /DBE and // the data bus, with /INT read back. #include "pch.h" #include #include using namespace BaseLogic; using namespace PPUSim; // PPUSim's classes befriend this one for tests; the shim uses it to read // internal signals into the trace (``inner``). namespace PPUSimUnitTest { class UnitTest { public: // ppusim_eval_probe: the sprite-evaluation signals the wiki maps to Visual 2C02 // nodes (BreakingNESWiki PPU/visual2c02.md), one bit each, in this order: // OSTEP OBJ_READ /H2' OAMCTR2 ORES I/OAM2 /EVAL OMFG H0' PCLK, then the // secondary OAM counter (OAM_Temp 0-4) and OAM8. static int eval(PPU &ppu) { auto one = [](TriState t) { return t == One ? 1 : 0; }; auto *e = ppu.eval; return one(e->OSTEP) | one(ppu.fsm.OBJ_READ) << 1 | one(ppu.wire.nH2_Dash) << 2 | one(ppu.wire.OAMCTR2) << 3 | one(e->ORES) << 4 | one(ppu.fsm.IOAM2) << 5 | one(ppu.fsm.n_EVAL) << 6 | one(e->OMFG) << 7 | one(ppu.wire.H0_Dash) << 8 | one(ppu.wire.PCLK) << 9 | (one(e->OAM_Temp[0]) | one(e->OAM_Temp[1]) << 1 | one(e->OAM_Temp[2]) << 2 | one(e->OAM_Temp[3]) << 3 | one(e->OAM_Temp[4]) << 4) << 10 | one(ppu.wire.OAM8) << 15; } // ppusim_regs_probe: the $2001 path to rendering, in this order (the wiki's // names; compare.py REGS has the Visual 2C02 nodes): /DBE /W1 PPU_CTRL1[3] // BGE BLACK BLNK OBJ_READ PCLK. static int regs(PPU &ppu) { auto one = [](TriState t) { return t == One ? 1 : 0; }; return one(ppu.wire.n_DBE) | one(ppu.wire.n_W1) << 1 | one(ppu.regs->PPU_CTRL1[3].get()) << 2 | one(ppu.wire.BGE) << 3 | one(ppu.wire.BLACK) << 4 | one(ppu.fsm.BLNK) << 5 | one(ppu.fsm.OBJ_READ) << 6 | one(ppu.wire.PCLK) << 7; } // ppusim_fifo_probe: sprite lane 0 against compare.py FIFO, in this order: // #0/H (n_ZH) PCLK, the lane's X down-counter (8 bits), its /ZCOL0 /ZCOL1 // outputs, then ZH_FF, the lane's enable (NOT /EN), /VIS and SR_EN. static int fifo(PPU &ppu) { auto one = [](TriState t) { return t == One ? 1 : 0; }; auto *lane = ppu.fifo->lane[0]; int counter = 0; for (int n = 0; n < 8; n++) counter |= one(lane->down_cnt[n].get()) << n; return one(ppu.wire.n_ZH) | one(ppu.wire.PCLK) << 1 | counter << 2 | one(lane->nZ_COL0) << 10 | one(lane->nZ_COL1) << 11 | one(lane->ZH_FF.get()) << 12 | (1 - one(lane->n_EN)) << 13 | one(ppu.fsm.nVIS) << 14 | one(lane->SR_EN) << 15; } // ppusim_vis_probe, compare.py VIS's order: VB, /VIS, BLNK, PCLK, V counter bit 0. static int vis(PPU &ppu) { auto one = [](TriState t) { return t == One ? 1 : 0; }; return one(ppu.fsm.VB) | one(ppu.fsm.nVIS) << 1 | one(ppu.fsm.BLNK) << 2 | one(ppu.wire.PCLK) << 3 | (int)(ppu.GetVCounter() & 1) << 4; } // ppusim_ob_probe, compare.py OB's order: the OAM buffer (OB0-7), OAM8, PCLK, // I/OAM2, /WE. static int ob(PPU &ppu) { auto one = [](TriState t) { return t == One ? 1 : 0; }; int bits = 0; for (int n = 0; n < 8; n++) bits |= one(ppu.wire.OB[n]) << n; return bits | one(ppu.wire.OAM8) << 8 | one(ppu.wire.PCLK) << 9 | one(ppu.fsm.IOAM2) << 10 | one(ppu.wire.n_WE) << 11; } static int oam(PPU &ppu) { // ppusim_oam_probe int a = ppu.wire.OAM8 == One ? 0x100 : 0; for (int n = 0; n < 8; n++) a |= (ppu.wire.n_OAM[n] == Zero ? 1 : 0) << n; int ob = 0; for (int n = 0; n < 8; n++) ob |= (ppu.wire.OB[n] == One ? 1 : 0) << n; return a | (ppu.fsm.IOAM2 == One ? 0x200 : 0) | ob << 10; } static void inner(PPU &ppu, uint8_t out[5]) { auto one = [](TriState t) { return t == One ? 1 : 0; }; out[0] = (uint8_t)(one(ppu.fsm.BLNK) | one(ppu.fsm.ZHPOS) << 1 | one(ppu.fsm.nVIS) << 2 | one(ppu.fsm.OBJ_READ) << 3 | one(ppu.wire.PCLK) << 4 | one(ppu.fsm.nFO) << 5 | one(ppu.wire.n_SPR0HIT) << 6 | one(ppu.fsm.n_EVAL) << 7); out[1] = (uint8_t)ppu.fifo->lane[0]->get_Counter(); uint8_t active = 0, enabled = 0; for (int n = 0; n < 8; n++) { active |= (uint8_t)(one(ppu.fifo->lane[n]->ZH_FF.get()) << n); enabled |= (uint8_t)((ppu.fifo->lane[n]->n_EN == Zero ? 1 : 0) << n); } out[2] = active; out[3] = enabled; out[4] = (uint8_t)(one(ppu.wire.BLACK) | one(ppu.fsm.SCCNT) << 1 | (ppu.fifo->lane[0]->nZ_COL0 == Zero ? 1 : 0) << 2 | (ppu.fifo->lane[0]->nZ_COL1 == Zero ? 1 : 0) << 3 | one(ppu.fifo->lane[0]->LOAD) << 4 | one(ppu.fifo->lane[0]->STEP) << 5); } // The background attribute path (bgcol.cpp): BGC_SRBit keeps its latches // private and befriends no one, so read them through a mirror of its layout. struct SRBitView { DLatch in_latch; DLatch out_latch; }; static TriState bit_out(BGC_SRBit &bit) { return reinterpret_cast(bit).out_latch.nget(); } static void background(PPU &ppu, uint8_t out[8]) { auto one = [](TriState t) { return t == One ? 1 : 0; }; BGCol &bg = *ppu.data_reader->bgcol; out[0] = (uint8_t)(one(bg.SRLOAD) | one(bg.STEP2) << 1 | one(bg.PD_SEL) << 2 | one(bg.NEXT) << 3 | one(bg.H01) << 4 | one(bit_out(bg.BGC2_SRBit1)) << 5 | one(bit_out(bg.BGC3_SRBit1)) << 6 | one(bg.STEP) << 7); uint8_t sr2 = 0, sr3 = 0, at = 0, sr0 = 0, lo2 = 0, lo1 = 0, hi1 = 0; for (int n = 0; n < 8; n++) { sr2 |= (uint8_t)(one(bit_out(bg.BGC2_SR1.sr[n])) << n); sr3 |= (uint8_t)(one(bit_out(bg.BGC3_SR1.sr[n])) << n); at |= (uint8_t)(one(bg.pd_latch[n].get()) << n); sr0 |= (uint8_t)(one(bit_out(bg.BGC1_SR2.sr[n])) << n); lo2 |= (uint8_t)(one(bit_out(bg.BGC0_SR2.sr[n])) << n); // inverted plane lo1 |= (uint8_t)(one(bit_out(bg.BGC0_SR1.sr[n])) << n); // inverted plane hi1 |= (uint8_t)(one(bit_out(bg.BGC1_SR1.sr[n])) << n); } out[1] = sr2; out[2] = sr3; out[3] = at; out[4] = (uint8_t)(one(ppu.wire.BGC[0]) | one(ppu.wire.BGC[1]) << 1 | one(ppu.wire.BGC[2]) << 2 | one(ppu.wire.BGC[3]) << 3 | one(ppu.wire.TVO[1]) << 4 | one(ppu.wire.THO[1]) << 5); out[5] = sr0; out[6] = (uint8_t)~lo2; out[7] = (uint8_t)~lo1; out[2] = hi1; // (BGC3's shifter, bit 1 of the attribute, gives way) } }; } // namespace PPUSimUnitTest namespace { // One master half-cycle as the pins and counters show it, for comparisons // with famicom-python's PPU (scripts/ppusim_compare.py). struct TraceEntry { uint64_t half; uint16_t h, v; uint16_t address; // PA0-13: the octal latch and PA8-13 uint16_t video; // RAW: eeellcccc, 0 outside the picture uint8_t pins; // bit 0 ALE, 1 /RD low, 2 /WR low, 3 /INT low // Internal signals (PPUSimUnitTest::UnitTest::inner): 0 BLNK, ZHPOS, // nVIS, OBJ_READ, PCLK, nFO, /SPR0HIT, /EVAL; 1 lane 0's X counter; // 2 each lane's "active" flip-flop (ZH_FF); 3 each lane's shifter enable; // 4 BLACK, SCCNT, lane 0's pixel bits 0-1, LOAD, STEP. uint8_t inner[5]; // The background path (PPUSimUnitTest::UnitTest::background): 0 SRLOAD, // STEP2, PD_SEL, NEXT, H01, the attribute latches' outputs (bits 0, 1), // STEP; 1 the attribute's bit-0 shifter; 3 the attribute byte latch; // 4 BGC0-3, TVO1, THO1; 5, 6 the high and low planes' second (current // tile) shifters, 2 and 7 their first (next tile) ones; bit n is the // pixel n dots on. uint8_t background[8]; // The OAM main counter (OAMADDR) and its controls: 0 OMSTEP, 1 OMOUT, // 2 OMFG, 3 a $2003 write (/W3 and /DBE low), 4 PCLK, 5 DO_COPY, // 6 COPY_OVF, 7 OVZ. uint8_t oam_main; uint8_t oam_wires; }; static_assert(sizeof(TraceEntry) == 32); struct Board { explicit Board(Revision revision) : ppu{revision} {} PPU ppu; TriState in[(size_t)InputPad::Max]{}; TriState out[(size_t)OutputPad::Max]{}; uint8_t ext = 0, db = 0, ad = 0, ahi = 0; VideoOutSignal video{}; uint8_t latch = 0; // the octal latch (74LS373) on AD0-7 std::vector chr; bool chr_writable = false; int a10_bit = 11; // 11: horizontal arrangement, 10: vertical uint8_t ciram[0x800]{}; uint64_t half = 0; // master half-cycles simulated uint8_t clk = 0; std::vector trace; // filled while tracing (ppusim_trace) bool tracing = false; // RAW video by (V, H), each dot's last half-cycle (ppusim_screen): whole // frames without the trace's cost, for replays (breaknes_regdump.py). 312 // lines for the 2C07; a 2C02's 262 fill the start, laid out the same. uint16_t screen[312 * 341]{}; uint16_t address() const { return (uint16_t)(((ahi & 0x3F) << 8) | latch); } uint8_t *cell(uint16_t a) { if (a & 0x2000) { int a10 = (a >> a10_bit) & 1; return &ciram[(a10 << 10) | (a & 0x3FF)]; } return &chr[a % chr.size()]; } // One master half-cycle: the CLK pin toggles, the chip settles, then the // memories answer the strobes it raised. void step() { in[(size_t)InputPad::CLK] = clk ? One : Zero; clk ^= 1; ppu.sim(in, out, &ext, &db, &ad, &ahi, video); if (out[(size_t)OutputPad::ALE] == One) latch = ad; if (out[(size_t)OutputPad::n_RD] == Zero) ad = *cell(address()); if (out[(size_t)OutputPad::n_WR] == Zero) { uint16_t a = address(); if ((a & 0x2000) || chr_writable) *cell(a) = ad; } { int v = ppu.GetVCounter(), hc = ppu.GetHCounter(); if (v < 312 && hc < 341) screen[v * 341 + hc] = (uint16_t)(video.RAW.raw & 0x1FF); } if (tracing) { TraceEntry e{}; e.half = half; e.h = (uint16_t)ppu.GetHCounter(); e.v = (uint16_t)ppu.GetVCounter(); e.address = address(); e.video = (uint16_t)(video.RAW.raw & 0x1FF); e.pins = (uint8_t)((out[(size_t)OutputPad::ALE] == One) | (out[(size_t)OutputPad::n_RD] == Zero) << 1 | (out[(size_t)OutputPad::n_WR] == Zero) << 2 | (out[(size_t)OutputPad::n_INT] == Zero) << 3); PPUSimUnitTest::UnitTest::inner(ppu, e.inner); PPUSimUnitTest::UnitTest::background(ppu, e.background); PPU_Registers regs{}; ppu.GetDebugInfo_Regs(regs); e.oam_main = (uint8_t)regs.MainOAMCounter; OAMEvalWires eval{}; ppu.GetDebugInfo_OAMEval(eval); PPU_Interconnects wires{}; ppu.GetDebugInfo_Wires(wires); e.oam_wires = (uint8_t)((eval.OMSTEP & 1) | (eval.OMOUT & 1) << 1 | (eval.OMFG & 1) << 2 | (!wires.n_W3 && !wires.n_DBE) << 3 | (wires.PCLK & 1) << 4 | (eval.DO_COPY & 1) << 5 | (eval.COPY_OVF & 1) << 6 | (eval.OVZ & 1) << 7); trace.push_back(e); } half++; } }; } // namespace extern "C" { // pal: 0 the RP2C02G, 1 the RP2C07-0 (PAL), 2 the UMC UA6538 (the Dendy's). void *ppusim_new_revision(const uint8_t *chr, int chr_len, int chr_writable, int vertical, int pal) { auto *b = new Board(pal == 2 ? Revision::UMC_UA6538 : pal ? Revision::RP2C07_0 : Revision::RP2C02G); b->chr.assign(chr, chr + (chr_len > 0 ? chr_len : 0x2000)); if (chr_len <= 0) std::memset(b->chr.data(), 0, b->chr.size()); b->chr_writable = chr_writable != 0; b->a10_bit = vertical ? 10 : 11; for (auto &x : b->in) x = One; b->in[(size_t)InputPad::RnW] = One; b->in[(size_t)InputPad::n_DBE] = One; b->ppu.SetRAWOutput(true); return b; } void *ppusim_new(const uint8_t *chr, int chr_len, int chr_writable, int vertical) { return ppusim_new_revision(chr, chr_len, chr_writable, vertical, 0); } void ppusim_free(void *h) { delete (Board *)h; } // Hold /RES low for `halves` half-cycles, then release it. void ppusim_reset(void *h, int halves) { auto *b = (Board *)h; b->in[(size_t)InputPad::n_RES] = Zero; for (int i = 0; i < halves; i++) b->step(); b->in[(size_t)InputPad::n_RES] = One; } // Run with the CPU interface idle (/DBE high). void ppusim_run(void *h, uint64_t halves) { auto *b = (Board *)h; b->in[(size_t)InputPad::n_DBE] = One; for (uint64_t i = 0; i < halves; i++) b->step(); } // One CPU access: `before` idle half-cycles, then /DBE low for `during` with // R/W and RS0-2 set and, for a write, the data bus driven (with `early` the // value the bus holds for the first `early_halves` of the window: the "early // write" open-bus value, PPU_glitches.wiki). Returns the data bus at the end of // the window (what the CPU latches at M2's fall). int ppusim_access(void *h, int rnw, int reg, int value, int early, int early_halves, int before, int during) { auto *b = (Board *)h; b->in[(size_t)InputPad::n_DBE] = One; for (int i = 0; i < before; i++) b->step(); b->in[(size_t)InputPad::RnW] = rnw ? One : Zero; b->in[(size_t)InputPad::RS0] = (reg & 1) ? One : Zero; b->in[(size_t)InputPad::RS1] = (reg & 2) ? One : Zero; b->in[(size_t)InputPad::RS2] = (reg & 4) ? One : Zero; b->in[(size_t)InputPad::n_DBE] = Zero; for (int i = 0; i < during; i++) { if (!rnw) b->db = (uint8_t)(i < early_halves ? early : value); b->step(); } int result = b->db; b->in[(size_t)InputPad::n_DBE] = One; b->in[(size_t)InputPad::RnW] = One; return result; } int ppusim_int(void *h) { return ((Board *)h)->out[(size_t)OutputPad::n_INT] == Zero; } int ppusim_h(void *h) { return (int)((Board *)h)->ppu.GetHCounter(); } int ppusim_v(void *h) { return (int)((Board *)h)->ppu.GetVCounter(); } int ppusim_vbl(void *h) { return ((Board *)h)->ppu.Dbg_GetVBLFlag() == One; } uint64_t ppusim_halves(void *h) { return ((Board *)h)->half; } int ppusim_ciram(void *h, int index) { return ((Board *)h)->ciram[index & 0x7FF]; } int ppusim_chr(void *h, int index) { auto *b = (Board *)h; return b->chr[index % b->chr.size()]; } // Start (clearing) or stop recording a TraceEntry per half-cycle. void ppusim_trace(void *h, int on) { auto *b = (Board *)h; if (on) b->trace.clear(); b->tracing = on != 0; } uint64_t ppusim_trace_len(void *h) { return ((Board *)h)->trace.size(); } const void *ppusim_trace_data(void *h) { return ((Board *)h)->trace.data(); } // Secondary ("temp") OAM, 32 bytes, as the evaluation left it. int ppusim_temp_oam(void *h, int index) { return ((Board *)h)->ppu.Dbg_TempOAMReadByte((size_t)(index & 31)); } int ppusim_raw_video(void *h) { return ((Board *)h)->video.RAW.raw; } const void *ppusim_screen(void *h) { return ((Board *)h)->screen; } // The OAM address on its pins into the array: OAM8 (secondary OAM) << 8 | OAM0-7, // with I_OAM2 << 9 (the buffer forced to $FF) and the OAM buffer << 10. int ppusim_fifo_probe(void *h) { return PPUSimUnitTest::UnitTest::fifo(((Board *)h)->ppu); } int ppusim_vis_probe(void *h) { return PPUSimUnitTest::UnitTest::vis(((Board *)h)->ppu); } int ppusim_ob_probe(void *h) { return PPUSimUnitTest::UnitTest::ob(((Board *)h)->ppu); } int ppusim_regs_probe(void *h) { return PPUSimUnitTest::UnitTest::regs(((Board *)h)->ppu); } int ppusim_eval_probe(void *h) { return PPUSimUnitTest::UnitTest::eval(((Board *)h)->ppu); } int ppusim_oam_probe(void *h) { return PPUSimUnitTest::UnitTest::oam(((Board *)h)->ppu); } } // extern "C"