From 2717799df459a859d7edade1293d2569a6df984a Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 26 Jun 2022 01:41:58 +0200 Subject: [PATCH] - fix for busy loop in console_posix (due to poll with 0ms timeout) - disable kw11-l interrupt when emulation is not running --- ESP32/main.ino | 6 +++--- console_posix.cpp | 2 +- cpu.cpp | 4 ++++ kw11-l.cpp | 17 ++++++++++++----- kw11-l.h | 4 +++- main.cpp | 4 ++-- utils.cpp | 1 + 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/ESP32/main.ino b/ESP32/main.ino index 236abca..9ae4471 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -181,14 +181,14 @@ void setup() { Serial.println(F("Connect CPU to BUS")); b->add_cpu(c); - Serial.println(F("Start line-frequency interrupt")); - kw11_l *lf = new kw11_l(b); - c->setEmulateMFPT(true); Serial.println(F("Init console")); cnsl = new console_esp32(&stop_event, b); + Serial.println(F("Start line-frequency interrupt")); + kw11_l *lf = new kw11_l(b, cnsl); + running = cnsl->get_running_flag(); Serial.println(F("Init TTY")); diff --git a/console_posix.cpp b/console_posix.cpp index 9d7b3b8..a999fb8 100644 --- a/console_posix.cpp +++ b/console_posix.cpp @@ -32,7 +32,7 @@ int console_posix::wait_for_char_ll(const short timeout) { struct pollfd fds[] = { { STDIN_FILENO, POLLIN, timeout } }; - if (poll(fds, 1, 0) == 1 && fds[0].revents) + if (poll(fds, 1, timeout) == 1 && fds[0].revents) return getchar(); return -1; diff --git a/cpu.cpp b/cpu.cpp index cf2dbe6..8496683 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -2232,6 +2232,10 @@ void cpu::step_b() try { uint16_t instr = b->readWord(temp_pc); +// FILE *fh = fopen("/home/folkert/kek.dat", "a+"); +// fprintf(fh, "%06o %06o\n", temp_pc, instr); +// fclose(fh); + addRegister(7, false, 2); if (double_operand_instructions(instr)) diff --git a/kw11-l.cpp b/kw11-l.cpp index 18637b6..e208c4b 100644 --- a/kw11-l.cpp +++ b/kw11-l.cpp @@ -1,10 +1,12 @@ #include +#include "console.h" #include "cpu.h" #include "kw11-l.h" +#include "utils.h" -kw11_l::kw11_l(bus *const b) : b(b) +kw11_l::kw11_l(bus *const b, console *const cnsl) : b(b), cnsl(cnsl) { th = new std::thread(std::ref(*this)); } @@ -21,11 +23,16 @@ kw11_l::~kw11_l() void kw11_l::operator()() { while(!stop_flag) { - b->set_lf_crs_b7(); + if (*cnsl->get_running_flag()) { + b->set_lf_crs_b7(); - if (b->get_lf_crs() & 64) - b->getCpu()->queue_interrupt(6, 0100); + if (b->get_lf_crs() & 64) + b->getCpu()->queue_interrupt(6, 0100); - usleep(1000000 / 50); + myusleep(1000000 / 50); + } + else { + myusleep(1000000 / 10); + } } } diff --git a/kw11-l.h b/kw11-l.h index 88158db..25ade31 100644 --- a/kw11-l.h +++ b/kw11-l.h @@ -8,11 +8,13 @@ class kw11_l { private: bus *const b { nullptr }; + console *const cnsl { nullptr }; + std::thread * th { nullptr }; std::atomic_bool stop_flag { false }; public: - kw11_l(bus *const b); + kw11_l(bus *const b, console *const cnsl); virtual ~kw11_l(); void operator()(); diff --git a/main.cpp b/main.cpp index 3fa826e..b5bb6da 100644 --- a/main.cpp +++ b/main.cpp @@ -168,8 +168,6 @@ int main(int argc, char *argv[]) c->set_34(mode_34); - kw11_l *lf = new kw11_l(b); - c->setEmulateMFPT(true); std::atomic_bool interrupt_emulation { false }; @@ -235,6 +233,8 @@ int main(int argc, char *argv[]) return 0; #endif + kw11_l *lf = new kw11_l(b, cnsl); + cnsl->start_thread(); if (run_debugger) diff --git a/utils.cpp b/utils.cpp index a8435cb..9583731 100644 --- a/utils.cpp +++ b/utils.cpp @@ -83,6 +83,7 @@ void myusleep(uint64_t us) struct timespec rem { 0, 0 }; int rc = nanosleep(&req, &rem); + if (rc == 0 || (rc == -1 && errno != EINTR)) break;