openMSX
JoyTap.cc
Go to the documentation of this file.
1 #include "JoyTap.hh"
2 #include "JoystickPort.hh"
3 #include "PluggingController.hh"
4 #include "serialize.hh"
5 #include "memory.hh"
6 #include "strCat.hh"
7 
8 namespace openmsx {
9 
10 using std::string;
11 
12 JoyTap::JoyTap(PluggingController& pluggingController_, string name_)
13  : pluggingController(pluggingController_)
14  , name(std::move(name_))
15 {
16 }
17 
18 JoyTap::~JoyTap() = default;
19 
20 void JoyTap::createPorts(const string& baseDescription) {
21  for (int i = 0; i < 4; ++i) {
22  slaves[i] = make_unique<JoystickPort>(
24  strCat(name, "_port_", char('1' + i)),
25  strCat(baseDescription, char('1' + i)));
26  }
27 }
28 
30 {
31  return "MSX Joy Tap device";
32 }
33 
34 const std::string& JoyTap::getName() const
35 {
36  return name;
37 }
38 
39 void JoyTap::plugHelper(Connector& /*connector*/, EmuTime::param /*time*/)
40 {
41  createPorts("Joy Tap port ");
42 }
43 
44 void JoyTap::unplugHelper(EmuTime::param time)
45 {
46  for (auto& s : slaves) {
47  s->unplug(time);
48  s.reset();
49  }
50 }
51 
52 byte JoyTap::read(EmuTime::param time)
53 {
54  byte value = 255;
55  for (auto& s : slaves) {
56  value &= s->read(time);
57  }
58  return value;
59 }
60 
61 void JoyTap::write(byte value, EmuTime::param time)
62 {
63  for (auto& s : slaves) {
64  s->write(value, time);
65  }
66 }
67 
68 template<typename Archive>
69 void JoyTap::serialize(Archive& ar, unsigned /*version*/)
70 {
71  // saving only happens when plugged in
72  if (!ar.isLoader()) assert(isPluggedIn());
73  // restore plugged state when loading
74  if (ar.isLoader()) {
76  }
77 
78  char tag[6] = { 'p', 'o', 'r', 't', 'X', 0 };
79  for (int i = 0; i < 4; ++i) {
80  tag[4] = char('0' + i);
81  ar.serialize(tag, *slaves[i]);
82  }
83 }
86 
87 } // namespace openmsx
This device is pluged in into the joyports and consolidates several other joysticks plugged into it...
Definition: JoyTap.hh:20
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: JoyTap.cc:39
void serialize(Archive &ar, unsigned version)
Definition: JoyTap.cc:69
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
Represents something you can plug devices into.
Definition: Connector.hh:20
Connector * getConnector() const
Get the connector this Pluggable is plugged into.
Definition: Pluggable.hh:43
virtual ~JoyTap()
bool isPluggedIn() const
Returns true if this pluggable is currently plugged into a connector.
Definition: Pluggable.hh:49
void createPorts(const std::string &baseDescription)
Definition: JoyTap.cc:20
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
STL namespace.
PluggingController & pluggingController
Definition: JoyTap.hh:43
Central administration of Connectors and Pluggables.
EmuTime::param getCurrentTime() const
Convenience method: get current time.
void unplugHelper(EmuTime::param time) override
Definition: JoyTap.cc:44
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
std::unique_ptr< JoystickPort > slaves[4]
Definition: JoyTap.hh:42
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:15
void write(byte value, EmuTime::param time) override
Write a value to the joystick device.
Definition: JoyTap.cc:61
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
std::string strCat(Ts &&...ts)
Definition: strCat.hh:577
string_view getDescription() const override
Description for this pluggable.
Definition: JoyTap.cc:29
JoyTap(PluggingController &pluggingController, std::string name)
Definition: JoyTap.cc:12
const std::string & getName() const override
Name used to identify this pluggable.
Definition: JoyTap.cc:34
byte read(EmuTime::param time) override
Read from the joystick device.
Definition: JoyTap.cc:52