1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use super::{
EmulationMode, CRX_DATA_CGA_ADDRESS, CRX_DATA_MDA_ADDRESS, CRX_INDEX_CGA_ADDRESS,
CRX_INDEX_MDA_ADDRESS,
};
use x86_64::instructions::port::Port;
#[derive(Debug, Copy, Clone)]
#[repr(u8)]
pub enum CrtcControllerIndex {
HorizontalTotal = 0x00,
HorizontalDisplayEnableEnd = 0x01,
HorizontalBlankingStart = 0x02,
HorizontalBlankingEnd = 0x03,
HorizontalSyncStart = 0x04,
HorizontalSyncEnd = 0x05,
VeritcalTotal = 0x06,
Overflow = 0x07,
PresetRowScan = 0x08,
MaximumScanLine = 0x09,
TextCursorStart = 0x0A,
TextCursorEnd = 0x0B,
StartAddressHigh = 0x0C,
StartAddressLow = 0x0D,
TextCursorLocationHigh = 0x0E,
TextCursorLocationLow = 0x0F,
VerticalSyncStart = 0x10,
VerticalSyncEnd = 0x11,
VerticalDisplayEnableEnd = 0x12,
Offset = 0x13,
UnderlineLocation = 0x14,
VerticalBlankingStart = 0x15,
VerticalBlankingEnd = 0x16,
ModeControl = 0x17,
LineCompare = 0x18,
MemoryReadLatchData = 0x22,
ToggleStateOfAttributeController = 0x24,
}
impl From<CrtcControllerIndex> for u8 {
fn from(value: CrtcControllerIndex) -> u8 {
value as u8
}
}
#[derive(Debug)]
pub struct CrtcControllerRegisters {
crx_index_cga: Port<u8>,
crx_index_mda: Port<u8>,
crx_data_cga: Port<u8>,
crx_data_mda: Port<u8>,
}
impl CrtcControllerRegisters {
pub(crate) fn new() -> CrtcControllerRegisters {
CrtcControllerRegisters {
crx_index_cga: Port::new(CRX_INDEX_CGA_ADDRESS),
crx_index_mda: Port::new(CRX_INDEX_MDA_ADDRESS),
crx_data_cga: Port::new(CRX_DATA_CGA_ADDRESS),
crx_data_mda: Port::new(CRX_DATA_MDA_ADDRESS),
}
}
pub fn read(&mut self, emulation_mode: EmulationMode, index: CrtcControllerIndex) -> u8 {
self.set_index(emulation_mode, index);
unsafe { self.get_data_port(emulation_mode).read() }
}
pub fn write(&mut self, emulation_mode: EmulationMode, index: CrtcControllerIndex, value: u8) {
self.set_index(emulation_mode, index);
unsafe {
self.get_data_port(emulation_mode).write(value);
}
}
fn set_index(&mut self, emulation_mode: EmulationMode, index: CrtcControllerIndex) {
unsafe {
self.get_index_port(emulation_mode).write(u8::from(index));
}
}
fn get_data_port(&mut self, emulation_mode: EmulationMode) -> &mut Port<u8> {
match emulation_mode {
EmulationMode::Cga => &mut self.crx_data_cga,
EmulationMode::Mda => &mut self.crx_data_mda,
}
}
fn get_index_port(&mut self, emulation_mode: EmulationMode) -> &mut Port<u8> {
match emulation_mode {
EmulationMode::Cga => &mut self.crx_index_cga,
EmulationMode::Mda => &mut self.crx_index_mda,
}
}
}