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 &bankPtr[address / BANK_SIZE][address & BANK_MASK];
70 }
71 
72 template <unsigned BANK_SIZE>
73 void RomBlocks<BANK_SIZE>::setBank(byte region, const byte* adr, int block)
74 {
75  assert("address passed to setBank() is not serializable" &&
76  ((adr == unmappedRead) ||
77  ((&rom[0] <= adr) && (adr <= &rom[rom.getSize() - 1])) ||
78  (sram && (&(*sram)[0] <= adr) &&
79  (adr <= &(*sram)[sram->getSize() - 1])) ||
80  ((extraMem <= adr) && (adr <= &extraMem[extraSize - 1]))));
81  bankPtr[region] = adr;
82  blockNr[region] = block; // only for debuggable
83  invalidateMemCache(region * BANK_SIZE, BANK_SIZE);
84 }
85 
86 template <unsigned BANK_SIZE>
88 {
89  setBank(region, unmappedRead, 255);
90 }
91 
92 template <unsigned BANK_SIZE>
94 {
95  extraMem = mem;
96  extraSize = size;
97 }
98 
99 template <unsigned BANK_SIZE>
100 void RomBlocks<BANK_SIZE>::setRom(byte region, int block)
101 {
102  // Note: Some cartridges have a number of blocks that is not a power of 2,
103  // for those we have to make an exception for "block < nrBlocks".
104  block = (block < nrBlocks) ? block : block & blockMask;
105  if (block < nrBlocks) {
106  setBank(region, &rom[block * BANK_SIZE], block);
107  } else {
108  setBank(region, unmappedRead, 255);
109  }
110 }
111 
112 // version 1: initial version
113 // version 2: added blockNr
114 template <unsigned BANK_SIZE>
115 template<typename Archive>
116 void RomBlocks<BANK_SIZE>::serialize(Archive& ar, unsigned /*version*/)
117 {
118  // skip MSXRom base class
119  ar.template serializeBase<MSXDevice>(*this);
120 
121  if (sram) ar.serialize("sram", *sram);
122 
123  unsigned offsets[NUM_BANKS];
124  unsigned romSize = rom.getSize();
125  unsigned sramSize = sram ? sram->getSize() : 0;
126  if (ar.isLoader()) {
127  ar.serialize("banks", offsets);
128  for (unsigned i = 0; i < NUM_BANKS; ++i) {
129  if (offsets[i] == unsigned(-1)) {
130  bankPtr[i] = unmappedRead;
131  } else if (offsets[i] < romSize) {
132  bankPtr[i] = &rom[offsets[i]];
133  } else if (offsets[i] < (romSize + sramSize)) {
134  assert(sram);
135  bankPtr[i] = &(*sram)[offsets[i] - romSize];
136  } else if (offsets[i] < (romSize + sramSize + extraSize)) {
137  bankPtr[i] = &extraMem[offsets[i] - romSize - sramSize];
138  } else {
139  // TODO throw
140  UNREACHABLE;
141  }
142  }
143  } else {
144  for (unsigned i = 0; i < NUM_BANKS; ++i) {
145  if (bankPtr[i] == unmappedRead) {
146  offsets[i] = unsigned(-1);
147  } else if ((&rom[0] <= bankPtr[i]) &&
148  (bankPtr[i] <= &rom[romSize - 1])) {
149  offsets[i] = unsigned(bankPtr[i] - &rom[0]);
150  } else if (sram && (&(*sram)[0] <= bankPtr[i]) &&
151  (bankPtr[i] <= &(*sram)[sramSize - 1])) {
152  offsets[i] = unsigned(bankPtr[i] - &(*sram)[0] + romSize);
153  } else if ((extraMem <= bankPtr[i]) &&
154  (bankPtr[i] <= &extraMem[extraSize - 1])) {
155  offsets[i] = unsigned(bankPtr[i] - extraMem + romSize + sramSize);
156  } else {
157  UNREACHABLE;
158  }
159  }
160  ar.serialize("banks", offsets);
161  }
162 
163  // Commented out because versioning doesn't work correct on subclasses
164  // that don't override the serialize() method (e.g. RomPlain)
165  /*if (ar.versionAtLeast(version, 2)) {
166  ar.serialize("blockNr", blockNr);
167  } else {
168  assert(ar.isLoader());
169  // set dummy value, anyway only used for debuggable
170  for (unsigned i = 0; i < NUM_BANKS; ++i) {
171  blockNr[i] = 255;
172  }
173  }*/
174 }
175 
176 template class RomBlocks<0x1000>;
177 template class RomBlocks<0x2000>;
178 template class RomBlocks<0x4000>;
182 
183 } // namespace openmsx
void setRom(byte region, int block)
Selects a block of the ROM image for reading in a certain region.
Definition: RomBlocks.cc:100
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:67
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:73
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
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:61
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
const byte * bankPtr[NUM_BANKS]
Definition: RomBlocks.hh:80
byte blockNr[NUM_BANKS]
Definition: RomBlocks.hh:82
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:87
void serialize(Archive &ar, unsigned version)
Definition: RomBlocks.cc:116
std::unique_ptr< SRAM > sram
Definition: RomBlocks.hh:81
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:93
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