pcengine_rust/vdc/
regs.rs

1//! The HuC6270's register file and the fields the timing generator reads (`vdc/regs.py`).
2//!
3//! [hw: pcetech 5.1; mfr: Hudson HuC6270 manual] Registers are 16 bits, written a byte at a time.
4//! Valid register numbers are $00-$02 and $05-$13; the rest do nothing.
5
6use serde::{Deserialize, Serialize};
7
8// Status port bits (read of $0). [hw: pcetech 5]
9pub const BSY: u8 = 0x40;
10pub const VD: u8 = 0x20; // vertical blank
11pub const DV: u8 = 0x10; // VRAM -> VRAM DMA done
12pub const DS: u8 = 0x08; // VRAM -> SAT DMA done
13pub const RR: u8 = 0x04; // raster compare
14pub const OR: u8 = 0x02; // sprite overflow
15pub const CR_FLAG: u8 = 0x01; // sprite 0 collision
16pub const IRQ_BITS: u8 = 0x3F;
17
18// Control register enable bits.
19pub const CR_COLLISION_IRQ: u16 = 0x01;
20pub const CR_OVERFLOW_IRQ: u16 = 0x02;
21pub const CR_RASTER_IRQ: u16 = 0x04;
22pub const CR_VBLANK_IRQ: u16 = 0x08;
23pub const CR_SPRITES: u16 = 0x40;
24pub const CR_BACKGROUND: u16 = 0x80;
25
26/// [hw: pcetech 5.1 register $05] CR bits 12-11 select how much MAWR/MARR advance per access.
27pub const INCREMENTS: [u16; 4] = [1, 32, 64, 128];
28
29/// $00-$02 and $05-$13.
30pub fn valid_register(register: u8) -> bool {
31    matches!(register, 0x00..=0x02 | 0x05..=0x13)
32}
33
34pub const MAWR: usize = 0x00;
35pub const MARR: usize = 0x01;
36pub const VRW: usize = 0x02;
37pub const CR: usize = 0x05;
38pub const RCR: usize = 0x06;
39pub const BXR: usize = 0x07;
40pub const BYR: usize = 0x08;
41pub const MWR: usize = 0x09;
42pub const HSR: usize = 0x0A;
43pub const HDR: usize = 0x0B;
44pub const VPR: usize = 0x0C;
45pub const VDW: usize = 0x0D;
46pub const VCR: usize = 0x0E;
47pub const DCR: usize = 0x0F;
48pub const SOUR: usize = 0x10;
49pub const DESR: usize = 0x11;
50pub const LENR: usize = 0x12;
51pub const SATB: usize = 0x13;
52
53/// Raw 16-bit register values, with the fields the sequencer uses as methods.
54#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
55pub struct VdcRegisters {
56    pub select: u8,
57    pub value: [u16; 0x20],
58    /// [hw: pcetech 5.1] The read buffer is indeterminate on power-up, "usually $FFFF".
59    pub read_buffer: u16,
60    pub write_latch: u8,
61}
62
63impl Default for VdcRegisters {
64    fn default() -> Self {
65        VdcRegisters::new()
66    }
67}
68
69impl VdcRegisters {
70    pub fn new() -> VdcRegisters {
71        VdcRegisters {
72            select: 0,
73            value: [0; 0x20],
74            read_buffer: 0xFFFF,
75            write_latch: 0,
76        }
77    }
78
79    pub fn set_byte(&mut self, register: usize, byte: u8, msb: bool) {
80        let old = self.value[register];
81        if msb {
82            self.value[register] = (old & 0x00FF) | ((byte as u16) << 8);
83        } else {
84            self.value[register] = (old & 0xFF00) | byte as u16;
85        }
86    }
87
88    // -- fields ---------------------------------------------------------------------------------
89
90    #[inline]
91    pub fn cr(&self) -> u16 {
92        self.value[CR]
93    }
94
95    #[inline]
96    pub fn increment(&self) -> u16 {
97        INCREMENTS[((self.value[CR] >> 11) & 3) as usize]
98    }
99
100    #[inline]
101    pub fn rcr(&self) -> i64 {
102        (self.value[RCR] & 0x3FF) as i64
103    }
104
105    #[inline]
106    pub fn mwr(&self) -> u16 {
107        self.value[MWR]
108    }
109
110    #[inline]
111    pub fn hds(&self) -> i64 {
112        ((self.value[HSR] >> 8) & 0x7F) as i64
113    }
114
115    #[inline]
116    pub fn hdw(&self) -> i64 {
117        (self.value[HDR] & 0x7F) as i64
118    }
119
120    #[inline]
121    pub fn hde(&self) -> i64 {
122        ((self.value[HDR] >> 8) & 0x7F) as i64
123    }
124
125    #[inline]
126    pub fn vsw(&self) -> i64 {
127        (self.value[VPR] & 0x1F) as i64
128    }
129
130    #[inline]
131    pub fn vds(&self) -> i64 {
132        ((self.value[VPR] >> 8) & 0xFF) as i64
133    }
134
135    #[inline]
136    pub fn vdw(&self) -> i64 {
137        (self.value[VDW] & 0x1FF) as i64
138    }
139
140    #[inline]
141    pub fn vcr(&self) -> i64 {
142        (self.value[VCR] & 0xFF) as i64
143    }
144}