// SPDX-License-Identifier: MIT //! ANTIC (C012296, NTSC) behind a pin-shaped interface. //! //! ANTIC owns time: its horizontal counter (machine cycles 0-113) and vertical //! counter (scan lines 0-261), which cycles are DMA (HALT), when RDY is low //! (WSYNC), when NMI falls, and what goes to GTIA on AN0-AN2. It touches memory //! only through the address it puts out and the data byte the board hands back, //! so a bus capture or a die simulation could drive it; it imports nothing from //! the board (tests/test_boundaries.py). Source keys are docs/start-here.md's; //! the working specification is [AHRM] chapter 4 (tier 3), with [IJOR] (tier 1) //! for anything [AHRM] leaves open. Cycle numbers are [AHRM]'s: cycle 0 is the //! missile DMA slot. //! //! **One cycle** is two calls from the board: //! //! `begin()` //! ANTIC decides, from its registers as the previous cycles left them, //! whether this cycle is its own. It returns the address it drives (HALT //! asserted), `REFRESH` (a halted cycle that drives no memory), or `-1` //! (the CPU's cycle). It also runs the cycle's timed events (NMIST at cycle //! 7, the WSYNC release at 105, the end-of-mode-line check at 109). //! `end(data)` //! `data` is what was on the data bus at the end of the cycle (ANTIC's //! fetch, the CPU's access, or the floating bus). ANTIC latches what it //! fetched, including "virtual" playfield fetches past cycle 105 that read //! whatever the CPU had on the bus ([AHRM] 4.14), produces the two AN codes //! of the cycle (`an_a`, `an_b`), sets `nmi_fire` if NMI falls in this //! cycle, and advances its counters. //! //! A CPU register write in cycle k is therefore seen by `begin()` from cycle //! k+1: an [AHRM] deadline "by cycle N" is a comparison made at cycle N+1. //! //! **The playfield DMA clock** is modelled as [AHRM] 4.12 describes the //! circuit: an 8-bit shift register whose recirculating part is 2, 4 or 8 bits //! long (the fetch rate); a bit is injected at playfield start and that //! position is cleared at playfield stop; taps at bit 0, 2 and 3 request the //! character name, the bitmap byte and the character data. Start and stop are //! comparisons of the horizontal counter against the live DMACTL width and //! HSCROL, so the mid-line effects [AHRM] describes follow from the structure //! rather than from cases. //! //! **AN codes** ([AHRM] 6.9 Table 17): 0 background, 1 vertical sync, 2/3 //! horizontal blank and switch to lores/hires, 4-7 PF0-PF3 (lores) or a hires //! pixel pair 1xy. Cycle c (in [AHRM] 4.14's chart numbering) carries the //! colour clocks at GTIA horizontal positions 2c and 2c+1. This places [AHRM] //! 6.10's example write ("cycle 65", shown at $81) on chart cycle 64: the //! Acid5200 tests and [AHRM] 6.10 count cycles one lower than the DMA charts //! (their refresh slots read 24, 28, ...; C11). Two judges settle it: in //! gtia_pmretrigger a HPOSP0 write on chart cycle 30 must still let the player //! trigger at $40, and in antic_virtdma a collision at $DD must be visible to a //! read on chart cycle 111. //! //! Evidence tags (methodology): DOC is [AHRM], [HM], [DS-A]; DIE is [IJOR]'s //! schematics drawn from the die; TEST ROM is an Acid5200 test (author-stated //! hardware passes, no recorded 5200 run: U7). The cycle-numbering choice above //! is TEST ROM (gtia_pmretrigger, antic_virtdma) against DOC [AHRM] 6.10 (C11). //! Every DMA chart of [AHRM] 4.14 (modes 2-F, every width and HSCROL) is //! machine-compared with this model: 38,760 cells, 36 differ, all C16. //! //! A transcription of a5200-python's `antic.py` (at cf82d44): same names (Python's leading //! underscores kept), same order, same comments. Rust-forced differences: //! //! - Python's integers are `i32` here (counters, registers, addresses; `REFRESH` and `NO_DMA` //! are negative), the two machine-cycle clocks (`clock`, `line_start`, `wsync_at`, //! `_wsync_after`) `i64`. Python's `x & 3` of a negative `c - 3` is the same in two's //! complement. //! - `queue` (a list popped from the front) is a `VecDeque`; `line_buffer` a 48-byte `Vec`; //! `SHIFT` (a dict by mode) an array indexed by mode, with modes 0-1 unused. //! - `snapshot()` / `restore()` (Python's `vars()` dict) are serde: the whole struct is the //! state. The `K_*` constants and `LB_UNSELECTED` are at the end, where Python defines them. use std::collections::VecDeque; use serde::{Deserialize, Serialize}; pub const REFRESH: i32 = -2; pub const NO_DMA: i32 = -1; // DOC: scan lines per mode line, by IR mode ([AHRM] 4.4, 4.5). Mode 0 and 1 are special. const MODE_LINES: [i32; 16] = [1, 1, 8, 10, 8, 16, 8, 16, 8, 4, 4, 2, 1, 2, 1, 1]; // DOC: playfield DMA rate in cycles, the length of the DMA clock's ring ([AHRM] 4.12 Table 7). const RATE: [i32; 16] = [0, 0, 2, 2, 2, 2, 4, 4, 8, 8, 4, 4, 4, 2, 2, 2]; // DOC: fetch window, first name-tap cycle and length, by fetch width 1-3 ([AHRM] 4.14); // TEST ROM antic_pfstarttiming, antic_pfstoptiming, antic_dmapattern. const WINDOW_START: [i32; 4] = [0, 26, 18, 10]; const WINDOW_LENGTH: [i32; 4] = [0, 64, 80, 96]; // DOC: displayed playfield in GTIA horizontal positions, by DMACTL width ([AHRM] 4.3, 6.2). const REGION: [(i32, i32); 4] = [(0, 0), (0x40, 0xC0), (0x30, 0xD0), (0x2C, 0xE0)]; // DOC: display delay in cycles from fetch to the first colour clock shown ([AHRM] 6.10: // a normal-width mode E byte fetched on chart cycle 66 shows at $8C; character // data is fetched one cycle later relative to display than bitmap data, [AHRM] // 4.14). Read through the C11 numbering; TEST ROM antic_charcontrol, gtia_* pictures. const BITMAP_DELAY: i32 = 4; const CHAR_DELAY: i32 = 3; const VISIBLE_LEFT: i32 = 0x22; // DOC: [AHRM] 6.2, $22-$DD visible const VISIBLE_RIGHT: i32 = 0xDE; pub const FIRST_LINE: i32 = 8; // DOC: [AHRM] 4.6; TEST ROM antic_vcount, antic_nmist pub const VBLANK_LINE: i32 = 248; const VSYNC_LINES: [i32; 3] = [251, 252, 253]; // DOC: NTSC ([AHRM] 6.2) // DOC: nine cycles from 25 ([AHRM] 4.14 charts); TEST ROM antic_dmapattern. /// `REFRESH_SLOTS = frozenset(range(25, 58, 4))` fn in_refresh_slots(c: i32) -> bool { (25..58).contains(&c) && (c - 25) % 4 == 0 } // DOC: how each mode shifts pixels out ([AHRM] 4.12 Table 7): (bits per pixel, // colour clocks per pixel, kind). Kinds: 0 hires pair, 1 two-bit lores // bitmap (00 BAK, 01-11 PF0-PF2), 2 one-bit lores bitmap (PF0), 3 two-bit // character (11 = PF2 or PF3 by name bit 7), 4 one-bit character (PF by name // bits 6-7). const SHIFT: [(i32, i32, i32); 16] = [ (0, 0, 0), // mode 0: not in Python's dict (never looked up) (0, 0, 0), // mode 1: likewise (2, 1, 0), (2, 1, 0), (2, 1, 3), (2, 1, 3), (1, 1, 4), (1, 1, 4), (2, 4, 1), (1, 2, 2), (2, 2, 1), (1, 1, 2), (1, 1, 2), (2, 1, 1), (2, 1, 1), (2, 1, 0), ]; const LORES2: [i32; 4] = [0, 4, 5, 6]; // BAK, PF0, PF1, PF2 /// The chip. Registers by their [HM] names; everything else is internal state. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct ANTIC { pub lines: i32, // Counters (DOC [AHRM] 4.1: not reset by RESET; power-on value varies, U8). pub hcount: i32, pub vcount: i32, pub clock: i64, // machine cycles since power, for WSYNC bookkeeping pub line_start: i64, // clock at cycle 0 of the current line // Registers (DOC [AHRM] 4.1 "Reset behavior": NMIEN, DMACTL cleared; the // others' power-on values are HYPOTHESIS 0, U8; TEST ROM antic_default). pub dmactl: i32, pub chactl: i32, pub dlist: i32, pub hscrol: i32, pub vscrol: i32, pub pmbase: i32, pub chbase: i32, pub nmien: i32, // DOC: bits 0-4 read as 1 ([AHRM] Table 5; its power-up $7F is a bias, not a rule) pub nmist: i32, // HYPOTHESIS: light-pen latches, unconnected on the 5200 ([FSM] has no pen // input); values a console reads would settle them. pub penh: i32, pub penv: i32, // Copies one cycle old: P/M and display-list DMA enables and CHBASE take // effect two cycles after the write (DOC [AHRM] 4.4, 4.6, 4.13; TEST ROM // antic_pmdma, antic_charcontrol). pub _dmactl_d: i32, pub _chbase_d: i32, // WSYNC: the clock of the write that set the latch, or -1. pub wsync_at: i64, // Display list state. pub ir: i32, pub row: i32, pub row_end: i32, // -1: the end row is VSCROL, compared live pub prev_vs: bool, pub new_mode_line: bool, pub first_line: bool, pub jvb_wait: bool, pub dli_line: bool, pub msc: i32, pub _addr_lo: i32, // Playfield DMA clock and shifter. pub dma_clock: i32, pub _inject: bool, pub _clear_bit: bool, pub line_buffer: Vec, pub lb_index: i32, pub _names: [i32; 4], // character names by fetch cycle & 3 pub queue: VecDeque<(i32, i32, i32)>, // (colour clock, byte, name) pub shifter: i32, pub shift_name: i32, pub shift_phase: i32, pub mode: i32, // IR mode the shifter uses pub hires: bool, pub refresh_pending: bool, // Per-cycle outputs. pub kind: i32, // what this cycle's DMA was for (bit set of K_* below) pub halt: bool, pub address: i32, pub an_a: u8, pub an_b: u8, pub nmi_fire: bool, pub _nmi_armed: i32, // cycle at which an NMI is to fall, 0 = none, 99 = maybe at 9 pub _nmi_bit: i32, pub _vs_latch: i32, pub _wsync_release: bool, pub _wsync_after: i64, pub _end_check: bool, } impl ANTIC { pub fn new(lines: i32, line: i32, cycle: i32) -> Self { ANTIC { lines, hcount: cycle, vcount: line, clock: 0, line_start: -(cycle as i64), dmactl: 0, chactl: 0, dlist: 0, hscrol: 0, vscrol: 0, pmbase: 0, chbase: 0, nmien: 0, nmist: 0x1F, penh: 0x00, penv: 0xFF, _dmactl_d: 0, _chbase_d: 0, wsync_at: -1, ir: 0, row: 0, row_end: 0, prev_vs: false, new_mode_line: true, first_line: false, jvb_wait: false, dli_line: false, msc: 0, _addr_lo: 0, dma_clock: 0, _inject: false, _clear_bit: false, line_buffer: vec![0; 48], lb_index: 0, _names: [0, 0, 0, 0], queue: VecDeque::new(), shifter: 0, shift_name: 0, shift_phase: 0, mode: 0, hires: false, refresh_pending: false, kind: 0, halt: false, address: NO_DMA, an_a: 2, an_b: 2, nmi_fire: false, _nmi_armed: 0, _nmi_bit: 0, _vs_latch: 0, _wsync_release: false, _wsync_after: -1, _end_check: true, } } // -- registers ---------------------------------------------------------- pub fn read(&self, register: i32) -> u8 { let register = register & 0x0F; if register == 0x0B { let mut y = self.vcount; // DOC [AHRM] 4.10: the new value shows from cycle 111; TEST ROM antic_vcount. if self.hcount >= 111 { y += 1; if y == self.lines && self.hcount >= 112 { y = 0; } } return ((y >> 1) & 0xFF) as u8; } if register == 0x0F { return (self.nmist | 0x1F) as u8; } if register == 0x0C { return self.penh as u8; } if register == 0x0D { return self.penv as u8; } 0xFF // DOC [AHRM] 4.1: $FF for unassigned addresses; TEST ROM antic_default } pub fn write(&mut self, register: i32, value: u8) { let register = register & 0x0F; let value = value as i32; if register == 0x00 { self.dmactl = value & 0x3F; } else if register == 0x01 { self.chactl = value & 0x07; } else if register == 0x02 { self.dlist = (self.dlist & 0xFF00) | value; } else if register == 0x03 { self.dlist = (self.dlist & 0x00FF) | value << 8; } else if register == 0x04 { self.hscrol = value & 0x0F; } else if register == 0x05 { self.vscrol = value & 0x0F; } else if register == 0x07 { self.pmbase = value; } else if register == 0x09 { self.chbase = value; } else if register == 0x0A { // RDY falls two cycles after the first write that set the latch; a // second write before the release keeps that start (INC WSYNC's two // writes, DOC [AHRM] 4.9). A write at 104 or later, while a latch from // before 104 is still to be released at 105, starts a new wait that // the next line's 105 releases (TEST ROM antic_wsync; build trap 5). let now = self.clock; if self.wsync_at < 0 { self.wsync_at = now; } else if self.hcount >= 104 && self.wsync_at < self.line_start + 104 { self._wsync_after = now; } } else if register == 0x0E { self.nmien = value & 0xE0; } else if register == 0x0F { // NMIRES. A bit set in this very cycle (cycle 7) survives: the set // wins over a reset in the same cycle (TEST ROM antic_nmist). let keep = if self.hcount == 7 && self._nmi_armed != 0 { self._nmi_bit } else { 0 }; self.nmist &= 0x1F | keep; } } /// The RESET pin: clears NMIEN, DMACTL and the playfield DMA clock (DOC [AHRM] 4.1). pub fn reset(&mut self) { self.nmien = 0; self.dmactl = 0; self.dma_clock = 0; } /// Whether RDY is low for a CPU read on machine cycle `clock`. /// /// RDY falls one cycle after the cycle that follows the WSYNC write /// (DOC [AHRM] 4.9: "one more cycle elapses before the CPU is halted"; /// TEST ROM antic_wsync, cpu_timing). pub fn wsync_holds(&self, clock: i64) -> bool { self.wsync_at >= 0 && clock >= self.wsync_at + 2 } // -- one machine cycle ---------------------------------------------------- pub fn begin(&mut self) -> i32 { let c = self.hcount; let y = self.vcount; let pm_ctl = self._dmactl_d; self._dmactl_d = self.dmactl; let chbase = self._chbase_d; self._chbase_d = self.chbase; let mut kind = 0; let mut address = NO_DMA; let display = (FIRST_LINE..VBLANK_LINE).contains(&y); // The playfield DMA clock (DOC [AHRM] 4.12): shift, recirculate, inject, clear. // Dropping a clear request when the clock is empty (build trap 3): the stop // clears one bit position and has nothing to clear in an empty ring (DOC // [AHRM] 4.12, as read); found by tests/test_picture.py's synthetic mode line. let mut clk = self.dma_clock; let mut stopped = false; if clk == 0 { self._clear_bit = false; // nothing to clear: the stop pulse passes unseen } if clk != 0 || self._inject { let rate = RATE[(self.ir & 0x0F) as usize]; if rate != 0 { // (the ring, spelled out) clk = ((clk << 1) | ((clk >> (rate - 1)) & 1)) & 0xFF; } else { clk = 0; } if self._inject { clk |= 1; self._inject = false; } if self._clear_bit { stopped = clk & 1 != 0; clk &= 0xFE; self._clear_bit = false; } self.dma_clock = clk; } let mode = self.ir & 0x0F; if display && mode >= 2 { let width = self.dmactl & 3; let hs = self.ir & 0x10; let fetch = if hs != 0 && width != 0 && width < 3 { width + 1 } else { width }; let shift = if hs != 0 { self.hscrol >> 1 } else { 0 }; if fetch != 0 { let start = WINDOW_START[fetch as usize] + shift; if c == start - 1 { self._inject = true; } if c == start + WINDOW_LENGTH[fetch as usize] - 1 { self._clear_bit = true; } } if c == 105 + shift { // DOC: the wide stop is always active ([AHRM] 4.11) self._clear_bit = true; } } else if clk != 0 { // DOC: blank, jump or vertical blank clears it ([AHRM] 4.12) clk = 0; self.dma_clock = clk; } // Special DMA (DOC [AHRM] 4.13, 4.14; TEST ROM antic_pmdma, antic_dmapattern): // missiles 0, display list 1, players 2-5, LMS 6-7. // Playfield taps are not excluded here: an unstopped DMA clock runs on // through horizontal blank and taps in these cycles too (below). if c < 8 { if c != 0 { self.lb_index = 0; // DIE: RamCntRst (see cycle 8 below) } if display { if c == 0 { if pm_ctl & 0x0C != 0 { kind = K_MISSILE; if self.dmactl & 0x10 != 0 { address = (self.pmbase & 0xF8) << 8 | 0x300 | y; } else { address = (self.pmbase & 0xFC) << 8 | 0x180 | y >> 1; } } } else if c == 1 { self._vs_latch = self.vscrol; // DOC [AHRM] 4.7: VSCROL by cycle 0, first row if self.new_mode_line { if !self.jvb_wait && pm_ctl & 0x20 != 0 { kind = K_DLIST; address = self.dlist; } else { // Display-list DMA off, or JVB waiting: the instruction // byte in IR is replayed (DOC [AHRM] 4.6; TEST ROM antic_dlistwrap). self._start_mode_line(); } } } else if c < 6 { if pm_ctl & 0x08 != 0 { kind = K_PLAYER; let n = c - 2; if self.dmactl & 0x10 != 0 { address = (self.pmbase & 0xF8) << 8 | (0x400 + (n << 8)) | y; } else { address = (self.pmbase & 0xFC) << 8 | (0x200 + (n << 7)) | y >> 1; } } } else if (self.first_line || mode == 1) && !self.jvb_wait && pm_ctl & 0x20 != 0 && (mode == 1 || (mode >= 2 && self.ir & 0x40 != 0)) { kind = K_ADDR; address = self.dlist; } } if c == 6 { // DOC [AHRM] 4.8: VSCROL by cycle 5 to affect a DLI; TEST ROM antic_vscroldli. self.dli_line = display && self.ir & 0x80 != 0 && self._last_row(); } else if c == 7 { // NMIST from cycle 7, enabled or not; NMI at 8 if NMIEN allowed it by 6 // (DOC [AHRM] 4.8; DIE [IJOR] sheet 2; TEST ROM antic_nmist, antic_dlitiming; // the NMIEN deadline one cycle earlier than [AHRM] says is TEST ROM, C11). if y == VBLANK_LINE { self.nmist = (self.nmist & 0x3F) | 0x40; self._nmi_bit = 0x40; self._nmi_armed = if self.nmien & 0x40 != 0 { 8 } else { 99 }; } else if self.dli_line { self.nmist = (self.nmist & 0xBF) | 0x80; self._nmi_bit = 0x80; self._nmi_armed = if self.nmien & 0x80 != 0 { 8 } else { 99 }; } } } if c == 8 { // DIE [IJOR] sheet 6: RamCntRst is the latch set by the horizontal decode // "6-7" and cleared by "14-15"; with the one-cycle lag of // nRamShiftEn (sheet 6, DMA clock) it holds the line buffer's // address counter at zero for taps on cycles 0-7. self.lb_index = 0; } let mut steal = kind != 0; // special DMA always halts the CPU if (clk != 0 || stopped) && display { // Playfield taps: name at bit 0, bitmap at bit 2, character data at bit 3. let mut pf = 0; let mut pf_address = 0; if (2..=7).contains(&mode) { if clk & 1 != 0 && self.first_line { pf = K_NAME; pf_address = self.msc; } if clk & 8 != 0 { let name = self._names[((c - 3) & 3) as usize]; let char_address = self._char_address(name, mode, chbase); // DOC: overlapping fetches AND their addresses ([AHRM] 4.12). pf_address = if pf != 0 { pf_address & char_address } else { char_address }; pf |= K_CHAR; } } else if clk & 4 != 0 && self.first_line { pf = K_BITMAP; pf_address = self.msc; } if pf == 0 && clk & 1 != 0 && (2..=7).contains(&mode) { // A later scan line: the name comes from the line buffer, no bus cycle. self._names[(c & 3) as usize] = self._lb_read(); self._lb_step(c); } else if pf == 0 && clk & 4 != 0 && mode >= 8 { let data = self._lb_read(); self._load(c, data, 0, BITMAP_DELAY); self._lb_step(c); } if pf != 0 { // DIE [IJOR] sheet 6: pfDmaReq is gated by hzDmaEn, the latch set by // the decode "6-7" and cleared by HrzEq-111: a playfield request // halts the CPU on cycles 1-105 only; outside, the fetch is // "virtual" (ANTIC latches whatever is on the bus, DOC [AHRM] 4.14). // TEST ROM antic_hscrolbug, antic_virtdma (U15). if (1..=105).contains(&c) { steal = true; // A tap colliding with special DMA drives the bus at the // same time: the addresses AND (DOC [AHRM] 4.12). address = if kind != 0 { address & pf_address } else { pf_address }; } else { // Suppressed: no address driven (mScanOe is gated by // nHzDmaEn too, DIE [IJOR] sheet 6), any special DMA keeps its own address. pf |= K_VIRTUAL; if kind == 0 { address = pf_address; } } kind |= pf; } } // Refresh (DOC [AHRM] 4.14): nine slots from cycle 25, every 4 cycles. A // blocked one takes the next free cycle; only one waits, further blocked // ones are dropped. On a first line of modes 2-5 the first free cycle can // be the playfield stop cycle (90, 98, 106 by width: DOC [AHRM] p. 90 chart, // TEST ROM antic_dmapattern). A refresh may coincide with a virtual fetch. if steal { if in_refresh_slots(c) { self.refresh_pending = true; } } else if in_refresh_slots(c) || self.refresh_pending { self.refresh_pending = false; kind |= K_REFRESH; address = REFRESH; steal = true; } if c == 8 && self._nmi_armed == 99 { // Enabled exactly on cycle 7: the NMI comes one cycle late (DOC [AHRM] 4.8; // DIE [IJOR] sheet 2, NMIEN sampled during the two-cycle pulse; TEST ROM antic_nmist). self._nmi_armed = if self.nmien & self._nmi_bit != 0 { 9 } else { 0 }; } else if c == 104 { // WSYNC: a write by cycle 103 is released at 105; at 104 or later, next line // (DOC [AHRM] 4.9; TEST ROM antic_wsync). self._wsync_release = 0 <= self.wsync_at && self.wsync_at < self.line_start + 104; } else if c == 105 && self._wsync_release { self.wsync_at = self._wsync_after; self._wsync_after = -1; self._wsync_release = false; } else if c == 109 { // End of the mode line (DOC [AHRM] 4.7: VSCROL by cycle 108 for the end row; // TEST ROM antic_vscroll). self._end_check = self._last_row(); } self.kind = kind; self.halt = steal; self.address = address; if steal { address } else { NO_DMA } } pub fn end(&mut self, data: u8) { let c = self.hcount; let kind = self.kind; let data = data as i32; if kind != 0 { if kind & K_DLIST != 0 { self.ir = data; self.dlist = (self.dlist & 0xFC00) | ((self.dlist + 1) & 0x3FF); self._start_mode_line(); } else if kind & K_ADDR != 0 { self.dlist = (self.dlist & 0xFC00) | ((self.dlist + 1) & 0x3FF); if c == 6 { self._addr_lo = data; } else { let target = self._addr_lo | data << 8; if self.ir & 0x0F == 1 { self.dlist = target; if self.ir & 0x40 != 0 { self.jvb_wait = true; } } else { self.msc = target; } } } // A playfield tap may share the cycle with special DMA (an unstopped // DMA clock in horizontal blank): both take the byte on the bus. if kind & (K_NAME | K_CHAR | K_BITMAP) != 0 { if kind & K_NAME != 0 { self._names[(c & 3) as usize] = data; self._lb_write(data); self._lb_step(c); self.msc = (self.msc & 0xF000) | ((self.msc + 1) & 0x0FFF); } if kind & K_CHAR != 0 { let name = self._names[((c - 3) & 3) as usize]; let char_data = self._char_data(name, data); self._load(c, char_data, name, CHAR_DELAY); } if kind & K_BITMAP != 0 { self._lb_write(data); self._lb_step(c); self.msc = (self.msc & 0xF000) | ((self.msc + 1) & 0x0FFF); self._load(c, data, 0, BITMAP_DELAY); } } } self._render(c); // NMI falls at cycle 8 if NMIEN allowed it by cycle 6, or at 9 if it was // enabled exactly on cycle 7 (DOC [AHRM] 4.8). Once armed it is not blocked // by a later NMIEN write: TEST ROM antic_nmist finds a disable on cycle 7 // too late, against DOC [AHRM] 4.8's "by cycle 8 to disable" (C11). self.nmi_fire = false; if self._nmi_armed != 0 && self._nmi_armed == c { self._nmi_armed = 0; self.nmi_fire = true; } // Advance the counters. self.clock += 1; if c == 113 { self._next_line(); } else { self.hcount = c + 1; } } // -- internals -------------------------------------------------------------- fn _last_row(&self) -> bool { let mut end = self.row_end; if end < 0 { end = self.vscrol; } self.row == end } // The line buffer (DIE [IJOR] sheet 4; TEST ROM antic_linebuffering, // antic_hscrolbug): 48 rows of 8 bits addressed by a 6-bit // LFSR (feedback XNOR of stages 4 and 5, cleared by RamCntRst). Counted in // sequence order the LFSR is a counter of period 63 of which 48 states // select a row; the other 15 select none (U17: what such a read returns). fn _lb_read(&self) -> i32 { let index = self.lb_index; if index < 48 { self.line_buffer[index as usize] as i32 } else { LB_UNSELECTED } } fn _lb_write(&mut self, data: i32) { let index = self.lb_index; if index < 48 { self.line_buffer[index as usize] = data as u8; } } /// nRamShiftEn: step after a name or bitmap tap, unless RamCntRst holds the counter. /// /// DIE: [IJOR] sheet 6. fn _lb_step(&mut self, c: i32) { if c >= 8 { self.lb_index = (self.lb_index + 1) % 63; } else { self.lb_index = 0; } } /// IR has just been loaded on cycle 1: set up the mode line. /// /// DOC [AHRM] 4.6, 4.7 (vertical scroll region start and end); TEST ROM /// antic_vscroll, antic_vscroldli. fn _start_mode_line(&mut self) { let ir = self.ir; let mode = ir & 0x0F; let vs = ir & 0x20 != 0 && mode >= 2; let height = if mode == 0 { (ir >> 4 & 7) + 1 } else { MODE_LINES[mode as usize] }; self.row = if vs && !self.prev_vs { self._vs_latch } else { 0 }; if self.prev_vs && !vs { self.row_end = -1; // the region ends here: VSCROL sets the last row } else { self.row_end = (height - 1) & 0x0F; } self.prev_vs = vs; self.first_line = mode >= 2; } fn _next_line(&mut self) { let mut y = self.vcount; let display = (FIRST_LINE..VBLANK_LINE).contains(&y); if display && !self.jvb_wait { if self._end_check { self.new_mode_line = true; } else { self.new_mode_line = false; self.row = (self.row + 1) & 0x0F; self.first_line = false; } } else if display { self.new_mode_line = true; // DOC: JVB replays a one-line blank instruction ([AHRM] 4.6) } self._end_check = true; self.hcount = 0; y += 1; if y == self.lines { y = 0; } self.vcount = y; self.line_start = self.clock; self.queue.clear(); self.shifter = 0; self.refresh_pending = false; if y == VBLANK_LINE { self.ir &= 0xBF; // DOC: IR bit 6 is cleared across vertical blank ([AHRM] 4.6) self.dma_clock = 0; self.first_line = false; } else if y == FIRST_LINE { self.new_mode_line = true; self.jvb_wait = false; } } fn _char_address(&self, name: i32, mode: i32, chbase: i32) -> i32 { let mut row = self.row; if mode == 5 || mode == 7 { row >>= 1; } let mut data_row = row & 7; if self.chactl & 4 != 0 { data_row ^= 7; // DOC: vertical reflection ([AHRM] 4.4); TEST ROM antic_charcontrol } if mode <= 5 { return (chbase & 0xFC) << 8 | (name & 0x7F) << 3 | data_row; } (chbase & 0xFE) << 8 | (name & 0x3F) << 3 | data_row } fn _char_data(&self, name: i32, data: i32) -> i32 { // DOC: mode 3 descenders and the blank rows 8-9 ([AHRM] 4.4); TEST ROM // antic_charcontrol. let mut data = data; let mode = self.ir & 0x0F; if mode == 2 || mode == 3 { let row = self.row; let descender = (name & 0x60) == 0x60; if mode == 3 { if (row < 2 && descender) || ((8..10).contains(&row) && !descender) { data = 0; } } else if (8..10).contains(&row) && !descender { data = 0; } if name & 0x80 != 0 { if self.chactl & 1 != 0 { data = 0; // DOC: blink, blanked ([AHRM] 4.4) } if self.chactl & 2 != 0 { data ^= 0xFF; // DOC: inverse video ([AHRM] 4.4) } } } data } /// Schedule a byte into the shift register `delay` cycles from now. fn _load(&mut self, c: i32, data: i32, name: i32, delay: i32) { let hs = self.ir & 0x10 != 0 && self.ir & 0x0F >= 2; let cc = 2 * (c + delay) + if hs { self.hscrol & 1 } else { 0 }; self.queue.push_back((cc, data, name)); } /// The two AN codes of cycle `c`: positions 2c and 2c+1. fn _render(&mut self, c: i32) { let y = self.vcount; let mut pos = 2 * c; let mode = self.ir & 0x0F; let mut hires = mode == 2 || mode == 3 || mode == 15; if !(FIRST_LINE..VBLANK_LINE).contains(&y) { let code = if hires { 3 } else { 2 }; if VSYNC_LINES.contains(&y) { self.an_a = if (VISIBLE_LEFT..VISIBLE_RIGHT).contains(&pos) { 1 } else { code }; self.an_b = if (VISIBLE_LEFT..VISIBLE_RIGHT).contains(&(pos + 1)) { 1 } else { code }; } else if hires && self.dmactl & 3 != 0 { // The hires bug: ANTIC fails to hold blank in vertical blank (DOC [AHRM] // 6.2; TEST ROM antic_hiresbug). let (left, right) = REGION[(self.dmactl & 3) as usize]; self.an_a = if (left..right).contains(&pos) { 4 } else { code }; self.an_b = if (left..right).contains(&(pos + 1)) { 4 } else { code }; } else { self.an_a = code; self.an_b = code; } if !self.queue.is_empty() { self.queue.clear(); } return; } let width = self.dmactl & 3; let (left, right) = REGION[width as usize]; if mode < 2 { hires = false; } if self.queue.is_empty() && self.shifter == 0 { if !(VISIBLE_LEFT..VISIBLE_RIGHT).contains(&pos) { self.an_a = if hires { 3 } else { 2 }; } else if hires && (left..right).contains(&pos) { self.an_a = 4; } else { self.an_a = 0; } pos += 1; if !(VISIBLE_LEFT..VISIBLE_RIGHT).contains(&pos) { self.an_b = if hires { 3 } else { 2 }; } else if hires && (left..right).contains(&pos) { self.an_b = 4; } else { self.an_b = 0; } return; } self.an_a = self._pixel(pos, mode, left, right, hires); self.an_b = self._pixel(pos + 1, mode, left, right, hires); } fn _pixel(&mut self, pos: i32, mode: i32, left: i32, right: i32, hires: bool) -> u8 { if let Some(&(cc, data, name)) = self.queue.front() { if cc <= pos { self.queue.pop_front(); self.shifter |= data; self.shift_name = name; self.shift_phase = 0; } } if mode < 2 { self.shifter = 0; return if !(VISIBLE_LEFT..VISIBLE_RIGHT).contains(&pos) { 2 } else { 0 }; } let (bits, per, kind) = SHIFT[mode as usize]; let sr = self.shifter; let value = if bits == 2 { sr >> 6 } else { sr >> 7 }; self.shift_phase += 1; if self.shift_phase >= per { self.shift_phase = 0; self.shifter = (sr << bits) & 0xFF; } if !(VISIBLE_LEFT..VISIBLE_RIGHT).contains(&pos) { return if hires { 3 } else { 2 }; } if !(left..right).contains(&pos) { return 0; } if kind == 0 { return (4 | value) as u8; } if kind == 1 { return LORES2[value as usize] as u8; } if kind == 2 { return if value != 0 { 4 } else { 0 }; } if kind == 3 { if value == 3 && self.shift_name & 0x80 != 0 { return 7; } return LORES2[value as usize] as u8; } if value != 0 { (4 | (self.shift_name >> 6)) as u8 } else { 0 } } // -- state ------------------------------------------------------------------ pub fn snapshot(&self) -> ANTIC { self.clone() } pub fn restore(&mut self, state: &ANTIC) { *self = state.clone(); } } // What a cycle's DMA was for. pub const K_MISSILE: i32 = 1; pub const K_DLIST: i32 = 2; pub const K_PLAYER: i32 = 4; pub const K_ADDR: i32 = 8; pub const K_NAME: i32 = 16; pub const K_CHAR: i32 = 32; pub const K_BITMAP: i32 = 64; pub const K_REFRESH: i32 = 128; pub const K_VIRTUAL: i32 = 256; pub const K_EXTRA: i32 = 512; // What a line-buffer read returns when the address LFSR selects no row. // HYPOTHESIS (U17): the column lines are precharged ([IJOR] sheet 4), the read // polarity is not traced. Tracing the sense path on the die schematic, or a // probe ROM forcing an unselected row on a console, would settle it. pub const LB_UNSELECTED: i32 = 0xFF;