"""M2: the background modes, driven through the board's ports, each checked against an address computed from TI's tables (data manual 2.4-2.7; Young 3 for the undocumented combinations; docs/vdp.md). Mode bits by register: R0 bit 1, R1 bit 4, R1 bit 3. The board runs a two-byte idle loop while the frame is drawn; the VDP is set up through `write_port`, the path the CPU's OUT takes. """ from __future__ import annotations import pytest from conftest import program_board from colecovision_python import BoardOptions, mame_options TEXT_LEFT = 19 - 13 # TI table 3-3: text left border 19, graphics 13 class Host: def __init__(self, options=None): self.board = program_board(b"\x18\xfe", options=options or mame_options()) # JR $ def reg(self, number, value): self.board.write_port(0xBF, value) self.board.write_port(0xBF, 0x80 | number) def poke(self, address, data): self.board.write_port(0xBF, address & 0xFF) self.board.write_port(0xBF, 0x40 | (address >> 8)) for byte in data: self.board.write_port(0xBE, byte) def frame(self): """The first frame drawn wholly after the setup.""" board, seen = self.board, 0 while True: board.step() frame = board.vdp.take_frame() if frame is not None: seen += 1 if seen == 2: return frame.pixels def setup(r0, r1, regs=None, options=None): host = Host(options) for number, value in {2: 0x06, 3: 0x80, 4: 0x00, 5: 0x36, 6: 0x07, 7: 0x04, **(regs or {})}.items(): host.reg(number, value) host.reg(0, r0) host.reg(1, 0x80 | r1) # 16K return host def test_graphics_1(): # Names at R2*$400 = $1800, patterns at R4*$800 = $0000, colours at R3*$40 = $2000. host = setup(0x00, 0x40) host.poke(0x1800 + 5 * 32 + 7, [0x41]) # row 5, column 7: pattern $41 host.poke(0x0000 + 0x41 * 8 + 3, [0b10100000]) # its line 3 host.poke(0x2000 + 0x41 // 8, [0x9C]) # one colour byte per 8 patterns: fg 9, bg C pixels = host.frame() y = 5 * 8 + 3 assert list(pixels[y][56:60]) == [9, 0xC, 9, 0xC] # Name 0 everywhere else: pattern 0 is zeros in colour byte 0 (transparent): backdrop. assert pixels[0][0] == 0x04 @pytest.mark.parametrize(("r4", "third_pattern_base"), [(0x03, 0x1000), (0x00, 0x0000)]) def test_graphics_2_thirds_and_the_r4_mask(r4, third_pattern_base): # Patterns at (R4 bit 2) * $2000 = 0; colours at (R3 bit 7) * $2000 = $2000. host = setup(0x02, 0x40, {3: 0xFF, 4: r4}) host.poke(0x1800 + 16 * 32 + 1, [0x22]) # row 16 is in the third third (rows 16-23) # Full masks: character = 2 * 256 + $22; R4 = 0 ANDs the third away: character $22. host.poke(third_pattern_base + 0x22 * 8 + 2, [0xF0]) # The R4 mask is the pattern table's only; R3 = $FF leaves the colour third alone. host.poke(0x2000 + (2 * 256 + 0x22) * 8 + 2, [0x5A]) pixels = host.frame() assert list(pixels[16 * 8 + 2][8:16]) == [5, 5, 5, 5, 0xA, 0xA, 0xA, 0xA] def test_graphics_2_r3_mask_reaches_the_colour_table(): # R3 = $9F: bits 6-0 = $1F mask the top of the character number (Young 3.3). host = setup(0x02, 0x40, {3: 0x9F, 4: 0x03}) host.poke(0x1800, [0xFF]) # row 0, column 0: character $FF; masked to $FF & $FF host.poke(0x0000 + 0xFF * 8, [0xFF]) # Colour address = $2000 | ((char * 8 + line) & (($1F << 6) | $3F)) = $2000 + $7F8. host.poke(0x2000 + 0x7F8, [0x30]) pixels = host.frame() assert list(pixels[0][0:8]) == [3] * 8 def test_multicolor(): # R1 bit 3: names at $1800, patterns at R4*$800 = 0; byte (row & 3) * 2 + (y // 4 & 1). host = setup(0x00, 0x48) host.poke(0x1800 + 2 * 32 + 3, [0x10]) # name row 2, column 3: pattern $10 host.poke(0x10 * 8 + (2 & 3) * 2 + 1, [0x6E]) # lower half of the 8-line cell pixels = host.frame() y = 2 * 8 + 4 assert list(pixels[y][24:32]) == [6, 6, 6, 6, 0xE, 0xE, 0xE, 0xE] # high nibble left (C4) def test_text(): # R1 bit 4: 40 x 24 cells of 6 pixels; R7's two colours. TI table 3-3: the text left # border is 19 pixels against 13 in the other modes, so the 240 pixels start 6 in. host = setup(0x00, 0x50, {7: 0xF4}) host.poke(0x1800 + 1 * 40 + 2, [0x07]) host.poke(0x07 * 8 + 0, [0b11001100]) # 6 pixels used: 110011 pixels = host.frame() x = TEXT_LEFT + 2 * 6 assert list(pixels[8][x : x + 6]) == [0xF, 0xF, 4, 4, 0xF, 0xF] def test_text_with_graphics_2_thirds(): # Undocumented R0 bit 1 + R1 bit 4 (Young 3.5): text with the three pattern thirds. host = setup(0x02, 0x50, {7: 0xF4, 4: 0x03, 3: 0xFF}) host.poke(0x1800 + 16 * 40, [0x01]) # row 16: third 2 host.poke(0x1000 + 0x01 * 8, [0b10000000]) pixels = host.frame() assert list(pixels[16 * 8][TEXT_LEFT : TEXT_LEFT + 2]) == [0xF, 4] def test_multicolor_with_graphics_2_thirds(): # Undocumented R0 bit 1 + R1 bit 3 (Young 3.6): multicolor whose names index the thirds. host = setup(0x02, 0x48, {4: 0x03, 3: 0xFF}) host.poke(0x1800 + 8 * 32, [0x00]) # row 8: third 1 # Character 1 * 256 + 0 (pattern $0800); byte (line >> 2) & 7 = (64 >> 2) & 7 = 0. host.poke(0x0800 + ((8 * 8 >> 2) & 7), [0x2B]) pixels = host.frame() assert list(pixels[64][0:8]) == [2, 2, 2, 2, 0xB, 0xB, 0xB, 0xB] @pytest.mark.parametrize("r1", [0x58, 0x50 | 0x08]) def test_tableless_modes(r1): # R1 bits 4 and 3 together (Young 3.7): 40 columns of 4 pixels text colour, 2 backdrop. host = setup(0x00, r1, {7: 0xF4}) pixels = host.frame() assert list(pixels[50][TEXT_LEFT : TEXT_LEFT + 12]) == [0xF, 0xF, 0xF, 0xF, 4, 4] * 2 def test_blanking_shows_the_backdrop_only(): host = setup(0x00, 0x00, {7: 0x0D}) # R1 bit 6 clear host.poke(0x1800, [0x01] * 768) host.poke(0x0008, [0xFF] * 8) host.poke(0x2000, [0xF0]) pixels = host.frame() assert set(b"".join(pixels)) == {0x0D} def test_transparent_colour_shows_the_backdrop(): host = setup(0x00, 0x40, {7: 0x0B}) host.poke(0x0000, [0xFF] * 8) # pattern 0 all foreground ... host.poke(0x2000, [0x00]) # ... in colour 0: transparent pixels = host.frame() assert set(b"".join(pixels)) == {0x0B} def test_4k_mode_moves_the_cell_with_the_cores_default_and_not_with_mames(): # R1 bit 7 = 0: MacDonald's row/column mapping (tms9918-python C11, provisional). def cell_of(options): host = Host(options) host.reg(1, 0x00) host.poke(0x0123, [0xAA]) for _ in range(20): # with windows on, the write waits for an access window host.board.step() return host.board.vdp.vram.index(0xAA) assert cell_of(mame_options()) == 0x0123 assert cell_of(BoardOptions()) != 0x0123