# SPDX-License-Identifier: MIT """Rung 1: reset (DS (25); C6), power-on (U18), and state capture.""" from __future__ import annotations import pytest from words import core, op from upd7725_python import UPD7725, UPD96050_MODEL, CPUState def test_power_on_is_zero(): dsp = core() state = dsp.capture_state() assert state.registers() == CPUState() assert set(state.ram) == {0} and len(state.ram) == 256 def test_reset_per_data_sheet(): """DS (25): PC, FLAG A, FLAG B, SR = 0, RP = 3FFh; nothing else changes.""" dsp = core(pc=0x123, flaga=0x3F, flagb=0x3F, sr=0x9C83, rp=5, dp=0x42, a=7, k=9, tr=3) dsp.ram[3] = 0x77 dsp.reset() assert (dsp.pc, dsp.flaga, dsp.flagb, dsp.sr, dsp.rp) == (0, 0, 0, 0, 0x3FF) assert (dsp.dp, dsp.a, dsp.k, dsp.tr, dsp.ram[3]) == (0x42, 7, 9, 3, 0x77) def test_reset_rp_on_the_upd96050(): dsp = core(model=UPD96050_MODEL, rp=5) dsp.reset() assert dsp.rp == 0x3FF def test_c6_switch(): class Emulators(UPD7725): RESET_SETS_RP = False dsp = core(cls=Emulators, rp=5) dsp.reset() assert dsp.rp == 5 def test_state_round_trip(): dsp = core(op(alu=9), a=0x1234, dp=0x10, sp=2) dsp.ram[0x10] = 0xBEEF before = dsp.capture_state() dsp.step() dsp.ram[0x10] = 0 dsp.restore_state(before) assert dsp.capture_state() == before registers_only = before.registers() dsp.ram[0x10] = 1 dsp.restore_state(registers_only) assert dsp.ram[0x10] == 1 # a registers-only state leaves RAM alone assert before.za is False and CPUState(flaga=0x04).za is True def test_state_validation(): with pytest.raises(ValueError): CPUState(a=0x10000) with pytest.raises(ValueError): core().restore_state(CPUState(stack=(0,) * 8)) def test_step_returns_one_and_run_returns_count(): dsp = core() assert dsp.step() == 1 assert dsp.run(100) == 100 and dsp.pc == 101