28 auto result = std::make_unique<HardwareConfig>(
29 motherBoard, std::move(machineName));
31 result->load(
"machines");
36 MSXMotherBoard& motherBoard,
string extensionName, std::string_view slotName)
38 auto result = std::make_unique<HardwareConfig>(
39 motherBoard, std::move(extensionName));
40 result->load(
"extensions");
41 result->setName(result->hwName);
43 result->setSlot(slotName);
49 std::string_view slotName, std::span<const TclObject> options)
51 auto result = std::make_unique<HardwareConfig>(motherBoard,
"rom");
52 result->setName(romFile);
55 std::vector<std::string_view> ipsFiles;
64 throw MSXException(
"Invalid option \"", args.front().getString(),
'\"');
69 for (
const auto& ips : ipsFiles) {
78 throw MSXException(
"Invalid ROM file: ", resolvedFilename);
107 auto* devices = extension->setFirstChild(doc.
allocateElement(
"devices"));
108 auto* primary = devices->setFirstChild(doc.
allocateElement(
"primary"));
111 auto* secondary = primary->setFirstChild(doc.
allocateElement(
"secondary"));
120 auto* mapperType = sound->setNextSibling(doc.
allocateElement(
"mappertype"));
121 mapperType->setData(mapper.empty() ?
"auto" : doc.
allocateString(mapper));
122 auto* sramName = mapperType->setNextSibling(doc.
allocateElement(
"sramname"));
125 auto* rfName = rom->setFirstChild(doc.
allocateElement(
"resolvedFilename"));
129 if (!ipsFiles.empty()) {
136 result->setConfig(extension);
137 result->setFileContext(std::move(context));
142 : motherBoard(motherBoard_)
143 , hwName(
std::move(hwName_))
146 for (
auto& sub : externalSlots) {
162 std::cerr <<
e.getMessage() <<
'\n';
166 while (!devices.empty()) {
171 for (
auto ps :
xrange(4)) {
172 for (
auto ss :
xrange(4)) {
173 if (externalSlots[ps][ss]) {
177 if (externalPrimSlots[ps]) {
178 slotManager.removeExternalSlot(ps);
180 if (expandedSlots[ps]) {
183 if (allocatedPrimarySlots[ps]) {
184 slotManager.freePrimarySlot(ps, *
this);
191 auto et = devices.end();
192 for (
auto rit = devices.rbegin(), ret = devices.rend();
194#ifdef _LIBCPP_VERSION
198 std::span alreadyRemoved(&*rit.base(), et - rit.base());
200 std::span alreadyRemoved{rit.base(), et};
202 (*rit)->testRemove(alreadyRemoved);
206 for (
auto ps :
xrange(4)) {
207 for (
auto ss :
xrange(4)) {
208 if (externalSlots[ps][ss]) {
212 if (externalPrimSlots[ps]) {
213 slotManager.testRemoveExternalSlot(ps, *
this);
215 if (expandedSlots[ps]) {
227static void loadHelper(
XMLDocument& doc,
const std::string& filename)
230 doc.
load(filename,
"msxconfig2.dtd");
231 }
catch (XMLException&
e) {
233 "Loading of hardware configuration failed: ",
238static string getFilename(std::string_view type, std::string_view name)
244 type,
'/', name,
".xml"));
245 }
catch (MSXException&
e) {
250 type,
'/', name,
"/hardwareconfig.xml"));
251 }
catch (MSXException&) {
259 loadHelper(doc, getFilename(type, name));
262void HardwareConfig::load(std::string_view type_)
264 string filename = getFilename(type_, hwName);
265 loadHelper(config, filename);
267 assert(!userName.empty());
278 for (
const auto* psElem :
getDevicesElem().getChildren(
"primary")) {
279 const auto& primSlot = psElem->getAttribute(
"slot");
281 if (psElem->getAttributeValueAsBool(
"external",
false)) {
284 "Cannot mark unspecified primary slot '",
285 primSlot.getValue(),
"' as external");
287 if (psElem->hasChildren()) {
290 " is marked as external, but that would only "
291 "make sense if its <primary> tag would be "
294 createExternalSlot(ps);
297 for (
const auto* ssElem : psElem->getChildren(
"secondary")) {
298 auto secSlot = ssElem->getAttributeValue(
"slot");
300 if ((-16 <= ss) && (ss <= -1) && (ss != ps)) {
302 "Invalid secondary slot specification: \"",
306 if ((ss >= -128) && (0 <= ps) && (ps < 4) &&
315 ps = getAnyFreePrimarySlot();
317 ps = getSpecificFreePrimarySlot(-ps - 1);
319 auto& mutablePrimSlot =
const_cast<XMLAttribute&
>(primSlot);
322 createExpandedSlot(ps);
323 if (ssElem->getAttributeValueAsBool(
"external",
false)) {
324 if (ssElem->hasChildren()) {
326 "Secondary slot ", ps,
'-', ss,
327 " is marked as external, but that would "
328 "only make sense if its <secondary> tag "
331 createExternalSlot(ps, ss);
339 byte initialPrimarySlots = 0;
340 if (
const auto* slotMap =
getConfig().findChild(
"slotmap")) {
341 for (
const auto* child : slotMap->getChildren(
"map")) {
342 int page = child->getAttributeValueAsInt(
"page", -1);
343 if (page < 0 || page > 3) {
344 throw MSXException(
"Invalid or missing page in slotmap entry");
346 int slot = child->getAttributeValueAsInt(
"slot", -1);
347 if (slot < 0 || slot > 3) {
348 throw MSXException(
"Invalid or missing slot in slotmap entry");
350 unsigned offset = page * 2;
351 initialPrimarySlots &=
byte(~(3 << offset));
352 initialPrimarySlots |=
byte(slot << offset);
355 return initialPrimarySlots;
367 const auto& childName = c.getName();
368 if (childName ==
"primary") {
370 }
else if (childName ==
"secondary") {
374 DeviceConfig(*
this, c, primary, secondary));
376 addDevice(std::move(device));
384void HardwareConfig::createExternalSlot(
int ps)
387 assert(!externalPrimSlots[ps]);
388 externalPrimSlots[ps] =
true;
391void HardwareConfig::createExternalSlot(
int ps,
int ss)
394 assert(!externalSlots[ps][ss]);
395 externalSlots[ps][ss] =
true;
398void HardwareConfig::createExpandedSlot(
int ps)
400 if (!expandedSlots[ps]) {
402 expandedSlots[ps] =
true;
406int HardwareConfig::getAnyFreePrimarySlot()
409 assert(!allocatedPrimarySlots[ps]);
410 allocatedPrimarySlots[ps] =
true;
414int HardwareConfig::getSpecificFreePrimarySlot(
unsigned slot)
417 assert(!allocatedPrimarySlots[ps]);
418 allocatedPrimarySlots[ps] =
true;
422void HardwareConfig::addDevice(std::unique_ptr<MSXDevice> device)
425 devices.push_back(std::move(device));
428void HardwareConfig::setName(std::string_view proposedName)
435 name =
strCat(proposedName,
" (", ++n,
')');
440void HardwareConfig::setSlot(std::string_view slotName)
442 for (
const auto* psElem :
getDevicesElem().getChildren(
"primary")) {
443 const auto& primSlot = psElem->getAttribute(
"slot");
444 if (primSlot.getValue() ==
"any") {
445 auto& mutablePrimSlot =
const_cast<XMLAttribute&
>(primSlot);
451static constexpr std::initializer_list<enum_string<HardwareConfig::Type>> configTypeInfo = {
464template<
typename Archive>
472 if (ar.versionAtLeast(version, 6)) {
473 ar.serialize(
"config", config);
476 ar.serialize(
"config", elem);
479 if (ar.versionAtLeast(version, 2)) {
480 if (ar.versionAtLeast(version, 4)) {
481 ar.serialize(
"context", context);
483 std::unique_ptr<FileContext> ctxt;
484 ar.serialize(
"context", ctxt);
485 if (ctxt) context = *ctxt;
492 if constexpr (Archive::IS_LOADER) {
503 for (
auto& d : devices) {
504 ar.serializePolymorphic(
"device", *d);
506 ar.serialize(
"name", name);
507 if (ar.versionAtLeast(version, 5)) {
508 ar.serialize(
"type", type);
510 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
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 & 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)
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)
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)