openMSX
RomBlocks.cc
Go to the documentation of this file.
1 #include "CliComm.hh"
2 #include "RomBlocks.hh"
3 #include "SRAM.hh"
4 #include "MSXException.hh"
5 #include "Math.hh"
6 #include "StringOp.hh"
7 #include "serialize.hh"
8 #include "unreachable.hh"
9 
10 namespace openmsx {
11 
12 template<bool C, class T, class F> struct if_log2_ : F {};
13 template< class T, class F> struct if_log2_<true, T, F> : T {};
14 template<unsigned A, unsigned R = 0> struct log2
15  : if_log2_<A == 1, std::integral_constant<int, R>, log2<A / 2, R + 1>> {};
16 
17 // minimal attempt to avoid seeing this warning too often
18 static Sha1Sum alreadyWarnedForSha1Sum;
19 
20 template <unsigned BANK_SIZE>
22  const DeviceConfig& config, Rom&& rom_,
23  unsigned debugBankSizeShift)
24  : MSXRom(config, std::move(rom_))
25  , romBlockDebug(
26  *this, blockNr, 0x0000, 0x10000,
27  log2<BANK_SIZE>::value, debugBankSizeShift)
28 {
29  static_assert(Math::isPowerOfTwo(BANK_SIZE), "BANK_SIZE must be a power of two");
30  auto extendedSize = (rom.getSize() + BANK_SIZE - 1) & ~(BANK_SIZE - 1);
31  if (extendedSize != rom.getSize() && alreadyWarnedForSha1Sum != rom.getOriginalSHA1()) {
32  config.getCliComm().printWarning(
33  "(uncompressed) ROM image filesize was not a multiple "
34  "of " + StringOp::toString(BANK_SIZE / 1024) + "kB "
35  "(which is required for mapper type " +
36  config.findChild("mappertype")->getData() + "), so we "
37  "padded it to be correct. But if the ROM you are "
38  "running was just dumped, the dump is probably not "
39  "complete/correct!");
40  alreadyWarnedForSha1Sum = rom.getOriginalSHA1();
41  }
42  rom.addPadding(extendedSize);
43  nrBlocks = rom.getSize() / BANK_SIZE;
44  assert((nrBlocks * BANK_SIZE) == rom.getSize());
45 
46  // by default no extra mappable memory block
47  extraMem = nullptr;
48  extraSize = 0;
49 
50  // Default mask: wraps at end of ROM image.
51  blockMask = nrBlocks - 1;
52  for (unsigned i = 0; i < NUM_BANKS; i++) {
53  setRom(i, 0);
54  }
55 }
56 
57 template <unsigned BANK_SIZE>
59 
60 template <unsigned BANK_SIZE>
62 {
63  return bankPtr[address / BANK_SIZE][address & BANK_MASK];
64 }
65 
66 template <unsigned BANK_SIZE>
68 {
69  return RomBlocks<BANK_SIZE>::peekMem(address, time);
70 }
71 
72 template <unsigned BANK_SIZE>
74 {
75  return &bankPtr[address / BANK_SIZE][address & BANK_MASK];
76 }
77 
78 template <unsigned BANK_SIZE>
79 void RomBlocks<BANK_SIZE>::setBank(byte region, const byte* adr, int block)
80 {
81  assert("address passed to setBank() is not serializable" &&
82  ((adr == unmappedRead) ||
83  ((&rom[0] <= adr) && (adr <= &rom[rom.getSize() - 1])) ||
84  (sram && (&(*sram)[0] <= adr) &&
85  (adr <= &(*sram)[sram->getSize() - 1])) ||
86  ((extraMem <= adr) && (adr <= &extraMem[extraSize - 1]))));
87  bankPtr[region] = adr;
88  blockNr[region] = block; // only for debuggable
89  invalidateMemCache(region * BANK_SIZE, BANK_SIZE);
90 }
91 
92 template <unsigned BANK_SIZE>
94 {
95  setBank(region, unmappedRead, 255);
96 }
97 
98 template <unsigned BANK_SIZE>
100 {
101  extraMem = mem;
102  extraSize = size;
103 }
104 
105 template <unsigned BANK_SIZE>
106 void RomBlocks<BANK_SIZE>::setRom(byte region, int block)
107 {
108  // Note: Some cartridges have a number of blocks that is not a power of 2,
109  // for those we have to make an exception for "block < nrBlocks".
110  block = (block < nrBlocks) ? block : block & blockMask;
111  if (block < nrBlocks) {
112  setBank(region, &rom[block * BANK_SIZE], block);
113  } else {
114  setBank(region, unmappedRead, 255);
115  }
116 }
117 
118 // version 1: initial version
119 // version 2: added blockNr
120 template <unsigned BANK_SIZE>
121 template<typename Archive>
122 void RomBlocks<BANK_SIZE>::serialize(Archive& ar, unsigned /*version*/)
123 {
124  // skip MSXRom base class
125  ar.template serializeBase<MSXDevice>(*this);
126 
127  if (sram) ar.serialize("sram", *sram);
128 
129  unsigned offsets[NUM_BANKS];
130  unsigned romSize = rom.getSize();
131  unsigned sramSize = sram ? sram->getSize() : 0;
132  if (ar.isLoader()) {
133  ar.serialize("banks", offsets);
134  for (unsigned i = 0; i < NUM_BANKS; ++i) {
135  if (offsets[i] == unsigned(-1)) {
136  bankPtr[i] = unmappedRead;
137  } else if (offsets[i] < romSize) {
138  bankPtr[i] = &rom[offsets[i]];
139  } else if (offsets[i] < (romSize + sramSize)) {
140  assert(sram);
141  bankPtr[i] = &(*sram)[offsets[i] - romSize];
142  } else if (offsets[i] < (romSize + sramSize + extraSize)) {
143  bankPtr[i] = &extraMem[offsets[i] - romSize - sramSize];
144  } else {
145  // TODO throw
146  UNREACHABLE;
147  }
148  }
149  } else {
150  for (unsigned i = 0; i < NUM_BANKS; ++i) {
151  if (bankPtr[i] == unmappedRead) {
152  offsets[i] = unsigned(-1);
153  } else if ((&rom[0] <= bankPtr[i]) &&
154  (bankPtr[i] <= &rom[romSize - 1])) {
155  offsets[i] = unsigned(bankPtr[i] - &rom[0]);
156  } else if (sram && (&(*sram)[0] <= bankPtr[i]) &&
157  (bankPtr[i] <= &(*sram)[sramSize - 1])) {
158  offsets[i] = unsigned(bankPtr[i] - &(*sram)[0] + romSize);
159  } else if ((extraMem <= bankPtr[i]) &&
160  (bankPtr[i] <= &extraMem[extraSize - 1])) {
161  offsets[i] = unsigned(bankPtr[i] - extraMem + romSize + sramSize);
162  } else {
163  UNREACHABLE;
164  }
165  }
166  ar.serialize("banks", offsets);
167  }
168 
169  // Commented out because versioning doesn't work correct on subclasses
170  // that don't override the serialize() method (e.g. RomPlain)
171  /*if (ar.versionAtLeast(version, 2)) {
172  ar.serialize("blockNr", blockNr);
173  } else {
174  assert(ar.isLoader());
175  // set dummy value, anyway only used for debuggable
176  for (unsigned i = 0; i < NUM_BANKS; ++i) {
177  blockNr[i] = 255;
178  }
179  }*/
180 }
181 
182 template class RomBlocks<0x1000>;
183 template class RomBlocks<0x2000>;
184 template class RomBlocks<0x4000>;
188 
189 } // namespace openmsx
void setRom(byte region, int block)
Selects a block of the ROM image for reading in a certain region.
Definition: RomBlocks.cc:106
string toString(long long a)
Definition: StringOp.cc:150
void printWarning(string_ref message)
Definition: CliComm.cc:20
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
STL namespace.
const byte * getReadCacheLine(word start) const override
Test that the memory in the interval [start, start + CacheLine::SIZE) is cacheable for reading...
Definition: RomBlocks.cc:73
const XMLElement * findChild(string_ref name) const
Definition: DeviceConfig.cc:61
void setBank(byte region, const byte *adr, int block)
Sets the memory visible for reading in a certain region.
Definition: RomBlocks.cc:79
CliComm & getCliComm() const
Definition: DeviceConfig.cc:18
const std::string & getData() const
Definition: XMLElement.hh:35
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:19
const Sha1Sum & getOriginalSHA1() const
Definition: Rom.cc:358
byte peekMem(word address, EmuTime::param time) const override
Read a byte from a given memory location.
Definition: RomBlocks.cc:61
static const unsigned NUM_BANKS
Definition: RomBlocks.hh:17
byte readMem(word address, EmuTime::param time) override
Read a byte from a location at a certain time from this device.
Definition: RomBlocks.cc:67
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
const byte * bankPtr[NUM_BANKS]
Definition: RomBlocks.hh:81
byte blockNr[NUM_BANKS]
Definition: RomBlocks.hh:83
size_t size() const
unsigned getSize() const
Definition: Rom.hh:32
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
void addPadding(unsigned newSize, byte filler=0xff)
Definition: Rom.cc:366
static byte unmappedRead[0x10000]
Definition: MSXDevice.hh:268
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:840
static const unsigned BANK_SIZE
Definition: RomBlocks.hh:16
void setUnmapped(byte region)
Select &#39;unmapped&#39; memory for this region.
Definition: RomBlocks.cc:93
void serialize(Archive &ar, unsigned version)
Definition: RomBlocks.cc:122
std::unique_ptr< SRAM > sram
Definition: RomBlocks.hh:82
static const unsigned BANK_MASK
Definition: RomBlocks.hh:18
void setExtraMemory(const byte *mem, unsigned size)
Inform this base class of extra mapable memory block.
Definition: RomBlocks.cc:99
RomBlocks(const DeviceConfig &config, Rom &&rom, unsigned debugBankSizeShift=0)
Constructor.
Definition: RomBlocks.cc:21
constexpr bool isPowerOfTwo(unsigned a)
Is the given number an integer power of 2? Not correct for zero (according to this test 0 is a power ...
Definition: Math.hh:20
void invalidateMemCache(word start, unsigned size)
Invalidate CPU memory-mapping cache.
Definition: MSXDevice.cc:452
#define UNREACHABLE
Definition: unreachable.hh:35