openMSX
JoyTap.cc
Go to the documentation of this file.
1#include "JoyTap.hh"
3#include "enumerate.hh"
4#include "serialize.hh"
5#include "strCat.hh"
6#include <array>
7#include <memory>
8
9namespace openmsx {
10
11JoyTap::JoyTap(PluggingController& pluggingController_, std::string name_)
12 : pluggingController(pluggingController_)
13 , name(std::move(name_))
14{
15}
16
17void JoyTap::createPorts(std::string_view description, EmuTime::param time)
18{
19 for (auto [i, slave] : enumerate(slaves)) {
20 slave.emplace(
22 strCat(name, "_port_", char('1' + i)),
23 strCat(description, ' ', char('1' + i)));
24 slave->write(0, time);
25 }
26}
27
28std::string_view JoyTap::getDescription() const
29{
30 return "MSX Joy Tap device";
31}
32
33std::string_view JoyTap::getName() const
34{
35 return name;
36}
37
38void JoyTap::plugHelper(Connector& /*connector*/, EmuTime::param time)
39{
40 createPorts("Joy Tap port", time);
41}
42
43void JoyTap::unplugHelper(EmuTime::param time)
44{
45 for (auto& s : slaves) {
46 s->unplug(time);
47 s.reset();
48 }
49}
50
51uint8_t JoyTap::read(EmuTime::param time)
52{
53 uint8_t value = 255;
54 for (auto& s : slaves) {
55 value &= s->read(time);
56 }
57 return value;
58}
59
60void JoyTap::write(uint8_t value, EmuTime::param time)
61{
62 for (auto& s : slaves) {
63 s->write(value, time);
64 }
65}
66
67template<typename Archive>
68void JoyTap::serialize(Archive& ar, unsigned /*version*/)
69{
70 // saving only happens when plugged in
71 if constexpr (!Archive::IS_LOADER) assert(isPluggedIn());
72 // restore plugged state when loading
73 if constexpr (Archive::IS_LOADER) {
75 }
76
77 std::array<char, 6> tag = {'p', 'o', 'r', 't', 'X', 0};
78 for (auto [i, slave] : enumerate(slaves)) {
79 tag[4] = char('0' + i);
80 ar.serialize(tag.data(), *slave);
81 }
82}
85
86} // namespace openmsx
Represents something you can plug devices into.
Definition Connector.hh:21
This device is plugged in into the joy-ports and consolidates several other joysticks plugged into it...
Definition JoyTap.hh:23
void unplugHelper(EmuTime::param time) override
Definition JoyTap.cc:43
void plugHelper(Connector &connector, EmuTime::param time) override
Definition JoyTap.cc:38
uint8_t read(EmuTime::param time) override
Read from the joystick device.
Definition JoyTap.cc:51
std::string_view getDescription() const override
Description for this pluggable.
Definition JoyTap.cc:28
void serialize(Archive &ar, unsigned version)
Definition JoyTap.cc:68
void write(uint8_t value, EmuTime::param time) override
Write a value to the joystick device.
Definition JoyTap.cc:60
JoyTap(PluggingController &pluggingController, std::string name)
Definition JoyTap.cc:11
std::string_view getName() const override
Name used to identify this pluggable.
Definition JoyTap.cc:33
void createPorts(std::string_view description, EmuTime::param time)
Definition JoyTap.cc:17
std::array< std::optional< JoystickPort >, 4 > slaves
Definition JoyTap.hh:44
PluggingController & pluggingController
Definition JoyTap.hh:45
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.
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:11
STL namespace.
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
#define REGISTER_POLYMORPHIC_INITIALIZER(BASE, CLASS, NAME)
std::string strCat()
Definition strCat.hh:703