// SPDX-License-Identifier: MIT //! GTIA (C014805, NTSC) behind a pin-shaped interface. //! //! GTIA owns the picture: it turns ANTIC's AN0-AN2 codes into colour-register //! values, shifts out players and missiles, applies priority, records //! collisions, reads the triggers and drives the four CONSOL lines. It sees the //! bus only as the data byte of each cycle and ANTIC's HALT pin (it latches P/M //! DMA bytes itself: [AHRM] 6.5 "Player/missile graphics DMA"). It imports //! nothing from the board (tests/test_boundaries.py). The working specification //! is [AHRM] chapter 6 (tier 3) and the GTIA data sheet [DS-G] (tier 2). //! //! **One cycle** is `cycle(an_a, an_b, halt, data)`: the two colour clocks of //! a machine cycle, at horizontal positions 2c and 2c+1 of ANTIC's cycle c. //! GTIA keeps its own horizontal counter, set to $DE at the leading edge of //! horizontal blank (an AN code 2 or 3 after a non-blank one) and free-running //! otherwise ([AHRM] 6.2 "Blanking and sync regions"); a line starts when it //! reaches $DE. Output goes to `line`, two bytes per colour clock (the //! two half-clock pixels, equal except in high resolution), indexed by //! position, as colour-register values (hue << 4 | luma), not RGB. //! //! **Register writes take effect after the delays of [AHRM] 6.10 Table 18**, //! counted in colour clocks from the first colour clock of the write cycle: //! colour 1, PRIOR 2, P/M graphics 3, P/M position and size 5 (the 2-3 and 3-5 //! ranges there are unit-dependent: the first value is modelled). //! //! **Priority is the logic of [AHRM] 6.7**, evaluated as written (the SP/SF/SB //! equations), and cached per input combination; it is not a fitted table. //! //! Evidence tags (methodology): DOC is [AHRM] and [DS-G]; TEST ROM is an //! Acid5200 test (author-stated hardware passes, no recorded 5200 run: U7); //! FITTED names the tests a value was chosen for. //! //! A transcription of a5200-python's `gtia.py` (at cf82d44): same names (Python's leading //! underscores kept), same order, same comments. Rust-forced differences: //! //! - `consol_out` is a [`Pin`], a shared cell: the controllers read the CONSOL lines from //! inside POKEY's `key_response` and `pot_level` callbacks, which in Rust are closures the //! chip owns and cannot reach the GTIA through the board (Python's read //! `board.gtia.consol_out`, docs/architecture.md: "a read of that one field"). Reads are //! `consol_out.get()`. //! - `_triggers` (a dict from position to object bits, holding only non-zero bits) is a //! 256-entry table where 0 means "no entry"; `hpos in self._triggers` is `!= 0`. //! - `_cache` (a dict memo of `resolve`) is a table indexed by the same key, `None` when not //! yet filled; `resolve` returns the selected indices as a `Vec` (Python's tuple). //! - `pending` keeps Python's list of `(colour clock, register, value)` and its order; `cc` is //! `i64`. Integers are `i32`; `line` is a `Vec`. //! - `snapshot()` / `restore()` are serde on the struct, `_cache` skipped as in Python; //! `restore` keeps the live `consol_out` cell and sets its value. use std::cell::Cell; use std::rc::Rc; use serde::{Deserialize, Deserializer, Serialize, Serializer}; const HBLANK_START: i32 = 0xDE; // DOC: [AHRM] 6.2 (the counter's value at the blank edge) const VISIBLE_LEFT: i32 = 0x22; // DOC: [AHRM] 6.2 pub const LINE_POSITIONS: i32 = 228; // DOC: [AHRM] 4.2, 228 colour clocks per line // Write delays in colour clocks (DOC [AHRM] 6.10 Table 18), by register 0-31. const DELAY: [i64; 32] = [ 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, ]; // HPOS (0-7) SIZE (8-12) GRAF (13-17) COLPM0-COLBK (18-26) PRIOR VDELAY GRACTL HITCLR CONSOL // SIZE: FITTED 3, not Table 18's 5 (which lists position and size together): // gtia_pmresize needs a size change to steer the state machine from the third // colour clock (C13); 5 (Table 18) was tried first and fails it. HPOS: DOC 5 as // in Table 18; TEST ROM gtia_pmretrigger brackets it at 5-8 and gtia_pmoverlap // at 5-6. GRAF 3, colours 1, PRIOR 2: DOC Table 18 (first value of each range). /// A line level shared with the controllers' callbacks (Rust-forced, see the module header). #[derive(Debug, Default, PartialEq)] pub struct Pin(Rc>); impl Pin { pub fn get(&self) -> u8 { self.0.get() } pub fn set(&self, value: u8) { self.0.set(value) } /// Another handle on the same cell (for a callback that reads the line). pub fn share(&self) -> Rc> { Rc::clone(&self.0) } } impl Clone for Pin { /// A copy of the level, not of the wire: a snapshot does not follow the live pin. fn clone(&self) -> Self { Pin(Rc::new(Cell::new(self.0.get()))) } } impl Serialize for Pin { fn serialize(&self, serializer: S) -> Result { self.0.get().serialize(serializer) } } impl<'de> Deserialize<'de> for Pin { fn deserialize>(deserializer: D) -> Result { Ok(Pin(Rc::new(Cell::new(u8::deserialize(deserializer)?)))) } } /// The colour registers that show, from [AHRM] 6.7's priority equations. /// /// DOC: the equations as [AHRM] 6.7 writes them (themselves from the GTIA /// data sheet's logic); TEST ROM gtia_collision, gtia_collision2, gtia_psuedomodee. /// /// `players`: bits 0-3 = P0-P3 (missiles already merged in unless fifth /// player); `playfield`: bits 0-3 = PF0-PF3 (fifth player merged into PF3). /// Returns indices: 0-3 COLPM0-3, 4-7 COLPF0-3, 8 COLBK; empty = black. pub fn resolve(prior: i32, players: i32, playfield: i32) -> Vec { let (pri0, pri1, pri2, pri3) = ( prior & 1, (prior >> 1) & 1, (prior >> 2) & 1, (prior >> 3) & 1, ); let multi = (prior >> 5) & 1; let (p0, p1, p2, p3) = ( players & 1, (players >> 1) & 1, (players >> 2) & 1, (players >> 3) & 1, ); let (f0, f1, f2, f3) = ( playfield & 1, (playfield >> 1) & 1, (playfield >> 2) & 1, (playfield >> 3) & 1, ); let (pri01, pri12, pri23, pri03) = (pri0 | pri1, pri1 | pri2, pri2 | pri3, pri0 | pri3); let (pf01, pf23, p01, p23) = (f0 | f1, f2 | f3, p0 | p1, p2 | p3); let n = |bit: i32| 1 ^ bit; // logical NOT of a bit let sp0 = p0 & n(pf01 & pri23) & n(pri2 & pf23); let sp1 = p1 & n(pf01 & pri23) & n(pri2 & pf23) & (n(p0) | multi); let sp2 = p2 & n(p01) & n(pf23 & pri12) & n(pf01 & n(pri0)); let sp3 = p3 & n(p01) & n(pf23 & pri12) & n(pf01 & n(pri0)) & (n(p2) | multi); let sf3 = f3 & n(p23 & pri03) & n(p01 & n(pri2)); let sf0 = f0 & n(p23 & pri0) & n(p01 & pri01) & n(sf3); let sf1 = f1 & n(p23 & pri0) & n(p01 & pri01) & n(sf3); let sf2 = f2 & n(p23 & pri03) & n(p01 & n(pri2)) & n(sf3); let sb = n(p01) & n(p23) & n(pf01) & n(pf23); let mut out = Vec::new(); for (index, bit) in [sp0, sp1, sp2, sp3, sf0, sf1, sf2, sf3, sb] .into_iter() .enumerate() { if bit != 0 { out.push(index); } } out } fn empty_cache() -> Vec>> { vec![None; 0x4000] } #[derive(Clone, Debug, Serialize, Deserialize)] #[allow(clippy::upper_case_acronyms)] pub struct GTIA { pub pal: bool, // chip fields for a future computer board (U14) // Registers power up at 0: HYPOTHESIS (U8); TEST ROM gtia_default passes. pub ctia: bool, pub hposp: [i32; 4], pub hposm: [i32; 4], pub sizep: [i32; 4], pub sizem: i32, pub grafp: [i32; 4], pub grafm: i32, // COLPM0-3, COLPF0-3, COLBK in register order (index 0-8). pub colour: [i32; 9], pub prior: i32, pub vdelay: i32, pub gractl: i32, pub consol_out: Pin, // lines pulled down by writes (bits 0-3) // Collisions: M0PF-M3PF, P0PF-P3PF, M0PL-M3PL, P0PL-P3PL. pub mpf: [i32; 4], pub ppf: [i32; 4], pub mpl: [i32; 4], pub ppl: [i32; 4], pub trig_in: [i32; 4], // pin levels: 0 = pressed pub trig_latch: [i32; 4], // P/M shifters (DOC [AHRM] 6.5): eight-bit players, two-bit missiles, two-bit states. pub psr: [i32; 4], pub pst: [i32; 4], pub msr: [i32; 4], pub mst: [i32; 4], pub pm_live: bool, pub _triggers: Vec, pub pending: Vec<(i64, i32, i32)>, pub cc: i64, // absolute colour clock of this cycle's first half pub hpos: i32, // position of the colour clock last processed pub vline: i32, pub hires: bool, pub _blank: bool, pub _pm_slot: i32, pub _pair: i32, pub _pixel10: i32, // mode 10: the delayed pixel still to show, -1 = none since blank pub _pm_even: (i32, i32), // modes 9/11: the even clock's players and missiles #[serde(skip, default = "empty_cache")] pub _cache: Vec>>, pub line: Vec, pub line_done: bool, // set when a line ended (hpos reached $DE) } impl PartialEq for GTIA { /// Python's `snapshot()` leaves `_cache` out; so does equality. fn eq(&self, other: &Self) -> bool { let mut a = self.clone(); a._cache = Vec::new(); let mut b = other.clone(); b._cache = Vec::new(); serde_json::to_string(&a).ok() == serde_json::to_string(&b).ok() } } impl GTIA { pub fn new(pal: bool, ctia: bool) -> Self { GTIA { pal, ctia, hposp: [0, 0, 0, 0], hposm: [0, 0, 0, 0], sizep: [0, 0, 0, 0], sizem: 0, grafp: [0, 0, 0, 0], grafm: 0, colour: [0; 9], prior: 0, vdelay: 0, gractl: 0, consol_out: Pin::default(), mpf: [0, 0, 0, 0], ppf: [0, 0, 0, 0], mpl: [0, 0, 0, 0], ppl: [0, 0, 0, 0], trig_in: [1, 1, 1, 1], trig_latch: [1, 1, 1, 1], psr: [0, 0, 0, 0], pst: [0, 0, 0, 0], msr: [0, 0, 0, 0], mst: [0, 0, 0, 0], pm_live: false, _triggers: vec![0; 256], pending: Vec::new(), cc: 0, hpos: HBLANK_START, vline: 0, hires: false, _blank: true, _pm_slot: -1, _pair: 0, _pixel10: -1, _pm_even: (0, 0), _cache: empty_cache(), line: vec![0; 2 * LINE_POSITIONS as usize], line_done: false, } } // -- registers ---------------------------------------------------------- pub fn read(&self, register: i32) -> u8 { let r = register & 0x1F; if r < 4 { return self.mpf[r as usize] as u8; } if r < 8 { return self.ppf[(r - 4) as usize] as u8; } if r < 12 { return self.mpl[(r - 8) as usize] as u8; } if r < 16 { return self.ppl[(r - 12) as usize] as u8; } if r < 20 { let n = (r - 16) as usize; // DOC: GRACTL bit 2 latches the triggers ([AHRM] 6.1); TEST ROM gtia_default. return if self.gractl & 4 != 0 { self.trig_latch[n] as u8 } else { self.trig_in[n] as u8 }; } if r == 0x1F { // DOC: CONSOL reads the lines the writes left high ([AHRM] 6.1, 13.3); // TEST ROM gtia_consol. return !self.consol_out.get() & 0x0F; } // DOC: PAL at $14; unassigned reads alike ([AHRM] 6.1); TEST ROM gtia_default. if self.pal { 0x01 } else { 0x0F } } pub fn write(&mut self, register: i32, value: u8) { let r = register & 0x1F; let value = value as i32; let delay = DELAY[r as usize]; if r == 27 { // PRIOR bits 6-7 (the GTIA modes) take 3 colour clocks, the rest 2 // (DOC [AHRM] 6.10 Table 18: "3-5", first value modelled). Index 32 is // an internal name for the late half. self.pending.push((self.cc + 3, 32, value)); } if delay != 0 { self.pending.push((self.cc + delay, r, value)); return; } self._apply(r, value); } fn _apply(&mut self, r: i32, value: i32) { if r < 4 { self.hposp[r as usize] = value; } else if r < 8 { self.hposm[(r - 4) as usize] = value; } else if r < 12 { self.sizep[(r - 8) as usize] = value & 3; } else if r == 12 { self.sizem = value; } else if r < 17 { self.grafp[(r - 13) as usize] = value; } else if r == 17 { self.grafm = value; } else if r < 27 { self.colour[(r - 18) as usize] = value & 0xFE; // DOC: luma bit 0 not stored ([AHRM] 6.1) } else if r == 27 { self.prior = (self.prior & 0xC0) | (value & 0x3F); } else if r == 32 { self.prior = (self.prior & 0x3F) | (value & 0xC0); return; } else if r == 28 { self.vdelay = value; } else if r == 29 { self.gractl = value; if value & 4 == 0 { self.trig_latch = [1, 1, 1, 1]; } } else if r == 30 { self.mpf = [0, 0, 0, 0]; self.ppf = [0, 0, 0, 0]; self.mpl = [0, 0, 0, 0]; self.ppl = [0, 0, 0, 0]; } else if r == 31 { self.consol_out.set((value & 0x0F) as u8); } self._retrigger(); } /// Positions where an object with a non-empty image triggers. fn _retrigger(&mut self) { let mut triggers = vec![0; 256]; for n in 0..4 { if self.grafp[n] != 0 { triggers[self.hposp[n] as usize] |= 1 << n; } if self.grafm >> (2 * n) & 3 != 0 { triggers[self.hposm[n] as usize] |= 16 << n; } } self._triggers = triggers; } pub fn set_trigger(&mut self, n: usize, pressed: bool) { self.trig_in[n] = if pressed { 0 } else { 1 }; if pressed { self.trig_latch[n] = 0; } } // -- one machine cycle ---------------------------------------------------- pub fn cycle(&mut self, an_a: u8, an_b: u8, halt: bool, data: u8) { // P/M DMA as GTIA sees it: the first halted cycle in horizontal blank is // the missile fetch; players follow two to five cycles later (DOC [AHRM] 6.5; // TEST ROM gtia_phantomdma, gtia_vdelay with the writes redirected). VDELAY's // odd-line rule: DOC [AHRM] 6.5. let data = data as i32; let pos = (self.hpos + 1) % LINE_POSITIONS; if pos >= HBLANK_START || pos < VISIBLE_LEFT { let mut slot = self._pm_slot; if slot < 0 { if halt { slot = 0; } } else { slot += 1; } if slot >= 0 { self._pm_slot = slot; if slot == 0 { if self.gractl & 1 != 0 { self._dma_missiles(data); } } else if (2..=5).contains(&slot) && self.gractl & 2 != 0 { let n = slot - 2; if (self.vdelay >> (4 + n)) & 1 == 0 || self.vline & 1 != 0 { self.pending.push((self.cc + 3, 13 + n, data)); } } } } self._clock(an_a as i32); self.cc += 1; self._clock(an_b as i32); self.cc += 1; } fn _dma_missiles(&mut self, data: i32) { let mut keep = 0; if self.vline & 1 == 0 { for n in 0..4 { if (self.vdelay >> n) & 1 != 0 { keep |= 3 << (2 * n); } } } let value = (self.grafm & keep) | (data & !keep & 0xFF); self.pending.push((self.cc + 3, 17, value)); } fn _clock(&mut self, code: i32) { if !self.pending.is_empty() { let now = self.cc; if self.pending.iter().any(|p| p.0 <= now) { let due: Vec<(i64, i32, i32)> = self .pending .iter() .copied() .filter(|p| p.0 <= now) .collect(); self.pending.retain(|p| p.0 > now); for (_, r, value) in due { self._apply(r, value); } } } let mut hpos = self.hpos + 1; if hpos == LINE_POSITIONS { hpos = 0; } let blank = code == 2 || code == 3; if blank && !self._blank { hpos = HBLANK_START; // DOC: resynchronise at the blank's leading edge ([AHRM] 6.2) } self._blank = blank; if hpos == HBLANK_START { self.vline += 1; self._pm_slot = -1; self.line_done = true; } self.hpos = hpos; // The high-resolution flip-flop (DOC [AHRM] 6.8): set from ANTIC's blank // code during horizontal blank, reset whenever PRIOR 6-7 is non-zero, // so a GTIA mode switched off after blank leaves "pseudo mode E" (TEST // ROM gtia_psuedomodee). if self.prior & 0xC0 != 0 && !self.ctia { self.hires = false; } else if blank { self.hires = code == 3; } if code == 1 { self.vline = 251; // DOC: vertical sync lines are 251-253 ([AHRM] 6.2) } // Players and missiles: trigger on position, then shift (DOC [AHRM] 6.5; // TEST ROM gtia_pmretrigger, gtia_pmoverlap, gtia_pmresize). let mut players = 0; let mut missiles = 0; if self.pm_live || self._triggers[hpos as usize] != 0 { (players, missiles) = self._shift_objects(hpos); } let i = (2 * hpos) as usize; if blank || code == 1 { self.line[i] = 0; self.line[i + 1] = 0; self._pixel10 = -1; return; } let prior = self.prior; let gmode = if self.ctia { 0 } else { prior >> 6 }; let hires = self.hires && gmode == 0; if gmode != 0 { self._gtia_mode(code, hpos, players, missiles, gmode); return; } // Playfield signals from the AN code (DOC [AHRM] 6.9 Table 17, 6.8). let mut playfield; let pf_collide; if hires { playfield = if code >= 4 { 4 } else { 0 }; pf_collide = if code & 3 != 0 && code >= 4 { 4 } else { 0 }; } else { playfield = if code >= 4 { 1 << (code - 4) } else { 0 }; pf_collide = playfield; } let pm; if players != 0 || missiles != 0 { self._collide(players, missiles, pf_collide); if prior & 0x10 != 0 { // DOC: fifth player, missiles join PF3's layer ([AHRM] 6.7) pm = players; if missiles != 0 { playfield |= 8; } } else { pm = players | missiles; } } else { pm = 0; } let key = ((prior & 0x3F) << 8 | pm << 4 | playfield) as usize; if self._cache[key].is_none() { self._cache[key] = Some(resolve(prior, pm, playfield)); } let sel = self._cache[key].as_ref().unwrap(); let colour = &self.colour; let mut value = 0; for &index in sel { value |= colour[index]; } if prior & 0x10 != 0 && missiles != 0 && sel.contains(&7) && playfield & 8 != 0 { // the fifth player already shows as COLPF3 through index 7 } if hires && code >= 4 { let lum = colour[5] & 0x0E; // DOC: PF1's luminance on 1 bits ([AHRM] 6.8) self.line[i] = if code & 2 != 0 { ((value & 0xF0) | lum) as u8 } else { value as u8 }; self.line[i + 1] = if code & 1 != 0 { ((value & 0xF0) | lum) as u8 } else { value as u8 }; } else { self.line[i] = value as u8; self.line[i + 1] = value as u8; } } fn _shift_objects(&mut self, hpos: i32) -> (i32, i32) { let trig = self._triggers[hpos as usize]; let mut players = 0; let mut missiles = 0; let mut live = false; for n in 0..4 { // A trigger forces the state machine to %00; if it was elsewhere, // that transition shifts the register once before the image is ORed // in ([AHRM] 6.5: "a shift occurs each time the state machine // transitions to %00, which is forced whenever the shift register is // reloaded"; C14). DOC read literally; TEST ROM gtia_pmoverlap needs // exactly that. if trig & (1 << n) != 0 { let mut sr = self.psr[n]; if self.pst[n] != 0 { sr = (sr << 1) & 0xFF; } self.psr[n] = sr | self.grafp[n]; self.pst[n] = 0; } let mut sr = self.psr[n]; if sr != 0 { if sr & 0x80 != 0 { players |= 1 << n; } let st = (self.pst[n] + 1) & self.sizep[n]; self.pst[n] = st; if st == 0 { sr = (sr << 1) & 0xFF; self.psr[n] = sr; } live = live || sr != 0; } if trig & (16 << n) != 0 { let mut sr = self.msr[n]; if self.mst[n] != 0 { sr = (sr << 1) & 3; } self.msr[n] = sr | ((self.grafm >> (2 * n)) & 3); self.mst[n] = 0; } let mut sr = self.msr[n]; if sr != 0 { if sr & 2 != 0 { missiles |= 1 << n; } let st = (self.mst[n] + 1) & ((self.sizem >> (2 * n)) & 3); self.mst[n] = st; if st == 0 { sr = (sr << 1) & 3; self.msr[n] = sr; } live = live || sr != 0; } } self.pm_live = live; (players, missiles) } fn _collide(&mut self, players: i32, missiles: i32, playfield: i32) { // DOC: collision latches ([AHRM] 6.6); TEST ROM gtia_collision, gtia_collision2. for n in 0..4 { let bit = 1 << n; if players & bit != 0 { self.ppf[n] |= playfield; self.ppl[n] |= players & !bit; } if missiles & bit != 0 { self.mpf[n] |= playfield; self.mpl[n] |= players; } } } /// GTIA modes 9-11 ([AHRM] 6.9): two colour clocks form one 4-bit pixel. /// /// Only the playfield pixel is two colour clocks wide (and, in mode 10, /// delayed by one colour clock: it covers the odd clock and the even /// clock after it). Players and missiles keep their colour-clock /// resolution (DOC [AHRM] 6.5: "Position registers have color clock /// resolution"; OBSERVED: Atari800 98cc2dc agrees on the three titles; /// no Acid5200 test covers it), so priority, colour and collisions are resolved on /// every colour clock with that clock's objects. Before 2026-11-12 the /// board sampled the objects on the odd clock only and painted the /// result over the pair, which moved Rescue on Fractalus!'s fifth-player /// missiles one colour clock right (docs/validation.md, milestone 7). fn _gtia_mode(&mut self, code: i32, hpos: i32, players: i32, missiles: i32, gmode: i32) { let i = (2 * hpos) as usize; if hpos & 1 == 0 { self._pair = code & 3; if gmode == 2 { // The second half of the delayed pixel of the previous pair. let pixel = self._pixel10; if pixel < 0 { let v = if i != 0 { self.line[i - 1] } else { 0 }; self.line[i] = v; self.line[i + 1] = v; } else { let v = self._gtia_value(pixel, players, missiles, gmode) as u8; self.line[i] = v; self.line[i + 1] = v; } } else { // Drawn when the pair completes, with this clock's objects. self._pm_even = (players, missiles); } return; } let pixel = self._pair << 2 | (code & 3); let value = self._gtia_value(pixel, players, missiles, gmode) as u8; if gmode == 2 { // DOC: mode 10 is delayed by one colour clock ([AHRM] 6.9). self._pixel10 = pixel; self.line[i] = value; self.line[i + 1] = value; } else { let (even_players, even_missiles) = self._pm_even; if i >= 2 { let first = self._gtia_value(pixel, even_players, even_missiles, gmode) as u8; self.line[i - 2] = first; self.line[i - 1] = first; } self.line[i] = value; self.line[i + 1] = value; } } /// One colour clock of a GTIA-mode pixel with that clock's players and missiles. fn _gtia_value(&mut self, pixel: i32, players: i32, missiles: i32, gmode: i32) -> i32 { let prior = self.prior; let fifth = prior & 0x10; let mut pm = players | if fifth != 0 { 0 } else { missiles }; let mut playfield = 0; let mut pf_collide = 0; if gmode == 2 { // DOC: nine colours, 0-3 P0-P3, x100-x111 PF0-PF3, 10xx background ([AHRM] 6.9). if pixel < 4 { pm |= 1 << pixel; } else if pixel & 4 != 0 { playfield = 1 << (pixel & 3); pf_collide = playfield; } } if fifth != 0 && missiles != 0 { playfield |= 8; } if players != 0 || missiles != 0 { self._collide(players, missiles, pf_collide); } let key = ((prior & 0x3F) << 8 | pm << 4 | playfield) as usize; if self._cache[key].is_none() { self._cache[key] = Some(resolve(prior, pm, playfield)); } let colour = &self.colour; let mut value = 0; for &index in self._cache[key].as_ref().unwrap() { value |= colour[index]; } if gmode != 2 && pm == 0 { // DOC: mode 9 takes luma from the pixel, mode 11 hue ([AHRM] 6.9). let base = if fifth != 0 && missiles != 0 { value } else { colour[8] }; value = if gmode == 1 { base | pixel } else if pixel != 0 { base | pixel << 4 } else { base & 240 }; } value } // -- state ------------------------------------------------------------------ pub fn snapshot(&self) -> GTIA { let mut state = self.clone(); state._cache = Vec::new(); state } pub fn restore(&mut self, state: >IA) { let pin = std::mem::take(&mut self.consol_out); *self = state.clone(); pin.set(state.consol_out.get()); self.consol_out = pin; self._cache = empty_cache(); } }