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