openMSX
ESE_RAM.cc
Go to the documentation of this file.
1 /*
2  * MEGA-SCSI and ESE-RAM cartridge:
3  * The mapping does SRAM and MB89352A(MEGA-SCSI) to ASCII8 or
4  * an interchangeable bank controller.
5  * The rest of this documentation is only about ESE-RAM specifically.
6  *
7  * Specification:
8  * SRAM(MegaROM) controller: ASCII8 type
9  * SRAM capacity : 128, 256, 512 and 1024KB
10  *
11  * Bank changing address:
12  * bank 4(0x4000-0x5fff): 0x6000 - 0x67FF (0x6000 used)
13  * bank 6(0x6000-0x7fff): 0x6800 - 0x6FFF (0x6800 used)
14  * bank 8(0x8000-0x9fff): 0x7000 - 0x77FF (0x7000 used)
15  * bank A(0xa000-0xbfff): 0x7800 - 0x7FFF (0x7800 used)
16  *
17  * ESE-RAM Bank Map:
18  * BANK 00H-7FH (read only)
19  * BANK 80H-FFH (write and read. mirror of 00H-7FH)
20  */
21 
22 #include "ESE_RAM.hh"
23 #include "MSXException.hh"
24 #include "serialize.hh"
25 #include <cassert>
26 
27 namespace openmsx {
28 
29 unsigned ESE_RAM::getSramSize() const
30 {
31  unsigned sramSize = getDeviceConfig().getChildDataAsInt("sramsize", 256); // size in kb
32  if (sramSize != 1024 && sramSize != 512 && sramSize != 256 && sramSize != 128) {
33  throw MSXException(
34  "SRAM size for ", getName(),
35  " should be 128, 256, 512 or 1024kB and not ",
36  sramSize, "kB!");
37  }
38  return sramSize * 1024; // in bytes
39 }
40 
42  : MSXDevice(config)
43  , sram(getName() + " SRAM", getSramSize(), config)
44  , romBlockDebug(*this, mapped, 0x4000, 0x8000, 13)
45  , blockMask((sram.getSize() / 8192) - 1)
46 {
47  reset(EmuTime::dummy());
48 }
49 
50 void ESE_RAM::reset(EmuTime::param /*time*/)
51 {
52  for (int i = 0; i < 4; ++i) {
53  setSRAM(i, 0);
54  }
55 }
56 
57 byte ESE_RAM::readMem(word address, EmuTime::param /*time*/)
58 {
59  byte result;
60  if ((0x4000 <= address) && (address < 0xC000)) {
61  unsigned page = (address / 8192) - 2;
62  word addr = address & 0x1FFF;
63  result = sram[8192 * mapped[page] + addr];
64  } else {
65  result = 0xFF;
66  }
67  return result;
68 }
69 
70 const byte* ESE_RAM::getReadCacheLine(word address) const
71 {
72  if ((0x4000 <= address) && (address < 0xC000)) {
73  unsigned page = (address / 8192) - 2;
74  address &= 0x1FFF;
75  return &sram[8192 * mapped[page] + address];
76  } else {
77  return unmappedRead;
78  }
79 }
80 
81 void ESE_RAM::writeMem(word address, byte value, EmuTime::param /*time*/)
82 {
83  if ((0x6000 <= address) && (address < 0x8000)) {
84  byte region = ((address >> 11) & 3);
85  setSRAM(region, value);
86  } else if ((0x4000 <= address) && (address < 0xC000)) {
87  unsigned page = (address / 8192) - 2;
88  address &= 0x1FFF;
89  if (isWriteable[page]) {
90  sram.write(8192 * mapped[page] + address, value);
91  }
92  }
93 }
94 
96 {
97  if ((0x6000 <= address) && (address < 0x8000)) {
98  return nullptr;
99  } else if ((0x4000 <= address) && (address < 0xC000)) {
100  unsigned page = (address / 8192) - 2;
101  if (isWriteable[page]) {
102  return nullptr;
103  }
104  }
105  return unmappedWrite;
106 }
107 
108 void ESE_RAM::setSRAM(unsigned region, byte block)
109 {
110  invalidateMemCache(region * 0x2000 + 0x4000, 0x2000);
111  assert(region < 4);
112  isWriteable[region] = (block & 0x80) != 0;
113  mapped[region] = block & blockMask;
114 }
115 
116 template<typename Archive>
117 void ESE_RAM::serialize(Archive& ar, unsigned /*version*/)
118 {
119  ar.template serializeBase<MSXDevice>(*this);
120  ar.serialize("SRAM", sram);
121  ar.serialize("isWriteable", isWriteable);
122  ar.serialize("mapped", mapped);
123 }
125 REGISTER_MSXDEVICE(ESE_RAM, "ESE_RAM");
126 
127 } // namespace openmsx
void serialize(Archive &ar, unsigned version)
Definition: ESE_RAM.cc:117
void writeMem(word address, byte value, EmuTime::param time) override
Write a given byte to a given location at a certain time to this device.
Definition: ESE_RAM.cc:81
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
void write(unsigned addr, byte value)
Definition: SRAM.cc:67
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:374
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX...
Definition: MSXDevice.hh:31
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:274
int getChildDataAsInt(string_view name, int defaultValue=0) const
Definition: XMLElement.cc:206
ESE_RAM(const DeviceConfig &config)
Definition: ESE_RAM.cc:41
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:275
byte * getWriteCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing...
Definition: ESE_RAM.cc:95
void reset(EmuTime::param time) override
This method is called on reset.
Definition: ESE_RAM.cc:50
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition: MSXDevice.hh:218
void invalidateMemCache(word start, unsigned size)
Invalidate CPU memory-mapping cache.
Definition: MSXDevice.cc:458
const byte * getReadCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading...
Definition: ESE_RAM.cc:70
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: ESE_RAM.cc:57