Emulator: ncurses

This commit is contained in:
Daniele Verducci 2025-02-09 09:52:37 +01:00
parent ec678cb3d0
commit ed3ed7bd03
2 changed files with 38 additions and 21 deletions

View File

@ -5,3 +5,7 @@ add_executable(pat80-emulator main.c)
find_package(Z80 REQUIRED) find_package(Z80 REQUIRED)
target_link_libraries(pat80-emulator Z80) target_link_libraries(pat80-emulator Z80)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
target_link_libraries(pat80-emulator ${CURSES_LIBRARIES})

View File

@ -7,8 +7,8 @@
#include <Z/constants/pointer.h> /* Z_NULL */ #include <Z/constants/pointer.h> /* Z_NULL */
#include <Z80.h> #include <Z80.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <ncurses.h>
#define ROM_SIZE 0x8000 /* 32 KiB */ #define ROM_SIZE 0x8000 /* 32 KiB */
#define MEMORY_SIZE 0xFFFF /* 64 KiB */ #define MEMORY_SIZE 0xFFFF /* 64 KiB */
@ -46,45 +46,44 @@ static zuint8 machine_cpu_in(Machine *self, zuint16 port) {
int decoded = port & bitmask; int decoded = port & bitmask;
if (decoded <= 0x1F) { if (decoded <= 0x1F) {
// Port 0 (0x00 to 0x1F): terminal // Port 0 (0x00 to 0x1F): terminal
//printf("TERMINAL_READ"); // Read char from stin
//return 'H'; return getch();
return 0x00;
} }
if (decoded <= 0x3F) { if (decoded <= 0x3F) {
// Port 1 (0x20 to 0x3F): sound card (sn76489) // Port 1 (0x20 to 0x3F): sound card (sn76489)
printf("\nsound_cmd[IN]: Not supported!\n"); printw("\nsound_cmd[IN]: Not supported!\n");
return 0x00; return 0x00;
} }
if (decoded <= 0x5F) { if (decoded <= 0x5F) {
// Port 2 (0x40 to 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; return 0x00;
} }
if (decoded <= 0x7F) { if (decoded <= 0x7F) {
// Port 3 (0x60 to 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; return 0x00;
} }
if (decoded <= 0x9F) { if (decoded <= 0x9F) {
// Port 4 (0x80 to 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; return 0x00;
} }
if (decoded <= 0x5F) { if (decoded <= 0x5F) {
// Port 5 (0xA0 to 0xBF) // 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; return 0x00;
} }
if (decoded <= 0x5F) { if (decoded <= 0x5F) {
// Port 6 (0xC0 to 0xDF) // 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; return 0x00;
} }
if (decoded <= 0x5F) { if (decoded <= 0x5F) {
// Port 7 (0xE0 to 0xFF) // 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 { } 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; int decoded = port & bitmask;
if (decoded <= 0x1F) { if (decoded <= 0x1F) {
// Port 0 (0x00 to 0x1F): terminal // Port 0 (0x00 to 0x1F): terminal
printf("%c", value); attron(A_BOLD);
printw("%c", value);
attroff(A_BOLD);
} else if (decoded <= 0x3F) { } else if (decoded <= 0x3F) {
// Port 1 (0x20 to 0x3F): sound card (sn76489) // Port 1 (0x20 to 0x3F): sound card (sn76489)
printf("\nsound_cmd[%#04x]\n", value); printw("\nsound_cmd[%#04x]\n", value);
} else if (decoded <= 0x5F) { } else if (decoded <= 0x5F) {
// Port 2 (0x40 to 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) { } else if (decoded <= 0x7F) {
// Port 3 (0x60 to 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) { } else if (decoded <= 0x9F) {
// Port 4 (0x80 to 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) { } else if (decoded <= 0x5F) {
// Port 5 (0xA0 to 0xBF) // 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) { } else if (decoded <= 0x5F) {
// Port 6 (0xC0 to 0xDF) // 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) { } else if (decoded <= 0x5F) {
// Port 7 (0xE0 to 0xFF) // 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 { } 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 > fread(&pat80.memory,ROM_SIZE,1,romFile); // load rom from file into memory, up >
fclose(romFile); 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_reset(&pat80);
machine_run(&pat80); machine_run(&pat80);
// Stop ncurses
endwin(); /* End curses mode */
return 0;
} }