29 auto result = std::make_unique<HardwareConfig>(
30 motherBoard, std::move(machineName));
32 result->load(
"machines");
37 MSXMotherBoard& motherBoard, std::string extensionName, std::string_view slotName)
39 auto result = std::make_unique<HardwareConfig>(
40 motherBoard, std::move(extensionName));
41 result->load(
"extensions");
42 result->setName(result->hwName);
44 result->setSlot(slotName);
50 std::string_view slotName, std::span<const TclObject> options)
52 auto result = std::make_unique<HardwareConfig>(motherBoard,
"rom");
53 result->setName(romFile);
56 std::vector<std::string_view> ipsFiles;
65 throw MSXException(
"Invalid option \"", args.front().getString(),
'\"');
70 for (
const auto& ips : ipsFiles) {
79 throw MSXException(
"Invalid ROM file: ", resolvedFilename);
108 auto* devices = extension->setFirstChild(doc.
allocateElement(
"devices"));
109 auto* primary = devices->setFirstChild(doc.
allocateElement(
"primary"));
112 auto* secondary = primary->setFirstChild(doc.
allocateElement(
"secondary"));
121 auto* mapperType = sound->setNextSibling(doc.
allocateElement(
"mappertype"));
122 mapperType->setData(mapper.empty() ?
"auto" : doc.
allocateString(mapper));
123 auto* sramName = mapperType->setNextSibling(doc.
allocateElement(
"sramname"));
126 auto* rfName = rom->setFirstChild(doc.
allocateElement(
"resolvedFilename"));
130 if (!ipsFiles.empty()) {
137 result->setConfig(extension);
138 result->setFileContext(std::move(context));
145 assert(type ==
Type::ROM &&
"should only be used on ROM extensions");
147 const auto* p = d->findChild(
"primary"); assert(p);
148 const auto* s = p->findChild(
"secondary"); assert(s);
149 const auto*
R = s->findChild(
"ROM"); assert(
R);
150 const auto* r =
R->findChild(
"rom"); assert(r);
151 const auto* f = r->findChild(
"filename"); assert(f);
156 : motherBoard(motherBoard_)
157 , hwName(
std::move(hwName_))
160 for (
auto& sub : externalSlots) {
176 std::cerr << e.getMessage() <<
'\n';
180 while (!devices.empty()) {
185 for (
auto ps :
xrange(4)) {
186 for (
auto ss :
xrange(4)) {
187 if (externalSlots[ps][ss]) {
191 if (externalPrimSlots[ps]) {
192 slotManager.removeExternalSlot(ps);
194 if (expandedSlots[ps]) {
197 if (allocatedPrimarySlots[ps]) {
198 slotManager.freePrimarySlot(ps, *
this);
205 auto et = devices.end();
206 for (
auto rit = devices.rbegin(), ret = devices.rend();
208#ifdef _LIBCPP_VERSION
212 std::span alreadyRemoved(std::to_address(rit.base()), et - rit.base());
214 std::span alreadyRemoved{rit.base(), et};
216 (*rit)->testRemove(alreadyRemoved);
220 for (
auto ps :
xrange(4)) {
221 for (
auto ss :
xrange(4)) {
222 if (externalSlots[ps][ss]) {
226 if (externalPrimSlots[ps]) {
227 slotManager.testRemoveExternalSlot(ps, *
this);
229 if (expandedSlots[ps]) {
241static void loadHelper(
XMLDocument& doc,
const std::string& filename)
244 doc.
load(filename,
"msxconfig2.dtd");
245 }
catch (XMLException& e) {
247 "Loading of hardware configuration failed: ",
252static std::string getFilename(std::string_view type, std::string_view name)
258 type,
'/', name,
".xml"));
259 }
catch (MSXException& e) {
264 type,
'/', name,
"/hardwareconfig.xml"));
265 }
catch (MSXException&) {
273 loadHelper(doc, getFilename(type, name));
276void HardwareConfig::load(std::string_view type_)
278 std::string filename = getFilename(type_, hwName);
279 loadHelper(config, filename);
281 assert(!userName.empty());
293 const auto& primSlot = psElem->getAttribute(
"slot");
295 if (psElem->getAttributeValueAsBool(
"external",
false)) {
298 "Cannot mark unspecified primary slot '",
299 primSlot.getValue(),
"' as external");
301 if (psElem->hasChildren()) {
304 " is marked as external, but that would only "
305 "make sense if its <primary> tag would be "
308 createExternalSlot(ps);
311 for (
const auto* ssElem : psElem->getChildren(
"secondary")) {
312 auto secSlot = ssElem->getAttributeValue(
"slot");
314 if ((-16 <= ss) && (ss <= -1) && (ss != ps)) {
316 "Invalid secondary slot specification: \"",
320 if ((ss >= -128) && (0 <= ps) && (ps < 4) &&
329 ps = getAnyFreePrimarySlot();
331 ps = getSpecificFreePrimarySlot(-ps - 1);
333 auto& mutablePrimSlot =
const_cast<XMLAttribute&
>(primSlot);
336 createExpandedSlot(ps);
337 if (ssElem->getAttributeValueAsBool(
"external",
false)) {
338 if (ssElem->hasChildren()) {
340 "Secondary slot ", ps,
'-', ss,
341 " is marked as external, but that would "
342 "only make sense if its <secondary> tag "
345 createExternalSlot(ps, ss);
353 byte initialPrimarySlots = 0;
354 if (
const auto* slotMap =
getConfig().findChild(
"slotmap")) {
355 for (
const auto* child : slotMap->getChildren(
"map")) {
356 int page = child->getAttributeValueAsInt(
"page", -1);
357 if (page < 0 || page > 3) {
358 throw MSXException(
"Invalid or missing page in slotmap entry");
360 int slot = child->getAttributeValueAsInt(
"slot", -1);
361 if (slot < 0 || slot > 3) {
362 throw MSXException(
"Invalid or missing slot in slotmap entry");
364 unsigned offset = page * 2;
365 initialPrimarySlots &=
byte(~(3 << offset));
366 initialPrimarySlots |=
byte(slot << offset);
369 return initialPrimarySlots;
380 for (
const auto& c : elem.getChildren()) {
381 const auto& childName = c.getName();
382 if (childName ==
"primary") {
384 }
else if (childName ==
"secondary") {
388 DeviceConfig(*
this, c, primary, secondary));
390 addDevice(std::move(device));
398void HardwareConfig::createExternalSlot(
int ps)
401 assert(!externalPrimSlots[ps]);
402 externalPrimSlots[ps] =
true;
405void HardwareConfig::createExternalSlot(
int ps,
int ss)
408 assert(!externalSlots[ps][ss]);
409 externalSlots[ps][ss] =
true;
412void HardwareConfig::createExpandedSlot(
int ps)
414 if (!expandedSlots[ps]) {
416 expandedSlots[ps] =
true;
420int HardwareConfig::getAnyFreePrimarySlot()
423 assert(!allocatedPrimarySlots[ps]);
424 allocatedPrimarySlots[ps] =
true;
428int HardwareConfig::getSpecificFreePrimarySlot(
unsigned slot)
431 assert(!allocatedPrimarySlots[ps]);
432 allocatedPrimarySlots[ps] =
true;
436void HardwareConfig::addDevice(std::unique_ptr<MSXDevice> device)
439 devices.push_back(std::move(device));
442void HardwareConfig::setName(std::string_view proposedName)
449 name =
strCat(proposedName,
" (", ++n,
')');
454void HardwareConfig::setSlot(std::string_view slotName)
456 for (
const auto* psElem :
getDevicesElem().getChildren(
"primary")) {
457 const auto& primSlot = psElem->getAttribute(
"slot");
458 if (primSlot.getValue() ==
"any") {
459 auto& mutablePrimSlot =
const_cast<XMLAttribute&
>(primSlot);
465static constexpr std::initializer_list<enum_string<HardwareConfig::Type>> configTypeInfo = {
478template<
typename Archive>
486 if (ar.versionAtLeast(version, 6)) {
487 ar.serialize(
"config", config);
490 ar.serialize(
"config", elem);
493 if (ar.versionAtLeast(version, 2)) {
494 if (ar.versionAtLeast(version, 4)) {
495 ar.serialize(
"context", context);
497 std::unique_ptr<FileContext> ctxt;
498 ar.serialize(
"context", ctxt);
499 if (ctxt) context = *ctxt;
506 if constexpr (Archive::IS_LOADER) {
517 for (
auto& d : devices) {
518 ar.serializePolymorphic(
"device", *d);
520 ar.serialize(
"name", name);
521 if (ar.versionAtLeast(version, 5)) {
522 ar.serialize(
"type", type);
524 assert(Archive::IS_LOADER);
static int getSlotNum(std::string_view slot)
void createExternalSlot(int ps)
void testRemoveExternalSlot(int ps, const HardwareConfig &allowed) const
void removeExternalSlot(int ps)
int allocateAnyPrimarySlot(const HardwareConfig &hwConfig)
int allocateSpecificPrimarySlot(unsigned slot, const HardwareConfig &hwConfig)
virtual Interpreter & getInterpreter()=0
static std::unique_ptr< MSXDevice > create(const DeviceConfig &conf)
std::string resolve(std::string_view filename) const
HardwareConfig(const HardwareConfig &)=delete
std::string_view getRomFilename() const
static std::unique_ptr< HardwareConfig > createRomConfig(MSXMotherBoard &motherBoard, std::string_view romFile, std::string_view slotName, std::span< const TclObject > options)
static std::unique_ptr< HardwareConfig > createMachineConfig(MSXMotherBoard &motherBoard, std::string machineName)
void serialize(Archive &ar, unsigned version)
void setFileContext(FileContext &&ctxt)
const XMLElement & getConfig() const
byte parseSlotMap() const
Parses a slot mapping.
static void loadConfig(XMLDocument &doc, std::string_view type, std::string_view name)
static std::unique_ptr< HardwareConfig > createExtensionConfig(MSXMotherBoard &motherBoard, std::string extensionName, std::string_view slotName)
void testRemove() const
Checks whether this HardwareConfig can be deleted.
const XMLElement & getDevicesElem() const
void testUnsetExpanded(int ps, std::span< const std::unique_ptr< MSXDevice > > allowed) const
void unsetExpanded(int ps)
bool isExpanded(int ps) const
HardwareConfig * findExtension(std::string_view extensionName)
void setMachineConfig(HardwareConfig *machineConfig)
void freeUserName(const std::string &hwName, const std::string &userName)
const HardwareConfig * getMachineConfig() const
MSXCPUInterface & getCPUInterface()
std::string getUserName(const std::string &hwName)
Keep track of which 'usernames' are in use.
CommandController & getCommandController()
CartridgeSlotManager & getSlotManager()
void removeDevice(MSXDevice &device)
void addDevice(MSXDevice &device)
All MSXDevices should be registered by the MotherBoard.
void setValue(const char *value_)
void generateList(XMLElement &parent, const char *itemName, Range &&range, UnaryOp op)
const char * allocateString(std::string_view str)
XMLElement * allocateElement(const char *name)
XMLAttribute * allocateAttribute(const char *name, const char *value)
void load(const std::string &filename, std::string_view systemID)
XMLElement * setData(const char *data_)
const XMLElement * findChild(std::string_view childName) const
const XMLElement & getChild(std::string_view childName) const
ChildRange getChildren() const
std::optional< Context > context
string_view getDirName(string_view path)
Returns the directory portion of a path.
bool isRegularFile(const Stat &st)
string getAbsolutePath(string_view path)
Transform given path into an absolute path.
string_view getFilename(string_view path)
Returns the file portion of a path name.
This file implemented 3 utility functions:
const FileContext & systemFileContext()
uint8_t byte
8 bit unsigned integer
FileContext configFileContext(string_view path, string_view hwDescr, string_view userName)
ArgsInfo valueArg(std::string_view name, T &value)
std::vector< TclObject > parseTclArgs(Interpreter &interp, std::span< const TclObject > inArgs, std::span< const ArgsInfo > table)
const FileContext & userFileContext()
constexpr void fill(ForwardRange &&range, const T &value)
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
#define SERIALIZE_ENUM(TYPE, INFO)
TemporaryString tmpStrCat(Ts &&... ts)
static std::unique_ptr< FileContext > getLastSerializedFileContext()
constexpr auto xrange(T e)