OXYGENHUB
◆ main openmsx / src / input / SG1000JoystickIO.cc
1.3 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "SG1000JoystickIO.hh"
2
3
#include "JoystickPort.hh"
4
5
#include "MSXMotherBoard.hh"
6
#include "serialize.hh"
7
8
#include "narrow.hh"
9
10
namespace openmsx {
11
12
// MSXDevice
13
SG1000JoystickIO::SG1000JoystickIO(const DeviceConfig& config)
14
	: MSXDevice(config)
15
16
{
17
	MSXMotherBoard& motherBoard = getMotherBoard();
18
	auto time = getCurrentTime();
19
	for (unsigned i = 0; i < 2; i++) {
20
		ports[i] = &motherBoard.getJoystickPort(i);
21
		// Clear strobe bit, or MSX joysticks will stay silent.
22
		ports[i]->write(0xFB, time);
23
	}
24
}
25
26
byte SG1000JoystickIO::readIO(uint16_t port, EmuTime time)
27
{
28
	return peekIO(port, time);
29
}
30
31
byte SG1000JoystickIO::peekIO(uint16_t port, EmuTime time) const
32
{
33
	// Joystick hardware cannot detect when it's being read, so using the
34
	// read() method for peeking should be safe.
35
	if (port & 1) {
36
		byte joy2 = ports[1]->read(time) & 0x3F;
37
		return 0xF0 | (joy2 >> 2);
38
	} else {
39
		byte joy1 = ports[0]->read(time) & 0x3F;
40
		byte joy2 = ports[1]->read(time) & 0x3F;
41
		return narrow_cast<byte>(joy1 | (joy2 << 6));
42
	}
43
}
44
45
46
template<typename Archive>
47
void SG1000JoystickIO::serialize(Archive& ar, unsigned /*version*/)
48
{
49
	ar.template serializeBase<MSXDevice>(*this);
50
}
51
INSTANTIATE_SERIALIZE_METHODS(SG1000JoystickIO);
52
REGISTER_MSXDEVICE(SG1000JoystickIO, "SG1000Joystick");
53
54
} // namespace openmsx