psx 0.1.8

Library for developing homebrew for the Sony PlayStation 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Direct memory access (DMA) control registers

use crate::dma::{BlockMode, Chop, Direction, Error, Step, TransferMode};
use crate::hw::{MemRegister, Register};

type Result<T> = core::result::Result<T, Error>;

const STEP: u32 = 1;
const CHOP: u32 = 8;
const TRANFER_MODE: u32 = 9;
const DMA_WIN: u32 = 16;
const CPU_WIN: u32 = 20;
const BUSY: u32 = 24;
const TRIGGER: u32 = 28;

/// Only the lower 24 bits are stored in the address registers.
const ADDRESS_MASK: u32 = 0x00FF_FFFF;

/// The name of a DMA channel
#[derive(Debug)]
pub enum Name {
    /// RAM to Macroblock decoder DMA channel
    MDECIn = 0,
    /// Macroblock decoder to RAM DMA channel
    MDECOut,
    /// GPU DMA channel
    GPU,
    /// CD-ROM DMA channel
    CDROM,
    /// SPU DMA channel
    SPU,
    /// Expansion port DMA channel
    PIO,
    /// Ordering table clear DMA channel
    OTC,
}

/// RAM to Macroblock decode DMA channel.
pub mod mdec_in {
    use super::{BlockControl, ChannelControl, MemoryAddress};
    use crate::hw::dma::Name;
    use crate::hw::MemRegister;

    /// DMA channel address register.
    pub type Address = MemRegister<u32, 0x1F80_1080>;
    impl MemoryAddress for Address {}

    /// DMA channel block register.
    pub type Block = MemRegister<u32, 0x1F80_1084>;
    impl BlockControl for Block {}

    /// DMA channel control register.
    pub type Control = MemRegister<u32, 0x1F80_1088>;
    impl ChannelControl for Control {
        const NAME: Name = Name::MDECIn;
    }
}

/// Macroblock decoder to RAM DMA channel
pub mod mdec_out {
    use super::{BlockControl, ChannelControl, MemoryAddress};
    use crate::hw::dma::Name;
    use crate::hw::MemRegister;

    /// DMA channel address register.
    pub type Address = MemRegister<u32, 0x1F80_1090>;
    impl MemoryAddress for Address {}

    /// DMA channel block register.
    pub type Block = MemRegister<u32, 0x1F80_1094>;
    impl BlockControl for Block {}

    /// DMA channel control register.
    pub type Control = MemRegister<u32, 0x1F80_1098>;
    impl ChannelControl for Control {
        const NAME: Name = Name::MDECOut;
    }
}

/// GPU DMA channel
pub mod gpu {
    use super::{BlockControl, ChannelControl, MemoryAddress};
    use crate::hw::dma::Name;
    use crate::hw::MemRegister;

    /// DMA channel address register.
    pub type Address = MemRegister<u32, 0x1F80_10A0>;
    impl MemoryAddress for Address {}

    /// DMA channel block register.
    pub type Block = MemRegister<u32, 0x1F80_10A4>;
    impl BlockControl for Block {}

    /// DMA channel control register.
    pub type Control = MemRegister<u32, 0x1F80_10A8>;
    impl ChannelControl for Control {
        const NAME: Name = Name::GPU;
    }
}

/// CD-ROM DMA channel
pub mod cdrom {
    use super::{BlockControl, ChannelControl, MemoryAddress};
    use crate::hw::dma::Name;
    use crate::hw::MemRegister;

    /// DMA channel address register.
    pub type Address = MemRegister<u32, 0x1F80_10B0>;
    impl MemoryAddress for Address {}

    /// DMA channel block register.
    pub type Block = MemRegister<u32, 0x1F80_10B4>;
    impl BlockControl for Block {}

    /// DMA channel control register.
    pub type Control = MemRegister<u32, 0x1F80_10B8>;
    impl ChannelControl for Control {
        const NAME: Name = Name::CDROM;
    }
}

/// SPU DMA channel
pub mod spu {
    use super::{BlockControl, ChannelControl, MemoryAddress};
    use crate::hw::dma::Name;
    use crate::hw::MemRegister;

    /// DMA channel address register.
    pub type Address = MemRegister<u32, 0x1F80_10C0>;
    impl MemoryAddress for Address {}

    /// DMA channel block register.
    pub type Block = MemRegister<u32, 0x1F80_10C4>;
    impl BlockControl for Block {}

    /// DMA channel control register.
    pub type Control = MemRegister<u32, 0x1F80_10C8>;
    impl ChannelControl for Control {
        const NAME: Name = Name::SPU;
    }
}

/// Expansion port DMA channel
pub mod pio {
    use super::{BlockControl, ChannelControl, MemoryAddress};
    use crate::hw::dma::Name;
    use crate::hw::MemRegister;

    /// DMA channel address register.
    pub type Address = MemRegister<u32, 0x1F80_10D0>;
    impl MemoryAddress for Address {}

    /// DMA channel block register.
    pub type Block = MemRegister<u32, 0x1F80_10D4>;
    impl BlockControl for Block {}

    /// DMA channel control register.
    pub type Control = MemRegister<u32, 0x1F80_10D8>;
    impl ChannelControl for Control {
        const NAME: Name = Name::PIO;
    }
}

/// Ordering table clear DMA channel
pub mod otc {
    use super::{BlockControl, ChannelControl, MemoryAddress};
    use crate::hw::dma::Name;
    use crate::hw::MemRegister;

    /// DMA channel address register.
    pub type Address = MemRegister<u32, 0x1F80_10E0>;
    impl MemoryAddress for Address {}

    /// DMA channel block register.
    pub type Block = MemRegister<u32, 0x1F80_10E4>;
    impl BlockControl for Block {}

    /// DMA channel control register.
    pub type Control = MemRegister<u32, 0x1F80_10E8>;
    impl ChannelControl for Control {
        const NAME: Name = Name::OTC;
    }
}

/// DMA Control register
pub type GlobalControl = MemRegister<u32, 0x1F80_10F0>;

/// DMA Interrupt register
pub type Interrupt = MemRegister<u32, 0x1F80_10F4>;

/// An address register for a DMA channel.
pub trait MemoryAddress: Register<u32> {
    /// Get a pointer to the channel's start address.
    fn get_address(&self) -> *const u32 {
        (self.as_ref() & ADDRESS_MASK) as *const u32
    }

    /// Sets the channel's start address.
    fn set_address(&mut self, ptr: &u32) -> &mut Self {
        let val = (ptr as *const u32 as u32) & ADDRESS_MASK;
        self.assign(val)
    }
}

/// A block size control register for a DMA channel.
pub trait BlockControl: Register<u32> {
    /// Gets the block mode assuming the DMA channel is in the specified
    /// transfer mode.
    fn get_block(&self, transfer_mode: TransferMode) -> Option<BlockMode> {
        match transfer_mode {
            TransferMode::Immediate => match self.as_ref() {
                0 => Some(BlockMode::Single(0x1_0000u32)),
                1..=0xFFFF => Some(BlockMode::Single(*self.as_ref())),
                _ => None,
            },
            TransferMode::Request => Some(BlockMode::Multi {
                words: *self.as_ref() as u16,
                blocks: (self.as_ref() >> 16) as u16,
            }),
            TransferMode::LinkedList => Some(BlockMode::LinkedList),
        }
    }

    /// Sets the block mode for the DMA channel, returning `None` if the block
    /// size is too large.
    fn set_block<B>(&mut self, block_mode: B) -> Result<&mut Self>
    where BlockMode: From<B> {
        match block_mode.into() {
            BlockMode::Single(words) => {
                let words = match words {
                    0..=0xFFFF => words,
                    0x1_0000 => 0,
                    _ => return Err(Error::OversizedBlock),
                };
                Ok(self.assign(words))
            },
            BlockMode::Multi { words, blocks } => {
                let value = words as u32 | ((blocks as u32) << 16);
                Ok(self.assign(value))
            },
            BlockMode::LinkedList => Ok(self),
        }
    }
}

/// A channel control register for a DMA channel.
pub trait ChannelControl: Register<u32> {
    /// The name of the DMA channel.
    const NAME: Name;

    /// Wait until the DMA channel is not busy.
    fn wait(&mut self) -> &mut Self {
        self.load();
        while self.busy() {
            self.load();
        }
        self
    }

    /// Checks if the DMA channel is busy.
    fn busy(&self) -> bool {
        self.all_set(1 << BUSY)
    }

    /// Gets the DMA channel transfer mode, returning `None` for invalid values.
    fn get_mode(&self) -> Option<TransferMode> {
        match (self.as_ref() >> TRANFER_MODE) & 0b11 {
            0 => Some(TransferMode::Immediate),
            1 => Some(TransferMode::Request),
            2 => Some(TransferMode::LinkedList),
            _ => None,
        }
    }

    /// Checks the direction of the DMA channel.
    fn get_direction(&self) -> Direction {
        if self.all_set(1) {
            Direction::FromMemory
        } else {
            Direction::ToMemory
        }
    }

    /// Checks the memory address step of the DMA channel.
    fn get_step(&self) -> Step {
        if self.all_set(1 << STEP) {
            Step::Backward
        } else {
            Step::Forward
        }
    }

    /// Checks the channel's CPU/DMA window sizes, returning `None` if chopping
    /// is disabled or if the value is invalid.
    fn get_chop(&self) -> Option<Chop> {
        if self.all_set(1 << CHOP) {
            let cpu_window = self.as_ref() >> CPU_WIN;
            let dma_window = self.as_ref() >> DMA_WIN;
            if cpu_window & 0b111 != 0 {
                return None
            }
            if dma_window & 0b111 != 0 {
                return None
            }
            Some(Chop {
                cpu_window,
                dma_window,
            })
        } else {
            None
        }
    }

    /// Sets the direction of the DMA channel.
    fn set_direction(&mut self, direction: Direction) -> &mut Self {
        self.clear_bits(1).set_bits(direction as u32)
    }

    /// Sets the memory address step of the DMA channel.
    fn set_step(&mut self, step: Step) -> &mut Self {
        self.clear_bits(1 << STEP).set_bits((step as u32) << STEP)
    }

    /// Enables chopping and sets the channel's CPU/DMA window sizes. Chopping
    /// is disabled if `chop` is `None`. Returns `None` if the chopping windows
    /// are invalid.
    fn set_chop(&mut self, chop: Option<Chop>) -> Option<&mut Self> {
        match chop {
            Some(chop) => {
                if chop.cpu_window & !0b111 != 0 {
                    return None
                }
                if chop.dma_window & !0b111 != 0 {
                    return None
                }
                Some(
                    self.clear_bits(0b111 << CPU_WIN | 0b111 << DMA_WIN)
                        .set_bits(
                            1 << CHOP | chop.cpu_window << CPU_WIN | chop.dma_window << DMA_WIN,
                        ),
                )
            },
            None => Some(self.clear_bits(1 << CHOP)),
        }
    }

    /// Sets the DMA channel's transfer mode.
    fn set_mode(&mut self, mode: TransferMode) -> &mut Self {
        self.clear_bits(0b11 << TRANFER_MODE)
            .set_bits((mode as u32) << TRANFER_MODE)
    }

    /// Starts a DMA transfer.
    fn start(&mut self) -> &mut Self {
        if let Some(TransferMode::Immediate) = self.get_mode() {
            self.set_bits(1 << TRIGGER);
        }
        self.set_bits(1 << BUSY)
    }

    /// Stops an ongoing DMA transfer.
    fn stop(&mut self) -> &mut Self {
        self.clear_bits(1 << BUSY)
    }
}

impl GlobalControl {
    const fn enable_bit(channel: Name) -> u32 {
        let bit = (channel as u32 * 4) + 3;
        1 << bit
    }

    const fn all_channels() -> u32 {
        Self::enable_bit(Name::MDECIn) |
            Self::enable_bit(Name::MDECOut) |
            Self::enable_bit(Name::GPU) |
            Self::enable_bit(Name::CDROM) |
            Self::enable_bit(Name::SPU) |
            Self::enable_bit(Name::PIO) |
            Self::enable_bit(Name::OTC)
    }

    /// Checks if the DMA `channel` is enabled.
    pub fn enabled(&self, channel: Name) -> bool {
        self.all_set(Self::enable_bit(channel))
    }

    /// Enables the DMA `channel`.
    pub fn enable(&mut self, channel: Name) -> &mut Self {
        self.set_bits(Self::enable_bit(channel))
    }

    /// Disables the DMA `channel`.
    pub fn disable(&mut self, channel: Name) -> &mut Self {
        self.clear_bits(Self::enable_bit(channel))
    }

    /// Enables all DMA channels.
    pub fn enable_all(&mut self) -> &mut Self {
        self.set_bits(Self::all_channels())
    }

    /// Disables all DMA channels.
    pub fn disable_all(&mut self) -> &mut Self {
        self.clear_bits(Self::all_channels())
    }
}