# SPDX-License-Identifier: MIT """A viewer for rungs 0 and 1: the backgrounds as the registers stand, so that a test's printed verdict can be read from the screen (docs/validation.md "Reading the library ROMs"). This is an instrument, not the picture specification (that is rung 3: ppu/line.py). It draws the text backgrounds of modes 0 and 1 and the bitmap of modes 3-5 from GBATEK §LCD VRAM BG Screen Data Format, §LCD VRAM Bitmap BG Modes, §LCD I/O BG Control (DOC), with priorities, at the moment it is called; affine backgrounds, OBJs, windows, blending and mosaic are left out. Forced blank draws white (§LCD I/O Display Control; DOC). """ from __future__ import annotations import struct import zlib WIDTH, HEIGHT = 240, 160 WHITE = 0x7FFF def render(board) -> list[list[int]]: """160 rows of 240 15-bit BGR colours.""" bus = board.bus regs = board.ppu.regs dispcnt = regs[0] vram, palette = bus.vram, bus.palette if dispcnt & 0x80: return [[WHITE] * WIDTH for _ in range(HEIGHT)] backdrop = palette[0] | palette[1] << 8 rows = [[backdrop] * WIDTH for _ in range(HEIGHT)] mode = dispcnt & 7 if mode >= 3: if dispcnt & 0x400: draw_bitmap(rows, mode, dispcnt, vram, palette) return rows text_layers = (0, 1, 2, 3) if mode == 0 else ((0, 1) if mode == 1 else ()) layers = [] for bg in text_layers: if dispcnt & (0x100 << bg): control = regs[4 + bg] layers.append((control & 3, bg)) # Lower priority numbers are on top; equal priorities: BG0 first (DOC). Draw bottom up. for _, bg in sorted(layers, reverse=True): control = regs[4 + bg] hofs = regs[8 + 2 * bg] & 0x1FF vofs = regs[9 + 2 * bg] & 0x1FF draw_text(rows, control, hofs, vofs, vram, palette) return rows def draw_text(rows, control, hofs, vofs, vram, palette) -> None: char_base = ((control >> 2) & 3) * 0x4000 screen_base = ((control >> 8) & 0x1F) * 0x800 eight_bpp = control & 0x80 size = (control >> 14) & 3 width_tiles = 64 if size & 1 else 32 height_tiles = 64 if size & 2 else 32 for y in range(HEIGHT): row = rows[y] sy = (y + vofs) % (height_tiles * 8) for x in range(WIDTH): sx = (x + hofs) % (width_tiles * 8) tx, ty = sx >> 3, sy >> 3 block = (tx >> 5) + ((ty >> 5) * (2 if width_tiles == 64 else 1)) entry_at = screen_base + block * 0x800 + ((ty & 31) * 32 + (tx & 31)) * 2 if entry_at + 1 >= len(vram): continue entry = vram[entry_at] | vram[entry_at + 1] << 8 tile = entry & 0x3FF px, py = sx & 7, sy & 7 if entry & 0x400: px = 7 - px if entry & 0x800: py = 7 - py if eight_bpp: at = char_base + tile * 64 + py * 8 + px if at >= 0x10000: continue index = vram[at] else: at = char_base + tile * 32 + py * 4 + (px >> 1) if at >= 0x10000: continue index = (vram[at] >> ((px & 1) * 4)) & 0xF if index: index |= (entry >> 12) << 4 if index: row[x] = palette[index * 2] | palette[index * 2 + 1] << 8 def draw_bitmap(rows, mode, dispcnt, vram, palette) -> None: page = 0xA000 if dispcnt & 0x10 else 0 for y in range(HEIGHT): row = rows[y] for x in range(WIDTH): if mode == 3: at = (y * 240 + x) * 2 row[x] = vram[at] | vram[at + 1] << 8 elif mode == 4: index = vram[page + y * 240 + x] if index: row[x] = palette[index * 2] | palette[index * 2 + 1] << 8 elif x < 160 and y < 128: at = page + (y * 160 + x) * 2 row[x] = vram[at] | vram[at + 1] << 8 def to_rgb(color: int) -> tuple[int, int, int]: r, g, b = color & 31, (color >> 5) & 31, (color >> 10) & 31 return (r << 3 | r >> 2, g << 3 | g >> 2, b << 3 | b >> 2) def png_bytes(rows: list[list[int]]) -> bytes: """An 8-bit RGB PNG of the picture.""" raw = bytearray() for row in rows: raw.append(0) for color in row: raw.extend(to_rgb(color)) def chunk(kind: bytes, body: bytes) -> bytes: return ( struct.pack(">I", len(body)) + kind + body + struct.pack(">I", zlib.crc32(kind + body)) ) header = struct.pack(">IIBBBBB", len(rows[0]), len(rows), 8, 2, 0, 0, 0) return ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(bytes(raw), 6)) + chunk(b"IEND", b"") )