// ---- from src/control.rs lines 38-74,470-535 /// 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`. In every mode an interrupt with no source left is /// never entered through IRQ2 by default (issue 0002). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum VanishedInterrupt { /// The 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. Mesen2, /// 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). Latched, /// Dropped unless a source is still asserted at entry. Drop, } /// U25 (issue 0003): which cycle of an instruction the interrupt lines are /// sampled at. The I flag is the sampled one in every mode (U21). Emulator /// tier; no source above it says. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum IrqPollCycle { /// The 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. SecondToLast, /// The levels at the boundary all count, a line that rises (or drops) /// in the last cycle included. Last, /// Mednafen's rule as the reference states it: IRQ1 and IRQ2 are taken /// from a sample taken at the start of the last cycle (the default's /// point), but the timer is tested against the live line at the /// boundary, so TIQ alone behaves as `Last`. Mednafen, } /// The frame's boundary decision, after working out which lines the poll saw. pub(crate) fn boundary_interrupt_sequence(&mut self) -> u8 { let pins = &mut self.pins; // 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_sequence), else the lines as they stand. let polled_lines = pins.polled_lines.take(); let lines = pins.lines; let seen = match self.irq_poll_cycle { IrqPollCycle::SecondToLast => None, IrqPollCycle::Last => Some(lines), IrqPollCycle::Mednafen => { let seen = polled_lines.unwrap_or(lines); Some((seen & !TIQ_BIT) | (lines & TIQ_BIT)) } }; match seen { None => pins.poll_lines = polled_lines.unwrap_or(lines), Some(seen) => { pins.poll_lines = seen; let polled = pins.frame.polled.unwrap_or(pins.frame.requests); if polled & NO_POLL == 0 { // The frame kept the requests from before the last cycle; // the mode counts its own lines instead. let asserted = if seen & !pins.irq_disable & 7 != 0 { IRQ_LINE } else { 0 }; pins.frame.polled = Some((polled & !IRQ_LINE) | asserted); } } } let p = self.p; self.pins.frame.boundary_interrupt(p) } /// The vector of the highest-priority source the entry takes (HW HB-13), /// or `None` to drop. fn maskable_vector(&self) -> Option { let pins = &self.pins; let enabled = !pins.irq_disable & 7; let polled = pins.poll_lines & enabled; let live = pins.lines & enabled; let pending = match self.vanished_interrupt { VanishedInterrupt::Mesen2 => (polled & !TIQ_BIT) | (polled & live & TIQ_BIT), VanishedInterrupt::Drop => { if live != 0 { polled } else { 0 } } VanishedInterrupt::Latched => polled, }; if pending & TIQ_BIT != 0 { return Some(VECTOR_TIQ); } if pending & IRQ1_BIT != 0 { return Some(VECTOR_IRQ1); } if pending & IRQ2_BIT != 0 { return Some(VECTOR_IRQ2); } None }