From 5f0ed87581808ca0a4a88f80e763368dc94b4c51 Mon Sep 17 00:00:00 2001 From: folkert van heusden Date: Sun, 5 May 2024 23:24:21 +0200 Subject: [PATCH] WIN32 build fixes --- CMakeLists.txt | 42 +++++++++++++++++++++++++----------------- dc11.cpp | 43 ++++++++++++++++++++++++++++--------------- dc11.h | 11 +++++++++++ debugger.cpp | 19 +++++++++++-------- disk_backend_file.cpp | 4 ++++ log.cpp | 7 ++++++- main.cpp | 9 +++++++-- memory.cpp | 1 + utils.cpp | 14 +++++++++----- 9 files changed, 102 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e51244..1f52f61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,8 @@ add_compile_options(-Wall -pedantic -Wextra) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) +include(FindPkgConfig) + if (NOT WIN32) add_executable( @@ -49,16 +51,38 @@ add_executable( utils.cpp ) +pkg_check_modules(NCURSES REQUIRED ncurses) +target_link_libraries(kek ${NCURSES_LIBRARIES}) +target_include_directories(kek PUBLIC ${NCURSES_INCLUDE_DIRS}) +target_compile_options(kek PUBLIC ${NCURSES_CFLAGS_OTHER}) + +pkg_check_modules(PANEL REQUIRED panel) +target_link_libraries(kek ${PANEL_LIBRARIES}) +target_include_directories(kek PUBLIC ${PANEL_INCLUDE_DIRS}) +target_compile_options(kek PUBLIC ${PANEL_CFLAGS_OTHER}) + +pkg_check_modules(JANSSON REQUIRED jansson) +target_link_libraries(kek ${JANSSON_LIBRARIES}) +target_include_directories(kek PUBLIC ${JANSSON_INCLUDE_DIRS}) +target_compile_options(kek PUBLIC ${JANSSON_CFLAGS_OTHER}) + endif (NOT WIN32) if (WIN32) add_executable( kek-win32 + breakpoint.cpp + breakpoint_and.cpp + breakpoint_memory.cpp + breakpoint_or.cpp + breakpoint_parser.cpp + breakpoint_register.cpp bus.cpp console.cpp console_posix.cpp cpu.cpp + dc11.cpp debugger.cpp disk_backend.cpp disk_backend_file.cpp @@ -69,6 +93,7 @@ add_executable( log.cpp main.cpp memory.cpp + mmu.cpp rk05.cpp rl02.cpp tm-11.cpp @@ -95,20 +120,3 @@ target_link_libraries(kek-win32 Threads::Threads) target_link_libraries(kek-win32 ws2_32) endif () - -include(FindPkgConfig) - -pkg_check_modules(NCURSES REQUIRED ncurses) -target_link_libraries(kek ${NCURSES_LIBRARIES}) -target_include_directories(kek PUBLIC ${NCURSES_INCLUDE_DIRS}) -target_compile_options(kek PUBLIC ${NCURSES_CFLAGS_OTHER}) - -pkg_check_modules(PANEL REQUIRED panel) -target_link_libraries(kek ${PANEL_LIBRARIES}) -target_include_directories(kek PUBLIC ${PANEL_INCLUDE_DIRS}) -target_compile_options(kek PUBLIC ${PANEL_CFLAGS_OTHER}) - -pkg_check_modules(JANSSON REQUIRED jansson) -target_link_libraries(kek ${JANSSON_LIBRARIES}) -target_include_directories(kek PUBLIC ${JANSSON_INCLUDE_DIRS}) -target_compile_options(kek PUBLIC ${JANSSON_CFLAGS_OTHER}) diff --git a/dc11.cpp b/dc11.cpp index 5e2d391..7f4ae0e 100644 --- a/dc11.cpp +++ b/dc11.cpp @@ -3,13 +3,18 @@ #if defined(ESP32) #include +#include +#include +#elif defined(_WIN32) +#include +#include #else #include +#include +#include #endif #include #include -#include -#include #include "bus.h" #include "cpu.h" @@ -24,7 +29,11 @@ dc11::dc11(const int base_port, bus *const b): base_port(base_port), b(b) { +#if defined(_WIN32) + pfds = new WSAPOLLFD[dc11_n_lines * 2](); +#else pfds = new pollfd[dc11_n_lines * 2](); +#endif // TODO move to begin() th = new std::thread(std::ref(*this)); @@ -55,7 +64,7 @@ void dc11::operator()() for(int i=0; i(&listen_addr), sizeof(listen_addr)) == -1) { close(pfds[i].fd); - pfds[i].fd = -1; + pfds[i].fd = INVALID_SOCKET; DOLOG(warning, true, "Cannot bind to port %d (DC11)", port); continue; @@ -90,7 +99,7 @@ void dc11::operator()() if (listen(pfds[i].fd, SOMAXCONN) == -1) { close(pfds[i].fd); - pfds[i].fd = -1; + pfds[i].fd = INVALID_SOCKET; DOLOG(warning, true, "Cannot listen on port %d (DC11)", port); continue; @@ -100,7 +109,11 @@ void dc11::operator()() } while(!stop_flag) { +#if defined(_WIN32) + int rc = WSAPoll(pfds, dc11_n_lines * 2, 100); +#else int rc = poll(pfds, dc11_n_lines * 2, 100); +#endif if (rc == 0) continue; @@ -113,14 +126,14 @@ void dc11::operator()() // disconnect any existing client session // yes, one can ddos with this - if (pfds[client_i].fd != -1) { + if (pfds[client_i].fd != INVALID_SOCKET) { close(pfds[client_i].fd); DOLOG(info, false, "Restarting session for port %d", base_port + i + 1); } pfds[client_i].fd = accept(pfds[i].fd, nullptr, nullptr); - if (pfds[client_i].fd != -1) { + if (pfds[client_i].fd != INVALID_SOCKET) { set_nodelay(pfds[client_i].fd); std::unique_lock lck(input_lock[i]); @@ -149,7 +162,7 @@ void dc11::operator()() registers[line_nr * 4 + 0] |= 0140000; // "ERROR", CARRIER TRANSITION close(pfds[i].fd); - pfds[i].fd = -1; + pfds[i].fd = INVALID_SOCKET; } else { for(int k=0; k #include #include +#if defined(_WIN32) +#include +#include +#else +#define SOCKET int +#define INVALID_SOCKET -1 +#endif #include "gen.h" #include "bus.h" @@ -33,7 +40,11 @@ private: std::thread *th { nullptr }; // not statically allocated because of compiling problems on arduino +#if defined(_WIN32) + WSAPOLLFD *pfds { nullptr }; +#else pollfd *pfds { nullptr }; +#endif std::vector recv_buffers[dc11_n_lines]; std::mutex input_lock[dc11_n_lines]; diff --git a/debugger.cpp b/debugger.cpp index c54db9e..cf631b5 100644 --- a/debugger.cpp +++ b/debugger.cpp @@ -3,22 +3,24 @@ #include #include "gen.h" -#if IS_POSIX +#if IS_POSIX || defined(_WIN32) #include -#include #include #include #else #include #include #endif +#if IS_POSIX +#include +#endif #include "breakpoint_parser.h" #include "bus.h" #include "console.h" #include "cpu.h" #include "disk_backend.h" -#if IS_POSIX +#if IS_POSIX || defined(_WIN32) #include "disk_backend_file.h" #else #include "disk_backend_esp32.h" @@ -45,7 +47,7 @@ void start_network(console *const cnsl); void set_tty_serial_speed(console *const c, const uint32_t bps); #endif -#if !defined(BUILD_FOR_RP2040) && !defined(linux) +#if !defined(BUILD_FOR_RP2040) && !defined(linux) && !defined(_WIN32) extern SdFs SD; #endif @@ -111,6 +113,7 @@ std::optional select_host_file(console *const c) entry.close(); } +#elif defined(_WIN32) #else SD.ls("/", LS_DATE | LS_SIZE | LS_R); #endif @@ -127,9 +130,9 @@ std::optional select_host_file(console *const c) bool can_open_file = false; -#if IS_POSIX +#if IS_POSIX || defined(_WIN32) struct stat st { }; - can_open_file = stat(selected_file.c_str(), &st) == 0; + can_open_file = ::stat(selected_file.c_str(), &st) == 0; #else File32 fh; can_open_file = fh.open(selected_file.c_str(), O_RDWR); @@ -147,7 +150,7 @@ std::optional select_host_file(console *const c) // disk image files std::optional select_disk_file(console *const c) { -#if IS_POSIX +#if IS_POSIX || defined(_WIN32) c->put_string_lf("Files in current directory: "); #else c->put_string_lf(format("MISO: %d", int(MISO))); @@ -179,7 +182,7 @@ std::optional select_disk_file(console *const c) if (selected_file.has_value() == false) break; -#if IS_POSIX +#if IS_POSIX || defined(_WIN32) disk_backend *temp = new disk_backend_file(selected_file.value()); #else disk_backend *temp = new disk_backend_esp32(selected_file.value()); diff --git a/disk_backend_file.cpp b/disk_backend_file.cpp index 13fb400..b8c55c9 100644 --- a/disk_backend_file.cpp +++ b/disk_backend_file.cpp @@ -45,7 +45,9 @@ disk_backend_file *disk_backend_file::deserialize(const json_t *const j) bool disk_backend_file::begin(const bool snapshots) { +#if IS_POSIX use_overlay = snapshots; +#endif fd = open(filename.c_str(), O_RDWR); @@ -97,8 +99,10 @@ bool disk_backend_file::write(const off_t offset, const size_t n, const uint8_t { TRACE("disk_backend_file::write: write %zu bytes to offset %zu", n, offset); +#if IS_POSIX if (store_mem_range_in_overlay(offset, n, from, sector_size)) return true; +#endif #if defined(_WIN32) // hope for the best if (lseek(fd, offset, SEEK_SET) == -1) diff --git a/log.cpp b/log.cpp index 5b6201c..bffb442 100644 --- a/log.cpp +++ b/log.cpp @@ -7,9 +7,14 @@ #include #include #include +#if defined(_WIN32) +#include +#include +#else #include #include #include +#endif #include #include "error.h" @@ -72,7 +77,7 @@ void setlogfile(const char *const lf, const log_level_t ll_file, const log_level bool setloghost(const char *const host, const log_level_t ll) { syslog_ip_addr.sin_family = AF_INET; - bool ok = inet_aton(host, &syslog_ip_addr.sin_addr) == 1; + bool ok = inet_pton(AF_INET, host, &syslog_ip_addr.sin_addr) == 1; syslog_ip_addr.sin_port = htons(514); is_file = false; diff --git a/main.cpp b/main.cpp index 986b896..9c92874 100644 --- a/main.cpp +++ b/main.cpp @@ -4,7 +4,9 @@ #include #include #include +#if !defined(_WIN32) #include +#endif #include #include #include @@ -53,7 +55,6 @@ void sw_handler(int s) event = EVENT_TERMINATE; } } -#endif int run_cpu_validation(const std::string & filename) { @@ -262,6 +263,7 @@ int run_cpu_validation(const std::string & filename) return 0; } +#endif void get_metrics(cpu *const c) { @@ -493,8 +495,10 @@ int main(int argc, char *argv[]) setlogfile(logfile, ll_file, ll_screen, timestamp); +#if !defined(_WIN32) if (validate_json.empty() == false) return run_cpu_validation(validate_json); +#endif DOLOG(info, true, "PDP11 emulator, by Folkert van Heusden"); @@ -551,6 +555,7 @@ int main(int argc, char *argv[]) if (enable_bootloader) set_boot_loader(b, bootloader); } +#if IS_POSIX else { FILE *fh = fopen(deserialize.c_str(), "r"); if (!fh) @@ -568,9 +573,9 @@ int main(int argc, char *argv[]) json_decref(j); - DOLOG(warning, true, "DO NOT FORGET TO DELETE AND NOT TO RE-USE THE STATE FILE (\"%s\")! (unless updated)", deserialize.c_str()); myusleep(251000); } +#endif if (b->getTty() == nullptr) { tty *tty_ = new tty(cnsl, b); diff --git a/memory.cpp b/memory.cpp index d50beb5..0d00503 100644 --- a/memory.cpp +++ b/memory.cpp @@ -4,6 +4,7 @@ #if defined(ESP32) #include #endif +#include #include #include "memory.h" diff --git a/utils.cpp b/utils.cpp index 1b86e81..faec346 100644 --- a/utils.cpp +++ b/utils.cpp @@ -6,11 +6,15 @@ #if defined(ESP32) || defined(BUILD_FOR_RP2040) #include #include "rp2040.h" +#include +#elif defined(_WIN32) +#include +#include #else #include #include -#endif #include +#endif #include #include @@ -175,7 +179,7 @@ void set_thread_name(std::string name) std::string get_thread_name() { -#if IS_POSIX +#if IS_POSIX || defined(_WIN32) char buffer[16 + 1] { }; pthread_getname_np(pthread_self(), buffer, sizeof buffer); @@ -246,10 +250,10 @@ void update_word(uint16_t *const w, const bool msb, const uint8_t v) void set_nodelay(const int fd) { int flags = 1; -#if defined(__FreeBSD__) || defined(ESP32) - if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags)) == -1) +#if defined(__FreeBSD__) || defined(ESP32) || defined(_WIN32) + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&flags), sizeof(flags)) == -1) #else - if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags)) == -1) + if (setsockopt(fd, SOL_TCP, TCP_NODELAY, reinterpret_cast(&flags), sizeof(flags)) == -1) #endif DOLOG(warning, true, "Cannot disable nagle algorithm"); }