openMSX
MegaSCSI.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  *
6  * Specification:
7  * SRAM(MegaROM) controller: ASCII8 type
8  * SRAM capacity : 128, 256, 512 and 1024KB
9  * SCSI Protocol Controller: Fujitsu MB89352A
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  * MEGA-SCSI Bank Map:
22  * BANK 00H-3FH (sram read only. mirror of 80H-BFH)
23  * BANK 40H-7EH (mirror of 7FH. Use is prohibited)
24  * BANK 7FH (SPC)
25  * BANK 80H-FFH (sram write and read)
26  *
27  * SPC Bank:
28  * 0x0000 - 0x0FFF :
29  * SPC Data register r/w (mirror of all 0x1FFA)
30  * 0x1000 - 0x1FEF :
31  * mirror of 0x1FF0 - 0x1FFF
32  * Use is prohibited about the image
33  * 0x1FF0 - 0x1FFE :
34  * SPC register
35  * 0x1FFF :
36  * un mapped
37  *
38  * Note:
39  * It is possible to access it by putting it out to 8000H - BFFFH
40  * though the SPC bank is arranged in chiefly 4000H-5FFF.
41  */
42 
43 #include "MegaSCSI.hh"
44 #include "MSXException.hh"
45 #include "serialize.hh"
46 #include <cassert>
47 
48 namespace openmsx {
49 
50 static const byte SPC = 0x7F;
51 
52 unsigned MegaSCSI::getSramSize() const
53 {
54  unsigned sramSize = getDeviceConfig().getChildDataAsInt("sramsize", 1024); // size in kb
55  if (sramSize != 1024 && sramSize != 512 && sramSize != 256 && sramSize != 128) {
56  throw MSXException(
57  "SRAM size for ", getName(),
58  " should be 128, 256, 512 or 1024kB and not ",
59  sramSize, "kB!");
60  }
61  return sramSize * 1024; // in bytes
62 }
63 
65  : MSXDevice(config)
66  , mb89352(config)
67  , sram(getName() + " SRAM", getSramSize(), config)
68  , romBlockDebug(*this, mapped, 0x4000, 0x8000, 13)
69  , blockMask((sram.getSize() / 0x2000) - 1)
70 {
71 }
72 
73 void MegaSCSI::reset(EmuTime::param /*time*/)
74 {
75  for (int i = 0; i < 4; ++i) {
76  setSRAM(i, 0);
77  }
78  mb89352.reset(true);
79 }
80 
81 byte MegaSCSI::readMem(word address, EmuTime::param /*time*/)
82 {
83  byte result;
84  if ((0x4000 <= address) && (address < 0xC000)) {
85  unsigned page = (address / 0x2000) - 2;
86  word addr = address & 0x1FFF;
87  if (mapped[page] == SPC) {
88  // SPC read
89  if (addr < 0x1000) {
90  // Data Register
91  result = mb89352.readDREG();
92  } else {
93  result = mb89352.readRegister(addr & 0x0F);
94  }
95  } else {
96  result = sram[0x2000 * mapped[page] + addr];
97  }
98  } else {
99  result = 0xFF;
100  }
101  return result;
102 }
103 
104 byte MegaSCSI::peekMem(word address, EmuTime::param /*time*/) const
105 {
106  if (const byte* cacheline = MegaSCSI::getReadCacheLine(address)) {
107  return *cacheline;
108  } else {
109  address &= 0x1FFF;
110  if (address < 0x1000) {
111  return mb89352.peekDREG();
112  } else {
113  return mb89352.peekRegister(address & 0x0F);
114  }
115  }
116 }
117 
118 const byte* MegaSCSI::getReadCacheLine(word address) const
119 {
120  if ((0x4000 <= address) && (address < 0xC000)) {
121  unsigned page = (address / 0x2000) - 2;
122  address &= 0x1FFF;
123  if (mapped[page] == SPC) {
124  return nullptr;
125  } else {
126  return &sram[0x2000 * mapped[page] + address];
127  }
128  } else {
129  return unmappedRead;
130  }
131 }
132 
133 void MegaSCSI::writeMem(word address, byte value, EmuTime::param /*time*/)
134 {
135  if ((0x6000 <= address) && (address < 0x8000)) {
136  byte region = ((address >> 11) & 3);
137  setSRAM(region, value);
138  } else if ((0x4000 <= address) && (address < 0xC000)) {
139  unsigned page = (address / 0x2000) - 2;
140  address &= 0x1FFF;
141  if (mapped[page] == SPC) {
142  if (address < 0x1000) {
143  mb89352.writeDREG(value);
144  } else {
145  mb89352.writeRegister(address & 0x0F, value);
146  }
147  } else if (isWriteable[page]) {
148  sram.write(0x2000 * mapped[page] + address, value);
149  }
150  }
151 }
152 
154 {
155  if ((0x6000 <= address) && (address < 0x8000)) {
156  return nullptr;
157  } else if ((0x4000 <= address) && (address < 0xC000)) {
158  unsigned page = (address / 0x2000) - 2;
159  if (mapped[page] == SPC) {
160  return nullptr;
161  } else if (isWriteable[page]) {
162  return nullptr;
163  }
164  }
165  return unmappedWrite;
166 }
167 
168 void MegaSCSI::setSRAM(unsigned region, byte block)
169 {
170  invalidateMemCache(region * 0x2000 + 0x4000, 0x2000);
171  assert(region < 4);
172  isWriteable[region] = (block & 0x80) != 0;
173  mapped[region] = ((block & 0xC0) == 0x40) ? 0x7F : (block & blockMask);
174 }
175 
176 template<typename Archive>
177 void MegaSCSI::serialize(Archive& ar, unsigned /*version*/)
178 {
179  ar.serialize("SRAM", sram);
180  ar.serialize("MB89352", mb89352);
181  ar.serialize("isWriteable", isWriteable);
182  ar.serialize("mapped", mapped);
183 }
185 REGISTER_MSXDEVICE(MegaSCSI, "MegaSCSI");
186 
187 } // namespace openmsx
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MegaSCSI.cc:104
byte readDREG()
Definition: MB89352.cc:408
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
void write(unsigned addr, byte value)
Definition: SRAM.cc:67
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MegaSCSI.cc:73
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:374
MegaSCSI(const DeviceConfig &config)
Definition: MegaSCSI.cc:64
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
byte * getWriteCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing...
Definition: MegaSCSI.cc:153
void writeDREG(byte value)
Definition: MB89352.cc:426
const byte * getReadCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading...
Definition: MegaSCSI.cc:118
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: MegaSCSI.cc:81
void writeRegister(byte reg, byte value)
Definition: MB89352.cc:441
byte peekRegister(byte reg) const
Definition: MB89352.cc:713
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
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: MegaSCSI.cc:133
void serialize(Archive &ar, unsigned version)
Definition: MegaSCSI.cc:177
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
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:275
void reset(bool scsireset)
Definition: MB89352.cc:169
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition: MSXDevice.hh:218
byte peekDREG() const
Definition: MB89352.cc:704
void invalidateMemCache(word start, unsigned size)
Invalidate CPU memory-mapping cache.
Definition: MSXDevice.cc:458
byte readRegister(byte reg)
Definition: MB89352.cc:664