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
/*
    Copyright (C) 2020  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 pointing devices / mouses.
use core::num::NonZeroU16;
use core::fmt::{self, Debug};
use core::marker::PhantomData;
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::mouse::{
    MouseDevice, MouseInterface, NullMouseDevice,
    kempston::*
};

/// A convenient Kempston Mouse [BusDevice] type.
pub type KempstonMouse<D> = MouseBusDevice<
                                            KempstonMousePortAddress,
                                            KempstonMouseDevice,
                                            D>;

impl<D> fmt::Display for KempstonMouse<D> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Kempston Mouse")
    }
}
/// A mouse controller, providing a [BusDevice] implementation that can be used with [mouse devices][MouseDevice].
#[derive(Clone, Default, Debug)]
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
pub struct MouseBusDevice<P, M, D> {
    /// A [MouseDevice] implementation, which may also implement [MouseInterface] trait
    /// for providing user input.
    #[cfg_attr(feature = "snapshot", serde(default))]
    pub mouse: M,
    #[cfg_attr(feature = "snapshot", serde(default))]
    bus: D,
    #[cfg_attr(feature = "snapshot", serde(skip))]
    _port_decode: PhantomData<P>,
}

/// Kempston Mouse [PortAddress].
#[derive(Clone, Copy, Default, Debug)]
pub struct KempstonMousePortAddress;
impl PortAddress for KempstonMousePortAddress {
    const ADDRESS_MASK: u16 = 0b0000_0000_0010_0000;
    const ADDRESS_BITS: u16 = 0b1111_1010_1101_1111;
}

impl<P, M: MouseInterface, D> Deref for MouseBusDevice<P, M, D> {
    type Target = M;
    fn deref(&self) -> &Self::Target {
        &self.mouse
    }
}

impl<P, M: MouseInterface, D> DerefMut for MouseBusDevice<P, M, D> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.mouse
    }
}

impl<P, M, D> PassByAyAudioBusDevice for MouseBusDevice<P, M, D> {}

impl<P, M, D> BusDevice for MouseBusDevice<P, M, D>
    where P: PortAddress,
          D: BusDevice,
          M: MouseDevice
{
    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 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 mouse_data = self.mouse.port_read(port);
            if let Some((data, ws)) = bus_data {
                return Some((data & mouse_data, ws))
            }
            return Some((mouse_data, None))
        }
        bus_data
    }

    #[inline]
    fn write_io(&mut self, port: u16, data: u8, timestamp: Self::Timestamp) -> Option<u16> {
        if P::match_port(port) && self.mouse.port_write(port, data) {
            return Some(0);
        }
        self.bus.write_io(port, data, timestamp)
    }
}