pcengine_rust/pagetable.rs
1//! One decode for every bus master: the 8192-entry page table (`pagetable.py`).
2//!
3//! A page is 256 bytes of the 21-bit physical space. Each page has a handler; the board fills
4//! the base map (RAM, the hardware page, an empty slot answering $FF) and a card or expansion
5//! device *claims* ranges of pages, which replaces the base handler there, as the hardware does
6//! bank by bank (a Super System Card's RAM overrides the ROM mirror). There is no second bus master
7//! on a PC Engine, so the CPU's data and fetch accesses, `ST0`-`ST2`, block transfers, the
8//! debugger's peek and the lockstep probe all go through here.
9//!
10//! Differences forced by Rust: a handler is a name (`Handler`), not an object, and the access
11//! itself is the board's (`Board::page_read`, `Board::page_write`), which owns what each name
12//! stands for. `release` compares names where the reference compares identities.
13
14use serde::{Deserialize, Serialize};
15
16use crate::machine::{ADDRESS_MASK, PAGE_BITS, PAGE_COUNT};
17
18/// Who answers a page.
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
20pub enum Handler {
21 /// [hw: pcetech 3] Unused banks always return $FF; writes do nothing.
22 Unmapped,
23 /// Work RAM.
24 Ram,
25 /// The first VDC, $FF:0000-03FF (hardware_page.rs).
26 Vdc,
27 /// The VCE, $FF:0400-07FF.
28 Vce,
29 /// PSG, timer, I/O port, interrupt registers: $FF:0800-17FF.
30 CpuIo,
31 /// The card in the slot.
32 Card,
33 /// The expansion port's device.
34 Port,
35 /// The pixel path (the SuperGrafx's VPC decodes the VDC pages).
36 PixelPath,
37}
38
39/// Work RAM: `size` bytes mirrored through the pages it is claimed on.
40#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
41pub struct Ram {
42 pub data: Vec<u8>,
43 mask: usize, // sizes are powers of two (8 KiB, 32 KiB)
44}
45
46impl Ram {
47 pub fn new(size: usize) -> Ram {
48 Ram {
49 data: vec![0; size],
50 mask: size - 1,
51 }
52 }
53
54 #[inline]
55 pub fn read(&self, address: u32) -> u8 {
56 self.data[address as usize & self.mask]
57 }
58
59 #[inline]
60 pub fn write(&mut self, address: u32, value: u8) {
61 self.data[address as usize & self.mask] = value;
62 }
63}
64
65#[derive(Clone, Debug, PartialEq, Eq)]
66pub struct PageTable {
67 base: Vec<Handler>,
68 current: Vec<Handler>,
69}
70
71impl Default for PageTable {
72 fn default() -> Self {
73 PageTable::new()
74 }
75}
76
77impl PageTable {
78 pub fn new() -> PageTable {
79 PageTable {
80 base: vec![Handler::Unmapped; PAGE_COUNT],
81 current: vec![Handler::Unmapped; PAGE_COUNT],
82 }
83 }
84
85 // -- construction ---------------------------------------------------------------------------
86
87 /// The board's own map. Replaces whatever is there, claims included.
88 pub fn set_base(&mut self, first_page: usize, last_page: usize, handler: Handler) {
89 for page in first_page..=last_page {
90 self.base[page] = handler;
91 self.current[page] = handler;
92 }
93 }
94
95 /// A device takes a range of pages from the base map.
96 pub fn claim(
97 &mut self,
98 first_page: usize,
99 last_page: usize,
100 handler: Handler,
101 ) -> Result<(), String> {
102 if first_page > last_page || last_page >= PAGE_COUNT {
103 return Err(format!(
104 "page range {first_page:#x}-{last_page:#x} outside 0-{PAGE_COUNT:#x}"
105 ));
106 }
107 self.current[first_page..=last_page].fill(handler);
108 Ok(())
109 }
110
111 /// Give pages back to the base map, where `handler` still holds them.
112 pub fn release(&mut self, first_page: usize, last_page: usize, handler: Handler) {
113 for page in first_page..=last_page {
114 if self.current[page] == handler {
115 self.current[page] = self.base[page];
116 }
117 }
118 }
119
120 #[inline]
121 pub fn handler(&self, address: u32) -> Handler {
122 self.current[((address & ADDRESS_MASK) >> PAGE_BITS) as usize]
123 }
124}