pcengine_rust/cpu_io/
irq_ctl.rs

1//! The interrupt controller ($1400-$17FF, mirrored every 4 bytes) (`cpu_io/irq_ctl.py`).
2//!
3//! [hw: pcetech 2.1] `$1402`: bits 2-0 are disable bits, 1 = disabled (bit 2 timer, bit 1 IRQ1, bit 0
4//! IRQ2), readable and writable, bits 7-3 return the I/O buffer. `$1403`: reads the *line* state
5//! in bits 2-0 even when masked, upper five bits the buffer; any write acknowledges the timer.
6//! `$1400`/`$1401`: writes do nothing, reads return the buffer. The mask only stops the CPU from
7//! taking the interrupt; the core gets the raw lines and the mask (`irq_disable`) and decides.
8//!
9//! Differences forced by Rust: the reference's two callables (acknowledge the timer, tell the
10//! CPU the mask changed) are `write`'s return value and the `mask_changed` flag.
11
12use serde::{Deserialize, Serialize};
13
14pub const TIMER: u8 = 0b100;
15pub const IRQ1: u8 = 0b010;
16pub const IRQ2: u8 = 0b001;
17
18#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
19pub struct IrqController {
20    /// [hw: pcetech 2] power-up: the disable mask is zero
21    pub disable: u8,
22    pub irq1: bool,
23    pub irq2: bool,
24    /// `$1402` was written since the board last looked (the reference's `on_mask_change`).
25    #[serde(skip)]
26    pub mask_changed: bool,
27}
28
29impl IrqController {
30    pub fn new() -> IrqController {
31        IrqController::default()
32    }
33
34    pub fn reset(&mut self) {
35        self.disable = 0;
36        self.irq1 = false;
37        self.irq2 = false;
38    }
39
40    pub fn status(&self, timer_irq: bool) -> u8 {
41        (if timer_irq { TIMER } else { 0 })
42            | (if self.irq1 { IRQ1 } else { 0 })
43            | (if self.irq2 { IRQ2 } else { 0 })
44    }
45
46    pub fn read(&self, offset: u32, buffer: u8, timer_irq: bool) -> u8 {
47        if offset & 2 == 0 {
48            return buffer;
49        }
50        let low = if offset & 1 == 0 {
51            self.disable
52        } else {
53            self.status(timer_irq)
54        };
55        low | (buffer & 0xF8)
56    }
57
58    /// True when the write acknowledges the timer (any write to `$1403`).
59    pub fn write(&mut self, offset: u32, value: u8) -> bool {
60        if offset & 3 == 2 {
61            self.disable = value & 7;
62            self.mask_changed = true;
63        } else if offset & 3 == 3 {
64            return true;
65        }
66        false
67    }
68}