openMSX
VDPIODelay.cc
Go to the documentation of this file.
1 #include "VDPIODelay.hh"
2 #include "MSXCPU.hh"
4 #include "DummyDevice.hh"
5 #include "serialize.hh"
6 #include <cassert>
7 
8 namespace openmsx {
9 
10 VDPIODelay::VDPIODelay(const DeviceConfig& config, MSXCPUInterface& cpuInterface)
11  : MSXDevice(config)
12  , cpu(getCPU()) // used frequently, so cache it
13  , lastTime(EmuTime::zero())
14 {
15  for (int port = 0x098; port <= 0x9B; ++port) {
16  getInDevicePtr (port) = &cpuInterface.getDummyDevice();
17  getOutDevicePtr(port) = &cpuInterface.getDummyDevice();
18  }
19 }
20 
21 const MSXDevice& VDPIODelay::getInDevice(byte port) const
22 {
23  assert((0x98 <= port) && (port <= 0x9B));
24  return *inDevices[port - 0x98];
25 }
26 
27 MSXDevice*& VDPIODelay::getInDevicePtr(byte port)
28 {
29  assert((0x98 <= port) && (port <= 0x9B));
30  return inDevices[port - 0x98];
31 }
32 
33 MSXDevice*& VDPIODelay::getOutDevicePtr(byte port)
34 {
35  assert((0x98 <= port) && (port <= 0x9B));
36  return outDevices[port - 0x98];
37 }
38 
39 byte VDPIODelay::readIO(word port, EmuTime::param time)
40 {
41  delay(time);
42  return getInDevicePtr(byte(port))->readIO(byte(port), lastTime.getTime());
43 }
44 
45 byte VDPIODelay::peekIO(word port, EmuTime::param time) const
46 {
47  return getInDevice(byte(port)).peekIO(byte(port), time);
48 }
49 
50 void VDPIODelay::writeIO(word port, byte value, EmuTime::param time)
51 {
52  delay(time);
53  getOutDevicePtr(byte(port))->writeIO(byte(port), value, lastTime.getTime());
54 }
55 
56 void VDPIODelay::delay(EmuTime::param time)
57 {
58  if (cpu.isR800Active()) {
59  // Number of cycles based on measurements on real HW.
60  // See doc/turbor-vdp-io-timing.ods for details.
61  lastTime += 62; // 8us
62  if (time < lastTime.getTime()) {
63  cpu.wait(lastTime.getTime());
64  return;
65  }
66  }
67  lastTime.advance(time);
68 }
69 
70 template<typename Archive>
71 void VDPIODelay::serialize(Archive& ar, unsigned /*version*/)
72 {
73  ar.serialize("lastTime", lastTime);
74 }
76 
77 } // namespace openmsx
bool getEnum() const noexcept
Definition: EnumSetting.hh:96
bool isR800Active() const
Is the R800 currently active?
Definition: MSXCPU.hh:95
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX...
Definition: MSXDevice.hh:31
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
void serialize(Archive &ar, unsigned version)
Definition: VDPIODelay.cc:71
VDPIODelay(const DeviceConfig &config, MSXCPUInterface &cpuInterface)
Definition: VDPIODelay.cc:10