openMSX
MSXHiResTimer.cc
Go to the documentation of this file.
1 #include "MSXHiResTimer.hh"
2 #include "serialize.hh"
3 
4 namespace openmsx {
5 
7  : MSXDevice(config)
8  , reference(getCurrentTime())
9 {
11 }
12 
13 void MSXHiResTimer::reset(EmuTime::param time)
14 {
15  reference.reset(time);
16  latchedValue = 0;
17 }
18 
19 void MSXHiResTimer::writeIO(word /*port*/, byte /*value*/, EmuTime::param time)
20 {
21  // write to any port will reset the counter
22  reference.advance(time);
23 }
24 
25 byte MSXHiResTimer::readIO(word port, EmuTime::param time)
26 {
27  if ((port & 3) == 0) {
28  // reading port 0 will latch the current counter value
29  latchedValue = reference.getTicksTill(time);
30  }
31  // reading port {0, 1, 2, 3} will read bits {0-7, 8-15, 16-23, 24-32}
32  // of the counter
33  return latchedValue >> (8 * (port & 3));
34 }
35 
36 byte MSXHiResTimer::peekIO(word port, EmuTime::param time) const
37 {
38  unsigned tmp = (port & 3) ? latchedValue : reference.getTicksTill(time);
39  return tmp >> (8 * (port & 3));
40 }
41 
42 template<typename Archive>
43 void MSXHiResTimer::serialize(Archive& ar, unsigned /*version*/)
44 {
45  ar.template serializeBase<MSXDevice>(*this);
46  ar.serialize("reference", reference,
47  "latchedValue", latchedValue);
48 }
50 REGISTER_MSXDEVICE(MSXHiResTimer, "HiResTimer");
51 
52 } // namespace openmsx
openmsx::MSXDevice
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:31
openmsx::Clock::getTicksTill
constexpr unsigned getTicksTill(EmuTime::param e) const
Calculate the number of ticks for this clock until the given time.
Definition: Clock.hh:58
serialize.hh
openmsx::MSXHiResTimer::MSXHiResTimer
MSXHiResTimer(const DeviceConfig &config)
Definition: MSXHiResTimer.cc:6
openmsx::DeviceConfig
Definition: DeviceConfig.hh:19
MSXHiResTimer.hh
openmsx::REGISTER_MSXDEVICE
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
openmsx::MSXHiResTimer::peekIO
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
Definition: MSXHiResTimer.cc:36
openmsx::MSXDevice::getCurrentTime
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:131
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::Clock::reset
constexpr void reset(EmuTime::param e)
Reset the clock to start ticking at the given time.
Definition: Clock.hh:102
openmsx::Clock::advance
constexpr void advance(EmuTime::param e)
Advance this clock in time until the last tick which is not past the given time.
Definition: Clock.hh:110
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
openmsx::MSXHiResTimer::serialize
void serialize(Archive &ar, unsigned version)
Definition: MSXHiResTimer.cc:43
openmsx::MSXHiResTimer::reset
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MSXHiResTimer.cc:13
openmsx::MSXHiResTimer::readIO
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
Definition: MSXHiResTimer.cc:25
openmsx::MSXHiResTimer
Definition: MSXHiResTimer.hh:9
openmsx::MSXHiResTimer::writeIO
void writeIO(word port, byte value, EmuTime::param time) override
Write a byte to a given IO port at a certain time to this device.
Definition: MSXHiResTimer.cc:19
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5