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
use std::rc::Rc;
use std::cell::RefCell;
use femtos::{Instant, Duration};
use emulator_hal::{BusAdapter, NoBus, Instant as EmuInstant};

use moa_core::{System, Error, Bus, Address, Steppable, Interruptable, /* Signalable, Signal,*/ Debuggable, Transmutable};

use crate::{Z80, Z80Error, Z80Decoder};
use crate::instructions::Register;
use crate::emuhal::Z80Port;

pub struct MoaZ80<Instant>
where
    Instant: EmuInstant,
{
    pub bus: Rc<RefCell<Bus>>,
    pub cpu: Z80<Instant>,
}

impl Steppable for MoaZ80<Instant>
where
    Instant: EmuInstant,
{
    fn step(&mut self, system: &System) -> Result<Duration, Error> {
        let bus = &mut *self.bus.borrow_mut();
        let mut adapter = BusAdapter::<_, _, _, Z80Error>::new(bus, |addr| addr as u64);
        let mut io_bus = NoBus::new();
        let mut bus = Z80Port::new(&mut adapter, &mut io_bus);

        let mut executor = self.cpu.begin(system.clock, &mut bus)?;
        let clocks = executor.step_one()?;
        self.cpu.previous_cycle = executor.end();
        Ok(Instant::hertz_to_duration(self.cpu.frequency.as_hz() as u64) * clocks as u32)
    }

    fn on_error(&mut self, system: &System) {
        let bus = &mut *system.bus.borrow_mut();
        let mut adapter = BusAdapter::<_, _, _, Z80Error>::new(bus, |addr| addr as u64);
        let mut io_bus = NoBus::new();
        let mut bus = Z80Port::new(&mut adapter, &mut io_bus);
        let mut output = String::with_capacity(256);
        let _ = self.cpu.dump_state(&mut output, system.clock, &mut bus);
        println!("{}", output);
    }
}

impl Interruptable for MoaZ80<Instant> {}

/*
impl Signalable for Z80<Instant> {
    fn set_signal(&mut self, signal: Signal, flag: bool) -> Result<(), Error> {
        match signal {
            Signal::Reset => self.signals.reset = flag,
            Signal::BusRequest => self.signals.bus_request = flag,
        }
        Ok(())
    }

    fn signal(&mut self, signal: Signal) -> Option<bool> {
        match signal {
            Signal::Reset => Some(self.signals.reset),
            Signal::BusRequest => Some(self.signals.bus_request),
        }
    }
}
*/

impl Transmutable for MoaZ80<Instant> {
    fn as_steppable(&mut self) -> Option<&mut dyn Steppable> {
        Some(self)
    }

    fn as_interruptable(&mut self) -> Option<&mut dyn Interruptable> {
        Some(self)
    }

    fn as_debuggable(&mut self) -> Option<&mut dyn Debuggable> {
        Some(self)
    }

    //#[inline]
    //fn as_signalable(&mut self) -> Option<&mut dyn Signalable> {
    //    Some(self)
    //}
}

impl From<Z80Error> for Error {
    fn from(err: Z80Error) -> Self {
        match err {
            Z80Error::Halted => Self::Other("cpu halted".to_string()),
            Z80Error::Breakpoint => Self::Breakpoint("breakpoint".to_string()),
            Z80Error::Unimplemented(instruction) => Self::new(format!("unimplemented instruction {:?}", instruction)),
            Z80Error::UnexpectedInstruction(instruction) => Self::new(format!("unexpected instruction {:?}", instruction)),
            Z80Error::Other(msg) => Self::Other(msg),
            Z80Error::BusError(msg) => Self::Other(msg),
        }
    }
}

impl From<Error> for Z80Error {
    fn from(err: Error) -> Self {
        match err {
            Error::Processor(ex) => Z80Error::BusError(format!("processor error {}", ex)),
            Error::Breakpoint(_) => Z80Error::Breakpoint,
            Error::Other(msg) | Error::Assertion(msg) | Error::Emulator(_, msg) => Z80Error::BusError(msg),
        }
    }
}

impl Debuggable for MoaZ80<Instant> {
    fn add_breakpoint(&mut self, addr: Address) {
        self.cpu.debugger.breakpoints.push(addr as u16);
    }

    fn remove_breakpoint(&mut self, addr: Address) {
        if let Some(index) = self.cpu.debugger.breakpoints.iter().position(|a| *a == addr as u16) {
            self.cpu.debugger.breakpoints.remove(index);
        }
    }

    fn print_current_step(&mut self, system: &System) -> Result<(), Error> {
        let bus = &mut *system.bus.borrow_mut();
        let mut adapter = BusAdapter::<_, _, _, Z80Error>::new(bus, |addr| addr as u64);
        let mut io_bus = NoBus::new();
        let mut bus = Z80Port::new(&mut adapter, &mut io_bus);

        self.cpu.previous_cycle.decoder.dump_decoded(&mut bus);
        let mut output = String::with_capacity(256);
        let _ = self.cpu.dump_state(&mut output, system.clock, &mut bus);
        println!("{}", output);
        Ok(())
    }

    fn print_disassembly(&mut self, system: &System, addr: Address, count: usize) {
        let bus = &mut *system.bus.borrow_mut();
        let mut adapter = BusAdapter::<_, _, _, Z80Error>::new(bus, |addr| addr as u64);
        let mut io_bus = NoBus::new();
        let mut bus = Z80Port::new(&mut adapter, &mut io_bus);

        Z80Decoder::dump_disassembly(&mut bus, addr as u16, count as u16);
    }

    fn run_command(&mut self, _system: &System, args: &[&str]) -> Result<bool, Error> {
        match args[0] {
            "l" => self.cpu.state.reg[Register::L as usize] = 0x05,
            _ => {
                return Ok(true);
            },
        }
        Ok(false)
    }
}