pcengine_rust/vdc/
background.rs

1//! The background layer: one line of 9-bit pixels from the BAT and the character generator
2//! (`vdc/background.py`).
3//!
4//! [hw: pcetech 5.2; mfr: Hudson HuC6270 manual] Tiles are 8x8, 4 bits per pixel, planar: 16 words
5//! per tile, word `y` holds bitplanes 0 (low byte) and 1 (high byte) of line `y`, word `y + 8`
6//! bitplanes 2 and 3. The BAT starts at VRAM word 0, one word per tile `ppppnnnnnnnnnnnn`: palette
7//! and name, no flips and no priority. Only names 0-2047 exist in 64 KiB of VRAM; the rest read as
8//! 0 here (U23).
9//!
10//! The line is drawn in one go when the display width begins, from the scroll registers latched a few
11//! dots earlier, as Mednafen does (tier 5, U3); a VRAM change during the line shows on the next one.
12//!
13//! Differences forced by Rust: none in behaviour. The reference's per-instance decode cache (a
14//! dict that only saves PyPy time) is not carried over; the eight pixels are decoded each time
15//! (the fork: from a table, a bitplane byte at a time, into the row itself).
16
17pub const BAT_WIDTHS: [usize; 4] = [32, 64, 128, 128]; // MWR D5-D4 [hw: pcetech 5.1 $09]
18pub const BAT_SHIFTS: [u32; 4] = [5, 6, 7, 7];
19pub const BAT_HEIGHTS: [i64; 2] = [32, 64]; // MWR D6
20
21/// The fork: one bitplane byte of a tile line as eight nibbles, bit `7 - x` of the byte at bit
22/// `4 * x`, so the four planes of a line OR into its eight pixels at once (the base board
23/// shifts each pixel's four bits out one at a time).
24const SPREAD: [u32; 256] = {
25    let mut table = [0u32; 256];
26    let mut byte = 0;
27    while byte < 256 {
28        let mut x = 0;
29        while x < 8 {
30            if (byte >> (7 - x)) & 1 != 0 {
31                table[byte] |= 1 << (4 * x);
32            }
33            x += 1;
34        }
35        byte += 1;
36    }
37    table
38};
39
40/// `width` pixels, each `palette << 4 | colour`; colour 0 is transparent.
41pub fn row(
42    vram: &[u16],
43    size: usize,
44    mwr: u16,
45    x_offset: i64,
46    y_offset: i64,
47    width: i64,
48) -> Vec<u16> {
49    let mut out = vec![0; width.max(0) as usize];
50    row_into(vram, size, mwr, x_offset, y_offset, &mut out);
51    out
52}
53
54/// `row`, drawn into `out` (its length is the width).
55pub fn row_into(
56    vram: &[u16],
57    size: usize,
58    mwr: u16,
59    x_offset: i64,
60    y_offset: i64,
61    out: &mut [u16],
62) {
63    let shape = ((mwr >> 4) & 3) as usize;
64    let bat_mask = BAT_WIDTHS[shape] - 1;
65    let bat_row = (((y_offset >> 3) & (BAT_HEIGHTS[((mwr >> 6) & 1) as usize] - 1)) as usize)
66        << BAT_SHIFTS[shape];
67    let line = (y_offset & 7) as usize;
68    // [hw: pcetech 5.2] MWR D1-D0 = 3 is the one-bitplane-pair mode: D7 picks planes 0/1 or
69    // 2/3 and the other pair reads as zero.
70    let mut mask = 0x0F;
71    if mwr & 3 == 3 {
72        mask = if mwr & 0x80 != 0 { 0x0C } else { 0x03 };
73    }
74    let planes = mask * 0x1111_1111u32; // the mask on every pixel of a tile line
75    let mut skip = (x_offset & 7) as u32;
76    let mut column = ((x_offset >> 3) as usize) & bat_mask;
77    let mut at = 0;
78    while at < out.len() {
79        let bat = vram[bat_row | column];
80        let base = (bat & 0xFFF) as usize * 16 + line;
81        let w0 = if base < size { vram[base] } else { 0 };
82        let w1 = if base + 8 < size { vram[base + 8] } else { 0 };
83        let palette = (bat >> 8) & 0xF0;
84        let pixels = (SPREAD[(w0 & 0xFF) as usize]
85            | (SPREAD[(w0 >> 8) as usize] << 1)
86            | (SPREAD[(w1 & 0xFF) as usize] << 2)
87            | (SPREAD[(w1 >> 8) as usize] << 3))
88            & planes;
89        let mut pixels = pixels >> (4 * skip);
90        let take = ((8 - skip) as usize).min(out.len() - at);
91        for slot in &mut out[at..at + take] {
92            *slot = palette | (pixels & 0x0F) as u16;
93            pixels >>= 4;
94        }
95        at += take;
96        skip = 0;
97        column = (column + 1) & bat_mask;
98    }
99}