supergrafx_rust/
board.rs

1//! The SuperGrafx: the base PC Engine board with a machine row, a VPC and a second VDC
2//! (`board.py`).
3//!
4//! Nothing here is copied from pcengine-rust; this module only composes it through its sockets
5//! (the reference's docs/start-here.md lists them):
6//!
7//! - socket 12 (instances) and the machine row: `Machine::vdc_count = 2` makes the board build,
8//!   run, sync and read a second `Vdc` and OR its IRQ1;
9//! - socket 3 (the decode): `VideoPage` claims `$FF:0000-03FF` over the VDC page;
10//! - socket 7 (pixel path): `VideoPage::mix` (the `Vpc`'s) is the board's `PixelPath`;
11//! - the work RAM is the machine row's 32 KiB, mirrored nowhere (banks `$F8-$FB`).
12//!
13//! Differences forced by Rust:
14//!
15//! - **The board owns the VPC and the CPU owns the board**, so `SuperGrafx` cannot keep `vpc`,
16//!   `vdc1`, `vdc2` and `page` beside the board as the reference does. They are reached through
17//!   the board: `board.vdcs[0]`, `board.vdcs[1]`, and `video_page(&mut board)` for the page and
18//!   its `vpc` (the base board's `PixelPath::as_any_mut`). `build` consumes the `SuperGrafx` and
19//!   gives the `System`; afterwards the same things are reached with `system.board_mut()`.
20//! - **The constructor's keyword arguments** are `SuperGrafxOptions`: `compat`, `vpc` and the
21//!   base board's `BoardOptions` (the reference's `**board_kwargs`), whose `machine` and
22//!   `pixel_path` are the add-on's to set.
23//! - **`_store_immediate`** is not here: the base board hands the opcode to the page with each
24//!   access (vpc.rs). **`reset`** is `Board::reset`, which calls the page's (vpc.rs).
25
26use pcengine_rust::board::{Board, BoardOptions};
27use pcengine_rust::cards::make_card;
28use pcengine_rust::pads::Pad2;
29use pcengine_rust::sockets::ResetKind;
30use pcengine_rust::system::System;
31
32use crate::machine::SUPERGRAFX;
33use crate::vpc::{VideoPage, Vpc};
34
35/// The reference's `SuperGrafx(card, compat=..., vpc=..., **board_kwargs)`.
36pub struct SuperGrafxOptions {
37    /// `true` is the back-panel switch in PC Engine position.
38    pub compat: bool,
39    /// A VPC with other switches than the defaults (`None`: `Vpc::new()`).
40    pub vpc: Option<Vpc>,
41    /// U-SGX4 (`VideoPage::st_steers_in_compat`).
42    pub st_steers_in_compat: bool,
43    /// The base board's arguments (card, pad, port, power_on_seed, start_line, ...). `machine`
44    /// is replaced by the `SUPERGRAFX` row and `pixel_path` must be `None`: the VPC is the path.
45    pub board: BoardOptions,
46}
47
48impl Default for SuperGrafxOptions {
49    fn default() -> Self {
50        SuperGrafxOptions {
51            compat: false,
52            vpc: None,
53            st_steers_in_compat: false,
54            board: BoardOptions {
55                machine: SUPERGRAFX,
56                ..BoardOptions::default()
57            },
58        }
59    }
60}
61
62/// A base board configured as a SuperGrafx. `board` is the plain `pcengine_rust::board::Board`.
63pub struct SuperGrafx {
64    pub board: Board,
65}
66
67impl SuperGrafx {
68    pub fn new(options: SuperGrafxOptions) -> Result<SuperGrafx, String> {
69        if options.board.pixel_path.is_some() {
70            return Err("the SuperGrafx's pixel path is its VPC".to_string());
71        }
72        let mut page = VideoPage::new(options.vpc.unwrap_or_default(), options.compat);
73        page.st_steers_in_compat = options.st_steers_in_compat;
74        let board = Board::new(BoardOptions {
75            machine: SUPERGRAFX,
76            pixel_path: Some(Box::new(page)),
77            ..options.board
78        })?;
79        Ok(SuperGrafx { board })
80    }
81
82    /// A SuperGrafx with this card image in the slot and a 2-button pad in the port (a host's
83    /// usual machine: `make_card` strips a copier header; every SuperGrafx card is a plain ROM).
84    pub fn with_card(image: &[u8], compat: bool) -> Result<SuperGrafx, String> {
85        let mut options = SuperGrafxOptions {
86            compat,
87            ..SuperGrafxOptions::default()
88        };
89        options.board.card = Some(make_card(image)?);
90        options.board.pad = Some(Box::new(Pad2::new()));
91        SuperGrafx::new(options)
92    }
93
94    /// The video page (the decode, the compatibility switch) and its `vpc`.
95    pub fn page(&mut self) -> &mut VideoPage {
96        video_page(&mut self.board).expect("a SuperGrafx's pixel path is its video page")
97    }
98
99    pub fn compat(&mut self) -> bool {
100        self.page().compat
101    }
102
103    pub fn set_compat(&mut self, value: bool) {
104        self.page().compat = value;
105    }
106
107    /// The board's reset; a power-on reset also resets the VPC (`VideoPage`'s
108    /// `PixelPath::reset`).
109    pub fn reset(&mut self, kind: ResetKind) {
110        self.board.reset(kind);
111    }
112
113    /// The real HuC6280 core on this board.
114    pub fn build(self) -> System {
115        System::new(self.board)
116    }
117}
118
119/// The video page of a board built by `SuperGrafx` (`None` for any other board): after
120/// `build`, `video_page(system.board_mut())`.
121pub fn video_page(board: &mut Board) -> Option<&mut VideoPage> {
122    board.pixel_path.as_any_mut().downcast_mut::<VideoPage>()
123}
124
125/// Move the back-panel switch of a running SuperGrafx (`true`: PC Engine position). `false`
126/// if the system is not a SuperGrafx.
127pub fn set_compat(system: &mut System, value: bool) -> bool {
128    match video_page(system.board_mut()) {
129        Some(page) => {
130            page.compat = value;
131            true
132        }
133        None => false,
134    }
135}