# SPDX-License-Identifier: MIT """The bus contract of docs/handoff-brief.md, item by item, and the special states.""" import pytest from spc700_python import FLAG_B, FLAG_I, FLAG_P, SPC700, CPUState class Host: def __init__(self) -> None: self.memory = bytearray(0x10000) self.events: list[tuple] = [] self.cpu = SPC700(self.read, self.write, self.idle) def read(self, address: int) -> int: cpu = self.cpu self.events.append(("read", address, cpu.cycle, cpu.dummy, cpu.psw)) return self.memory[address] def write(self, address: int, value: int) -> None: cpu = self.cpu self.events.append(("write", address, cpu.cycle, cpu.psw)) self.memory[address] = value def idle(self) -> None: cpu = self.cpu self.events.append(("idle", cpu.opcode, cpu.cycle)) def test_callables_are_checked(): memory = bytearray(0x10000) with pytest.raises(TypeError): SPC700(None, memory.__setitem__) # type: ignore[arg-type] with pytest.raises(TypeError): SPC700(memory.__getitem__, None) # type: ignore[arg-type] with pytest.raises(TypeError): SPC700(memory.__getitem__, memory.__setitem__, 1) # type: ignore[arg-type] def test_idle_defaults_to_nothing_and_the_count_still_holds(): memory = bytearray(0x10000) memory[0] = 0xCF # MUL YA assert SPC700(memory.__getitem__, memory.__setitem__).step() == 9 def test_cycle_is_the_index_within_the_instruction_and_opcode_is_current_in_idle(): host = Host() host.memory[0x300] = 0x9F # XCN A host.cpu.restore_state(CPUState(pc=0x300)) assert host.cpu.step() == 5 assert [e[2] for e in host.events] == [0, 1, 2, 3, 4] assert host.events[2:] == [("idle", 0x9F, 2), ("idle", 0x9F, 3), ("idle", 0x9F, 4)] def test_p_is_current_inside_the_write_callback(): """U9: SETP then a write: the board reads P at write time.""" host = Host() host.memory[0x300:0x303] = bytes((0x40, 0xC4, 0xF0)) # SETP; MOV $F0,A host.cpu.restore_state(CPUState(pc=0x300)) host.cpu.step() host.cpu.step() kind, address, _, psw = host.events[-1] assert (kind, address) == ("write", 0x01F0) assert psw & FLAG_P def test_the_store_read_is_a_dummy_and_a_real_callback(): host = Host() host.memory[0x300:0x302] = bytes((0xC4, 0xFD)) # MOV $FD,A host.cpu.restore_state(CPUState(pc=0x300)) host.cpu.step() assert [e[3] for e in host.events[:3]] == [False, False, True] assert host.events[2][1] == 0x00FD @pytest.mark.parametrize("opcode", [0xEF, 0xFF], ids=["SLEEP", "STOP"]) def test_stopped_is_one_idle_per_step_until_reset(opcode): host = Host() host.memory[0x300] = opcode host.memory[0xFFFE:0x10000] = bytes((0xC0, 0xFF)) host.cpu.restore_state(CPUState(pc=0x300, psw=FLAG_P | FLAG_B | FLAG_I | 0x81)) assert host.cpu.step() == 1 assert host.cpu.stopped assert host.cpu.pc == 0x301 host.events.clear() for _ in range(5): assert host.cpu.step() == 1 assert host.events == [("idle", opcode, 0)] * 5 assert host.cpu.stopped host.events.clear() host.cpu.reset() assert not host.cpu.stopped assert host.cpu.pc == 0xFFC0 assert host.cpu.psw == 0x81 # P = B = I = 0, the others keep their values (U12) assert [e[:2] for e in host.events] == [("read", 0xFFFE), ("read", 0xFFFF)] def test_constructed_state_is_u12(): cpu = SPC700(bytearray(0x10000).__getitem__, bytearray(0x10000).__setitem__) assert cpu.capture_state() == CPUState(a=0, x=0, y=0, sp=0xEF, pc=0, psw=2, stopped=False) def test_ya_is_y_and_a_paired(): host = Host() host.cpu.ya = 0x1234 assert (host.cpu.y, host.cpu.a) == (0x12, 0x34) assert host.cpu.ya == 0x1234 assert host.cpu.capture_state().ya == 0x1234 @pytest.mark.parametrize( "field,value", [("a", 256), ("x", -1), ("sp", 300), ("pc", 0x10000), ("psw", 256), ("stopped", 1)], ) def test_state_is_validated(field, value): with pytest.raises(ValueError): CPUState(**{field: value})