"""The background layers of one line: text (tiled), affine (rotated and scaled) and bitmap. Each function returns 240 pixels for one layer, left to right. A pixel is a palette index (0 = transparent) or, in the 15-bit bitmap modes 3 and 5, a colour with ``DIRECT`` set, which the compositor uses without a palette lookup (GBATEK "LCD VRAM Bitmap BG Modes"). These draw a layer whole, for a line on which nothing they read changes (the line renderer). The hardware fetches map and tile data a dot at a time; ``dot.py`` does that, on the same addresses, for lines that change while they are drawn (docs/timing.md "The PPU's line"). Background fetches at or above the OBJ boundary (6010000, or 6014000 in modes 3-5) do not reach VRAM: they return the last halfword the background engine did read (NanoBoyAdvance ``FetchVRAM_BG``, ``vram_bg_latch``). ``Latch`` holds that halfword between fetches. """ from __future__ import annotations DIRECT = 0x10000 # a bitmap pixel that is a colour, not a palette index #: text BG sizes in 32x32-tile screen blocks: (wide, tall) by BGxCNT bits 14-15 TEXT_BLOCKS = ((False, False), (True, False), (False, True), (True, True)) class Latch: """The background engine's last VRAM halfword, returned for fetches past the boundary.""" __slots__ = ("value",) def __init__(self) -> None: self.value = 0 def byte(self, vram: bytearray, address: int, boundary: int) -> int: if address < boundary: self.value = vram[address & ~1] | vram[address | 1] << 8 return vram[address] return self.value >> ((address & 1) << 3) & 0xFF def text_line( vram: bytearray, bgcnt: int, hofs: int, line: int, boundary: int, latch: Latch ) -> list[int]: """BG0-BG3 in modes 0 and 1: 8x8 tiles from a 256 or 512 dot square map. ``line`` is VCOUNT + BGxVOFS (less the mosaic counter); only its low 9 bits address the map. """ tile_base = (bgcnt >> 2 & 3) << 14 map_block = bgcnt >> 8 & 31 full_palette = bgcnt & 0x80 # 256 colours, one palette, 8 bits per pixel wide, tall = TEXT_BLOCKS[bgcnt >> 14] grid_y = (line & 0x1FF) >> 3 tile_y = line & 7 if tall and grid_y >> 5 & 1: map_block += 2 if wide else 1 row_address = (map_block << 11) + ((grid_y & 31) << 6) out = [0] * 240 hofs &= 0x1FF first = hofs >> 3 x = -(hofs & 7) # the first tile starts left of the screen by the fine scroll for column in range(first, first + 31): address = row_address + ((column & 31) << 1) if wide and column >> 5 & 1: address += 0x800 # the right-hand screen block entry = vram[address] | vram[address + 1] << 8 latch.value = entry number = entry & 0x3FF flip_x = entry & 0x400 y = 7 - tile_y if entry & 0x800 else tile_y if full_palette: row = tile_base + (number << 6) + (y << 3) if row + 7 < boundary: pixels = vram[row : row + 8] latch.value = pixels[6] | pixels[7] << 8 else: pixels = bytes(latch.byte(vram, row + i, boundary) for i in range(8)) else: row = tile_base + (number << 5) + (y << 2) if row + 3 < boundary: packed = vram[row : row + 4] latch.value = packed[2] | packed[3] << 8 else: packed = bytes(latch.byte(vram, row + i, boundary) for i in range(4)) palette = entry >> 12 << 4 pixels = [] for byte in packed: low, high = byte & 15, byte >> 4 pixels.append(low and low | palette) pixels.append(high and high | palette) if flip_x: pixels = pixels[::-1] for index in pixels: if 0 <= x < 240: out[x] = index x += 1 return out def affine_line( vram: bytearray, bgcnt: int, x: int, y: int, pa: int, pc: int, latch: Latch ) -> list[int]: """BG2 in modes 1-2 and BG3 in mode 2: a 128-1024 dot square map of 8-bit tile numbers, 256-colour tiles, sampled at (x, y) stepping by (PA, PC) per dot (8.8 fixed point).""" log_size = bgcnt >> 14 size = 128 << log_size mask = size - 1 wrap = bgcnt & 0x2000 # bit 13: wrap around instead of transparent outside the map map_base = (bgcnt >> 8 & 31) << 11 tile_base = (bgcnt >> 2 & 3) << 14 shift = 4 + log_size # tiles per map row = size / 8 out = [0] * 240 for screen_x in range(240): tx = x >> 8 ty = y >> 8 x += pa y += pc if wrap: tx &= mask ty &= mask elif (tx | ty) & -size: continue # outside a non-wrapping map: transparent # the map address is 16 bits wide (NanoBoyAdvance RenderMode2BG) tile = vram[(map_base + ((ty >> 3) << shift) + (tx >> 3)) & 0xFFFF] out[screen_x] = latch.byte( vram, tile_base + (tile << 6) + ((ty & 7) << 3) + (tx & 7), 0x10000 ) return out def bitmap_line( vram: bytearray, mode: int, page: int, x: int, y: int, pa: int, pc: int ) -> list[int]: """BG2 in modes 3-5, sampled through the affine parameters like an affine BG. Mode 3: 240x160 15-bit colours. Mode 4: 240x160 palette indices, two pages. Mode 5: 160x128 15-bit colours, two pages. Outside the bitmap the layer is transparent. """ out = [0] * 240 base = 0xA000 if page else 0 # DISPCNT bit 4 selects the second page (modes 4 and 5) width, height = (160, 128) if mode == 5 else (240, 160) for screen_x in range(240): tx = x >> 8 ty = y >> 8 x += pa y += pc if not (0 <= tx < width and 0 <= ty < height): continue if mode == 4: out[screen_x] = vram[base + ty * 240 + tx] else: address = (0 if mode == 3 else base) + (ty * width + tx) * 2 out[screen_x] = vram[address] | vram[address + 1] << 8 | DIRECT return out