Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

psx_rt/
peripherals.rs

1//! The one-per-program set of peripheral ownership tokens.
2
3use crate::critical_section::{self, Mutex};
4use core::cell::Cell;
5use psx_io::periph::{Cd, ControllerPort, GpuDma, MdecDma, OrderingTableClearDma, SpuDma};
6
7/// Every shared-state peripheral token, handed out once.
8///
9/// ```ignore
10/// let p = psx_rt::Peripherals::take().unwrap();
11/// let mut gpu = psx_gpu::Gpu::new(p.gpu_dma, display);
12/// let mut port = p.controller_port; // pads and memory cards borrow it
13/// let mut spu = psx_spu::Spu::new(p.spu_dma);
14/// let cd = p.cd; // or `SectorReader::with_cd(cd)`, `xa::Player::new(cd)`
15/// ```
16///
17/// Each token is owned by the driver that programs its device; see
18/// `psx_io::periph` for who that is.
19///
20/// The tokens are zero-sized, so `Peripherals` is too; moving it or its
21/// fields around costs nothing.
22#[derive(Debug)]
23#[non_exhaustive]
24pub struct Peripherals {
25    /// DMA channel 2 and the GP0 stream it feeds.
26    pub gpu_dma: GpuDma,
27    /// DMA channel 6, the ordering-table clear.
28    pub ordering_table_clear_dma: OrderingTableClearDma,
29    /// DMA channels 0 and 1 (MDEC in and out).
30    pub mdec_dma: MdecDma,
31    /// DMA channel 4 and the SPU transfer port.
32    pub spu_dma: SpuDma,
33    /// The CD-ROM controller and DMA channel 3.
34    pub cd: Cd,
35    /// The pad and memory-card port (SIO0).
36    pub controller_port: ControllerPort,
37}
38
39static TAKEN: Mutex<Cell<bool>> = Mutex::new(Cell::new(false));
40
41impl Peripherals {
42    /// The token set, the first time this is called; `None` after that.
43    ///
44    /// One flag test inside a short critical section.
45    pub fn take() -> Option<Self> {
46        critical_section::with(|cs| {
47            let taken = TAKEN.borrow(cs);
48            if taken.get() {
49                return None;
50            }
51            taken.set(true);
52            // SAFETY: the flag guarantees this is the only set handed out by
53            // `take`; `steal` callers carry their own contract.
54            Some(unsafe { Self::steal() })
55        })
56    }
57
58    /// Borrow every DMA owner token at once, for [`crate::cache::with_dma_quiesced`].
59    ///
60    /// For a program that kept its `Peripherals` whole. A program that handed the tokens to
61    /// drivers lends them back through the drivers (`Gpu::dma_mut`, `SectorReader::cd_mut`, ...)
62    /// into [`crate::cache::DmaOwners::new`].
63    pub fn dma_owners(&mut self) -> crate::cache::DmaOwners<'_> {
64        crate::cache::DmaOwners::new(
65            &mut self.gpu_dma,
66            &mut self.ordering_table_clear_dma,
67            &mut self.mdec_dma,
68            &mut self.spu_dma,
69            &mut self.cd,
70        )
71    }
72
73    /// The token set, whether or not [`take`](Self::take) already ran.
74    ///
75    /// # Safety
76    ///
77    /// The caller must not use a token from this set while the same token
78    /// from another set is in use (see `psx_io::periph`).
79    pub unsafe fn steal() -> Self {
80        // SAFETY: forwarded contract.
81        unsafe {
82            Self {
83                gpu_dma: GpuDma::steal(),
84                ordering_table_clear_dma: OrderingTableClearDma::steal(),
85                mdec_dma: MdecDma::steal(),
86                spu_dma: SpuDma::steal(),
87                cd: Cd::steal(),
88                controller_port: ControllerPort::steal(),
89            }
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn take_hands_the_set_out_once() {
100        assert_eq!(core::mem::size_of::<Peripherals>(), 0);
101        let first = Peripherals::take();
102        assert!(first.is_some());
103        assert!(Peripherals::take().is_none());
104    }
105}