1use crate::{BusInterface, Z80};
2
3pub trait Z80Debugger {
4    fn check_read_memory(&mut self, address: u16, cpu: &mut Z80);
5
6    fn check_read_io(&mut self, address: u16, cpu: &mut Z80);
7
8    fn check_write_memory(&mut self, address: u16, value: u8, cpu: &mut Z80);
9
10    fn check_write_io(&mut self, address: u16, value: u8, cpu: &mut Z80);
11
12    fn check_execute(&mut self, pc: u16, cpu: &mut Z80);
13}
14
15pub struct DummyZ80Debugger;
16
17impl Z80Debugger for DummyZ80Debugger {
18    fn check_read_memory(&mut self, _address: u16, _cpu: &mut Z80) {}
19
20    fn check_read_io(&mut self, _address: u16, _cpu: &mut Z80) {}
21
22    fn check_write_memory(&mut self, _address: u16, _value: u8, _cpu: &mut Z80) {}
23
24    fn check_write_io(&mut self, _address: u16, _value: u8, _cpu: &mut Z80) {}
25
26    fn check_execute(&mut self, _pc: u16, _cpu: &mut Z80) {}
27}
28
29pub(crate) trait BusDebugExt {
30    fn read_memory_debug(&mut self, address: u16, cpu: &mut Z80) -> u8;
31
32    fn read_io_debug(&mut self, address: u16, cpu: &mut Z80) -> u8;
33
34    fn write_memory_debug(&mut self, address: u16, value: u8, cpu: &mut Z80);
35
36    fn write_io_debug(&mut self, address: u16, value: u8, cpu: &mut Z80);
37
38    fn check_execute(&mut self, pc: u16, cpu: &mut Z80);
39}
40
41impl<B: BusInterface> BusDebugExt for B {
42    fn read_memory_debug(&mut self, address: u16, cpu: &mut Z80) -> u8 {
43        if let Some(mut debugger) = self.debug_view() {
44            debugger.check_read_memory(address, cpu);
45        }
46        self.read_memory(address)
47    }
48
49    fn read_io_debug(&mut self, address: u16, cpu: &mut Z80) -> u8 {
50        if let Some(mut debugger) = self.debug_view() {
51            debugger.check_read_io(address, cpu);
52        }
53        self.read_io(address)
54    }
55
56    fn write_memory_debug(&mut self, address: u16, value: u8, cpu: &mut Z80) {
57        if let Some(mut debugger) = self.debug_view() {
58            debugger.check_write_memory(address, value, cpu);
59        }
60        self.write_memory(address, value);
61    }
62
63    fn write_io_debug(&mut self, address: u16, value: u8, cpu: &mut Z80) {
64        if let Some(mut debugger) = self.debug_view() {
65            debugger.check_write_io(address, value, cpu);
66        }
67        self.write_io(address, value);
68    }
69
70    fn check_execute(&mut self, pc: u16, cpu: &mut Z80) {
71        if let Some(mut debugger) = self.debug_view() {
72            debugger.check_execute(pc, cpu);
73        }
74    }
75}