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