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