# SPDX-License-Identifier: MIT """The sockets the Atari 7800 board needs (a7800-python's handoff brief, the a2600-python section). Existing TIA and RIOT objects handed to the board, with the RIOT's pulse count; the CPU/TIA phase as a machine-row parameter; the gates callable from outside with ``root=``. That the 2600 itself did not change is shown by the frame hashes (tests/frame_hashes.tsv, scripts/frame_hashes.py --check). """ from __future__ import annotations from dataclasses import replace from pathlib import Path import pytest from conftest import image_4k from a2600_python import gates from a2600_python.board import Board from a2600_python.cartridge import load from a2600_python.machine import NTSC_7800, VCS_NTSC from a2600_python.riot import RIOT from a2600_python.system import System from a2600_python.tia import TIA ROOT = Path(__file__).resolve().parents[1] # A loop that reads INTIM and RAM and writes the TIA: LDA $284; STA $80; # LDA $80; STA COLUBK; STA WSYNC; JMP start. LOOP = bytes.fromhex("ad84028580a580850985024c00f0") def cartridge(): return load(image_4k(LOOP), scheme="4K").cartridge def test_the_board_uses_handed_in_chips_as_they_stand(): tia = TIA(10) tia.inputs = 0x3F riot = RIOT(0x55, 0x40, 64, 64) riot.write(0x80, 0xA5, 100) # RAM written by "the BIOS" riot.input_b = 0x2B # whatever the host wired board = Board(VCS_NTSC, cartridge(), tia=tia, riot=riot, cycles=500) assert board.tia is tia and board.riot is riot assert board.cycles == 500 and tia.inputs == 0x3F and riot.input_b == 0x2B assert riot.ram[0] == 0xA5 seen = [] board.bus_observers.append(lambda kind, address, value, cycle: seen.append(cycle)) system = System(board.cartridge, VCS_NTSC, board=board) assert system.board is board system.step() # the reset sequence: seven cycles from the handed-in count assert seen[0] == 500 and board.cycles == 507 # The RIOT's lazy timer sees the cycle numbers go on from its own count. system.step() # LDA INTIM expected = RIOT(0x55, 0x40, 64, 64) assert riot.value == expected.timer_at(seen[-1]) def test_a_running_cpu_is_adopted_as_it_stands(): """issue a7800-python 0001: System(board=, cpu=) takes a CPU mid-run, no power-on.""" host = System(cartridge()) for _ in range(40): host.step() cpu = host.cpu state = (cpu.pc, cpu.a, cpu.x, cpu.y, cpu.s, cpu.p) board = Board(VCS_NTSC, host.cartridge, tia=host.board.tia, riot=host.board.riot, cycles=host.board.cycles) # fmt: skip seen = [] board.bus_observers.append(lambda kind, address, value, cycle: seen.append(address)) system = System(host.cartridge, VCS_NTSC, board=board, cpu=cpu) assert system.cpu is cpu and (cpu.pc, cpu.a, cpu.x, cpu.y, cpu.s, cpu.p) == state system.step() # the next instruction of the loop, not a reset sequence assert seen and seen[0] == state[0] & 0x1FFF assert host.board.cycles < board.cycles # the old board no longer sees the bus def test_a_hand_over_mid_run_continues_every_bus_cycle(): def run(hand_over: bool) -> list: system = System(cartridge()) log: list = [] system.board.bus_observers.append(lambda k, a, v, c: log.append((k, a, v, c))) for _ in range(300): system.step() if hand_over: old = system.board board = Board(VCS_NTSC, old.cartridge, tia=old.tia, riot=old.riot, cycles=old.cycles) board.bus_observers.append(lambda k, a, v, c: log.append((k, a, v, c))) board.data_bus = old.data_bus system = System(old.cartridge, VCS_NTSC, board=board, cpu=system.cpu) for _ in range(3000): system.step() return log assert run(True) == run(False) def test_on_sample_sees_every_sample_as_it_is_made(): """issue a7800-python 0003: a per-sample socket instead of replacing Audio.tick_b.""" system = System(cartridge()) tia = system.board.tia tia.audio.write(0x15, 0x04) # AUDC0: a pure tone tia.audio.write(0x19, 0x0F) # AUDV0 heard = [] tia.audio.take_samples() tia.audio.on_sample = lambda sample: heard.append((sample, tia.hclock)) system.run_cycles(76 * 50) samples = tia.audio.take_samples() assert len(samples) >= 98 and [s for s, _ in heard] == samples assert len({h for _, h in heard}) == 2 # two ticks a line, at two fixed clocks assert len(set(samples)) == 2 def test_the_riot_ports_a_host_composes_are_readable(): """issue a7800-python 0003: input_a, and port B's direction and output (read only).""" riot = RIOT(0, 0, 1024, 1024) riot.set_input_a(0x5F) riot.write(0x283, 0x14, 0) # SWBCNT: D2 and D4 outputs riot.write(0x282, 0x10, 1) # SWCHB: D4 high, D2 low assert (riot.input_a, riot.ddr_b, riot.out_b) == (0x5F, 0x14, 0x10) def test_a_clocked_scheme_takes_the_row_clock_and_the_rest_ignore_it(): """issue a7800-python 0003: load(cpu_hz=) reaches DPC's oscillator only.""" from a2600_python.cartridge import build from a2600_python.cartridge.dpc import MUSIC_HZ, NTSC_CPU_HZ from a2600_python.machine import PAL_7800 dpc_image = bytes(0x2800 + 255) pal = build(dpc_image, "DPC", cpu_hz=PAL_7800.timing.cpu_hz) assert pal.ticks_per_cycle == MUSIC_HZ / PAL_7800.timing.cpu_hz # The NTSC row's clock is the scheme's default to the last bit, so passing # it changes nothing on the 2600's reference row. assert VCS_NTSC.timing.cpu_hz == NTSC_CPU_HZ assert build(dpc_image, "DPC").ticks_per_cycle == MUSIC_HZ / NTSC_CPU_HZ plain = load(image_4k(LOOP), cpu_hz=PAL_7800.timing.cpu_hz).cartridge assert plain.name in ("2K", "4K") banked = build(bytes(0x2000), "F8", cpu_hz=PAL_7800.timing.cpu_hz) assert banked.name.startswith("F8") def test_a_2600_row_keeps_the_cycle_grid_and_a_hosted_row_does_not(): off_grid = replace(VCS_NTSC, power_on=replace(VCS_NTSC.power_on, hclock=0)) with pytest.raises(ValueError): Board(off_grid, cartridge()) for hclock, half in ((0, 0), (1, 1), (100, 1)): row = replace( NTSC_7800, power_on=replace(NTSC_7800.power_on, hclock=hclock, half_step=half) ) board = Board(row, cartridge()) assert board.tia.step_in_line == 2 * hclock + half def test_lockstep_with_a_board_built_from_handed_in_chips(): def handed_over(cart, machine): power = machine.power_on tia = TIA(power.hclock, machine.tia_revision) tia.inputs = 0x30 riot = RIOT(power.ram_fill, power.timer_value, power.timer_divider, 1023) riot.input_b = machine.switches return System(cart, machine, board=Board(machine, cart, tia=tia, riot=riot)) agreed, difference = gates.lockstep(image_4k(LOOP), system_factory=handed_over, cycles=20_000) assert difference is None and agreed >= 20_000 def test_lockstep_finds_a_board_that_differs(): def other_timer(cart, machine): power = replace(machine.power_on, timer_value=0x10) return System(cart, replace(machine, power_on=power)) _, difference = gates.lockstep(image_4k(LOOP), system_factory=other_timer, cycles=20_000) assert difference is not None def test_the_die_gate_runs_on_a_supplied_tia(): trace = ROOT / "reference/src/tia-fpga/sim/traces/phi2/vdel.trace" if not trace.exists(): pytest.skip("die traces not fetched") made = [] def factory(): made.append(TIA()) return made[-1] results = gates.die_traces(ROOT, tia_factory=factory, paths=[trace]) ((_, (scored, bad, passed)),) = results.items() assert passed and not bad and scored > 20_000 and len(made) == 1 @pytest.mark.slow def test_the_timer_rom_gate_runs_from_outside(): if not (ROOT / "tests/test_roms/stella-issues/606-129-TimerTest.zip").exists(): pytest.skip("Stella issue ROMs not fetched") results = gates.timer_roms(ROOT) assert len(results) == 14 and all(results.values()), results @pytest.mark.slow def test_the_timer_rom_gate_hands_its_row_to_the_factory(): """issue a7800-python 0002: timer_roms(machine=), as the other gates.""" if not (ROOT / "tests/test_roms/stella-issues/606-129-TimerTest.zip").exists(): pytest.skip("Stella issue ROMs not fetched") rows = [] def factory(cart, machine): rows.append(machine) return System(cart, machine) results = gates.timer_roms(ROOT, system_factory=factory, machine=NTSC_7800) assert rows == [NTSC_7800] * 14 assert len(results) == 14 and all(results.values()), results def test_the_smoke_list_is_on_gates(): """issue a7800-python 0002: gates.smoke_names(root), in file order, comments dropped.""" names = gates.smoke_names(ROOT) assert len(names) > 20 and len(set(names)) == len(names) assert "Alpha Beam with Ernie (Japan, USA) (En)" in names assert not any(n.startswith("#") or n != n.strip() for n in names)