openMSX
SG1000JoystickIO.cc
Go to the documentation of this file.
1 #include "SG1000JoystickIO.hh"
2 #include "MSXMotherBoard.hh"
3 #include "JoystickPort.hh"
4 #include "serialize.hh"
5 
6 namespace openmsx {
7 
8 // MSXDevice
10  : MSXDevice(config)
11 
12 {
13  MSXMotherBoard& motherBoard = getMotherBoard();
14  auto time = getCurrentTime();
15  for (unsigned i = 0; i < 2; i++) {
16  ports[i] = &motherBoard.getJoystickPort(i);
17  // Clear strobe bit, or MSX joysticks will stay silent.
18  ports[i]->write(0xFB, time);
19  }
20 }
21 
23 {
24 }
25 
26 byte SG1000JoystickIO::readIO(word port, EmuTime::param time)
27 {
28  return peekIO(port, time);
29 }
30 
31 byte SG1000JoystickIO::peekIO(word port, EmuTime::param 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 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 }
53 
54 } // namespace openmsx
virtual void write(byte value, EmuTime::param time)=0
virtual byte read(EmuTime::param time)=0
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:33
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:71
EmuTime::param getCurrentTime() const
Definition: MSXDevice.cc:126
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:9
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1009