// SPDX-License-Identifier: MIT // The PAL window is the visible area of MAME 0.285 a7800.cpp's a7800p screen (BSD-3-Clause, // Dan Boris, Fabio Priuli, Mike Saarna, Robert Tuccitto; a fact, no code). NOTICE, section 2. //! Frames from MARIA's lines, and a PNG writer. //! //! The board emits colour bytes only (docs/start-here.md "Output"): per line, //! 454 values at MARIA-clock resolution, blank in HBLANK. This stage cuts the //! picture MAME 0.285 shows for comparison: lines 27-250 (a detector setting, not //! a hardware value) and clocks 107-426, the 320 visible clocks (DOC: [GLD] sheet //! 16). The board's line numbers are MAME's (the power-on placement, machine.rs, //! OBSERVED; MSTAT falls on line 16 on both, DOC: [SG]). Both //! show a line's DMA on the next line: MAME's `draw_scanline` swaps its two //! line buffers before it draws, as MARIA does at LRC. //! //! `rgb` is a placeholder palette computed from the colour byte's hue and //! luminance (hue 0 grey; hues 1-15 around the colour wheel at 24 degrees; 16 //! luminance steps; not a hardware value). It is **not** one of the 7800 palettes (the //! wiki's cool/warm/hot charts are the frontend's data); frame comparisons //! against detectors use colour-byte-to-RGB consistency, never this palette. //! //! Rust-forced differences (a7800-python `television.py` at d6b220d): the television //! keeps its frames in a shared cell (`Rc>`) that MARIA's `on_line` closure //! writes; `frames` is read through [`Television::frames`]. The PNG's deflate stream is //! flate2's (zlib's level 6, as Python's `zlib.compress`); the picture is the same. use std::cell::RefCell; use std::io::Write; use std::rc::Rc; use crate::machine::Machine; use crate::maria::Maria; pub const FIRST_LINE: u32 = 27; // detector setting (MAME 0.285's screen), not a hardware value pub const LINES: u32 = 224; pub const FIRST_CLOCK: usize = 107; // DOC: [GLD] sheet 16, the visible clocks pub const WIDTH: usize = 320; // The PAL crop (detector setting): lines 35-294 (260 lines) of the 292 DMA lines 16-307 // (DOC: [MT84] 2.2: 292 active PAL lines; the board's PAL row). It is the comparison // window of MAME 0.285's a7800p screen (a7800.cpp's PAL visible area, a // detector setting like the NTSC one above; the wiki's Software Guide gives // 16-308 as the PAL "visible" screen and 41-281 as the area every television // shows, which this window contains). pub const PAL_FIRST_LINE: u32 = 35; pub const PAL_LINES: u32 = 260; /// (first line, lines) of the comparison window for a machine row. pub fn crop_for(machine: &Machine) -> (u32, u32) { if machine.lines == 313 { (PAL_FIRST_LINE, PAL_LINES) } else { (FIRST_LINE, LINES) } } pub type Frame = Vec>; pub struct TvState { pub first_line: u32, pub lines: u32, pub current: Frame, pub frames: Vec, pub keep: usize, pub count: u64, pub started: bool, } impl TvState { fn blank(&self) -> Frame { vec![vec![0u8; WIDTH]; self.lines as usize] } pub fn line(&mut self, line: u32, pixels: &[u8]) { let row = line as i64 - self.first_line as i64; let lines = self.lines as i64; if (0..lines).contains(&row) { self.current[row as usize] = pixels[FIRST_CLOCK..FIRST_CLOCK + WIDTH].to_vec(); if row == 0 { self.started = true; } if row == lines - 1 { if !self.started { // A frame whose first rows passed before power (the board // powers on at line 250): not a frame. MAME's first // frame_done comes one whole frame after its power-on, so // frame N here is MAME's frame N (detector setting). self.current = self.blank(); return; } self.started = false; let blank = self.blank(); let done = std::mem::replace(&mut self.current, blank); self.frames.push(done); if self.frames.len() > self.keep { let extra = self.frames.len() - self.keep; self.frames.drain(..extra); } self.count += 1; } } } } /// Collects MARIA's lines into frames (`frames` keeps the last `keep`). pub struct Television { pub state: Rc>, } impl Television { pub fn new(maria: &mut Maria, keep: usize, first_line: u32, lines: u32) -> Television { let state = Rc::new(RefCell::new(TvState { first_line, lines, current: vec![vec![0u8; WIDTH]; lines as usize], frames: Vec::new(), keep, count: 0, started: false, })); let s = state.clone(); maria.on_line = Some(Box::new(move |line, pixels, _kill| s.borrow_mut().line(line, pixels))); Television { state } } /// `Television(maria, keep)` with the NTSC crop. pub fn ntsc(maria: &mut Maria, keep: usize) -> Television { Television::new(maria, keep, FIRST_LINE, LINES) } pub fn count(&self) -> u64 { self.state.borrow().count } pub fn last_frame(&self) -> Option { self.state.borrow().frames.last().cloned() } } pub fn rgb(colour: u8) -> (u8, u8, u8) { let (hue, luma) = (colour >> 4, colour & 0x0F); let y = luma as f64 / 15.0; let (i, q) = if hue == 0 { (0.0, 0.0) } else { let angle = (((hue as f64) - 1.0) * 24.0 + 180.0).to_radians(); (0.2 * angle.cos(), 0.2 * angle.sin()) }; let r = y + 0.956 * i + 0.621 * q; let g = y - 0.272 * i - 0.647 * q; let b = y - 1.106 * i + 1.703 * q; let c = |v: f64| (v * 255.0).round_ties_even().clamp(0.0, 255.0) as u8; (c(r), c(g), c(b)) } /// A PNG of RGB rows (8-bit, colour type 2). pub fn png(width: usize, rows: &[Vec]) -> Vec { let mut raw = Vec::new(); for row in rows { raw.push(0); raw.extend_from_slice(row); } let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::new(6)); encoder.write_all(&raw).expect("in memory"); let data = encoder.finish().expect("in memory"); let chunk = |kind: &[u8], data: &[u8]| { let mut out = Vec::new(); out.extend_from_slice(&(data.len() as u32).to_be_bytes()); 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 as u32).to_be_bytes()); header.extend_from_slice(&(rows.len() as u32).to_be_bytes()); header.extend_from_slice(&[8, 2, 0, 0, 0]); let mut out = b"\x89PNG\r\n\x1a\n".to_vec(); out.extend(chunk(b"IHDR", &header)); out.extend(chunk(b"IDAT", &data)); out.extend(chunk(b"IEND", b"")); out } pub fn frame_png(frame: &Frame) -> Vec { let palette: Vec<(u8, u8, u8)> = (0..=255u8).map(rgb).collect(); let rows: Vec> = frame .iter() .map(|line| { line.iter() .flat_map(|&v| { let (r, g, b) = palette[v as usize]; [r, g, b] }) .collect() }) .collect(); png(WIDTH, &rows) }