From 61f191400062daae688e42f1129098ccc3f92dca Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Wed, 21 Oct 2026 18:19:27 +0100 Subject: [PATCH] Windows fixes --- interrupts.c | 164 ++++++++++++++++++++++++++++++++++++--------------- interrupts.h | 5 ++ main.c | 30 ++++++++++ 3 files changed, 152 insertions(+), 47 deletions(-) diff --git a/interrupts.c b/interrupts.c index fe47b99..cecfe92 100644 --- a/interrupts.c +++ b/interrupts.c @@ -1149,6 +1149,73 @@ static int host_windows_key_repeat = -1; static unsigned host_windows_key_repeats; static uint8_t host_windows_shift_flags; +static HANDLE host_windows_console_input = INVALID_HANDLE_VALUE; +static DWORD host_windows_console_input_mode; + +static int host_windows_console_input_mode_changed; + +void host_windows_keyboard_initialize (void) { + + DWORD mode, new_mode; + HANDLE handle; + + if (host_windows_console_input != INVALID_HANDLE_VALUE) { + return; + } + + handle = CreateFileA ("CONIN$", GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, 0, NULL); + + if (handle == INVALID_HANDLE_VALUE) { + return; + } + + if (!GetConsoleMode (handle, &mode)) { + + CloseHandle (handle); + return; + + } + + new_mode = (mode | ENABLE_EXTENDED_FLAGS) & ~(ENABLE_QUICK_EDIT_MODE | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT); + + if (new_mode != mode) { + + if (!SetConsoleMode (handle, new_mode)) { + + CloseHandle (handle); + return; + + } + + host_windows_console_input_mode_changed = 1; + + } + + host_windows_console_input_mode = mode; + host_windows_console_input = handle; + +} + +void host_windows_keyboard_shutdown (void) { + + if (host_windows_console_input == INVALID_HANDLE_VALUE) { + return; + } + + if (host_windows_console_input_mode_changed) { + + SetConsoleMode (host_windows_console_input, host_windows_console_input_mode); + host_windows_console_input_mode_changed = 0; + + } + + CloseHandle (host_windows_console_input); + host_windows_console_input = INVALID_HANDLE_VALUE; + +} + static int host_windows_translate_key (const KEY_EVENT_RECORD *key) { WORD vk = key->wVirtualKeyCode; @@ -1289,12 +1356,9 @@ static int host_windows_console_key (FILE *source, int wait) { HANDLE handle; - DWORD old_mode, new_mode, read; + DWORD read; INPUT_RECORD event; - int close_handle = 0; - int changed_mode = 0; - int key = -1; if (host_windows_key_pending >= 0) { @@ -1314,29 +1378,13 @@ static int host_windows_console_key (FILE *source, int wait) { } if (source == stdout || source == stderr) { - - handle = CreateFileA ("CONIN$", GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, 0, NULL); - - if (handle == INVALID_HANDLE_VALUE) { - return -1; - } - - close_handle = 1; - + handle = host_windows_console_input; } else { handle = (HANDLE) _get_osfhandle (_fileno (source)); } - if (handle == NULL || handle == INVALID_HANDLE_VALUE || !GetConsoleMode (handle, &old_mode)) { - goto done; - } - - new_mode = old_mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT); - - if (new_mode != old_mode && SetConsoleMode (handle, new_mode)) { - changed_mode = 1; + if (handle == NULL || handle == INVALID_HANDLE_VALUE) { + return -1; } for (;;) { @@ -1391,21 +1439,7 @@ static int host_windows_console_key (FILE *source, int wait) { break; } - - if (changed_mode) { - SetConsoleMode (handle, old_mode); - } - - } - if (changed_mode) { - SetConsoleMode (handle, old_mode); - } - -done: - - if (close_handle) { - CloseHandle (handle); } return key; @@ -1427,12 +1461,14 @@ static int host_read_key_from (FILE *source) { HANDLE handle; DWORD mode; + int key; if (host_extended_key >= 0) { key = host_extended_key; host_extended_key = -1; + return key; } @@ -1453,6 +1489,8 @@ static int host_read_key_from (FILE *source) { return 0; } + + return key & 0xFF; } @@ -1936,7 +1974,13 @@ static int host_key_available_from (FILE *source) { static int host_read_key (CPU8086 *cpu) { - FILE *source = dos_get_handle_file (cpu, 0); + FILE *source; + +#if defined (_WIN32) + bios_video_refresh (cpu); +#endif + + source = dos_get_handle_file (cpu, 0); return host_read_key_from (source ? source : stdin); } @@ -1958,6 +2002,10 @@ static int bios_read_key (CPU8086 *cpu, int wait) { } #if defined (_WIN32) + if (wait) { + bios_video_refresh (cpu); + } + ch = host_windows_console_key (stdout, wait); mem_write8 (cpu->mem, cpu->mem_size, get_phys (0x0040, 0x0017), host_windows_shift_flags); #else @@ -9278,10 +9326,10 @@ static void bios_windows_show_cursor (int visible) { static void bios_windows_render (unsigned top, unsigned left, unsigned bottom, unsigned right) { - HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE); + static CHAR_INFO cells[80 * 25]; + HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO info; - CHAR_INFO *cells; COORD size, origin = { 0, 0 }; SMALL_RECT region; @@ -9311,7 +9359,7 @@ static void bios_windows_render (unsigned top, unsigned left, unsigned bottom, u width = right - left + 1; - if (!(cells = malloc (width * (bottom - top + 1) * sizeof (*cells)))) { + if (width * (bottom - top + 1) > sizeof (cells) / sizeof (cells[0])) { return; } @@ -9345,7 +9393,6 @@ static void bios_windows_render (unsigned top, unsigned left, unsigned bottom, u region.Bottom = (SHORT) (info.srWindow.Top + bottom); WriteConsoleOutputW (handle, cells, size, origin, ®ion); - free (cells); } #endif @@ -9524,6 +9571,13 @@ static void int_10 (CPU8086 *cpu) { uint8_t ah = get_reg8 (cpu, 4); +#if defined (_WIN32) + /* BIOS screen operations must see any pending direct video-memory writes. */ + if (mem_video_dirty) { + bios_video_refresh (cpu); + } +#endif + switch (ah) { case 0x00: { /* set video mode */ @@ -10045,6 +10099,13 @@ void bios_video_refresh (CPU8086 *cpu) { unsigned dirty = mem_video_dirty; unsigned row, col, first, last; +#if defined (_WIN32) + unsigned redraw_top = bios_video_rows, redraw_left = bios_video_columns; + unsigned redraw_bottom = 0, redraw_right = 0; + + int legacy_console = bios_windows_legacy_console (); +#endif + int full_redraw = bios_video_force_redraw; uint32_t base; @@ -10084,7 +10145,7 @@ void bios_video_refresh (CPU8086 *cpu) { bios_video_host_redraw = 0; #if defined (_WIN32) - if (bios_windows_legacy_console ()) { + if (legacy_console) { fflush (stdout); dos_windows_clear_screen (GetStdHandle (STD_OUTPUT_HANDLE), 0x07); @@ -10102,7 +10163,7 @@ void bios_video_refresh (CPU8086 *cpu) { #ifndef _WIN32 fputs ("\0337\033[?7l", stdout); #else - if (!bios_windows_legacy_console ()) { + if (!legacy_console) { fputs ("\0337\033[?7l", stdout); } #endif @@ -10146,8 +10207,13 @@ void bios_video_refresh (CPU8086 *cpu) { } #if defined (_WIN32) - if (bios_windows_legacy_console ()) { - bios_windows_render (row, first, row, last); + if (legacy_console) { + + if (row < redraw_top) redraw_top = row; + if (first < redraw_left) redraw_left = first; + if (row > redraw_bottom) redraw_bottom = row; + if (last > redraw_right) redraw_right = last; + } else #endif { @@ -10169,7 +10235,11 @@ void bios_video_refresh (CPU8086 *cpu) { #ifndef _WIN32 fputs ("\033[0m\033[?7h\0338", stdout); #else - if (!bios_windows_legacy_console ()) { + if (legacy_console && redraw_top < bios_video_rows) { + bios_windows_render (redraw_top, redraw_left, redraw_bottom, redraw_right); + } + + if (!legacy_console) { fputs ("\033[0m\033[?7h\0338", stdout); } #endif diff --git a/interrupts.h b/interrupts.h index 69c0923..929d661 100644 --- a/interrupts.h +++ b/interrupts.h @@ -17,4 +17,9 @@ const char *dos_register_initial_program (const char *path); void bios_timer_refresh (CPU8086 *cpu); void bios_video_refresh (CPU8086 *cpu); +#if defined (_WIN32) +void host_windows_keyboard_initialize (void); +void host_windows_keyboard_shutdown (void); +#endif + #endif /* _INTERRUPTS_H */ diff --git a/main.c b/main.c index 68838c3..2c74eb1 100644 --- a/main.c +++ b/main.c @@ -291,6 +291,7 @@ static void restore_terminal_cursor (void) { #if defined (_WIN32) DWORD mode; + host_windows_keyboard_shutdown (); if (terminal_output_handle == NULL || terminal_output_handle == INVALID_HANDLE_VALUE || !GetConsoleMode (terminal_output_handle, &mode)) { @@ -437,6 +438,10 @@ int main (int argc, char **argv) { int offset = 0, len, i; unsigned long steps = 0; +#if defined (_WIN32) + DWORD video_refresh_time = GetTickCount (); +#endif + for (i = 1; i < argc; i++) { if (strcmp (argv[i], "--trace") == 0) { @@ -521,6 +526,7 @@ int main (int argc, char **argv) { #if defined (_WIN32) initialize_terminal_output (); + host_windows_keyboard_initialize (); #else initialize_terminal_input (); #endif @@ -542,10 +548,30 @@ int main (int argc, char **argv) { } rc = cpu_step (&cpu); + +#if defined (_WIN32) + if ((steps & 0xFF) == 0) { + + DWORD now = GetTickCount (); + + if ((DWORD) (now - video_refresh_time) >= 16) { + + bios_video_refresh (&cpu); + video_refresh_time = now; + + } + + } +#else bios_video_refresh (&cpu); +#endif if (rc <= 0) { +#if defined (_WIN32) + bios_video_refresh (&cpu); +#endif + free (mem); return 1; @@ -555,6 +581,10 @@ int main (int argc, char **argv) { } +#if defined (_WIN32) + bios_video_refresh (&cpu); +#endif + free (mem); return 0; -- 2.34.1