pcenginecd_rust/
arcade.rs

1//! The Arcade Card: 2 MiB of RAM behind four address ports, and a 32-bit shift/rotate unit
2//! (`arcade.py`).
3//!
4//! An Arcade Card (Hudson, 1994) sits on the same EXT BUS as the CD unit; here it is a component
5//! of `CdUnit`, which claims its pages (one add-on: CD-ROM² + Arcade Card). It has no clock:
6//! every access is immediate, so it takes no part in `catch_up`.
7//!
8//! Sources, and what is tier what. **No hardware documentation of the card is in the
9//! reference's `reference/`**: the card is not in MacDonald's `pcetech.txt`, and the only text
10//! the sources cite for it is Mednafen's header ("based on information provided by Ki and David
11//! Shadoff"). So every behaviour below is [emu: Mednafen `arcade_card.cpp`, Geargrafx
12//! `arcade_card_mapper*`, Mesen2 `PceArcadeCard.cpp`] (tier 5, detectors) and holds where **all
13//! three agree**; where they differ the choice is a field of `ArcadeSwitches` and a U/C entry in
14//! the reference's docs/m9-record.md.
15//!
16//! Address map [emu, all three]:
17//!
18//! | where | what |
19//! | --- | --- |
20//! | banks `$40-$43` | the data of port `bank & 3` at any offset, as `$1A00 + 16 * port` |
21//! | `$1A00-$1A7F` | four port groups, `port = (addr >> 4) & 3`: `$1A40-$1A7F` mirrors `$1A00` |
22//! | `$1AE0-$1AE3` | the 32-bit value register, low byte first |
23//! | `$1AE4` | shift: write n (4 bits) shifts the value, read returns n |
24//! | `$1AE5` | rotate: write n (4 bits) rotates the value, read returns n |
25//! | `$1AFE`, `$1AFF` | version `$10` and ID `$51` |
26//!
27//! Port registers (low nibble of the address):
28//!
29//! | reg | meaning |
30//! | --- | --- |
31//! | `$0`, `$1` | data: read or write RAM at the effective address, then auto-increment |
32//! | `$2-$4` | base address, 24 bits, low byte first |
33//! | `$5`, `$6` | offset, 16 bits, low byte then high byte |
34//! | `$7`, `$8` | increment, 16 bits |
35//! | `$9` | control (below) |
36//! | `$A` | trigger write (reads 0) |
37//!
38//! Control bits: D0 auto-increment; D1 add the offset to the base for the access; D2 signed
39//! increment (no emulator uses it); D3 signed offset; D4 the increment goes to the base (else to
40//! the offset); D6-D5 when the offset is written it is added into the base: 0 never, 1 on the
41//! low-byte write, 2 on the high-byte write, 3 on a write to `$A`.
42//!
43//! The effective address is `(base [+ offset [+ $FF0000 if signed]]) & $1FFFFF` (2 MiB).
44//!
45//! Differences forced by Rust: the reference's class attributes are `ArcadeSwitches` (a
46//! subclass there, a value handed to `ArcadeCard::with` here).
47
48use pcengine_rust::sockets::ResetKind;
49use serde::{Deserialize, Serialize};
50
51pub const ARCADE_RAM_BYTES: usize = 2 * 1024 * 1024;
52pub const FIRST_PORT_BANK: usize = 0x40;
53pub const PORT_BANKS: usize = 4;
54/// banks $40-$43 at physical $080000-$087FFF (8 KiB banks)
55pub const PORT_BANK_PAGES: (usize, usize) = (0x0800, 0x087F);
56/// $1A00-$1AFF
57pub const REGISTER_PAGE: usize = 0x1FFA;
58
59pub const CTL_AUTO_INCREMENT: u8 = 0x01;
60pub const CTL_ADD_OFFSET: u8 = 0x02;
61pub const CTL_SIGNED_OFFSET: u8 = 0x08;
62pub const CTL_INCREMENT_BASE: u8 = 0x10;
63pub const CTL_TRIGGER_SHIFT: u8 = 5;
64pub const TRIGGER_NONE: u8 = 0;
65pub const TRIGGER_LOW: u8 = 1;
66pub const TRIGGER_HIGH: u8 = 2;
67pub const TRIGGER_REG_A: u8 = 3;
68
69/// The emulator-only choices (the reference's `ArcadeCard` class attributes, its defaults).
70#[derive(Clone, Copy, Debug, PartialEq, Eq)]
71pub struct ArcadeSwitches {
72    /// [emu: Mednafen, Geargrafx $FF; Mesen2 returns the open bus] a read of a decoded address
73    /// the card has no register at ($1A0B-$1A0F per port group, $1A80-$1ADF, $1AE6-$1AFD,
74    /// $1B00-$1BFF)
75    pub unmapped_read: u8,
76    /// [emu: Mednafen, Geargrafx mask to 4 bits; Mesen2 returns the byte written] $1AE4 /
77    /// $1AE5 reads
78    pub shift_read_mask: u8,
79    /// [emu: Mesen2, Geargrafx read 0; Mednafen falls through to $FF] port register $A on a
80    /// read
81    pub trigger_reg_read: u8,
82    /// [emu: Mednafen zeroes the RAM at power-up; Mesen2 uses its configurable random fill]
83    /// the real RAM is unspecified (U7 again)
84    pub power_on_ram_byte: u8,
85    /// [emu: Geargrafx clears the ports and the shift unit on reset; Mednafen has no reset,
86    /// Mesen2 none visible] a warm reset leaves the registers alone unless this is true
87    pub reset_clears_registers: bool,
88}
89
90impl Default for ArcadeSwitches {
91    fn default() -> Self {
92        ArcadeSwitches {
93            unmapped_read: 0xFF,
94            shift_read_mask: 0x0F,
95            trigger_reg_read: 0x00,
96            power_on_ram_byte: 0x00,
97            reset_clears_registers: false,
98        }
99    }
100}
101
102/// The card's RAM, four ports and the shift unit.
103#[derive(Serialize, Deserialize)]
104pub struct ArcadeCard {
105    #[serde(skip)]
106    pub switches: ArcadeSwitches,
107    pub ram: Vec<u8>,
108    pub base: [u32; 4],
109    pub offset: [u32; 4],
110    pub increment: [u32; 4],
111    pub control: [u8; 4],
112    /// the 32-bit shift register, $1AE0-$1AE3
113    pub value: u32,
114    pub shift: u8,
115    pub rotate: u8,
116}
117
118impl Default for ArcadeCard {
119    fn default() -> Self {
120        ArcadeCard::new()
121    }
122}
123
124impl ArcadeCard {
125    pub fn new() -> ArcadeCard {
126        ArcadeCard::with(ArcadeSwitches::default())
127    }
128
129    pub fn with(switches: ArcadeSwitches) -> ArcadeCard {
130        ArcadeCard {
131            switches,
132            ram: vec![switches.power_on_ram_byte; ARCADE_RAM_BYTES],
133            base: [0; 4],
134            offset: [0; 4],
135            increment: [0; 4],
136            control: [0; 4],
137            value: 0,
138            shift: 0,
139            rotate: 0,
140        }
141    }
142
143    // -- the socket ------------------------------------------------------------------------------
144
145    pub fn claims(&self) -> Vec<(usize, usize)> {
146        vec![PORT_BANK_PAGES, (REGISTER_PAGE, REGISTER_PAGE)]
147    }
148
149    pub fn reset(&mut self, kind: ResetKind) {
150        if kind == ResetKind::PowerOn {
151            self.ram.fill(self.switches.power_on_ram_byte);
152            self.clear_registers();
153        } else if self.switches.reset_clears_registers {
154            self.clear_registers();
155        }
156    }
157
158    fn clear_registers(&mut self) {
159        self.base = [0; 4];
160        self.offset = [0; 4];
161        self.increment = [0; 4];
162        self.control = [0; 4];
163        self.value = 0;
164        self.shift = 0;
165        self.rotate = 0;
166    }
167
168    /// A physical address in the claimed pages: a bank `$40-$43` byte or a `$1Axx` register.
169    pub fn read(&mut self, address: u32) -> u8 {
170        if ((address >> 8) as usize) < REGISTER_PAGE {
171            return self.read_data(((address >> 13) & 3) as usize);
172        }
173        self.read_register((address & 0xFF) as u8)
174    }
175
176    pub fn write(&mut self, address: u32, value: u8) {
177        if ((address >> 8) as usize) < REGISTER_PAGE {
178            self.write_data(((address >> 13) & 3) as usize, value);
179        } else {
180            self.write_register((address & 0xFF) as u8, value);
181        }
182    }
183
184    // -- the ports -------------------------------------------------------------------------------
185
186    pub fn effective_address(&self, port: usize) -> usize {
187        let mut address = self.base[port];
188        let control = self.control[port];
189        if control & CTL_ADD_OFFSET != 0 {
190            address += self.offset[port];
191            if control & CTL_SIGNED_OFFSET != 0 {
192                address += 0xFF0000;
193            }
194        }
195        (address & 0x1FFFFF) as usize
196    }
197
198    fn auto_increment(&mut self, port: usize) {
199        let control = self.control[port];
200        if control & CTL_AUTO_INCREMENT != 0 {
201            if control & CTL_INCREMENT_BASE != 0 {
202                self.base[port] = (self.base[port] + self.increment[port]) & 0xFFFFFF;
203            } else {
204                self.offset[port] = (self.offset[port] + self.increment[port]) & 0xFFFF;
205            }
206        }
207    }
208
209    fn add_offset_to_base(&mut self, port: usize) {
210        let mut total = self.base[port] + self.offset[port];
211        if self.control[port] & CTL_SIGNED_OFFSET != 0 {
212            total += 0xFF0000;
213        }
214        self.base[port] = total & 0xFFFFFF;
215    }
216
217    pub fn read_data(&mut self, port: usize) -> u8 {
218        let value = self.ram[self.effective_address(port)];
219        self.auto_increment(port);
220        value
221    }
222
223    pub fn write_data(&mut self, port: usize, value: u8) {
224        let address = self.effective_address(port);
225        self.ram[address] = value;
226        self.auto_increment(port);
227    }
228
229    fn trigger(&self, port: usize) -> u8 {
230        (self.control[port] >> CTL_TRIGGER_SHIFT) & 3
231    }
232
233    // -- the registers ---------------------------------------------------------------------------
234
235    /// `reg` is the low byte of the address within `$1A00-$1AFF`.
236    pub fn read_register(&mut self, reg: u8) -> u8 {
237        if reg < 0x80 {
238            let port = ((reg >> 4) & 3) as usize;
239            let n = (reg & 0x0F) as u32;
240            return match n {
241                0 | 1 => self.read_data(port),
242                2..=4 => (self.base[port] >> (8 * (n - 2))) as u8,
243                5 | 6 => (self.offset[port] >> (8 * (n - 5))) as u8,
244                7 | 8 => (self.increment[port] >> (8 * (n - 7))) as u8,
245                9 => self.control[port],
246                0x0A => self.switches.trigger_reg_read,
247                _ => self.switches.unmapped_read,
248            };
249        }
250        if reg < 0xE0 {
251            return self.switches.unmapped_read;
252        }
253        let n = (reg & 0x1F) as u32;
254        match n {
255            0..=3 => (self.value >> (8 * n)) as u8,
256            4 => self.shift & self.switches.shift_read_mask,
257            5 => self.rotate & self.switches.shift_read_mask,
258            0x1C | 0x1D => 0x00,
259            0x1E => 0x10, // version [emu: all three; Mednafen: "we should verify this"]
260            0x1F => 0x51, // ID
261            _ => self.switches.unmapped_read,
262        }
263    }
264
265    pub fn write_register(&mut self, reg: u8, value: u8) {
266        let wide = value as u32;
267        if reg < 0x80 {
268            let port = ((reg >> 4) & 3) as usize;
269            let n = (reg & 0x0F) as u32;
270            match n {
271                0 | 1 => self.write_data(port, value),
272                2..=4 => {
273                    let shift = 8 * (n - 2);
274                    self.base[port] = (self.base[port] & !(0xFF << shift)) | (wide << shift);
275                }
276                5 => {
277                    self.offset[port] = (self.offset[port] & 0xFF00) | wide;
278                    if self.trigger(port) == TRIGGER_LOW {
279                        self.add_offset_to_base(port);
280                    }
281                }
282                6 => {
283                    self.offset[port] = (self.offset[port] & 0x00FF) | (wide << 8);
284                    if self.trigger(port) == TRIGGER_HIGH {
285                        self.add_offset_to_base(port);
286                    }
287                }
288                7 => self.increment[port] = (self.increment[port] & 0xFF00) | wide,
289                8 => self.increment[port] = (self.increment[port] & 0x00FF) | (wide << 8),
290                9 => self.control[port] = value & 0x7F,
291                0x0A if self.trigger(port) == TRIGGER_REG_A => self.add_offset_to_base(port),
292                _ => {}
293            }
294            return;
295        }
296        if reg < 0xE0 {
297            return;
298        }
299        let n = (reg & 0x1F) as u32;
300        match n {
301            0..=3 => {
302                let shift = 8 * n;
303                self.value = (self.value & !(0xFF << shift)) | (wide << shift);
304            }
305            4 => {
306                self.shift = value;
307                let count = wide & 0x0F;
308                if count != 0 {
309                    if count & 8 != 0 {
310                        self.value >>= 16 - count; // right by 8..1 for 8..15
311                    } else {
312                        self.value <<= count;
313                    }
314                }
315            }
316            5 => {
317                self.rotate = value;
318                let count = wide & 0x0F;
319                if count != 0 {
320                    self.value = if count & 8 != 0 {
321                        self.value.rotate_right(16 - count)
322                    } else {
323                        self.value.rotate_left(count)
324                    };
325                }
326            }
327            _ => {}
328        }
329    }
330}