// SPDX-License-Identifier: MIT //! Mikey's display: the line and frame from timers 0 and 2, display DMA, the picture //! (`mikey/display.py`). //! //! Modelling level: transaction (one DMA burst = one bus hold of fixed length). //! //! - DOC (Spec ยง5, Appx 2 DISPCTL/DISPADR) and TEST ROM + MEASURED (LT video-timing //! guide, from the Lynx II logic-analyser captures): a line starts at Timer 0's //! borrow; its last 1920 ticks are the LCD transfer, ten groups of 192 ticks, 16 //! pens each. A frame starts at Timer 2's borrow: three blank lines (the third //! latches DISPADR, low two bits masked), then the visible lines, //! `min(102, TIM2BKUP - 2)`. //! - Display DMA: ten fetches of 8 bytes per visible line, 192 ticks apart, each //! about 8-12 ticks after its LCD group begins (LT); visible line 0 gets one extra //! fetch at its Timer 0 borrow (LT). Video has the highest bus priority (Spec Appx //! "bus interplay"). //! - The picture: 4 bits per pen through the 16-entry palette (green `$FDA0+n` low //! nibble, blue|red `$FDB0+n`), flip reads the buffer backwards from DISPADR //! (Appx 2, C11). //! //! Differences forced by Rust: the Python display keeps a reference to its Mikey and reads //! DISPCTL, DISPADR, the palette, RAM and timers 0 and 2 through it; here those arrive as //! arguments ([`MikeyLines`]). `dma_times` (a list popped at the front) is a `VecDeque`. use std::collections::VecDeque; use serde::{Deserialize, Serialize}; pub const ACTIVE_TICKS: i64 = 1920; // TEST ROM + MEASURED (LT video-timing): fixed LCD transfer per line pub const GROUP_TICKS: i64 = 192; // ten groups of 16 pens (LT) /// MEASURED (scripts/la_bus.py on lynx2-mikey-page_mode.dsl): a fetch is two 4-CAS page-mode /// RAS pulses 14 ticks apart, each followed after 2 ticks by a 1-CAS pulse of about 3 ticks: /// about 28 ticks of bus from the first RAS fall to the end of the second pair. pub const DMA_TICKS: i64 = 28; pub const DMA_OFFSET: i64 = 10; // LT: "approximately 8-12 ticks after the corresponding group begins" pub const WIDTH: usize = 160; pub const HEIGHT: usize = 102; /// What the display reads of Mikey (and of the bus) when a line starts. pub struct MikeyLines<'a> { pub dispadr: u32, pub dispctl: u8, pub timer0_backup: u8, pub timer0_period: i64, pub ram: &'a [u8], pub palette: &'a [u8], } #[derive(Clone, Serialize, Deserialize)] pub struct Display { pub dma_times: VecDeque, pub frame: Vec, pub frame_period: i64, pub frame_start: i64, pub frames: u64, pub latched: u32, pub line_index: i64, pub ready: bool, pub row_pens: Vec, pub visible: i64, } impl Default for Display { fn default() -> Self { Self::new() } } impl Display { pub fn new() -> Display { Display { line_index: 1 << 20, // lines since the last Timer 2 borrow (none yet) visible: 0, latched: 0, dma_times: VecDeque::new(), frame: vec![0; WIDTH * HEIGHT * 2], // 12-bit colours, little-endian words frame_start: -1, frame_period: 0, frames: 0, row_pens: vec![0; WIDTH], ready: false, } } /// Timer 2 borrowed: the vertical-blank interrupt; three blank lines follow. pub fn frame_borrow(&mut self, when: i64, timer2_backup: u8) { if self.frame_start >= 0 { self.frame_period = when - self.frame_start; } self.frame_start = when; self.frames += 1; self.ready = true; self.line_index = 0; let backup = timer2_backup as i64; self.visible = 0.max((HEIGHT as i64).min(backup - 2)); // LT: visible = min(102, TIM2BKUP - 2) } /// Timer 0 borrowed: a line starts. Called before Timer 2 is clocked by it. pub fn line_borrow(&mut self, when: i64, mikey: &MikeyLines) { self.line_index += 1; let index = self.line_index - 3; // the frame borrow line is blank line 1 (index 0) if self.line_index == 2 { self.latched = mikey.dispadr; // LT: the third blank line latches DISPADR } if mikey.dispctl & 1 == 0 || !(0 <= index && index < self.visible) { return; } let line = (mikey.timer0_backup as i64 + 1) * mikey.timer0_period; let active = when + 0.max(line - ACTIVE_TICKS); let mut times: [i64; 10] = std::array::from_fn(|k| active + GROUP_TICKS * k as i64 + DMA_OFFSET); if index == 0 { times[0] = when; // LT: the one-off fetch at line 0's Timer 0 borrow } self.dma_times.extend(times); self.draw_row(index as usize, mikey); } /// Fetch row `row`'s pens and colour them (HYPOTHESIS: with the palette as it /// stands when the line's DMA is scheduled; the per-group timing is M3's refinement). pub fn draw_row(&mut self, row: usize, mikey: &MikeyLines) { let ram = mikey.ram; let palette = mikey.palette; let flip = mikey.dispctl & 2 != 0; let base = self.latched as usize + row * 80; let frame = &mut self.frame; let mut out = (if !flip { row } else { HEIGHT - 1 - row }) * WIDTH * 2; for i in 0..80usize { let pens = if flip { let byte = ram[(self.latched as i64 - (row * 80) as i64 - i as i64) as usize & 0xFFFF]; [byte & 0x0F, byte >> 4] } else { let byte = ram[(base + i) & 0xFFFF]; [byte >> 4, byte & 0x0F] }; for pen in pens { let green = palette[pen as usize] & 0x0F; let blue_red = palette[0x10 + pen as usize]; frame[out] = blue_red; frame[out + 1] = green; out += 2; } } } /// True in a line whose pens are fetched by DMA (refresh then rides on DMA). pub fn fetching(&self, dispctl: u8) -> bool { let index = self.line_index - 3; dispctl & 1 != 0 && 0 <= index && index < self.visible } pub fn next_dma(&self) -> i64 { self.dma_times.front().copied().unwrap_or(1 << 62) } /// The frame as 8-bit RGB triples (4-bit channels scaled by 17). pub fn rgb(&self) -> Vec { let mut out = vec![0u8; WIDTH * HEIGHT * 3]; let frame = &self.frame; for i in 0..WIDTH * HEIGHT { let (blue_red, green) = (frame[2 * i], frame[2 * i + 1]); out[3 * i] = (blue_red & 0x0F) * 17; out[3 * i + 1] = green * 17; out[3 * i + 2] = (blue_red >> 4) * 17; } out } }