deserialize

This commit is contained in:
folkert van heusden 2024-04-25 19:21:31 +02:00
parent e0bbef778d
commit 859577479f
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1

104
main.cpp
View file

@ -298,6 +298,7 @@ void get_metrics(cpu *const c)
void help()
{
printf("-h this help\n");
printf("-D x deserialize state from file\n");
printf("-T t.bin load file as a binary tape file (like simh \"load\" command), also for .BIC files\n");
printf("-B run tape file as a unit test (for .BIC files)\n");
printf("-R d.rk load file as a RK05 disk device\n");
@ -372,14 +373,20 @@ int main(int argc, char *argv[])
bool metrics = false;
std::string deserialize;
int opt = -1;
while((opt = getopt(argc, argv, "hMT:Br:R:p:ndtL:bl:s:Q:N:J:XS:")) != -1)
while((opt = getopt(argc, argv, "hD:MT:Br:R:p:ndtL:bl:s:Q:N:J:XS:")) != -1)
{
switch(opt) {
case 'h':
help();
return 1;
case 'D':
deserialize = optarg;
break;
case 'M':
metrics = true;
break;
@ -500,7 +507,10 @@ int main(int argc, char *argv[])
if (validate_json.empty() == false)
return run_cpu_validation(validate_json);
bus *b = new bus();
bus *b = nullptr;
if (deserialize.empty()) {
b = new bus();
if (set_ram_size.has_value())
b->set_memory_size(set_ram_size.value());
@ -512,35 +522,6 @@ int main(int argc, char *argv[])
cpu *c = new cpu(b, &event);
b->add_cpu(c);
std::atomic_bool interrupt_emulation { false };
std::optional<uint16_t> bic_start;
if (tape.empty() == false) {
bic_start = loadTape(b, tape);
if (bic_start.has_value() == false)
return 1; // fail
c->setRegister(7, bic_start.value());
}
if (sa_set)
c->setRegister(7, start_addr);
#if !defined(_WIN32)
if (withUI)
cnsl = new console_ncurses(&event, b);
else
#endif
{
DOLOG(info, true, "This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden.");
DOLOG(info, true, "Built on: " __DATE__ " " __TIME__);
cnsl = new console_posix(&event, b);
}
if (rk05_files.empty() == false) {
if (enable_bootloader == false)
DOLOG(warning, true, "Note: loading RK05 with no (RK05-) bootloader selected");
@ -562,13 +543,60 @@ int main(int argc, char *argv[])
if (enable_bootloader)
setBootLoader(b, bootloader);
running = cnsl->get_running_flag();
tty *tty_ = new tty(cnsl, b);
b->add_tty(tty_);
}
else {
FILE *fh = fopen(deserialize.c_str(), "r");
if (!fh)
error_exit(true, "Failed to open %s", deserialize.c_str());
DOLOG(info, true, "Start running at %06o", c->getRegister(7));
json_error_t je { };
json_t *j = json_loadf(fh, 0, &je);
fclose(fh);
if (!j)
error_exit(true, "State file %s is corrupt: %s", deserialize.c_str(), je.text);
b = bus::deserialize(j, cnsl, &event);
json_decref(j);
}
running = cnsl->get_running_flag();
std::atomic_bool interrupt_emulation { false };
std::optional<uint16_t> bic_start;
if (tape.empty() == false) {
bic_start = loadTape(b, tape);
if (bic_start.has_value() == false)
return 1; // fail
b->getCpu()->setRegister(7, bic_start.value());
}
if (sa_set)
b->getCpu()->setRegister(7, start_addr);
#if !defined(_WIN32)
if (withUI)
cnsl = new console_ncurses(&event, b);
else
#endif
{
DOLOG(info, true, "This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden.");
DOLOG(info, true, "Built on: " __DATE__ " " __TIME__);
cnsl = new console_posix(&event, b);
}
DOLOG(info, true, "Start running at %06o", b->getCpu()->getRegister(7));
#if !defined(_WIN32)
struct sigaction sa { };
@ -588,7 +616,7 @@ int main(int argc, char *argv[])
std::thread *metrics_thread = nullptr;
if (metrics)
metrics_thread = new std::thread(get_metrics, c);
metrics_thread = new std::thread(get_metrics, b->getCpu());
cnsl->start_thread();
@ -599,13 +627,13 @@ int main(int argc, char *argv[])
else if (run_debugger || (bootloader == BL_NONE && test.empty() && tape.empty()))
debugger(cnsl, b, &event, tracing);
else {
c->emulation_start(); // for statistics
b->getCpu()->emulation_start(); // for statistics
for(;;) {
*running = true;
while(event == EVENT_NONE)
c->step();
b->getCpu()->step();
*running = false;
@ -615,7 +643,7 @@ int main(int argc, char *argv[])
break;
}
auto stats = c->get_mips_rel_speed({ }, { });
auto stats = b->getCpu()->get_mips_rel_speed({ }, { });
cnsl->put_string_lf(format("MIPS: %.2f, relative speed: %.2f%%, instructions executed: %" PRIu64 " in %.2f seconds", std::get<0>(stats), std::get<1>(stats), std::get<2>(stats), std::get<3>(stats) / 1000000.));
}