# SPDX-License-Identifier: MIT """The composite (milestone 4, ``composite.py``): the rule, the transparent Mega Drive, blank rows, the order the two sides arrive in, the H40 and H32 alignment and the border. Each test names the quirk (``sega32x_python.machine.Quirks``) it pins. The rule rests on the Manual and detectors only (docs/claims.md); these tests pin the board's choice. """ from __future__ import annotations from megadrive_python.sockets import PictureLine from sega32x_python import Sega32X from sega32x_python.composite import Composite from sega32x_python.machine import Quirks from sega32x_python.vdp import _RGB MD = 0x123456 # the Mega Drive's finished colour, everywhere RED = 0x001F # BGR555 red; | 0x8000 sets the through bit THROUGH = 0x8000 def _line(start: int = 1000, *, hmode: str = "H40", ys: int = 0) -> PictureLine: width, pixel_mclk = (320, 8) if hmode == "H40" else (256, 10) return PictureLine( row=0, start=start, hmode=hmode, width=width, pixel_mclk=pixel_mclk, display_at=start + 100, output_delay=21 if hmode == "H40" else 24, rgb=[MD] * width, ys=bytes([ys]) * width, shade=bytes(width), ) def _composite() -> Composite: device = Sega32X(None) device.adapter |= 1 # ADEN: the composite only acts with the 32X enabled return Composite(device) def test_rule_through_bit_against_pri() -> None: """Quirks.composite_rule (Manual 3.3 figure 3.9): over an opaque Mega Drive pixel the 32X shows where its through bit differs from PRI.""" red = _RGB[RED] for pri, word, shows in ((0, RED, False), (0, RED | THROUGH, True), (1, RED, True), (1, RED | THROUGH, False)): # fmt: skip composite = _composite() composite.row_drawn(1000, [word] * 320, pri) out = composite.picture_line(_line(ys=0)) assert out is not None and len(out) == 320 assert out[0] == (red if shows else MD), (pri, hex(word)) def test_transparent_mega_drive_shows_the_32x() -> None: """Quirks.composite_md_transparent: where YS is 1 (no plane or sprite pixel) the 32X's pixel shows whatever PRI and the through bit say.""" for pri, word in ((0, RED), (1, RED | THROUGH)): composite = _composite() composite.row_drawn(1000, [word] * 320, pri) out = composite.picture_line(_line(ys=1)) assert out == [_RGB[RED]] * 320 def test_shade_is_not_looked_at() -> None: """Quirks.composite_ys_shadow: a shadowed transparent pixel is still transparent.""" composite = _composite() composite.row_drawn(1000, [RED] * 320, 0) line = _line(ys=1)._replace(shade=b"\x01" * 320) assert composite.picture_line(line) == [_RGB[RED]] * 320 assert Quirks().composite_ys_shadow.value == "ignored" def test_blank_row_hands_the_mega_drive_back() -> None: """Quirks.blank_row: a blank-mode row is transparent, so the line is the Mega Drive's own (None to socket 7, or the copy already handed back left as it is).""" composite = _composite() composite.row_drawn(1000, None, 1) assert composite.picture_line(_line(ys=1)) is None composite = _composite() out = composite.picture_line(_line(start=2000, ys=1)) composite.row_drawn(2000, None, 1) assert out == [MD] * 320 def test_without_aden_nothing_is_composed() -> None: """Manual 3.5: with ADEN = 0 the console is a Mega Drive (the passive gate).""" composite = Composite(Sega32X(None)) assert composite.picture_line(_line(ys=1)) is None def test_a_line_handed_over_first_is_filled_in_place() -> None: """The two sides meet by the line's start clock, in either order: the list returned for a line handed over before its 32X row becomes the composite when the row comes.""" composite = _composite() out = composite.picture_line(_line(start=5000, ys=1)) assert out == [MD] * 320 composite.row_drawn(4000, [RED] * 320, 0) # another line's row: not this one's assert out == [MD] * 320 composite.row_drawn(5000, [RED] * 320, 0) assert out == [_RGB[RED]] * 320 assert composite.composed == 1 def test_h40_is_one_on_one() -> None: """Quirks.composite_alignment H40 = 0: 32X pixel x over Mega Drive pixel x.""" composite = _composite() words = [RED | THROUGH] * 320 words[7] = RED # behind (PRI 0): the Mega Drive's pixel 7 shows through composite.row_drawn(1000, words, 0) line = _line(ys=0)._replace(rgb=list(range(320))) out = composite.picture_line(line) assert out is not None assert out[7] == 7 and out[6] == out[8] == _RGB[RED] assert Quirks().composite_alignment.value["H40"] == 0 def test_h32_spreads_320_over_256_with_the_offset() -> None: """Quirks.composite_alignment H32 = 26 MCLK and Quirks.composite_dot = 8: in H32 the 32X's 320 pixels cover the width of the Mega Drive's 256 (2,560 MCLK each), 3.25 32X pixels to the right. The line starts at the Mega Drive's output pixel 0 and is 323 wide; each output pixel takes what lies under its centre.""" composite = _composite() composite.row_drawn(1000, [RED] * 320, 0) # behind everywhere: the Mega Drive shows line = _line(hmode="H32", ys=0)._replace(rgb=list(range(256))) out = composite.picture_line(line) assert out is not None and len(out) == 323 # The output pixel's centre is 8x + 4 MCLK after the Mega Drive's output pixel 0. assert out[0] == 0 # 4 MCLK assert out[1] == 1 # 12 assert out[4] == 3 # 36 assert out[5] == 4 # 44 assert out[319] == 255 # 2,556 # 2,564 MCLK onwards is past the Mega Drive's picture: its border, taken as YS # (Quirks.composite_border), so the 32X's pixel shows. assert out[320] == out[322] == _RGB[RED] def test_h32_left_of_the_32x_shows_the_mega_drive() -> None: """Quirks.composite_alignment: the first four output pixels of an H32 line lie left of the 32X's first pixel (26 MCLK), so only the Mega Drive shows there, YS or not; the 32X's pixel x is under output pixel x + 3 (pixel 0 under the fourth, centre 28 MCLK).""" composite = _composite() words = [RED | THROUGH] * 320 words[0] = 0x03E0 | THROUGH # green: the 32X's pixel 0, to find it composite.row_drawn(1000, words, 0) line = _line(hmode="H32", ys=1)._replace(rgb=list(range(256))) out = composite.picture_line(line) assert out is not None assert out[:3] == [0, 1, 2] # centres 4, 12, 20 MCLK: Mega Drive pixels assert out[3] == _RGB[0x03E0] and out[4] == _RGB[RED] def test_rectangle_lays_narrow_rows_on_the_32x_dot() -> None: """composite.rectangle: a 256-pixel row beside 324-pixel ones comes out 324 wide, each pixel the Mega Drive's under its centre, past its end black.""" from sega32x_python.composite import rectangle rows = rectangle([[1] * 324, list(range(1, 257))]) assert len(rows[1]) == 324 assert rows[1][0] == 1 and rows[1][5] == 5 and rows[1][319] == 256 and rows[1][320] == 0 def test_quirks_of_milestone_4_keep_three_records() -> None: """docs/validation.md, "Three records, kept apart".""" for name, quirk in Quirks().table(): if quirk.milestone == 4: assert quirk.documents and quirk.hardware and quirk.unresolved, name def test_through_the_board() -> None: """Socket 7 on megadrive-python's renderer: a direct-colour 32X row over the Mega Drive's backdrop (YS everywhere) is what ``frame_rgb`` gives for that row; a blank row is the Mega Drive's own (its backdrop, CRAM colour 0 = black here).""" from test_vdp import FB, _frames, _line_table, _swap, _system system, device = _system() system.attach_picture(device) board = system.board _line_table(system, dict.fromkeys(range(8), 256), default=0x8000) for x in range(320): board.write_word(FB + 0x200 + 2 * x, RED) _swap(system, device) board.write_word(0xA15180, 2) # direct colour; rows 8 on read zeros (black) _frames(system, 3) rows = system.vdp.renderer.frame_rgb() assert rows[0] == [_RGB[RED]] * 320 assert rows[8] == [0] * 320 assert device.composite is not None and device.composite.composed >= 224 board.write_word(0xA15180, 0) # blank: the Mega Drive's line, handed back as None _frames(system, 3) assert system.vdp.renderer.frame_rgb()[0] == [0] * 320 def test_eeprom_boards_of_32x_cartridges() -> None: """Quirks.cartridge_boards: a 32X serial in the table gets its EEPROM board; any other image keeps megadrive-python's choice.""" from sega32x_python.console import cartridge_for image = bytearray(0x400) image[0x180:0x18E] = b"GM T-8102B -00" cart = cartridge_for(bytes(image)) assert cart.eeprom is not None image[0x180:0x18E] = b"GM T-9999 -00" assert cartridge_for(bytes(image)).eeprom is None def test_acclaim_sda_reads_the_line() -> None: """console.AcclaimEEPROMCartridge: /SDA at $200001 reads the wired AND of the line (the I2C bus's SDA is open-drain, NXP UM10204 3.1.1): with the EEPROM idle (it releases SDA), /SDA written 1 reads 1 and written 0 reads 0, from power-on (the EEPROM answers there always, MAME eeprom.cpp's model, OBSERVED).""" from sega32x_python.console import cartridge_for image = bytearray(0x300000) image[0x180:0x18E] = b"GM T-8102B -00" cart = cartridge_for(bytes(image)) cart.write_byte(0x200001, 1) assert cart.read_byte(0x200001) & 1 == 1 cart.write_byte(0x200001, 0) assert cart.read_byte(0x200001) & 1 == 0