reformat one source file, as an example

This commit is contained in:
Christopher A. Mosher 2022-12-10 01:06:44 -05:00
parent ddf97fff28
commit 5ff50c9c0c

View File

@ -303,17 +303,10 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis
void Config::parseLine(const std::string& line, MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revision, ScreenImage& gui, CassetteIn& cassetteIn, CassetteOut& cassetteOut, Apple2* apple2) {
try {
void Config::parseLine(const std::string& line, MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revision, ScreenImage& gui, CassetteIn& cassetteIn, CassetteOut& cassetteOut, Apple2* apple2)
{
try
{
tryParseLine(line, ram, rom, slts, revision, gui, cassetteIn, cassetteOut, apple2); tryParseLine(line, ram, rom, slts, revision, gui, cassetteIn, cassetteOut, apple2);
} } catch (const ConfigException& err) {
catch (const ConfigException& err)
{
std::cerr << err.msg.c_str() << std::endl; std::cerr << err.msg.c_str() << std::endl;
} }
} }
@ -325,21 +318,18 @@ static std::string filter_row(const std::string &row) {
return std::string(1, static_cast<char> (std::toupper(row[0]))); return std::string(1, static_cast<char> (std::toupper(row[0])));
} }
void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revision, ScreenImage& gui, CassetteIn& cassetteIn, CassetteOut& cassetteOut, Apple2* apple2) void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revision, ScreenImage& gui, CassetteIn& cassetteIn, CassetteOut& cassetteOut, Apple2* apple2) {
{
std::istringstream tok(line); std::istringstream tok(line);
std::string cmd; std::string cmd;
tok >> cmd; tok >> cmd;
if (cmd == "slot") if (cmd == "slot") {
{
int slot; int slot;
std::string sCardType; std::string sCardType;
tok >> slot >> sCardType; tok >> slot >> sCardType;
insertCard(sCardType, slot, slts, gui, tok); insertCard(sCardType, slot, slts, gui, tok);
} } else if (cmd == "motherboard") {
else if (cmd == "motherboard") {
std::string op; std::string op;
tok >> op; tok >> op;
if (op == "ram") { if (op == "ram") {
@ -389,19 +379,14 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
} else { } else {
throw ConfigException("error at \"motherboard\"; expected \"ram\" or \"strap\""); throw ConfigException("error at \"motherboard\"; expected \"ram\" or \"strap\"");
} }
} } else if (cmd == "import") {
else if (cmd == "import")
{
std::string sm; std::string sm;
tok >> sm; tok >> sm;
int slot(-1); int slot(-1);
if (sm == "slot") if (sm == "slot") {
{
tok >> slot; tok >> slot;
} } else if (sm != "motherboard") {
else if (sm != "motherboard")
{
throw ConfigException("error at \"" + sm + "\"; expected \"slot #\" or \"motherboard\""); throw ConfigException("error at \"" + sm + "\"; expected \"slot #\" or \"motherboard\"");
} }
@ -415,32 +400,24 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
std::getline(tok, file); std::getline(tok, file);
trim(file); trim(file);
std::ifstream *memfile = new std::ifstream(file.c_str(), std::ios::binary); std::ifstream *memfile = new std::ifstream(file.c_str(), std::ios::binary);
if (!memfile->is_open()) if (!memfile->is_open()) {
{
std::filesystem::path f = wxGetApp().GetResDir(); std::filesystem::path f = wxGetApp().GetResDir();
f /= file; f /= file;
memfile = new std::ifstream(f, std::ios::binary); memfile = new std::ifstream(f, std::ios::binary);
if (!memfile->is_open()) if (!memfile->is_open()) {
{
throw ConfigException("cannot open file " + file); throw ConfigException("cannot open file " + file);
} }
} }
if (slot < 0) // motherboard if (slot < 0) // motherboard
{ {
if (romtype == "rom") if (romtype == "rom") {
{
rom.load(base, *memfile); rom.load(base, *memfile);
} } else {
else
{
throw ConfigException("error at \"" + romtype + "\"; expected rom or ram"); throw ConfigException("error at \"" + romtype + "\"; expected rom or ram");
} }
} } else {
else if (8 <= slot) {
{
if (8 <= slot)
{
throw ConfigException("invalid slot number"); throw ConfigException("invalid slot number");
} }
Card* card = slts.get(slot); Card* card = slts.get(slot);
@ -454,13 +431,10 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
throw ConfigException("error at \"" + romtype + "\"; expected rom, rom7, or rombank"); throw ConfigException("error at \"" + romtype + "\"; expected rom, rom7, or rombank");
} }
memfile->close(); memfile->close();
} } else if (cmd == "load" || cmd == "save" || cmd == "unload") {
else if (cmd == "load" || cmd == "save" || cmd == "unload")
{
std::string slotk; std::string slotk;
tok >> slotk; tok >> slotk;
if (slotk != "slot") if (slotk != "slot") {
{
throw ConfigException("error at \"" + slotk + "\"; expected \"slot\""); throw ConfigException("error at \"" + slotk + "\"; expected \"slot\"");
} }
@ -469,16 +443,14 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
std::string drivek; std::string drivek;
tok >> drivek; tok >> drivek;
if (drivek != "drive") if (drivek != "drive") {
{
throw ConfigException("error at \"" + drivek + "\"; expected \"drive\""); throw ConfigException("error at \"" + drivek + "\"; expected \"drive\"");
} }
int drive(-1); int drive(-1);
tok >> drive; tok >> drive;
if (cmd == "load") if (cmd == "load") {
{
std::string fn_optional; std::string fn_optional;
std::getline(tok, fn_optional); std::getline(tok, fn_optional);
trim(fn_optional); trim(fn_optional);
@ -494,44 +466,29 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
// TODO check if file exists, if not then check resources // TODO check if file exists, if not then check resources
loadDisk(slts, slot, drive, fn_optional); loadDisk(slts, slot, drive, fn_optional);
} }
} } else if (cmd == "unload") {
else if (cmd == "unload")
{
unloadDisk(slts, slot, drive); unloadDisk(slts, slot, drive);
} } else if (cmd == "save") {
else if (cmd == "save")
{
saveDisk(slts, slot, drive); saveDisk(slts, slot, drive);
} }
} } else if (cmd == "revision") {
else if (cmd == "revision")
{
tok >> std::hex >> revision; tok >> std::hex >> revision;
} } else if (cmd == "cassette") {
else if (cmd == "cassette")
{
std::string cas; std::string cas;
tok >> cas; tok >> cas;
if (cas == "rewind") if (cas == "rewind") {
{
cassetteIn.rewind(); cassetteIn.rewind();
} } else if (cas == "tone") {
else if (cas == "tone")
{
cassetteIn.tone(); cassetteIn.tone();
} } else if (cas == "blank") {
else if (cas == "blank")
{
std::string fcas; std::string fcas;
std::getline(tok, fcas); std::getline(tok, fcas);
trim(fcas); trim(fcas);
if (!fcas.empty()) { if (!fcas.empty()) {
cassetteOut.blank(fcas); cassetteOut.blank(fcas);
} }
} } else if (cas == "load") {
else if (cas == "load")
{
std::string fn_optional; std::string fn_optional;
std::getline(tok, fn_optional); std::getline(tok, fn_optional);
trim(fn_optional); trim(fn_optional);
@ -546,9 +503,7 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
if (fn_optional.length() > 0) { if (fn_optional.length() > 0) {
cassetteIn.load(fn_optional); cassetteIn.load(fn_optional);
} }
} } else if (cas == "eject") {
else if (cas == "eject")
{
std::string eject; std::string eject;
tok >> eject; tok >> eject;
if (eject == "in") { if (eject == "in") {
@ -558,18 +513,12 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
} else { } else {
throw ConfigException("error: unknown cassette to eject: " + eject); throw ConfigException("error: unknown cassette to eject: " + eject);
} }
} } else if (cas == "save") {
else if (cas == "save")
{
cassetteOut.save(); cassetteOut.save();
} } else {
else
{
throw ConfigException("error: unknown cassette command: " + cas); throw ConfigException("error: unknown cassette command: " + cas);
} }
} } else if (cmd == "cpu") {
else if (cmd == "cpu")
{
std::string cpu; std::string cpu;
tok >> cpu; tok >> cpu;
if (apple2 != NULL) { if (apple2 != NULL) {
@ -581,9 +530,7 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
throw ConfigException("invalid value for cpu command: " + cpu); throw ConfigException("invalid value for cpu command: " + cpu);
} }
} }
} } else {
else
{
throw ConfigException("Invalid command: " + cmd); throw ConfigException("Invalid command: " + cmd);
} }
@ -596,19 +543,14 @@ void Config::tryParseLine(const std::string& line, MemoryRandomAccess& ram, Memo
unsigned char Config::disk_mask(0); unsigned char Config::disk_mask(0);
void Config::loadDisk(Slots& slts, int slot, int drive, const std::string& fnib) {
if (drive < 1 || 2 < drive) {
void Config::loadDisk(Slots& slts, int slot, int drive, const std::string& fnib)
{
if (drive < 1 || 2 < drive)
{
throw ConfigException("Invalid drive; must be 1 or 2"); throw ConfigException("Invalid drive; must be 1 or 2");
} }
// TODO if file doesn't exist, name still gets displayed, and there's no error message // TODO if file doesn't exist, name still gets displayed, and there's no error message
Card* card = slts.get(slot); Card* card = slts.get(slot);
if (!(disk_mask & (1 << slot))) if (!(disk_mask & (1 << slot))) {
{
std::cerr << "Slot " << slot << " doesn't have a disk controller card" << std::endl; std::cerr << "Slot " << slot << " doesn't have a disk controller card" << std::endl;
return; return;
} }
@ -617,16 +559,13 @@ void Config::loadDisk(Slots& slts, int slot, int drive, const std::string& fnib)
controller->loadDisk(drive - 1, fnib); controller->loadDisk(drive - 1, fnib);
} }
void Config::unloadDisk(Slots& slts, int slot, int drive) void Config::unloadDisk(Slots& slts, int slot, int drive) {
{ if (drive < 1 || 2 < drive) {
if (drive < 1 || 2 < drive)
{
throw ConfigException("Invalid drive; must be 1 or 2"); throw ConfigException("Invalid drive; must be 1 or 2");
} }
Card* card = slts.get(slot); Card* card = slts.get(slot);
if (!(disk_mask & (1 << slot))) if (!(disk_mask & (1 << slot))) {
{
std::cerr << "Slot " << slot << " doesn't have a disk controller card" << std::endl; std::cerr << "Slot " << slot << " doesn't have a disk controller card" << std::endl;
return; return;
} }
@ -635,19 +574,15 @@ void Config::unloadDisk(Slots& slts, int slot, int drive)
controller->unloadDisk(drive - 1); controller->unloadDisk(drive - 1);
} }
void Config::saveDisk(Slots& slts, int slot, int drive) void Config::saveDisk(Slots& slts, int slot, int drive) {
{ if (drive < 1 || 2 < drive) {
if (drive < 1 || 2 < drive)
{
throw ConfigException("Invalid drive; must be 1 or 2"); throw ConfigException("Invalid drive; must be 1 or 2");
} }
slts.get(slot)->save(drive - 1); slts.get(slot)->save(drive - 1);
} }
void Config::insertCard(const std::string& cardType, int slot, Slots& slts, ScreenImage& gui, std::istringstream& tok) void Config::insertCard(const std::string& cardType, int slot, Slots& slts, ScreenImage& gui, std::istringstream& tok) {
{ if (slot < 0 || 8 <= slot) {
if (slot < 0 || 8 <= slot)
{
throw ConfigException("Invalid slot number"); throw ConfigException("Invalid slot number");
} }
@ -655,53 +590,39 @@ void Config::insertCard(const std::string& cardType, int slot, Slots& slts, Scre
disk_mask &= ~(1 << slot); disk_mask &= ~(1 << slot);
if (cardType == "language") if (cardType == "language") {
{
card = new LanguageCard(gui, slot); card = new LanguageCard(gui, slot);
} } else if (cardType == "firmware") {
else if (cardType == "firmware")
{
card = new FirmwareCard(gui, slot); card = new FirmwareCard(gui, slot);
} } else if (cardType == "disk") {
else if (cardType == "disk") // 16-sector LSS ROM // 16-sector LSS ROM
{
double random_ones_rate(0.3); // WOZ spec v2.0: 30% double random_ones_rate(0.3); // WOZ spec v2.0: 30%
tok >> random_ones_rate; tok >> random_ones_rate;
std::cerr << "MC3470: rate of 1 bits during random bit generation: " << random_ones_rate << std::endl; std::cerr << "MC3470: rate of 1 bits during random bit generation: " << random_ones_rate << std::endl;
card = new DiskController(gui, slot, false, random_ones_rate); card = new DiskController(gui, slot, false, random_ones_rate);
disk_mask |= (1 << slot); disk_mask |= (1 << slot);
} } else if (cardType == "disk13") {
else if (cardType == "disk13") // 13-sector LSS ROM // 13-sector LSS ROM
{
double random_ones_rate(0.3); // WOZ spec v2.0: 30% double random_ones_rate(0.3); // WOZ spec v2.0: 30%
tok >> random_ones_rate; tok >> random_ones_rate;
std::cerr << "MC3470: rate of 1 bits during random bit generation: " << random_ones_rate << std::endl; std::cerr << "MC3470: rate of 1 bits during random bit generation: " << random_ones_rate << std::endl;
card = new DiskController(gui, slot, true, random_ones_rate); card = new DiskController(gui, slot, true, random_ones_rate);
disk_mask |= (1 << slot); disk_mask |= (1 << slot);
} } else if (cardType == "clock") {
else if (cardType == "clock")
{
card = new ClockCard(); card = new ClockCard();
} } else if (cardType == "stdout") {
else if (cardType == "stdout")
{
card = new StandardOut(); card = new StandardOut();
} } else if (cardType == "stdin") {
else if (cardType == "stdin")
{
card = new StandardIn(); card = new StandardIn();
} } else if (cardType == "empty") {
else if (cardType == "empty")
{
card = 0; card = 0;
} } else {
else
{
throw ConfigException("Invalid card type: " + cardType); throw ConfigException("Invalid card type: " + cardType);
} }
if (card) if (card) {
slts.set(slot, card); slts.set(slot, card);
else } else {
slts.remove(slot); slts.remove(slot);
} }
}