spectrusty_peripherals/bus/
zxprinter.rs1use core::num::NonZeroU16;
10use core::fmt;
11use core::marker::PhantomData;
12use core::ops::{Deref, DerefMut};
13
14#[cfg(feature = "snapshot")]
15use serde::{Serialize, Deserialize};
16
17use spectrusty_core::{
18 bus::{BusDevice, PortAddress},
19 clock::TimestampOps
20};
21use super::ay::PassByAyAudioBusDevice;
22
23pub use crate::zxprinter::*;
24
25pub type ZxPrinter<S, D> = ZxPrinterBusDevice<ZxPrinterPortAddress, S, D>;
26pub type Alphacom32<S, D> = ZxPrinterBusDevice<Alphacom32PortAddress, S, D>;
27pub type TS2040<S, D> = ZxPrinterBusDevice<TS2040PortAddress, S, D>;
28
29macro_rules! printer_names {
30 ($($ty:ident: $name:expr),*) => { $(
31 impl<S, D: BusDevice> fmt::Display for $ty<S, D> {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 f.write_str($name)
34 }
35 }
36 )*};
37}
38
39printer_names! {
40 ZxPrinter: "ZX Printer",
41 Alphacom32: "Alphacom 32 Printer",
42 TS2040: "TS2040 Printer"
43}
44
45#[derive(Clone, Default, Debug)]
47#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
48#[cfg_attr(feature = "snapshot", serde(bound(deserialize = "
49 S: Default,
50 D: Deserialize<'de> + Default,
51 D::Timestamp: Deserialize<'de> + Default",
52serialize = "
53 D: Serialize,
54 D::Timestamp: Serialize")))]
55pub struct ZxPrinterBusDevice<P, S, D: BusDevice>
56{
57 #[cfg_attr(feature = "snapshot", serde(default))]
59 pub printer: ZxPrinterDevice<D::Timestamp, S>,
60 #[cfg_attr(feature = "snapshot", serde(default))]
61 bus: D,
62 #[cfg_attr(feature = "snapshot", serde(skip))]
63 _port_decode: PhantomData<P>
64}
65
66#[derive(Clone, Copy, Default, Debug)]
67pub struct ZxPrinterPortAddress;
68impl PortAddress for ZxPrinterPortAddress {
69 const ADDRESS_MASK: u16 = 0b0000_0000_0000_0100;
70 const ADDRESS_BITS: u16 = 0b0000_0000_1111_1011;
71}
72
73#[derive(Clone, Copy, Default, Debug)]
74pub struct Alphacom32PortAddress;
75impl PortAddress for Alphacom32PortAddress {
76 const ADDRESS_MASK: u16 = 0b0000_0000_0100_0100;
77 const ADDRESS_BITS: u16 = 0b0000_0000_1111_1011;
78}
79
80#[derive(Clone, Copy, Default, Debug)]
81pub struct TS2040PortAddress;
82impl PortAddress for TS2040PortAddress {
83 const ADDRESS_MASK: u16 = 0b0000_0000_1000_0100;
84 const ADDRESS_BITS: u16 = 0b0000_0000_1111_1011;
85}
86
87impl<P, S, D: BusDevice> Deref for ZxPrinterBusDevice<P, S, D> {
88 type Target = ZxPrinterDevice<D::Timestamp, S>;
89 fn deref(&self) -> &Self::Target {
90 &self.printer
91 }
92}
93
94impl<P, S, D: BusDevice> DerefMut for ZxPrinterBusDevice<P, S, D> {
95 fn deref_mut(&mut self) -> &mut Self::Target {
96 &mut self.printer
97 }
98}
99
100impl<P, S, D: BusDevice> PassByAyAudioBusDevice for ZxPrinterBusDevice<P, S, D> {}
101
102impl<P, S, D> BusDevice for ZxPrinterBusDevice<P, S, D>
103 where P: PortAddress,
104 S: Spooler,
105 D: BusDevice,
106 D::Timestamp: TimestampOps
107{
108 type Timestamp = D::Timestamp;
109 type NextDevice = D;
110
111 #[inline]
112 fn next_device_mut(&mut self) -> &mut Self::NextDevice {
113 &mut self.bus
114 }
115
116 #[inline]
117 fn next_device_ref(&self) -> &Self::NextDevice {
118 &self.bus
119 }
120
121 #[inline]
122 fn into_next_device(self) -> Self::NextDevice {
123 self.bus
124 }
125
126 #[inline]
127 fn reset(&mut self, timestamp: Self::Timestamp) {
128 self.printer.reset();
129 self.bus.reset(timestamp);
130 }
131
132 #[inline]
133 fn next_frame(&mut self, eof_timestamp: Self::Timestamp) {
134 self.printer.next_frame(eof_timestamp);
135 self.bus.next_frame(eof_timestamp)
136 }
137
138 #[inline]
139 fn read_io(&mut self, port: u16, timestamp: Self::Timestamp) -> Option<(u8, Option<NonZeroU16>)> {
140 let bus_data = self.bus.read_io(port, timestamp);
141 if P::match_port(port) {
142 let pr_data = self.printer.read_status(timestamp);
143 if let Some((data, ws)) = bus_data {
144 return Some((data & pr_data, ws))
145 }
146 return Some((pr_data, None))
147 }
148 bus_data
149 }
150
151 #[inline]
152 fn write_io(&mut self, port: u16, data: u8, timestamp: Self::Timestamp) -> Option<u16> {
153 if P::match_port(port) {
154 self.printer.write_control(data, timestamp);
156 return Some(0);
157 }
158 self.bus.write_io(port, data, timestamp)
159 }
160}