openMSX
SG1000JoystickIO.cc
Go to the documentation of this file.
1#include "SG1000JoystickIO.hh"
2#include "MSXMotherBoard.hh"
3#include "JoystickPort.hh"
4#include "narrow.hh"
5#include "serialize.hh"
6
7namespace openmsx {
8
9// MSXDevice
11 : MSXDevice(config)
12
13{
14 MSXMotherBoard& motherBoard = getMotherBoard();
15 auto time = getCurrentTime();
16 for (unsigned i = 0; i < 2; i++) {
17 ports[i] = &motherBoard.getJoystickPort(i);
18 // Clear strobe bit, or MSX joysticks will stay silent.
19 ports[i]->write(0xFB, time);
20 }
21}
22
23byte SG1000JoystickIO::readIO(word port, EmuTime::param time)
24{
25 return peekIO(port, time);
26}
27
28byte SG1000JoystickIO::peekIO(word port, EmuTime::param time) const
29{
30 // Joystick hardware cannot detect when it's being read, so using the
31 // read() method for peeking should be safe.
32 if (port & 1) {
33 byte joy2 = ports[1]->read(time) & 0x3F;
34 return 0xF0 | (joy2 >> 2);
35 } else {
36 byte joy1 = ports[0]->read(time) & 0x3F;
37 byte joy2 = ports[1]->read(time) & 0x3F;
38 return narrow_cast<byte>(joy1 | (joy2 << 6));
39 }
40}
41
42
43template<typename Archive>
44void SG1000JoystickIO::serialize(Archive& ar, unsigned /*version*/)
45{
46 ar.template serializeBase<MSXDevice>(*this);
47}
50
51} // namespace openmsx
#define REGISTER_MSXDEVICE(CLASS, NAME)
Definition MSXDevice.hh:354
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition MSXDevice.hh:36
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition MSXDevice.cc:70
EmuTime::param getCurrentTime() const
Definition MSXDevice.cc:125
JoystickPortIf & getJoystickPort(unsigned port)
I/O port access to the joysticks for the Sega SG-1000.
SG1000JoystickIO(const DeviceConfig &config)
void serialize(Archive &ar, unsigned version)
byte readIO(word port, EmuTime::param time) override
Read a byte from an IO port at a certain time from this device.
byte peekIO(word port, EmuTime::param time) const override
Read a byte from a given IO port.
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)