openMSX
MSXS1985.cc
Go to the documentation of this file.
1#include "MSXS1985.hh"
2#include "MSXMapperIO.hh"
3#include "MSXMotherBoard.hh"
4#include "SRAM.hh"
5#include "enumerate.hh"
6#include "narrow.hh"
7#include "serialize.hh"
8#include <array>
9#include <memory>
10
11namespace openmsx {
12
13static constexpr byte ID = 0xFE;
14
16 : MSXDevice(config)
17 , MSXSwitchedDevice(getMotherBoard(), ID)
18{
19 if (!config.findChild("sramname")) {
20 // special case for backwards compatibility (S1985 didn't
21 // always have SRAM in its config...)
22 sram = std::make_unique<SRAM>(
23 getName() + " SRAM", "S1985 Backup RAM",
24 0x10, config, SRAM::DontLoadTag{});
25 } else {
26 sram = std::make_unique<SRAM>(
27 getName() + " SRAM", "S1985 Backup RAM",
28 0x10, config);
29 }
30
31 auto& mapperIO = getMotherBoard().createMapperIO();
32 byte mask = 0b0001'1111; // always(?) 5 bits
33 auto baseValue = narrow_cast<byte>(config.getChildDataAsInt("MapperReadBackBaseValue", 0x80));
34 mapperIO.setMode(MSXMapperIO::Mode::INTERNAL, mask, baseValue);
35
36 reset(EmuTime::dummy());
37}
38
43
44void MSXS1985::reset(EmuTime::param /*time*/)
45{
46 color1 = color2 = pattern = address = 0; // TODO check this
47}
48
49byte MSXS1985::readSwitchedIO(word port, EmuTime::param time)
50{
51 byte result = peekSwitchedIO(port, time);
52 switch (port & 0x0F) {
53 case 7:
54 pattern = byte((pattern << 1) | (pattern >> 7));
55 break;
56 }
57 return result;
58}
59
60byte MSXS1985::peekSwitchedIO(word port, EmuTime::param /*time*/) const
61{
62 switch (port & 0x0F) {
63 case 0:
64 return byte(~ID);
65 case 2:
66 return (*sram)[address];
67 case 7:
68 return (pattern & 0x80) ? color2 : color1;
69 default:
70 return 0xFF;
71 }
72}
73
74void MSXS1985::writeSwitchedIO(word port, byte value, EmuTime::param /*time*/)
75{
76 switch (port & 0x0F) {
77 case 1:
78 address = value & 0x0F;
79 break;
80 case 2:
81 sram->write(address, value);
82 break;
83 case 6:
84 color2 = color1;
85 color1 = value;
86 break;
87 case 7:
88 pattern = value;
89 break;
90 }
91}
92
93// version 1: initial version
94// version 2: replaced RAM with SRAM
95template<typename Archive>
96void MSXS1985::serialize(Archive& ar, unsigned version)
97{
98 ar.template serializeBase<MSXDevice>(*this);
99 // no need to serialize MSXSwitchedDevice base class
100
101 if (ar.versionAtLeast(version, 2)) {
102 // serialize normally...
103 ar.serialize("sram", *sram);
104 } else {
105 assert(Archive::IS_LOADER);
106 // version 1 had here
107 // <ram>
108 // <ram encoding="..">...</ram>
109 // </ram>
110 // deserialize that structure and transfer it to SRAM
111 std::array<byte, 0x10> tmp;
112 ar.beginTag("ram");
113 ar.serialize_blob("ram", tmp);
114 ar.endTag("ram");
115 for (auto [i, t] : enumerate(tmp)) {
116 sram->write(unsigned(i), t);
117 }
118 }
119
120 ar.serialize("address", address,
121 "color1", color1,
122 "color2", color2,
123 "pattern", pattern);
124}
127
128} // namespace openmsx
#define REGISTER_MSXDEVICE(CLASS, NAME)
Definition MSXDevice.hh:356
TclObject t
const XMLElement * findChild(std::string_view name) const
int getChildDataAsInt(std::string_view name, int defaultValue) const
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition MSXDevice.hh:36
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition MSXDevice.cc:70
virtual const std::string & getName() const
Returns a human-readable name for this device.
Definition MSXDevice.cc:375
MSXMapperIO & createMapperIO()
All memory mappers in one MSX machine share the same four (logical) memory mapper registers.
byte readSwitchedIO(word port, EmuTime::param time) override
Definition MSXS1985.cc:49
MSXS1985(const DeviceConfig &config)
Definition MSXS1985.cc:15
byte peekSwitchedIO(word port, EmuTime::param time) const override
Definition MSXS1985.cc:60
void reset(EmuTime::param time) override
This method is called on reset.
Definition MSXS1985.cc:44
void serialize(Archive &ar, unsigned version)
Definition MSXS1985.cc:96
void writeSwitchedIO(word port, byte value, EmuTime::param time) override
Definition MSXS1985.cc:74
~MSXS1985() override
Definition MSXS1985.cc:39
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
uint8_t byte
8 bit unsigned integer
Definition openmsx.hh:26
uint16_t word
16 bit unsigned integer
Definition openmsx.hh:29
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)