openMSX
RomPlain.cc
Go to the documentation of this file.
1#include "RomPlain.hh"
2
3#include "MSXException.hh"
4#include "XMLElement.hh"
5#include "serialize.hh"
6
7#include "one_of.hh"
8#include "unreachable.hh"
9#include "xrange.hh"
10
11#include <array>
12
13namespace openmsx {
14
15// return x inside [start, start+len)
16[[nodiscard]] static constexpr bool isInside(unsigned x, unsigned start, unsigned len)
17{
18 unsigned tmp = x - start;
19 return tmp < len;
20}
21
22[[nodiscard]] static std::string toString(unsigned start, unsigned len)
23{
24 return strCat("[0x", hex_string<4>(start), ", "
25 "0x", hex_string<4>(start + len), ')');
26}
27
28RomPlain::RomPlain(const DeviceConfig& config, Rom&& rom_, RomType type)
29 : Rom8kBBlocks(config, std::move(rom_))
30{
31 unsigned windowBase = 0x0000;
32 unsigned windowSize = 0x10000;
33 if (const auto* mem = config.findChild("mem")) {
34 windowBase = mem->getAttributeValueAsInt("base", 0);
35 windowSize = mem->getAttributeValueAsInt("size", 0);
36 }
37
38 auto romSize_ = rom.size();
39 if ((romSize_ > 0x10000) || (romSize_ & 0x1FFF)) {
40 throw MSXException(rom.getName(),
41 ": invalid rom size: must be smaller than or equal to 64kB "
42 "and must be a multiple of 8kB.");
43 }
44 auto romSize = narrow<unsigned>(romSize_);
45
46 const int start = [&] {
47 switch (type) {
48 case ROM_MIRRORED: return -1;
49 case ROM_NORMAL: return -1;
50 case ROM_MIRRORED0000: return 0x0000;
51 case ROM_MIRRORED4000: return 0x4000;
52 case ROM_MIRRORED8000: return 0x8000;
53 case ROM_MIRROREDC000: return 0xC000;
54 case ROM_NORMAL0000: return 0x0000;
55 case ROM_NORMAL4000: return 0x4000;
56 case ROM_NORMAL8000: return 0x8000;
57 case ROM_NORMALC000: return 0xC000;
58 default: UNREACHABLE;
59 }
60 }();
61 const bool mirrored = type == one_of(ROM_MIRRORED,
64
65 unsigned romBase = (start == -1)
66 ? guessLocation(windowBase, windowSize)
67 : unsigned(start);
68 if ((start == -1) &&
69 (!isInside(romBase, windowBase, windowSize) ||
70 !isInside(romBase + romSize - 1, windowBase, windowSize))) {
71 // ROM must fall inside the boundaries given by the <mem>
72 // tag (this code only looks at one <mem> tag), but only
73 // check when the start address was not explicitly specified
74 throw MSXException(rom.getName(),
75 ": invalid rom position: interval ",
76 toString(romBase, romSize), " must fit in ",
77 toString(windowBase, windowSize), '.');
78 }
79 if (romBase & 0x1FFF) {
80 throw MSXException(rom.getName(),
81 ": invalid rom position: must start at a 8kB boundary.");
82 }
83
84 unsigned firstPage = romBase / 0x2000;
85 unsigned numPages = romSize / 0x2000;
86 for (auto page : xrange(8)) {
87 unsigned romPage = page - firstPage;
88 if (romPage < numPages) {
89 setRom(page, romPage);
90 } else {
91 if (mirrored) {
92 setRom(page, romPage & (numPages - 1));
93 } else {
94 setUnmapped(page);
95 }
96 }
97 }
98 // RomPlain is currently implemented as a subclass of Rom8kBBlocks,
99 // because of that it inherits a 8kB alignment requirement on the
100 // base/size attributes in the <mem> tag in the hardware description.
101 // But for example for the 'Boosted_audio' extension this requirement
102 // is too strict. Except for the calls to setRom() and setUnmapped() in
103 // this constructor, RomPlain doesn't dynamically change the memory
104 // layout. So if we undo the cache-pre-filling stuff done by setRom()
105 // it is OK to relax the alignment requirements again.
106 //
107 // An alternative is to not inherit from Rom8kBBlocks but from MSXRom.
108 // That would simplify the alignment stuff, but OTOH then we have to
109 // reimplement the debuggable stuff. Also the format of savestates will
110 // change (so it would require extra backwards compatibility code).
111 // Therefor, at least for now, I've not chosen this alternative.
113}
114
115void RomPlain::guessHelper(unsigned offset, std::span<int, 3> pages) const
116{
117 if ((rom[offset++] == 'A') && (rom[offset++] =='B')) {
118 for (auto i : xrange(4)) {
119 if (auto addr = rom[offset + 2 * i + 0] +
120 rom[offset + 2 * i + 1] * 256) {
121 unsigned page = (addr >> 14) - (offset >> 14);
122 if (page <= 2) {
123 pages[page]++;
124 }
125 }
126 }
127 }
128}
129
130unsigned RomPlain::guessLocation(unsigned windowBase, unsigned windowSize) const
131{
132 std::array<int, 3> pages = {0, 0, 0};
133
134 // count number of possible routine pointers
135 if (rom.size() >= 0x0010) {
136 guessHelper(0x0000, pages);
137 }
138 if (rom.size() >= 0x4010) {
139 guessHelper(0x4000, pages);
140 }
141
142 // start address must be inside memory window
143 if (!isInside(0x0000, windowBase, windowSize)) pages[0] = 0;
144 if (!isInside(0x4000, windowBase, windowSize)) pages[1] = 0;
145 if (!isInside(0x8000, windowBase, windowSize)) pages[2] = 0;
146
147 // we prefer 0x4000, then 0x000 and then 0x8000
148 if (pages[1] && (pages[1] >= pages[0]) && (pages[1] >= pages[2])) {
149 return 0x4000;
150 } else if (pages[0] && pages[0] >= pages[2]) {
151 return 0x0000;
152 } else if (pages[2]) {
153 return 0x8000;
154 }
155
156 // heuristics didn't work, return start of window
157 return windowBase;
158}
159
161{
162 // Because this mapper has no switchable banks, we can relax the
163 // alignment requirements again. See also the comment at the end of the
164 // constructor.
166}
167
169
170} // namespace openmsx
#define REGISTER_MSXDEVICE(CLASS, NAME)
Definition MSXDevice.hh:354
const XMLElement * findChild(std::string_view name) const
void invalidateDeviceRCache()
Definition MSXDevice.hh:213
virtual unsigned getBaseSizeAlignment() const
The 'base' and 'size' attribute values need to be at least aligned to CacheLine::SIZE.
Definition MSXDevice.cc:396
void setUnmapped(unsigned region)
Select 'unmapped' memory for this region.
Definition RomBlocks.cc:92
void setRom(unsigned region, unsigned block)
Selects a block of the ROM image for reading in a certain region.
Definition RomBlocks.cc:104
unsigned getBaseSizeAlignment() const override
The 'base' and 'size' attribute values need to be at least aligned to CacheLine::SIZE.
Definition RomPlain.cc:160
RomPlain(const DeviceConfig &config, Rom &&rom, RomType type)
Definition RomPlain.cc:28
auto size() const
Definition Rom.hh:36
const std::string & getName() const
Definition Rom.hh:41
This file implemented 3 utility functions:
Definition Autofire.cc:11
std::string toString(const BooleanInput &input)
@ ROM_NORMAL0000
Definition RomTypes.hh:62
@ ROM_MIRRORED8000
Definition RomTypes.hh:50
@ ROM_MIRRORED0000
Definition RomTypes.hh:48
@ ROM_MIRRORED4000
Definition RomTypes.hh:49
@ ROM_NORMALC000
Definition RomTypes.hh:65
@ ROM_NORMAL4000
Definition RomTypes.hh:63
@ ROM_MIRRORED
Definition RomTypes.hh:47
@ ROM_NORMAL8000
Definition RomTypes.hh:64
@ ROM_NORMAL
Definition RomTypes.hh:61
@ ROM_MIRROREDC000
Definition RomTypes.hh:51
STL namespace.
std::string strCat()
Definition strCat.hh:703
#define UNREACHABLE
constexpr auto xrange(T e)
Definition xrange.hh:132