25CartridgeSlotManager::Slot::~Slot()
28 assert(useCount == 0);
31bool CartridgeSlotManager::Slot::exists()
const
33 return cartCommand.has_value();
36bool CartridgeSlotManager::Slot::used(
const HardwareConfig* allowed)
const
38 assert((useCount == 0) == (config ==
nullptr));
39 return config && (config != allowed);
42void CartridgeSlotManager::Slot::getMediaInfo(TclObject& result)
47 result.addDictKeyValues(
"target", config->getConfigName(),
48 "devicename", config->getName(),
52 result.addDictKeyValue(
"type",
"rom");
54 const auto& romConfig = config->getConfig()
55 .getChild(
"devices").getChild(
"primary").getChild(
"secondary")
56 .getChild(
"ROM").getChild(
"rom");
57 result.addDictKeyValue(
"target", romConfig.getChildData(
"filename"));
59 if (
const auto* patchesElem = romConfig.findChild(
"patches")) {
60 for (
const auto* p : patchesElem->getChildren(
"ips")) {
61 patches.addListElement(p->getData());
64 result.addDictKeyValue(
"patches", patches);
65 if (
const auto* rom =
dynamic_cast<const MSXRom*
>(cpuInterface->getMSXDevice(ps, ss < 0 ? 0: ss, 1))) {
70 result.addDictKeyValue(
"target", std::string_view{});
77 : motherBoard(motherBoard_)
78 , cartCmd(*this, motherBoard,
"cart")
79 , extSlotInfo(motherBoard.getMachineInfoCommand())
87 assert(!slots[slot].exists());
88 assert(!slots[slot].used());
94 if (slot.size() == 1) {
95 if ((
'0' <= slot[0]) && (slot[0] <=
'3')) {
97 }
else if ((
'a' <= slot[0]) && (slot[0] <=
'p')) {
98 return -(1 + slot[0] -
'a');
99 }
else if (slot[0] ==
'X') {
102 }
else if (slot.size() == 2) {
103 if ((slot[0] ==
'?') && (
'0' <= slot[1]) && (slot[1] <=
'3')) {
104 return slot[1] -
'0' - 128;
106 }
else if (slot ==
"any") {
109 throw MSXException(
"Invalid slot specification: ", slot);
120 throw MSXException(
"Slot is already an external slot.");
123 auto& slot = slots[slotNum];
124 if (!slot.exists()) {
128 std::array slotName = {
'c',
'a',
'r',
't',
'X',
'\0'};
129 slotName[4] = narrow<char>(
'a' + slotNum);
132 slot.cartCommand.emplace(
133 *
this, motherBoard, slotName.data());
135 slot.cartCommand->getName(), slot);
137 std::array extName = {
'e',
'x',
't',
'X',
'\0'};
138 extName[3] = narrow<char>(
'a' + slotNum);
139 slot.extCommand.emplace(
140 motherBoard, extName.data());
149unsigned CartridgeSlotManager::getSlot(
int ps,
int ss)
const
152 if (slots[slot].exists() &&
153 (slots[slot].ps == ps) && (slots[slot].ss == ss)) {
169 auto slot = getSlot(ps, ss);
170 if (slots[slot].used(&allowed)) {
182 auto slotNum = getSlot(ps, ss);
183 auto& slot = slots[slotNum];
184 assert(!slot.used());
188 slot.cartCommand.reset();
189 slot.extCommand.reset();
195 if (!slots[slot].exists()) {
196 throw MSXException(
"slot-",
char(
'a' + slot),
" not defined.");
198 if (slots[slot].used()) {
199 throw MSXException(
"slot-",
char(
'a' + slot),
" already in use.");
208 if (!slots[slot].exists()) {
209 throw MSXException(
"slot-",
char(
'a' + slot),
" not defined.");
211 if (slots[slot].used()) {
212 throw MSXException(
"slot-",
char(
'a' + slot),
" already in use.");
214 if (slots[slot].ss != -1) {
215 throw MSXException(
"slot-",
char(
'a' + slot),
" is not a primary slot.");
217 assert(slots[slot].useCount == 0);
218 slots[slot].config = &hwConfig;
219 slots[slot].useCount = 1;
220 return slots[slot].ps;
228 if (slots[slot].exists() && !slots[slot].used()) {
229 int p = slots[slot].ps;
230 int s = slots[slot].ss;
231 if ((p < ps) || ((p == ps) && (s < ss))) {
245 if (slots[slot].exists() && (slots[slot].ss == -1) &&
246 !slots[slot].used()) {
247 assert(slots[slot].useCount == 0);
248 slots[slot].config = &hwConfig;
249 slots[slot].useCount = 1;
250 return slots[slot].ps;
259 auto slot = getSlot(ps, -1);
260 assert(slots[slot].config == &hwConfig); (void)hwConfig;
261 assert(slots[slot].useCount == 1);
262 slots[slot].config =
nullptr;
263 slots[slot].useCount = 0;
270 if (!slots[slot].exists())
continue;
271 if ((slots[slot].ps == ps) && (slots[slot].ss == ss)) {
272 if (slots[slot].useCount == 0) {
273 slots[slot].config = &hwConfig;
275 if (slots[slot].config != &hwConfig) {
277 "Slot ", ps,
'-', ss,
278 " already in use by ",
279 slots[slot].config->getName());
282 ++slots[slot].useCount;
292 if (!slots[slot].exists())
continue;
293 if ((slots[slot].ps == ps) && (slots[slot].ss == ss)) {
294 assert(slots[slot].config == &hwConfig); (void)hwConfig;
295 assert(slots[slot].useCount > 0);
296 --slots[slot].useCount;
297 if (slots[slot].useCount == 0) {
298 slots[slot].config =
nullptr;
309 int tmp = (convert && (slots[slot].ss == -1)) ? 0 : slots[slot].ss;
310 return slots[slot].exists() && (slots[slot].ps == ps) && (tmp == ss);
316CartridgeSlotManager::CartCmd::CartCmd(
318 std::string_view commandName)
320 motherBoard_.getStateChangeDistributor(),
321 motherBoard_.getScheduler(),
324 , cliComm(motherBoard_.getMSXCliComm())
328const HardwareConfig* CartridgeSlotManager::CartCmd::getExtensionConfig(
329 std::string_view cartName)
const
331 if (cartName.size() != 5) {
334 return manager.getConfigForSlot(cartName[4] -
'a');
337void CartridgeSlotManager::CartCmd::execute(
338 std::span<const TclObject> tokens, TclObject& result, EmuTime::param )
340 std::string_view cartName = tokens[0].getString();
346 if (
auto pos = cartName.rfind(
"::"); pos != std::string_view::npos) {
347 cartName = cartName.substr(pos + 2);
349 if (tokens.size() == 1) {
351 const auto* extConf = getExtensionConfig(cartName);
352 result.addListElement(
tmpStrCat(cartName,
':'),
353 extConf ? extConf->getName() : string{});
356 result.addListElement(options);
358 }
else if (tokens[1] ==
one_of(
"eject",
"-eject")) {
360 if (tokens[1] ==
"-eject") {
362 "Warning: use of '-eject' is deprecated, "
363 "instead use the 'eject' subcommand";
365 if (
const auto* extConf = getExtensionConfig(cartName)) {
367 manager.motherBoard.removeExtension(*extConf);
369 }
catch (MSXException& e) {
370 throw CommandException(
"Can't remove cartridge: ",
376 auto slotName = (cartName.size() == 5)
377 ? cartName.substr(4, 1)
379 size_t extensionNameToken = 1;
380 if (tokens[1] ==
"insert") {
381 if (tokens.size() > 2) {
382 extensionNameToken = 2;
384 throw CommandException(
"Missing argument to insert subcommand");
387 auto options = tokens.subspan(extensionNameToken + 1);
389 std::string_view romName = tokens[extensionNameToken].getString();
391 manager.motherBoard, romName, slotName, options);
392 if (slotName !=
"any") {
393 if (
const auto* extConf = getExtensionConfig(cartName)) {
395 manager.motherBoard.removeExtension(*extConf);
398 result = manager.motherBoard.insertExtension(
399 "ROM", std::move(extension));
401 }
catch (MSXException& e) {
402 throw CommandException(std::move(e).getMessage());
407string CartridgeSlotManager::CartCmd::help(std::span<const TclObject> tokens)
const
409 auto cart = tokens[0].getString();
411 cart,
" eject : remove the ROM cartridge from this slot\n",
412 cart,
" insert <filename> : insert ROM cartridge with <filename>\n",
413 cart,
" <filename> : insert ROM cartridge with <filename>\n",
414 cart,
" : show which ROM cartridge is in this slot\n",
415 "The following options are supported when inserting a cartridge:\n"
416 "-ips <filename> : apply the given IPS patch to the ROM image\n"
417 "-romtype <romtype> : specify the ROM mapper type\n");
420void CartridgeSlotManager::CartCmd::tabCompletion(std::vector<string>& tokens)
const
422 using namespace std::literals;
423 static constexpr std::array extra = {
"eject"sv,
"insert"sv};
425 (tokens.size() < 3) ? extra :
std::span<const
std::string_view>{});
429bool CartridgeSlotManager::CartCmd::needRecord(std::span<const TclObject> tokens)
const
431 return tokens.size() > 1;
437CartridgeSlotManager::CartridgeSlotInfo::CartridgeSlotInfo(
438 InfoCommand& machineInfoCommand)
439 : InfoTopic(machineInfoCommand,
"external_slot")
443void CartridgeSlotManager::CartridgeSlotInfo::execute(
444 std::span<const TclObject> tokens, TclObject& result)
const
446 checkNumArgs(tokens, Between{2, 3}, Prefix{2},
"?slot?");
447 auto& manager =
OUTER(CartridgeSlotManager, extSlotInfo);
448 switch (tokens.size()) {
451 string slot =
"slotX";
452 for (
auto i :
xrange(CartridgeSlotManager::MAX_SLOTS)) {
453 if (!manager.slots[i].exists())
continue;
454 slot[4] = char(
'a' + i);
455 result.addListElement(slot);
461 const auto& slotName = tokens[2].getString();
462 if ((slotName.size() != 5) || !slotName.starts_with(
"slot")) {
463 throw CommandException(
"Invalid slot name: ", slotName);
465 unsigned num = slotName[4] -
'a';
467 throw CommandException(
"Invalid slot name: ", slotName);
469 auto& slot = manager.slots[num];
470 if (!slot.exists()) {
471 throw CommandException(
"Slot '", slotName,
"' doesn't currently exist in this msx machine.");
473 result.addListElement(slot.ps);
475 result.addListElement(
"X");
477 result.addListElement(slot.ss);
480 result.addListElement(slot.config->getName());
482 result.addListElement(std::string_view{});
489string CartridgeSlotManager::CartridgeSlotInfo::help(
490 std::span<const TclObject> )
const
492 return "Without argument: show list of available external slots.\n"
493 "With argument: show primary and secondary slot number for "
494 "given external slot.\n";
CartridgeSlotManager(MSXMotherBoard &motherBoard)
void getAnyFreeSlot(int &ps, int &ss) const
static int getSlotNum(std::string_view slot)
void createExternalSlot(int ps)
void testRemoveExternalSlot(int ps, const HardwareConfig &allowed) const
static constexpr unsigned MAX_SLOTS
void getSpecificSlot(unsigned slot, int &ps, int &ss) const
bool isExternalSlot(int ps, int ss, bool convert) const
void removeExternalSlot(int ps)
void freeSlot(int ps, int ss, const HardwareConfig &hwConfig)
int allocateAnyPrimarySlot(const HardwareConfig &hwConfig)
int allocateSpecificPrimarySlot(unsigned slot, const HardwareConfig &hwConfig)
void allocateSlot(int ps, int ss, const HardwareConfig &hwConfig)
void freePrimarySlot(int ps, const HardwareConfig &hwConfig)
static std::unique_ptr< HardwareConfig > createRomConfig(MSXMotherBoard &motherBoard, std::string_view romFile, std::string_view slotName, std::span< const TclObject > options)
void update(UpdateType type, std::string_view name, std::string_view value) override
MSXCPUInterface & getCPUInterface()
void registerMediaInfo(std::string_view name, MediaInfoProvider &provider)
Register and unregister providers of media info, for the media info topic.
void unregisterMediaInfo(MediaInfoProvider &provider)
MSXCliComm & getMSXCliComm()
Commands that directly influence the MSX state should send and events so that they can be recorded by...
This file implemented 3 utility functions:
const FileContext & userFileContext()
TclObject makeTclList(Args &&... args)
constexpr bool any_of(InputRange &&range, UnaryPredicate pred)
#define OUTER(type, member)
TemporaryString tmpStrCat(Ts &&... ts)
constexpr auto xrange(T e)