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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use core::num::NonZeroU16;
use core::fmt;
use core::ops::{Deref, DerefMut};
#[cfg(feature = "snapshot")]
use serde::{Serialize, Deserialize};
use spectrusty_core::{
bus::{BusDevice, PortAddress},
};
use super::ay::PassByAyAudioBusDevice;
pub use crate::parallel::*;
pub type Plus3CentronicsWriterBusDevice<D, W> = Plus3CentronicsBusDevice<ParallelPortWriter<
<D as BusDevice>::Timestamp, W>, D>;
impl<P, D> fmt::Display for Plus3CentronicsBusDevice<P, D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("+3 Centronics Port")
}
}
bitflags! {
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "snapshot", serde(from = "u8", into = "u8"))]
#[derive(Default)]
struct CentronicsFlags: u8 {
const BUSY = 0b0000_0001;
const STROBE = 0b0001_0000;
}
}
impl CentronicsFlags {
fn set_busy(&mut self, busy: bool) {
self.set(CentronicsFlags::BUSY, busy)
}
fn is_busy(self) -> bool {
self.intersects(CentronicsFlags::BUSY)
}
fn is_strobe(self) -> bool {
self.intersects(CentronicsFlags::STROBE)
}
}
#[derive(Clone, Default, Debug)]
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
pub struct Plus3CentronicsBusDevice<P, D>
{
#[cfg_attr(feature = "snapshot", serde(default))]
pub parallel: P,
flags: CentronicsFlags,
#[cfg_attr(feature = "snapshot", serde(default))]
bus: D
}
#[derive(Clone, Copy, Default, Debug)]
struct Ula3CtrlPortAddress;
impl PortAddress for Ula3CtrlPortAddress {
const ADDRESS_MASK: u16 = 0b1111_0000_0000_0010;
const ADDRESS_BITS: u16 = 0b0001_1111_1111_1101;
}
#[derive(Clone, Copy, Default, Debug)]
struct Ula3CentronicsPortAddress;
impl PortAddress for Ula3CentronicsPortAddress {
const ADDRESS_MASK: u16 = 0b1111_0000_0000_0010;
const ADDRESS_BITS: u16 = 0b0000_1111_1111_1101;
}
impl<P, D> Deref for Plus3CentronicsBusDevice<P, D> {
type Target = P;
fn deref(&self) -> &Self::Target {
&self.parallel
}
}
impl<P, D> DerefMut for Plus3CentronicsBusDevice<P, D> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.parallel
}
}
impl<P, D> PassByAyAudioBusDevice for Plus3CentronicsBusDevice<P, D> {}
impl<P, D> BusDevice for Plus3CentronicsBusDevice<P, D>
where P: ParallelPortDevice<Timestamp=D::Timestamp> + fmt::Debug,
D: BusDevice,
D::Timestamp: Copy
{
type Timestamp = D::Timestamp;
type NextDevice = D;
#[inline]
fn next_device_mut(&mut self) -> &mut Self::NextDevice {
&mut self.bus
}
#[inline]
fn next_device_ref(&self) -> &Self::NextDevice {
&self.bus
}
#[inline]
fn into_next_device(self) -> Self::NextDevice {
self.bus
}
#[inline]
fn reset(&mut self, timestamp: Self::Timestamp) {
self.bus.reset(timestamp);
}
#[inline]
fn next_frame(&mut self, eof_timestamp: Self::Timestamp) {
self.parallel.next_frame(eof_timestamp);
self.bus.next_frame(eof_timestamp)
}
#[inline]
fn update_timestamp(&mut self, timestamp: Self::Timestamp) {
if self.flags.is_busy() {
self.flags.set_busy( self.parallel.poll_busy() );
};
self.bus.update_timestamp(timestamp)
}
#[inline]
fn read_io(&mut self, port: u16, timestamp: Self::Timestamp) -> Option<(u8, Option<NonZeroU16>)> {
if Ula3CentronicsPortAddress::match_port(port) {
let data = (self.flags & CentronicsFlags::BUSY).bits() | !CentronicsFlags::BUSY.bits();
return Some((data, None))
}
self.bus.read_io(port, timestamp)
}
#[inline]
fn write_io(&mut self, port: u16, data: u8, timestamp: Self::Timestamp) -> Option<u16> {
if Ula3CentronicsPortAddress::match_port(port) {
self.parallel.write_data(data, timestamp);
return Some(0);
}
else if Ula3CtrlPortAddress::match_port(port) {
let flags = CentronicsFlags::from_bits_truncate(data);
let flags_diff = (self.flags ^ flags) & CentronicsFlags::STROBE;
if flags_diff == CentronicsFlags::STROBE {
self.flags ^= flags_diff;
self.flags.set_busy(
self.parallel.write_strobe(flags.is_strobe(), timestamp));
}
}
self.bus.write_io(port, data, timestamp)
}
}
impl From<CentronicsFlags> for u8 {
#[inline]
fn from(flags: CentronicsFlags) -> u8 {
flags.bits()
}
}
impl From<u8> for CentronicsFlags {
#[inline]
fn from(flags: u8) -> CentronicsFlags {
CentronicsFlags::from_bits_truncate(flags)
}
}