pcengine_rust/
vce.rs

1//! The HuC6260 VCE: colour table, control register, dot clock, frame length (`vce.py`).
2//!
3//! [mfr: Hudson HuC6260 manual; hw: pcetech 6; doc: wiki "VCE pinout", "Timing"] (MacDonald's
4//! headings swap the VDC and VCE names: C1.)
5//!
6//! Registers ($FF:0400-07FF, 8-byte mirror):
7//!
8//! ```text
9//! $0 control (write only)    D7 monochrome, D2 blur / line count (C3), D1-D0 dot clock
10//! $2/$3 colour address LSB/MSB (write only; 9 bits)
11//! $4/$5 colour data LSB/MSB  (the address advances after the MSB, read or write)
12//! ```
13//!
14//! Everything else reads $FF. The palette is 512 entries of `-------GGGRRRBBB`.
15//!
16//! Output colour: the board exposes the 9-bit pixel and palette values; turning GRB into RGB is a
17//! replaceable table (`rgb_table`), because there is no standard conversion (docs/vce.md). The
18//! default is a plain linear 3-bit expansion, which is *not* any emulator's colours.
19
20use serde::{Deserialize, Serialize};
21
22use crate::mt::PythonRandom;
23
24/// Dot clock divider by CR bits 1-0 [hw: pcetech 6, doc: wiki Timing]: 5.37, 7.16, 10.7 MHz.
25/// Hudson's manual knows only 4 and 3 (C8); bit 1 set is MacDonald's 10 MHz mode.
26pub const DOT_DIVIDERS: [i64; 4] = [4, 3, 2, 2];
27
28/// Entry `GGGRRRBBB` -> (r, g, b), each 3-bit value scaled by 255/7.
29pub fn linear_rgb_table() -> Vec<(u8, u8, u8)> {
30    (0..512u32)
31        .map(|entry| {
32            let (g, rd, b) = ((entry >> 6) & 7, (entry >> 3) & 7, entry & 7);
33            (
34                (rd * 255 / 7) as u8,
35                (g * 255 / 7) as u8,
36                (b * 255 / 7) as u8,
37            )
38        })
39        .collect()
40}
41
42#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
43pub struct Vce {
44    pub palette: Vec<u16>,
45    pub control: u8,
46    pub address: u16,
47    /// Host-side: not state (a loaded state keeps the system's own table).
48    #[serde(skip, default = "linear_rgb_table")]
49    pub rgb_table: Vec<(u8, u8, u8)>,
50    /// (mclk, entry, value) of every palette change, drained by the line capture.
51    pub palette_log: Vec<(i64, u16, u16)>,
52}
53
54impl Vce {
55    pub fn new(power_on: Option<&mut PythonRandom>) -> Vce {
56        // U7: zeros by default, random on request.
57        let (palette, control, address) = match power_on {
58            None => (vec![0; 512], 0, 0),
59            Some(rng) => {
60                let palette = (0..512).map(|_| rng.getrandbits(9) as u16).collect();
61                let control = rng.getrandbits(8) as u8;
62                let address = rng.getrandbits(9) as u16;
63                (palette, control, address)
64            }
65        };
66        Vce {
67            palette,
68            control,
69            address,
70            rgb_table: linear_rgb_table(),
71            palette_log: Vec::new(),
72        }
73    }
74
75    // -- derived --------------------------------------------------------------------------------
76
77    #[inline]
78    pub fn dot_divider(&self) -> i64 {
79        DOT_DIVIDERS[(self.control & 3) as usize]
80    }
81
82    pub fn monochrome(&self) -> bool {
83        self.control & 0x80 != 0
84    }
85
86    pub fn bit2(&self) -> bool {
87        self.control & 0x04 != 0
88    }
89
90    pub fn rgb(&self, entry: usize) -> (u8, u8, u8) {
91        self.rgb_table[self.palette[entry & 0x1FF] as usize]
92    }
93
94    // -- the CPU's port -------------------------------------------------------------------------
95
96    pub fn read(&mut self, offset: u32, _mclk: i64) -> u8 {
97        let reg = offset & 7;
98        if reg == 4 {
99            return (self.palette[self.address as usize] & 0xFF) as u8;
100        }
101        if reg == 5 {
102            let value = 0xFE | ((self.palette[self.address as usize] >> 8) & 1) as u8;
103            self.address = (self.address + 1) & 0x1FF;
104            return value;
105        }
106        0xFF
107    }
108
109    pub fn write(&mut self, offset: u32, value: u8, mclk: i64) {
110        let reg = offset & 7;
111        let value = value as u16;
112        match reg {
113            0 => self.control = value as u8,
114            2 => self.address = (self.address & 0x100) | value,
115            3 => self.address = (self.address & 0x0FF) | ((value & 1) << 8),
116            4 => self.set_entry((self.palette[self.address as usize] & 0x100) | value, mclk),
117            5 => {
118                self.set_entry(
119                    (self.palette[self.address as usize] & 0x0FF) | ((value & 1) << 8),
120                    mclk,
121                );
122                self.address = (self.address + 1) & 0x1FF;
123            }
124            _ => {}
125        }
126    }
127
128    fn set_entry(&mut self, value: u16, mclk: i64) {
129        self.palette[self.address as usize] = value;
130        self.palette_log.push((mclk, self.address, value));
131    }
132}