Skip to content

Commit

Permalink
WIN32 build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed May 5, 2024
1 parent bb17a47 commit 5f0ed87
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 48 deletions.
42 changes: 25 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -69,6 +93,7 @@ add_executable(
log.cpp
main.cpp
memory.cpp
mmu.cpp
rk05.cpp
rl02.cpp
tm-11.cpp
Expand All @@ -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})
43 changes: 28 additions & 15 deletions dc11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -82,15 +91,15 @@ 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;
}

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;
Expand All @@ -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;

Expand All @@ -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]);
Expand Down Expand Up @@ -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++)
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down
11 changes: 11 additions & 0 deletions dc11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,7 +40,11 @@ class dc11
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];

Expand Down
19 changes: 11 additions & 8 deletions debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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)));
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions disk_backend_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 5f0ed87

Please sign in to comment.