# 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. """ from __future__ import annotations REFRESH = -2 NO_DMA = -1 # DOC: scan lines per mode line, by IR mode ([AHRM] 4.4, 4.5). Mode 0 and 1 are special. MODE_LINES = (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). RATE = (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. WINDOW_START = (0, 26, 18, 10) WINDOW_LENGTH = (0, 64, 80, 96) # DOC: displayed playfield in GTIA horizontal positions, by DMACTL width ([AHRM] 4.3, 6.2). REGION = ((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. BITMAP_DELAY = 4 CHAR_DELAY = 3 VISIBLE_LEFT, VISIBLE_RIGHT = 0x22, 0xDE # DOC: [AHRM] 6.2, $22-$DD visible FIRST_LINE, VBLANK_LINE = 8, 248 # DOC: [AHRM] 4.6; TEST ROM antic_vcount, antic_nmist VSYNC_LINES = (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)) REFRESH_BOUNDARIES = frozenset(range(25, 62, 4)) # 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). SHIFT = { 2: (2, 1, 0), 3: (2, 1, 0), 4: (2, 1, 3), 5: (2, 1, 3), 6: (1, 1, 4), 7: (1, 1, 4), 8: (2, 4, 1), 9: (1, 2, 2), 10: (2, 2, 1), 11: (1, 1, 2), 12: (1, 1, 2), 13: (2, 1, 1), 14: (2, 1, 1), 15: (2, 1, 0), } LORES2 = (0, 4, 5, 6) # BAK, PF0, PF1, PF2 class ANTIC: """The chip. Registers by their [HM] names; everything else is internal state.""" def __init__(self, lines: int = 262, line: int = 0, cycle: int = 0) -> None: self.lines = lines # Counters (DOC [AHRM] 4.1: not reset by RESET; power-on value varies, U8). self.hcount = cycle self.vcount = line self.clock = 0 # machine cycles since power, for WSYNC bookkeeping self.line_start = -cycle # 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). self.dmactl = 0 self.chactl = 0 self.dlist = 0 self.hscrol = 0 self.vscrol = 0 self.pmbase = 0 self.chbase = 0 self.nmien = 0 # DOC: bits 0-4 read as 1 ([AHRM] Table 5; its power-up $7F is a bias, not a rule) self.nmist = 0x1F # HYPOTHESIS: light-pen latches, unconnected on the 5200 ([FSM] has no pen # input); values a console reads would settle them. self.penh = 0x00 self.penv = 0xFF # 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). self._dmactl_d = 0 self._chbase_d = 0 # WSYNC: the clock of the write that set the latch, or -1. self.wsync_at = -1 # Display list state. self.ir = 0 self.row = 0 self.row_end = 0 # -1: the end row is VSCROL, compared live self.prev_vs = False self.new_mode_line = True self.first_line = False self.jvb_wait = False self.dli_line = False self.msc = 0 self._addr_lo = 0 # Playfield DMA clock and shifter. self.dma_clock = 0 self._inject = False self._clear_bit = False self.line_buffer = bytearray(48) self.lb_index = 0 self._names = [0, 0, 0, 0] # character names by fetch cycle & 3 self.queue: list[tuple[int, int, int]] = [] # (colour clock, byte, name) self.shifter = 0 self.shift_name = 0 self.shift_phase = 0 self.mode = 0 # IR mode the shifter uses self.hires = False self.refresh_pending = False # Per-cycle outputs. self.kind = 0 # what this cycle's DMA was for (bit set of K_* below) self.halt = False self.address = NO_DMA self.an_a = 2 self.an_b = 2 self.nmi_fire = False self._nmi_armed = 0 # cycle at which an NMI is to fall, 0 = none, 99 = maybe at 9 self._nmi_bit = 0 self._vs_latch = 0 self._wsync_release = False self._wsync_after = -1 self._end_check = True # -- registers ---------------------------------------------------------- def read(self, register: int) -> int: register &= 0x0F if register == 0x0B: 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 and self.hcount >= 112: y = 0 return (y >> 1) & 0xFF if register == 0x0F: return self.nmist | 0x1F if register == 0x0C: return self.penh if register == 0x0D: return self.penv return 0xFF # DOC [AHRM] 4.1: $FF for unassigned addresses; TEST ROM antic_default def write(self, register: int, value: int) -> None: register &= 0x0F if register == 0x00: self.dmactl = value & 0x3F elif register == 0x01: self.chactl = value & 0x07 elif register == 0x02: self.dlist = (self.dlist & 0xFF00) | value elif register == 0x03: self.dlist = (self.dlist & 0x00FF) | value << 8 elif register == 0x04: self.hscrol = value & 0x0F elif register == 0x05: self.vscrol = value & 0x0F elif register == 0x07: self.pmbase = value elif register == 0x09: self.chbase = value elif 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). now = self.clock if self.wsync_at < 0: self.wsync_at = now elif self.hcount >= 104 and self.wsync_at < self.line_start + 104: self._wsync_after = now elif register == 0x0E: self.nmien = value & 0xE0 elif 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). keep = self._nmi_bit if self.hcount == 7 and self._nmi_armed else 0 self.nmist &= 0x1F | keep def reset(self) -> None: """The RESET pin: clears NMIEN, DMACTL and the playfield DMA clock (DOC [AHRM] 4.1).""" self.nmien = 0 self.dmactl = 0 self.dma_clock = 0 def wsync_holds(self, clock: int) -> bool: """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). """ return self.wsync_at >= 0 and clock >= self.wsync_at + 2 # -- one machine cycle ---------------------------------------------------- def begin(self) -> int: c = self.hcount y = self.vcount pm_ctl = self._dmactl_d self._dmactl_d = self.dmactl chbase = self._chbase_d self._chbase_d = self.chbase kind = 0 address = NO_DMA display = FIRST_LINE <= y < VBLANK_LINE # 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. clk = self.dma_clock stopped = False if not clk: self._clear_bit = False # nothing to clear: the stop pulse passes unseen if clk or self._inject: rate = RATE[self.ir & 0x0F] if rate: # noqa: SIM108 (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 = bool(clk & 1) clk &= 0xFE self._clear_bit = False self.dma_clock = clk mode = self.ir & 0x0F if display and mode >= 2: width = self.dmactl & 3 hs = self.ir & 0x10 fetch = width + 1 if hs and width and width < 3 else width shift = self.hscrol >> 1 if hs else 0 if fetch: start = WINDOW_START[fetch] + shift if c == start - 1: self._inject = True if c == start + WINDOW_LENGTH[fetch] - 1: self._clear_bit = True if c == 105 + shift: # DOC: the wide stop is always active ([AHRM] 4.11) self._clear_bit = True elif clk: self.dma_clock = clk = 0 # DOC: blank, jump or vertical blank clears it ([AHRM] 4.12) # 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: self.lb_index = 0 # DIE: RamCntRst (see cycle 8 below) if display: if c == 0: if pm_ctl & 0x0C: kind = K_MISSILE if self.dmactl & 0x10: address = (self.pmbase & 0xF8) << 8 | 0x300 | y else: address = (self.pmbase & 0xFC) << 8 | 0x180 | y >> 1 elif c == 1: self._vs_latch = self.vscrol # DOC [AHRM] 4.7: VSCROL by cycle 0, first row if self.new_mode_line: if not self.jvb_wait and pm_ctl & 0x20: 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() elif c < 6: if pm_ctl & 0x08: kind = K_PLAYER n = c - 2 if self.dmactl & 0x10: address = (self.pmbase & 0xF8) << 8 | (0x400 + (n << 8)) | y else: address = (self.pmbase & 0xFC) << 8 | (0x200 + (n << 7)) | y >> 1 elif ( (self.first_line or mode == 1) and not self.jvb_wait and pm_ctl & 0x20 and (mode == 1 or (mode >= 2 and self.ir & 0x40)) ): 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 and bool(self.ir & 0x80) and self._last_row() elif 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 = 8 if self.nmien & 0x40 else 99 elif self.dli_line: self.nmist = (self.nmist & 0xBF) | 0x80 self._nmi_bit = 0x80 self._nmi_armed = 8 if self.nmien & 0x80 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 steal = kind != 0 # special DMA always halts the CPU if (clk or stopped) and display: # Playfield taps: name at bit 0, bitmap at bit 2, character data at bit 3. pf = 0 pf_address = 0 if 2 <= mode <= 7: if clk & 1 and self.first_line: pf = K_NAME pf_address = self.msc if clk & 8: name = self._names[(c - 3) & 3] char_address = self._char_address(name, mode, chbase) # DOC: overlapping fetches AND their addresses ([AHRM] 4.12). pf_address = pf_address & char_address if pf else char_address pf |= K_CHAR elif clk & 4 and self.first_line: pf = K_BITMAP pf_address = self.msc if not pf and clk & 1 and 2 <= mode <= 7: # A later scan line: the name comes from the line buffer, no bus cycle. self._names[c & 3] = self._lb_read() self._lb_step(c) elif not pf and clk & 4 and mode >= 8: self._load(c, self._lb_read(), 0, BITMAP_DELAY) self._lb_step(c) if pf: # 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 <= c <= 105: steal = True # A tap colliding with special DMA drives the bus at the # same time: the addresses AND (DOC [AHRM] 4.12). address = address & pf_address if kind 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 not kind: 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 c in REFRESH_SLOTS: self.refresh_pending = True elif c in REFRESH_SLOTS or self.refresh_pending: self.refresh_pending = False kind |= K_REFRESH address = REFRESH steal = True if c == 8 and 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 = 9 if self.nmien & self._nmi_bit else 0 elif 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.line_start + 104 elif c == 105 and self._wsync_release: self.wsync_at = self._wsync_after self._wsync_after = -1 self._wsync_release = False elif 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 return address if steal else NO_DMA def end(self, data: int) -> None: c = self.hcount kind = self.kind if kind: if kind & K_DLIST: self.ir = data self.dlist = (self.dlist & 0xFC00) | ((self.dlist + 1) & 0x3FF) self._start_mode_line() elif kind & K_ADDR: self.dlist = (self.dlist & 0xFC00) | ((self.dlist + 1) & 0x3FF) if c == 6: self._addr_lo = data else: target = self._addr_lo | data << 8 if self.ir & 0x0F == 1: self.dlist = target if self.ir & 0x40: 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): if kind & K_NAME: self._names[c & 3] = data self._lb_write(data) self._lb_step(c) self.msc = (self.msc & 0xF000) | ((self.msc + 1) & 0x0FFF) if kind & K_CHAR: name = self._names[(c - 3) & 3] self._load(c, self._char_data(name, data), name, CHAR_DELAY) if kind & K_BITMAP: 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 and 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 -------------------------------------------------------------- def _last_row(self) -> bool: end = self.row_end if end < 0: end = self.vscrol return 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). def _lb_read(self) -> int: index = self.lb_index return self.line_buffer[index] if index < 48 else LB_UNSELECTED def _lb_write(self, data: int) -> None: index = self.lb_index if index < 48: self.line_buffer[index] = data def _lb_step(self, c: int) -> None: """nRamShiftEn: step after a name or bitmap tap, unless RamCntRst holds the counter. DIE: [IJOR] sheet 6.""" if c >= 8: self.lb_index = (self.lb_index + 1) % 63 else: self.lb_index = 0 def _start_mode_line(self) -> None: """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.""" ir = self.ir mode = ir & 0x0F vs = bool(ir & 0x20) and mode >= 2 height = (ir >> 4 & 7) + 1 if mode == 0 else MODE_LINES[mode] self.row = self._vs_latch if vs and not self.prev_vs else 0 if self.prev_vs and not 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 def _next_line(self) -> None: y = self.vcount display = FIRST_LINE <= y < VBLANK_LINE if display and not self.jvb_wait: if getattr(self, "_end_check", True): self.new_mode_line = True else: self.new_mode_line = False self.row = (self.row + 1) & 0x0F self.first_line = False elif 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 elif y == FIRST_LINE: self.new_mode_line = True self.jvb_wait = False def _char_address(self, name: int, mode: int, chbase: int) -> int: row = self.row if mode in (5, 7): row >>= 1 data_row = row & 7 if self.chactl & 4: 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 return (chbase & 0xFE) << 8 | (name & 0x3F) << 3 | data_row def _char_data(self, name: int, data: int) -> int: # DOC: mode 3 descenders and the blank rows 8-9 ([AHRM] 4.4); TEST ROM # antic_charcontrol. mode = self.ir & 0x0F if mode == 2 or mode == 3: row = self.row descender = (name & 0x60) == 0x60 if mode == 3: if (row < 2 and descender) or (8 <= row < 10 and not descender): data = 0 elif 8 <= row < 10 and not descender: data = 0 if name & 0x80: if self.chactl & 1: data = 0 # DOC: blink, blanked ([AHRM] 4.4) if self.chactl & 2: data ^= 0xFF # DOC: inverse video ([AHRM] 4.4) return data def _load(self, c: int, data: int, name: int, delay: int) -> None: """Schedule a byte into the shift register ``delay`` cycles from now.""" hs = self.ir & 0x10 and self.ir & 0x0F >= 2 cc = 2 * (c + delay) + (self.hscrol & 1 if hs else 0) self.queue.append((cc, data, name)) def _render(self, c: int) -> None: """The two AN codes of cycle ``c``: positions 2c and 2c+1.""" y = self.vcount pos = 2 * c queue = self.queue mode = self.ir & 0x0F hires = mode in (2, 3, 15) if not (FIRST_LINE <= y < VBLANK_LINE): code = 3 if hires else 2 if y in VSYNC_LINES: self.an_a = 1 if VISIBLE_LEFT <= pos < VISIBLE_RIGHT else code self.an_b = 1 if VISIBLE_LEFT <= pos + 1 < VISIBLE_RIGHT else code elif hires and self.dmactl & 3: # The hires bug: ANTIC fails to hold blank in vertical blank (DOC [AHRM] # 6.2; TEST ROM antic_hiresbug). left, right = REGION[self.dmactl & 3] self.an_a = 4 if left <= pos < right else code self.an_b = 4 if left <= pos + 1 < right else code else: self.an_a = self.an_b = code if queue: queue.clear() return width = self.dmactl & 3 left, right = REGION[width] if mode < 2: hires = False if not queue and not self.shifter: if pos < VISIBLE_LEFT or pos >= VISIBLE_RIGHT: self.an_a = 3 if hires else 2 elif hires and left <= pos < right: self.an_a = 4 else: self.an_a = 0 pos += 1 if pos < VISIBLE_LEFT or pos >= VISIBLE_RIGHT: self.an_b = 3 if hires else 2 elif hires and left <= pos < right: 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) def _pixel(self, pos: int, mode: int, left: int, right: int, hires: bool) -> int: queue = self.queue if queue and queue[0][0] <= pos: _, data, name = queue.pop(0) self.shifter |= data self.shift_name = name self.shift_phase = 0 if mode < 2: self.shifter = 0 return 2 if pos < VISIBLE_LEFT or pos >= VISIBLE_RIGHT else 0 bits, per, kind = SHIFT[mode] sr = self.shifter value = sr >> 6 if bits == 2 else sr >> 7 self.shift_phase += 1 if self.shift_phase >= per: self.shift_phase = 0 self.shifter = (sr << bits) & 0xFF if pos < VISIBLE_LEFT or pos >= VISIBLE_RIGHT: return 3 if hires else 2 if not (left <= pos < right): return 0 if kind == 0: return 4 | value if kind == 1: return LORES2[value] if kind == 2: return 4 if value else 0 if kind == 3: if value == 3 and self.shift_name & 0x80: return 7 return LORES2[value] return 4 | (self.shift_name >> 6) if value else 0 # -- state ------------------------------------------------------------------ def snapshot(self) -> dict: state = {} for key, value in vars(self).items(): if isinstance(value, bytearray): value = bytes(value) elif isinstance(value, list): value = [tuple(v) if isinstance(v, tuple) else v for v in value] state[key] = value return state def restore(self, state: dict) -> None: for key, value in state.items(): if key == "line_buffer": value = bytearray(value) elif isinstance(value, list): value = list(value) setattr(self, key, value) # What a cycle's DMA was for. K_MISSILE = 1 K_DLIST = 2 K_PLAYER = 4 K_ADDR = 8 K_NAME = 16 K_CHAR = 32 K_BITMAP = 64 K_REFRESH = 128 K_VIRTUAL = 256 K_EXTRA = 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. LB_UNSELECTED = 0xFF