spectrusty_peripherals/bus/
debug.rs

1/*
2    Copyright (C) 2020-2022  Rafal Michalski
3
4    This file is part of SPECTRUSTY, a Rust library for building emulators.
5
6    For the full copyright notice, see the lib.rs file.
7*/
8//! A passthrough debugging device.
9use core::num::NonZeroU16;
10use core::fmt::Debug;
11
12#[cfg(feature = "snapshot")]
13use serde::{Serialize, Deserialize};
14
15#[allow(unused_imports)]
16use log::{error, warn, info, debug, trace};
17
18use spectrusty_core::bus::BusDevice;
19use super::ay::PassByAyAudioBusDevice;
20
21/// A passthrough [BusDevice] that outputs I/O data read and written by CPU using [log] `debug`.
22#[derive(Clone, Default, Debug)]
23#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
24pub struct DebugBusDevice<D> {
25    #[cfg_attr(feature = "snapshot", serde(default))]
26    bus: D,
27}
28
29impl<D: BusDevice> BusDevice for DebugBusDevice<D>
30    where D::Timestamp: Debug
31{
32    type Timestamp = D::Timestamp;
33    type NextDevice = D;
34
35    fn next_device_mut(&mut self) -> &mut Self::NextDevice {
36        &mut self.bus
37    }
38    fn next_device_ref(&self) -> &Self::NextDevice {
39        &self.bus
40    }
41    fn into_next_device(self) -> Self::NextDevice {
42        self.bus
43    }
44    fn read_io(&mut self, port: u16, timestamp: Self::Timestamp) -> Option<(u8, Option<NonZeroU16>)> {
45        debug!("read_io: {:04x} {:?}", port, timestamp);
46        self.bus.read_io(port, timestamp)
47    }
48    /// Called by the control unit on IO::write_io.
49    fn write_io(&mut self, port: u16, data: u8, timestamp: Self::Timestamp) -> Option<u16> {
50        debug!("write_io: {:04x} {:02x} {:?}", port, data, timestamp);
51        self.bus.write_io(port, data, timestamp)
52    }
53}
54
55impl<D> PassByAyAudioBusDevice for DebugBusDevice<D> {}