diff --git a/src/vdc/background.rs b/src/vdc/background.rs index a2cb7c0..4ecbb01 100644 --- a/src/vdc/background.rs +++ b/src/vdc/background.rs @@ -21,7 +21,7 @@ pub const BAT_HEIGHTS: [i64; 2] = [32, 64]; // MWR D6 /// The fork: one bitplane byte of a tile line as eight nibbles, bit `7 - x` of the byte at bit /// `4 * x`, so the four planes of a line OR into its eight pixels at once (the base board /// shifts each pixel's four bits out one at a time). -const SPREAD: [u32; 256] = { +pub(crate) const SPREAD: [u32; 256] = { let mut table = [0u32; 256]; let mut byte = 0; while byte < 256 { diff --git a/src/vdc/sprites.rs b/src/vdc/sprites.rs index 229aaa9..afeb8d7 100644 --- a/src/vdc/sprites.rs +++ b/src/vdc/sprites.rs @@ -21,6 +21,8 @@ use serde::{Deserialize, Serialize}; +use crate::vdc::background::SPREAD; + pub const HEIGHTS: [i64; 4] = [16, 32, 64, 64]; pub const HEIGHT_MASKS: [u32; 4] = [!0, !2, !6, !6]; /// [mfr: SM table; emu: Mesen2] dots of fetch time per entry @@ -128,6 +130,71 @@ pub fn scan_into( /// /// Returns the display column of the first pixel where sprite 0 hit another sprite, or None. pub fn draw(entries: &[Entry], width: i64, row: &mut [u16], first: usize) -> Option { + // The fork: the base board keeps a line buffer of the whole width, draws every entry's + // sixteen pixels into it one bit of each plane at a time, then merges every column of + // the width into the line. Here the buffer is on the stack and only the columns under + // an entry are cleared and merged (a merge done twice, where two entries overlap, + // changes nothing more), and a pattern row's pixels come from the bitplane table. The + // pixels, their order and the collision test are the base board's. + let width = width.max(0); + if width as usize > LINE_DOTS { + return draw_wide(entries, width, row, first); + } + let mut buffer = [0u16; LINE_DOTS + 32]; + // an entry's sixteen columns, clipped to the width, as indices of `buffer` + let span = |entry: &Entry| { + let low = entry.x.clamp(0, width) as usize; + let high = (entry.x + 16).clamp(0, width) as usize; + low..high + }; + let mut hit: Option = None; + for entry in entries.iter().rev() { + let [p0, p1, p2, p3] = entry.planes; + // pixel x of the unflipped row (bit 15 - x of each plane) in nibble x + let pixels = spread(p0) | (spread(p1) << 1) | (spread(p2) << 2) | (spread(p3) << 3); + if pixels == 0 { + continue; + } + let high = if entry.priority { 0x300 } else { 0x100 } | entry.palette; + for column in span(entry) { + let x = column as i64 - entry.x; + let nibble = if entry.hflip { 15 - x } else { x }; + let raw = ((pixels >> (4 * nibble)) & 0x0F) as u16; + if raw == 0 { + continue; + } + let slot = &mut buffer[column]; + if entry.sprite0 && *slot & 0x0F != 0 && hit.is_none_or(|h| (column as i64) < h) { + hit = Some(column as i64); + } + *slot = high | raw; + } + } + for entry in entries { + for column in span(entry) { + let value = buffer[column]; + if value & 0x0F != 0 { + let target = first + column; + if row[target] & 0x0F == 0 || value & 0x200 != 0 { + row[target] = value & 0x1FF; + } + } + } + } + hit +} + +/// The most dots a line has (1365 master clocks at 2 a dot). +const LINE_DOTS: usize = 682; + +/// A plane's sixteen bits as sixteen nibbles, bit `15 - x` at bit `4 * x`. +#[inline] +fn spread(plane: u16) -> u64 { + SPREAD[(plane >> 8) as usize] as u64 | ((SPREAD[(plane & 0xFF) as usize] as u64) << 32) +} + +/// `draw` as the base board has it, for a width no line has. +fn draw_wide(entries: &[Entry], width: i64, row: &mut [u16], first: usize) -> Option { let mut buffer = vec![0u16; width.max(0) as usize]; let mut hit: Option = None; for entry in entries.iter().rev() {