# SPDX-License-Identifier: MIT """Rung 1, flow: jumps and their costs (U1), calls and the stack (C4), far jumps (C5, C17), SKIP, HALT and the IRQ (U13). Costs are byuu's model's (ares ``step(2)``).""" from __future__ import annotations from rig import Rig from hg51b_python import HG51B def test_jump_taken_costs_two_more_clocks() -> None: rig = Rig() rig.place([0x0803, 0x0000, 0x0000, 0xFC00]) # JMP $03 over two NOPs, HALT rig.cx.run(10_000) # nothing: halted rig.start() loaded = rig.cx.clock rig.finish() assert rig.cx.instructions == 2 # one page load (512 x (1 + 4)) + JMP (1 + 2) + HALT (1) assert rig.cx.clock - loaded == 2560 + 3 + 1 def test_conditional_jump_not_taken_costs_one() -> None: rig = Rig() rig.cx.z = False rig.place([0x0C03, 0xFC00]) # JMP EQ,$03 (not taken), HALT rig.start() start = rig.cx.clock rig.finish() assert rig.cx.instructions == 2 and rig.cx.clock - start == 2560 + 2 def test_conditions() -> None: for word, flag in ((0x0C00, "z"), (0x1000, "c"), (0x1400, "n"), (0x1800, "v")): for state in (False, True): rig = Rig() setattr(rig.cx, flag, state) # word -> $03; LD A,#1; HALT; (03:) LD A,#2; HALT rig.execute([word | 3, 0x6401, 0xFC00, 0x6402]) assert rig.cx.a == (2 if state else 1), (hex(word), state) def test_jsr_rts_and_the_stack() -> None: rig = Rig() # 00: JSR $04; 01: LD A,#7; 02: HALT; 04: LD R0? (ST R0,A after LD A,#3); RTS rig.place([0x2804, 0x6407, 0xFC00, 0x0000, 0x6403, 0xE060, 0x3C00]) rig.start() rig.finish() assert rig.cx.r[0] == 3 and rig.cx.a == 7 assert rig.cx.stack == [0] * 8 # pulled def test_stack_ares_shift_register_drops_the_oldest() -> None: cx = HG51B(lambda address: 0) for pc in range(10): cx.pb, cx.pc = 1, pc cx._push() assert cx.stack == [1 << 8 | pc for pc in range(9, 1, -1)] for _ in range(8): cx._pull() assert (cx.pb, cx.pc) == (1, 2) cx._pull() # from empty: 0 assert (cx.pb, cx.pc) == (0, 0) def test_far_jump_loads_pb_from_p() -> None: rig = Rig() rig.place([0x7C03, 0x0A10], page=0) # LD PL,#3; JMP P:$10 rig.place([0x6455, 0xFC00], page=3, pc=0x10) # page 3: LD A,#$55; HALT rig.start() rig.finish() assert rig.cx.a == 0x55 and rig.cx.pb == 3 def test_skip() -> None: for word, flag, state, skipped in ( (0x2501, "c", True, True), # SKIP C=1 with C set (0x2501, "c", False, False), (0x2600, "z", False, True), # SKIP Z=0 with Z clear (0x2701, "n", True, True), (0x2401, "v", True, True), ): rig = Rig() setattr(rig.cx, flag, state) rig.execute([0x6401, word, 0x6402]) # LD A,#1; SKIP; LD A,#2 assert rig.cx.a == (1 if skipped else 2), (hex(word), state) def test_skip_taken_costs_one_more() -> None: rig = Rig() rig.cx.c = True rig.place([0x2501, 0x0000, 0xFC00]) rig.start() start = rig.cx.clock rig.finish() assert rig.cx.instructions == 2 and rig.cx.clock - start == 2560 + 2 + 1 def test_halt_raises_irq_when_enabled() -> None: rig = Rig() rig.cx.write(0x7F51, 0) # IRQ enabled rig.execute([]) assert rig.cx.irq and rig.cx.status & 0x02 rig.cx.write(0x7F5E, 0) # clears the flag, not the line (U13) assert rig.cx.irq and not rig.cx.status & 0x02 rig.cx.write(0x7F51, 1) # disabling drops the line assert not rig.cx.irq def test_halt_quiet_when_disabled() -> None: rig = Rig() rig.execute([]) assert not rig.cx.irq and rig.cx.status == 0