/// The VRAM serial port through a run of border lines (`serial_line`). serial: Option, /// A border line's pixels where a forced plane shows the serial port, RGB888. serial_rows: Vec>>, done_serial_rows: Vec>>, /// The VRAM serial port as a run of border lines finds it: the row the last transfer /// loaded, as the 64 four-byte groups the pointer walks, where the pointer stood after /// it, and how many lines have passed since. #[derive(Clone)] struct SerialPort { groups: [[u8; 4]; 64], column: i64, lines: i64, } serial: None, serial_rows: Vec::new(), done_serial_rows: Vec::new(), self.serial_rows = vec![None; 512]; self.done_serial_rows = vec![None; 512]; self.serial = None; std::mem::swap(&mut self.serial_rows, &mut self.done_serial_rows); self.serial_rows.fill(None); /// A border line with plane A forced on (debug bits 8-7 = 10) shows the VRAM serial /// port. The plane's pattern registers keep loading four bytes from it for every /// eight pixels, and no transfer reloads it, so the pointer runs on, one byte per /// half dot clock, through the row the last transfer before the border loaded: the /// fourth sprite tile fetch for the first border line, at H $14E, after which the /// border stops the fetches. In 64 KB mode a row is VRAM bytes `R << 2` stepped by /// $400 in groups of four, so each eight pixels show the pattern row at /// `(R << 2) | (column >> 2 << 10)`, with the palette of the last tiles fetched /// (Nuked-MD `vdp.c`'s plane pipeline and `vram.c`'s serial port, traced on Overdrive /// 2's arcade, die frames 12,980-13,021). fn serial_line(&mut self, vdp: &VdpState, row: usize) { if let Some(port) = &mut self.serial { port.lines += 1; } else { let address = match &self.scanned { Some(((r, width, im2), latched)) if *r == row as i64 => { Renderer::fetch_address(latched, *width, *im2, 3).unwrap_or(self.slots[3]) } _ => self.slots[3], }; let r = (address >> 2) & 0xFF; let mut groups = [[0u8; 4]; 64]; for (g, group) in groups.iter_mut().enumerate() { let base = (r << 2) | (g << 10); for (i, byte) in group.iter_mut().enumerate() { *byte = vdp.vram[base + i]; } } let column = ((address & 3) | ((address >> 10) << 2)) as i64; self.serial = Some(SerialPort { groups, column, lines: 0, }); } if vdp.debug_register & 0x180 != 0x100 || vdp.hmode.name != HName::H40 { return; } let port = self.serial.as_ref().expect("set above"); let (phase, group_phase) = serial_phase(); let palette = make_palette(&vdp.cram); let width = LEFT_BORDER + 320 + RIGHT_BORDER; let mut line = Vec::with_capacity(width); for x in 0..width as i64 { let i = (x - group_phase).rem_euclid(8); let start = x - i; let pointer = (port.column + 840 * port.lines + 2 * start + phase).rem_euclid(256); let group = &port.groups[(pointer >> 2) as usize]; let byte = group[(i >> 1) as usize]; let pixel = if i & 1 == 0 { byte >> 4 } else { byte & 15 }; let attribute = self.last_attributes[((start - group_phase) >> 3 & 1) as usize]; line.push(palette[0][usize::from(attribute | pixel)]); } self.serial_rows[row] = Some(line); } /// The address sprite tile fetch `n` reads for a line with these latched sprites, or /// None if fewer tiles are fetched (`sprite_draw`'s order). fn fetch_address(latched: &[Latched], width: usize, im2: bool, n: usize) -> Option { let pattern_bytes: i64 = if im2 { 64 } else { 32 }; let tiles = sprite_limits(width).2 as usize; let mut count = 0; for &(_, _, flip_x, cells_x, cells_y, pattern, cell_row, pattern_line) in latched { for cell in 0..cells_x { if count == tiles { return None; } if count == n { let column = if flip_x { cells_x - 1 - cell } else { cell }; let tile = pattern + column * cells_y + cell_row; return Some(((tile * pattern_bytes + pattern_line * 4) & 0xFFFF) as usize); } count += 1; } } None } /// The serial port's phase against the raster: what the pointer adds at a group of /// eight pixels beyond its column and two bytes a pixel, and where the groups begin, in /// pixels of the 347 (fitted to the die's border lines; any phase of 162-165 reads the /// same groups). fn serial_phase() -> (i64, i64) { (164, 5) }