// 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/boundary.rs`): the board hands //! [`Maria::clock`] a [`DmaBus`] for its DMA reads and calls it once per MARIA //! clock, so a recorded bus trace can drive it alone. //! //! **Time.** One [`Maria::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 [`Maria::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). //! //! Rust-forced differences (a7800-python `maria.py` at d6b220d): //! //! - **The DMA engine.** The reference writes the line DMA and the VBE fetch as Python //! generators that `yield` the clocks each step costs (`_line_dma`, `_vbe_dma`, //! `_dll_entry`, chained with `yield from`). Rust has no stable generators, so each //! is the explicit resumable state machine [`Engine`]: its locals are fields, and its //! `label` names the point after a `yield` where the next `next()` resumes. Every read //! and every assignment happens at the same resume as in the reference, in the same //! order (each label's comment quotes the Python it continues from), including the //! reference's `break` out of the object loop (which goes back to the header loop, //! not out of the list) and the post-code of `_dll_entry` that runs only at the //! resume after its third read. //! - **The DMA read.** The reference hands MARIA a callable (`maria.read = //! board.dma_read`). MARIA is a field of the board, so the callable cannot borrow the //! board while MARIA runs; instead [`Maria::clock`] takes the board's [`DmaBus`] as an //! argument and passes itself (`&mut Maria`) to each read, because the board's decoder //! reaches MARIA's MSTAT (and a light gun's view of the picture) through mirrors a //! DMA read can hit. While a read runs the engine is out of `dma` (taken, then put //! back), as nothing in a read touches it. //! - `faults` is a bit set ([`Faults`]) rather than a set of names; the board shares //! it by reading MARIA's (`board.maria.faults`), as the reference shares one set. //! - `on_line` is a boxed closure `(line, pixels, colour_kill)`. //! - `_wait` is an `i32`: after an engine ends in its first step of a clock the //! reference decrements it to -1, and this keeps that. /// MARIA's side of the bus: one DMA read (HALT asserted), as the board decodes it. pub trait DmaBus { fn dma_read(&mut self, maria: &mut Maria, address: u16) -> u8; } /// A bus that reads 0 everywhere (the reference's default `read`). pub struct NoBus; impl DmaBus for NoBus { fn dma_read(&mut self, _maria: &mut Maria, _address: u16) -> u8 { 0 } } pub const LINE_CLOCKS: u32 = 454; // DOC: [SG] App. 3, [HF] (reported console measurement); [GLD] sheet 16 pub const H_OFFSET: u32 = 440; // H = (clock + 440) % 454; DOC: [GLD] sheet 16 pub const VCOUNT_CLOCK: u32 = 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 pub const DMA_REQUEST: u32 = 28; pub const LRC: u32 = 426; // H = 412: line RAM swap, DMA cut-off; DOC: [GLD] sheet 16 pub const VISIBLE_START: u32 = 107; // H = 93; DOC: [GLD] sheet 16 pub const HBLANK_END: u32 = 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". pub const STARTUP: u32 = 4; pub const SHUTDOWN: u32 = 8; pub const VBE_COST: u32 = 8; pub const BLANK: u8 = 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). pub const BACKGRND: u8 = 0x00; pub const WSYNC: u8 = 0x04; pub const MSTAT: u8 = 0x08; pub const DPPH: u8 = 0x0C; pub const DPPL: u8 = 0x10; pub const CHARBASE: u8 = 0x14; pub const OFFSET: u8 = 0x18; pub const CTRL: u8 = 0x1C; pub const WRITTEN: u8 = 0x20; // a line RAM cell's "written this line" flag; not a hardware value /// The seeded faults and detector settings the reference names, as bits. Empty in every /// real run; not hardware values. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Faults(pub u32); impl Faults { pub const FAST_TIA: u32 = 1 << 0; // "fast-tia": TIA cycles left at 4 clocks pub const IGNORE_LOCK: u32 = 1 << 1; // "ignore-lock": the latch keeps taking writes after D0 pub const END_TEST_ZERO: u32 = 1 << 2; // "end-test-zero" pub const LAST_LINE_COST_DROPPED: u32 = 1 << 3; // "last-line-cost-dropped" pub const DLI_EARLY: u32 = 1 << 4; // "dli-early" pub const DLI_CLEAR_ON_NMI: u32 = 1 << 5; // "dli-clear-on-nmi" (ProSystem's rule; detector setting) pub const C23_MAME: u32 = 1 << 6; // "c23-mame" (MAME's and A7800's; detector setting) pub const NAMES: [(&'static str, u32); 7] = [ ("fast-tia", Faults::FAST_TIA), ("ignore-lock", Faults::IGNORE_LOCK), ("end-test-zero", Faults::END_TEST_ZERO), ("last-line-cost-dropped", Faults::LAST_LINE_COST_DROPPED), ("dli-early", Faults::DLI_EARLY), ("dli-clear-on-nmi", Faults::DLI_CLEAR_ON_NMI), ("c23-mame", Faults::C23_MAME), ]; #[inline] pub fn has(&self, bit: u32) -> bool { self.0 & bit != 0 } /// `faults.add(name)`. pub fn add(&mut self, name: &str) -> Result<(), String> { match Faults::NAMES.iter().find(|(n, _)| *n == name) { Some((_, bit)) => { self.0 |= bit; Ok(()) } None => Err(format!("unknown fault {name}")), } } } pub type LineCallback = Box; /// Which generator an [`Engine`] is. #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum Kind { Vbe, // _vbe_dma Line, // _line_dma } /// Where an [`Engine`] resumes (the point after a `yield`, or a jump target). #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub enum Label { // _vbe_dma VbeStart, VbeAfterEntry, // _dll_entry (returns to `ret`) EntryFlags, EntryHigh, EntryLow, EntryPost, // _line_dma LineStart, LineInit, Header, HeaderMode, HeaderTest, Header5PaletteWidth, Header5Hpos, Header5Done, Header4Hpos, Header4Done, Object, For, IndirectGraphics, IndirectWide, Next, AfterLoop, EntryReturned, C23, End, } /// One of the reference's DMA generators, as a resumable state machine (see the module /// header). The fields are the generators' locals. #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Engine { pub kind: Kind, pub label: Label, pub ret: Label, // where _dll_entry returns to // _dll_entry's locals pub flags: u8, pub entry_high: u8, pub entry_low: u8, // _line_dma's locals pub pointer: u32, pub offset: u8, pub end_mask: u8, pub low: u8, pub mode: u8, pub high: u8, pub palette_width: u8, pub hpos: u8, pub indirect: bool, pub palette: u8, pub count: u32, pub wide: bool, pub gfx_high: u32, pub source: u32, pub i: u32, pub char: u8, pub address: u32, } impl Engine { fn new(kind: Kind) -> Engine { Engine { kind, label: match kind { Kind::Vbe => Label::VbeStart, Kind::Line => Label::LineStart, }, ret: Label::End, flags: 0, entry_high: 0, entry_low: 0, pointer: 0, offset: 0, end_mask: 0, low: 0, mode: 0, high: 0, palette_width: 0, hpos: 0, indirect: false, palette: 0, count: 0, wide: false, gfx_high: 0, source: 0, i: 0, char: 0, address: 0, } } /// `next(generator)`: run to the next `yield` (its clocks) or to the end (None). fn resume(&mut self, m: &mut Maria, bus: &mut dyn DmaBus) -> Option { loop { match self.label { // -- _vbe_dma -------------------------------------------------- Label::VbeStart => { // registers = self.registers; self.dll = DPPH << 8 | DPPL m.dll = (m.registers[DPPH as usize] as u32) << 8 | m.registers[DPPL as usize] as u32; // yield from self._dll_entry() self.ret = Label::VbeAfterEntry; self.label = Label::EntryFlags; } Label::VbeAfterEntry => { self.label = Label::End; return Some(VBE_COST - 6); } // -- _dll_entry -------------------------------------------------- Label::EntryFlags => { // 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 self.flags = bus.dma_read(m, m.dll as u16); self.label = Label::EntryHigh; return Some(2); } Label::EntryHigh => { self.entry_high = bus.dma_read(m, ((m.dll + 1) & 0xFFFF) as u16); self.label = Label::EntryLow; return Some(2); } Label::EntryLow => { self.entry_low = bus.dma_read(m, ((m.dll + 2) & 0xFFFF) as u16); self.label = Label::EntryPost; return Some(2); } Label::EntryPost => { let flags = self.flags; m.dll = (m.dll + 3) & 0xFFFF; m.dl = (self.entry_high as u32) << 8 | self.entry_low as u32; m.zone_offset = flags & 0x0F; m.holey = (flags >> 5) & 3; if flags & 0x80 != 0 { m.dli_pending = true; } self.label = self.ret; } // -- _line_dma ---------------------------------------------------- Label::LineStart => { // 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). m.dli_pending = false; self.label = Label::LineInit; return Some(STARTUP); } Label::LineInit => { self.pointer = m.dl; self.offset = m.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. self.end_mask = if m.faults.has(Faults::END_TEST_ZERO) { 0x00 } else { 0x5F }; self.label = Label::Header; } Label::Header => { // while True: if m.clock_in_line + 4 > LRC { self.label = Label::AfterLoop; // cut off at LRC; DOC, [1702B] p. 18 continue; } self.low = bus.dma_read(m, self.pointer as u16); self.label = Label::HeaderMode; return Some(2); } Label::HeaderMode => { self.mode = bus.dma_read(m, ((self.pointer + 1) & 0xFFFF) as u16); self.label = Label::HeaderTest; return Some(2); } Label::HeaderTest => { let mode = self.mode; let end = if self.end_mask != 0 { mode & self.end_mask == 0 } else { mode == 0 }; if end { self.label = Label::AfterLoop; continue; } if mode & 0x5F == 0x40 { // 5-byte header: write mode, indirect; DOC, [SG] "Display list" if m.clock_in_line + 6 > LRC { self.label = Label::AfterLoop; continue; } self.high = bus.dma_read(m, ((self.pointer + 2) & 0xFFFF) as u16); self.label = Label::Header5PaletteWidth; return Some(2); } if m.clock_in_line + 4 > LRC { self.label = Label::AfterLoop; continue; } self.high = bus.dma_read(m, ((self.pointer + 2) & 0xFFFF) as u16); self.label = Label::Header4Hpos; return Some(2); } Label::Header5PaletteWidth => { self.palette_width = bus.dma_read(m, ((self.pointer + 3) & 0xFFFF) as u16); self.label = Label::Header5Hpos; return Some(2); } Label::Header5Hpos => { self.hpos = bus.dma_read(m, ((self.pointer + 4) & 0xFFFF) as u16); self.label = Label::Header5Done; return Some(2); } Label::Header5Done => { self.pointer = (self.pointer + 5) & 0xFFFF; m.write_mode = self.mode >> 7; self.indirect = self.mode & 0x20 != 0; self.label = Label::Object; } Label::Header4Hpos => { self.hpos = bus.dma_read(m, ((self.pointer + 3) & 0xFFFF) as u16); self.label = Label::Header4Done; return Some(2); } Label::Header4Done => { self.pointer = (self.pointer + 4) & 0xFFFF; self.palette_width = self.mode; self.indirect = false; self.label = Label::Object; } Label::Object => { // palette D7-D5, width as 32 - D4-D0, CWIDTH = CTRL D4, CHARBASE + offset: // DOC, [SG] "Display list", Appendix 2 self.palette = self.palette_width >> 5; let width = (self.palette_width & 0x1F) as u32; self.count = if width != 0 { 32 - width } else { 32 }; self.wide = self.indirect && m.ctrl & 0x10 != 0; let base = if self.indirect { m.registers[CHARBASE as usize] } else { self.high } as u32; self.gfx_high = (base + self.offset as u32) & 0xFF; if m.in_hole(self.gfx_high << 8) { // DOC: [SG] App. 3 (C3 provisional) let cost = if self.wide { 9 } else if self.indirect { 6 } else { 3 }; if m.clock_in_line + cost > LRC { self.label = Label::AfterLoop; continue; } self.label = Label::Header; // continue return Some(cost); } self.source = (self.high as u32) << 8 | self.low as u32; self.i = 0; self.label = Label::For; } Label::For => { // for i in range(count): if self.i >= self.count { self.label = Label::Header; continue; } if self.indirect { if m.clock_in_line + (if self.wide { 9 } else { 6 }) > LRC { self.label = Label::Header; // break: out of the object loop only continue; } self.char = bus.dma_read(m, ((self.source + self.i) & 0xFFFF) as u16); self.label = Label::IndirectGraphics; return Some(3); } if m.clock_in_line + 3 > LRC { self.label = Label::Header; // break: out of the object loop only continue; } let address = (self.gfx_high << 8 | self.low as u32) + self.i; let byte = bus.dma_read(m, (address & 0xFFFF) as u16); self.hpos = m.write_cells(byte, self.palette, self.hpos); self.label = Label::Next; return Some(3); } Label::IndirectGraphics => { self.address = self.gfx_high << 8 | self.char as u32; let byte = bus.dma_read(m, self.address as u16); self.hpos = m.write_cells(byte, self.palette, self.hpos); self.label = Label::IndirectWide; return Some(3); } Label::IndirectWide => { if self.wide { let byte = bus.dma_read(m, ((self.address + 1) & 0xFFFF) as u16); self.hpos = m.write_cells(byte, self.palette, self.hpos); self.label = Label::Next; return Some(3); } self.label = Label::Next; } Label::Next => { self.i += 1; self.label = Label::For; } Label::AfterLoop => { if self.offset == 0 { if m.faults.has(Faults::LAST_LINE_COST_DROPPED) { // seeded fault: the fetch takes no time (`for _ in self._dll_entry(): pass`) self.flags = bus.dma_read(m, m.dll as u16); self.entry_high = bus.dma_read(m, ((m.dll + 1) & 0xFFFF) as u16); self.entry_low = bus.dma_read(m, ((m.dll + 2) & 0xFFFF) as u16); self.ret = Label::C23; self.label = Label::EntryPost; continue; } // yield from self._dll_entry() self.ret = Label::EntryReturned; self.label = Label::EntryFlags; continue; } m.zone_offset = self.offset - 1; self.label = Label::End; return Some(SHUTDOWN); } Label::EntryReturned => { self.label = Label::C23; return Some(2); // HYPOTHESIS (C2): the split of the DOC +8 ([HF]) as 6 + 2 } Label::C23 => { if m.line == m.vblank_start && m.faults.has(Faults::C23_MAME) { // 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. m.dli_pending = false; } self.label = Label::End; return Some(SHUTDOWN); } Label::End => return None, } } } } /// The video chip, clock by clock. pub struct Maria { pub lines: u32, pub vblank_end: u32, pub vblank_start: u32, pub line: u32, pub clock_in_line: u32, pub frame: u64, pub vblank: bool, pub enabled: bool, // INPTCTRL MEN: off, MARIA held in reset; DOC: [MT84] Fig. 2, [SG] pub registers: [u8; 32], // as last written, by address & $1F pub ctrl: u8, pub dma_on: bool, pub read_mode: u8, pub kangaroo: bool, pub rdy_low: bool, // the WSYNC latch pub halt: bool, // MARIA asks for the bus pub granted: bool, // the bus is MARIA's (the board clears it once HALT reaches the CPU) pub nmi: bool, // a DLI for the board to pass on // The DMA engine's registers. pub dll: u32, // address of the next DLL entry pub dl: u32, // current display list pub zone_offset: u8, pub holey: u8, // DLL D6-D5 pub dli_pending: bool, pub write_mode: u8, pub dma: Option, pub wait: i32, pub hbs: bool, // this line's HBLANK began outside vertical blank pub dma_clocks: u64, // clocks the engine ran this frame (instrumentation) // Line RAM and output. pub build: [u8; 256], pub show: [u8; 256], pub pixels: [u8; LINE_CLOCKS as usize], pub rendered: u32, pub colour_kill: bool, pub on_line: Option, pub faults: Faults, } impl Maria { pub fn new(lines: u32, vblank_end: u32, dma_lines: u32, line: u32, clock: u32) -> Maria { let vblank_start = vblank_end + dma_lines; Maria { lines, vblank_end, vblank_start, line, clock_in_line: clock, frame: 0, vblank: !(vblank_end <= line && line < vblank_start), enabled: false, registers: [0; 32], ctrl: 0, dma_on: false, read_mode: 0, kangaroo: false, rdy_low: false, halt: false, granted: true, nmi: false, dll: 0, dl: 0, zone_offset: 0, holey: 0, dli_pending: false, write_mode: 0, dma: None, wait: 0, hbs: false, dma_clocks: 0, build: [0; 256], show: [0; 256], pixels: [0; LINE_CLOCKS as usize], rendered: 0, colour_kill: false, on_line: None, faults: Faults::default(), } } // -- the registers ---------------------------------------------------- /// MSTAT reads D7 = vertical blank (DOC: [SG]); the other bits 0 (OBSERVED: MAME 0.285). pub fn read_register(&self, register: u8) -> Option { if register & 0x1F == MSTAT { return Some(if self.vblank { 0x80 } else { 0x00 }); } None } pub fn write_register(&mut self, register: u8, value: u8) { let register = register & 0x1F; if register & 3 != 0 || register == BACKGRND || register == 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 as usize] = value; if register == WSYNC { self.rdy_low = true; } else if 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 = value & 0x04 != 0; self.colour_kill = value & 0x80 != 0; } } // -- the clock -------------------------------------------------------- /// Advance one MARIA clock. #[inline] pub fn clock(&mut self, bus: &mut dyn DmaBus) { let mut 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(); } else if c == DMA_REQUEST { if self.hbs && self.dma_on && self.enabled { self.start(Kind::Line); } } else if c == LRC { self.render_to(LRC + 1); std::mem::swap(&mut self.build, &mut self.show); self.build = [0; 256]; } if self.dma.is_some() && 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(bus); } self.wait -= 1; if self.wait == 0 { self.step(bus); } } } fn step(&mut self, bus: &mut dyn DmaBus) { let mut engine = self.dma.take().expect("an engine to step"); match engine.resume(self, bus) { Some(wait) => { self.wait = wait as i32; self.dma = Some(engine); } None => { 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 self.faults.has(Faults::DLI_CLEAR_ON_NMI) { self.dli_pending = false; } self.nmi = true; } } } } pub fn run(&mut self, clocks: u32, bus: &mut dyn DmaBus) { for _ in 0..clocks { self.clock(bus); } } fn start(&mut self, kind: Kind) { self.dma = Some(Engine::new(kind)); self.wait = 0; // not started: the first granted clock starts it self.halt = true; } fn end_line(&mut self) { self.render_to(LINE_CLOCKS); if let Some(callback) = self.on_line.as_mut() { callback(self.line, &self.pixels, self.colour_kill); } self.rendered = 0; let mut 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 = !self.vblank; } fn vertical(&mut self) { let line = self.line; if line == self.vblank_end { self.vblank = false; if self.dma_on && self.enabled { self.start(Kind::Vbe); } } else if line == self.vblank_start { self.vblank = true; } } // -- the DMA engine ----------------------------------------------------- fn in_hole(&self, address: u32) -> bool { // Holes for A15 set with A12 (holey 2) / A11 (holey 1): DOC, [SG] "Display list list" let holey = self.holey; if address & 0x8000 == 0 || holey == 0 { return false; } (holey & 2 != 0 && address & 0x1000 != 0) || (holey & 1 != 0 && address & 0x0800 != 0) } fn write_cells(&mut self, byte: u8, palette: u8, hpos: u8) -> u8 { // Cell formats and the Kangaroo rule: OBSERVED, [MiS] line_ram.sv (from [GLD] LINBUF) let kangaroo = self.kangaroo; let cells = &mut self.build; let mut hpos = hpos; if self.write_mode == 0 { for shift in [6, 4, 2, 0] { let colour = (byte >> shift) & 3; if colour != 0 || kangaroo { cells[hpos as usize] = WRITTEN | palette << 2 | colour; } hpos = hpos.wrapping_add(1); } return hpos; } let p2 = (palette & 4) << 2; let colour = byte >> 6; if colour != 0 || kangaroo { cells[hpos as usize] = WRITTEN | p2 | ((byte >> 2) & 3) << 2 | colour; } hpos = hpos.wrapping_add(1); let colour = (byte >> 4) & 3; if colour != 0 || kangaroo { cells[hpos as usize] = WRITTEN | p2 | (byte & 3) << 2 | colour; } hpos.wrapping_add(1) } // -- the picture ---------------------------------------------------------- pub fn render_to(&mut self, end: u32) { let start = self.rendered; if end <= start { return; } self.rendered = end; let pixels = &mut self.pixels; if !self.enabled || self.vblank { pixels[start as usize..end as usize].fill(0); return; } let registers = &self.registers; let background = registers[BACKGRND as usize]; let border = if self.ctrl & 0x08 != 0 { background } else { 0 }; let show = &self.show; let mode = self.read_mode; for c in start..end { if c < HBLANK_END { pixels[c as usize] = BLANK; } else if c < VISIBLE_START || c > LRC { pixels[c as usize] = border; } else { let ix = c - VISIBLE_START; let cell = show[(ix >> 1) as usize]; if cell & WRITTEN == 0 { pixels[c as usize] = background; continue; } // decodes: DOC, [SG] "DMA modes", [GLD] sheet 32; OBSERVED the same in [MiS] let (palette, colour); if mode < 2 { palette = (cell >> 2) & 7; colour = cell & 3; } else if mode == 2 { // 320B/D palette = (cell >> 2) & 4; if ix & 1 != 0 { colour = (cell & 1) << 1 | (cell >> 2) & 1; } else { colour = (cell >> 1 & 1) << 1 | (cell >> 3) & 1; } } else { // 320A/C palette = (cell >> 2) & 7; colour = if ix & 1 != 0 { (cell & 1) << 1 } else { cell & 2 }; } pixels[c as usize] = if colour != 0 { registers[(palette << 2 | colour) as usize] } else { background }; } } } }