"""M1: decode and mirrors, the M1 wait, the READY wait, ports, interrupts, sockets. Expected clock counts are the Z80's documented T-states (Zilog UM0080) plus one per M1 cycle (U8A [SCH sheet 9; DB sheet 4]) plus 28 per sound write (C9, MAME's measured value, the board's default for lockstep). """ from __future__ import annotations import pytest from conftest import program_board from colecovision_python import BoardOptions, HandController, mame_options from colecovision_python.sockets import ( FakeInterrupts, FakeIO, FakeMemory, FakeVideo, FakeWait, Sockets, ) def run_steps(board, n): return [board.step() for _ in range(n)] # ---------------------------------------------------------------------- the M1 wait @pytest.mark.parametrize( ("code", "clocks", "what"), [ (b"\x00", 5, "NOP: 4 + 1"), (b"\x31\x00\x74", 11, "LD SP,nn: 10 + 1"), (b"\x2a\x00\x80", 17, "LD HL,(nn): 16 + 1"), (b"\xcd\x00\x90", 18, "CALL nn: 17 + 1"), (b"\xdd\x21\x34\x12", 16, "LD IX,nn: 14 + 2 (the prefix is its own M1)"), (b"\xed\x44", 10, "NEG: 8 + 2"), (b"\xcb\x07", 10, "RLC A: 8 + 2"), (b"\xdd\xcb\x00\x06", 25, "RLC (IX+d): 23 + 2 (d and op are not M1 cycles)"), (b"\xdd\xdd\x00", 15, "a stray DD, then DD NOP: 4 + 1 + 8 + 2"), (b"\xd3\xbe", 12, "OUT ($BE),A: 11 + 1"), (b"\xd3\xff", 40, "OUT ($FF),A: 11 + 1 + 28 READY"), (b"\xdb\xfc", 12, "IN A,($FC): 11 + 1"), ], ) def test_instruction_clocks(code, clocks, what): board = program_board(code) assert board.step() == 11 # JP $8000 at $0000: 10 + 1 # Steps until PC leaves the instruction (a stray prefix is a step of its own). total = 0 while board.cpu.pc < 0x8000 + len(code) and not board.cpu.halted: total += board.step() assert total == clocks, what def test_halt_cycles_get_the_wait(): board = program_board(b"\x76") board.step() assert board.step() == 5 # HALT itself: 4 + 1 assert run_steps(board, 3) == [5, 5, 5] # each idle M1 cycle: 4 + 1 (U8: both sources) def test_otir_to_the_sound_port_waits_per_byte(): # LD HL,$9000; LD BC,$02FF; OTIR -> two writes to $FF. board = program_board(b"\x21\x00\x90\x01\xff\x02\xed\xb3") run_steps(board, 3) assert board.step() == 21 + 2 + 28 # repeats assert board.step() == 16 + 2 + 28 # last @pytest.mark.parametrize(("on", "extra"), [(True, 1), (False, 0)]) def test_c11_nmi_response(on, extra): board = program_board(b"\x00" * 8, options=mame_options(m1_wait_on_acknowledge=on)) board.step() board.cpu.request_non_maskable_interrupt() assert board.step() == 11 + extra assert board.cpu.pc == 0x0066 @pytest.mark.parametrize(("on", "extra"), [(True, 1), (False, 0)]) def test_c11_interrupt_acknowledge(on, extra): # IM 1; EI; NOP... board = program_board(b"\xed\x56\xfb" + b"\x00" * 8, options=mame_options(m1_wait_on_acknowledge=on)) run_steps(board, 4) board.cpu.request_maskable_interrupt() assert board.step() == 13 + extra assert board.cpu.pc == 0x0038 # ---------------------------------------------------------------------- memory def test_ram_is_1k_mirrored_through_6000_7fff(): board = program_board(b"") board.write_byte(0x7000, 0x5A) for base in range(0x6000, 0x8000, 0x400): assert board.read_byte(base) == 0x5A board.write_byte(0x63FF, 0xA5) assert board.read_byte(0x7FFF) == 0xA5 def test_unmapped_and_rom_writes(): board = program_board(b"\x12\x34") assert board.read_byte(0x2000) == 0xFF # U5 assert board.read_byte(0x5FFF) == 0xFF board.write_byte(0x0000, 0x00) # the BIOS is ROM assert board.read_byte(0x0000) == 0xC3 board.write_byte(0x8000, 0x00) # a plain cartridge ignores the access assert board.read_byte(0x8000) == 0x12 def test_empty_slot_reads_open_bus(bios): from colecovision_python import Board board = Board(bios) assert {board.read_byte(a) for a in (0x8000, 0xA000, 0xC000, 0xFFFF)} == {0xFF} # ---------------------------------------------------------------------- ports def test_write_only_blocks_and_low_ports_read_open_bus(): board = program_board(b"") for port in (0x00, 0x7F, 0x80, 0x9F, 0xC0, 0xDF): assert board.read_port(port) == 0xFF # U4 def test_vdp_decodes_a0_bf_with_a0_as_mode(): board = program_board(b"") # Address $0000 for writing through $A1 (a mirror of $BF), data through $A0. board.write_port(0xA1, 0x00) board.write_port(0xA1, 0x40) board.write_port(0xA0, 0x77) assert board.vdp.vram[0] == 0x77 board.write_port(0xBF, 0x07) # R7 through the usual port board.write_port(0xBF, 0x87) assert board.vdp.regs[7] == 0x07 def test_controller_segments_and_idle_bits(): pad = HandController() board = program_board(b"", port1=pad) assert board.read_port(0xFC) == 0x7F # idle, either segment assert board.read_port(0xFF) == 0x7F # port 2 unplugged pad.directions = {"up"} pad.keys = {"1"} board.write_port(0xC0, 0) # joystick segment assert board.read_port(0xFC) == 0x7E assert board.read_port(0xFE) == 0x7F # A1 = 1: port 2 board.write_port(0x80, 0) # keypad segment assert board.read_port(0xFC) == 0x70 | 0x0D pad.keys = {"1", "2"} # the AND of $D and $7 assert board.read_port(0xE0) == 0x70 | 0x05 pad.right_fire = True assert board.read_port(0xFC) == 0x30 | 0x05 # ---------------------------------------------------------------------- NMI edge def test_nmi_is_the_rising_edge_of_the_vdp_int_line(): board = program_board(b"\x00" * 64) board.step() vdp = board.vdp vdp.status |= 0x80 # F set while IE is 0: no edge yet board.step() assert not board.cpu.non_maskable_interrupt_pending board.write_port(0xBF, 0x20) # setting IE while F is 1 is an edge (Young 2.4) board.write_port(0xBF, 0x81) assert board.cpu.non_maskable_interrupt_pending board.cpu.clear_non_maskable_interrupt() board.step() assert not board.cpu.non_maskable_interrupt_pending # a held level is not a new edge def test_u3_acknowledge_reads_the_vdp_when_pc_low_byte_is_in_a0_bf(): # Put EI; NOP... so that the interrupt is taken with PC = $80BF. code = bytearray(b"\x00" * 0xC0) code[0:3] = b"\xed\x56\xfb" # IM 1; EI for options, cleared in ((BoardOptions(), True), (mame_options(), False)): board = program_board(bytes(code), options=options) while board.cpu.pc != 0x80BF: board.step() board.vdp.status |= 0x80 board.cpu.request_maskable_interrupt() board.step() assert board.cpu.pc == 0x0038 assert bool(board.vdp.status & 0x80) is not cleared # ---------------------------------------------------------------------- sockets def test_memory_and_io_sockets_claim_what_they_cover(): memory = FakeMemory(0x2000, 0x1000) io = FakeIO({0x50: 0x12}) board = program_board(b"", sockets=Sockets(memory=memory, io=io)) board.write_byte(0x2000, 0x99) assert board.read_byte(0x2000) == 0x99 assert board.read_byte(0x3000) == 0xFF # not claimed: the console's open bus assert board.read_port(0x50) == 0x12 assert board.read_port(0x51) == 0xFF board.write_port(0x53, 1) assert io.log == [(0x53, 1)] def test_wait_socket_adds_clocks(): wait = FakeWait(7) board = program_board(b"\x00", sockets=Sockets(wait=wait)) board.step() assert board.step() == 5 + 7 def test_interrupt_socket_pulls_int_and_nmi(): lines = FakeInterrupts() board = program_board(b"\xed\x56\xfb" + b"\x00" * 8, sockets=Sockets(interrupts=lines)) run_steps(board, 3) lines.int_line = True board.step() board.step() assert board.cpu.pc == 0x0038 lines.int_line = False lines.nmi_line = True board.step() assert board.cpu.non_maskable_interrupt_pending def test_power_on_vdp_phase_is_mames_and_status_is_clear(): board = program_board(b"") assert (board.vdp.line, board.vdp.dot) == (203, 315) # MAME: raster 244, beam x 0 assert board.read_port(0xBF) == 0x00 # U4: power-on status, F not set by the phase run def test_video_socket_replaces_the_picture(bios): from colecovision_python import ColecoVision console = ColecoVision(bios=bios, sockets=Sockets(video=FakeVideo("external"))) assert console.run_frame() == "external"