diff --git a/ESP32/main.ino b/ESP32/main.ino index 50a7cd8..eb1a29e 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/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/bus.cpp b/bus.cpp index 8ac8d26..c953507 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 c276cbe..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" @@ -821,6 +822,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_all(true) == false) + cnsl->put_string_lf("HEAP corruption!"); + + continue; + } else if (cmd == "cfgnet") { configure_network(cnsl); @@ -859,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)); } @@ -1016,6 +1023,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 diff --git a/disk_backend.cpp b/disk_backend.cpp index 488ee94..a3e087b 100644 --- a/disk_backend.cpp +++ b/disk_backend.cpp @@ -4,6 +4,7 @@ #include #include "disk_backend.h" +#include "gen.h" #if IS_POSIX #include "disk_backend_file.h" #include "disk_backend_nbd.h" 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 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/memory.h b/memory.h index 7a43089..fdf3e3a 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; 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)