From ed3ed7bd03afde78b6e5e6b865a555d4ae55f8fa Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Sun, 9 Feb 2025 09:52:37 +0100 Subject: [PATCH] Emulator: ncurses --- pat80-emulator/CMakeLists.txt | 4 +++ pat80-emulator/main.c | 55 ++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/pat80-emulator/CMakeLists.txt b/pat80-emulator/CMakeLists.txt index f7e2139..4634cd6 100644 --- a/pat80-emulator/CMakeLists.txt +++ b/pat80-emulator/CMakeLists.txt @@ -5,3 +5,7 @@ add_executable(pat80-emulator main.c) find_package(Z80 REQUIRED) target_link_libraries(pat80-emulator Z80) + +find_package(Curses REQUIRED) +include_directories(${CURSES_INCLUDE_DIR}) +target_link_libraries(pat80-emulator ${CURSES_LIBRARIES}) diff --git a/pat80-emulator/main.c b/pat80-emulator/main.c index 901103c..2c80b55 100644 --- a/pat80-emulator/main.c +++ b/pat80-emulator/main.c @@ -7,8 +7,8 @@ #include /* Z_NULL */ #include #include -#include #include +#include #define ROM_SIZE 0x8000 /* 32 KiB */ #define MEMORY_SIZE 0xFFFF /* 64 KiB */ @@ -46,45 +46,44 @@ static zuint8 machine_cpu_in(Machine *self, zuint16 port) { int decoded = port & bitmask; if (decoded <= 0x1F) { // Port 0 (0x00 to 0x1F): terminal - //printf("TERMINAL_READ"); - //return 'H'; - return 0x00; + // Read char from stin + return getch(); } if (decoded <= 0x3F) { // Port 1 (0x20 to 0x3F): sound card (sn76489) - printf("\nsound_cmd[IN]: Not supported!\n"); + printw("\nsound_cmd[IN]: Not supported!\n"); return 0x00; } if (decoded <= 0x5F) { // Port 2 (0x40 to 0x5F) - printf("IO_ERROR_IN: No device at port 2\n"); + printw("IO_ERROR_IN: No device at port 2\n"); return 0x00; } if (decoded <= 0x7F) { // Port 3 (0x60 to 0x7F) - printf("IO_ERROR_IN: No device at port 3\n"); + printw("IO_ERROR_IN: No device at port 3\n"); return 0x00; } if (decoded <= 0x9F) { // Port 4 (0x80 to 0x9F) - printf("IO_ERROR_IN: No device at port 4\n"); + printw("IO_ERROR_IN: No device at port 4\n"); return 0x00; } if (decoded <= 0x5F) { // Port 5 (0xA0 to 0xBF) - printf("IO_ERROR_IN: No device at port 5\n"); + printw("IO_ERROR_IN: No device at port 5\n"); return 0x00; } if (decoded <= 0x5F) { // Port 6 (0xC0 to 0xDF) - printf("IO_ERROR_IN: No device at port 6\n"); + printw("IO_ERROR_IN: No device at port 6\n"); return 0x00; } if (decoded <= 0x5F) { // Port 7 (0xE0 to 0xFF) - printf("IO_ERROR_IN: No device at port 7\n"); + printw("IO_ERROR_IN: No device at port 7\n"); } else { - printf("IO_ERROR_IN: Invalid port address: %#04x\n", port); + printw("IO_ERROR_IN: Invalid port address: %#04x\n", port); } } @@ -98,30 +97,32 @@ static void machine_cpu_out(Machine *self, zuint16 port, zuint8 value) { int decoded = port & bitmask; if (decoded <= 0x1F) { // Port 0 (0x00 to 0x1F): terminal - printf("%c", value); + attron(A_BOLD); + printw("%c", value); + attroff(A_BOLD); } else if (decoded <= 0x3F) { // Port 1 (0x20 to 0x3F): sound card (sn76489) - printf("\nsound_cmd[%#04x]\n", value); + printw("\nsound_cmd[%#04x]\n", value); } else if (decoded <= 0x5F) { // Port 2 (0x40 to 0x5F) - printf("IO_ERROR_OUT: No device at port 2\n"); + printw("IO_ERROR_OUT: No device at port 2\n"); } else if (decoded <= 0x7F) { // Port 3 (0x60 to 0x7F) - printf("IO_ERROR_OUT: No device at port 3\n"); + printw("IO_ERROR_OUT: No device at port 3\n"); } else if (decoded <= 0x9F) { // Port 4 (0x80 to 0x9F) - printf("IO_ERROR_OUT: No device at port 4\n"); + printw("IO_ERROR_OUT: No device at port 4\n"); } else if (decoded <= 0x5F) { // Port 5 (0xA0 to 0xBF) - printf("IO_ERROR_OUT: No device at port 5\n"); + printw("IO_ERROR_OUT: No device at port 5\n"); } else if (decoded <= 0x5F) { // Port 6 (0xC0 to 0xDF) - printf("IO_ERROR_OUT: No device at port 6\n"); + printw("IO_ERROR_OUT: No device at port 6\n"); } else if (decoded <= 0x5F) { // Port 7 (0xE0 to 0xFF) - printf("IO_ERROR_OUT: No device at port 7\n"); + printw("IO_ERROR_OUT: No device at port 7\n"); } else { - printf("IO_ERROR_OUT: Invalid port address: %#04x\n", port); + printw("IO_ERROR_OUT: Invalid port address: %#04x\n", port); } } @@ -196,6 +197,18 @@ int main(int argc, char *argv[]) { fread(&pat80.memory,ROM_SIZE,1,romFile); // load rom from file into memory, up > fclose(romFile); + // Init ncurses + initscr(); // Start curses mode + raw(); // Line buffering disabled (get character without waiting for ENTER key) + keypad(stdscr, TRUE); // We get F1, F2 etc.. + noecho(); // Don't echo() while we do getch + scrollok(stdscr, TRUE); // Allow scrolling when reached end of window + + // Start emulated computer machine_reset(&pat80); machine_run(&pat80); + + // Stop ncurses + endwin(); /* End curses mode */ + return 0; }