openMSX
JoyTap.cc
Go to the documentation of this file.
1 #include "JoyTap.hh"
2 #include "PluggingController.hh"
3 #include "enumerate.hh"
4 #include "serialize.hh"
5 #include "strCat.hh"
6 #include <memory>
7 
8 namespace openmsx {
9 
10 JoyTap::JoyTap(PluggingController& pluggingController_, std::string name_)
11  : pluggingController(pluggingController_)
12  , name(std::move(name_))
13 {
14 }
15 
17 {
18  for (auto [i, slave] : enumerate(slaves)) {
19  slave.emplace(
21  strCat(name, "_port_", char('1' + i)),
22  description);
23  }
24 }
25 
26 std::string_view JoyTap::getDescription() const
27 {
28  return "MSX Joy Tap device";
29 }
30 
31 std::string_view JoyTap::getName() const
32 {
33  return name;
34 }
35 
36 void JoyTap::plugHelper(Connector& /*connector*/, EmuTime::param /*time*/)
37 {
38  createPorts("Joy Tap port");
39 }
40 
41 void JoyTap::unplugHelper(EmuTime::param time)
42 {
43  for (auto& s : slaves) {
44  s->unplug(time);
45  s.reset();
46  }
47 }
48 
49 byte JoyTap::read(EmuTime::param time)
50 {
51  byte value = 255;
52  for (auto& s : slaves) {
53  value &= s->read(time);
54  }
55  return value;
56 }
57 
58 void JoyTap::write(byte value, EmuTime::param time)
59 {
60  for (auto& s : slaves) {
61  s->write(value, time);
62  }
63 }
64 
65 template<typename Archive>
66 void JoyTap::serialize(Archive& ar, unsigned /*version*/)
67 {
68  // saving only happens when plugged in
69  if constexpr (!Archive::IS_LOADER) assert(isPluggedIn());
70  // restore plugged state when loading
71  if constexpr (Archive::IS_LOADER) {
73  }
74 
75  char tag[6] = { 'p', 'o', 'r', 't', 'X', 0 };
76  for (auto [i, slave] : enumerate(slaves)) {
77  tag[4] = char('0' + i);
78  ar.serialize(tag, *slave);
79  }
80 }
83 
84 } // namespace openmsx
Represents something you can plug devices into.
Definition: Connector.hh:21
This device is plugged in into the joyports and consolidates several other joysticks plugged into it.
Definition: JoyTap.hh:22
void unplugHelper(EmuTime::param time) override
Definition: JoyTap.cc:41
byte read(EmuTime::param time) override
Read from the joystick device.
Definition: JoyTap.cc:49
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: JoyTap.cc:36
std::string_view getDescription() const override
Description for this pluggable.
Definition: JoyTap.cc:26
void serialize(Archive &ar, unsigned version)
Definition: JoyTap.cc:66
void createPorts(static_string_view description)
Definition: JoyTap.cc:16
void write(byte value, EmuTime::param time) override
Write a value to the joystick device.
Definition: JoyTap.cc:58
std::optional< JoystickPort > slaves[4]
Definition: JoyTap.hh:43
JoyTap(PluggingController &pluggingController, std::string name)
Definition: JoyTap.cc:10
std::string_view getName() const override
Name used to identify this pluggable.
Definition: JoyTap.cc:31
PluggingController & pluggingController
Definition: JoyTap.hh:44
bool isPluggedIn() const
Returns true if this pluggable is currently plugged into a connector.
Definition: Pluggable.hh:49
Connector * getConnector() const
Get the connector this Pluggable is plugged into.
Definition: Pluggable.hh:43
Central administration of Connectors and Pluggables.
EmuTime::param getCurrentTime() const
Convenience method: get current time.
static_string_view
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition: enumerate.hh:28
This file implemented 3 utility functions:
Definition: Autofire.cc:9
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:998
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591