2024-06-16 18:27:48 +00:00
|
|
|
//
|
|
|
|
// CSL.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 12/06/2024.
|
|
|
|
// Copyright © 2024 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "CSL.hpp"
|
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
#include <cctype>
|
2024-06-16 18:27:48 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <set>
|
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
#include "../../Machines/AmstradCPC/Keyboard.hpp"
|
2024-06-16 18:27:48 +00:00
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
using namespace Storage::Automation;
|
2024-06-16 18:27:48 +00:00
|
|
|
|
2024-06-21 02:13:19 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool append_typed(std::vector<Storage::Automation::CSL::KeyEvent> &down, std::vector<Storage::Automation::CSL::KeyEvent> &up, std::istringstream &stream) {
|
|
|
|
const auto press = [&](uint16_t key) {
|
|
|
|
CSL::KeyEvent event;
|
|
|
|
event.key = key;
|
|
|
|
event.down = true;
|
|
|
|
down.push_back(event);
|
|
|
|
event.down = false;
|
|
|
|
up.push_back(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto shift = [&](uint16_t key) {
|
|
|
|
CSL::KeyEvent event;
|
|
|
|
event.key = AmstradCPC::Key::KeyShift;
|
|
|
|
event.down = true;
|
|
|
|
down.push_back(event);
|
|
|
|
press(key);
|
|
|
|
event.down = false;
|
|
|
|
up.push_back(event);
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto next = stream.get();
|
|
|
|
if(stream.eof()) return false;
|
|
|
|
|
|
|
|
switch(next) {
|
|
|
|
default: throw CSL::InvalidArgument;
|
|
|
|
case '\'': return false;
|
|
|
|
case '}': return false;
|
|
|
|
|
|
|
|
case 'A': press(AmstradCPC::Key::KeyA); break;
|
|
|
|
case 'B': press(AmstradCPC::Key::KeyB); break;
|
|
|
|
case 'C': press(AmstradCPC::Key::KeyC); break;
|
|
|
|
case 'D': press(AmstradCPC::Key::KeyD); break;
|
|
|
|
case 'E': press(AmstradCPC::Key::KeyE); break;
|
|
|
|
case 'F': press(AmstradCPC::Key::KeyF); break;
|
|
|
|
case 'G': press(AmstradCPC::Key::KeyG); break;
|
|
|
|
case 'H': press(AmstradCPC::Key::KeyH); break;
|
|
|
|
case 'I': press(AmstradCPC::Key::KeyI); break;
|
|
|
|
case 'J': press(AmstradCPC::Key::KeyJ); break;
|
|
|
|
case 'K': press(AmstradCPC::Key::KeyK); break;
|
|
|
|
case 'L': press(AmstradCPC::Key::KeyL); break;
|
|
|
|
case 'M': press(AmstradCPC::Key::KeyM); break;
|
|
|
|
case 'N': press(AmstradCPC::Key::KeyN); break;
|
|
|
|
case 'O': press(AmstradCPC::Key::KeyO); break;
|
|
|
|
case 'P': press(AmstradCPC::Key::KeyP); break;
|
|
|
|
case 'Q': press(AmstradCPC::Key::KeyQ); break;
|
|
|
|
case 'R': press(AmstradCPC::Key::KeyR); break;
|
|
|
|
case 'S': press(AmstradCPC::Key::KeyS); break;
|
|
|
|
case 'T': press(AmstradCPC::Key::KeyT); break;
|
|
|
|
case 'U': press(AmstradCPC::Key::KeyU); break;
|
|
|
|
case 'V': press(AmstradCPC::Key::KeyV); break;
|
|
|
|
case 'W': press(AmstradCPC::Key::KeyW); break;
|
|
|
|
case 'X': press(AmstradCPC::Key::KeyX); break;
|
|
|
|
case 'Y': press(AmstradCPC::Key::KeyY); break;
|
|
|
|
case 'Z': press(AmstradCPC::Key::KeyZ); break;
|
|
|
|
case ' ': press(AmstradCPC::Key::KeySpace); break;
|
|
|
|
case '0': press(AmstradCPC::Key::Key0); break;
|
|
|
|
case '1': press(AmstradCPC::Key::Key1); break;
|
|
|
|
case '2': press(AmstradCPC::Key::Key2); break;
|
|
|
|
case '3': press(AmstradCPC::Key::Key3); break;
|
|
|
|
case '4': press(AmstradCPC::Key::Key4); break;
|
|
|
|
case '5': press(AmstradCPC::Key::Key5); break;
|
|
|
|
case '6': press(AmstradCPC::Key::Key6); break;
|
|
|
|
case '7': press(AmstradCPC::Key::Key7); break;
|
|
|
|
case '8': press(AmstradCPC::Key::Key8); break;
|
|
|
|
case '9': press(AmstradCPC::Key::Key9); break;
|
|
|
|
|
|
|
|
case '"': shift(AmstradCPC::Key::Key2); break;
|
|
|
|
|
|
|
|
case '\\': {
|
|
|
|
if(stream.peek() != '(') {
|
|
|
|
press(AmstradCPC::Key::KeyBackSlash);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stream.get();
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
while(stream.peek() != ')') {
|
|
|
|
name.push_back(char(stream.get()));
|
|
|
|
}
|
|
|
|
stream.get();
|
|
|
|
|
|
|
|
static const std::unordered_map<std::string, uint16_t> names = {
|
|
|
|
{"ESC", AmstradCPC::Key::KeyEscape},
|
|
|
|
{"TAB", AmstradCPC::Key::KeyTab},
|
|
|
|
{"CAP", AmstradCPC::Key::KeyCapsLock},
|
|
|
|
{"SHI", AmstradCPC::Key::KeyShift},
|
|
|
|
{"CTR", AmstradCPC::Key::KeyControl},
|
|
|
|
{"COP", AmstradCPC::Key::KeyCopy},
|
|
|
|
{"CLR", AmstradCPC::Key::KeyClear},
|
|
|
|
{"DEL", AmstradCPC::Key::KeyDelete},
|
|
|
|
{"RET", AmstradCPC::Key::KeyReturn},
|
|
|
|
{"ENT", AmstradCPC::Key::KeyEnter},
|
|
|
|
{"ARL", AmstradCPC::Key::KeyLeft},
|
|
|
|
{"ARR", AmstradCPC::Key::KeyRight},
|
|
|
|
{"ARU", AmstradCPC::Key::KeyUp},
|
|
|
|
{"ARD", AmstradCPC::Key::KeyDown},
|
|
|
|
{"FN0", AmstradCPC::Key::KeyF0},
|
|
|
|
{"FN1", AmstradCPC::Key::KeyF1},
|
|
|
|
{"FN2", AmstradCPC::Key::KeyF2},
|
|
|
|
{"FN3", AmstradCPC::Key::KeyF3},
|
|
|
|
{"FN4", AmstradCPC::Key::KeyF4},
|
|
|
|
{"FN5", AmstradCPC::Key::KeyF5},
|
|
|
|
{"FN6", AmstradCPC::Key::KeyF6},
|
|
|
|
{"FN7", AmstradCPC::Key::KeyF7},
|
|
|
|
{"FN8", AmstradCPC::Key::KeyF8},
|
|
|
|
{"FN9", AmstradCPC::Key::KeyF9},
|
|
|
|
//TODO: { } \ ' KOF
|
|
|
|
};
|
|
|
|
const auto name_pair = names.find(name);
|
|
|
|
if(name_pair == names.end()) {
|
|
|
|
throw CSL::InvalidArgument;
|
|
|
|
}
|
|
|
|
press(name_pair->second);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
while(append_typed(down, up, stream));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
std::vector<CSL::Instruction> CSL::parse(const std::string &file_name) {
|
|
|
|
std::vector<Instruction> instructions;
|
2024-06-16 18:27:48 +00:00
|
|
|
std::ifstream file;
|
|
|
|
file.open(file_name);
|
|
|
|
|
|
|
|
using Type = Instruction::Type;
|
|
|
|
static const std::unordered_map<std::string, Type> keywords = {
|
|
|
|
{"csl_version", Type::Version},
|
|
|
|
{"reset", Type::Reset},
|
|
|
|
{"crtc_select", Type::CRTCSelect},
|
|
|
|
{"disk_insert", Type::DiskInsert},
|
|
|
|
{"disk_dir", Type::SetDiskDir},
|
|
|
|
{"tape_insert", Type::TapeInsert},
|
|
|
|
{"tape_dir", Type::SetTapeDir},
|
|
|
|
{"tape_play", Type::TapeInsert},
|
|
|
|
{"tape_stop", Type::TapeStop},
|
|
|
|
{"tape_rewind", Type::TapeRewind},
|
2024-06-19 02:16:23 +00:00
|
|
|
{"snapshot_load", Type::LoadSnapshot},
|
2024-06-16 18:27:48 +00:00
|
|
|
{"snapshot_dir", Type::SetSnapshotDir},
|
|
|
|
{"key_delay", Type::KeyDelay},
|
|
|
|
{"key_output", Type::KeyOutput},
|
|
|
|
{"key_from_file", Type::KeyFromFile},
|
|
|
|
{"wait", Type::Wait},
|
|
|
|
{"wait_driveonoff", Type::WaitDriveOnOff},
|
|
|
|
{"wait_ssm0000", Type::WaitSSM0000},
|
|
|
|
{"screenshot_name", Type::SetScreenshotName},
|
|
|
|
{"screenshot_dir", Type::SetScreenshotDir},
|
|
|
|
{"screenshot", Type::Screenshot},
|
|
|
|
{"snapshot_name", Type::SetSnapshotDir},
|
|
|
|
{"csl_load", Type::LoadCSL},
|
|
|
|
};
|
|
|
|
|
|
|
|
for(std::string line; std::getline(file, line); ) {
|
2024-06-19 02:16:23 +00:00
|
|
|
// Ignore comments and empty lines.
|
|
|
|
if(line.empty() || line[0] == ';') {
|
2024-06-16 18:27:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::istringstream stream(line);
|
|
|
|
std::string keyword;
|
|
|
|
stream >> keyword;
|
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
// Second way for a line to be empty: purely whitespace.
|
|
|
|
if(keyword.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-06-16 18:27:48 +00:00
|
|
|
const auto key_pair = keywords.find(keyword);
|
|
|
|
if(key_pair == keywords.end()) {
|
|
|
|
throw InvalidKeyword;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction instruction;
|
|
|
|
instruction.type = key_pair->second;
|
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
// TODO: strings are encoded specially in order to capture whitespace.
|
|
|
|
// They're surrounded in single quotes with some special keys escaped.
|
2024-06-16 18:27:48 +00:00
|
|
|
const auto require = [&](auto &&target) {
|
|
|
|
stream >> target;
|
2024-06-19 02:16:23 +00:00
|
|
|
if(stream.fail()) {
|
2024-06-16 18:27:48 +00:00
|
|
|
throw InvalidArgument;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(instruction.type) {
|
2024-06-19 02:16:23 +00:00
|
|
|
// Keywords with no argument.
|
|
|
|
case Type::TapePlay:
|
|
|
|
case Type::TapeStop:
|
|
|
|
case Type::TapeRewind:
|
|
|
|
case Type::WaitVsyncOnOff:
|
|
|
|
case Type::WaitSSM0000:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Keywords with a single string mandatory argument
|
|
|
|
// that can be directly captured as a string.
|
2024-06-16 18:27:48 +00:00
|
|
|
case Type::Version: {
|
|
|
|
std::string argument;
|
|
|
|
require(argument);
|
|
|
|
instruction.argument = argument;
|
|
|
|
} break;
|
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
// Keywords with a single string mandatory argument
|
|
|
|
// that is within quotes but otherwise directly usable
|
|
|
|
// as a string.
|
|
|
|
case Type::LoadCSL:
|
|
|
|
case Type::SetScreenshotDir:
|
|
|
|
case Type::SetScreenshotName:
|
|
|
|
case Type::SetSnapshotDir:
|
|
|
|
case Type::SetSnapshotName:
|
|
|
|
case Type::LoadSnapshot:
|
|
|
|
case Type::SetTapeDir:
|
|
|
|
case Type::TapeInsert:
|
|
|
|
case Type::SetDiskDir:
|
|
|
|
case Type::KeyFromFile: {
|
|
|
|
std::string argument;
|
|
|
|
|
|
|
|
char next;
|
|
|
|
stream >> next;
|
|
|
|
if(next != '\'') {
|
|
|
|
throw InvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(true) {
|
2024-06-21 02:13:19 +00:00
|
|
|
next = static_cast<char>(stream.get());
|
|
|
|
if(stream.eof()) break;
|
2024-06-19 02:16:23 +00:00
|
|
|
|
|
|
|
// Take a bit of a random guess about what's escaped
|
|
|
|
// in regular string arguments.
|
|
|
|
if(next == '\\' && stream.peek() == '(') {
|
|
|
|
stream.get();
|
|
|
|
if(stream.peek() != '\'') {
|
|
|
|
argument.push_back('\\');
|
|
|
|
argument.push_back('(');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(next == '\'') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
argument.push_back(next);
|
|
|
|
}
|
|
|
|
instruction.argument = argument;
|
|
|
|
} break;
|
|
|
|
|
2024-06-16 18:27:48 +00:00
|
|
|
// Keywords with a single number mandatory argument.
|
2024-06-19 02:16:23 +00:00
|
|
|
case Type::WaitDriveOnOff:
|
2024-06-16 18:27:48 +00:00
|
|
|
case Type::Wait: {
|
|
|
|
uint64_t argument;
|
|
|
|
require(argument);
|
|
|
|
instruction.argument = argument;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
// Miscellaneous:
|
2024-06-19 02:16:23 +00:00
|
|
|
case Type::Snapshot:
|
|
|
|
case Type::Screenshot: {
|
|
|
|
std::string vsync;
|
|
|
|
stream >> vsync;
|
|
|
|
if(stream.fail()) {
|
|
|
|
instruction.argument = ScreenshotOrSnapshot::Now;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(vsync != "vsync") {
|
|
|
|
throw InvalidArgument;
|
|
|
|
}
|
|
|
|
instruction.argument = ScreenshotOrSnapshot::WaitForVSync;
|
|
|
|
} break;
|
|
|
|
|
2024-06-16 18:27:48 +00:00
|
|
|
case Type::Reset: {
|
|
|
|
std::string type;
|
|
|
|
stream >> type;
|
2024-06-19 02:16:23 +00:00
|
|
|
if(!stream.fail()) {
|
2024-06-16 18:27:48 +00:00
|
|
|
if(type != "soft" && type != "hard") {
|
|
|
|
throw InvalidArgument;
|
|
|
|
}
|
2024-06-19 02:16:23 +00:00
|
|
|
instruction.argument = (type == "soft") ? Reset::Soft : Reset::Hard;
|
2024-06-16 18:27:48 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Type::CRTCSelect: {
|
|
|
|
std::string type;
|
|
|
|
require(type);
|
|
|
|
|
|
|
|
static const std::set<std::string> allowed_types = {
|
|
|
|
"0", "1", "1A", "1B", "2", "3", "4",
|
|
|
|
};
|
|
|
|
if(allowed_types.find(type) == allowed_types.end()) {
|
|
|
|
throw InvalidArgument;
|
|
|
|
}
|
|
|
|
|
|
|
|
instruction.argument = static_cast<uint64_t>(std::stoi(type));
|
|
|
|
} break;
|
|
|
|
|
2024-06-19 02:16:23 +00:00
|
|
|
case Type::DiskInsert: {
|
|
|
|
std::string name;
|
|
|
|
require(name);
|
|
|
|
|
|
|
|
DiskInsert argument;
|
|
|
|
if(name.size() == 1) {
|
|
|
|
argument.drive = toupper(name[0]) - 'A';
|
|
|
|
require(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
argument.file = name;
|
|
|
|
instruction.argument = argument;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Type::KeyOutput: {
|
|
|
|
std::vector<KeyEvent> argument;
|
|
|
|
|
|
|
|
char next;
|
|
|
|
stream >> next;
|
|
|
|
if(next != '\'') {
|
|
|
|
throw InvalidArgument;
|
|
|
|
}
|
|
|
|
|
2024-06-21 02:13:19 +00:00
|
|
|
std::vector<KeyEvent> down;
|
|
|
|
std::vector<KeyEvent> up;
|
|
|
|
while(append_typed(down, up, stream)) {
|
|
|
|
std::copy(down.begin(), down.end(), std::back_inserter(argument));
|
|
|
|
std::copy(up.begin(), up.end(), std::back_inserter(argument));
|
|
|
|
down.clear();
|
|
|
|
up.clear();
|
2024-06-19 02:16:23 +00:00
|
|
|
}
|
|
|
|
instruction.argument = argument;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Type::KeyDelay: {
|
|
|
|
KeyDelay argument;
|
|
|
|
require(argument.press_delay);
|
|
|
|
require(argument.interpress_delay);
|
|
|
|
|
|
|
|
uint64_t carriage_return_delay;
|
|
|
|
stream >> carriage_return_delay;
|
|
|
|
if(!stream.fail()) {
|
|
|
|
argument.carriage_return_delay = carriage_return_delay;
|
|
|
|
}
|
|
|
|
instruction.argument = argument;
|
|
|
|
} break;
|
2024-06-16 18:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
instructions.push_back(std::move(instruction));
|
|
|
|
}
|
2024-06-19 02:16:23 +00:00
|
|
|
|
|
|
|
return instructions;
|
2024-06-16 18:27:48 +00:00
|
|
|
}
|