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