# SPDX-License-Identifier: MIT # Follows, where noted as [MiS], Atari7800_MiSTer (k7800) by Jamie Blanks, MIT License, # Copyright (c) 2019-2026 Jamie Blanks: DMA.sv's state order (cross-check) and # line_ram.sv's cell formats. No HDL copied. Full notice: NOTICE, section 3. """MARIA (GCC1702B NTSC / GCC1712 PAL): registers, the line and frame counters, WSYNC, MSTAT, the DMA engine, the line RAM and the pixel output. MARIA imports nothing from the board (``tests/test_boundary.py``): the board hands it ``read(address) -> byte`` for its DMA reads and calls :meth:`clock` once per MARIA clock, so a recorded bus trace can drive it alone. **Time.** One :meth:`clock` is one MARIA clock (crystal / 2: one 320-mode pixel). A line is 454 clocks numbered from HBLANK's leading edge; in the drawing's own horizontal count H (HSYNC at 0) that is H = 440, so ``H = (clock + 440) mod 454`` (DOC: [GLD] sheet 16, docs/gld-reading.md). From the same sheet: HBLANK is H 440-67 (clocks 0-81), the border H 413-92 (clocks 427-106), the visible 320 clocks H 93-412 (clocks 107-426), and LRC, where the line RAM buffers swap and DMA is cut off, is H = 412 (clock 426). The vertical count steps at HRESET (H = 452, clock 12) and VBLANK (MSTAT D7) follows it: it falls on the line where the count becomes 16 and rises at 258 (DOC: [GLD] sheet 17), at clock 12 (U4 answered there). **DMA** (DOC: [SG] "Display list list", "Display list", App. 3; [1702B] p. 13-18; the state machine [MiS] DMA.sv transcribes from [GLD] sheets 20-26 as the cross-check, OBSERVED). On a line whose HBLANK begins outside vertical blank, with CTRL's DMA mode 2, MARIA asks for the bus; the request is placed at clock 28 (the CPU "keeps running for 7 cycles" after the line starts, [SG] App. 3, DOC: a reported console measurement that includes the handover latency [MiS] fits as two CPU cycles; the exact placement is HYPOTHESIS, a HALT-vs-HSYNC capture would settle it). The board honours it at the end of the CPU cycle in progress and holds the CPU's clock in whole Φ2 periods (DOC: [MT84] 2.1, C1) while the engine below runs on the MARIA clock; the engine's time is the sum of what it did: - start-up 4 (HYPOTHESIS, C2), then per header the bytes it reads at 2 clocks each (the end test reads two: `(byte 1 & $5F) == 0`, C5, DOC: the wiki), so a 4-byte header costs 8 and a 5-byte one 10 (DOC: [SG] App. 3, reported console measurement); - graphics bytes 3 each; indirect: the character-map byte 3 and its graphics 3 (6), or 9 with CWIDTH (DOC: [SG] App. 3, reported console measurement); - an object whose graphics address falls in a Holey-DMA hole costs its header and 3 / 6 / 9 once, and is dropped (DOC: [SG] App. 3; C3 provisional); - shut-down 8, or on the last line of a zone the next DLL entry (3 bytes at 2) plus 2, then 8: null lists cost 16 and 24, the measured totals (DOC: [HF], reported console measurement; the split 6 + 2 + 8 is HYPOTHESIS, C2); - at the trailing edge of vertical blank (line ``vblank_end``, clock 12) a separate request reads the first DLL entry: 8 clocks, the measured "+8 once a frame" (DOC: [SG] App. 3, reported console measurement; which line pays was U5, settled here on the drawing's VBE request, DOC: [GLD] sheet 17); - a read that would end after LRC (clock 426) is not made: the engine skips to its shut-down (DOC: [1702B] p. 18 "aborted on the leading edge of Border"). The splits 4 / 8 inside the measured 16 are this board's choice (HYPOTHESIS: C2 gives only ranges; a per-line HALT capture would settle it); the totals are DOC. **DLI**: the DLI bit of a DLL entry raises :attr:`nmi` when the DMA that read it ends; the board passes it to the CPU one cycle after HALT is released (DOC: [1702B] p. 14). The flag behind it is [GLD] sheet 17's DLI latch (DOC): set by the DLL fetch, cleared only when a line DMA starts, so an entry with the bit read on the last DMA line raises a second NMI at the end of the next VBE fetch (docs/gld-reading.md, C23). ``dli-clear-on-nmi`` is ProSystem's rule (OBSERVED, NTSC; a detector setting), ``c23-mame`` MAME's and A7800's (OBSERVED; detector setting). **Line RAM** (DOC: [MT84] Figure 1; cell formats OBSERVED: [MiS] line_ram.sv, as it states them from [GLD]'s LINBUF sheets): two buffers of 256 five-bit cells (palette 3, colour 2), one written by DMA, one shown; they swap at LRC and the new write buffer starts empty (= background). Write mode 0: a byte gives four cells `{P2 P1 P0 Dn Dn-1}`; write mode 1: two cells `{P2 D3 D2 D7 D6}`, `{P2 D1 D0 D5 D4}`. A cell whose two colour bits are 0 is not written unless CTRL's Kangaroo bit is set. The horizontal position is 8 bits and wraps (OBSERVED: [MiS]). **Read modes** (CTRL D1-D0): 0 and 1 are 160A/B (the output decoder ignores RM0 unless RM1 is set: DOC: [GLD] sheet 32, C6 answered there); 2 is 320B/D, 3 is 320A/C, cell to colour as [SG] "DMA modes" (DOC) and [MiS] (OBSERVED) give it. The border shows BACKGRND with CTRL D3 set, black otherwise (DOC: [SG] CTRL); HBLANK is blank (DOC: [GLD] sheet 16). """ from __future__ import annotations from collections.abc import Callable, Iterator LINE_CLOCKS = 454 # DOC: [SG] App. 3, [HF] (reported console measurement); [GLD] sheet 16 H_OFFSET = 440 # H = (clock + 440) % 454; DOC: [GLD] sheet 16 VCOUNT_CLOCK = 12 # HRESET: the vertical count steps, VBLANK follows; DOC: [GLD] sheets 16-17 # DOC: [SG] App. 3 "DMA start" total (reported console measurement); the placement at 28 # is HYPOTHESIS (C2): a HALT-vs-HSYNC capture would settle it DMA_REQUEST = 28 LRC = 426 # H = 412: line RAM swap, DMA cut-off; DOC: [GLD] sheet 16 VISIBLE_START = 107 # H = 93; DOC: [GLD] sheet 16 HBLANK_END = 82 # H = 68; DOC: [GLD] sheet 16 # HYPOTHESIS (C2): the 4 / 8 split inside the DOC total of 16 ([HF], reported console # measurement); a per-line HALT capture would settle it. VBE_COST: DOC, [SG] App. 3 "+8". STARTUP, SHUTDOWN, VBE_COST = 4, 8, 8 BLANK = 0 # the value of blanked clocks in a line (the frontend crops them); not a hardware value # Register numbers (address & $1F), [SG] Appendix 2; [1702B] p. 11 (DOC). BACKGRND, WSYNC, MSTAT, DPPH, DPPL, CHARBASE, OFFSET, CTRL = ( 0x00, 0x04, 0x08, 0x0C, 0x10, 0x14, 0x18, 0x1C, ) # fmt: skip WRITTEN = 0x20 # a line RAM cell's "written this line" flag; not a hardware value class Maria: """The video chip, clock by clock.""" def __init__(self, lines: int, vblank_end: int, dma_lines: int, line: int, clock: int) -> None: self.lines = lines self.vblank_end = vblank_end self.vblank_start = vblank_end + dma_lines self.line = line self.clock_in_line = clock self.frame = 0 self.vblank = not (vblank_end <= line < self.vblank_start) self.enabled = False # INPTCTRL MEN: off, MARIA held in reset; DOC: [MT84] Fig. 2, [SG] self.registers = bytearray(32) # as last written, by address & $1F self.ctrl = 0 self.dma_on = False self.read_mode = 0 self.kangaroo = False self.rdy_low = False # the WSYNC latch self.halt = False # MARIA asks for the bus self.granted = True # the bus is MARIA's (the board clears it once HALT reaches the CPU) self.nmi = False # a DLI for the board to pass on self.read: Callable[[int], int] = lambda address: 0 # The DMA engine's registers. self.dll = 0 # address of the next DLL entry self.dl = 0 # current display list self.zone_offset = 0 self.holey = 0 # DLL D6-D5 self.dli_pending = False self.write_mode = 0 self._dma: Iterator[int] | None = None self._wait = 0 self._hbs = False # this line's HBLANK began outside vertical blank self.dma_clocks = 0 # clocks the engine ran this frame (instrumentation) # Line RAM and output. self.build = bytearray(256) self.show = bytearray(256) self.pixels = bytearray(LINE_CLOCKS) self._rendered = 0 self.colour_kill = False self.on_line: Callable[[int, bytearray, bool], None] | None = None self.faults: set[str] = set() # -- the registers ---------------------------------------------------- def read_register(self, register: int) -> int | None: """MSTAT reads D7 = vertical blank (DOC: [SG]); the other bits 0 (OBSERVED: MAME 0.285).""" if register & 0x1F == MSTAT: return 0x80 if self.vblank else 0x00 return None def write_register(self, register: int, value: int) -> None: register &= 0x1F if register & 3 or register in (BACKGRND, CTRL): # the change shows from this clock on: DOC, [MT84] Fig. 1, [GLD] sheets 27-31 (C19) self._render_to(self.clock_in_line) self.registers[register] = value if register == WSYNC: self.rdy_low = True elif register == CTRL: # bit fields: DOC, [SG] Appendix 2 CTRL self.ctrl = value self.dma_on = (value >> 5) & 3 == 2 self.read_mode = value & 3 self.kangaroo = bool(value & 0x04) self.colour_kill = bool(value & 0x80) # -- the clock -------------------------------------------------------- def clock(self) -> None: """Advance one MARIA clock.""" c = self.clock_in_line + 1 if c == LINE_CLOCKS: self._end_line() c = 0 self.clock_in_line = c if c == VCOUNT_CLOCK: self._vertical() elif c == DMA_REQUEST: if self._hbs and self.dma_on and self.enabled: self._start(self._line_dma()) elif c == LRC: self._render_to(LRC + 1) self.build, self.show = self.show, self.build self.build[:] = bytes(256) if self._dma is not None and self.granted: # The engine acts at the start of its first granted clock, then # each yielded count later; it lets go at the end of its last clock. self.dma_clocks += 1 if self._wait == 0: self._step() self._wait -= 1 if self._wait == 0: self._step() def _step(self) -> None: try: self._wait = next(self._dma) except StopIteration: self._dma = None self._wait = 0 self.halt = False if self.dli_pending: # DOC, [GLD] sheet 17: the INTF pulse follows the end of any DMA # (line or VBE) while the DLI flag is set; firing does not # clear the flag, only the start of a line DMA does. if "dli-clear-on-nmi" in self.faults: self.dli_pending = False self.nmi = True def run(self, clocks: int) -> None: for _ in range(clocks): self.clock() def _start(self, engine: Iterator[int]) -> None: self._dma = engine self._wait = 0 # not started: the first granted clock starts it self.halt = True def _end_line(self) -> None: self._render_to(LINE_CLOCKS) if self.on_line is not None: self.on_line(self.line, self.pixels, self.colour_kill) self._rendered = 0 line = self.line + 1 if line == self.lines: line = 0 self.frame += 1 self.dma_clocks = 0 self.line = line # WSYNC releases at the start of the line: HYPOTHESIS (U9); a console run of the # AtariAge 339785 screen test would settle it self.rdy_low = False self._hbs = not self.vblank def _vertical(self) -> None: line = self.line if line == self.vblank_end: self.vblank = False if self.dma_on and self.enabled: self._start(self._vbe_dma()) elif line == self.vblank_start: self.vblank = True # -- the DMA engine ----------------------------------------------------- def _dll_entry(self) -> Iterator[int]: # Entry layout (DLI D7, holey D6-D5, offset D3-D0, then address high, low) and # 2 clocks a byte: DOC, [SG] "Display list list", App. 3 read = self.read flags = read(self.dll) yield 2 high = read((self.dll + 1) & 0xFFFF) yield 2 low = read((self.dll + 2) & 0xFFFF) yield 2 self.dll = (self.dll + 3) & 0xFFFF self.dl = high << 8 | low self.zone_offset = flags & 0x0F self.holey = (flags >> 5) & 3 if flags & 0x80: self.dli_pending = True def _vbe_dma(self) -> Iterator[int]: registers = self.registers self.dll = registers[DPPH] << 8 | registers[DPPL] yield from self._dll_entry() yield VBE_COST - 6 def _in_hole(self, address: int) -> bool: # Holes for A15 set with A12 (holey 2) / A11 (holey 1): DOC, [SG] "Display list list" holey = self.holey if not address & 0x8000 or not holey: return False return bool((holey & 2 and address & 0x1000) or (holey & 1 and address & 0x0800)) def _line_dma(self) -> Iterator[int]: read = self.read # DOC, [GLD] sheet 17: a DMA begun by START (a line DMA, not the VBE fetch) # clears the DLI flag as it starts (latch at grid 136,22, cleared by # the NOR at 137,13 from the START latch and the delayed PLA term c2). self.dli_pending = False yield STARTUP pointer = self.dl offset = self.zone_offset # $5F: DOC, the wiki's "Common emulator development issues" (C5); OBSERVED the same # in [MiS] DMA.sv and MAME 0.285. "end-test-zero" is a seeded fault. end_mask = 0x00 if "end-test-zero" in self.faults else 0x5F while True: if self.clock_in_line + 4 > LRC: break # cut off at LRC; DOC, [1702B] p. 18 low = read(pointer) yield 2 mode = read((pointer + 1) & 0xFFFF) yield 2 if mode & end_mask == 0 if end_mask else mode == 0: break if mode & 0x5F == 0x40: # 5-byte header: write mode, indirect; DOC, [SG] "Display list" if self.clock_in_line + 6 > LRC: break high = read((pointer + 2) & 0xFFFF) yield 2 palette_width = read((pointer + 3) & 0xFFFF) yield 2 hpos = read((pointer + 4) & 0xFFFF) yield 2 pointer = (pointer + 5) & 0xFFFF self.write_mode = mode >> 7 indirect = bool(mode & 0x20) else: if self.clock_in_line + 4 > LRC: break high = read((pointer + 2) & 0xFFFF) yield 2 hpos = read((pointer + 3) & 0xFFFF) yield 2 pointer = (pointer + 4) & 0xFFFF palette_width = mode indirect = False # palette D7-D5, width as 32 - D4-D0, CWIDTH = CTRL D4, CHARBASE + offset: # DOC, [SG] "Display list", Appendix 2 palette = palette_width >> 5 width = palette_width & 0x1F count = 32 - width if width else 32 wide = indirect and bool(self.ctrl & 0x10) base = self.registers[CHARBASE] if indirect else high gfx_high = (base + offset) & 0xFF if self._in_hole(gfx_high << 8): cost = 9 if wide else (6 if indirect else 3) # DOC: [SG] App. 3 (C3 provisional) if self.clock_in_line + cost > LRC: break yield cost continue source = high << 8 | low for i in range(count): if indirect: if self.clock_in_line + (9 if wide else 6) > LRC: break char = read((source + i) & 0xFFFF) yield 3 address = gfx_high << 8 | char hpos = self._write_cells(read(address), palette, hpos) yield 3 if wide: hpos = self._write_cells(read((address + 1) & 0xFFFF), palette, hpos) yield 3 else: if self.clock_in_line + 3 > LRC: break address = (gfx_high << 8 | low) + i hpos = self._write_cells(read(address & 0xFFFF), palette, hpos) yield 3 if offset == 0: if "last-line-cost-dropped" in self.faults: # seeded fault: the fetch takes no time for _ in self._dll_entry(): pass else: yield from self._dll_entry() yield 2 # HYPOTHESIS (C2): the split of the DOC +8 ([HF]) as 6 + 2 if self.line == self.vblank_start and "c23-mame" in self.faults: # A detector setting, never a board behaviour (docs/undocumented- # behavior.md C23): MAME 0.285 and A7800 5.2 raise no DLI from # the entry read on the last DMA line. self.dli_pending = False else: self.zone_offset = offset - 1 yield SHUTDOWN def _write_cells(self, byte: int, palette: int, hpos: int) -> int: # Cell formats and the Kangaroo rule: OBSERVED, [MiS] line_ram.sv (from [GLD] LINBUF) cells = self.build kangaroo = self.kangaroo if self.write_mode == 0: for shift in (6, 4, 2, 0): colour = (byte >> shift) & 3 if colour or kangaroo: cells[hpos] = WRITTEN | palette << 2 | colour hpos = (hpos + 1) & 0xFF return hpos p2 = (palette & 4) << 2 colour = byte >> 6 if colour or kangaroo: cells[hpos] = WRITTEN | p2 | ((byte >> 2) & 3) << 2 | colour hpos = (hpos + 1) & 0xFF colour = (byte >> 4) & 3 if colour or kangaroo: cells[hpos] = WRITTEN | p2 | (byte & 3) << 2 | colour return (hpos + 1) & 0xFF # -- the picture ---------------------------------------------------------- def _render_to(self, end: int) -> None: start = self._rendered if end <= start: return self._rendered = end pixels = self.pixels if not self.enabled or self.vblank: pixels[start:end] = bytes(end - start) return registers = self.registers background = registers[BACKGRND] border = background if self.ctrl & 0x08 else 0 show = self.show mode = self.read_mode for c in range(start, end): if c < HBLANK_END: pixels[c] = BLANK elif c < VISIBLE_START or c > LRC: pixels[c] = border else: ix = c - VISIBLE_START cell = show[ix >> 1] if not cell & WRITTEN: pixels[c] = background continue # decodes: DOC, [SG] "DMA modes", [GLD] sheet 32; OBSERVED the same in [MiS] if mode < 2: palette, colour = (cell >> 2) & 7, cell & 3 elif mode == 2: # 320B/D palette = (cell >> 2) & 4 if ix & 1: colour = (cell & 1) << 1 | (cell >> 2) & 1 else: colour = (cell >> 1 & 1) << 1 | (cell >> 3) & 1 else: # 320A/C palette = (cell >> 2) & 7 colour = (cell & 1) << 1 if ix & 1 else (cell & 2) pixels[c] = registers[palette << 2 | colour] if colour else background