# SPDX-License-Identifier: MIT import json m = {} m["module-doc"] = '''// SPDX-License-Identifier: MIT //! The HuC6202 video priority controller (VPC): the SuperGrafx's pixel path and its register //! block. Transcribed from supergrafx-python's `vpc.py` (f04d648). //! //! [hw: sgxtech, "Video Priority Controller"] The VPC //! //! - mixes the 9-bit pixel buses of VDC 1 and VDC 2 and hands the result to the VCE //! (`Vpc::mix`, the board's `PixelPath` socket 7: one row per VDC, one row back); //! - keeps two window widths and four 4-bit (enable, enable, priority) groups, one per window area; //! - steers `ST0`-`ST2` to VDC 2 when `$0E` bit 0 is set (`Vpc::st_to_vdc2`, used by `VideoPage`); //! - decodes the video chip area `$FF:0000-03FF` (`VideoPage`, with the compat switch). //! //! A VDC row value is 9 bits: bit 8 sprite, bits 7-4 palette, bits 3-0 colour; a colour of 0 is //! transparent. "Priority between backgrounds and sprites is managed internally to the VDC": the //! VPC sees only the bit-8 sprite flag and the transparency, so it cannot tell a high-priority //! sprite from a low one. //! //! Everything the sources do not settle is a **named switch** with a provisional default and its //! tier, so a later judge changes one line (the reference's `docs/m6-record.md` lists them as U/C //! entries). The reference's class attributes are fields here: `Vpc::priority_labels`, //! `Vpc::window_origin`, `Vpc::window_off_at_or_below`, `VideoPage::st_steers_in_compat`. //! //! **Where Rust forced a difference** (docs/rust-differences.md): the reference's `Vpc` and //! `VideoPage` are two objects sharing state; here `VideoPage` is the board's `PixelPath`, owns //! the `Vpc` and reaches both VDCs through the `BoardPins` handed in with every access, each VDC //! access made with `pins.vdc_read` / `vdc_write` so that the base board's VDC wait can stop and //! repeat it. `store_immediate` is `pins.opcode`. The mid-line dot's ceiling uses the base's //! `py_floordiv`, and window edges are `i64`, so that a switch that makes one negative behaves as //! in Python.''' m["labels-doc"] = '''/// C-SGX1: the two readings of the 2-bit priority codes (`Vpc::priority_labels`).''' m["labels-variant-doc-1"] = ''' /// The default: codes 01b and 10b as the detectors (Mesen2, Beetle PCE) draw them /// [OBSERVED, closed box].''' m["labels-variant-doc-2"] = ''' /// sgxtech's labels [DOC]: the two codes swapped.''' m["WindowOrigin"] = '''/// The window origin per dot divider (U-SGX1; the reference's `WINDOW_ORIGIN` dict, keyed by the /// divider: 4 = 5.37 MHz, 3 = 7.16 MHz, 2 = 10.7 MHz). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct WindowOrigin { /// divider 4, 5.37 MHz pub divider4: i64, /// divider 3, 7.16 MHz pub divider3: i64, /// divider 2, 10.7 MHz pub divider2: i64, } impl WindowOrigin { /// `WINDOW_ORIGIN[div]`; another divider is a `KeyError` in the reference, a panic here. pub fn of(&self, div: i64) -> i64 { match div { 4 => self.divider4, 3 => self.divider3, 2 => self.divider2, _ => panic!("no window origin for dot divider {div}"), } } }''' m["priority_labels-field-doc"] = ''' /// C-SGX1 (undecidable without hardware): which 2-bit priority code means what. [DOC sgxtech] /// 01b = "VDC #1 sprites behind the VDC #2 background" and 10b = "VDC #2 sprites in front of /// the VDC #1 background". [OBSERVED, closed box] Mesen2 and Beetle PCE draw Dai Makai Mura /// (Ghouls 'n Ghosts), which sets code 01b, as if the two were the other way round, and under /// sgxtech's labels the game's statues and score display go missing. A shipped game's picture /// outranks the document's labels, so the default follows the detectors; `Sgxtech` swaps the /// two codes. Tier: two detectors + a game's picture (5), against tier 2 text. /// Decided 2026-11-12 (Aubrey): this default stays. Past the title Dai Makai Mura's cutscene /// and stage 1 lose their statues and score display under `Sgxtech` and are row-identical to /// both detectors under `Detectors` (the reference's docs/m6-record.md); still no console run.''' m["window_origin-field"] = ''' /// U-SGX1: where the window edges fall. A window of width W covers the row's dots /// [0, W - $40 + origin), dots counted from the end of horizontal sync as `mix` counts them. /// [DOC sgxtech "Windows"] the windows start at the leftmost pixel of the physical screen, /// which is a width of $0040, and are set in single pixels. The origin per dot clock (divider /// 4 = 5.37 MHz, 3 = 7.16, 2 = 10.7) is a closed-box measurement (emulator tier: Beetle PCE /// b96c11e and Mesen2 b9fa69d on the reference's scripts/sgx_probe.py window cards), retaken /// 2026-11-19 on base board pcengine-python 3318027, whose display starts 8 dots earlier in /// the row: both 24 at 5.37 and 10.7 MHz; at 7.16 MHz Beetle PCE 38 and Mesen2 16. 38 is /// kept, provisional. (On base board 4c32f26 the same probes gave 32 / 46 (Beetle), 24 /// (Mesen2) / 32: the detectors' edges stayed put against the picture, the picture moved in /// the row.) No library card brings a window edge into view. The measurement is in this /// board's row dots, so it is retaken whenever the base board's line geometry moves. pub window_origin: WindowOrigin,''' m["U-SGX3-comment"] = ''' // U-SGX3: the dot, counted from the end of the line's horizontal sync, at which a register // write takes effect is the first dot whose time is at or after the write (the same rule as // the VCE's palette). A choice of this model, not measured; not a switch.''' m["window_origin-init"] = ''' window_origin: WindowOrigin { divider4: 24, divider3: 38, divider2: 24, },''' m["window_edge"] = ''' /// The first row dot outside a window of `width` (U-SGX1); 0 when the window is off /// (U-SGX2: a width at or below $40 [DOC sgxtech]), at most `dots`. pub fn window_edge(&self, width: u16, div: i64, dots: i64) -> i64 { if width as i64 <= self.window_off_at_or_below { return 0; } dots.min(width as i64 - 0x40 + self.window_origin.of(div)) }''' m["mix_run"] = ''' /// Mix a run of dots of one window area: VDC 1's values `a`, VDC 2's `b`, the area's /// 4-bit group `cfg` (bit 0 VDC 1 on, bit 1 VDC 2 on, bits 3-2 the priority code), onto `out`. fn mix_run(&self, out: &mut Vec, a: &[u16], b: &[u16], cfg: u8) { let n = a.len(); if cfg & 3 == 0 { out.resize(out.len() + n, 0); // [DOC sgxtech] both layers off: colour 0, the overscan too return; } if cfg & 2 == 0 { out.extend_from_slice(a); // VDC 1 alone, its transparent values included return; } if cfg & 1 == 0 { out.extend_from_slice(b); return; } let mut code = (cfg >> 2) & 3; if self.priority_labels == PriorityLabels::Sgxtech && (code == 1 || code == 2) { code ^= 3; // C-SGX1: the document's labels } assert_eq!(n, b.len()); // the reference's zip(strict=True) for (&p, &q) in a.iter().zip(b) { let mut front = p; if p & COLOUR == 0 { // U-SGX5: VDC 1 transparent, VDC 2's value whatever it is, so both transparent // shows VDC 2's backdrop code. Closed-box measurement (Mesen2 b9fa69d, Beetle PCE // b96c11e, the reference's scripts/sgx_probe.py clear-* cards, every dot clock); // MAME huc6202.cpp (BSD-3-Clause) the same. sgxtech silent. front = q; } else if code == 1 { // (detectors' 01b) VDC 2 sprites in front of VDC 1's background, behind its sprites if q & COLOUR != 0 && q & SPRITE != 0 && p & SPRITE == 0 { front = q; } } else if code == 2 && p & SPRITE != 0 && q & COLOUR != 0 && q & SPRITE == 0 { // (detectors' 10b) VDC 1 sprites behind VDC 2's background, in front of its // sprites; [DOC sgxtech] VDC 1 shows only the sprite there, so its background // has a sprite-shaped hole front = q; } out.push(front); } } }''' json.dump(m, open("scratch/vpc_fill.json", "w"))