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