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