// SPDX-License-Identifier: MIT //! The desktop window's picture and keys, without the window (milestone 8): the //! palettes, the composite option, the key map and what each key does to the board. //! The window itself is `src/bin/a7800-play.rs` (feature `frontend`, SDL2). //! //! Picture: the television stage's window (television.rs: the frame MAME shows, //! NTSC lines 27-250 or the PAL window), 320 clocks wide, each colour byte through //! one of three palettes. The palettes are frontend data computed from the wiki's //! colour documentation (DOC, tier 3, "Atari 7800 Color Documentation"): one uniform //! saturation for hues 1-15, none for hue 0, sixteen luminance steps, and the hue //! step that depends on the console's tuning and warmth: **cool** 24.7 degrees (a //! cold console), **warm** 26.7 (the second published tuning standard), **hot** //! 27.7 (fully warmed up). Where hue 1 sits (on the colour burst) and the //! saturation and luminance scales are fits, not measurements (FITTED in claims.md's //! word, but the judge checks only the hue step, so nothing constrains them: in effect //! HYPOTHESIS; a capture of a console's video output would settle it). //! //! Composite artefacts (`--composite`, off by default; a fit, as above): the wiki (DOC) //! says a 320-mode pixel is half a colour-burst cycle, so alternating light and dark //! pixels read as colour, one hue with light pixels on even positions and //! another with dark ones there, and pairs give a weaker striped set. The option //! averages luminance over each two-pixel cycle and adds the alternation as //! chroma on one fixed axis (`ARTEFACT_ANGLE`, a fit) on top of the pixels' //! own chroma averaged the same way. //! //! Keys: arrows = joystick 1, Z / X = its left and right buttons (Space = both), //! Enter = Reset, Tab = Select, P = Pause, F1 / F2 toggle the left and right //! difficulty switches, Escape quits. //! //! Rust-forced differences (a7800-python `frontend.py` at d6b220d): pygame-ce is SDL2 //! through the `sdl2` crate, in the binary; the key handling that the reference's //! `Frontend.handle` does is [`Keys::handle`] here, on action names, so the binary maps //! SDL key codes to [`KEYS`]' names and the judge can drive it without a window. Python's //! `round` (half to even) is `round_ties_even`. use std::collections::BTreeSet; use crate::controllers::Stick; use crate::system::System; pub const PALETTES: [(&str, f64); 3] = [("cool", 24.7), ("warm", 26.7), ("hot", 27.7)]; // hue step, degrees (DOC: wiki) // The next three: "fits" no judge constrains (HYPOTHESIS; a console video capture would // settle them); the judge checks only the hue step and the read-back. pub const BURST_ANGLE: f64 = 180.0; // hue 1 on the colour burst, in the U/V plane (a fit) pub const SATURATION: f64 = 0.2; // chroma amplitude, luminance 0-1 (a fit) pub const ARTEFACT_ANGLE: f64 = 135.0; // the artefact axis (a fit) pub fn step_of(kind: &str) -> Option { PALETTES.iter().find(|(k, _)| *k == kind).map(|(_, s)| *s) } fn yuv_rgb(y: f64, u: f64, v: f64) -> [u8; 3] { // The standard YUV -> RGB matrix: a display conversion, not a hardware value. let r = y + 1.140 * v; let g = y - 0.395 * u - 0.581 * v; let b = y + 2.032 * u; let c = |x: f64| (x * 255.0).round_ties_even().clamp(0.0, 255.0) as u8; [c(r), c(g), c(b)] } /// (U, V) of a colour byte's hue: none for hue 0, else on the wheel at `step`. pub fn chroma(colour: u8, step: f64) -> (f64, f64) { let hue = colour >> 4; if hue == 0 { return (0.0, 0.0); } let angle = (BURST_ANGLE + (hue as f64 - 1.0) * step).to_radians(); (SATURATION * angle.cos(), SATURATION * angle.sin()) } /// 256 RGB triples, index = MARIA colour byte. pub fn palette(kind: &str) -> Vec<[u8; 3]> { let step = step_of(kind).expect("cool, warm or hot"); (0..=255u8) .map(|colour| { let (u, v) = chroma(colour, step); yuv_rgb((colour & 0x0F) as f64 / 15.0, u, v) }) .collect() } /// A frame of colour bytes as packed RGB rows (320 pixels a row). pub fn render(frame: &[Vec], kind: &str, composite: bool) -> Vec { let mut out = Vec::with_capacity(frame.len() * 320 * 3); if !composite { let table = palette(kind); for row in frame { for &c in row { out.extend_from_slice(&table[c as usize]); } } return out; } let step = step_of(kind).expect("cool, warm or hot"); let axis = (ARTEFACT_ANGLE.to_radians().cos(), ARTEFACT_ANGLE.to_radians().sin()); for row in frame { let n = row.len(); for x in 0..n { let (a, b) = (row[x], row[if x + 1 < n { x + 1 } else { x }]); let (ya, yb) = ((a & 0x0F) as f64 / 15.0, (b & 0x0F) as f64 / 15.0); let (ua, va) = chroma(a, step); let (ub, vb) = chroma(b, step); let alt = (ya - yb) / 2.0 * (if x % 2 == 0 { 1.0 } else { -1.0 }); // light on even: +, dark on even: - let y = (ya + yb) / 2.0; let u = (ua + ub) / 2.0 + alt * axis.0; let v = (va + vb) / 2.0 + alt * axis.1; out.extend_from_slice(&yuv_rgb(y, u, v)); } } out } // Key -> action (SDL key names, as pygame's); not hardware values. pub const KEYS: [(&str, &str); 12] = [ ("up", "up"), ("down", "down"), ("left", "left"), ("right", "right"), ("z", "b1"), ("x", "b2"), ("space", "b12"), ("return", "reset"), ("tab", "select"), ("p", "pause"), ("f1", "left-difficulty"), ("f2", "right-difficulty"), ]; pub fn action_of(key: &str) -> Option<&'static str> { KEYS.iter().find(|(k, _)| *k == key).map(|(_, a)| *a) } /// The held keys and the seeded faults of the window (`Frontend.held`, `faults`). #[derive(Default)] pub struct Keys { pub held: BTreeSet<&'static str>, pub faults: BTreeSet<&'static str>, } impl Keys { /// `Frontend.handle` for one key event, by action. pub fn handle(&mut self, system: &mut System, action: &'static str, down: bool) { if self.faults.contains("keys") && action == "right" { return; } let riot = system.board().riot.clone(); // SWCHB: difficulties D6/D7, reset D0, select D1, pause D3, low = pressed (DOC: [SG]) if action.ends_with("-difficulty") { if down { riot.borrow_mut().input_b ^= if action == "left-difficulty" { 0x40 } else { 0x80 }; } return; } if action == "reset" || action == "select" || action == "pause" { let bit = match action { "reset" => 0x01, "select" => 0x02, _ => 0x08, }; let mut r = riot.borrow_mut(); r.input_b = if down { r.input_b & !bit } else { r.input_b | bit }; return; } if down { self.held.insert(action); } else { self.held.remove(action); } let held = &self.held; system.controllers.set( 0, Stick { up: held.contains("up"), down: held.contains("down"), left: held.contains("left"), right: held.contains("right"), left_button: held.contains("b1") || held.contains("b12"), right_button: held.contains("b2") || held.contains("b12"), }, ); } }