# SPDX-License-Identifier: MIT """Mikey's display: the line and frame from timers 0 and 2, display DMA, the picture. 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). """ from __future__ import annotations ACTIVE_TICKS = 1920 # TEST ROM + MEASURED (LT video-timing): fixed LCD transfer per line GROUP_TICKS = 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. DMA_TICKS = 28 DMA_OFFSET = 10 # LT: "approximately 8-12 ticks after the corresponding group begins" WIDTH, HEIGHT = 160, 102 class Display: __slots__ = ( "dma_times", "frame", "frame_period", "frame_start", "frames", "latched", "line_index", "mikey", "ready", "row_pens", "visible", ) # fmt: skip def __init__(self, mikey) -> None: self.mikey = mikey self.line_index = 1 << 20 # lines since the last Timer 2 borrow (none yet) self.visible = 0 self.latched = 0 self.dma_times: list[int] = [] self.frame = bytearray(WIDTH * HEIGHT * 2) # 12-bit colours, little-endian words self.frame_start = -1 self.frame_period = 0 self.frames = 0 self.row_pens = bytearray(WIDTH) self.ready = False def frame_borrow(self, when: int) -> None: """Timer 2 borrowed: the vertical-blank interrupt; three blank lines follow.""" 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 backup = self.mikey.timers.timers[2].backup self.visible = max(0, min(HEIGHT, backup - 2)) # LT: visible = min(102, TIM2BKUP - 2) def line_borrow(self, when: int) -> None: """Timer 0 borrowed: a line starts. Called before Timer 2 is clocked by it.""" self.line_index += 1 mikey = self.mikey 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 not mikey.dispctl & 1 or not 0 <= index < self.visible: return timer0 = mikey.timers.timers[0] line = (timer0.backup + 1) * timer0.period() active = when + max(0, line - ACTIVE_TICKS) times = [active + GROUP_TICKS * k + DMA_OFFSET for k in range(10)] 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) def draw_row(self, row: int) -> None: """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).""" mikey = self.mikey ram = mikey.ram palette = mikey.palette flip = mikey.dispctl & 2 base = self.latched + row * 80 frame = self.frame out = (row if not flip else HEIGHT - 1 - row) * WIDTH * 2 for i in range(80): if flip: byte = ram[(self.latched - row * 80 - i) & 0xFFFF] pens = (byte & 0x0F, byte >> 4) else: byte = ram[(base + i) & 0xFFFF] pens = (byte >> 4, byte & 0x0F) for pen in pens: green = palette[pen] & 0x0F blue_red = palette[0x10 + pen] frame[out] = blue_red frame[out + 1] = green out += 2 def fetching(self) -> bool: """True in a line whose pens are fetched by DMA (refresh then rides on DMA).""" index = self.line_index - 3 return bool(self.mikey.dispctl & 1) and 0 <= index < self.visible def next_dma(self) -> int: return self.dma_times[0] if self.dma_times else 1 << 62 def rgb(self) -> bytes: """The frame as 8-bit RGB triples (4-bit channels scaled by 17).""" out = bytearray(WIDTH * HEIGHT * 3) frame = self.frame for i in range(WIDTH * HEIGHT): 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 return bytes(out)