27 auto result = std::make_unique<HardwareConfig>(
28 motherBoard, std::move(machineName));
30 result->load(
"machines");
35 MSXMotherBoard& motherBoard,
string extensionName, std::string_view slotname)
37 auto result = std::make_unique<HardwareConfig>(
38 motherBoard, std::move(extensionName));
39 result->load(
"extensions");
40 result->setName(result->hwName);
42 result->setSlot(slotname);
48 string slotname, std::span<const TclObject> options)
50 auto result = std::make_unique<HardwareConfig>(motherBoard,
"rom");
51 result->setName(romfile);
54 std::vector<std::string_view> ipsfiles;
63 throw MSXException(
"Invalid option \"", args.front().getString(),
'\"');
68 for (
const auto& ips : ipsfiles) {
77 throw MSXException(
"Invalid ROM file: ", resolvedFilename);
106 auto* devices = extension->setFirstChild(doc.
allocateElement(
"devices"));
107 auto* primary = devices->setFirstChild(doc.
allocateElement(
"primary"));
110 auto* secondary = primary->setFirstChild(doc.
allocateElement(
"secondary"));
119 auto* mappertype = sound->setNextSibling(doc.
allocateElement(
"mappertype"));
120 mappertype->setData(mapper.empty() ?
"auto" : doc.
allocateString(mapper));
121 auto* sramname = mappertype->setNextSibling(doc.
allocateElement(
"sramname"));
124 auto* rfName = rom->setFirstChild(doc.
allocateElement(
"resolvedFilename"));
128 if (!ipsfiles.empty()) {
135 result->setConfig(extension);
136 result->setFileContext(std::move(context));
141 : motherBoard(motherBoard_)
142 , hwName(std::move(hwName_))
145 for (
auto& sub : externalSlots) {
161 std::cerr <<
e.getMessage() <<
'\n';
165 while (!devices.empty()) {
170 for (
auto ps :
xrange(4)) {
171 for (
auto ss :
xrange(4)) {
172 if (externalSlots[ps][ss]) {
176 if (externalPrimSlots[ps]) {
177 slotManager.removeExternalSlot(ps);
179 if (expandedSlots[ps]) {
182 if (allocatedPrimarySlots[ps]) {
183 slotManager.freePrimarySlot(ps, *
this);
190 auto et = devices.end();
191 for (
auto rit = devices.rbegin(), ret = devices.rend();
193 #ifdef _LIBCPP_VERSION
197 std::span alreadyRemoved(&*rit.base(), et - rit.base());
199 std::span alreadyRemoved{rit.base(), et};
201 (*rit)->testRemove(alreadyRemoved);
205 for (
auto ps :
xrange(4)) {
206 for (
auto ss :
xrange(4)) {
207 if (externalSlots[ps][ss]) {
211 if (externalPrimSlots[ps]) {
212 slotManager.testRemoveExternalSlot(ps, *
this);
214 if (expandedSlots[ps]) {
230 }
catch (XMLException&
e) {
232 "Loading of hardware configuration failed: ",
237 static string getFilename(std::string_view type, std::string_view name)
243 type,
'/', name,
".xml"));
244 }
catch (MSXException&
e) {
249 type,
'/', name,
"/hardwareconfig.xml"));
250 }
catch (MSXException&) {
258 loadHelper(doc, getFilename(type, name));
261 void HardwareConfig::load(std::string_view type_)
263 string filename = getFilename(type_, hwName);
266 assert(!userName.empty());
277 for (
const auto* psElem :
getDevicesElem().getChildren(
"primary")) {
278 const auto& primSlot = psElem->getAttribute(
"slot");
280 if (psElem->getAttributeValueAsBool(
"external",
false)) {
283 "Cannot mark unspecified primary slot '",
284 primSlot.getValue(),
"' as external");
286 if (psElem->hasChildren()) {
289 " is marked as external, but that would only "
290 "make sense if its <primary> tag would be "
293 createExternalSlot(ps);
296 for (
const auto* ssElem : psElem->getChildren(
"secondary")) {
297 auto secSlot = ssElem->getAttributeValue(
"slot");
299 if ((-16 <= ss) && (ss <= -1) && (ss != ps)) {
301 "Invalid secondary slot specification: \"",
305 if ((ss >= -128) && (0 <= ps) && (ps < 4) &&
314 ps = getAnyFreePrimarySlot();
316 ps = getSpecificFreePrimarySlot(-ps - 1);
318 auto& mutablePrimSlot =
const_cast<XMLAttribute&
>(primSlot);
321 createExpandedSlot(ps);
322 if (ssElem->getAttributeValueAsBool(
"external",
false)) {
323 if (ssElem->hasChildren()) {
325 "Secondary slot ", ps,
'-', ss,
326 " is marked as external, but that would "
327 "only make sense if its <secondary> tag "
330 createExternalSlot(ps, ss);
338 byte initialPrimarySlots = 0;
339 if (
const auto* slotmap =
getConfig().findChild(
"slotmap")) {
340 for (
const auto* child : slotmap->getChildren(
"map")) {
341 int page = child->getAttributeValueAsInt(
"page", -1);
342 if (page < 0 || page > 3) {
343 throw MSXException(
"Invalid or missing page in slotmap entry");
345 int slot = child->getAttributeValueAsInt(
"slot", -1);
346 if (slot < 0 || slot > 3) {
347 throw MSXException(
"Invalid or missing slot in slotmap entry");
349 unsigned offset = page * 2;
350 initialPrimarySlots &= ~(3 << offset);
351 initialPrimarySlots |= slot << offset;
354 return initialPrimarySlots;
366 const auto& childName = c.getName();
367 if (childName ==
"primary") {
369 }
else if (childName ==
"secondary") {
373 DeviceConfig(*
this, c, primary, secondary));
375 addDevice(std::move(device));
383 void HardwareConfig::createExternalSlot(
int ps)
386 assert(!externalPrimSlots[ps]);
387 externalPrimSlots[ps] =
true;
390 void HardwareConfig::createExternalSlot(
int ps,
int ss)
393 assert(!externalSlots[ps][ss]);
394 externalSlots[ps][ss] =
true;
397 void HardwareConfig::createExpandedSlot(
int ps)
399 if (!expandedSlots[ps]) {
401 expandedSlots[ps] =
true;
405 int HardwareConfig::getAnyFreePrimarySlot()
408 assert(!allocatedPrimarySlots[ps]);
409 allocatedPrimarySlots[ps] =
true;
413 int HardwareConfig::getSpecificFreePrimarySlot(
unsigned slot)
416 assert(!allocatedPrimarySlots[ps]);
417 allocatedPrimarySlots[ps] =
true;
421 void HardwareConfig::addDevice(std::unique_ptr<MSXDevice> device)
424 devices.push_back(std::move(device));
427 void HardwareConfig::setName(std::string_view proposedName)
434 name =
strCat(proposedName,
" (", ++n,
')');
439 void HardwareConfig::setSlot(std::string_view slotname)
441 for (
const auto* psElem :
getDevicesElem().getChildren(
"primary")) {
442 const auto& primSlot = psElem->getAttribute(
"slot");
443 if (primSlot.getValue() ==
"any") {
444 auto& mutablePrimSlot =
const_cast<XMLAttribute&
>(primSlot);
450 static constexpr std::initializer_list<enum_string<HardwareConfig::Type>> configTypeInfo = {
463 template<
typename Archive>
471 if (ar.versionAtLeast(version, 6)) {
472 ar.serialize(
"config", config);
475 ar.serialize(
"config", elem);
478 if (ar.versionAtLeast(version, 2)) {
479 if (ar.versionAtLeast(version, 4)) {
480 ar.serialize(
"context", context);
482 std::unique_ptr<FileContext> ctxt;
483 ar.serialize(
"context", ctxt);
484 if (ctxt) context = *ctxt;
491 if constexpr (Archive::IS_LOADER) {
502 for (
auto& d : devices) {
503 ar.serializePolymorphic(
"device", *d);
505 ar.serialize(
"name", name);
506 if (ar.versionAtLeast(version, 5)) {
507 ar.serialize(
"type", type);
509 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
static std::unique_ptr< HardwareConfig > createRomConfig(MSXMotherBoard &motherBoard, std::string romfile, std::string slotname, std::span< const TclObject > options)
const XMLElement & getConfig() const
HardwareConfig(const HardwareConfig &)=delete
static std::unique_ptr< HardwareConfig > createMachineConfig(MSXMotherBoard &motherBoard, std::string machineName)
void serialize(Archive &ar, unsigned version)
void setFileContext(FileContext &&ctxt)
static std::unique_ptr< HardwareConfig > createExtensionConfig(MSXMotherBoard &motherBoard, std::string extensionName, std::string_view slotname)
byte parseSlotMap() const
Parses a slot mapping.
static void loadConfig(XMLDocument &doc, std::string_view type, std::string_view name)
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)
MSXCPUInterface & getCPUInterface()
std::string getUserName(const std::string &hwName)
Keep track of which 'usernames' are in use.
CartridgeSlotManager & getSlotManager()
CommandController & getCommandController()
void removeDevice(MSXDevice &device)
const HardwareConfig * getMachineConfig() const
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 & 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()
FileContext configFileContext(string_view path, string_view hwDescr, string_view userName)
SERIALIZE_ENUM(CassettePlayer::State, stateInfo)
ArgsInfo valueArg(std::string_view name, T &value)
std::vector< TclObject > parseTclArgs(Interpreter &interp, std::span< const TclObject > inArgs, std::span< const ArgsInfo > table)
constexpr const char *const filename
FileContext userFileContext(string_view savePath)
constexpr void fill(ForwardRange &&range, const T &value)
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
TemporaryString tmpStrCat(Ts &&... ts)
std::string strCat(Ts &&...ts)
static std::unique_ptr< FileContext > getLastSerializedFileContext()
constexpr auto xrange(T e)