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/console_esp32.cpp b/ESP32/console_esp32.cpp index 3bd68be..787733f 100644 --- a/ESP32/console_esp32.cpp +++ b/ESP32/console_esp32.cpp @@ -1,4 +1,4 @@ -// (C) 2018-2023 by Folkert van Heusden +// (C) 2018-2024 by Folkert van Heusden // Released under MIT license #include @@ -13,7 +13,7 @@ #define NEOPIXELS_PIN 25 -console_esp32::console_esp32(std::atomic_uint32_t *const stop_event, bus *const b, std::vector & io_ports, const int t_width, const int t_height) : +console_esp32::console_esp32(std::atomic_uint32_t *const stop_event, std::vector & io_ports, const int t_width, const int t_height) : console(stop_event, t_width, t_height), io_ports(io_ports) { @@ -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/console_esp32.h b/ESP32/console_esp32.h index fd3aa30..b83aded 100644 --- a/ESP32/console_esp32.h +++ b/ESP32/console_esp32.h @@ -1,4 +1,4 @@ -// (C) 2018-2023 by Folkert van Heusden +// (C) 2018-2024 by Folkert van Heusden // Released under MIT license #include @@ -18,7 +18,7 @@ protected: void put_char_ll(const char c) override; public: - console_esp32(std::atomic_uint32_t *const stop_event, bus *const b, std::vector & io_ports, const int t_width, const int t_height); + console_esp32(std::atomic_uint32_t *const stop_event, std::vector & io_ports, const int t_width, const int t_height); virtual ~console_esp32(); void put_string_lf(const std::string & what) override; diff --git a/ESP32/console_shabadge.cpp b/ESP32/console_shabadge.cpp index 8720724..b8cdf77 100644 --- a/ESP32/console_shabadge.cpp +++ b/ESP32/console_shabadge.cpp @@ -15,8 +15,8 @@ #define COLORED 0 #define UNCOLORED 1 -console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, bus *const b, std::vector & io_ports) : - console_esp32(stop_event, b, io_ports, 296 / 8, 128 / 8) +console_shabadge::console_shabadge(std::atomic_uint32_t *const stop_event, std::vector & io_ports) : + console_esp32(stop_event, io_ports, 296 / 8, 128 / 8) { if (epd.Init() != 0) Serial.println("Init of DEPG0290B01 failed"); diff --git a/ESP32/console_shabadge.h b/ESP32/console_shabadge.h index 8a77cb2..bf09419 100644 --- a/ESP32/console_shabadge.h +++ b/ESP32/console_shabadge.h @@ -22,7 +22,7 @@ private: void put_char_ll(const char c) override; public: - console_shabadge(std::atomic_uint32_t *const stop_event, bus *const b, std::vector & io_ports); + console_shabadge(std::atomic_uint32_t *const stop_event, std::vector & io_ports); virtual ~console_shabadge(); void panel_update_thread() override; diff --git a/ESP32/disk_backend_esp32.cpp b/ESP32/disk_backend_esp32.cpp index fdc33c5..9f42509 100644 --- a/ESP32/disk_backend_esp32.cpp +++ b/ESP32/disk_backend_esp32.cpp @@ -29,7 +29,7 @@ void disk_backend_esp32::emit_error() DOLOG(ll_error, true, "SdFat error: %d/%d", sd.sdErrorCode(), sd.sdErrorData()); } -bool disk_backend_esp32::begin() +bool disk_backend_esp32::begin(const bool dummy) { if (!fh->open(filename.c_str(), O_RDWR)) { DOLOG(ll_error, true, "rk05: cannot open \"%s\"", filename.c_str()); @@ -41,7 +41,7 @@ bool disk_backend_esp32::begin() return true; } -bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const target) +bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) { DOLOG(debug, false, "disk_backend_esp32::read: read %zu bytes from offset %zu", n, offset); @@ -77,7 +77,7 @@ bool disk_backend_esp32::read(const off_t offset, const size_t n, uint8_t *const return true; } -bool disk_backend_esp32::write(const off_t offset, const size_t n, const uint8_t *const from) +bool disk_backend_esp32::write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) { DOLOG(debug, false, "disk_backend_esp32::write: write %zu bytes to offset %zu", n, offset); diff --git a/ESP32/disk_backend_esp32.h b/ESP32/disk_backend_esp32.h index c24ae51..b0f048a 100644 --- a/ESP32/disk_backend_esp32.h +++ b/ESP32/disk_backend_esp32.h @@ -23,9 +23,11 @@ public: disk_backend_esp32(const std::string & filename); virtual ~disk_backend_esp32(); - bool begin() override; + std::string get_identifier() const { return filename; } - bool read(const off_t offset, const size_t n, uint8_t *const target) override; + bool begin(const bool dummy) override; - bool write(const off_t offset, const size_t n, const uint8_t *const from) override; + bool read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) override; + + bool write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) override; }; diff --git a/ESP32/disk_device.h b/ESP32/disk_device.h new file mode 120000 index 0000000..dbcee94 --- /dev/null +++ b/ESP32/disk_device.h @@ -0,0 +1 @@ +../disk_device.h \ No newline at end of file diff --git a/ESP32/main.ino b/ESP32/main.ino index 075f153..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" @@ -38,7 +41,6 @@ #include "esp32.h" #endif #include "gen.h" -#include "kw11-l.h" #include "loaders.h" #include "memory.h" #include "tty.h" @@ -71,11 +73,16 @@ 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); cnsl->panel_update_thread(); + + vTaskSuspend(nullptr); } uint32_t load_serial_speed_configuration() @@ -194,12 +201,7 @@ void recall_configuration(console *const cnsl) cnsl->put_string_lf("Starting network..."); start_network(cnsl); - auto disk_configuration = load_disk_configuration(cnsl); - - if (disk_configuration.has_value()) { - cnsl->put_string_lf("Starting disk..."); - set_disk_configuration(b, cnsl, disk_configuration.value()); - } + // TODO } #endif @@ -211,13 +213,21 @@ 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); while(!Serial) delay(100); - Serial.println(F("This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden.")); + Serial.println(F("PDP11 emulator, by Folkert van Heusden")); Serial.print(F("GIT hash: ")); Serial.println(version_str); Serial.println(F("Build on: " __DATE__ " " __TIME__)); @@ -225,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()); @@ -264,6 +278,9 @@ void setup() { Serial.println(F("Init bus")); b = new bus(); + Serial.println(F("Allocate memory")); + b->set_memory_size(DEFAULT_N_PAGES); + Serial.println(F("Init CPU")); c = new cpu(b, &stop_event); @@ -286,14 +303,19 @@ void setup() { std::vector serial_ports { &Serial_RS232, &Serial }; #if defined(SHA2017) - cnsl = new console_shabadge(&stop_event, b, serial_ports); + cnsl = new console_shabadge(&stop_event, serial_ports); #elif defined(ESP32) || defined(BUILD_FOR_RP2040) - cnsl = new console_esp32(&stop_event, b, serial_ports, 80, 25); + cnsl = new console_esp32(&stop_event, serial_ports, 80, 25); #endif cnsl->set_bus(b); 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_rl02(new rl02(rl02_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + Serial.println(F("Init TTY")); tty_ = new tty(cnsl, b); Serial.println(F("Connect TTY to bus")); diff --git a/ESP32/platformio.ini b/ESP32/platformio.ini index 948ef3a..903dbdf 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 @@ -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/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), { } }; } diff --git a/bus.h b/bus.h index d0fdd6b..800e926 100644 --- a/bus.h +++ b/bus.h @@ -119,6 +119,8 @@ public: kw11_l *getKW11_L() { return kw11_l_; } tty *getTty() { return tty_; } mmu *getMMU() { return mmu_; } + rk05 *getRK05() { return rk05_; } + rl02 *getRL02() { return rl02_; } uint16_t read (const uint16_t a, const word_mode_t word_mode, const rm_selection_t mode_selection, const bool peek_only=false, const d_i_space_t s = i_space); uint16_t readByte(const uint16_t a) { return read(a, wm_byte, rm_cur); } 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 189d17f..ee35957 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -10,7 +10,6 @@ #include #else #include -#include #include #endif @@ -39,264 +38,46 @@ #include "rp2040.h" #endif -void setBootLoader(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); - -void recall_configuration(console *const c); #endif -#define NET_DISK_CFG_FILE "net-disk.json" - #if !defined(BUILD_FOR_RP2040) && !defined(linux) extern SdFs SD; #endif -#ifndef linux -#define MAX_CFG_SIZE 1024 -StaticJsonDocument json_doc; -#endif - -typedef enum { BE_NETWORK, BE_SD } disk_backend_t; - #if !defined(BUILD_FOR_RP2040) -std::optional, std::vector, std::string> > load_disk_configuration(console *const c) +std::optional select_nbd_server(console *const cnsl) { -#if IS_POSIX - json_error_t error; - json_t *json = json_load_file("." NET_DISK_CFG_FILE, JSON_REJECT_DUPLICATES, &error); - if (!json) { - c->put_string_lf(format("Cannot load ." NET_DISK_CFG_FILE ": %s", error.text)); + cnsl->flush_input(); - return { }; - } - - std::string nbd_host = json_string_value (json_object_get(json, "NBD-host")); - int nbd_port = json_integer_value(json_object_get(json, "NBD-port")); - - std::string disk_type_temp = json_string_value (json_object_get(json, "disk-type")); - - std::string tape_file = json_string_value (json_object_get(json, "tape-file")); - - json_decref(json); -#else - File dataFile = LittleFS.open("/" NET_DISK_CFG_FILE, "r"); - if (!dataFile) - return { }; - - size_t size = dataFile.size(); - - char buffer[MAX_CFG_SIZE]; - - if (size > sizeof buffer) { // this should not happen - dataFile.close(); - - return { }; - } - - dataFile.read(reinterpret_cast(buffer), size); - buffer[(sizeof buffer) - 1] = 0x00; - - dataFile.close(); - - auto error = deserializeJson(json_doc, buffer); - if (error) // this should not happen - return { }; - - String nbd_host = json_doc["NBD-host"]; - int nbd_port = json_doc["NBD-port"]; - - String disk_type_temp = json_doc["disk-type"]; - - String tape_file = json_doc["tape-file"]; -#endif - - disk_type_t disk_type = DT_RK05; - - if (disk_type_temp == "rl02") - disk_type = DT_RL02; - else if (disk_type_temp == "tape") - disk_type = DT_TAPE; - - disk_backend *d = new disk_backend_nbd(nbd_host.c_str(), nbd_port); - - if (d->begin() == false) { - c->put_string_lf("Cannot initialize NBD client from configuration file"); - delete d; - return { }; - } - - c->put_string_lf(format("Connection to NBD server at %s:%d success", nbd_host.c_str(), nbd_port)); - - if (disk_type == DT_RK05) - return { { { d }, { }, "" } }; - - if (disk_type == DT_RL02) - return { { { }, { d }, "" } }; - - if (disk_type == DT_TAPE) - return { { { }, { }, tape_file.c_str() } }; - - return { }; -} - -bool save_disk_configuration(const std::string & nbd_host, const int nbd_port, const std::optional & tape_file, const disk_type_t dt, console *const cnsl) -{ -#if IS_POSIX - json_t *json = json_object(); - - json_object_set(json, "NBD-host", json_string(nbd_host.c_str())); - json_object_set(json, "NBD-port", json_integer(nbd_port)); - - if (dt == DT_RK05) - json_object_set(json, "disk-type", json_string("rk05")); - else if (dt == DT_RL02) - json_object_set(json, "disk-type", json_string("rl02")); - else - json_object_set(json, "disk-type", json_string("tape")); - - json_object_set(json, "tape-file", json_string(tape_file.has_value() ? tape_file.value().c_str() : "")); - - bool succeeded = json_dump_file(json, "." NET_DISK_CFG_FILE, 0) == 0; - json_decref(json); - - if (succeeded == false) { - cnsl->put_string_lf(format("Cannot write ." NET_DISK_CFG_FILE)); - - return false; - } -#else - json_doc["NBD-host"] = nbd_host; - json_doc["NBD-port"] = nbd_port; - - if (dt == DT_RK05) - json_doc["disk-type"] = "rk05"; - else if (dt == DT_RL02) - json_doc["disk-type"] = "rl02"; - else - json_doc["disk-type"] = "tape"; - - json_doc["tape-file"] = tape_file.has_value() ? tape_file.value() : ""; - - File dataFile = LittleFS.open("/" NET_DISK_CFG_FILE, "w"); - if (!dataFile) - return false; - - serializeJson(json_doc, dataFile); - - dataFile.close(); -#endif - - return true; -} -#endif - -std::optional select_disk_backend(console *const c) -{ -#if defined(BUILD_FOR_RP2040) - return BE_SD; -#elif linux - c->put_string("1. network (NBD), 2. local filesystem, 9. abort"); -#else - c->put_string("1. network (NBD), 2. local SD card, 9. abort"); -#endif - - int ch = -1; - while(ch == -1 && ch != '1' && ch != '2' && ch != '9') { - auto temp = c->wait_char(500); - - if (temp.has_value()) - ch = temp.value(); - } - - c->put_string_lf(format("%c", ch)); - - if (ch == '1') - return BE_NETWORK; - - if (ch == '2') - return BE_SD; - - return { }; -} - -std::optional select_disk_type(console *const c) -{ - c->put_string("1. RK05, 2. RL02, 3. tape/BIC, 9. abort"); - - int ch = -1; - while(ch == -1 && ch != '1' && ch != '2' && ch != '3' && ch != '9') { - auto temp = c->wait_char(500); - - if (temp.has_value()) - ch = temp.value(); - } - - c->put_string_lf(format("%c", ch)); - - if (ch == '1') - return DT_RK05; - - if (ch == '2') - return DT_RL02; - - if (ch == '3') - return DT_TAPE; - - return { }; -} - -#if !defined(BUILD_FOR_RP2040) -std::optional, std::vector, std::string> > select_nbd_server(console *const c) -{ - c->flush_input(); - - std::string hostname = c->read_line("Enter hostname (or empty to abort): "); + std::string hostname = cnsl->read_line("Enter hostname (or empty to abort): "); if (hostname.empty()) return { }; - std::string port_str = c->read_line("Enter port number (or empty to abort): "); + std::string port_str = cnsl->read_line("Enter port number (or empty to abort): "); if (port_str.empty()) return { }; - auto disk_type = select_disk_type(c); - - if (disk_type.has_value() == false) - return { }; - disk_backend *d = new disk_backend_nbd(hostname, atoi(port_str.c_str())); - if (d->begin() == false) { - c->put_string_lf("Cannot initialize NBD client"); + if (d->begin(false) == false) { + cnsl->put_string_lf("Cannot initialize NBD client"); delete d; return { }; } - if (save_disk_configuration(hostname, atoi(port_str.c_str()), { }, disk_type.value(), c)) - c->put_string_lf("NBD disk configuration saved"); - else - c->put_string_lf("NBD disk configuration NOT saved"); - - if (disk_type.value() == DT_RK05) - return { { { d }, { }, "" } }; - - if (disk_type.value() == DT_RL02) - return { { { }, { d }, "" } }; - - return { }; + return d; } #endif -// RK05, RL02 files -std::optional, std::vector, std::string> > select_disk_files(console *const c) +// disk image files +std::optional select_disk_file(console *const c) { #if IS_POSIX c->put_string_lf("Files in current directory: "); @@ -368,11 +149,6 @@ std::optional, std::vectorput_string("Opening file: "); c->put_string_lf(selected_file.c_str()); @@ -389,16 +165,13 @@ std::optional, std::vectorbegin()) { + if (!temp->begin(false)) { c->put_string("Cannot use: "); c->put_string_lf(selected_file.c_str()); @@ -407,63 +180,136 @@ std::optional, std::vectorput_string_lf("open failed"); } + + return { }; } -void set_disk_configuration(bus *const b, console *const cnsl, std::tuple, std::vector, std::string> & disk_files) +int wait_for_key(const std::string & title, console *const cnsl, const std::vector & allowed) { - if (std::get<0>(disk_files).empty() == false) - b->add_rk05(new rk05(std::get<0>(disk_files), b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + cnsl->put_string_lf(title); - if (std::get<1>(disk_files).empty() == false) - b->add_rl02(new rl02(std::get<1>(disk_files), b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); + cnsl->put_string("> "); - if (std::get<2>(disk_files).empty() == false) { - auto addr = loadTape(b, std::get<2>(disk_files)); + int ch = -1; + while(ch == -1) { + auto temp = cnsl->wait_char(500); - if (addr.has_value()) - b->getCpu()->setPC(addr.value()); + if (temp.has_value()) { + for(auto & a: allowed) { + if (a == temp.value()) { + ch = temp.value(); + break; + } + } + } } - if (std::get<0>(disk_files).empty() == false) - setBootLoader(b, BL_RK05); - else if (std::get<1>(disk_files).empty() == false) - setBootLoader(b, BL_RL02); + cnsl->put_string_lf(format("%c", ch)); + + return ch; +} + +std::optional select_disk_backend(console *const cnsl) +{ +#if defined(BUILD_FOR_RP2040) + return select_disk_file(cnsl); +#else + int ch = wait_for_key("1. local disk, 2. network disk (NBD), 9. abort", cnsl, { '1', '2', '9' }); + if (ch == '9') + return { }; + + if (ch == '1') + return select_disk_file(cnsl); + + if (ch == '2') + return select_nbd_server(cnsl); + + return { }; +#endif } 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' }); + + bootloader_t bl = BL_NONE; + disk_device *dd = nullptr; + + if (ch == '1') { + dd = b->getRK05(); + bl = BL_RK05; + } + else if (ch == '2') { + dd = b->getRL02(); + bl = BL_RL02; + } + else if (ch == '9') { + return; + } + for(;;) { - cnsl->put_string_lf("Load disk"); + std::vector keys_allowed { '1', '2', '9' }; - auto backend = select_disk_backend(cnsl); + auto cartridge_slots = dd->access_disk_backends(); + int slot_key = 'A'; + for(auto & slot: *cartridge_slots) { + cnsl->put_string_lf(format(" %c. %s", slot_key, slot ? slot->get_identifier().c_str() : "-")); + keys_allowed.push_back(slot_key); + slot_key++; + } - if (backend.has_value() == false) + int ch = wait_for_key("Select cartridge to setup, 1. to add a cartridge, 2. to load a bootloader or 9. to exit", cnsl, keys_allowed); + if (ch == '9') break; - std::optional, std::vector, std::string> > files; + if (ch == '1') { + auto image_file = select_disk_backend(cnsl); -#if !defined(BUILD_FOR_RP2040) - if (backend == BE_NETWORK) - files = select_nbd_server(cnsl); - else // if (backend == BE_SD) -#endif - files = select_disk_files(cnsl); + if (image_file.has_value()) { + cartridge_slots->push_back(image_file.value()); - if (files.has_value() == false) - break; + cnsl->put_string_lf("Cartridge loaded"); + } + } + else if (ch == '2') { + set_boot_loader(b, bl); - set_disk_configuration(b, cnsl, files.value()); + cnsl->put_string_lf("Bootloader loaded"); + } + else { + int slot = ch - 'A'; - break; + for(;;) { + int ch = wait_for_key("Select cartridge action: 1. load, 2. unload, 9. exit", cnsl, { '1', '2', '9' }); + if (ch == '9') + break; + + if (ch == '1') { + auto image_file = select_disk_backend(cnsl); + + if (image_file.has_value()) { + delete cartridge_slots->at(slot); + cartridge_slots->at(slot) = image_file.value(); + + cnsl->put_string_lf("Cartridge loaded"); + } + } + else if (ch == '2') { + if (cartridge_slots->at(slot)) { + delete cartridge_slots->at(slot); + cartridge_slots->at(slot) = nullptr; + + cnsl->put_string_lf("Cartridge unloaded"); + } + } + } + } } } @@ -976,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); @@ -1002,11 +854,6 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto cnsl->put_string_lf("serspd requires an (decimal) parameter"); } - continue; - } - else if (cmd == "init") { - recall_configuration(cnsl); - continue; } #endif @@ -1027,7 +874,7 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto continue; } else if (parts[0] == "bl" && parts.size() == 2) { - setBootLoader(b, parts.at(1) == "rk05" ? BL_RK05 : BL_RL02); + set_boot_loader(b, parts.at(1) == "rk05" ? BL_RK05 : BL_RL02); cnsl->put_string_lf("Bootloader set"); continue; @@ -1098,7 +945,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; } @@ -1107,6 +957,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!"); @@ -1160,14 +1015,15 @@ void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const sto "bl - set bootload (rl02 or rk05)", #if IS_POSIX "ser - serialize state to a file", - "dser - deserialize state from a file", +// "dser - deserialize state from a file", #endif + "dp - disable panel", #if defined(ESP32) "cfgnet - configure network (e.g. WiFi)", "startnet - start network", "chknet - check network status", "serspd - set serial speed in bps (8N1 are default)", - "init - reload (disk-)configuration from flash", + "debug - debugging info", #endif "cfgdisk - configure disk", nullptr diff --git a/debugger.h b/debugger.h index 6a39e46..083f0a1 100644 --- a/debugger.h +++ b/debugger.h @@ -6,10 +6,6 @@ #include "gen.h" -std::optional, std::vector, std::string> > load_disk_configuration(console *const c); -bool save_disk_configuration(const std::string & nbd_host, const int nbd_port, const disk_type_t dt); -void set_disk_configuration(bus *const b, console *const cnsl, std::tuple, std::vector, std::string> & disk_files); - int disassemble(cpu *const c, console *const cnsl, const uint16_t pc, const bool instruction_only); void debugger(console *const cnsl, bus *const b, std::atomic_uint32_t *const stop_event, const bool tracing); diff --git a/disk_backend.cpp b/disk_backend.cpp index 1041ab2..a3e087b 100644 --- a/disk_backend.cpp +++ b/disk_backend.cpp @@ -7,8 +7,8 @@ #include "gen.h" #if IS_POSIX #include "disk_backend_file.h" -#endif #include "disk_backend_nbd.h" +#endif disk_backend::disk_backend() @@ -20,6 +20,80 @@ disk_backend::~disk_backend() } #if IS_POSIX +void disk_backend::store_object_in_overlay(const off_t id, const std::vector & data) +{ + overlay.insert_or_assign(id, data); +} + +std::optional > disk_backend::get_object_from_overlay(const off_t id) +{ + auto it = overlay.find(id); + if (it != overlay.end()) + return it->second; + + return { }; +} + +std::optional > disk_backend::get_from_overlay(const off_t offset, const size_t sector_size) +{ + assert((offset % sector_size) == 0); + + if (use_overlay) + return get_object_from_overlay(offset / sector_size); + + return { }; +} + +bool disk_backend::store_mem_range_in_overlay(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) +{ + assert((offset % sector_size) == 0); + assert((n % sector_size) == 0); + + if (use_overlay) { + for(size_t o=0; o(from + o, from + o + sector_size)); + + return true; + } + + return false; +} + +json_t *disk_backend::serialize_overlay() const +{ + json_t *out = json_object(); + + for(auto & id: overlay) { + json_t *j_data = json_array(); + + for(size_t i=0; i data; + for(size_t i=0; ibegin(); + d->deserialize_overlay(j); + + // assume we want snapshots (again?) + d->begin(true); return d; } diff --git a/disk_backend.h b/disk_backend.h index 141c2d8..48fbb20 100644 --- a/disk_backend.h +++ b/disk_backend.h @@ -3,7 +3,11 @@ #pragma once +#include +#include #include +#include +#include #include #include "gen.h" @@ -11,6 +15,20 @@ class disk_backend { +protected: +#if IS_POSIX + bool use_overlay { false }; + std::map > overlay; + + void store_object_in_overlay(const off_t id, const std::vector & data); + bool store_mem_range_in_overlay(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size); + std::optional > get_object_from_overlay(const off_t id); + std::optional > get_from_overlay(const off_t offset, const size_t sector_size); + + json_t *serialize_overlay() const; + void deserialize_overlay(const json_t *const j); +#endif + public: disk_backend(); virtual ~disk_backend(); @@ -20,9 +38,11 @@ public: static disk_backend *deserialize(const json_t *const j); #endif - virtual bool begin() = 0; + virtual std::string get_identifier() const = 0; - virtual bool read(const off_t offset, const size_t n, uint8_t *const target) = 0; + virtual bool begin(const bool disk_snapshots) = 0; - virtual bool write(const off_t offset, const size_t n, const uint8_t *const from) = 0; + virtual bool read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) = 0; + + virtual bool write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) = 0; }; diff --git a/disk_backend_file.cpp b/disk_backend_file.cpp index 127faa3..19ee4d2 100644 --- a/disk_backend_file.cpp +++ b/disk_backend_file.cpp @@ -1,11 +1,13 @@ // (C) 2018-2024 by Folkert van Heusden // Released under MIT license +#include #include #include #include #include "disk_backend_file.h" +#include "gen.h" #include "log.h" @@ -26,6 +28,8 @@ json_t *disk_backend_file::serialize() const json_object_set(j, "disk-backend-type", json_string("file")); + json_object_set(j, "overlay", serialize_overlay()); + // TODO store checksum of backend json_object_set(j, "filename", json_string(filename.c_str())); @@ -39,8 +43,10 @@ disk_backend_file *disk_backend_file::deserialize(const json_t *const j) } #endif -bool disk_backend_file::begin() +bool disk_backend_file::begin(const bool snapshots) { + use_overlay = snapshots; + fd = open(filename.c_str(), O_RDWR); if (fd == -1) { @@ -52,30 +58,48 @@ bool disk_backend_file::begin() return true; } -bool disk_backend_file::read(const off_t offset, const size_t n, uint8_t *const target) +bool disk_backend_file::read(const off_t offset_in, const size_t n, uint8_t *const target, const size_t sector_size) { - DOLOG(debug, false, "disk_backend_file::read: read %zu bytes from offset %zu", n, offset); + DOLOG(debug, false, "disk_backend_file::read: read %zu bytes from offset %zu", n, offset_in); + + assert((offset % sector_size) == 0); + assert((n % sector_size) == 0); + + for(off_t o=0; o #include #include #include @@ -54,6 +55,8 @@ json_t *disk_backend_nbd::serialize() const json_object_set(j, "disk-backend-type", json_string("nbd")); + json_object_set(j, "overlay", serialize_overlay()); + // TODO store checksum of backend json_object_set(j, "host", json_string(host.c_str())); json_object_set(j, "port", json_integer(port)); @@ -68,8 +71,12 @@ disk_backend_nbd *disk_backend_nbd::deserialize(const json_t *const j) } #endif -bool disk_backend_nbd::begin() +bool disk_backend_nbd::begin(const bool snapshots) { +#if IS_POSIX + use_overlay = snapshots; +#endif + if (!connect(false)) { DOLOG(ll_error, true, "disk_backend_nbd: cannot connect to NBD server"); return false; @@ -84,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 }; @@ -130,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) { @@ -154,14 +161,27 @@ bool disk_backend_nbd::connect(const bool retry) return fd != -1; } -bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const target) +bool disk_backend_nbd::read(const off_t offset_in, const size_t n, uint8_t *const target, const size_t sector_size) { - DOLOG(debug, false, "disk_backend_nbd::read: read %zu bytes from offset %zu", n, offset); + DOLOG(debug, false, "disk_backend_nbd::read: read %zu bytes from offset %zu", n, offset_in); if (n == 0) return true; - do { + size_t o = 0; + off_t offset = offset_in; + + while(offset < offset_in + off_t(n)) { +#if IS_POSIX + auto o_rc = get_from_overlay(offset, sector_size); + if (o_rc.has_value()) { + memcpy(&target[o], o_rc.value().data(), sector_size); + offset += sector_size; + o += sector_size; + continue; + } +#endif + if (fd == -1 && !connect(true)) { DOLOG(warning, true, "disk_backend_nbd::read: (re-)connect"); sleep(1); @@ -174,12 +194,12 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t uint64_t handle; uint64_t offset; uint32_t length; - } nbd_request { 0 }; + } nbd_request { }; nbd_request.magic = ntohl(0x25609513); nbd_request.type = 0; // READ nbd_request.offset = HTONLL(uint64_t(offset)); - nbd_request.length = htonl(n); + nbd_request.length = htonl(sector_size); if (WRITE(fd, reinterpret_cast(&nbd_request), sizeof nbd_request) != sizeof nbd_request) { DOLOG(warning, true, "disk_backend_nbd::read: problem sending request"); @@ -217,26 +237,33 @@ bool disk_backend_nbd::read(const off_t offset, const size_t n, uint8_t *const t return false; } - if (READ(fd, reinterpret_cast(target), n) != ssize_t(n)) { + if (READ(fd, reinterpret_cast(target), sector_size) != ssize_t(sector_size)) { DOLOG(warning, true, "disk_backend_nbd::read: problem receiving payload"); close(fd); fd = -1; sleep(1); continue; } + + offset += sector_size; + o += sector_size; } - while(fd == -1); return true; } -bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *const from) +bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) { DOLOG(debug, false, "disk_backend_nbd::write: write %zu bytes to offset %zu", n, offset); if (n == 0) return true; +#if IS_POSIX + if (store_mem_range_in_overlay(offset, n, from, sector_size)) + return true; +#endif + do { if (!connect(true)) { DOLOG(warning, true, "disk_backend_nbd::write: (re-)connect"); @@ -250,7 +277,7 @@ bool disk_backend_nbd::write(const off_t offset, const size_t n, const uint8_t * uint64_t handle; uint64_t offset; uint32_t length; - } nbd_request { 0 }; + } nbd_request { }; nbd_request.magic = ntohl(0x25609513); nbd_request.type = 1; // WRITE diff --git a/disk_backend_nbd.h b/disk_backend_nbd.h index b6679a8..fb369b9 100644 --- a/disk_backend_nbd.h +++ b/disk_backend_nbd.h @@ -1,4 +1,4 @@ -// (C) 2018-2023 by Folkert van Heusden +// (C) 2018-2024 by Folkert van Heusden // Released under MIT license #include @@ -6,6 +6,7 @@ #include "disk_backend.h" #include "gen.h" +#include "utils.h" class disk_backend_nbd : public disk_backend @@ -26,9 +27,11 @@ public: static disk_backend_nbd *deserialize(const json_t *const j); #endif - bool begin() override; + std::string get_identifier() const override { return format("%s:%d", host.c_str(), port); } - bool read(const off_t offset, const size_t n, uint8_t *const target) override; + bool begin(const bool snapshots) override; - bool write(const off_t offset, const size_t n, const uint8_t *const from) override; + bool read(const off_t offset, const size_t n, uint8_t *const target, const size_t sector_size) override; + + bool write(const off_t offset, const size_t n, const uint8_t *const from, const size_t sector_size) override; }; diff --git a/disk_device.h b/disk_device.h new file mode 100644 index 0000000..266fe84 --- /dev/null +++ b/disk_device.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +#include "device.h" +#include "disk_backend.h" + + +class disk_device: public device +{ +protected: + std::vector fhs; + +public: + disk_device() { + } + + virtual ~disk_device() { + } + + std::vector * access_disk_backends() { return &fhs; } +}; diff --git a/gen.h b/gen.h index 8629ac9..291e32b 100644 --- a/gen.h +++ b/gen.h @@ -28,5 +28,6 @@ typedef enum { rm_prev, rm_cur } rm_selection_t; // see also https://github.com/espressif/esp-idf/issues/1934 #define DEFAULT_N_PAGES 12 #else +// more requires unibusmap support #define DEFAULT_N_PAGES 31 #endif diff --git a/loaders.cpp b/loaders.cpp index a28ac4c..3c89805 100644 --- a/loaders.cpp +++ b/loaders.cpp @@ -23,14 +23,14 @@ void loadbin(bus *const b, uint16_t base, const char *const file) FILE *fh = fopen(file, "rb"); while(!feof(fh)) - b -> writeByte(base++, fgetc(fh)); + b->writeByte(base++, fgetc(fh)); fclose(fh); } -void setBootLoader(bus *const b, const bootloader_t which) +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 setBootLoader(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 setBootLoader(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 setBootLoader(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, @@ -139,12 +139,12 @@ void setBootLoader(bus *const b, const bootloader_t which) } for(int i=0; i writeWord(offset + i * 2, bl[i]); + b->writeWord(offset + i * 2, bl[i]); - c -> setRegister(7, start); + c->setRegister(7, start); } -std::optional loadTape(bus *const b, const std::string & file) +std::optional load_tape(bus *const b, const std::string & file) { #if defined(ESP32) File32 fh; @@ -211,7 +211,7 @@ std::optional loadTape(bus *const b, const std::string & file) #endif csum += c; - b -> writeByte(p++, c); + b->writeByte(p++, c); } #if defined(ESP32) @@ -271,6 +271,6 @@ void load_p11_x11(bus *const b, const std::string & file) fclose(fh); - cpu *const c = b -> getCpu(); - c -> setRegister(7, 0); + cpu *const c = b->getCpu(); + c->setRegister(7, 0); } diff --git a/loaders.h b/loaders.h index 35235df..4b6846f 100644 --- a/loaders.h +++ b/loaders.h @@ -1,4 +1,4 @@ -// (C) 2018-2023 by Folkert van Heusden +// (C) 2018-2024 by Folkert van Heusden // Released under MIT license #include @@ -11,6 +11,6 @@ typedef enum { BL_NONE, BL_RK05, BL_RL02 } bootloader_t; void loadbin(bus *const b, uint16_t base, const char *const file); -void setBootLoader(bus *const b, const bootloader_t which); -std::optional loadTape(bus *const b, const std::string & file); +void set_boot_loader(bus *const b, const bootloader_t which); +std::optional load_tape(bus *const b, const std::string & file); void load_p11_x11(bus *const b, const std::string & file); 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, ...); diff --git a/main.cpp b/main.cpp index d25804c..7426408 100644 --- a/main.cpp +++ b/main.cpp @@ -295,10 +295,19 @@ void get_metrics(cpu *const c) } } +void start_disk_devices(const std::vector & backends, const bool enable_snapshots) +{ + for(auto & backend: backends) { + if (backend->begin(enable_snapshots) == false) + error_exit(false, "Failed to initialize disk backend \"%s\"", backend->get_identifier().c_str()); + } +} + void help() { printf("-h this help\n"); printf("-D x deserialize state from file\n"); + printf("-P when serializing state to file (in the debugger), include an overlay: changes to disk-files are then non-persistent, they only exist in the state-dump\n"); printf("-T t.bin load file as a binary tape file (like simh \"load\" command), also for .BIC files\n"); printf("-B run tape file as a unit test (for .BIC files)\n"); printf("-R d.rk load file as a RK05 disk device\n"); @@ -318,27 +327,8 @@ void help() printf("-M log metrics\n"); } -#include "breakpoint_parser.h" int main(int argc, char *argv[]) { -#if 0 - { - bus *b = new bus(); - cpu *c = new cpu(b, &event); - b->add_cpu(c); - - std::pair > rc = parse_breakpoint(b, "(pc=0123 and (r0=01456 or r2=1) and memWV[0444]=0222)"); - printf("%p\n", rc.first); - - if (rc.second.has_value()) - printf("%s\n", rc.second.value().c_str()); - delete rc.first; - delete b; - } - - return 0; -#endif - //setlocale(LC_ALL, ""); std::vector rk05_files; @@ -365,7 +355,7 @@ int main(int argc, char *argv[]) std::string test; - disk_backend *temp_d = nullptr; + bool disk_snapshots = false; std::optional set_ram_size; @@ -376,7 +366,7 @@ int main(int argc, char *argv[]) std::string deserialize; int opt = -1; - while((opt = getopt(argc, argv, "hD:MT:Br:R:p:ndtL:bl:s:Q:N:J:XS:")) != -1) + while((opt = getopt(argc, argv, "hD:MT:Br:R:p:ndtL:bl:s:Q:N:J:XS:P")) != -1) { switch(opt) { case 'h': @@ -441,17 +431,11 @@ int main(int argc, char *argv[]) break; case 'R': - temp_d = new disk_backend_file(optarg); - if (!temp_d->begin()) - error_exit(false, "Cannot use file \"%s\" for RK05", optarg); - rk05_files.push_back(temp_d); + rk05_files.push_back(new disk_backend_file(optarg)); break; case 'r': - temp_d = new disk_backend_file(optarg); - if (!temp_d->begin()) - error_exit(false, "Cannot use file \"%s\" for RL02", optarg); - rl02_files.push_back(temp_d); + rl02_files.push_back(new disk_backend_file(optarg)); break; case 'N': { @@ -459,7 +443,7 @@ int main(int argc, char *argv[]) if (parts.size() != 3) error_exit(false, "-N: parameter missing"); - temp_d = new disk_backend_nbd(parts.at(0), atoi(parts.at(1).c_str())); + disk_backend *temp_d = new disk_backend_nbd(parts.at(0), atoi(parts.at(1).c_str())); if (parts.at(2) == "rk05") rk05_files.push_back(temp_d); @@ -494,6 +478,10 @@ int main(int argc, char *argv[]) set_ram_size = std::stoi(optarg); break; + case 'P': + disk_snapshots = true; + break; + default: fprintf(stderr, "-%c is not understood\n", opt); return 1; @@ -507,10 +495,14 @@ int main(int argc, char *argv[]) if (validate_json.empty() == false) return run_cpu_validation(validate_json); - DOLOG(info, true, "This PDP-11 emulator is called \"kek\" (reason for that is forgotten) and was written by Folkert van Heusden."); + DOLOG(info, true, "PDP11 emulator, by Folkert van Heusden"); DOLOG(info, true, "Built on: " __DATE__ " " __TIME__); + start_disk_devices(rk05_files, disk_snapshots); + + start_disk_devices(rl02_files, disk_snapshots); + #if !defined(_WIN32) if (withUI) cnsl = new console_ncurses(&event); @@ -533,26 +525,18 @@ int main(int argc, char *argv[]) cpu *c = new cpu(b, &event); b->add_cpu(c); - if (rk05_files.empty() == false) { - if (enable_bootloader == false) - DOLOG(warning, true, "Note: loading RK05 with no (RK05-) bootloader selected"); - else - bootloader = BL_RK05; + if (rk05_files.empty() == false) + bootloader = BL_RK05; - b->add_rk05(new rk05(rk05_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); - } - - if (rl02_files.empty() == false) { - if (enable_bootloader == false) - DOLOG(warning, true, "Note: loading RL02 with no (RL02-) bootloader selected"); - else - bootloader = BL_RL02; - - b->add_rl02(new rl02(rl02_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); - } + if (rl02_files.empty() == false) + bootloader = BL_RL02; if (enable_bootloader) - setBootLoader(b, 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_rl02(new rl02(rl02_files, b, cnsl->get_disk_read_activity_flag(), cnsl->get_disk_write_activity_flag())); } else { FILE *fh = fopen(deserialize.c_str(), "r"); @@ -590,7 +574,7 @@ int main(int argc, char *argv[]) std::optional bic_start; if (tape.empty() == false) { - bic_start = loadTape(b, tape); + bic_start = load_tape(b, tape); if (bic_start.has_value() == false) return 1; // fail 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() diff --git a/rk05.cpp b/rk05.cpp index 8b1aaf8..34d0c37 100644 --- a/rk05.cpp +++ b/rk05.cpp @@ -156,7 +156,7 @@ void rk05::writeWord(const uint16_t addr, const uint16_t v) for(size_t i=0; ireadUnibusByte(work_memoff++); - if (!fhs.at(device)->write(work_diskoffb, cur, xfer_buffer)) + if (!fhs.at(device)->write(work_diskoffb, cur, xfer_buffer, 512)) DOLOG(ll_error, true, "RK05(%d) write error %s to %u len %u", device, strerror(errno), work_diskoffb, cur); work_diskoffb += cur; @@ -191,7 +191,7 @@ void rk05::writeWord(const uint16_t addr, const uint16_t v) while(temp > 0) { uint32_t cur = std::min(uint32_t(sizeof xfer_buffer), temp); - if (!fhs.at(device)->read(temp_diskoffb, cur, xfer_buffer)) { + 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); break; } diff --git a/rk05.h b/rk05.h index 7ba4682..5189011 100644 --- a/rk05.h +++ b/rk05.h @@ -9,7 +9,7 @@ #include #include -#include "device.h" +#include "disk_device.h" #include "disk_backend.h" @@ -25,12 +25,11 @@ class bus; -class rk05 : public device +class rk05: public disk_device { private: bus *const b { nullptr }; uint16_t registers [7] { 0 }; - std::vector fhs; uint8_t xfer_buffer[512] { 0 }; std::atomic_bool *const disk_read_acitivity { nullptr }; diff --git a/rl02.cpp b/rl02.cpp index 0256c86..6974218 100644 --- a/rl02.cpp +++ b/rl02.cpp @@ -271,7 +271,7 @@ void rl02::writeWord(const uint16_t addr, uint16_t v) mpr[0]++; } - if (!fhs.at(device)->write(temp_disk_offset, cur, xfer_buffer)) { + if (fhs.at(device) == nullptr || fhs.at(device)->write(temp_disk_offset, cur, xfer_buffer, 256) == false) { DOLOG(ll_error, true, "RL02: write error, device %d, disk offset %u, read size %u, cylinder %d, head %d, sector %d", device, temp_disk_offset, cur, track, head, sector); break; } @@ -325,7 +325,7 @@ void rl02::writeWord(const uint16_t addr, uint16_t v) while(count > 0) { uint32_t cur = std::min(uint32_t(sizeof xfer_buffer), count); - if (!fhs.at(device)->read(temp_disk_offset, cur, xfer_buffer)) { + if (fhs.at(device) == nullptr || fhs.at(device)->read(temp_disk_offset, cur, xfer_buffer, 256) == false) { DOLOG(ll_error, true, "RL02: read error, device %d, disk offset %u, read size %u, cylinder %d, head %d, sector %d", device, temp_disk_offset, cur, track, head, sector); break; } @@ -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) { diff --git a/rl02.h b/rl02.h index 4a9858e..be8a96d 100644 --- a/rl02.h +++ b/rl02.h @@ -9,7 +9,7 @@ #include #include -#include "device.h" +#include "disk_device.h" #include "disk_backend.h" #include "gen.h" @@ -27,7 +27,7 @@ constexpr const int rl02_bytes_per_sector = 256; class bus; -class rl02 : public device +class rl02: public disk_device { private: bus *const b; @@ -37,10 +37,9 @@ private: uint8_t head { 0 }; uint8_t sector { 0 }; uint16_t mpr[3]; - std::vector fhs; - std::atomic_bool *const disk_read_activity { nullptr }; - std::atomic_bool *const disk_write_activity { nullptr }; + std::atomic_bool *const disk_read_activity { nullptr }; + std::atomic_bool *const disk_write_activity { nullptr }; uint32_t get_bus_address() const; void update_bus_address(const uint32_t a);