From 108de1ccba2f67af3924ff1f101f0754af0e69c2 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 08:54:54 +0200 Subject: [PATCH 01/47] overlay chatty debug removed --- rl02.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rl02.cpp b/rl02.cpp index a62067f..6974218 100644 --- a/rl02.cpp +++ b/rl02.cpp @@ -365,7 +365,7 @@ void rl02::writeWord(const uint16_t addr, uint16_t v) *disk_read_activity = false; } else { - DOLOG(warning, false, "RL02: command %d not implemented", command); + DOLOG(debug, false, "RL02: command %d not implemented", command); } if (do_int) { From 4c921738b4833b09b19e0c4a9a3a0ab5cba7e992 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 09:36:48 +0200 Subject: [PATCH 02/47] tweaks --- CMakeLists.txt | 4 ++-- ESP32/platformio.ini | 2 +- breakpoint_register.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bdb83e8..70291a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,8 @@ cmake_minimum_required(VERSION 3.9) add_compile_options(-Wall -pedantic -Wextra) -#add_compile_options(-fsanitize=address) -#add_link_options(-fsanitize=address) +#add_compile_options(-fsanitize=undefined) +#add_link_options(-fsanitize=undefined) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) diff --git a/ESP32/platformio.ini b/ESP32/platformio.ini index 948ef3a..c085fdd 100644 --- a/ESP32/platformio.ini +++ b/ESP32/platformio.ini @@ -17,7 +17,7 @@ board_build.filesystem = littlefs lib_deps = greiman/SdFat@^2.1.2 adafruit/Adafruit NeoPixel bblanchon/ArduinoJson@^6.19.4 -build_flags = -std=gnu++2a -DESP32=1 -ggdb3 -D_GLIBCXX_USE_C99 +build_flags = -std=gnu++2a -DESP32=1 -ggdb3 -D_GLIBCXX_USE_C99 -Wall build_unflags = -std=gnu++11 -std=gnu++17 extra_scripts = pre:prepare.py diff --git a/breakpoint_register.cpp b/breakpoint_register.cpp index 9a94f63..832aef0 100644 --- a/breakpoint_register.cpp +++ b/breakpoint_register.cpp @@ -101,12 +101,12 @@ std::pair > breakpoint_registe else if (key == "PC" || key == "pc") { return { new breakpoint_register(b, 7, values), { } }; } - else if (key.substr(0, 3) == "MMR" or key.substr(0, 3) == "mmr") { + else if (key.substr(0, 3) == "MMR" || key.substr(0, 3) == "mmr") { int which = key[3] - '0'; return { new breakpoint_register(b, hr_mmr0 + which, values), { } }; } - else if (key.substr(0, 3) == "PSW" or key.substr(0, 3) == "psw") { + else if (key.substr(0, 3) == "PSW" || key.substr(0, 3) == "psw") { return { new breakpoint_register(b, hr_psw, values), { } }; } From b3932c2100da5b9012b46c83bccd6b0c8a762fbd Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 12:23:30 +0200 Subject: [PATCH 03/47] init variables --- mmu.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/mmu.cpp b/mmu.cpp index 5e42949..17abfe5 100644 --- a/mmu.cpp +++ b/mmu.cpp @@ -9,6 +9,7 @@ mmu::mmu() { + reset(); } mmu::~mmu() From 19ea36c809ad921d47a114450051e3004acdd189 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 21:28:00 +0200 Subject: [PATCH 04/47] dp - stop panel --- ESP32/console_esp32.cpp | 8 ++++++-- ESP32/main.ino | 2 ++ ESP32/platformio.ini | 15 +++++++++++++++ console.h | 4 +++- console_ncurses.cpp | 2 +- debugger.cpp | 6 ++++++ 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/ESP32/console_esp32.cpp b/ESP32/console_esp32.cpp index d4efeb0..787733f 100644 --- a/ESP32/console_esp32.cpp +++ b/ESP32/console_esp32.cpp @@ -72,7 +72,6 @@ void console_esp32::panel_update_thread() pixels.begin(); pixels.clear(); - pixels.show(); constexpr uint8_t brightness = 16; @@ -103,7 +102,7 @@ void console_esp32::panel_update_thread() pixels.clear(); pixels.show(); - for(;;) { + while(!stop_panel) { vTaskDelay(20 / portTICK_PERIOD_MS); try { @@ -141,5 +140,10 @@ void console_esp32::panel_update_thread() put_string_lf("Unknown exception in panel thread"); } } + + pixels.clear(); + pixels.show(); + + Serial.println(F("panel task terminating")); #endif } diff --git a/ESP32/main.ino b/ESP32/main.ino index bb29dd8..cbe8a39 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -78,6 +78,8 @@ void console_thread_wrapper_panel(void *const c) console *const cnsl = reinterpret_cast(c); cnsl->panel_update_thread(); + + vTaskSuspend(nullptr); } uint32_t load_serial_speed_configuration() diff --git a/ESP32/platformio.ini b/ESP32/platformio.ini index 948ef3a..6fb61ee 100644 --- a/ESP32/platformio.ini +++ b/ESP32/platformio.ini @@ -35,3 +35,18 @@ lib_deps = greiman/SdFat@^2.1.2 build_flags = -std=gnu++2a -DESP32=1 -DSHA2017 -ggdb3 -D_GLIBCXX_USE_C99 -ISHAdisplay/Arduino/libraries/epd2in9-badge -ISHAdisplay/Arduino/libraries/epdpaint -ISHAdisplay/components/epaper-29-dke build_unflags = -std=gnu++11 -std=gnu++17 upload_protocol = esptool + +[env:ESP32-ttgo-t-beam] +build_src_filter = +<*> -<.git/> -<.svn/> - - - - - - - - +platform = espressif32 +board = ttgo-t-beam +framework = arduino +monitor_speed = 115200 +upload_speed = 1000000 +board_build.filesystem = littlefs +lib_deps = greiman/SdFat@^2.1.2 + adafruit/Adafruit NeoPixel + bblanchon/ArduinoJson@^6.19.4 +build_flags = -std=gnu++17 -DESP32=1 -ggdb3 -D_GLIBCXX_USE_C99 +build_unflags = -std=gnu++11 +extra_scripts = pre:prepare.py diff --git a/console.h b/console.h index 5cb932f..6101602 100644 --- a/console.h +++ b/console.h @@ -29,7 +29,8 @@ private: #endif protected: - std::atomic_uint32_t *const stop_event { nullptr }; + std::atomic_uint32_t *const stop_event { nullptr }; + std::atomic_bool stop_panel { false }; bus *b { nullptr }; #if !defined(BUILD_FOR_RP2040) @@ -88,5 +89,6 @@ public: std::atomic_bool * get_disk_read_activity_flag() { return &disk_read_activity_flag; } std::atomic_bool * get_disk_write_activity_flag() { return &disk_write_activity_flag; } + void stop_panel_thread() { stop_panel = true; } virtual void panel_update_thread() = 0; }; diff --git a/console_ncurses.cpp b/console_ncurses.cpp index df2e736..9ec7753 100644 --- a/console_ncurses.cpp +++ b/console_ncurses.cpp @@ -133,7 +133,7 @@ void console_ncurses::panel_update_thread() constexpr int refresh_rate = 50; - while(*stop_event != EVENT_TERMINATE) { + while(*stop_event != EVENT_TERMINATE && stop_panel == false) { myusleep(1000000 / refresh_rate); // note that these are approximately as there's no mutex on the emulation diff --git a/debugger.cpp b/debugger.cpp index c48241b..9d2244b 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -951,6 +951,11 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto continue; } + else if (cmd == "dp") { + cnsl->stop_panel_thread(); + + continue; + } else if (cmd == "bt") { if (c->get_debug() == false) cnsl->put_string_lf("Debug mode is disabled!"); @@ -1006,6 +1011,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto "ser - serialize state to a file", // "dser - deserialize state from a file", #endif + "dp - stop panel", #if defined(ESP32) "cfgnet - configure network (e.g. WiFi)", "startnet - start network", From ff1da92dc8248d67066f1abf4a5a5642291b6a9e Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 21:39:13 +0200 Subject: [PATCH 05/47] fix for syslog --- debugger.cpp | 7 +++++-- log.cpp | 9 ++++++--- log.h | 3 ++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/debugger.cpp b/debugger.cpp index 9d2244b..9b5fcb9 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -942,7 +942,10 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto } #endif else if (parts[0] == "setsl" && parts.size() == 3) { - setloghost(parts.at(1).c_str(), parse_ll(parts[2])); + if (setloghost(parts.at(1).c_str(), parse_ll(parts[2])) == false) + cnsl->put_string_lf("Failed parsing IP address"); + else + send_syslog(info, "Hello, world!"); continue; } @@ -1011,7 +1014,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto "ser - serialize state to a file", // "dser - deserialize state from a file", #endif - "dp - stop panel", + "dp - disable panel", #if defined(ESP32) "cfgnet - configure network (e.g. WiFi)", "startnet - start network", diff --git a/log.cpp b/log.cpp index d250081..df44581 100644 --- a/log.cpp +++ b/log.cpp @@ -54,16 +54,19 @@ void setlogfile(const char *const lf, const log_level_t ll_file, const log_level atexit(closelog); } -void setloghost(const char *const host, const log_level_t ll) +bool setloghost(const char *const host, const log_level_t ll) { - inet_aton(host, &syslog_ip_addr.sin_addr); - syslog_ip_addr.sin_port = htons(514); + syslog_ip_addr.sin_family = AF_INET; + bool ok = inet_aton(host, &syslog_ip_addr.sin_addr) == 1; + syslog_ip_addr.sin_port = htons(514); is_file = false; log_level_file = ll; l_timestamp = false; + + return ok; } void setll(const log_level_t ll_screen, const log_level_t ll_file) diff --git a/log.h b/log.h index 50b6ce5..668cf33 100644 --- a/log.h +++ b/log.h @@ -12,9 +12,10 @@ typedef enum { ll_emerg = 0, ll_alert, ll_critical, ll_error, warning, notice, i log_level_t parse_ll(const std::string & str); void setlogfile(const char *const lf, const log_level_t ll_file, const log_level_t ll_screen, const bool l_timestamp); -void setloghost(const char *const host, const log_level_t ll); +bool setloghost(const char *const host, const log_level_t ll); void setll(const log_level_t ll_screen, const log_level_t ll_file); void setloguid(const int uid, const int gid); +void send_syslog(const int ll, const std::string & what); void closelog(); void dolog(const log_level_t ll, const char *fmt, ...); From 9e71b84b1ac0edbf39d6854c90c6a5910a5a8da4 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 21:57:09 +0200 Subject: [PATCH 06/47] static -> constexpr const --- loaders.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/loaders.cpp b/loaders.cpp index 8752141..3c89805 100644 --- a/loaders.cpp +++ b/loaders.cpp @@ -30,7 +30,7 @@ void loadbin(bus *const b, uint16_t base, const char *const file) void set_boot_loader(bus *const b, const bootloader_t which) { - cpu *const c = b->getCpu(); + cpu *const c = b->getCpu(); uint16_t offset = 0; uint16_t start = 0; @@ -40,7 +40,7 @@ void set_boot_loader(bus *const b, const bootloader_t which) if (which == BL_RK05) { start = offset = 01000; - static uint16_t rk05_code[] = { + constexpr const uint16_t rk05_code[] = { 0012700, 0177406, 0012710, @@ -92,7 +92,7 @@ void set_boot_loader(bus *const b, const bootloader_t which) start = offset = 01000; /* from https://www.pdp-11.nl/peripherals/disk/rl-info.html - static uint16_t rl02_code[] = { + constexpr const uint16_t rl02_code[] = { 0012701, 0174400, 0012761, @@ -120,7 +120,7 @@ void set_boot_loader(bus *const b, const bootloader_t which) */ // from http://gunkies.org/wiki/RL11_disk_controller - static uint16_t rl02_code[] = { + constexpr const uint16_t rl02_code[] = { 0012700, 0174400, 0012760, From c591387481f0fddff3670afc6f15caac0afcc4c6 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 22:01:13 +0200 Subject: [PATCH 07/47] old code --- debugger.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/debugger.cpp b/debugger.cpp index 9b5fcb9..c276cbe 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -37,13 +37,9 @@ #include "rp2040.h" #endif -void set_boot_loader(bus *const b); - -void configure_disk(console *const c); - -void configure_network(console *const c); -void check_network(console *const c); -void start_network(console *const c); +void configure_network(console *const cnsl); +void check_network(console *const cnsl); +void start_network(console *const cnsl); void set_tty_serial_speed(console *const c, const uint32_t bps); #endif From 473f247bccaba7aae8152602ace0b0dd72ec0eea Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 22:43:24 +0200 Subject: [PATCH 08/47] debug (ESP32) --- debugger.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/debugger.cpp b/debugger.cpp index c276cbe..12c259a 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -821,6 +821,12 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto continue; } #if defined(ESP32) + else if (cmd == "debug") { + if (heap_caps_check_integrity(MALLOC_CAP_DEFAULT, true) == false) + cnsl->put_string_lf("HEAP corruption!"); + + continue; + } else if (cmd == "cfgnet") { configure_network(cnsl); @@ -1016,6 +1022,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto "startnet - start network", "chknet - check network status", "serspd - set serial speed in bps (8N1 are default)", + "debug - debugging info", #endif "cfgdisk - configure disk", nullptr From f9797d5e8e2493775f5d8197afdfe439ea9680c8 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 22:43:43 +0200 Subject: [PATCH 09/47] init NBD structures --- disk_backend_nbd.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/disk_backend_nbd.cpp b/disk_backend_nbd.cpp index 310e31c..031aede 100644 --- a/disk_backend_nbd.cpp +++ b/disk_backend_nbd.cpp @@ -91,10 +91,10 @@ bool disk_backend_nbd::connect(const bool retry) { do { // LOOP until connected, logging message, exponential backoff? - addrinfo *res = nullptr; + addrinfo *res = nullptr; addrinfo hints { 0 }; - hints.ai_family = AF_INET; + hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; char port_str[8] { 0 }; @@ -137,7 +137,7 @@ bool disk_backend_nbd::connect(const bool retry) uint64_t size; uint32_t flags; uint8_t padding[124]; - } nbd_hello; + } nbd_hello { }; if (fd != -1) { if (READ(fd, reinterpret_cast(&nbd_hello), sizeof nbd_hello) != sizeof nbd_hello) { @@ -194,7 +194,7 @@ bool disk_backend_nbd::read(const off_t offset_in, const size_t n, uint8_t *cons uint64_t handle; uint64_t offset; uint32_t length; - } nbd_request { 0 }; + } nbd_request { }; nbd_request.magic = ntohl(0x25609513); nbd_request.type = 0; // READ From 3453b1d9c39f480cd202e390701baeca6696fd22 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 22:50:17 +0200 Subject: [PATCH 10/47] debugging on ESP32 --- ESP32/main.ino | 15 +++++++++++++++ debugger.cpp | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ESP32/main.ino b/ESP32/main.ino index cbe8a39..15c3220 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -18,6 +18,9 @@ #include #include #endif +#if defined(ESP32) +#include "esp_heap_caps.h" +#endif #if defined(SHA2017) #include "console_shabadge.h" @@ -210,6 +213,14 @@ void set_tty_serial_speed(console *const c, const uint32_t bps) c->put_string_lf("Failed to store configuration file with serial settings"); } +#if defined(ESP32) +void heap_caps_alloc_failed_hook(size_t requested_size, uint32_t caps, const char *function_name) +{ + printf("%s was called but failed to allocate %d bytes with 0x%X capabilities\r\n", function_name, requested_size, caps); +} + +#endif + void setup() { Serial.begin(115200); @@ -224,6 +235,10 @@ void setup() { Serial.print(F("Size of int: ")); Serial.println(sizeof(int)); +#if defined(ESP32) + heap_caps_register_failed_alloc_callback(heap_caps_alloc_failed_hook); +#endif + #if !defined(BUILD_FOR_RP2040) Serial.print(F("CPU clock frequency (MHz): ")); Serial.println(getCpuFrequencyMhz()); diff --git a/debugger.cpp b/debugger.cpp index 12c259a..51e425b 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -822,7 +822,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto } #if defined(ESP32) else if (cmd == "debug") { - if (heap_caps_check_integrity(MALLOC_CAP_DEFAULT, true) == false) + if (heap_caps_check_integrity_all(true) == false) cnsl->put_string_lf("HEAP corruption!"); continue; From 1fb4682efb5a274979eab091f0ebb33f059385b2 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 23:02:16 +0200 Subject: [PATCH 11/47] good upto and including 3879721b2822f27b34be905afb9b11d9567587b2 --- ESP32/main.ino | 3 --- ESP32/mmu.cpp | 1 + ESP32/mmu.h | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) create mode 120000 ESP32/mmu.cpp create mode 120000 ESP32/mmu.h diff --git a/ESP32/main.ino b/ESP32/main.ino index 3407eef..1cc5a97 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -291,9 +291,6 @@ void setup() { cnsl = new console_esp32(&stop_event, b, serial_ports, 80, 25); #endif - 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/ESP32/mmu.cpp b/ESP32/mmu.cpp new file mode 120000 index 0000000..b83d63b --- /dev/null +++ b/ESP32/mmu.cpp @@ -0,0 +1 @@ +../mmu.cpp \ No newline at end of file diff --git a/ESP32/mmu.h b/ESP32/mmu.h new file mode 120000 index 0000000..cc56dfb --- /dev/null +++ b/ESP32/mmu.h @@ -0,0 +1 @@ +../mmu.h \ No newline at end of file From 21b2f0c61b7cb95e7530edfa2e8710ba221ce689 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 23:18:14 +0200 Subject: [PATCH 12/47] constants removal --- bus.cpp | 16 +++++++--------- bus.h | 4 ---- debugger.cpp | 3 ++- memory.h | 2 ++ 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/bus.cpp b/bus.cpp index 5d73884..232d82d 100644 --- a/bus.cpp +++ b/bus.cpp @@ -120,8 +120,6 @@ bus *bus::deserialize(const json_t *const j, console *const cnsl, std::atomic_ui void bus::set_memory_size(const int n_pages) { - this->n_pages = n_pages; - uint32_t n_bytes = n_pages * 8192l; delete m; @@ -474,7 +472,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm } // LO size register field must be all 1s, so subtract 1 - uint32_t system_size = n_pages * 8192l / 64 - 1; + uint32_t system_size = m->get_memory_size() / 64 - 1; if (a == ADDR_SYSSIZE + 2) { // system size HI uint16_t temp = system_size >> 16; @@ -505,7 +503,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm return 0; } - if (m_offset >= uint32_t(n_pages * 8192)) { + if (m_offset >= m->get_memory_size()) { if (peek_only) { DOLOG(debug, false, "READ from %06o - out of range!", addr_in); return 0; @@ -696,8 +694,8 @@ uint32_t bus::calculate_physical_address(const int run_mode, const uint16_t a, c } } - if (m_offset >= n_pages * 8192l && !is_io) [[unlikely]] { - DOLOG(debug, !peek_only, "bus::calculate_physical_address %o >= %o", m_offset, n_pages * 8192l); + if (m_offset >= m->get_memory_size() && !is_io) [[unlikely]] { + DOLOG(debug, !peek_only, "bus::calculate_physical_address %o >= %o", m_offset, m->get_memory_size()); DOLOG(debug, false, "TRAP(04) (throw 6) on address %06o", a); if (mmu_->is_locked() == false) { @@ -1025,7 +1023,7 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1 DOLOG(debug, false, "WRITE to %06o/%07o %c %c: %06o", addr_in, m_offset, space == d_space ? 'D' : 'I', word_mode == wm_byte ? 'B' : 'W', value); - if (m_offset >= uint32_t(n_pages * 8192)) { + if (m_offset >= m->get_memory_size()) { c->trap(004); // no such RAM throw 1; } @@ -1042,7 +1040,7 @@ void bus::writePhysical(const uint32_t a, const uint16_t value) { DOLOG(debug, false, "physicalWRITE %06o to %o", value, a); - if (a >= n_pages * 8192l) { + if (a >= m->get_memory_size()) { DOLOG(debug, false, "physicalWRITE to %o: trap 004", a); c->trap(004); throw 12; @@ -1054,7 +1052,7 @@ void bus::writePhysical(const uint32_t a, const uint16_t value) uint16_t bus::readPhysical(const uint32_t a) { - if (a >= n_pages * 8192l) { + if (a >= m->get_memory_size()) { DOLOG(debug, false, "physicalREAD from %o: trap 004", a); c->trap(004); throw 13; diff --git a/bus.h b/bus.h index b0ae4c8..d0fdd6b 100644 --- a/bus.h +++ b/bus.h @@ -75,10 +75,7 @@ private: rl02 *rl02_ { nullptr }; tty *tty_ { nullptr }; kw11_l *kw11_l_ { nullptr }; - mmu *mmu_ { nullptr }; - - int n_pages { DEFAULT_N_PAGES }; memory *m { nullptr }; uint16_t microprogram_break_register { 0 }; @@ -104,7 +101,6 @@ public: void set_debug_mode() { console_switches |= 128; } uint16_t get_console_leds() { return console_leds; } - int get_memory_size() const { return n_pages; } void set_memory_size(const int n_pages); void mmudebug(const uint16_t a); diff --git a/debugger.cpp b/debugger.cpp index 30da333..189d17f 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -27,6 +27,7 @@ #include "disk_backend_nbd.h" #include "loaders.h" #include "log.h" +#include "memory.h" #include "tty.h" #include "utils.h" @@ -1018,7 +1019,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto if (parts.size() == 2) b->set_memory_size(std::stoi(parts.at(1))); else { - int n_pages = b->get_memory_size(); + int n_pages = b->getRAM()->get_memory_size(); cnsl->put_string_lf(format("Memory size: %u pages or %u kB (decimal)", n_pages, n_pages * 8192 / 1024)); } diff --git a/memory.h b/memory.h index 1a5dcbc..8ec1c14 100644 --- a/memory.h +++ b/memory.h @@ -18,6 +18,8 @@ public: memory(const uint32_t size); ~memory(); + uint32_t get_memory_size() const { return size; } + void reset(); #if IS_POSIX json_t *serialize() const; From fef50688aaa3ea05748591e4d47c0581394b7501 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 23:18:14 +0200 Subject: [PATCH 13/47] constants removal --- bus.cpp | 16 +++++++--------- bus.h | 4 ---- debugger.cpp | 3 ++- memory.h | 2 ++ 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/bus.cpp b/bus.cpp index 5d73884..232d82d 100644 --- a/bus.cpp +++ b/bus.cpp @@ -120,8 +120,6 @@ bus *bus::deserialize(const json_t *const j, console *const cnsl, std::atomic_ui void bus::set_memory_size(const int n_pages) { - this->n_pages = n_pages; - uint32_t n_bytes = n_pages * 8192l; delete m; @@ -474,7 +472,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm } // LO size register field must be all 1s, so subtract 1 - uint32_t system_size = n_pages * 8192l / 64 - 1; + uint32_t system_size = m->get_memory_size() / 64 - 1; if (a == ADDR_SYSSIZE + 2) { // system size HI uint16_t temp = system_size >> 16; @@ -505,7 +503,7 @@ uint16_t bus::read(const uint16_t addr_in, const word_mode_t word_mode, const rm return 0; } - if (m_offset >= uint32_t(n_pages * 8192)) { + if (m_offset >= m->get_memory_size()) { if (peek_only) { DOLOG(debug, false, "READ from %06o - out of range!", addr_in); return 0; @@ -696,8 +694,8 @@ uint32_t bus::calculate_physical_address(const int run_mode, const uint16_t a, c } } - if (m_offset >= n_pages * 8192l && !is_io) [[unlikely]] { - DOLOG(debug, !peek_only, "bus::calculate_physical_address %o >= %o", m_offset, n_pages * 8192l); + if (m_offset >= m->get_memory_size() && !is_io) [[unlikely]] { + DOLOG(debug, !peek_only, "bus::calculate_physical_address %o >= %o", m_offset, m->get_memory_size()); DOLOG(debug, false, "TRAP(04) (throw 6) on address %06o", a); if (mmu_->is_locked() == false) { @@ -1025,7 +1023,7 @@ write_rc_t bus::write(const uint16_t addr_in, const word_mode_t word_mode, uint1 DOLOG(debug, false, "WRITE to %06o/%07o %c %c: %06o", addr_in, m_offset, space == d_space ? 'D' : 'I', word_mode == wm_byte ? 'B' : 'W', value); - if (m_offset >= uint32_t(n_pages * 8192)) { + if (m_offset >= m->get_memory_size()) { c->trap(004); // no such RAM throw 1; } @@ -1042,7 +1040,7 @@ void bus::writePhysical(const uint32_t a, const uint16_t value) { DOLOG(debug, false, "physicalWRITE %06o to %o", value, a); - if (a >= n_pages * 8192l) { + if (a >= m->get_memory_size()) { DOLOG(debug, false, "physicalWRITE to %o: trap 004", a); c->trap(004); throw 12; @@ -1054,7 +1052,7 @@ void bus::writePhysical(const uint32_t a, const uint16_t value) uint16_t bus::readPhysical(const uint32_t a) { - if (a >= n_pages * 8192l) { + if (a >= m->get_memory_size()) { DOLOG(debug, false, "physicalREAD from %o: trap 004", a); c->trap(004); throw 13; diff --git a/bus.h b/bus.h index 1e8db64..800e926 100644 --- a/bus.h +++ b/bus.h @@ -75,10 +75,7 @@ private: rl02 *rl02_ { nullptr }; tty *tty_ { nullptr }; kw11_l *kw11_l_ { nullptr }; - mmu *mmu_ { nullptr }; - - int n_pages { DEFAULT_N_PAGES }; memory *m { nullptr }; uint16_t microprogram_break_register { 0 }; @@ -104,7 +101,6 @@ public: void set_debug_mode() { console_switches |= 128; } uint16_t get_console_leds() { return console_leds; } - int get_memory_size() const { return n_pages; } void set_memory_size(const int n_pages); void mmudebug(const uint16_t a); diff --git a/debugger.cpp b/debugger.cpp index 51e425b..ee35957 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -26,6 +26,7 @@ #include "disk_backend_nbd.h" #include "loaders.h" #include "log.h" +#include "memory.h" #include "tty.h" #include "utils.h" @@ -865,7 +866,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto if (parts.size() == 2) b->set_memory_size(std::stoi(parts.at(1))); else { - int n_pages = b->get_memory_size(); + int n_pages = b->getRAM()->get_memory_size(); cnsl->put_string_lf(format("Memory size: %u pages or %u kB (decimal)", n_pages, n_pages * 8192 / 1024)); } diff --git a/memory.h b/memory.h index 1a5dcbc..8ec1c14 100644 --- a/memory.h +++ b/memory.h @@ -18,6 +18,8 @@ public: memory(const uint32_t size); ~memory(); + uint32_t get_memory_size() const { return size; } + void reset(); #if IS_POSIX json_t *serialize() const; From 48e88b49d9f9704df2e48c257969a698b0fb5473 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 23:37:03 +0200 Subject: [PATCH 14/47] win32.* should not be in ESP32 folder --- ESP32/win32.cpp | 1 - ESP32/win32.h | 1 - log.cpp | 2 ++ utils.cpp | 2 ++ 4 files changed, 4 insertions(+), 2 deletions(-) delete mode 120000 ESP32/win32.cpp delete mode 120000 ESP32/win32.h diff --git a/ESP32/win32.cpp b/ESP32/win32.cpp deleted file mode 120000 index caccb16..0000000 --- a/ESP32/win32.cpp +++ /dev/null @@ -1 +0,0 @@ -../win32.cpp \ No newline at end of file diff --git a/ESP32/win32.h b/ESP32/win32.h deleted file mode 120000 index fba5da8..0000000 --- a/ESP32/win32.h +++ /dev/null @@ -1 +0,0 @@ -../win32.h \ No newline at end of file diff --git a/log.cpp b/log.cpp index df44581..ec1cf68 100644 --- a/log.cpp +++ b/log.cpp @@ -15,7 +15,9 @@ #include "error.h" #include "log.h" #include "utils.h" +#if defined(_WIN32) #include "win32.h" +#endif static const char *logfile = strdup("/tmp/kek.log"); diff --git a/utils.cpp b/utils.cpp index b273316..f5c5d99 100644 --- a/utils.cpp +++ b/utils.cpp @@ -19,7 +19,9 @@ #include #include +#if defined(_WIN32) #include "win32.h" +#endif void setBit(uint16_t & v, const int bit, const bool vb) From 183330d6c32c9b78d00fa11a8b4b78c8e8d6a946 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 23:47:16 +0200 Subject: [PATCH 15/47] debug code removal --- mmu.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/mmu.cpp b/mmu.cpp index 17abfe5..dddacca 100644 --- a/mmu.cpp +++ b/mmu.cpp @@ -53,8 +53,6 @@ void mmu::setMMR0(uint16_t value) value &= 254; // bits 7...1 are protected } -// TODO if bit 15/14/13 are set (either of them), then do not modify bit 1...7 - MMR0 = value; } @@ -103,17 +101,6 @@ void mmu::addToMMR1(const int8_t delta, const uint8_t reg) assert((getMMR0() & 0160000) == 0); // MMR1 should not be locked -#if defined(ESP32) -// if (MMR1 > 255) -// esp_backtrace_print(32); -#else - if (MMR1 > 255) { - extern FILE *lfh; - fflush(lfh); - } - assert(MMR1 < 256); -#endif - MMR1 <<= 8; MMR1 |= (delta & 31) << 3; From 12a2e88680713926a40a6ab414b58968130754aa Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 23:55:44 +0200 Subject: [PATCH 16/47] rl02/rk05 is now instantiated without any disks --- ESP32/main.ino | 7 ++----- main.cpp | 4 ++-- rk05.cpp | 4 +--- rk05.h | 2 +- rl02.cpp | 10 ++++------ rl02.h | 2 +- 6 files changed, 11 insertions(+), 18 deletions(-) diff --git a/ESP32/main.ino b/ESP32/main.ino index 15c3220..1d581cd 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -73,9 +73,6 @@ std::atomic_bool *running { nullptr }; bool trace_output { false }; -std::vector rk05_files; -std::vector rl02_files; - void console_thread_wrapper_panel(void *const c) { console *const cnsl = reinterpret_cast(c); @@ -312,9 +309,9 @@ void setup() { running = cnsl->get_running_flag(); Serial.println(F("Connect RK05 and RL02 to BUS")); - b->add_rk05(new rk05(rk05_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + b->add_rk05(new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); - b->add_rl02(new rl02(rl02_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + b->add_rl02(new rl02(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); Serial.println(F("Init TTY")); tty_ = new tty(cnsl, b); diff --git a/main.cpp b/main.cpp index 7426408..c816370 100644 --- a/main.cpp +++ b/main.cpp @@ -534,9 +534,9 @@ int main(int argc, char *argv[]) if (enable_bootloader) set_boot_loader(b, bootloader); - b->add_rk05(new rk05(rk05_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + b->add_rk05(new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); - b->add_rl02(new rl02(rl02_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + b->add_rl02(new rl02(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); } else { FILE *fh = fopen(deserialize.c_str(), "r"); diff --git a/rk05.cpp b/rk05.cpp index 34d0c37..2398e54 100644 --- a/rk05.cpp +++ b/rk05.cpp @@ -23,13 +23,11 @@ static const char * const regnames[] = { "RK05_DATABUF " }; -rk05::rk05(const std::vector & files, bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomic_bool *const disk_write_acitivity) : +rk05::rk05(bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomic_bool *const disk_write_acitivity) : b(b), disk_read_acitivity(disk_read_acitivity), disk_write_acitivity(disk_write_acitivity) { - fhs = files; - reset(); } diff --git a/rk05.h b/rk05.h index 5189011..e9b1d3d 100644 --- a/rk05.h +++ b/rk05.h @@ -39,7 +39,7 @@ private: void update_bus_address(const uint16_t v); public: - rk05(const std::vector & files, bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomic_bool *const disk_write_acitivity); + rk05(bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomic_bool *const disk_write_acitivity); virtual ~rk05(); void reset() override; diff --git a/rl02.cpp b/rl02.cpp index 6974218..3b56cce 100644 --- a/rl02.cpp +++ b/rl02.cpp @@ -31,13 +31,11 @@ static const char * const commands[] = { "read data w/o header check" }; -rl02::rl02(const std::vector & files, bus *const b, std::atomic_bool *const disk_read_activity, std::atomic_bool *const disk_write_activity) : +rl02::rl02(bus *const b, std::atomic_bool *const disk_read_activity, std::atomic_bool *const disk_write_activity) : b(b), disk_read_activity (disk_read_activity ), disk_write_activity(disk_write_activity) { - fhs = files; - reset(); } @@ -86,11 +84,11 @@ rl02 *rl02::deserialize(const json_t *const j, bus *const b) { std::vector backends; + rl02 *r = new rl02(b, nullptr, nullptr); + json_t *j_backends = json_object_get(j, "backends"); for(size_t i=0; iaccess_disk_backends()->push_back(disk_backend::deserialize(json_array_get(j_backends, i))); for(int regnr=0; regnr<4; regnr++) r->registers[regnr] = json_integer_value(json_object_get(j, format("register-%d", regnr).c_str())); diff --git a/rl02.h b/rl02.h index be8a96d..966cd14 100644 --- a/rl02.h +++ b/rl02.h @@ -47,7 +47,7 @@ private: uint32_t calc_offset() const; public: - rl02(const std::vector & files, bus *const b, std::atomic_bool *const disk_read_activity, std::atomic_bool *const disk_write_activity); + rl02(bus *const b, std::atomic_bool *const disk_read_activity, std::atomic_bool *const disk_write_activity); virtual ~rl02(); void reset() override; From 2238243940092688b989e29de3cd39b091641872 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 01:21:39 +0200 Subject: [PATCH 17/47] coverity: auto and & --- console_ncurses.cpp | 4 ++-- debugger.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/console_ncurses.cpp b/console_ncurses.cpp index df2e736..2a9c76c 100644 --- a/console_ncurses.cpp +++ b/console_ncurses.cpp @@ -187,11 +187,11 @@ void console_ncurses::panel_update_thread() auto psw = data["psw"][0]; std::string instruction_values; - for(auto iv : data["instruction-values"]) + for(auto & iv : data["instruction-values"]) instruction_values += (instruction_values.empty() ? "" : ",") + iv; std::string work_values; - for(auto wv : data["work-values"]) + for(auto & wv : data["work-values"]) work_values += (work_values.empty() ? "" : ",") + wv; std::string instruction = data["instruction-text"].at(0); diff --git a/debugger.cpp b/debugger.cpp index c48241b..8eb6212 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -325,11 +325,11 @@ int disassemble(cpu *const c, console *const cnsl, const uint16_t pc, const bool auto psw = data["psw"][0]; std::string instruction_values; - for(auto iv : data["instruction-values"]) + for(auto & iv : data["instruction-values"]) instruction_values += (instruction_values.empty() ? "" : ",") + iv; std::string work_values; - for(auto wv : data["work-values"]) + for(auto & wv : data["work-values"]) work_values += (work_values.empty() ? "" : ",") + wv; std::string instruction = data["instruction-text"].at(0); @@ -391,7 +391,7 @@ std::map split(const std::vector & kv_arr { std::map out; - for(auto pair : kv_array) { + for(auto & pair : kv_array) { auto kv = split(pair, splitter); if (kv.size() == 1) @@ -663,7 +663,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto cnsl->put_string_lf("Breakpoints:"); - for(auto a : bps) + for(auto & a : bps) cnsl->put_string_lf(format("%d: %s", a.first, a.second->emit().c_str())); if (bps.empty()) From f64f2b78abd6b0b0dd556bd3535f8701d6a9898e Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 01:22:38 +0200 Subject: [PATCH 18/47] coverity: in theory, socket() can return -1 --- log.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/log.cpp b/log.cpp index d250081..62ae93f 100644 --- a/log.cpp +++ b/log.cpp @@ -83,8 +83,10 @@ void send_syslog(const int ll, const std::string & what) std::string msg = format("<%d>%s", 16 * 8 + ll, what.c_str()); int s = socket(AF_INET, SOCK_DGRAM, 0); - (void)sendto(s, msg.c_str(), msg.size(), 0, reinterpret_cast(&syslog_ip_addr), sizeof syslog_ip_addr); - close(s); + if (s != -1) { + (void)sendto(s, msg.c_str(), msg.size(), 0, reinterpret_cast(&syslog_ip_addr), sizeof syslog_ip_addr); + close(s); + } } void closelog() From 5fdda243657f65c9351b3137979be2777a5f6a19 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 01:23:47 +0200 Subject: [PATCH 19/47] coverity: "values_in is passed-by-value as parameter to split when it could be moved instead" --- breakpoint_memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breakpoint_memory.cpp b/breakpoint_memory.cpp index e56ce0a..b37e3f4 100644 --- a/breakpoint_memory.cpp +++ b/breakpoint_memory.cpp @@ -41,7 +41,7 @@ std::pair > breakpoint_memory::p return { nullptr, "memory: key or value missing" }; auto values_in = parts.at(1); - auto v_parts = split(values_in, ","); + auto v_parts = split(std::move(values_in), ","); std::set values; for(auto & v: v_parts) values.insert(std::stoi(v, nullptr, 8)); From 09f3117fd165af6b9f9c88bd5d404ebae57b89af Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 01:27:42 +0200 Subject: [PATCH 20/47] coverity: fgetc could also fail for other reasons than feof --- loaders.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/loaders.cpp b/loaders.cpp index 8752141..5a809a2 100644 --- a/loaders.cpp +++ b/loaders.cpp @@ -207,7 +207,11 @@ std::optional load_tape(bus *const b, const std::string & file) break; } - uint8_t c = fgetc(fh); + int c = fgetc(fh); + if (c == -1) { + DOLOG(warning, true, "read failure"); + break; + } #endif csum += c; From 0fa8453b8edbfaf9416fad7d262c9764caab11be Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 01:29:32 +0200 Subject: [PATCH 21/47] coverity: ignoring the rc of putGAM is intentionally here --- cpu.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpu.cpp b/cpu.cpp index 3a024b6..a77d173 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -715,7 +715,7 @@ bool cpu::double_operand_instructions(const uint16_t instr) setPSW_z(result == 0); } - putGAM(g_dst, result); + (void)putGAM(g_dst, result); return true; } From 6d7f3d9512c3feda897b4606db4725dd737dd2df Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 01:51:26 +0200 Subject: [PATCH 22/47] Call to virtual method ... during construction bypasses virtual dispatch (scan-build / clang analyzer) --- ESP32/main.ino | 12 +++++++++--- bus.cpp | 1 + console.cpp | 4 ++++ console.h | 2 ++ console_ncurses.cpp | 11 +++++++---- console_ncurses.h | 2 ++ disk_device.h | 2 ++ main.cpp | 9 +++++++-- mmu.cpp | 7 ++++++- mmu.h | 2 ++ rk05.cpp | 6 +++++- rk05.h | 1 + rl02.cpp | 7 ++++++- rl02.h | 1 + 14 files changed, 55 insertions(+), 12 deletions(-) diff --git a/ESP32/main.ino b/ESP32/main.ino index 1d581cd..517bd84 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -305,13 +305,19 @@ void setup() { cnsl = new console_esp32(&stop_event, serial_ports, 80, 25); #endif cnsl->set_bus(b); + cnsl->begin(); running = cnsl->get_running_flag(); - Serial.println(F("Connect RK05 and RL02 to BUS")); - b->add_rk05(new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + Serial.println(F("Connect RK05 and RL02 devices to BUS")); + auto rk05_dev = new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()); + rk05_dev->begin(); + b->add_rk05(rk05_dev); + + auto rl02_dev = new rl02(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()); + rl02_dev->begin(); + b->add_rl02(rl02_dev); - b->add_rl02(new rl02(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); Serial.println(F("Init TTY")); tty_ = new tty(cnsl, b); diff --git a/bus.cpp b/bus.cpp index 232d82d..1ab4cc6 100644 --- a/bus.cpp +++ b/bus.cpp @@ -24,6 +24,7 @@ bus::bus() { mmu_ = new mmu(); + mmu_->begin(); kw11_l_ = new kw11_l(this); diff --git a/console.cpp b/console.cpp index f656aed..efb8001 100644 --- a/console.cpp +++ b/console.cpp @@ -46,6 +46,10 @@ console::~console() delete [] screen_buffer; } +void console::begin() +{ +} + void console::start_thread() { assert(b); diff --git a/console.h b/console.h index 6101602..3b8d7bd 100644 --- a/console.h +++ b/console.h @@ -61,6 +61,8 @@ public: console(std::atomic_uint32_t *const stop_event, const int t_width = 80, const int t_height = 25); virtual ~console(); + virtual void begin(); + void set_bus(bus *const b) { this->b = b; } void start_thread(); diff --git a/console_ncurses.cpp b/console_ncurses.cpp index c8803db..9988d46 100644 --- a/console_ncurses.cpp +++ b/console_ncurses.cpp @@ -16,10 +16,6 @@ console_ncurses::console_ncurses(std::atomic_uint32_t *const stop_event): console(stop_event) { init_ncurses(true); - - resize_terminal(); - - th_panel = new std::thread(&console_ncurses::panel_update_thread, this); } console_ncurses::~console_ncurses() @@ -51,6 +47,13 @@ console_ncurses::~console_ncurses() endwin(); } +void console_ncurses::begin() +{ + resize_terminal(); + + th_panel = new std::thread(&console_ncurses::panel_update_thread, this); +} + int console_ncurses::wait_for_char_ll(const short timeout) { struct pollfd fds[] = { { STDIN_FILENO, POLLIN, 0 } }; diff --git a/console_ncurses.h b/console_ncurses.h index 99ca686..ac34082 100644 --- a/console_ncurses.h +++ b/console_ncurses.h @@ -32,6 +32,8 @@ public: console_ncurses(std::atomic_uint32_t *const stop_event); virtual ~console_ncurses(); + void begin() override; + void put_string_lf(const std::string & what) override; void resize_terminal() override; diff --git a/disk_device.h b/disk_device.h index 266fe84..4aa4db3 100644 --- a/disk_device.h +++ b/disk_device.h @@ -18,5 +18,7 @@ public: virtual ~disk_device() { } + virtual void begin() = 0; + std::vector * access_disk_backends() { return &fhs; } }; diff --git a/main.cpp b/main.cpp index c816370..537f56e 100644 --- a/main.cpp +++ b/main.cpp @@ -534,9 +534,13 @@ int main(int argc, char *argv[]) if (enable_bootloader) set_boot_loader(b, bootloader); - b->add_rk05(new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + auto rk05_dev = new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()); + rk05_dev->begin(); + b->add_rk05(rk05_dev); - b->add_rl02(new rl02(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + auto rl02_dev = new rl02(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()); + rl02_dev->begin(); + b->add_rl02(rl02_dev); } else { FILE *fh = fopen(deserialize.c_str(), "r"); @@ -566,6 +570,7 @@ int main(int argc, char *argv[]) } cnsl->set_bus(b); + cnsl->begin(); running = cnsl->get_running_flag(); diff --git a/mmu.cpp b/mmu.cpp index dddacca..b0cf558 100644 --- a/mmu.cpp +++ b/mmu.cpp @@ -9,13 +9,17 @@ mmu::mmu() { - reset(); } mmu::~mmu() { } +void mmu::begin() +{ + reset(); +} + void mmu::reset() { memset(pages, 0x00, sizeof pages); @@ -265,6 +269,7 @@ void mmu::set_par_pdr(const json_t *const j_in, const int run_mode, const bool i mmu *mmu::deserialize(const json_t *const j) { mmu *m = new mmu(); + m->begin(); for(int run_mode=0; run_mode<4; run_mode++) { if (run_mode == 2) diff --git a/mmu.h b/mmu.h index 4b2b4ad..ee8ba4f 100644 --- a/mmu.h +++ b/mmu.h @@ -50,6 +50,8 @@ public: mmu(); virtual ~mmu(); + void begin(); + #if IS_POSIX json_t *serialize() const; static mmu *deserialize(const json_t *const j); diff --git a/rk05.cpp b/rk05.cpp index 2398e54..a824bcb 100644 --- a/rk05.cpp +++ b/rk05.cpp @@ -28,7 +28,6 @@ rk05::rk05(bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomi disk_read_acitivity(disk_read_acitivity), disk_write_acitivity(disk_write_acitivity) { - reset(); } rk05::~rk05() @@ -37,6 +36,11 @@ rk05::~rk05() delete fh; } +void rk05::begin() +{ + reset(); +} + void rk05::reset() { memset(registers, 0x00, sizeof registers); diff --git a/rk05.h b/rk05.h index e9b1d3d..f422347 100644 --- a/rk05.h +++ b/rk05.h @@ -42,6 +42,7 @@ public: rk05(bus *const b, std::atomic_bool *const disk_read_acitivity, std::atomic_bool *const disk_write_acitivity); virtual ~rk05(); + void begin() override; void reset() override; uint8_t readByte(const uint16_t addr) override; diff --git a/rl02.cpp b/rl02.cpp index 3b56cce..4411e16 100644 --- a/rl02.cpp +++ b/rl02.cpp @@ -36,7 +36,6 @@ rl02::rl02(bus *const b, std::atomic_bool *const disk_read_activity, std::atomic disk_read_activity (disk_read_activity ), disk_write_activity(disk_write_activity) { - reset(); } rl02::~rl02() @@ -45,6 +44,11 @@ rl02::~rl02() delete fh; } +void rl02::begin() +{ + reset(); +} + void rl02::reset() { memset(registers, 0x00, sizeof registers ); @@ -85,6 +89,7 @@ rl02 *rl02::deserialize(const json_t *const j, bus *const b) std::vector backends; rl02 *r = new rl02(b, nullptr, nullptr); + r->begin(); json_t *j_backends = json_object_get(j, "backends"); for(size_t i=0; i Date: Sun, 28 Apr 2024 01:54:33 +0200 Subject: [PATCH 23/47] Call to virtual method ... during construction bypasses virtual dispatch (scan-build / clang analyzer) --- tm-11.cpp | 12 ++++++++---- tm-11.h | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tm-11.cpp b/tm-11.cpp index 057376d..2420148 100644 --- a/tm-11.cpp +++ b/tm-11.cpp @@ -10,11 +10,8 @@ #include "memory.h" #include "utils.h" -tm_11::tm_11(const std::string & file, memory *const m) : m(m) +tm_11::tm_11(const std::string & file, memory *const m): file(file), m(m) { - fh = fopen(file.c_str(), "rb"); - - reset(); } tm_11::~tm_11() @@ -22,6 +19,13 @@ tm_11::~tm_11() fclose(fh); } +void tm_11::begin() +{ + fh = fopen(file.c_str(), "rb"); + + reset(); +} + void tm_11::reset() { memset(registers, 0x00, sizeof registers ); diff --git a/tm-11.h b/tm-11.h index 31801c8..45163c7 100644 --- a/tm-11.h +++ b/tm-11.h @@ -24,6 +24,7 @@ class memory; class tm_11 : public device { private: + std::string file; memory *const m { nullptr }; uint16_t registers[6] { 0 }; uint8_t xfer_buffer[65536]; @@ -34,6 +35,8 @@ public: tm_11(const std::string & file, memory *const m); virtual ~tm_11(); + void begin(); + void reset() override; uint8_t readByte(const uint16_t addr) override; From 64e0002051d55a867e8dab03f4b683927757af26 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 02:13:57 +0200 Subject: [PATCH 24/47] analyzer --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70291a2..219ca54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,8 @@ add_compile_options(-Wall -pedantic -Wextra) #add_compile_options(-fsanitize=undefined) #add_link_options(-fsanitize=undefined) +#add_compile_options(-fanalyzer) + set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) From e87f5bf252589c8239ba77d88f47038ca71ec623 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 02:29:01 +0200 Subject: [PATCH 25/47] missing requirement --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5172983..f9019e4 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ To build for e.g. linux: Required: * libncursesw5-dev + * libjansson-dev To build for e.g. windows: From ea57b04114869060e82876554c32e05a206f3d05 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 02:37:12 +0200 Subject: [PATCH 26/47] coverity: pointer to out-of-scope data --- loaders.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/loaders.cpp b/loaders.cpp index 849048b..c2470c1 100644 --- a/loaders.cpp +++ b/loaders.cpp @@ -40,7 +40,7 @@ void set_boot_loader(bus *const b, const bootloader_t which) if (which == BL_RK05) { start = offset = 01000; - constexpr const uint16_t rk05_code[] = { + static const uint16_t rk05_code[] = { 0012700, 0177406, 0012710, @@ -92,7 +92,7 @@ void set_boot_loader(bus *const b, const bootloader_t which) start = offset = 01000; /* from https://www.pdp-11.nl/peripherals/disk/rl-info.html - constexpr const uint16_t rl02_code[] = { + static const uint16_t rl02_code[] = { 0012701, 0174400, 0012761, @@ -120,7 +120,7 @@ void set_boot_loader(bus *const b, const bootloader_t which) */ // from http://gunkies.org/wiki/RL11_disk_controller - constexpr const uint16_t rl02_code[] = { + static const uint16_t rl02_code[] = { 0012700, 0174400, 0012760, From 5b505562732b87e97a79099eb96a49cb0e745aa3 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 09:24:16 +0200 Subject: [PATCH 27/47] RK05 serialization --- bus.cpp | 13 +++++++++++-- rk05.cpp | 35 +++++++++++++++++++++++++++++++++++ rk05.h | 6 ++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/bus.cpp b/bus.cpp index 1ab4cc6..b5e1af8 100644 --- a/bus.cpp +++ b/bus.cpp @@ -66,7 +66,10 @@ json_t *bus::serialize() const if (rl02_) json_object_set(j_out, "rl02", rl02_->serialize()); - // TODO: rk05, tm11 + if (rk05_) + json_object_set(j_out, "rk05", rk05_->serialize()); + + // TODO: tm11 return j_out; } @@ -113,7 +116,13 @@ bus *bus::deserialize(const json_t *const j, console *const cnsl, std::atomic_ui b->add_rl02(rl02_); } - // TODO: rk05, tm11 + temp = json_object_get(j, "rk05"); + if (temp) { + rk05 *rk05_ = rk05::deserialize(temp, b); + b->add_rk05(rk05_); + } + + // TODO: tm11 return b; } diff --git a/rk05.cpp b/rk05.cpp index a824bcb..e6cb3fd 100644 --- a/rk05.cpp +++ b/rk05.cpp @@ -250,3 +250,38 @@ void rk05::writeWord(const uint16_t addr, const uint16_t v) } } } + +#if IS_POSIX +json_t *rk05::serialize() const +{ + json_t *j = json_object(); + + json_t *j_backends = json_array(); + for(auto & dbe: fhs) + json_array_append(j_backends, dbe->serialize()); + + json_object_set(j, "backends", j_backends); + + for(int regnr=0; regnr<7; regnr++) + json_object_set(j, format("register-%d", regnr).c_str(), json_integer(registers[regnr])); + + return j; +} + +rk05 *rk05::deserialize(const json_t *const j, bus *const b) +{ + std::vector backends; + + rk05 *r = new rk05(b, nullptr, nullptr); + r->begin(); + + json_t *j_backends = json_object_get(j, "backends"); + for(size_t i=0; iaccess_disk_backends()->push_back(disk_backend::deserialize(json_array_get(j_backends, i))); + + for(int regnr=0; regnr<7; regnr++) + r->registers[regnr] = json_integer_value(json_object_get(j, format("register-%d", regnr).c_str())); + + return r; +} +#endif diff --git a/rk05.h b/rk05.h index f422347..5f4f4c4 100644 --- a/rk05.h +++ b/rk05.h @@ -11,6 +11,7 @@ #include "disk_device.h" #include "disk_backend.h" +#include "gen.h" #define RK05_DS 0177400 // drive status @@ -45,6 +46,11 @@ public: void begin() override; void reset() override; +#if IS_POSIX + json_t *serialize() const; + static rk05 *deserialize(const json_t *const j, bus *const b); +#endif + uint8_t readByte(const uint16_t addr) override; uint16_t readWord(const uint16_t addr) override; From 96c7cf41bd5888b2cdb7ffd4686073c32a622f34 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 09:44:25 +0200 Subject: [PATCH 28/47] console::debugger is no longer required --- console.cpp | 27 --------------------------- console.h | 2 -- debugger.cpp | 14 +++++++------- 3 files changed, 7 insertions(+), 36 deletions(-) diff --git a/console.cpp b/console.cpp index efb8001..884b9a5 100644 --- a/console.cpp +++ b/console.cpp @@ -270,33 +270,6 @@ std::string console::read_line(const std::string & prompt) return edit_lines_hist.at(line_nr); } -void console::debug(const std::string fmt, ...) -{ -#if defined(BUILD_FOR_RP2040) - char buffer[128]; - va_list ap; - - va_start(ap, fmt); - vsnprintf(buffer, sizeof buffer, fmt.c_str(), ap); - va_end(ap); - - put_string_lf(buffer); -#else - char *buffer = nullptr; - - va_list ap; - va_start(ap, fmt); - - vasprintf(&buffer, fmt.c_str(), ap); - - va_end(ap); - - put_string_lf(buffer); - - free(buffer); -#endif -} - void console::put_char(const char c) { put_char_ll(c); diff --git a/console.h b/console.h index 3b8d7bd..88e1b3c 100644 --- a/console.h +++ b/console.h @@ -79,8 +79,6 @@ public: void put_string(const std::string & what); virtual void put_string_lf(const std::string & what) = 0; - void debug(const std::string fmt, ...); - virtual void resize_terminal() = 0; virtual void refresh_virtual_terminal() = 0; diff --git a/debugger.cpp b/debugger.cpp index 2087c19..cf59b1c 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -82,10 +82,10 @@ std::optional select_disk_file(console *const c) #if IS_POSIX c->put_string_lf("Files in current directory: "); #else - c->debug("MISO: %d", int(MISO)); - c->debug("MOSI: %d", int(MOSI)); - c->debug("SCK : %d", int(SCK )); - c->debug("SS : %d", int(SS )); + c->put_string_lf(format("MISO: %d", int(MISO))); + c->put_string_lf(format("MOSI: %d", int(MOSI))); + c->put_string_lf(format("SCK : %d", int(SCK ))); + c->put_string_lf(format("SS : %d", int(SS ))); c->put_string_lf("Files on SD-card:"); @@ -96,9 +96,9 @@ std::optional select_disk_file(console *const c) if (!SD.begin(SS, SD_SCK_MHZ(15))) { auto err = SD.sdErrorCode(); if (err) - c->debug("SDerror: 0x%x, data: 0x%x", err, SD.sdErrorData()); + c->put_string_lf(format("SDerror: 0x%x, data: 0x%x", err, SD.sdErrorData())); else - c->debug("Failed to initialize SD card"); + c->put_string_lf("Failed to initialize SD card"); return { }; } @@ -355,7 +355,7 @@ int disassemble(cpu *const c, console *const cnsl, const uint16_t pc, const bool ); if (cnsl) - cnsl->debug(result); + cnsl->put_string_lf(result); else DOLOG(debug, false, "%s", result.c_str()); From d8635bb213e23ff5403e860392bd9b503a879c31 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 09:53:05 +0200 Subject: [PATCH 29/47] use stack-buffer for timestamp (logging) --- log.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/log.cpp b/log.cpp index d3374db..43e3954 100644 --- a/log.cpp +++ b/log.cpp @@ -139,11 +139,11 @@ void dolog(const log_level_t ll, const char *fmt, ...) if (!localtime_r(&t_now, &tm)) error_exit(true, "localtime_r failed"); #endif - char *ts_str = nullptr; + char ts_str[64] { }; const char *const ll_names[] = { "emerg ", "alert ", "crit ", "error ", "warning", "notice ", "info ", "debug ", "none " }; - asprintf(&ts_str, "%04d-%02d-%02d %02d:%02d:%02d.%06d %s|%s] ", + snprintf(ts_str, sizeof ts_str, "%04d-%02d-%02d %02d:%02d:%02d.%06d %s|%s] ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, int(now % 1000000), ll_names[ll], get_thread_name().c_str()); @@ -156,8 +156,6 @@ void dolog(const log_level_t ll, const char *fmt, ...) if (ll <= log_level_screen) printf("%s%s\r\n", ts_str, str); - - free(ts_str); } else { if (ll <= log_level_file && is_file == false) From 209f8f1e8888470c9654242144ce26565e1ab4bf Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 10:09:40 +0200 Subject: [PATCH 30/47] command line: did not attach disk-backends to devices --- main.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 537f56e..ddf729b 100644 --- a/main.cpp +++ b/main.cpp @@ -525,15 +525,6 @@ int main(int argc, char *argv[]) cpu *c = new cpu(b, &event); b->add_cpu(c); - if (rk05_files.empty() == false) - bootloader = BL_RK05; - - if (rl02_files.empty() == false) - bootloader = BL_RL02; - - if (enable_bootloader) - set_boot_loader(b, bootloader); - auto rk05_dev = new rk05(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()); rk05_dev->begin(); b->add_rk05(rk05_dev); @@ -541,6 +532,23 @@ int main(int argc, char *argv[]) auto rl02_dev = new rl02(b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag()); rl02_dev->begin(); b->add_rl02(rl02_dev); + + if (rk05_files.empty() == false) { + bootloader = BL_RK05; + + for(auto & file: rk05_files) + rk05_dev->access_disk_backends()->push_back(file); + } + + if (rl02_files.empty() == false) { + bootloader = BL_RL02; + + for(auto & file: rl02_files) + rl02_dev->access_disk_backends()->push_back(file); + } + + if (enable_bootloader) + set_boot_loader(b, bootloader); } else { FILE *fh = fopen(deserialize.c_str(), "r"); From dbc8aa220d96f169d0e65541a897d3ed00a9d3f5 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 10:13:55 +0200 Subject: [PATCH 31/47] less mallocs for logging --- log.cpp | 32 +++++++++++++++++++------------- utils.cpp | 4 ++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/log.cpp b/log.cpp index 43e3954..7663e22 100644 --- a/log.cpp +++ b/log.cpp @@ -29,6 +29,8 @@ FILE *lfh = nullptr; static int lf_uid = -1; static int lf_gid = -1; static bool l_timestamp = true; +static int log_buffer_size = 128; +static char *log_buffer = reinterpret_cast(malloc(log_buffer_size)); #if defined(ESP32) int gettid() @@ -121,12 +123,18 @@ void dolog(const log_level_t ll, const char *fmt, ...) #endif } - char *str = nullptr; + for(;;) { + va_list ap; + va_start(ap, fmt); + int needed_length = vsnprintf(log_buffer, log_buffer_size, fmt, ap); + va_end(ap); - va_list ap; - va_start(ap, fmt); - (void)vasprintf(&str, fmt, ap); - va_end(ap); + if (needed_length < log_buffer_size) + break; + + log_buffer_size *= 2; + log_buffer = reinterpret_cast(realloc(log_buffer, log_buffer_size)); + } if (l_timestamp) { uint64_t now = get_us(); @@ -148,28 +156,26 @@ void dolog(const log_level_t ll, const char *fmt, ...) ll_names[ll], get_thread_name().c_str()); if (ll <= log_level_file && is_file == false) - send_syslog(ll, str); + send_syslog(ll, log_buffer); #if !defined(ESP32) if (ll <= log_level_file && lfh != nullptr) - fprintf(lfh, "%s%s\n", ts_str, str); + fprintf(lfh, "%s%s\n", ts_str, log_buffer); #endif if (ll <= log_level_screen) - printf("%s%s\r\n", ts_str, str); + printf("%s%s\r\n", ts_str, log_buffer); } else { if (ll <= log_level_file && is_file == false) - send_syslog(ll, str); + send_syslog(ll, log_buffer); #if !defined(ESP32) if (ll <= log_level_file && lfh != nullptr) - fprintf(lfh, "%s\n", str); + fprintf(lfh, "%s\n", log_buffer); #endif if (ll <= log_level_screen) - printf("%s\r\n", str); + printf("%s\r\n", log_buffer); } - - free(str); #endif } diff --git a/utils.cpp b/utils.cpp index f5c5d99..1e5f294 100644 --- a/utils.cpp +++ b/utils.cpp @@ -36,8 +36,8 @@ void setBit(uint16_t & v, const int bit, const bool vb) std::string format(const char *const fmt, ...) { -#if defined(BUILD_FOR_RP2040) - char buffer[128]; +#if defined(BUILD_FOR_RP2040) || defined(ESP32) + char buffer[256]; va_list ap; va_start(ap, fmt); From 324bf68dba9fbe07d4f0ccd9c82da1a18cb50798 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 10:20:38 +0200 Subject: [PATCH 32/47] enlarged panel stack a bit as it triggered a stack overflow --- ESP32/main.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESP32/main.ino b/ESP32/main.ino index 517bd84..66fba41 100644 --- a/ESP32/main.ino +++ b/ESP32/main.ino @@ -326,7 +326,7 @@ void setup() { #if !defined(BUILD_FOR_RP2040) // FIXME: led ring Serial.println(F("Starting panel")); - xTaskCreate(&console_thread_wrapper_panel, "panel", 2048, cnsl, 1, nullptr); + xTaskCreate(&console_thread_wrapper_panel, "panel", 3072, cnsl, 1, nullptr); #endif #if !defined(BUILD_FOR_RP2040) From 0a4a5cd9d7172b89c45d96e2bf22a8b13b69c17d Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 19:21:31 +0200 Subject: [PATCH 33/47] missing dependency --- .github/workflows/codeql.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index e5baebf..30932b4 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -30,7 +30,7 @@ jobs: if: ${{ matrix.language == 'cpp' }} run: | sudo apt-get update - sudo apt-get install --yes libncurses-dev + sudo apt-get install --yes libncurses-dev libjansson-dev - name: Configure (cpp) if: ${{ matrix.language == 'cpp' }} From bf9d55a44fa56343376f82128ef9c9b523431945 Mon Sep 17 00:00:00 2001 From: Folkert van Heusden Date: Sun, 28 Apr 2024 19:25:50 +0200 Subject: [PATCH 34/47] Create msvc.yml --- .github/workflows/msvc.yml | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 .github/workflows/msvc.yml diff --git a/.github/workflows/msvc.yml b/.github/workflows/msvc.yml new file mode 100644 index 0000000..1952872 --- /dev/null +++ b/.github/workflows/msvc.yml @@ -0,0 +1,66 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# +# Find more information at: +# https://github.com/microsoft/msvc-code-analysis-action + +name: Microsoft C++ Code Analysis + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + schedule: + - cron: '23 16 * * 1' + +env: + # Path to the CMake build directory. + build: '${{ github.workspace }}/build' + +permissions: + contents: read + +jobs: + analyze: + permissions: + contents: read # for actions/checkout to fetch code + security-events: write # for github/codeql-action/upload-sarif to upload SARIF results + actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status + name: Analyze + runs-on: windows-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Configure CMake + run: cmake -B ${{ env.build }} + + # Build is not required unless generated source files are used + # - name: Build CMake + # run: cmake --build ${{ env.build }} + + - name: Initialize MSVC Code Analysis + uses: microsoft/msvc-code-analysis-action@04825f6d9e00f87422d6bf04e1a38b1f3ed60d99 + # Provide a unique ID to access the sarif output path + id: run-analysis + with: + cmakeBuildDirectory: ${{ env.build }} + # Ruleset file that will determine what checks will be run + ruleset: NativeRecommendedRules.ruleset + + # Upload SARIF file to GitHub Code Scanning Alerts + - name: Upload SARIF to GitHub + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: ${{ steps.run-analysis.outputs.sarif }} + + # Upload SARIF file as an Artifact to download and view + # - name: Upload SARIF as an Artifact + # uses: actions/upload-artifact@v3 + # with: + # name: sarif-file + # path: ${{ steps.run-analysis.outputs.sarif }} From 816d77226cd7ca125c043eed8d6bd72821f7011c Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 21:31:51 +0200 Subject: [PATCH 35/47] useless workflowfile --- .github/workflows/msvc.yml | 66 -------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 .github/workflows/msvc.yml diff --git a/.github/workflows/msvc.yml b/.github/workflows/msvc.yml deleted file mode 100644 index 1952872..0000000 --- a/.github/workflows/msvc.yml +++ /dev/null @@ -1,66 +0,0 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. -# -# Find more information at: -# https://github.com/microsoft/msvc-code-analysis-action - -name: Microsoft C++ Code Analysis - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - schedule: - - cron: '23 16 * * 1' - -env: - # Path to the CMake build directory. - build: '${{ github.workspace }}/build' - -permissions: - contents: read - -jobs: - analyze: - permissions: - contents: read # for actions/checkout to fetch code - security-events: write # for github/codeql-action/upload-sarif to upload SARIF results - actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status - name: Analyze - runs-on: windows-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Configure CMake - run: cmake -B ${{ env.build }} - - # Build is not required unless generated source files are used - # - name: Build CMake - # run: cmake --build ${{ env.build }} - - - name: Initialize MSVC Code Analysis - uses: microsoft/msvc-code-analysis-action@04825f6d9e00f87422d6bf04e1a38b1f3ed60d99 - # Provide a unique ID to access the sarif output path - id: run-analysis - with: - cmakeBuildDirectory: ${{ env.build }} - # Ruleset file that will determine what checks will be run - ruleset: NativeRecommendedRules.ruleset - - # Upload SARIF file to GitHub Code Scanning Alerts - - name: Upload SARIF to GitHub - uses: github/codeql-action/upload-sarif@v2 - with: - sarif_file: ${{ steps.run-analysis.outputs.sarif }} - - # Upload SARIF file as an Artifact to download and view - # - name: Upload SARIF as an Artifact - # uses: actions/upload-artifact@v3 - # with: - # name: sarif-file - # path: ${{ steps.run-analysis.outputs.sarif }} From 11aefffe77d3bc97c01837521dbd0b1c9ddb51ba Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 21:36:54 +0200 Subject: [PATCH 36/47] field shadows other declaration --- cpu.cpp | 7 +++---- debugger.cpp | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cpu.cpp b/cpu.cpp index a77d173..8fdc6ac 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -584,12 +584,12 @@ bool cpu::double_operand_instructions(const uint16_t instr) const uint8_t dst_mode = (dst >> 3) & 7; const uint8_t dst_reg = dst & 7; - bool set_flags = true; - switch(operation) { case 0b001: { // MOV/MOVB Move Word/Byte gam_rc_t g_src = getGAM(src_mode, src_reg, word_mode, rm_cur); + bool set_flags = true; + if (word_mode == wm_byte && dst_mode == 0) setRegister(dst_reg, int8_t(g_src.value.value())); // int8_t: sign extension else { @@ -945,7 +945,6 @@ bool cpu::single_operand_instructions(const uint16_t instr) const uint8_t dst_mode = (dst >> 3) & 7; const uint8_t dst_reg = dst & 7; const word_mode_t word_mode = instr & 0x8000 ? wm_byte : wm_word; - bool set_flags = true; switch(opcode) { case 0b00000011: { // SWAB @@ -959,7 +958,7 @@ bool cpu::single_operand_instructions(const uint16_t instr) v = (v << 8) | (v >> 8); - set_flags = putGAM(g_dst, v); + bool set_flags = putGAM(g_dst, v); if (set_flags) { setPSW_flags_nzv(v, wm_byte); diff --git a/debugger.cpp b/debugger.cpp index cf59b1c..4455b48 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -236,20 +236,20 @@ std::optional select_disk_backend(console *const cnsl) void configure_disk(bus *const b, console *const cnsl) { // TODO tape - int ch = wait_for_key("1. RK05, 2. RL02, 9. abort", cnsl, { '1', '2', '3', '9' }); + int type_ch = wait_for_key("1. RK05, 2. RL02, 9. abort", cnsl, { '1', '2', '3', '9' }); bootloader_t bl = BL_NONE; disk_device *dd = nullptr; - if (ch == '1') { + if (type_ch == '1') { dd = b->getRK05(); bl = BL_RK05; } - else if (ch == '2') { + else if (type_ch == '2') { dd = b->getRL02(); bl = BL_RL02; } - else if (ch == '9') { + else if (type_ch == '9') { return; } From 73f2200bc373732676c26bf49501c65ff59eaef0 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 22:36:19 +0200 Subject: [PATCH 37/47] typo in format string --- breakpoint_memory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/breakpoint_memory.cpp b/breakpoint_memory.cpp index b37e3f4..4c59e6b 100644 --- a/breakpoint_memory.cpp +++ b/breakpoint_memory.cpp @@ -31,7 +31,7 @@ std::optional breakpoint_memory::is_triggered() const if (it == values.end()) return { }; - return format("MEM%c%c[%08a]=%06o", word_mode == wm_byte ? 'B' : 'W', is_virtual ? 'V' : 'P', addr, v); + return format("MEM%c%c[%08o]=%06o", word_mode == wm_byte ? 'B' : 'W', is_virtual ? 'V' : 'P', addr, v); } std::pair > breakpoint_memory::parse(bus *const b, const std::string & in) From 42c4a84dde3af481b4c37c083e72b71714f0cafc Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 22:36:31 +0200 Subject: [PATCH 38/47] off_t is long unsigned --- disk_backend.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/disk_backend.cpp b/disk_backend.cpp index a3e087b..aa7d0b7 100644 --- a/disk_backend.cpp +++ b/disk_backend.cpp @@ -69,7 +69,7 @@ json_t *disk_backend::serialize_overlay() const for(size_t i=0; i Date: Sun, 28 Apr 2024 22:38:07 +0200 Subject: [PATCH 39/47] renamed short variable name --- log.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/log.cpp b/log.cpp index 7663e22..9704acc 100644 --- a/log.cpp +++ b/log.cpp @@ -25,7 +25,7 @@ static sockaddr_in syslog_ip_addr = { }; static bool is_file = true; log_level_t log_level_file = warning; log_level_t log_level_screen = warning; -FILE *lfh = nullptr; +static FILE *log_fh = nullptr; static int lf_uid = -1; static int lf_gid = -1; static bool l_timestamp = true; @@ -41,8 +41,8 @@ int gettid() void setlogfile(const char *const lf, const log_level_t ll_file, const log_level_t ll_screen, const bool timestamp) { - if (lfh) - fclose(lfh); + if (log_fh) + fclose(log_fh); free((void *)logfile); @@ -98,26 +98,26 @@ void send_syslog(const int ll, const std::string & what) void closelog() { - if (lfh) { - fclose(lfh); + if (log_fh) { + fclose(log_fh); - lfh = nullptr; + log_fh = nullptr; } } void dolog(const log_level_t ll, const char *fmt, ...) { #if !defined(BUILD_FOR_RP2040) - if (!lfh && logfile != nullptr) { + if (!log_fh && logfile != nullptr) { #if !defined(ESP32) - lfh = fopen(logfile, "a+"); - if (!lfh) + log_fh = fopen(logfile, "a+"); + if (!log_fh) error_exit(true, "Cannot access log-file %s", logfile); #if !defined(_WIN32) - if (lf_uid != -1 && fchown(fileno(lfh), lf_uid, lf_gid) == -1) + if (lf_uid != -1 && fchown(fileno(log_fh), lf_uid, lf_gid) == -1) error_exit(true, "Cannot change logfile (%s) ownership", logfile); - if (fcntl(fileno(lfh), F_SETFD, FD_CLOEXEC) == -1) + if (fcntl(fileno(log_fh), F_SETFD, FD_CLOEXEC) == -1) error_exit(true, "fcntl(FD_CLOEXEC) failed"); #endif #endif @@ -158,8 +158,8 @@ void dolog(const log_level_t ll, const char *fmt, ...) if (ll <= log_level_file && is_file == false) send_syslog(ll, log_buffer); #if !defined(ESP32) - if (ll <= log_level_file && lfh != nullptr) - fprintf(lfh, "%s%s\n", ts_str, log_buffer); + if (ll <= log_level_file && log_fh != nullptr) + fprintf(log_fh, "%s%s\n", ts_str, log_buffer); #endif if (ll <= log_level_screen) @@ -169,8 +169,8 @@ void dolog(const log_level_t ll, const char *fmt, ...) if (ll <= log_level_file && is_file == false) send_syslog(ll, log_buffer); #if !defined(ESP32) - if (ll <= log_level_file && lfh != nullptr) - fprintf(lfh, "%s\n", log_buffer); + if (ll <= log_level_file && log_fh != nullptr) + fprintf(log_fh, "%s\n", log_buffer); #endif if (ll <= log_level_screen) From 2269ef5c01a6148bec2cc23e14b559e3a5b8c24e Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 22:40:14 +0200 Subject: [PATCH 40/47] shadowing variable --- main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index ddf729b..4d891b0 100644 --- a/main.cpp +++ b/main.cpp @@ -183,9 +183,9 @@ int run_cpu_validation(const std::string & filename) { json_t *a_sp = json_object_get(registers_after, "sp"); - size_t array_size = json_array_size(a_sp); - assert(array_size == 4); - for(size_t i=0; ilowlevel_register_sp_get(i); if (json_integer_value(temp) != sp) { From 7d917dffbcd648810945551ae96f8fc6fcd87722 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 22:40:40 +0200 Subject: [PATCH 41/47] shadowing variable --- main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 4d891b0..229bbd2 100644 --- a/main.cpp +++ b/main.cpp @@ -111,9 +111,9 @@ int run_cpu_validation(const std::string & filename) } { json_t *b_sp = json_object_get(registers_before, "sp"); - size_t array_size = json_array_size(b_sp); - assert(array_size == 4); - for(size_t i=0; ilowlevel_register_sp_set(i, json_integer_value(temp)); } From 0b266a7fdc96ceae8cc465ff492da0d8638987e8 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 22:42:21 +0200 Subject: [PATCH 42/47] shadowing variable --- rk05.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rk05.cpp b/rk05.cpp index e6cb3fd..f529803 100644 --- a/rk05.cpp +++ b/rk05.cpp @@ -188,10 +188,10 @@ void rk05::writeWord(const uint16_t addr, const uint16_t v) uint32_t temp_diskoffb = diskoffb; - uint32_t temp = reclen; - uint32_t p = memoff; - while(temp > 0) { - uint32_t cur = std::min(uint32_t(sizeof xfer_buffer), temp); + uint32_t temp_reclen = reclen; + uint32_t p = memoff; + while(temp_reclen > 0) { + uint32_t cur = std::min(uint32_t(sizeof xfer_buffer), temp_reclen); if (!fhs.at(device)->read(temp_diskoffb, cur, xfer_buffer, 512)) { DOLOG(ll_error, true, "RK05 read error %s from %u len %u", strerror(errno), temp_diskoffb, cur); @@ -207,7 +207,7 @@ void rk05::writeWord(const uint16_t addr, const uint16_t v) update_bus_address(2); } - temp -= cur; + temp_reclen -= cur; if (++sector >= 12) { sector = 0; From 0ad23cc05ee02e328f5480b09864a2c0e7fc95e1 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 22:42:51 +0200 Subject: [PATCH 43/47] shadowing variable --- debugger.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/debugger.cpp b/debugger.cpp index 4455b48..3fc9be2 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -286,11 +286,11 @@ void configure_disk(bus *const b, console *const cnsl) int slot = ch - 'A'; for(;;) { - int ch = wait_for_key("Select cartridge action: 1. load, 2. unload, 9. exit", cnsl, { '1', '2', '9' }); - if (ch == '9') + int ch_action = wait_for_key("Select cartridge action: 1. load, 2. unload, 9. exit", cnsl, { '1', '2', '9' }); + if (ch_action == '9') break; - if (ch == '1') { + if (ch_action == '1') { auto image_file = select_disk_backend(cnsl); if (image_file.has_value()) { @@ -300,7 +300,7 @@ void configure_disk(bus *const b, console *const cnsl) cnsl->put_string_lf("Cartridge loaded"); } } - else if (ch == '2') { + else if (ch_action == '2') { if (cartridge_slots->at(slot)) { delete cartridge_slots->at(slot); cartridge_slots->at(slot) = nullptr; From f80b2244efc975c1b9c60cf2315b436a616ba918 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 28 Apr 2024 22:44:32 +0200 Subject: [PATCH 44/47] unused imports --- ESP32/prepare.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ESP32/prepare.py b/ESP32/prepare.py index 6713332..51a7d67 100755 --- a/ESP32/prepare.py +++ b/ESP32/prepare.py @@ -1,8 +1,6 @@ #! /usr/bin/python3 -import json import subprocess -import sys def get_git_hash(): @@ -12,6 +10,7 @@ def get_git_hash(): ret = subprocess.run(["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE, text=True) return ret.stdout.strip() + add + fh = open('version.cpp', 'w') fh.write('const char *version_str = "' + get_git_hash() + '";\n') fh.close() From 7f0f4ba4c50b67264a2cabd279598eebcf194afe Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Mon, 29 Apr 2024 08:27:33 +0200 Subject: [PATCH 45/47] threading tweak --- log.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/log.cpp b/log.cpp index 9704acc..66e4664 100644 --- a/log.cpp +++ b/log.cpp @@ -29,8 +29,8 @@ static FILE *log_fh = nullptr; static int lf_uid = -1; static int lf_gid = -1; static bool l_timestamp = true; -static int log_buffer_size = 128; -static char *log_buffer = reinterpret_cast(malloc(log_buffer_size)); +static thread_local int log_buffer_size = 128; +static thread_local char *log_buffer = reinterpret_cast(malloc(log_buffer_size)); #if defined(ESP32) int gettid() From 4a41ee6b1fc740d65599764b5ec053cab43d3089 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Mon, 29 Apr 2024 08:54:17 +0200 Subject: [PATCH 46/47] get_register_set() instead of getBitPSW(11) --- cpu.cpp | 6 +++--- cpu.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cpu.cpp b/cpu.cpp index 8fdc6ac..29fcca1 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -137,7 +137,7 @@ void cpu::reset() uint16_t cpu::getRegister(const int nr, const rm_selection_t mode_selection) const { if (nr < 6) { - int set = getBitPSW(11); + int set = get_register_set(); return regs0_5[set][nr]; } @@ -157,7 +157,7 @@ uint16_t cpu::getRegister(const int nr, const rm_selection_t mode_selection) con void cpu::setRegister(const int nr, const uint16_t value, const rm_selection_t mode_selection) { if (nr < 6) { - int set = getBitPSW(11); + int set = get_register_set(); regs0_5[set][nr] = value; } @@ -204,7 +204,7 @@ bool cpu::put_result(const gam_rc_t & g, const uint16_t value) uint16_t cpu::addRegister(const int nr, const rm_selection_t mode_selection, const uint16_t value) { if (nr < 6) - return regs0_5[getBitPSW(11)][nr] += value; + return regs0_5[get_register_set()][nr] += value; if (nr == 6) { if (mode_selection == rm_prev) diff --git a/cpu.h b/cpu.h index 98d2430..fa02705 100644 --- a/cpu.h +++ b/cpu.h @@ -160,6 +160,7 @@ public: bool getBitPSW(const int bit) const; int getPSW_runmode() const { return psw >> 14; }; int getPSW_prev_runmode() const { return (psw >> 12) & 3; }; + bool get_register_set() const { return !!(psw & 04000); } void setPSW_c(const bool v); void setPSW_v(const bool v); From a2acf902c747114fb7ecf4e3e6bb9d37f28ba67d Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Mon, 29 Apr 2024 09:47:45 +0200 Subject: [PATCH 47/47] wording --- rl02.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rl02.cpp b/rl02.cpp index 4411e16..9e9565a 100644 --- a/rl02.cpp +++ b/rl02.cpp @@ -204,7 +204,7 @@ void rl02::writeWord(const uint16_t addr, uint16_t v) bool do_int = false; if (size_t(device) >= fhs.size()) { - DOLOG(info, false, "RL02: PDP11/70 is accessing a not-attached virtual disk %d", device); + DOLOG(info, false, "RL02: PDP11/70 is accessing virtual disk %d which is not attached", device); registers[(RL02_CSR - RL02_BASE) / 2] |= (1 << 10) | (1 << 15);