WIN32 build fixes

This commit is contained in:
folkert van heusden 2024-05-05 23:24:21 +02:00
parent bb17a477b3
commit 5f0ed87581
Signed by untrusted user who does not match committer: folkert
GPG key ID: 6B6455EDFEED3BD1
9 changed files with 102 additions and 48 deletions

View file

@ -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})

View file

@ -3,13 +3,18 @@
#if defined(ESP32)
#include <lwip/sockets.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#elif defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#include <poll.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#endif
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#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<dc11_n_lines; i++) {
// client session
pfds[dc11_n_lines + i].fd = -1;
pfds[dc11_n_lines + i].fd = INVALID_SOCKET;
pfds[dc11_n_lines + i].events = POLLIN;
// listen on port
@ -66,7 +75,7 @@ void dc11::operator()()
int reuse_addr = 1;
if (setsockopt(pfds[i].fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse_addr, sizeof(reuse_addr)) == -1) {
close(pfds[i].fd);
pfds[i].fd = -1;
pfds[i].fd = INVALID_SOCKET;
DOLOG(warning, true, "Cannot set reuseaddress for port %d (DC11)", port);
continue;
@ -82,7 +91,7 @@ void dc11::operator()()
if (bind(pfds[i].fd, reinterpret_cast<struct sockaddr *>(&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<std::mutex> 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<rc; k++)
@ -166,7 +179,7 @@ void dc11::operator()()
DOLOG(info, true, "DC11 thread terminating");
for(int i=0; i<dc11_n_lines * 2; i++) {
if (pfds[i].fd != -1)
if (pfds[i].fd != INVALID_SOCKET)
close(pfds[i].fd);
}
}
@ -210,7 +223,7 @@ uint16_t dc11::read_word(const uint16_t addr)
registers[line_nr * 4 + 0] &= ~1; // DTR: bit 0 [RCSR]
registers[line_nr * 4 + 0] &= ~4; // CD : bit 2
if (pfds[line_nr + dc11_n_lines].fd != -1) {
if (pfds[line_nr + dc11_n_lines].fd != INVALID_SOCKET) {
registers[line_nr * 4 + 0] |= 1;
registers[line_nr * 4 + 0] |= 4;
}
@ -246,7 +259,7 @@ uint16_t dc11::read_word(const uint16_t addr)
registers[line_nr * 4 + 2] &= ~2; // CTS: bit 1 [TSCR]
registers[line_nr * 4 + 2] &= ~128; // READY: bit 7
if (pfds[line_nr + dc11_n_lines].fd != -1) {
if (pfds[line_nr + dc11_n_lines].fd != INVALID_SOCKET) {
registers[line_nr * 4 + 2] |= 2;
registers[line_nr * 4 + 2] |= 128;
}
@ -293,15 +306,15 @@ void dc11::write_word(const uint16_t addr, uint16_t v)
else
TRACE("DC11: transmit %c on line %d", c, line_nr);
int fd = pfds[dc11_n_lines + line_nr].fd;
SOCKET fd = pfds[dc11_n_lines + line_nr].fd;
if (fd != -1 && write(fd, &c, 1) != 1) {
if (fd != INVALID_SOCKET && write(fd, &c, 1) != 1) {
DOLOG(info, false, "DC11 line %d disconnected\n", line_nr + 1);
registers[line_nr * 4 + 0] |= 0140000; // "ERROR", CARRIER TRANSITION
close(fd);
pfds[dc11_n_lines + line_nr].fd = -1;
pfds[dc11_n_lines + line_nr].fd = INVALID_SOCKET;
}
if (is_tx_interrupt_enabled(line_nr))

11
dc11.h
View file

@ -8,6 +8,13 @@
#include <mutex>
#include <thread>
#include <vector>
#if defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#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<char> recv_buffers[dc11_n_lines];
std::mutex input_lock[dc11_n_lines];

View file

@ -3,22 +3,24 @@
#include <optional>
#include "gen.h"
#if IS_POSIX
#if IS_POSIX || defined(_WIN32)
#include <dirent.h>
#include <jansson.h>
#include <sys/stat.h>
#include <sys/types.h>
#else
#include <Arduino.h>
#include <LittleFS.h>
#endif
#if IS_POSIX
#include <jansson.h>
#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<std::string> 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<std::string> 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<std::string> select_host_file(console *const c)
// disk image files
std::optional<disk_backend *> 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<disk_backend *> 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());

View file

@ -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)

View file

@ -7,9 +7,14 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#if defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include <sys/types.h>
#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;

View file

@ -4,7 +4,9 @@
#include <assert.h>
#include <atomic>
#include <cinttypes>
#if !defined(_WIN32)
#include <jansson.h>
#endif
#include <signal.h>
#include <stdlib.h>
#include <string>
@ -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);

View file

@ -4,6 +4,7 @@
#if defined(ESP32)
#include <Arduino.h>
#endif
#include <cstdlib>
#include <cstring>
#include "memory.h"

View file

@ -6,11 +6,15 @@
#if defined(ESP32) || defined(BUILD_FOR_RP2040)
#include <Arduino.h>
#include "rp2040.h"
#include <sys/socket.h>
#elif defined(_WIN32)
#include <ws2tcpip.h>
#include <winsock2.h>
#else
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#include <sys/socket.h>
#endif
#include <errno.h>
#include <pthread.h>
@ -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<char *>(&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<void *>(&flags), sizeof(flags)) == -1)
#endif
DOLOG(warning, true, "Cannot disable nagle algorithm");
}