# SPDX-License-Identifier: MIT """The minimal harness: the S-SMP facts it models for the rungs (B1-B5), not a board.""" import pytest from spc700_python.harness import SPC_SIGNATURE, Harness, Timer, default_ipl IPL = bytes(range(64)) def assemble(harness: Harness, code: str) -> None: harness.load_program(bytes.fromhex(code), 0x0300) def test_io_writes_reach_the_ram_beneath(): """B4.""" harness = Harness() assemble(harness, "8f12f8 8f34fa") # MOV $F8,#$12; MOV $FA,#$34 harness.cpu.step() harness.cpu.step() assert harness.ram[0xF8] == 0x12 assert harness.ram[0xFA] == 0x34 assert harness.timers[0].target == 0x34 def test_ipl_overlay_reads_rom_and_writes_ram(): """B4: reads from ROM, writes to RAM; CONTROL bit 7 unmaps it.""" harness = Harness(IPL) assert harness.ipl_mapped assert harness.read(0xFFC1) == 1 harness.write(0xFFC1, 0x55) assert harness.read(0xFFC1) == 1 assert harness.ram[0xFFC1] == 0x55 harness.write(0xF1, 0) assert harness.read(0xFFC1) == 0x55 assert harness.peek(0xFFC1) == 0x55 def test_ipl_must_be_64_bytes(): with pytest.raises(ValueError): Harness(b"\x00") def test_control_clears_the_input_ports_on_every_write_of_a_1(): """B1.""" harness = Harness() harness.ports_in[:] = b"\x01\x02\x03\x04" harness.write(0xF1, 0x10) assert bytes(harness.ports_in) == b"\x00\x00\x03\x04" harness.write(0xF1, 0x20) assert bytes(harness.ports_in) == bytes(4) def test_timer_counts_stage_two_to_the_target_and_reading_clears(): """B2.""" harness = Harness() harness.write(0xFA, 2) # timer 0, divide by 2 harness.write(0xF1, 0x01) for _ in range(128 * 5): harness.cpu.idle() assert harness.read(0xFD) == 2 assert harness.read(0xFD) == 0 def test_timer_target_zero_means_256(): timer = Timer() timer.enable(True) for _ in range(255): timer.tick() assert timer.output == 0 timer.tick() assert timer.output == 1 def test_timer_2_is_eight_times_faster(): harness = Harness() harness.write(0xFC, 1) harness.write(0xF1, 0x04) for _ in range(128): harness.cpu.idle() assert harness.read(0xFF) == 8 def test_dummy_read_of_a_store_clears_the_timer(): """U1 + B2: why the core makes the read: MOV $FD,A loses the count, MOV $FD,$00 not.""" for code, survives in (("c4fd", False), ("fa00fd", True)): harness = Harness() assemble(harness, code) harness.timers[0].output = 3 harness.cpu.step() assert (harness.timers[0].output == 3) is survives def test_dsp_ports_are_a_register_file_with_a_read_only_mirror(): """B5.""" harness = Harness() harness.write(0xF2, 0x05) harness.write(0xF3, 0x77) assert harness.read(0xF3) == 0x77 harness.write(0xF2, 0x85) harness.write(0xF3, 0x11) assert harness.read(0xF3) == 0x77 assert harness.read(0xF2) == 0x85 def test_ports_log_and_script(): harness = Harness() seen = [] harness.on_port_write = lambda port, value: seen.append((port, value)) harness.write(0xF5, 9) assert seen == [(1, 9)] assert harness.ports_out[1] == 9 assert harness.port_log[0][1:] == (1, 9) harness.ports_in[2] = 0x42 assert harness.read(0xF6) == 0x42 def spc_file(pc: int = 0x0400, control: int = 0x83) -> bytes: data = bytearray(0x10200) data[:33] = SPC_SIGNATURE data[0x25:0x2C] = bytes((pc & 0xFF, pc >> 8, 1, 2, 3, 4, 0xEE)) data[0x100 + 0xF1] = control data[0x100 + 0xFA] = 0x10 data[0x100 + 0xFD] = 0x05 data[0x100 + 0xF4] = 0xAB data[0x10100 + 0x0C] = 0x7F data[0x101C0] = 0x66 # under the IPL ROM return bytes(data) def test_load_spc(): harness = Harness(IPL) harness.load_file(spc_file()) state = harness.cpu.capture_state() assert (state.pc, state.a, state.x, state.y, state.psw, state.sp) == (0x400, 1, 2, 3, 4, 0xEE) assert harness.timers[0].enabled assert harness.timers[0].target == 0x10 assert harness.timers[0].output == 5 assert harness.ports_in[0] == 0xAB assert harness.dsp[0x0C] == 0x7F assert harness.ipl_mapped assert harness.ram[0xFFC0] == 0x66 def test_load_spc_rejects_a_short_file(): with pytest.raises(ValueError): Harness().load_spc(b"not one") def test_run_stops_on_a_branch_to_itself_and_on_sleep(): harness = Harness() assemble(harness, "00 2ffe") assert harness.run().reason == "self-branch" harness = Harness() assemble(harness, "00 5f0103") assert harness.run().reason == "self-branch" harness = Harness() assemble(harness, "00 ef") assert harness.run().reason == "stopped" def test_run_does_not_stop_on_dbnz_to_itself(): harness = Harness() assemble(harness, "8d03 fefe 2ffe") # MOV Y,#3; DBNZ Y,self; BRA self result = harness.run() assert result.reason == "self-branch" assert result.instructions == 5 assert harness.cpu.pc == 0x0304 def test_run_honours_max_cycles(): harness = Harness() assemble(harness, "00 00 00 00 2ffb") result = harness.run(max_cycles=100) assert result.reason == "max-cycles" assert 100 <= result.cycles <= 104 def test_default_ipl_is_the_right_size_when_present(): ipl = default_ipl() assert ipl is None or len(ipl) == 64