From 1d87ecc570e33a567d958fb212ffabff920fbfa2 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 8 Apr 2023 15:08:13 +0200 Subject: [PATCH] debugger runs --- ESP32/main.ino | 13 +----------- RP2040/platformio.ini | 2 +- bus.cpp | 4 ++++ console.cpp | 48 ++++++++++++++++++------------------------- console.h | 2 +- cpu.cpp | 15 ++++++++------ cpu.h | 2 +- tty.cpp | 22 ++++++++++++++------ utils.cpp | 2 +- 9 files changed, 54 insertions(+), 56 deletions(-) diff --git a/ESP32/main.ino b/ESP32/main.ino index 72a9c2d..7aa80f4 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -84,13 +84,6 @@ void console_thread_wrapper_panel(void *const c) cnsl->panel_update_thread(); } -void console_thread_wrapper_io(void *const c) -{ - console *const cnsl = reinterpret_cast(c); - - cnsl->operator()(); -} - uint32_t load_serial_speed_configuration() { File dataFile = LittleFS.open(SERIAL_CFG_FILE, "r"); @@ -516,8 +509,6 @@ void setup() { while(!Serial) delay(100); - Serial.println("..."); - Serial.println(F("This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden.")); Serial.println(F("Build on: " __DATE__ " " __TIME__)); @@ -590,9 +581,6 @@ void setup() { xTaskCreate(&console_thread_wrapper_panel, "panel", 2048, cnsl, 1, nullptr); #endif - Serial.println(F("Starting I/O")); - xTaskCreate(&console_thread_wrapper_io, "c-io", 2048, cnsl, 1, nullptr); - #if !defined(BUILD_FOR_RP2040) Serial.print(F("Free RAM after init: ")); Serial.println(ESP.getFreeHeap()); @@ -604,6 +592,7 @@ void setup() { Serial.flush(); + Serial.println(F("Starting I/O")); cnsl->start_thread(); cnsl->put_string_lf("PDP-11/70 emulator, (C) Folkert van Heusden"); diff --git a/RP2040/platformio.ini b/RP2040/platformio.ini index 85aead2..9c04d70 100644 --- a/RP2040/platformio.ini +++ b/RP2040/platformio.ini @@ -12,5 +12,5 @@ board_build.core = earlephilhower board_build.filesystem_size = 0.2m lib_deps = bblanchon/ArduinoJson@^6.19.4 adafruit/Adafruit NeoPixel@^1.10.4 -build_flags = -Ofast -DBUILD_FOR_RP2040=1 -DPIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS -DPICO_STDIO_USB=1 -DDEBUG_RP2040_PORT=Serial -DDEBUG_RP2040_CORE +build_flags = -Ofast -DBUILD_FOR_RP2040=1 -DPIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS -DPICO_STDIO_USB=1 -DDEBUG_RP2040_PORT=Serial -DDEBUG_RP2040_CORE -ggdb3 build_unflags = -Os diff --git a/bus.cpp b/bus.cpp index 90d7e40..58966d8 100644 --- a/bus.cpp +++ b/bus.cpp @@ -34,6 +34,10 @@ bus::bus() memset(pages, 0x00, sizeof pages); CPUERR = MMR0 = MMR1 = MMR2 = MMR3 = PIR = CSR = 0; + +#if defined(BUILD_FOR_RP2040) + xSemaphoreGive(lf_csr_lock); // initialize +#endif } bus::~bus() diff --git a/console.cpp b/console.cpp index 28e3002..2d950f7 100644 --- a/console.cpp +++ b/console.cpp @@ -9,13 +9,14 @@ #include #include -#include "rp2040.h" #include "console.h" #include "gen.h" #include "log.h" #include "utils.h" #if defined(BUILD_FOR_RP2040) +#include "rp2040.h" + void thread_wrapper_console(void *p) { console *const consolel = reinterpret_cast(p); @@ -31,6 +32,10 @@ console::console(std::atomic_uint32_t *const stop_event, bus *const b, const int t_height(t_height) { screen_buffer = new char[t_width * t_height](); + +#if defined(BUILD_FOR_RP2040) + xSemaphoreGive(input_lock); // initialize +#endif } console::~console() @@ -47,7 +52,7 @@ void console::start_thread() stop_thread_flag = false; #if defined(BUILD_FOR_RP2040) - xTaskCreate(&thread_wrapper_console, "console-l", 2048, this, 1, nullptr); + xTaskCreate(&thread_wrapper_console, "console", 2048, this, 1, nullptr); #elif !defined(ESP32) th = new std::thread(std::ref(*this)); #endif @@ -109,38 +114,23 @@ int console::get_char() std::optional console::wait_char(const int timeout_ms) { #if defined(BUILD_FOR_RP2040) + uint8_t rc = 0; + if (xQueueReceive(have_data, &rc, timeout_ms / portTICK_PERIOD_MS) == pdFALSE || rc == 0) + return { }; + + std::optional c { }; + xSemaphoreTake(input_lock, portMAX_DELAY); - bool triggered = input_buffer.empty() == false; - - if (!triggered) { - uint32_t start = millis(); - - while(!have_data && millis() - start < timeout_ms) { - } - - have_data = false; // FIXME - - triggered = input_buffer.empty() == false; - } - - if (triggered) { - int c = input_buffer.at(0); + if (input_buffer.empty() == false) { + c = input_buffer.at(0); input_buffer.erase(input_buffer.begin() + 0); - -#if defined(BUILD_FOR_RP2040) - xSemaphoreGive(input_lock); -#endif - - return c; } -#if defined(BUILD_FOR_RP2040) xSemaphoreGive(input_lock); -#endif - return { }; + return c; #else std::unique_lock lck(input_lock); @@ -333,9 +323,11 @@ void console::operator()() input_buffer.push_back(c); #if defined(BUILD_FOR_RP2040) - have_data = true; - xSemaphoreGive(input_lock); + + uint8_t value = 1; + if (xQueueSend(have_data, &value, portMAX_DELAY) == pdFALSE) + Serial.println("xQueueSend failed"); #else have_data.notify_all(); #endif diff --git a/console.h b/console.h index e03129a..4ba1e3b 100644 --- a/console.h +++ b/console.h @@ -21,7 +21,7 @@ class console private: std::vector input_buffer; #if defined(BUILD_FOR_RP2040) - volatile bool have_data { false }; + QueueHandle_t have_data { xQueueCreate(16, 1) }; SemaphoreHandle_t input_lock { xSemaphoreCreateBinary() }; #else std::condition_variable have_data; diff --git a/cpu.cpp b/cpu.cpp index 9becf0f..543ef5e 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -18,6 +18,10 @@ cpu::cpu(bus *const b, std::atomic_uint32_t *const event) : b(b), event(event) { reset(); + +#if defined(BUILD_FOR_RP2040) + xSemaphoreGive(qi_lock); // initialize +#endif } cpu::~cpu() @@ -309,9 +313,10 @@ void cpu::queue_interrupt(const uint8_t level, const uint8_t vector) it->second.insert(vector); #if defined(BUILD_FOR_RP2040) - qi_cv = true; - xSemaphoreGive(qi_lock); + + uint8_t value = 1; + xQueueSend(qi_q, &value, portMAX_DELAY); #else qi_cv.notify_all(); @@ -1520,10 +1525,8 @@ bool cpu::misc_operations(const uint16_t instr) case 0b0000000000000001: // WAIT { #if defined(BUILD_FOR_RP2040) - while(!qi_cv) - vTaskDelay(10); - - qi_cv = false; // FIXME + uint8_t rc = 0; + xQueueReceive(qi_q, &rc, 0); #else std::unique_lock lck(qi_lock); diff --git a/cpu.h b/cpu.h index db581d8..ff973d2 100644 --- a/cpu.h +++ b/cpu.h @@ -44,7 +44,7 @@ private: std::map > queued_interrupts; #if defined(BUILD_FOR_RP2040) SemaphoreHandle_t qi_lock { xSemaphoreCreateBinary() }; - volatile bool qi_cv { false }; + QueueHandle_t qi_q { xQueueCreate(16, 1) }; #else std::mutex qi_lock; std::condition_variable qi_cv; diff --git a/tty.cpp b/tty.cpp index 96e99ef..4a0fb34 100644 --- a/tty.cpp +++ b/tty.cpp @@ -34,7 +34,11 @@ tty::tty(console *const c, bus *const b) : b(b) { #if defined(BUILD_FOR_RP2040) - xTaskCreate(&thread_wrapper_tty, "tty-l", 2048, this, 1, nullptr); + xSemaphoreGive(chars_lock); // initialize +#endif + +#if defined(BUILD_FOR_RP2040) + xTaskCreate(&thread_wrapper_tty, "tty", 2048, this, 1, nullptr); #else th = new std::thread(std::ref(*this)); #endif @@ -43,6 +47,7 @@ tty::tty(console *const c, bus *const b) : tty::~tty() { stop_flag = true; + #if !defined(BUILD_FOR_RP2040) th->join(); delete th; @@ -69,8 +74,9 @@ void tty::notify_rx() uint16_t tty::readWord(const uint16_t addr) { - const int reg = (addr - PDP11TTY_BASE) / 2; - uint16_t vtemp = registers[reg]; + const int reg = (addr - PDP11TTY_BASE) / 2; + uint16_t vtemp = registers[reg]; + bool notify = false; #if defined(BUILD_FOR_RP2040) xSemaphoreTake(chars_lock, portMAX_DELAY); @@ -94,7 +100,7 @@ uint16_t tty::readWord(const uint16_t addr) vtemp = ch | (parity(ch) << 7); if (chars.empty() == false) - notify_rx(); + notify = true; } } else if (addr == PDP11TTY_TPS) { @@ -109,6 +115,9 @@ uint16_t tty::readWord(const uint16_t addr) registers[reg] = vtemp; + if (notify) + notify_rx(); + return vtemp; } @@ -117,6 +126,7 @@ void tty::operator()() while(!stop_flag) { if (c->poll_char()) { #if defined(BUILD_FOR_RP2040) + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); xSemaphoreTake(chars_lock, portMAX_DELAY); #else std::unique_lock lck(chars_lock); @@ -124,11 +134,11 @@ void tty::operator()() chars.push_back(c->get_char()); - notify_rx(); - #if defined(BUILD_FOR_RP2040) xSemaphoreGive(chars_lock); #endif + + notify_rx(); } else { myusleep(100000); diff --git a/utils.cpp b/utils.cpp index 317cb6b..f93bb20 100644 --- a/utils.cpp +++ b/utils.cpp @@ -3,6 +3,7 @@ #if defined(ESP32) || defined(BUILD_FOR_RP2040) #include +#include "rp2040.h" #endif #include @@ -16,7 +17,6 @@ #include #include -#include "rp2040.h" #include "win32.h"