pcenginecd_rust/board.rs
1//! The CD-ROM² / Super CD-ROM² machine: the base board plus a System Card and a CD unit
2//! (`board.py`).
3//!
4//! Composition through the base board's sockets (the reference's docs/start-here.md):
5//!
6//! - socket 1 (HuCard slot): the System Card (`SystemCard`), which also holds the Super System
7//! Card's RAM at `$68-$7F` and its id registers;
8//! - socket 2 (expansion port): `CdUnit` claims CD RAM (`$80-$87`), BRAM (`$F7`) and
9//! `$1800-$18FF`, and pulls the I/O port's bit 7 low (`present`);
10//! - the Arcade Card (`arcade_card`) is a component of the unit: it adds banks `$40-$43` and
11//! `$1A00-$1AFF` to the unit's claims (arcade.rs);
12//! - socket 6 (interrupts): the unit's bit "cd" on the board's IRQ2 line;
13//! - socket 9 (audio): the CD-DA player and the ADPCM unit are the port device's audio
14//! sources, mixed after the PSG;
15//! - socket 5 (clock domains): the board catches the unit up to its clock **every advance**.
16//!
17//! Differences forced by Rust: the reference's `PcEngineCd` object keeps `card`, `unit`,
18//! `arcade` and `board` side by side; here the board owns the card and the unit, so
19//! `PcEngineCd` is the description (the reference's constructor arguments, with its defaults,
20//! and every named switch) and `build` hands back the base board's `System`; `unit`, `card`
21//! and `arcade` reach the devices inside a board. The reference's `**board_kwargs` is `board`,
22//! a `BoardOptions` whose card and port are replaced.
23
24use pcengine_rust::board::{Board, BoardOptions};
25use pcengine_rust::system::System;
26
27use crate::adpcm::AdpcmSwitches;
28use crate::arcade::{ArcadeCard, ArcadeSwitches};
29use crate::cdda::Fader;
30use crate::disc::Disc;
31use crate::scsi::ScsiSwitches;
32use crate::syscard::SystemCard;
33use crate::unit::{CdUnit, UnitSwitches};
34
35/// Every named switch of the add-on, with the reference's defaults.
36#[derive(Clone, Copy, Debug, PartialEq, Default)]
37pub struct CdSwitches {
38 pub scsi: ScsiSwitches,
39 pub unit: UnitSwitches,
40 /// the fade times and `pcetech_fade` (the reference's `CddaPlayer` attributes)
41 pub fader: Fader,
42 /// the reference's `CddaPlayer.SUBQ_STATUS`: playing, paused, stopped, seeking
43 pub subq_status: Option<[u8; 4]>,
44 pub adpcm: AdpcmSwitches,
45 pub arcade: ArcadeSwitches,
46}
47
48/// A CD-ROM² system. `system_card` is the BIOS image (bring your own).
49pub struct PcEngineCd {
50 pub system_card: Vec<u8>,
51 pub disc: Option<Disc>,
52 /// `None`: a 256 KiB image is a Super System Card (U37)
53 pub super_system_card: Option<bool>,
54 pub region_us: bool,
55 /// the backup RAM's content (`None`: formatted and empty, U36)
56 pub bram: Option<Vec<u8>>,
57 pub arcade_card: bool,
58 pub switches: CdSwitches,
59 /// The base board's options (machine row, pad, switches); `card` and `port` are replaced.
60 pub board: BoardOptions,
61}
62
63impl PcEngineCd {
64 pub fn new(system_card: &[u8], disc: Option<Disc>) -> PcEngineCd {
65 PcEngineCd {
66 system_card: system_card.to_vec(),
67 disc,
68 super_system_card: None,
69 region_us: false,
70 bram: None,
71 arcade_card: false,
72 switches: CdSwitches::default(),
73 board: BoardOptions::default(),
74 }
75 }
76
77 /// The unit as this description builds it (the tests drive it without a board).
78 pub fn make_unit(
79 disc: Option<Disc>,
80 bram: Option<&[u8]>,
81 arcade_card: bool,
82 switches: &CdSwitches,
83 ) -> CdUnit {
84 let arcade = arcade_card.then(|| ArcadeCard::with(switches.arcade));
85 let mut unit = CdUnit::with(disc, bram, arcade);
86 unit.switches = switches.unit;
87 unit.bus.switches = switches.scsi;
88 unit.adpcm.switches = switches.adpcm;
89 unit.bus.cdda.fader.set(switches.fader);
90 if let Some(codes) = switches.subq_status {
91 unit.bus.cdda.subq_status = codes;
92 }
93 unit
94 }
95
96 /// The board with the System Card in the slot and the unit on the port.
97 pub fn build_board(self) -> Result<Board, String> {
98 let card = SystemCard::new(&self.system_card, self.super_system_card, self.region_us)?;
99 let unit = PcEngineCd::make_unit(
100 self.disc,
101 self.bram.as_deref(),
102 self.arcade_card,
103 &self.switches,
104 );
105 Board::new(BoardOptions {
106 card: Some(Box::new(card)),
107 port: Some(Box::new(unit)),
108 ..self.board
109 })
110 }
111
112 /// The real HuC6280 core on this board.
113 pub fn build(self) -> Result<System, String> {
114 Ok(System::new(self.build_board()?))
115 }
116}
117
118/// The CD unit on a board's expansion port.
119pub fn unit(board: &mut Board) -> Option<&mut CdUnit> {
120 board.port.as_mut()?.as_any_mut().downcast_mut()
121}
122
123/// The System Card in a board's slot.
124pub fn card(board: &mut Board) -> Option<&mut SystemCard> {
125 board.slot.card.as_mut()?.as_any_mut().downcast_mut()
126}
127
128/// The Arcade Card, if the board's unit has one fitted.
129pub fn arcade(board: &mut Board) -> Option<&mut ArcadeCard> {
130 unit(board)?.arcade.as_mut()
131}