openMSX
JoystickPort.cc
Go to the documentation of this file.
1 #include "JoystickPort.hh"
2 #include "JoystickDevice.hh"
3 #include "DummyJoystick.hh"
4 #include "PluggingController.hh"
5 #include "checked_cast.hh"
6 #include "serialize.hh"
7 #include <memory>
8 
9 using std::string;
10 
11 namespace openmsx {
12 
14  std::string name_, std::string description_)
15  : Connector(pluggingController_, std::move(name_), std::make_unique<DummyJoystick>())
16  , lastValue(255) // != 0
17  , description(std::move(description_))
18 {
19 }
20 
22 {
23  return description;
24 }
25 
27 {
28  return "Joystick Port";
29 }
30 
32 {
33  return *checked_cast<JoystickDevice*>(&getPlugged());
34 }
35 
36 void JoystickPort::plug(Pluggable& device, EmuTime::param time)
37 {
38  Connector::plug(device, time);
39  getPluggedJoyDev().write(lastValue, time);
40 }
41 
42 byte JoystickPort::read(EmuTime::param time)
43 {
44  return getPluggedJoyDev().read(time);
45 }
46 
47 void JoystickPort::write(byte value, EmuTime::param time)
48 {
49  if (lastValue != value) writeDirect(value, time);
50 }
51 void JoystickPort::writeDirect(byte value, EmuTime::param time)
52 {
53  lastValue = value;
54  getPluggedJoyDev().write(value, time);
55 }
56 
57 template<typename Archive>
58 void JoystickPort::serialize(Archive& ar, unsigned /*version*/)
59 {
60  ar.template serializeBase<Connector>(*this);
61  if (ar.isLoader()) {
62  // The value of 'lastValue', is already restored via MSXPSG,
63  // but we still need to re-write this value to the plugged
64  // devices (do this after those devices have been re-plugged).
65  writeDirect(lastValue, getPluggingController().getCurrentTime());
66  }
67 }
69 
70 
71 // class DummyJoystickPort
72 
73 byte DummyJoystickPort::read(EmuTime::param /*time*/)
74 {
75  return 0x3F; // do as-if nothing is connected
76 }
77 
78 void DummyJoystickPort::write(byte /*value*/, EmuTime::param /*time*/)
79 {
80  // ignore writes
81 }
82 
83 } // namespace openmsx
Represents something you can plug devices into.
Definition: Connector.hh:20
string_view getClass() const override
A Connector belong to a certain class.
Definition: JoystickPort.cc:26
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
STL namespace.
Central administration of Connectors and Pluggables.
PluggingController & getPluggingController() const
Definition: Connector.hh:61
byte read(EmuTime::param time) override
Definition: JoystickPort.cc:73
JoystickDevice & getPluggedJoyDev() const
Definition: JoystickPort.cc:31
void write(byte value, EmuTime::param time) override
Definition: JoystickPort.cc:78
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
string_view getDescription() const override
Get a description for this connector.
Definition: JoystickPort.cc:21
void serialize(Archive &ar, unsigned version)
Definition: JoystickPort.cc:58
virtual byte read(EmuTime::param time)=0
Read from the joystick device.
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:1006
void write(byte value, EmuTime::param time) override
Definition: JoystickPort.cc:47
virtual void write(byte value, EmuTime::param time)=0
Write a value to the joystick device.
byte read(EmuTime::param time) override
Definition: JoystickPort.cc:42
Pluggable & getPlugged() const
Returns the Pluggable currently plugged in.
Definition: Connector.hh:59
JoystickPort(PluggingController &pluggingController, std::string name, std::string description)
Definition: JoystickPort.cc:13
void plug(Pluggable &device, EmuTime::param time) override
This plugs a Pluggable in this Connector.
Definition: JoystickPort.cc:36
virtual void plug(Pluggable &device, EmuTime::param time)
This plugs a Pluggable in this Connector.
Definition: Connector.cc:25