# tb Verilator testbenches, one per build step (DESIGN.md section 9). Outputs go to `sim/work*`. Each planted fault must fail; its runs stop at the first that does (a plant not caught runs them all). Build settings, `--part K/N` and the reuse of results: [docs/SIM_SPEED.md](../../docs/SIM_SPEED.md). | Bench | Step | Run | |---|---|---| | `tb_timing.sv` | 1: timing, video port, timing state port | `sim/tb/run_tb_timing.sh --all` | | `tb_loader.sv` | 2, 8: loader, CRC table, CFG and .rom parsers, map table, page selects, Intellicart bank registers, system ROMs, cart state port | `python3 -I sim/tb/tb_loader.py --all` (step-8 images: sim/testroms/step8; `--no-step8` leaves them out) | | `tb_cpu.sv` | 3: the CP1610 RTL in lockstep with the Python model | `sim/tb/run_tb_cpu.sh --all` | | `tb_system.sv` | 3-6: the machine (CPU, bus fabric, RAMs, STIC, PSG) with timing and loader, against the system model | `python3 -I sim/tb/tb_system.py --all`; the frame gate (steps 4-6) `--screens` | | `tb_input.sv` | 6: the input module against the input model and MAME's hand-controller tables | `python3 -I sim/tb/tb_input.py --all` | | `tb_voice.sv`, `tb_voicebus.sv` | 10: the Intellivoice (`sp0256.sv` alone; `intv_voice.sv` on CPU cycles) against `tools/sp0256.py` | `python3 -I sim/tb/tb_voice.py --all --plants` (needs the MAME references, INTV_VOICE_REFS) | `tb_loader.py` builds `tb_loader.sv` with the real `core_bridge_cmd.v`, agg23's `data_loader.sv`, `apf_datatable.sv` and `intv_media.sv`, plus our own simulation models in `models/` (Intel's `dcfifo` and the template's `mf_datatable`, which Verilator cannot take from Quartus, and the Pocket's AS6C2016 SRAM with its read and write timing checked). For every image of `sim/regress/step2_set.csv` it computes each line the bench prints from the mapper model and compares: CRC, words, layout, flags, slot status, the stored image, ECS/EXEC/GROM/Intellivoice ROMs, the full mapped space at reset paging and at pages 1, 2, 3 and 15 of every 4K range, every 2K of the Intellicart memory through the bank windows (all bank registers swept), after a write to every address, after a machine reset, and the state round trip (save, clear, restore), and last a slot read (0x0080) and "all complete" again with no cartridge stream, which must not run the loader's finish again. The host gives the cartridge's size with 0x0082 too; `apf_datatable` checks the data table's length against it, and four extra cases on the 4n+1 synthetic image cover it with the data table before and after the files: a 0x0082 size 4 bytes long (loaded with the table's length, err[4]) and a table without the cartridge (the 0x0082 size used). Two reload cases load that image and then, in the same session (no power-on reset), another cartridge with its own 0x0082 size: synthetic:b (in the table) and C2063C08 (paged, no table entry); the second must be mapped with its own length. Images are taken from the owner's library by path and extracted only into `sim/work` for the run; the `forced:` rows come from `tools/synth_image.py` (a table CRC, no game data). `--all` then builds 15 planted faults (CRC over the padded length, little-endian words, page table read one entry off, odd last byte, default map not cut, SRAM sampled early, selects kept on machine reset, selects dropped on restore, no size check, no length cross-check, no fallback to the 0x0082 size, the 0x0082 size of any slot, the finish rerun on every "all complete", a reload's byte 0 not arming the finish, only the first 0x0082 size kept); each must fail. `tb_cpu.sv` checks the CP1610 RTL (`src/fpga/core/cp1610.sv`) in lockstep with the Python model, cycle by cycle: bus phase, address and data, BUSAK, halt, TCI/SIN, the cycle every step ends in with R0-R7, S Z O C, I and D there, and the final registers. Cases from `tools/cpu_buslog.py`: the 36 programs of `sim/tests/cpu` (the three asm-error tests have none), 1,420 opcode-sweep cases and 300 random programs, each run once straight and once with save points (state saved mid-instruction, in the interrupt sequence and in bus waits, restored into the other CPU instance, fresh or stale, and run on). `run_tb_cpu.sh --all` also builds planted faults and fails unless the bench catches each one. `tb_system.py` runs `tb_system.sv` (step 4 additions below): `intv_machine` with the real `intv_timing`, `intv_media` and the SRAM model, the system ROMs and the cartridge loaded through the loader's byte port, for 10 frames (149,340 CPU cycles; systest 22). Images: no cartridge, Astrosmash (from the owner's library), `sim/testroms/systest.asm` in the EXEC slot (two different hand-controller bytes), and `sim/testroms/carttest.asm` in the EXEC slot with two synthetic cartridges (`tools/synth_image.py`, CRCs forced to the table's 8-bit cart RAM layout 25 and paged layout 26). For each, the system model (`tools/intv_system.py`) gives every bus phase other than NACT with its cycle, address and data, R0-R7, S Z O C, I and D at every step start, every change of the STIC handshake and mode latch with its cycle, and the RAMs, STIC and PSG registers, latches and page selects at two dumps (cycle 82,670 and the end); every run must match all of it. For systest it also checks that the section-7 probes land in their frame slots (3788-3791, INTRM's last sample and, from step 5, a write to $18 in a display-off frame's last slot, 14,933, where the STIC ORs in the frame's collision bits). Four runs per image: straight, and three save/restore round trips (DESIGN 8.4): into a reset machine at the last cartridge read (or in VBlank), over the state 20,000 cycles later (the run rewinds), and into a reset machine outside VBlank; before every restore all state words, and the 16K cart RAM cells through intv_media's aux port, are overwritten with junk. The freeze lands mid-cycle, the raster runs on and the machine resumes when it comes round. Every state word must also read back as written and be identical across the runs at both dumps. With `INTV_TRACE_ROOT` set, the system model is first compared with MAME's traces (no cartridge: image `none`; Astrosmash). `--all` then builds 32 planted faults, each of which must fail: bus behaviour (pull-ups, GROM / EXEC / scratchpad upper bits, INTRM a line late, CPU started a line early, IAB vector, STIC read mask, STIC always open, STIC aliases, GRAM windows, PSG mask, the mode latch's alias read, open check and write, the window's end one cycle either way, INTRM's end one cycle either way, hand controllers swapped, closed GRAM reads with 11 offset bits, cartridge writes not asked or cut to 8 bits) and the state port (RAM, PSG, held cartridge word, IAB vector flag, handshake, latched address, and restore writes lost in GRAM, scratchpad or System RAM alone). Step 4 (the STIC, `intv_stic.sv`). The model runs in STIC mode `stic` (BUSRQ on, as the core runs it) for every image except `systest` (its probe delays were tuned without BUSRQ) and `astro_nobusrq` (Astrosmash again with `+busrq=0`, model mode `none`). Every run is also compared on the STIC's side: every picture (189 x 240 colours, `+pic`, the model's picture numbering), every row fetch (its cycle's frame, row, loaded or missed, the FIFO address counter, the 20 words loaded) and every card row (card, GROM / GRAM / squares, card address at the row's first line); the dumps add the row buffer and the FIFO counter. Two more round trips where the display is on: a restore into a reset machine 17 clk_sys into a row copy (word 8 of 20, the CPU in BUSAK), and a rewind restore 3 cycles into a fetch line (the CPU in BUSAK, the card engine building the row's first line); the bench saves and restores its own picture capture with the state. Image `sticbus` runs our `sim/testroms/sticbus.asm` (24 frames, BUSRQ on): pseudo-random BACKTAB and GRAM (every card kind), delays, border extensions, Color Stack and FGBG frames, blank frames with a late border colour (it shows from that pixel on: STIC_NOTES D17), a display enabled after the window (blank, STIC_NOTES D8), a frame without INTAK (the FIFO counter past System RAM), shift blocks that make the CPU miss row fetches, and the row-load deadline exactly: BUSAK at R + 57 (the row loads) and at R + 58 (missed); the bench fails (STICBUS_DEADLINE) unless the model shows both. `--all` adds 41 STIC plants (colour-stack advance, restart and row start, vertical and horizontal delay one short, top border extension, BUSRQ three lines a row, frame BUSRQ 43 cycles, GRAM / GROM select swapped, FGBG GROM with 256 cards, squares off, squares quadrants 1 and 2 swapped, FIFO counter not cleared by INTAK or not stopped, missed rows loaded anyway, fetch a line late, display enable read live, border colour a CPU cycle late or latched per frame; the step-4 review's: pastel foreground dropped, FGBG background bits 12/13 swapped, squares colour 7 shown direct, squares quadrant 3 bit 2 dropped, 160th column shown, left extension ignored, pixel order reversed, frame BUSRQ always on, 13th row fetch dropped, Color Stack GROM limited to 64 cards, stack advance on squares cards, top extension moved by VDR, counter stepping on missed rows, row-load deadline a cycle early, a cycle late or two late; state port: row buffer, line buffer, copy state, the frame's latches, the next pixel, the colour-stack position or its row start lost), some of them on the frame gate's games (`('screen', name, pictures)`). `--screens` is the frame gate (DESIGN 9 steps 4 and 5; SCREENS): the EXEC without a cartridge (600 pictures) and with Astrosmash (its title screen, 700), Tower of Doom (700), Diner (300), Dig Dug (700), Sewer Sam (300) and TRON Solar Sailer (700), as far as the MAME snapshots the model was checked against (docs/STIC_NOTES.md 3). The bench runs with `+bushash=1` (one hash a frame of its B and S lines instead of the lines) while the model runs beside it; every picture, row fetch, card row and per-frame bus hash must be equal. Step 5 (MOBs and collisions, `intv_stic.sv`; docs/STIC_NOTES.md 6). The model runs with its MOBs on (`STIC_MOBS = True`; `False` gives step 4's backgrounds only). Pictures now include the MOBs, and the bench writes a K line at every frame's end with the collision bits the frame gathered (ORed into $18-$1F then), compared with the model's per frame; the CPU's reads of $18-$1F are in the bus lines (or the per-frame hash). Two more round trips on every image with MOBs: rt6 restores into a reset machine inside the collision sweep (3 lines after the VBlank window), rt7 rewinds to a state taken inside a line's MOB fetch (pixel clocks 196-203) on a line covered by a MOB; for sticmob, rt8 restores into a reset machine at the start of the sweep's pairs for the one line where two of its MOBs overlap (`sweep_clock` gives the clk_sys); the R lines say where they landed. Image `sticmob` runs `sim/testroms/sticmob.asm` (46 frames: 22 scenes, two passes), which checks its own collision reads against the hardware rules (STICMOB_SELFCHECK: the model's run must end with 0 fails). `--all` adds 37 MOB plants (flips, sizes, YRES, position, X = 0, order, PRIO, pastel, invisible drawn, M1, M2, M4, M5, background, INTR, border edges, pairs, the sweep's range, the bits' clear and stickiness, the CPU's clear; state port: sweep, sweep masks, line masks, gathered bits), and from the step-5 review 17 more: the background-collision area's edges (bottom line, right column, left and top edges with border extension), YFLIP with YRES, the sweep's first line and last row, pairs needing VISB or ignoring INTR, the sweep ignoring its port turn, XFLIP with XSIZE, VDR or HDR ignored for MOBs, the border test moved or not moved by HDR, MAME's PRIO-first order, and the frame's end dropping a CPU write to $18-$1F (systest p11): 128 plants in all. `--screens` adds the ten MOB games of docs/STIC_NOTES.md 5.4 (700 pictures each, MTE201 900). Step 6 (the PSG, `intv_psg.sv`; docs/PSG_NOTES.md 5). The model's PSG (`tools/psg.py`) steps with the system model's cycle count. The bench writes an A line at every frame's end: a hash of every PSG step's three levels in the frame (before the step: the 48 kHz filter's input) and a hash of the 48 kHz samples the frame's steps ended; the driver compares them with the model, steps in every run, samples in straight runs (the filter is not state, so a round trip may move them), and frame by frame in the frame gate. The dumps add the PSG's seven new state words (tone counts and outputs, noise count and prescaler, LFSR, envelope count, step, attack, holding, tick phase); the state port grows to 21 words. Image `psgtest` runs `sim/testroms/psgtest.asm` (22 frames, two different hand-controller bytes) with two more round trips in the middle of a slow envelope: rt9 into a reset machine, rt10 over a later state. `--all` adds 22 PSG plants (LFSR tap, tone compare, envelope period, volume selects, shape restart, alternate, tone-off bit, tick phase, write before the step, filter reciprocal, one low pass, a DAC level, the noise prescaler's phase, Continue = 0 holding, the mixer's noise bits, the port-direction bits obeyed; state port: tone, noise, LFSR, envelope count, envelope step, tick phase) and keeps the step-3 PSG ones (mask, registers lost, ports swapped) on `intv_psg.sv`. The play bench (`tb_system.py --play`, step 6): Astrosmash (1,200 frames) and B-17 Bomber (1,300), each started with a key press and played with scripted keys on both hand controllers (`PLAY`: keypad, side buttons and disc directions, held from one MAME frame notifier to another). The bench reads the keys as `+keys` (the bytes by frame, changing at the frame's first cycle) and the model (`intv_system.press_pads`) applies the same bytes by frame; every picture, row fetch, card row, collision bit, per-frame bus hash and PSG hash must be equal, mode `stic`. With `INTV_PLAY_ROOT` (one `tools/refs/mame_play.sh` OUTDIR per game) the model in mode `mame` must also equal MAME given the same keys: every 10th frame's snapshot pixel for pixel (`stic_frames.py compare --press`) and the PSG register log and step stream (`psg_compare.py --press`). The third case, PGA Golf (760 frames, 0.0.7), presses Pocket buttons (`pocket:L+right`: `press_pads` combines the buttons held in a frame and maps them through the input model, Layer mode): from the start sequence recorded in MAME (research/golf/NOTES.md), Right, L+Right, Up, L+Up, each held 10 frames, and the model's aim, RAM $015F, must read 0, 15, 4, 3 40 frames after each press (`PLAY_PEEKS`), with the RTL equal to the model as above (it has no MAME reference run: Pocket presses). The last line is `TB_PLAY_ALL pass`. `tb_input.py` writes about 561,000 vectors (`sim/work/tb_input/vectors.txt`) and runs `tb_input.sv` (`intv_input.sv` alone) over them: the agreed mapping written out row by row as the hand-controller inputs each Pocket input must press, with MAME's `read_ctrl()` of those inputs as the expected bytes; every combination of the 16 Pocket buttons in each keypad mode (player 1 on the Pocket, player 2 docked); both sticks over their whole range; every keyboard usage code in each report byte and random reports; every slot type on each slot; random vectors; the centres of MAME's analog grid, expected as its table says; and class 9, L + D-pad (0.0.7: in the Layer modes L turns the D-pad one notch clockwise; not in Face-button ring; never the stick): every D-pad state with every set of A/B/X/Y, with and without L and R, every mode, slot types 1-3 with the stick centred and in its 16 directions, on either player, expected from the mapping written out (`l_oracle`), not from the model. Both Swap settings throughout. The bench runs `clk` (clk_sys) and `clk_74a` (period drawn each cycle between 12.5 and 14.5 ns) and drives the inputs as clk_74a signals; each vector is checked 48 clocks after it is driven (16 before step 9's review: the keyboard's decode round adds up to two rounds of 8 clocks). For all but the spec rows and the grid the expected bytes are the model's, and the model must equal MAME's `read_ctrl()` on every vector. `--all` then builds 25 planted faults (Swap ignored, a disc code, the dead zone's edge, a sector slope, stick y flipped, L beating R, no cancel on the ring, the face buttons' left and right swapped, Backspace, the keyboard on any slot type, the keyboard's sixth byte, any slot type a controller, no R3, the right stick in every mode, sticks on the Pocket; for L + D-pad the turn counter-clockwise, no turn, the turn in Face-button ring too, an L chord dropping the disc, an L chord undoing the turn, two notches, L turning the stick; and `bitwise_sync`: the crossing before 0.0.5, every bit through two flip-flops on its own; `copy_while_in_flight`: the copy retaken at every clk_74a edge; `reg_then_bitwise`: a registered copy crossed bit by bit); each must fail. The plants run on the skew build (below). The crossing (`sim/work/tb_input/tear.txt`, 2,000 groups of 2-4 vectors: keyboard codes such as 1E and 21, button sets, Keypad Mode and Swap, the sticks, slot types, random words): the bench changes the inputs among a group's vectors at random clk_74a edges, 64 changes per group, each bit taking its new value at one of four moments within the clk_74a cycle (the routing of a clk_74a bus), and at every clk edge both bytes must be the bytes of one of the group's vectors. This model skews only the raw clk_74a inputs: it shows that `intv_input.sv` does not sample the inputs bit by bit (`bitwise_sync` shows about 27,000 torn bytes over the 930,000 clk edges), but the held copy is a register and changes all at once here, so it says nothing about the handshake. Two more checks do: - the handshake rule, in every run: the copy (`hold_74`) must not change at a clk_74a edge while a transfer is in flight (`req_74 != ack`); the bench counts copies and loads (`copy_while_in_flight` breaks it); - the skew build: `tb_input.py` builds `intv_input.sv` a second time with its clk blocks reading the copy and the request through views the bench drives (`-DTB_SKEW`), each bit arriving at one of eight moments 0.5 to 19.75 ns after it changes (twice the SDC's 10 ns bound, just under one clk period; drawn per group), and runs the crossing groups on it. A clk side that loads before every bit has arrived, or crosses a registered copy bit by bit (`reg_then_bitwise`), shows torn bytes. Neither shows metastability: a load decided on the request's first flip-flop (`req_s[0]`) passes both. That, and the 10 ns net-delay bound, are checked on the fit: `sim/check/fit_crossing_check.py` reads `ap_core.sta.rpt` (DEVELOPING, Build). `tb_savestate.py` runs `tb_savestate.sv` (step 7): `intv_core` (media, timing, machine and the save-state controller `intv_ss_ctl`) with the SRAM model behind the template's `core_bridge_cmd.v` and core_top's bridge multiplexer; the bench is the Pocket's host (command words at 0xF8000000, the blob at 0x50000000, one bridge access every 88 clk_74a). For each image of the regression set and USCF Chess (cart RAM) it runs a straight reference (R), the same run with saves at chosen moments, a menu pause and a save inside a menu pause (S: every frame equal to R, every blob verified by `tools/savestate.py`, its cart RAM equal to the SRAM model's at the freeze; in each menu window the machine's cycle stands still from the freeze to the resume, which spans the wait's raster frames), each state loaded into a fresh core (L: every frame after the restore equal to R's, the SRAM's cart RAM equal to the blob's), the first state loaded into a core that has run 40 frames past it (D), the wake with the host's reset held (W1: reset released after the answer; W2: released while the staged blob is checked), another image's state (F: refused, untouched) and five bad blobs (C: refused, then the good one loads). psgtest also runs S with Pause in Menu off (S0: the machine runs through the menu) and B (two blob reads while a save reads the state: one overwritten access counted, the save unharmed; then six blob reads, each with a blob write arriving while the read is served: every read answered with its own word, every write landed). On every clk the bench checks: never live while frozen, the sound 0 once the machine has not been live for 4 ms, no state access while the machine runs; every run ends with `E drops 0` (B: 1). `--plants` builds eleven planted faults (a module dropped, a word off by one, no CRC check, the resume one CPU cycle late, no cartridge check, no system-ROM check, the cart RAM not restored, the cart RAM one word off, the menu ignored, a bridge access's direction not latched, the PSG's envelope counter ignored on restore), each of which must fail for its own reason (a pattern per plant in `PLANTS`). Frames are machine frames (H, A, P lines: bus and CPU steps, PSG, picture); after a load the PSG's 48 kHz sample values are left out (its output filter is not machine state; docs/SAVESTATE.md 5). Last line `TB_SAVESTATE_ALL pass`. Step 9 (the ECS, docs/ECS_NOTES.md). `tb_system.py`: images ecstest and ecssynth (`sim/testroms/ecstest.asm` with the keyboard and with the synthesizer, keys from `+ekeys`), ecsboot (the ECS's boot with no cartridge), ecsastro (Astrosmash under the Auto rule: attached), the step-9 cartridges ecssound, ecshook, ecsavail, cart4000 and cart4000_on (the ECS forced on over a cartridge at $4000); the dumps add the ECS's words, PSG #2 and the ECS RAM, the E lines PSG #2 and the mixed sound; ecstest's keyboard scans are checked against ECSKBD_WANT (the matrix rule written out by hand) and the cartridges' PASS lines in the model. ECS plants: page decode, RAM width, byte writes, masking off, the 8914's envelope bits, the UART's status, the RAM not owned, the mix not saturated, the latches written while off. `--ecs-play` (TB_ECS_PLAY_ALL): RTL = model frame by frame over the keys of the MAME references (ECS BASIC typing, Mind Strike, WSMLB, Melody Blaster, Mr. BASIC, Astrosmash under Auto, ecssound). `tb_input.py` classes 10-11: the USB keyboard as the ECS keyboard and the synthesizer. `tb_loader.py`: the Auto rule's mode on every image (`ecs auto` lines) and six Auto plants. `tb_savestate.py`: ecsbasic, ecssound, ecsavail (SS_ECS) and the E runs (the own state into a core with the ECS off: refused). After the step-9 review: `tb_system.py` images ecsnorom (ecstest with the ECS on and no ECS ROM slot: every read of $2000, $7000, $E000 is $FFFF in the model, RTL = model), nfl_auto (Auto: on, the table's ECS flag since 0.0.24), atlantis_auto and bodyslam_auto (library games under Auto, the ECS attached: the hardware default), the mix scaled to 3/4 with the ECS attached or on (`mix_sample`), and an `L ecs full` line on every image (intv_ecs's mode-on output, the key routing); plants ecs_rom_ok_ignored, ecs_full_attached, ecs_mix_unscaled, ecs_mix_scaled_off. `tb_input.py`: the vectors' ECS field is the mode (0 off, 1 on, 2 attached); attached keeps the 0.0.8 keypad and no matrix key; plants ecs_always, attached_numpad_only, ecs_mods_always, round_any_load (the decode round taking a word mid-round: torn outputs). `tb_loader.py`: the images ecsoffcfg and ecsoffrom (ECS incompatible: Auto off), plant auto_ecs_off_ignored. `tb_savestate.py`: Atlantis (SS_ECS, attached) and Super NFL Football (on) under Auto, and the X runs (the own state into a core whose ECS ROM file differs by one byte: refused; plant ss_ecs_crc_unchecked). Step 10 (the Intellivoice, docs/VOICE_NOTES.md 8.7). `tb_voice.py`: `sp0256.sv` = `Voice` sample by sample on the six MAME references at 9,285 Hz and 24 random streams, with a save / reset / restore every 50,000 samples; `intv_voice.sv` = `VoiceBus` on 24 random bus programs at 10 kHz and 12 at MAME's rate (read values, sample cycles, `late` 0); 20 plants. `tb_system.py`: images voicetest, voicedac and voice_off (VOICE_OF: sim/testroms/voicetest.py in the EXEC slot), V lines (every voice sample's cycle and value), the mix's hash with the voice (`voice_mix`, `sp0256.VoiceOut`), intv_voice's words in the dumps; plants voice_mapped_off, voice_level2_lost, voice_mix_unsigned, voice_dac_bits. `tb_loader.py`: a `voice auto` line on every image, and the images "voice: CFG voice = 1", "voice: CFG voice_compat = 2", "voice: .rom tag (requires)" and "voice: flagged title, no ivoice.bin". `tb_savestate.py`: voicetest (On) and B-17 Bomber (Auto) in SS_VOICE, save kinds voice and voicewin, the voice's samples in the A lines, the V runs (the own state into a core with the voice off: refused); plants ss_voice_unchecked, ss_voice_acc_lost, ss_voice_busy_ignored.