// SPDX-License-Identifier: MIT //! The television: lines and VSYNC in, frames out. Nothing on the board depends on it. //! //! The TIA has no vertical counter (docs/start-here.md "The machines"): a frame is //! whatever the program puts between its VSYNC pulses. This stage collects the //! TIA's lines and starts a new frame when VSYNC ends, as a television's vertical //! sync does, with a bound on runaway frames for programs that stop sending it. //! //! A frame is a list of lines, each 160 bytes (the colour clocks after the //! 68-clock horizontal blank, `TIA.pixels` encoding: colour with D0 set, or 0 //! for blank). [`frame_rgb`] and [`frame_png`] turn them into colours //! through a palette (palettes.rs, each labelled with its origin). //! //! Rust-forced differences (a2600-python `television.py` at 28fb095): the //! television's state is shared with the line callback it installs on the //! TIA (`Rc>`), so `frames_seen` and `frame` are methods; the //! PNG's zlib stream comes from flate2 at the default level, which need not //! be byte-identical to Python's zlib (the pixels are). use std::cell::RefCell; use std::io::Write; use std::rc::Rc; use crate::palettes::{palette, Palette}; use crate::tia::TIA; pub const HBLANK_CLOCKS: usize = 68; // DOC: SPG, 68 colour clocks of horizontal blank // A frame longer than this is cut (no VSYNC). HYPOTHESIS: a television's // vertical oscillator free-runs somewhere past 312 lines; the value only // bounds a runaway program's frame here and changes no emulated state. pub const MAX_LINES: usize = 400; pub type Frame = Vec>; #[derive(Default)] pub struct TelevisionState { pub frames_seen: u64, pub frame: Option, // the last complete frame lines: Frame, vsync: bool, } impl TelevisionState { fn line(&mut self, tia: &TIA) { let vsync = tia.vsync; if (self.vsync && !vsync) || self.lines.len() >= MAX_LINES { self.frame = Some(std::mem::take(&mut self.lines)); self.frames_seen += 1; } self.vsync = vsync; self.lines.push(tia.pixels[HBLANK_CLOCKS..].to_vec()); } } #[derive(Clone)] pub struct Television { pub state: Rc>, } impl Television { pub fn new(tia: &mut TIA) -> Television { let state = Rc::new(RefCell::new(TelevisionState::default())); let shared = state.clone(); tia.on_line = Some(Box::new(move |tia: &TIA| shared.borrow_mut().line(tia))); Television { state } } pub fn frames_seen(&self) -> u64 { self.state.borrow().frames_seen } /// The last complete frame. pub fn frame(&self) -> Option { self.state.borrow().frame.clone() } } /// `frame` as rows of RGB bytes: blank as black, each colour clock `scale_x` wide. pub fn frame_rgb(frame: &[Vec], palette: &Palette, scale_x: usize) -> Vec> { let table: Vec> = (0..256u32) .map(|v| { if v & 1 != 0 { let (r, g, b) = palette.rgb(v as u8); [r, g, b].repeat(scale_x) } else { vec![0; 3 * scale_x] } }) .collect(); frame .iter() .map(|line| line.iter().flat_map(|&v| table[v as usize].iter().copied()).collect()) .collect() } /// A PNG of `frame` in `palette` (default NTSC, palettes.rs). pub fn frame_png(frame: &[Vec], scale_x: usize, palette_: Option<&Palette>) -> Vec { let rows: Vec = frame_rgb(frame, palette_.unwrap_or_else(|| palette("ntsc")), scale_x) .into_iter() .flat_map(|row| std::iter::once(0u8).chain(row)) .collect(); let width = if frame.is_empty() { 0 } else { (frame[0].len() * scale_x) as u32 }; let height = frame.len() as u32; fn chunk(kind: &[u8], data: &[u8]) -> Vec { let mut out = (data.len() as u32).to_be_bytes().to_vec(); out.extend_from_slice(kind); out.extend_from_slice(data); let mut crc = crc32fast::Hasher::new(); crc.update(kind); crc.update(data); out.extend_from_slice(&crc.finalize().to_be_bytes()); out } let mut header = Vec::new(); header.extend_from_slice(&width.to_be_bytes()); header.extend_from_slice(&height.to_be_bytes()); header.extend_from_slice(&[8, 2, 0, 0, 0]); let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); encoder.write_all(&rows).expect("in memory"); let compressed = encoder.finish().expect("in memory"); let mut png = b"\x89PNG\r\n\x1a\n".to_vec(); png.extend(chunk(b"IHDR", &header)); png.extend(chunk(b"IDAT", &compressed)); png.extend(chunk(b"IEND", b"")); png }