# The LCD controller and the video DMA All from KH ("The LCD system", "Timing", "VRAM Timing", "LCD System", "DMA", "DMA Timing"), measured on one unit with a logic analyser. Tier 3; no other evidence exists except MAME (closed box) and CC65's register names. ## The panel and how it is driven - 160 x 160 dots, 2 bits per pixel in VRAM, shown as **two 1-bit fields**: in one field the controller sends each pixel's low bit, in the next its high bit. Grey levels by the two fields (KH's table): | pixel value | field A | field B | look | | --- | --- | --- | --- | | 0 | 0 | 0 | lightest | | 1 | 0 | 1 | 1/3 dark | | 2 | 1 | 0 | 2/3 dark | | 3 | 1 | 1 | darkest | The 1/3 vs 2/3 difference comes from the frame-polarity / brightness line, toggled each field, which "darkens the display a bit" in one of the two fields (KH). How much is the panel's analogue behaviour, so the board outputs the 2-bit value per pixel (or the two 1-bit fields) and the four grey levels are the host's palette, not a claim about the hardware (U12: which field is the darker one and by how much; KH gives the field order by pixel bit, not the weights). - Nine ASIC pins: 4 data, pixel clock, line latch, frame latch, polarity, power. The column shift register takes 4 pixels per pixel clock (bits arranged 0123 0123 ... across the 160 columns); after 40 clocks a line latch lights the row; the frame latch is pulsed high for one line to start the row shift register. ## Timing (MEASURED by KH) - One LCD write every **6 clocks**; a line = 40 pixel writes + 1 latch write = **246 clocks**; a field = 160 lines = **39,360 clocks**; a frame = 2 fields = **78,720 clocks** (50.81 frames/s at 4 MHz). The pattern "does not deviate ... even when the CPU writes or reads from the VRAM". - The controller reads one VRAM byte per 6-clock slot and sends 4 of its bits (D0/D2/D4/D6 or D1/D3/D5/D7, by field): four pixels per byte, pixel *n* of a byte in bits 2n+1..2n. - A CPU access to VRAM takes one clock out of the slot; the controller "seems to read the LCD data on all 6 clocks" and keeps the last good one, so the picture is wrong only if the CPU accesses VRAM on all six clocks of a slot. - **Any write to 2026** restarts the scan at the top-left (KH "System Control"). - The LCD timing is "totally divorced" from the NMI (65,536 clocks): software cannot know where the scan is (KH "NMI"). There is no LCD interrupt and no status bit. ## The size and scroll registers - **2000 LCD_X_Size**: pixels a line; only bits 7-2 count (4-pixel units). Below A0h the right-hand columns stop being refreshed and hold their last state; A0h-C3h only slow the timing; above C3h the per-line VRAM step becomes 60h instead of 30h (the picture repeats). - **2001 LCD_Y_Size**: lines a field; above A0h the duty cycle falls (dimmer, slower); below it several rows are driven at once (repeats, darker, faster). - **Line length formula.** KH's prose gives `246 = (40 + 1) * 6` for A0h, but his summary formula reads `Scanlinelen = ((LCD_X_Size & 0xFC) + 4) * 6`, which gives 984 for A0h. The units disagree by four: **C2**. The reading consistent with his measurement is `((LCD_X_Size >> 2) + 1) * 6` clocks; his `Framelen = Scanlinelen * LCD_Y_Size * 2` then gives 78,720 for A0h/A0h. The build uses the measured figure and records C2. - **2002 X_Scroll**: bits 7-2 add to the VRAM start of each line, bits 1-0 delay the pixel stream by 0-3 pixels through a one-deep 4-bit shift register and a multiplexer (CC65 says bits 1-0 are unused: C3; KH measured it). - **2003 Y_Scroll**: adds `Y_Scroll x 30h` to the start of the field, wrapped to 8 KB; the hardware skips the last 20h bytes of VRAM (`if Vstart == 1FE0h then 0`), since 170 lines of 30h bytes fill 1FE0h. KH tested Y_Scroll = AAh (picture as for 0). - VRAM layout: **30h bytes (192 pixels) a line**, 40 bytes shown; 170 lines fit. KH gives the scan as pseudo-code. Its loops start at 1 (`for (Y = 1; Y < LCD_Y_Size; Y++)`, and likewise for X), which would draw 159 lines of 39 bytes; with his measured 41 writes a line and 160 lines a field this is read as a sketch, not a measurement (**C8**). The build writes the scan from the timing facts above and judges its pixel order against KH's prose. ## The video DMA (2008-200D) - Source 2008/9, destination 200A/B, length 200C (x 16 bytes, 0 = 4096), start: write 200D with bit 7 set. - **Only into VRAM.** KH measured a DMA to a non-VRAM destination failing and clobbering WRAM; VRAM to VRAM corrupts (the data is overwritten as it goes). The board reproduces the defined case and records the others as U13 (what exactly is written). - The registers are updated as it runs: after a DMA, the length register is 00h (so the next DMA moves 4096 bytes) and source and destination continue where they stopped. - Speed: **5 bytes every 6 clocks**, the sixth slot the LCD's; the CPU "is allowed to run for that cycle" (U9 in cpu.md). - The source may be WRAM or cartridge ROM; which bank of the 8000-BFFF window it reads (2026's bank, presumably) is U14. ## What judges the build No test ROM for this LCD exists. The judge is KH's measured timing and prose (tier 3), pictures from MAME (tier 5, closed box: a detector for regressions, not for timing, since MAME shows a frame per 61 Hz NMI where KH measured 50.81), and the build's own probe programs written with cc65 (validation.md).