# SPDX-License-Identifier: MIT """Reset, IRQ, NMI, ABORT, WAI and STP: what SingleStepTests has no cases of. Manufacturer tier: DS Tables 5-2 and 5-3 (vectors), Table 5-7 row 22a (the entry's cycles), notes 7 and 10, section 2.25 (reset), U8, U11, U12. The poll (end of the second-to-last cycle) is 6502-python's frame, tested there. """ from w65c816 import W65C816 PROGRAM = 0x8000 class Board: """A flat 16 MiB bus that counts callbacks and records kinds.""" def __init__(self, program: bytes = b"\xea" * 16) -> None: self.memory = bytearray(0x1000000) self.memory[PROGRAM : PROGRAM + len(program)] = program for vector, target in { 0xFFFC: 0x8000, 0xFFEE: 0x9000, 0xFFEA: 0x9100, 0xFFE8: 0x9200, 0xFFFE: 0xA000, 0xFFFA: 0xA100, 0xFFF8: 0xA200, }.items(): # fmt: skip self.memory[vector] = target & 0xFF self.memory[vector + 1] = target >> 8 self.callbacks = 0 self.log: list[tuple[str, int, bool]] = [] self.cpu = W65C816(self.read, self.write, self.idle) def read(self, address: int) -> int: self.callbacks += 1 self.log.append(("r", address, self.cpu.vpb)) return self.memory[address] def write(self, address: int, value: int) -> None: self.callbacks += 1 self.log.append(("w", address, self.cpu.vpb)) self.memory[address] = value def idle(self, address: int) -> None: self.callbacks += 1 self.log.append(("i", address, False)) def step(self) -> int: before = self.callbacks cycles = self.cpu.step() assert cycles == self.callbacks - before, "a step's cycles are its callbacks" return cycles def booted(program: bytes = b"\xea" * 16, native: bool = False, settle: bool = True) -> Board: """Reset, then (settle) run the first instruction: reset does not poll, so nothing requested before that instruction is taken at its boundary.""" board = Board(program) board.cpu.request_reset() board.step() if native: board.cpu.e = 0 if settle: board.step() board.log.clear() return board def test_nothing_is_taken_before_the_first_instruction_after_reset() -> None: board = booted(settle=False) board.cpu.request_non_maskable_interrupt() board.cpu.request_abort() assert board.step() == 2 # the NOP at the reset vector runs first board.step() assert board.cpu.pc == 0xA200 # then ABORT (emulation vector) def test_reset_enters_emulation_mode_from_the_vector_in_seven_cycles() -> None: board = Board() cpu = board.cpu cpu.e, cpu.p, cpu.d, cpu.dbr, cpu.pbr, cpu.x = 0, 0x08, 0x1234, 0x7E, 0x12, 0xABCD cpu.request_reset() assert board.step() == 7 assert (cpu.e, cpu.pc, cpu.pbr, cpu.dbr, cpu.d) == (1, 0x8000, 0, 0, 0) assert cpu.p & 0x34 == 0x34 and not cpu.p & 0x08 # M X I set, D clear assert cpu.x == 0xCD and cpu.s >> 8 == 0x01 assert [kind for kind, _, _ in board.log].count("w") == 0 # note 10: no writes def test_native_irq_pushes_pbr_pc_p_and_takes_the_native_vector() -> None: board = booted(native=True) cpu = board.cpu cpu.p &= ~0x04 cpu.p |= 0x08 cpu.pbr, cpu.pc, cpu.s = 0x12, 0x3456, 0x01FF cpu.request_maskable_interrupt() assert board.step() == 8 assert (cpu.pbr, cpu.pc) == (0, 0x9000) assert cpu.p & 0x04 and not cpu.p & 0x08 # I set, D cleared (U8) stack = board.memory assert (stack[0x01FF], stack[0x01FE], stack[0x01FD]) == (0x12, 0x34, 0x56) assert [vpb for _, _, vpb in board.log[-2:]] == [True, True] def test_emulation_irq_pushes_three_bytes_with_b_clear() -> None: board = booted() cpu = board.cpu cpu.p &= ~0x04 cpu.s = 0x01FF cpu.request_maskable_interrupt() assert board.step() == 7 # note 7: no PBR push assert cpu.pc == 0xA000 assert board.memory[0x01FD] & 0x10 == 0 # B clear for IRQ (U8) def test_nmi_is_taken_whatever_i_says_and_wins_over_irq() -> None: board = booted(native=True) board.cpu.request_maskable_interrupt() board.cpu.request_non_maskable_interrupt() board.step() assert board.cpu.pc == 0x9100 def test_abort_comes_first() -> None: board = booted(native=True) board.cpu.request_non_maskable_interrupt() board.cpu.request_abort() board.step() assert board.cpu.pc == 0x9200 def test_wai_with_i_set_resumes_after_wai_without_a_vector() -> None: board = booted(b"\xcb\xea\xea", settle=False) # WAI; NOP cpu = board.cpu assert board.step() == 3 assert cpu.waiting and cpu.halted assert board.step() == 1 # waiting: one IO cycle cpu.request_maskable_interrupt() # I is set after reset board.log.clear() assert board.step() == 1 # the wake-up cycle (U11) assert board.log == [("r", 0x8001, False)] # a program read at PC, byte dropped assert not cpu.waiting board.step() assert cpu.pc == 0x8002 # ran the NOP after WAI, took no vector def test_wai_with_i_clear_takes_the_interrupt() -> None: board = booted(b"\x58\xcb\xea", settle=False) # CLI; WAI cpu = board.cpu board.step() board.step() cpu.request_maskable_interrupt() board.step() # wakes board.step() # the boundary takes the IRQ assert cpu.pc == 0xA000 def test_stp_stops_until_reset() -> None: board = booted(b"\xdb", settle=False) cpu = board.cpu board.step() cpu.request_non_maskable_interrupt() for _ in range(3): assert board.step() == 1 assert cpu.stopped cpu.request_reset() board.step() assert not cpu.stopped and cpu.pc == 0x8000