#: C18: what an interrupt does when its source has gone between the poll (the end of the #: instruction's second-to-last cycle) and the entry, as when a handler runs ``CLI`` and then #: acknowledges the timer in the last cycle of ``STZ $1403``. ``"mesen2"`` (default, #: emulator-derived, Mesen2's documented rule): the timer is re-checked at entry and dropped if #: it has gone, IRQ1 and IRQ2 are taken through their own vector as the poll saw them. #: ``"latched"``: every source is taken as the poll saw it (what srg320's HDL does, read from its #: source: the ``*_INT`` registers are latched in the last cycle). ``"drop"``: dropped unless a #: source is still asserted at entry. In every mode an interrupt with no source left is never #: entered through IRQ2 by default (issue 0002). vanished_interrupt = "mesen2" #: U25 (issue 0003): which cycle of an instruction the interrupt lines are sampled at. #: ``"second_to_last"`` (default, the 6502 frame's rule, U21): the end of the second-to-last #: cycle, so a line that rises in the last cycle is seen one instruction later. ``"last"``: the #: levels at the boundary all count, a line that rises (or drops) in the last cycle included. #: ``"mednafen"``: Mednafen's rule, read from ``huc6280.cpp`` (``RunSub``): IRQ1 and IRQ2 are #: taken from ``IRQSample``, taken at the start of the last cycle (the default's point), but #: the timer is tested against the live ``IRQlow`` at the boundary #: (``IRQlow & IQTIMER & IRQMask``), so TIQ alone behaves as ``"last"``. The I flag is the #: sampled one in every mode (U21). Emulator tier; no source above it says. irq_poll_cycle = "second_to_last" def _boundary_interrupt(self) -> int: # Which lines the poll saw: those at the last instruction's last-but-one access when a # line changed after it (_polled_lines, set by _execute), else the lines as they stand. polled_lines = self._polled_lines self._polled_lines = None lines = self._lines mode = self.irq_poll_cycle if mode == "second_to_last": self._poll_lines = lines if polled_lines is None else polled_lines return super()._boundary_interrupt() if mode == "last": seen = lines elif mode == "mednafen": seen = lines if polled_lines is None else polled_lines seen = (seen & ~TIQ_BIT) | (lines & TIQ_BIT) else: raise ValueError(f"unknown irq_poll_cycle {mode!r}") self._poll_lines = seen polled = self._requests if self._polled is None else self._polled if not polled & NO_POLL: # The frame kept the requests from before the last cycle; the mode counts its own # lines instead. asserted = IRQ_LINE if seen & ~self._irq_disable & 7 else 0 self._polled = (polled & ~IRQ_LINE) | asserted return super()._boundary_interrupt() def _maskable_vector(self) -> int | None: """The vector of the highest-priority source the entry takes (HW HB-13), or None to drop.""" enabled = ~self._irq_disable & 7 polled = self._poll_lines & enabled live = self._lines & enabled mode = self.vanished_interrupt if mode == "mesen2": pending = (polled & ~TIQ_BIT) | (polled & live & TIQ_BIT) elif mode == "drop": pending = polled if live else 0 else: pending = polled if pending & TIQ_BIT: return VECTOR_TIQ if pending & IRQ1_BIT: return VECTOR_IRQ1 if pending & IRQ2_BIT: return VECTOR_IRQ2 return None