psx_io/lib.rs
1// SPDX-License-Identifier: GPL-2.0-or-later
2//! Volatile MMIO primitives.
3//!
4//! The lowest layer of the SDK. Wraps `core::ptr::read_volatile` /
5//! `write_volatile` in zero-overhead helpers that each peripheral
6//! module builds on. Keeping these centralised means we can audit
7//! every MMIO touchpoint in one file.
8//!
9//! All functions here are `#[inline(always)]` and `unsafe` -- they
10//! accept arbitrary addresses and widths. Higher-level SDK crates
11//! present safe APIs on top of these.
12
13#![no_std]
14#![cfg_attr(target_arch = "mips", feature(asm_experimental_arch))]
15#![deny(unsafe_op_in_unsafe_fn)]
16#![warn(missing_docs)]
17
18pub mod cd;
19pub mod cdda;
20pub mod cdrom;
21pub mod controller_port;
22pub mod diag;
23pub mod disc_base;
24pub mod dma;
25pub mod gpu;
26pub mod irq;
27pub mod periph;
28pub mod timers;
29
30/// Read a 32-bit word from `addr`. Caller must guarantee `addr` is
31/// valid MMIO for a 32-bit read.
32///
33/// # Safety
34/// Targets unchecked memory-mapped I/O. The caller is responsible for
35/// the address mapping and any side effects.
36#[inline(always)]
37pub unsafe fn read_u32(addr: u32) -> u32 {
38 // SAFETY: the caller guarantees `addr` is mapped and valid for an aligned 32-bit volatile read
39 // (this fn's `# Safety`).
40 unsafe { core::ptr::read_volatile(addr as *const u32) }
41}
42
43/// Read a 16-bit half-word from `addr`.
44///
45/// # Safety
46/// See [`read_u32`].
47#[inline(always)]
48pub unsafe fn read_u16(addr: u32) -> u16 {
49 // SAFETY: the caller guarantees `addr` is mapped and valid for an aligned 16-bit volatile read
50 // (this fn's `# Safety`).
51 unsafe { core::ptr::read_volatile(addr as *const u16) }
52}
53
54/// Read an 8-bit byte from `addr`.
55///
56/// # Safety
57/// See [`read_u32`].
58#[inline(always)]
59pub unsafe fn read_u8(addr: u32) -> u8 {
60 // SAFETY: the caller guarantees `addr` is mapped and valid for an 8-bit volatile read (this
61 // fn's `# Safety`).
62 unsafe { core::ptr::read_volatile(addr as *const u8) }
63}
64
65/// Write a 32-bit word to `addr`.
66///
67/// # Safety
68/// See [`read_u32`].
69#[inline(always)]
70pub unsafe fn write_u32(addr: u32, value: u32) {
71 // SAFETY: the caller guarantees `addr` is mapped and valid for an aligned 32-bit volatile write
72 // (this fn's `# Safety`).
73 unsafe { core::ptr::write_volatile(addr as *mut u32, value) }
74}
75
76/// Write a 16-bit half-word to `addr`.
77///
78/// # Safety
79/// See [`read_u32`].
80#[inline(always)]
81pub unsafe fn write_u16(addr: u32, value: u16) {
82 // SAFETY: the caller guarantees `addr` is mapped and valid for an aligned 16-bit volatile write
83 // (this fn's `# Safety`).
84 unsafe { core::ptr::write_volatile(addr as *mut u16, value) }
85}
86
87/// Write an 8-bit byte to `addr`.
88///
89/// # Safety
90/// See [`read_u32`].
91#[inline(always)]
92pub unsafe fn write_u8(addr: u32, value: u8) {
93 // SAFETY: the caller guarantees `addr` is mapped and valid for an 8-bit volatile write (this
94 // fn's `# Safety`).
95 unsafe { core::ptr::write_volatile(addr as *mut u8, value) }
96}
97
98/// Renamed to [`read_u8`].
99///
100/// # Safety
101/// See [`read_u8`].
102#[deprecated(note = "renamed to `read_u8`")]
103#[inline(always)]
104pub unsafe fn read8(addr: u32) -> u8 {
105 // SAFETY: same contract as the renamed function.
106 unsafe { read_u8(addr) }
107}
108
109/// Renamed to [`write_u8`].
110///
111/// # Safety
112/// See [`write_u8`].
113#[deprecated(note = "renamed to `write_u8`")]
114#[inline(always)]
115pub unsafe fn write8(addr: u32, value: u8) {
116 // SAFETY: same contract as the renamed function.
117 unsafe { write_u8(addr, value) }
118}