From 9e71b84b1ac0edbf39d6854c90c6a5910a5a8da4 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sat, 27 Apr 2024 21:57:09 +0200 Subject: [PATCH 1/9] 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 2/9] 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 3/9] 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 4/9] 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 5/9] 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 6/9] 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 7/9] 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 8/9] 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 9/9] 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)