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
use core::fmt;
use core::marker::PhantomData;
use emulator_hal::{BusAccess, Instant as EmuInstant, ErrorType, Step, Inspect, Debug};
use crate::state::{Z80, Z80Error, Z80Address, Z80IOAddress, Z80AddressSpace, Status};

#[derive(Clone, Debug)]
pub enum Z80BusError<MemError, IOError>
where
    MemError: ErrorType,
    IOError: ErrorType,
{
    Memory(MemError),
    IO(IOError),
}

impl<MemError, IOError> ErrorType for Z80BusError<MemError, IOError>
where
    MemError: ErrorType,
    IOError: ErrorType,
{
}

pub struct Z80Port<MemBus, IOBus, Instant>
where
    MemBus: BusAccess<Z80Address, Instant = Instant>,
    IOBus: BusAccess<Z80IOAddress, Instant = Instant>,
{
    mem_bus: MemBus,
    io_bus: IOBus,
    instant: PhantomData<Instant>,
}

impl<MemBus, IOBus, Instant> Z80Port<MemBus, IOBus, Instant>
where
    MemBus: BusAccess<Z80Address, Instant = Instant>,
    IOBus: BusAccess<Z80IOAddress, Instant = Instant>,
{
    pub fn new(mem_bus: MemBus, io_bus: IOBus) -> Self {
        Self {
            mem_bus,
            io_bus,
            instant: PhantomData,
        }
    }
}

impl<MemBus, IOBus, Instant> BusAccess<Z80AddressSpace> for Z80Port<MemBus, IOBus, Instant>
where
    Instant: EmuInstant,
    MemBus: BusAccess<Z80Address, Instant = Instant>,
    IOBus: BusAccess<Z80IOAddress, Instant = Instant>,
{
    type Instant = Instant;
    type Error = Z80BusError<MemBus::Error, IOBus::Error>;

    #[inline]
    fn read(&mut self, now: Self::Instant, addr: Z80AddressSpace, data: &mut [u8]) -> Result<usize, Self::Error> {
        match addr {
            Z80AddressSpace::Memory(addr) => self.mem_bus.read(now, addr, data).map_err(Z80BusError::Memory),
            Z80AddressSpace::IO(addr) => self.io_bus.read(now, addr, data).map_err(Z80BusError::IO),
        }
    }

    #[inline]
    fn write(&mut self, now: Self::Instant, addr: Z80AddressSpace, data: &[u8]) -> Result<usize, Self::Error> {
        match addr {
            Z80AddressSpace::Memory(addr) => self.mem_bus.write(now, addr, data).map_err(Z80BusError::Memory),
            Z80AddressSpace::IO(addr) => self.io_bus.write(now, addr, data).map_err(Z80BusError::IO),
        }
    }
}

impl ErrorType for Z80Error {}

impl<Instant, Bus> Step<Z80AddressSpace, Bus> for Z80<Instant>
where
    Instant: EmuInstant,
    Bus: BusAccess<Z80AddressSpace, Instant = Instant>,
{
    type Error = Z80Error;

    fn is_running(&mut self) -> bool {
        self.state.status == Status::Running
    }

    fn reset(&mut self, _now: Bus::Instant, _bus: &mut Bus) -> Result<(), Self::Error> {
        self.clear_state();
        Ok(())
    }

    fn step(&mut self, now: Bus::Instant, bus: &mut Bus) -> Result<Bus::Instant, Self::Error> {
        let mut executor = self.begin(now, bus)?;
        let clocks = executor.step_one()?;
        self.previous_cycle = executor.end();
        Ok(now + Instant::hertz_to_duration(self.frequency.as_hz() as u64) * clocks as u32)
    }
}


#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Z80Info {
    State,
}

impl<Bus, BusError, Instant, Writer> Inspect<Z80AddressSpace, Bus, Writer> for Z80<Instant>
where
    Bus: BusAccess<Z80AddressSpace, Instant = Instant, Error = BusError>,
    BusError: ErrorType,
    Instant: EmuInstant,
    Writer: fmt::Write,
{
    type InfoType = Z80Info;

    type Error = Z80Error;

    fn inspect(&mut self, info: Self::InfoType, bus: &mut Bus, writer: &mut Writer) -> Result<(), Self::Error> {
        match info {
            Z80Info::State => self
                .dump_state(writer, Instant::START, bus)
                .map_err(|_| Z80Error::Other("error while formatting state".to_string())),
        }
    }

    fn brief_summary(&mut self, bus: &mut Bus, writer: &mut Writer) -> Result<(), Self::Error> {
        self.inspect(Z80Info::State, bus, writer)
    }

    fn detailed_summary(&mut self, bus: &mut Bus, writer: &mut Writer) -> Result<(), Self::Error> {
        self.inspect(Z80Info::State, bus, writer)
    }
}

/// Control the execution of a CPU device for debugging purposes
impl<Bus, BusError, Instant, Writer> Debug<Z80AddressSpace, Bus, Writer> for Z80<Instant>
where
    Bus: BusAccess<Z80AddressSpace, Instant = Instant, Error = BusError>,
    BusError: ErrorType,
    Instant: EmuInstant,
    Writer: fmt::Write,
{
    // TODO this should be a new type
    type DebugError = Z80Error;

    fn get_execution_address(&mut self) -> Result<Z80AddressSpace, Self::DebugError> {
        Ok(Z80AddressSpace::Memory(self.state.pc))
    }

    fn set_execution_address(&mut self, address: Z80AddressSpace) -> Result<(), Self::DebugError> {
        if let Z80AddressSpace::Memory(address) = address {
            self.state.pc = address;
            Ok(())
        } else {
            Err(Z80Error::Other("PC can only be set to a memory address, given an IO address".to_string()))
        }
    }

    fn add_breakpoint(&mut self, address: Z80AddressSpace) {
        if let Z80AddressSpace::Memory(address) = address {
            self.debugger.breakpoints.push(address);
        }
    }

    fn remove_breakpoint(&mut self, address: Z80AddressSpace) {
        if let Z80AddressSpace::Memory(address) = address {
            if let Some(index) = self.debugger.breakpoints.iter().position(|a| *a == address) {
                self.debugger.breakpoints.remove(index);
            }
        }
    }

    fn clear_breakpoints(&mut self) {
        self.debugger.breakpoints.clear();
    }
}