# Developing the Intellivision core How to build, simulate, check and package the core. The plan is [DESIGN.md](DESIGN.md); section 9 is the build order. ## Layout | Path | What | |---|---| | `src/fpga/ap_core.qpf`, `ap_core.qsf` | Quartus project: Analogue's template settings and pins (as openFPGA-7800 has them), then this core's seed and file lists | | `src/fpga/apf/` | Analogue's APF framework, unmodified | | `src/fpga/core/core_top.v` | APF top level: bridge map, settings, data table, host commands, loader, clocks, wiring | | `src/fpga/core/core_bridge_cmd.v` | Analogue's host/target command handler, unmodified | | `src/fpga/core/pll/` | the PLL, generated (below) | | `src/fpga/core/intv_timing.sv` | master timing: the clock enables, pixel / line / frame counters, state port | | `src/fpga/core/pocket_video.sv` | video windows (scaler modes) and the Pocket's video port | | `src/fpga/core/intv_palette.sv` | the 16 STIC colours (MAME's values, BSD-3 notice in THIRD_PARTY_NOTICES.md) | | `src/fpga/core/test_pattern.sv` | step 1 stand-in for the STIC; from step 4 shown while the machine is held (slot indicators) | | `src/fpga/core/apf_datatable.sv` | the core's side of the APF data table: JLP size, cartridge length | | `src/fpga/core/intv_media.sv` | step 2: loader, cartridge port, system ROMs and SRAM controller together | | `src/fpga/core/intv_loader.sv` | data slots into SRAM and block RAM, CRC32 over the file length, CRC table, page table | | `src/fpga/core/intv_cart.sv` | page table, ECS-style page selects, the CPU's cartridge port, state port | | `src/fpga/core/intv_sysrom.sv`, `intv_sram.sv` | EXEC/GROM/Intellivoice block RAMs; the Pocket SRAM controller | | `src/fpga/core/cp1610.sv` | step 3: the CP1610 CPU, state port | | `src/fpga/core/intv_machine.sv` | steps 3-6: CPU, bus fabric, RAMs, STIC and PSG together, state ports OR-combined | | `src/fpga/core/intv_bus.sv` | the bus fabric: address latch, decode, pulled-up data-in, IAB vectors, INTRM and the bus-copy window, the CPU's start on a frame, the cartridge port | | `src/fpga/core/intv_stic.sv` | step 4: the STIC, backgrounds: registers and handshake, BUSRQ and the row fetches from System RAM, the card engine (GROM / GRAM, colour stack, squares, FGBG), delays and border, the picture; state port | | `src/fpga/core/intv_psg.sv` | step 6: the PSG (AY-3-8914): registers as MAME reads them back, tone, noise and envelope generators, the 48 kHz box filter for the Pocket, the hand controller ports; state port (docs/PSG_NOTES.md) | | `src/fpga/core/intv_core.sv` | step 7: everything on clk_sys under core_top (media, timing, machine, save states), shared with the bench `tb_savestate` | | `src/fpga/core/intv_ss_ctl.sv` | step 7: save, load and menu pause (0x00A0, 0x00A4, 0x00B0), the blob at 0x50000000, the freeze (docs/SAVESTATE.md) | | `src/fpga/core/intv_ss_pkg.sv` | the save-state module ids and blob layout, generated by `tools/savestate.py` | | `src/fpga/core/intv_ram.sv` | block RAM with a state port and a read-only port B for the STIC: scratchpad, System RAM, GRAM, the STIC's row and line buffers | | `src/fpga/core/tables/` | CRC map table and layouts (generated by `tools/gen_crc_table.py`) | | `src/fpga/pocket_utils/` | agg23's data loader and I2S output (MIT), unmodified | | `dist/` | the SD card layout: `Cores/Miasmark.Intellivision/`, `Platforms/intv.json`, `Assets/intv/common/` | | `sim/tb/` | Verilator benches, one per build step | | `sim/check/` | repository checks: `hygiene.sh`, `apf_json_check.py` | | `tools/` | `package.sh`, `reverse_bits.py`; the Python models (CPU, mapper, system) | ## Clocks One PLL from the 74.25 MHz `clk_74a` (DESIGN.md 2.3, NTSC): | Output | Divider | Frequency | Use | |---|---|---|---| | `clk_sys` | 20 | 42.954545 MHz = 12 x 3.579545 MHz | all machine logic, with clock enables | | `clk_mem` | 10 | 85.909091 MHz | memories (from step 2) | | `clk_vid` | 240 | 3.579545 MHz | the pixel clock | | `clk_vid_90` | 240, 90 degrees | 3.579545 MHz | the scaler's sample clock | VCO = 74.25 x (11 + 2449196227 / 2^32) = 859.0909091 MHz. All four share the VCO and their rising edges, so `core_constraints.sdc` puts them in one synchronous group. `intv_timing` makes the enables from `clk_sys`: pixel every 12, PSG every 24, CPU microcycle every 48, locked to the pixel counter (228 pixel clocks = 57 CPU cycles a line, 262 lines, 59.92275 Hz). PAL (4.000 MHz master, VCO 960 MHz, integer M = 12 instead of 11) needs a runtime retune of M and K; it is step 12 (DESIGN.md decision 5). ### Regenerate the PLL `src/fpga/core/pll/pll_core.v` is the tool's output with a header comment added above it; don't edit the parameters by hand. From the repository root (the output directory is outside git). ip-generate runs Java, which the image only lets root start, so the container runs as root and gives the files back to you at the end: ```sh mkdir -p sim/work/pll docker run --rm -e OWNER="$(id -u):$(id -g)" -v "$PWD/sim/work/pll:/out" raetro/quartus:21.1 bash -c 'cd /out && ip-generate --remove-qsys-generate-warning --project-directory=/out --output-directory=/out/pll_core \ --file-set=QUARTUS_SYNTH --component-name=altera_pll --output-name=pll_core \ --system-info=DEVICE_FAMILY="Cyclone V" --system-info=DEVICE=5CEBA4F23C8 --system-info=DEVICE_SPEEDGRADE=8_H6 \ --component-parameter=gui_device_speed_grade=8 --component-parameter=gui_pll_mode="Fractional-N PLL" \ --component-parameter=gui_reference_clock_frequency=74.25 --component-parameter=gui_operation_mode=direct \ --component-parameter=gui_fractional_cout=32 --component-parameter=gui_dsm_out_sel=1st_order \ --component-parameter=gui_en_reconf=false --component-parameter=gui_number_of_clocks=4 \ --component-parameter=gui_output_clock_frequency0=42.954545 --component-parameter=gui_output_clock_frequency1=85.909091 \ --component-parameter=gui_output_clock_frequency2=3.579545 --component-parameter=gui_output_clock_frequency3=3.579545 \ --component-parameter=gui_ps_units3=degrees --component-parameter=gui_phase_shift_deg3=90.0 \ --component-parameter=gui_en_adv_params=true \ --component-parameter=gui_multiply_factor=11 --component-parameter=gui_frac_multiply_factor=2449196227 \ --component-parameter=gui_divide_factor_n=1 --component-parameter=gui_divide_factor_c0=20 \ --component-parameter=gui_divide_factor_c1=10 --component-parameter=gui_divide_factor_c2=240 \ --component-parameter=gui_divide_factor_c3=240 \ --component-parameter=gui_pll_auto_reset=Off; rc=$?; chown -R "$OWNER" /out; exit $rc' ``` Then compare `sim/work/pll/pll_core/pll_core.v` with the tree's file below its header comment; they must be identical. `pll_core.qip` is kept by hand (the instance assignments openFPGA-7800 uses, without its reconfiguration files). On the owner's VM, run any Quartus tool under the shared Quartus lock (openFPGA-7800's docs/ENVIRONMENT.md: one Quartus job at a time). ## Build ```sh docker run --rm --user "$(id -u):$(id -g)" -e HOME=/tmp -v "$PWD:/build" -w /build/src/fpga \ raetro/quartus:21.1 quartus_sh --flow compile ap_core tools/package.sh # release/M_Intellivision_Pocket_.zip ``` `apf/build_id.mif` is written by the pre-flow script on every build and is not tracked. After every build check `src/fpga/output_files/ap_core.sta.summary`: every setup and hold slack must be positive. A clock name you don't recognise there means `core_constraints.sdc` no longer matches the PLL's clock names (`ic|pll|altera_pll_i|cyclonev_pll|counter[n].output_counter|divclk`). Then check the clock crossings with a net-delay bound (the input module, DESIGN.md 7.2; the save states, docs/SAVESTATE.md 1), which the clock groups leave out of the slack summary: ```sh python3 -I sim/check/fit_crossing_check.py # src/fpga/output_files/ap_core.sta.rpt ``` It must print `FIT_CROSSING pass`: both "Crossing net delay" panels with four bounds and no negative slack, one row per kept `word` bit plus the request, and in `intv_ss_ctl` one row per kept bit of a bridge access (`b_we` -> `b_we_s`, `b_w`, `b_d` -> `bp_*`), of a read's word (`r_q` -> `bridge_rd_data`), the two toggles and the eight result bits (-> `st_a`); and the metastability report ("Synchronizer chains") with `req_74`, `ack`, `b_t`, `r_t` and each result bit crossing through two flip-flops (`core/sta_reports.tcl` adds these panels to every fit; until 0.0.6 the panels were called "Input crossing net delay"). The first fit of step 1 (seed 1): 656 ALMs of 18,480, 3 M10K, 1 DSP; worst setup +3.537 ns (`clk_74a`), `clk_sys` +12.999 ns, `clk_vid` +15.690 ns (the `clk_sys` -> `clk_vid` crossing), worst hold +0.139 ns (`clk_74a`, fast 0 C model). With the free-running raster (seed 1): 705 ALMs, 3 M10K, 1 DSP; worst setup +2.840 ns (`clk_74a`), `clk_sys` +13.264 ns, `clk_vid` +15.011 ns, worst hold +0.120 ns (`clk_74a`, fast 0 C model). Step 3, the machine running the EXEC (seed 1): 2,548 ALMs (the machine 1,059: CPU 638, bus fabric 144, STIC stub 225, PSG stub 52), 3,776 registers, 18 RAM blocks (110,860 bits), 2 DSP; worst setup +2.817 ns (`clk_74a`), `clk_sys` +5.077 ns, `clk_vid` +20.167 ns, worst hold +0.111 ns (`clk_vid`, fast 0 C model). The machine's only outputs are the debugging status word on the bridge (0x60000000), which keeps it in the netlist until the STIC drives the picture (step 4). Step 6, the PSG with its sound on the Pocket's I2S (seed 1): 3,693 ALMs (the PSG 338, 379 registers, 1 DSP for the 48 kHz filter's division), 4,645 registers, 24 RAM blocks (140,108 bits), 4 DSP; worst setup `clk_sys` +0.757 ns (slow 85 C; the SRAM data inputs, as before this step), `clk_74a` +3.265 ns; worst hold +0.036 ns (`clk_sys`, fast 0 C); no negative slack. Step 6, input module added to steps 0-5 (seed 1): 3,726 ALMs, 4,879 registers, 140,108 RAM bits, 19 DSP; `intv_input` 326 ALMs, 367 registers and 16 DSP blocks (the sticks' squares and slope products; `multstyle` "logic" would move them to ALMs if DSPs run short); worst setup +0.757 ns (`clk_sys`, the SRAM data inputs, not the input module, whose worst path has +7.457 ns), worst hold +0.038 ns (fast 0 C model). Step 6 merged (branch step6: the PSG, the input module on the PSG's ports, sound on I2S; seed 1): 4,008 ALMs (`intv_psg` 333, `intv_input` 326), 5,170 registers, 24 RAM blocks (140,108 bits), 20 DSP; worst setup `clk_sys` +0.757 ns (slow 85 C, as before), worst hold +0.038 ns (`clk_sys`, fast 0 C); no negative slack. 0.0.5 (the input crossing as one word with a toggle handshake; seed 1): 4,020 ALMs (`intv_input` 339), 5,195 registers, 24 RAM blocks, 20 DSP; worst setup `clk_sys` +0.754 ns (slow 85 C), `clk_74a` +2.880 ns; worst hold +0.038 ns (`clk_sys`, fast 0 C); no negative slack, no critical warnings. The crossing's `set_net_delay` (10 ns) is met with 6.321 ns to spare (slow model; panels "Input crossing net delay" in `ap_core.sta.rpt`, from `core/sta_reports.tcl`); the metastability report finds 26 synchronizer chains (173 before: the old crossing's per-bit pairs). 0.0.6 (step 7: save states, sleep, menu pause, with the step-7 review fixes; seed 1): 6,488 ALMs (`intv_ss_ctl` 749; the rest of the 2,468 more is the modules' state read-back, DESIGN.md 4.5), 6,445 registers, 26 RAM blocks (143,756 bits), 20 DSP; worst setup `clk_sys` +0.757 ns (slow 85 C), `clk_74a` +1.936 ns; worst hold +0.037 ns (`clk_sys`, fast 0 C); no negative slack, no critical warnings. The four `set_net_delay` bounds (the input word and the save states' request, read word and result bits) are met with 6.709 ns to spare (slow model); 40 synchronizer chains. 0.0.7 (L + D-pad turns the disc one notch in Layer mode; seed 1): 6,449 ALMs (`intv_input` 347), 6,450 registers, 26 RAM blocks (143,756 bits), 20 DSP; worst setup `clk_sys` +0.757 ns (slow 85 C), `clk_74a` +2.612 ns; worst hold +0.038 ns (`clk_sys`, fast 0 C); no negative slack, no critical warnings. The four `set_net_delay` bounds are met with 7.122 ns to spare (slow model); FIT_CROSSING pass. 0.0.8 (step 8: BIN + CFG, Intellicart .rom, the map table, page selects, bank registers, the review's page rules and trims; seed 1): 7,913 ALMs (`intv_loader` 1,993 of which `intv_cfgparse` 507 and `intv_romparse` 210; `intv_cart` 552 with `intv_pagesel` 49: the map table is in block RAM now), 7,740 registers, 50 RAM blocks (306,028 bits; the map table 4,096 x 38), 20 DSP; worst setup `clk_sys` +0.757 ns (slow 85 C), `clk_74a` +3.001 ns; worst hold +0.038 ns (`clk_sys`, fast 0 C); no negative slack, no critical warnings. The four `set_net_delay` bounds are met with 6.851 ns to spare (slow model); FIT_CROSSING pass. 0.0.9 (step 9: the ECS, with the step-9 review's fixes; seed 1): 8,750 ALMs (`intv_ecs` 598 with PSG #2; `intv_input` 373: the HID decode round; `intv_loader` 1,996), 8,387 registers, 54 RAM blocks (322,412 bits; the ECS RAM 512 x 32), 21 DSP; worst setup `clk_sys` +0.757 ns (slow 85 C), `clk_74a` +2.734 ns; worst hold +0.036 ns (`clk_sys`, fast 0 C); no negative slack, no critical warnings. The four `set_net_delay` bounds are met with 6.927 ns to spare (slow model); FIT_CROSSING pass. (The first cut of step 9 measured 9,357 ALMs, `intv_input` 988.) ## Simulation ```sh VERILATOR=/opt/verilator-5.040/bin/verilator ~/daria_work/bin/simslot sim/tb/run_tb_timing.sh --all ``` `tb_timing` (step 1) runs `intv_timing`, `test_pattern` and `pocket_video` with `clk_sys` and `clk_vid` as the PLL makes them. It checks, per frame: 228 x 262 pixel clocks, 14,934 CPU and 29,868 PSG enables at fixed pixel phases, 59.92275 Hz between the Pocket's VS pulses, 262 HS pulses, HS to HS always 228 pixel clocks and DE always at the same distance after HS, the active window of each scaler mode (160 x 192, 167 x 210), the slot index after each line, the test pattern's pixels, and the state port. The machine is frozen 40 lines from mid-frame and 300 lines from VBlank (the second at a nonzero sub), refuses three out-of-frame state words, is restored to another position, and a saved word is restored into a second instance whose raster is at another phase: the raster (and so HS / VS / DE) runs on unchanged throughout, the picture is black while the machine is frozen, the machine resumes on exactly its saved clk_sys and loses whole frames only, and the restored copy replays the same machine enables for a frame. `--all` runs both scaler modes at three `clk_vid` phases and then eight planted faults (263 lines, a wrong palette entry, a state word that loses a counter, the raster stopped by the freeze, the machine resuming at once instead of at its position, no blanking while frozen, an out-of-frame state word accepted, live ignoring sub), each of which must fail. ```sh VERILATOR=/opt/verilator-5.040/bin/verilator ~/daria_work/bin/simslot python3 -u -I sim/tb/tb_system.py --all ``` `tb_system` (step 3) runs the machine with the timing and the loader for 10 frames and compares every bus cycle with `tools/intv_system.py`; see `sim/tb/README.md`. Step-3 choices the system model and the RTL share: the CPU leaves reset so that its reset IAB is the last CPU cycle of a frame and its first fetch the first cycle of VBlank (MAME starts its first frame the same way, without the VBlank); EXEC words read with the upper 6 bits 0 and GRAM, scratchpad and PSG bytes with the upper byte 0, as MAME has them (DESIGN 2.5 asks only for the $01xx range; the real bus may pull the EXEC's and GRAM's upper bits high: an open point for a test ROM on hardware); GROM reads $FF00 | byte; the first IAB after a machine reset reads $1000, later ones $1004. The save-state module ids are fixed in `tools/savestate.py` (step 7; `intv_ss_pkg.sv`). ```sh VERILATOR=/opt/verilator-5.040/bin/verilator SIMSLOT=~/daria_work/bin/simslot \ python3 -u -I sim/tb/tb_savestate.py --plants --jobs 2 ``` `tb_savestate` (step 7) runs `intv_core` behind the template's command handler as the Pocket drives it: saves asked for in the middle of an instruction, inside a BUSRQ, during a row fetch and while the PSG sounds, a menu pause (the machine's time frozen, the sound down to 0), sleep and wake (also with the host's reset held), each saved state loaded into a fresh core and one into a core that has run past it, the cart RAM compared with the SRAM at every freeze and load, a state of another image and five bad blobs refused; every machine frame compared with a straight run (docs/SAVESTATE.md 6). Each simulation runs under `SIMSLOT`, at most `--jobs` at once. `python3 -I tools/savestate.py gen --check` keeps the package and the doc table equal to the layout. The benches build through ccache at `-O2` (`sim/tb/tbcommon.py`); plants stop at their first failing run; tb_system runs each image in a process of its own; `--part K/N` (tb_system, tb_loader, tb_savestate's plants) splits a long bench over the simulation slots, and `sim/check/run_cache.py` (opt-in, `RUN_CACHE`) reuses a pass for byte-identical inputs only. Where the time goes and the rules: [SIM_SPEED.md](SIM_SPEED.md). ## Checks ```sh python3 sim/check/apf_json_check.py sim/check/hygiene.sh --names --data --data ``` `apf_json_check.py` holds every `dist/` file to Analogue's documented limits and to the RTL that answers it (slot addresses and ids, the data table, menu addresses and defaults, scaler modes against `pocket_video`'s windows and the pixel aspect). Run both before each commit. ## Pocket (APF) notes - **Data slots** (DESIGN.md 5.6): bridge addresses `0x0N000000`, N = the slot's number (bits 27:24 reach `intv_loader` through the data loader). EXEC, GROM, ECS and Intellivoice ROMs load at start from `/Assets/intv/common/` (parameters `0x88`); cartridge and CFG are reloadable with a core reload (`0x109`); JLP flash is one nonvolatile file per game (`0x84`), reported as 0 bytes until step 11. - **Cartridge length**: the bridge sends whole 32-bit words, so the loader takes the file's length from the data table (slot 0x100's size, read by `apf_datatable` when the host sends "all complete", 0x008F) and keeps the CRC after each of the last four bytes; strobes past the length are bridge padding (tools/mapper/README.md). The loader finishes about 3 ms after "all complete"; `ready` then holds the machine (step 3 on). - **SRAM** (AS6C2016-55, 128K x 16): `intv_sram` reads in 3 clk_sys (69.8 ns >= 55 ns) and writes with a 3-clock WE pulse; the map is in `intv_media.sv` (image at file offsets, ECS ROM at $10000, cart RAM at $18000). The pins have no I/O timing constraints: the controller registers every output and keeps a whole clock of margin on each edge. - **Save states** (step 7, docs/SAVESTATE.md): the 153,600-byte blob at `0x50000000`; `sleep_supported` true; menu setting Pause in Menu (`0x210`, on by default) freezes the machine while the menu is open. - **Menu**: Reset (`0x200`) and Border (`0x204`: show = scaler slot 1, 167 x 210; hide = slot 0, 160 x 192). - **Power-up values** go on internal declarations, never on output ports (openFPGA-7800's DEVELOPING.md explains why).