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