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
/*
    Copyright (C) 2020-2022  Rafal Michalski

    This file is part of SPECTRUSTY, a Rust library for building emulators.

    For the full copyright notice, see the lib.rs file.
*/
//! A bus device for connecting **ZX Printer** family of printers.
use core::num::NonZeroU16;
use core::fmt;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};

#[cfg(feature = "snapshot")]
use serde::{Serialize, Deserialize};

use spectrusty_core::{
    bus::{BusDevice, PortAddress},
    clock::TimestampOps
};
use super::ay::PassByAyAudioBusDevice;

pub use crate::zxprinter::*;

pub type ZxPrinter<S, D> = ZxPrinterBusDevice<ZxPrinterPortAddress, S, D>;
pub type Alphacom32<S, D> = ZxPrinterBusDevice<Alphacom32PortAddress, S, D>;
pub type TS2040<S, D> = ZxPrinterBusDevice<TS2040PortAddress, S, D>;

macro_rules! printer_names {
    ($($ty:ident: $name:expr),*) => { $(
        impl<S, D: BusDevice> fmt::Display for $ty<S, D> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str($name)
            }
        }
    )*};
}

printer_names! {
    ZxPrinter: "ZX Printer",
    Alphacom32: "Alphacom 32 Printer",
    TS2040: "TS2040 Printer"
}

/// Connects the [ZxPrinterDevice] emulator as a [BusDevice].
#[derive(Clone, Default, Debug)]
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "snapshot", serde(bound(deserialize = "
    S: Default,
    D: Deserialize<'de> + Default,
    D::Timestamp: Deserialize<'de> + Default",
serialize = "
    D: Serialize,
    D::Timestamp: Serialize")))]
pub struct ZxPrinterBusDevice<P, S, D: BusDevice>
{
    /// Provides direct access to the [ZxPrinterDevice].
    #[cfg_attr(feature = "snapshot", serde(default))]
    pub printer: ZxPrinterDevice<D::Timestamp, S>,
    #[cfg_attr(feature = "snapshot", serde(default))]
    bus: D,
    #[cfg_attr(feature = "snapshot", serde(skip))]
    _port_decode: PhantomData<P>
}

#[derive(Clone, Copy, Default, Debug)]
pub struct ZxPrinterPortAddress;
impl PortAddress for ZxPrinterPortAddress {
    const ADDRESS_MASK: u16 = 0b0000_0000_0000_0100;
    const ADDRESS_BITS: u16 = 0b0000_0000_1111_1011;
}

#[derive(Clone, Copy, Default, Debug)]
pub struct Alphacom32PortAddress;
impl PortAddress for Alphacom32PortAddress {
    const ADDRESS_MASK: u16 = 0b0000_0000_0100_0100;
    const ADDRESS_BITS: u16 = 0b0000_0000_1111_1011;
}

#[derive(Clone, Copy, Default, Debug)]
pub struct TS2040PortAddress;
impl PortAddress for TS2040PortAddress {
    const ADDRESS_MASK: u16 = 0b0000_0000_1000_0100;
    const ADDRESS_BITS: u16 = 0b0000_0000_1111_1011;
}

impl<P, S, D: BusDevice> Deref for ZxPrinterBusDevice<P, S, D> {
    type Target = ZxPrinterDevice<D::Timestamp, S>;
    fn deref(&self) -> &Self::Target {
        &self.printer
    }
}

impl<P, S, D: BusDevice> DerefMut for ZxPrinterBusDevice<P, S, D> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.printer
    }
}

impl<P, S, D: BusDevice> PassByAyAudioBusDevice for ZxPrinterBusDevice<P, S, D> {}

impl<P, S, D> BusDevice for ZxPrinterBusDevice<P, S, D>
    where P: PortAddress,
          S: Spooler,
          D: BusDevice,
          D::Timestamp: TimestampOps
{
    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.printer.reset();
        self.bus.reset(timestamp);
    }

    #[inline]
    fn next_frame(&mut self, eof_timestamp: Self::Timestamp) {
        self.printer.next_frame(eof_timestamp);
        self.bus.next_frame(eof_timestamp)
    }

    #[inline]
    fn read_io(&mut self, port: u16, timestamp: Self::Timestamp) -> Option<(u8, Option<NonZeroU16>)> {
        let bus_data = self.bus.read_io(port, timestamp);
        if P::match_port(port) {
            let pr_data = self.printer.read_status(timestamp);
            if let Some((data, ws)) = bus_data {
                return Some((data & pr_data, ws))
            }
            return Some((pr_data, None))
        }
        bus_data
    }

    #[inline]
    fn write_io(&mut self, port: u16, data: u8, timestamp: Self::Timestamp) -> Option<u16> {
        if P::match_port(port) {
            // println!("print: {:04x} {:b}", port, data);
            self.printer.write_control(data, timestamp);
            return Some(0);
        }
        self.bus.write_io(port, data, timestamp)
    }
}