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