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 "one_of.hh"
46 #include "serialize.hh"
47 #include <cassert>
48 
49 namespace openmsx {
50 
51 constexpr byte SPC = 0x7F;
52 
53 unsigned MegaSCSI::getSramSize() const
54 {
55  unsigned sramSize = getDeviceConfig().getChildDataAsInt("sramsize", 1024); // size in kb
56  if (sramSize != one_of(1024u, 512u, 256u, 128u)) {
57  throw MSXException(
58  "SRAM size for ", getName(),
59  " should be 128, 256, 512 or 1024kB and not ",
60  sramSize, "kB!");
61  }
62  return sramSize * 1024; // in bytes
63 }
64 
66  : MSXDevice(config)
67  , mb89352(config)
68  , sram(getName() + " SRAM", getSramSize(), config)
69  , romBlockDebug(*this, mapped, 0x4000, 0x8000, 13)
70  , blockMask((sram.getSize() / 0x2000) - 1)
71 {
72 }
73 
74 void MegaSCSI::reset(EmuTime::param /*time*/)
75 {
76  for (int i = 0; i < 4; ++i) {
77  setSRAM(i, 0);
78  }
79  mb89352.reset(true);
80 }
81 
82 byte MegaSCSI::readMem(word address, EmuTime::param /*time*/)
83 {
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  return mb89352.readDREG();
92  } else {
93  return mb89352.readRegister(addr & 0x0F);
94  }
95  } else {
96  return sram[0x2000 * mapped[page] + addr];
97  }
98  } else {
99  return 0xFF;
100  }
101 }
102 
103 byte MegaSCSI::peekMem(word address, EmuTime::param /*time*/) const
104 {
105  if (const byte* cacheline = MegaSCSI::getReadCacheLine(address)) {
106  return *cacheline;
107  } else {
108  address &= 0x1FFF;
109  if (address < 0x1000) {
110  return mb89352.peekDREG();
111  } else {
112  return mb89352.peekRegister(address & 0x0F);
113  }
114  }
115 }
116 
117 const byte* MegaSCSI::getReadCacheLine(word address) const
118 {
119  if ((0x4000 <= address) && (address < 0xC000)) {
120  unsigned page = (address / 0x2000) - 2;
121  address &= 0x1FFF;
122  if (mapped[page] == SPC) {
123  return nullptr;
124  } else {
125  return &sram[0x2000 * mapped[page] + address];
126  }
127  } else {
128  return unmappedRead;
129  }
130 }
131 
132 void MegaSCSI::writeMem(word address, byte value, EmuTime::param /*time*/)
133 {
134  if ((0x6000 <= address) && (address < 0x8000)) {
135  byte region = ((address >> 11) & 3);
136  setSRAM(region, value);
137  } else if ((0x4000 <= address) && (address < 0xC000)) {
138  unsigned page = (address / 0x2000) - 2;
139  address &= 0x1FFF;
140  if (mapped[page] == SPC) {
141  if (address < 0x1000) {
142  mb89352.writeDREG(value);
143  } else {
144  mb89352.writeRegister(address & 0x0F, value);
145  }
146  } else if (isWriteable[page]) {
147  sram.write(0x2000 * mapped[page] + address, value);
148  }
149  }
150 }
151 
152 byte* MegaSCSI::getWriteCacheLine(word address) const
153 {
154  if ((0x6000 <= address) && (address < 0x8000)) {
155  return nullptr;
156  } else if ((0x4000 <= address) && (address < 0xC000)) {
157  unsigned page = (address / 0x2000) - 2;
158  if (mapped[page] == SPC) {
159  return nullptr;
160  } else if (isWriteable[page]) {
161  return nullptr;
162  }
163  }
164  return unmappedWrite;
165 }
166 
167 void MegaSCSI::setSRAM(unsigned region, byte block)
168 {
169  invalidateDeviceRWCache(region * 0x2000 + 0x4000, 0x2000);
170  assert(region < 4);
171  isWriteable[region] = (block & 0x80) != 0;
172  mapped[region] = ((block & 0xC0) == 0x40) ? 0x7F : (block & blockMask);
173 }
174 
175 template<typename Archive>
176 void MegaSCSI::serialize(Archive& ar, unsigned /*version*/)
177 {
178  ar.serialize("SRAM", sram,
179  "MB89352", mb89352,
180  "isWriteable", isWriteable,
181  "mapped", mapped);
182 }
185 
186 } // namespace openmsx
openmsx::MSXDevice
An MSXDevice is an emulated hardware component connected to the bus of the emulated MSX.
Definition: MSXDevice.hh:32
one_of.hh
openmsx::MegaSCSI::MegaSCSI
MegaSCSI(const DeviceConfig &config)
Definition: MegaSCSI.cc:65
openmsx::MegaSCSI::peekMem
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: MegaSCSI.cc:103
serialize.hh
openmsx::MB89352::peekDREG
byte peekDREG() const
Definition: MB89352.cc:704
openmsx::DeviceConfig
Definition: DeviceConfig.hh:20
openmsx::MSXDevice::unmappedWrite
static byte unmappedWrite[0x10000]
Definition: MSXDevice.hh:301
openmsx::MB89352::peekRegister
byte peekRegister(byte reg) const
Definition: MB89352.cc:713
MSXException.hh
openmsx::SPC
constexpr byte SPC
Definition: MegaSCSI.cc:51
openmsx::MSXException
Definition: MSXException.hh:10
openmsx::MB89352::reset
void reset(bool scsireset)
Definition: MB89352.cc:169
openmsx::REGISTER_MSXDEVICE
REGISTER_MSXDEVICE(ChakkariCopy, "ChakkariCopy")
openmsx::MB89352::writeRegister
void writeRegister(byte reg, byte value)
Definition: MB89352.cc:441
openmsx::Keys::getName
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:742
one_of
Definition: one_of.hh:7
openmsx::MegaSCSI::readMem
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: MegaSCSI.cc:82
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:981
openmsx::MB89352::writeDREG
void writeDREG(byte value)
Definition: MB89352.cc:426
openmsx::SRAM::write
void write(unsigned addr, byte value)
Definition: SRAM.cc:67
openmsx::MB89352::readDREG
byte readDREG()
Definition: MB89352.cc:408
openmsx::MSXDevice::invalidateDeviceRWCache
void invalidateDeviceRWCache()
Calls MSXCPUInterface::invalidateXXCache() for the specific (part of) the slot that this device is lo...
Definition: MSXDevice.hh:208
openmsx::MegaSCSI::getWriteCacheLine
byte * getWriteCacheLine(word address) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for writing.
Definition: MegaSCSI.cc:152
openmsx::MegaSCSI::getReadCacheLine
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:117
openmsx::MSXDevice::getName
virtual std::string getName() const
Returns a human-readable name for this device.
Definition: MSXDevice.cc:381
openmsx::MB89352::readRegister
byte readRegister(byte reg)
Definition: MB89352.cc:664
openmsx::MegaSCSI
Definition: MegaSCSI.hh:12
openmsx::MSXDevice::unmappedRead
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:300
openmsx::word
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
MegaSCSI.hh
openmsx::XMLElement::getChildDataAsInt
int getChildDataAsInt(std::string_view childName, int defaultValue=0) const
Definition: XMLElement.cc:185
openmsx::MegaSCSI::writeMem
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:132
openmsx::MegaSCSI::reset
void reset(EmuTime::param time) override
This method is called on reset.
Definition: MegaSCSI.cc:74
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::MegaSCSI::serialize
void serialize(Archive &ar, unsigned version)
Definition: MegaSCSI.cc:176
openmsx::MSXDevice::getDeviceConfig
const XMLElement & getDeviceConfig() const
Get the configuration section for this device.
Definition: MSXDevice.hh:230