==== module-doc (from line 1) //! The HuC6202 video priority controller (VPC): the SuperGrafx's pixel path and its register //! block (`vpc.py`). //! //! [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). //! //! Differences forced by Rust: //! //! - **`VideoPage` is the board's `PixelPath` and owns the `Vpc`.** The reference has two objects //! that share state by reference: `Vpc` is the pixel path, `VideoPage` claims the pages and //! holds the VPC and both VDCs. Here one trait object is both (the base board's //! `sockets::PixelPath` may claim pages), `VideoPage::vpc` is the VPC, and the VDCs are //! `pins.vdcs`, handed in with every access. Each access steered to a VDC is made with //! `pins.vdc_read` / `vdc_write`, so that the base board's wait reaches it (pcengine-python //! issue 0004: where the reference's VDC calls the board's `_serve_vram` from inside the //! access, the base board serves the chip after the access stops and makes it again). //! - **`store_immediate`** is `pins.opcode`: the reference's page calls back into the //! `SuperGrafx` object, which reads the core's `opcode`; here the board hands the opcode to //! the device with each access (and 0 with no CPU, where the reference answers `False`). //! - **The named switches** are fields (`priority_labels`, `window_origin`, //! `window_off_at_or_below`, `VideoPage::st_steers_in_compat`) where the reference has class //! attributes; `PRIORITY_LABELS`' two strings are the enum `PriorityLabels`, and //! `WINDOW_ORIGIN`'s dict by dot divider is `WindowOrigin`. //! - **Save states**: `DeviceState` (bytes out, bytes in), where the reference's state walks the //! object graph. As there, the registers, the shadows, the write log and the position of the //! compatibility switch are in a state; the named switches are the system's own. //! - **Reset**: `PixelPath::reset` (called by `Board::reset`) is what the reference's //! `SuperGrafx.reset` does after the board's: a power-on reset resets the VPC. ==== labels-doc (from line 70) /// C-SGX1: which reading of the 2-bit priority codes (`Vpc::priority_labels`). ==== labels-variant-doc-1 (from line 73) /// Mednafen's and Mesen2's reading, and Dai Makai Mura's behaviour (the default). ==== labels-variant-doc-2 (from line 75) /// sgxtech's labels: the two codes swapped. ==== WindowOrigin (from line 79) /// U-SGX1: the window origin per dot clock (`Vpc::window_origin`), by the dot divider. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct WindowOrigin { /// 4 master clocks a dot: 5.37 MHz pub divider4: i64, /// 3: 7.16 MHz pub divider3: i64, /// 2: 10.7 MHz pub divider2: i64, } impl WindowOrigin { /// The reference's `WINDOW_ORIGIN[div]` (a `KeyError` there for another divider). pub fn of(&self, div: i64) -> i64 { match div { 4 => self.divider4, 3 => self.divider3, 2 => self.divider2, _ => panic!("no window origin for a dot divider of {div}"), } } } ==== priority_labels-field-doc (from line 105) /// C-SGX1 (undecidable without hardware): which 2-bit priority code means what. sgxtech says /// 01b = "VDC #1 sprites behind the VDC #2 background" and 10b = "VDC #2 sprites in front of /// the VDC #1 background"; Mednafen and Mesen2 (tier 5) read the same register the other way /// round, and Mednafen's source says Dai Makai Mura (Ghouls 'n Ghosts) *uses setting 1 and /// expects VDC #2 sprites in front of the VDC #1 background*. A shipped game that works on /// hardware outranks the document's labels, so the default follows the detectors. `Sgxtech` /// swaps the two codes. Tier: detectors + a game's behaviour (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. ==== window_origin-field (from line 117) /// U-SGX1: where the window edges fall. A window of width W covers the row's dots /// [0, W - 0x40 + ORIGIN\[dot_divider\]) and is off for W <= 0x40 (sgxtech: "values smaller /// than $0040 disable the window"; "windows start from the leftmost pixel of the physical /// screen"). The origin per dot clock (4 = 5.37 MHz, 3 = 7.16, 2 = 10.7) is Mednafen's /// calibrated `8 + 24`/`8 + 38`; Mesen2 uses 48 at every clock. Tier 5; the pictures decide /// (the reference's docs/m6-record.md). pub window_origin: WindowOrigin, ==== U-SGX3-comment (from line 128) // 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); Mesen2 and Mednafen both draw the line up to the write first. // (Documented behaviour of this model, tier 5; not a switch.) ==== window_origin-init (from line 151) window_origin: WindowOrigin { divider4: 32, divider3: 46, divider2: 32, }, ==== window_edge (from line 263) /// The first row dot outside a window of register value `width` (0 = no window). pub fn window_edge(&self, width: u16, div: i64, dots: i64) -> i64 { let width = width as i64; if width <= self.window_off_at_or_below { return 0; } dots.min(width - 0x40 + self.window_origin.of(div)) } ==== mix_run (from line 304) /// One window area: `cfg` bit 0 enables VDC 1, bit 1 VDC 2, bits 3-2 the priority code. fn mix_run(&self, out: &mut Vec, a: &[u16], b: &[u16], cfg: u8) { let (enable1, enable2, mut code) = (cfg & 1 != 0, cfg & 2 != 0, (cfg >> 2) & 3); if !enable1 && !enable2 { // [hw: sgxtech] both layers off: colour 0 of the VCE table for every pixel, overscan too out.extend(std::iter::repeat_n(0, a.len())); return; } if !enable2 { out.extend_from_slice(a); return; } if !enable1 { out.extend_from_slice(b); return; } if (code == 1 || code == 2) && self.priority_labels == PriorityLabels::Sgxtech { code = 3 - code; } let pairs = a.iter().zip(b.iter()); if code == 1 { // VDC 2 sprites in front of the VDC 1 background, behind VDC 1 sprites out.extend(pairs.map(|(&p, &q)| { if q & SPRITE != 0 && p & SPRITE == 0 && q & COLOUR != 0 { q } else if p & COLOUR != 0 { p } else { q } })); } else if code == 2 { // VDC 1 sprites behind the VDC 2 background (they stay in front of VDC 2 sprites) out.extend(pairs.map(|(&p, &q)| { if p & SPRITE != 0 && q & SPRITE == 0 && q & COLOUR != 0 { q } else if p & COLOUR != 0 { p } else { q } })); } else { // 00b and 11b: VDC 1 over VDC 2 wherever VDC 1 is opaque out.extend(pairs.map(|(&p, &q)| if p & COLOUR != 0 { p } else { q })); } } }