From: Robert Pengelly Date: Tue, 20 Oct 2026 07:15:18 +0000 (+0100) Subject: Added support for direct video memory writes X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=4d4618836fc0bdb01c6cd39a4eef8d2dd2d0da67;p=dosemu.git Added support for direct video memory writes --- diff --git a/interrupts.c b/interrupts.c index 60e77ff..bb7256f 100644 --- a/interrupts.c +++ b/interrupts.c @@ -7961,7 +7961,7 @@ static void bios_windows_render (unsigned top, unsigned left, unsigned bottom, u HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO info; - CHAR_INFO cells[80 * 25]; + CHAR_INFO *cells; COORD size, origin = { 0, 0 }; SMALL_RECT region; @@ -7991,6 +7991,10 @@ 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)))) { + return; + } + for (row = top; row <= bottom; row++) { for (col = left; col <= right; col++) { @@ -8021,10 +8025,55 @@ 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 +static uint32_t bios_video_memory_base (void) { + return bios_video_mode == 0x07 ? 0xB0000 : 0xB8000; +} + +static void bios_video_copy_to_memory (CPU8086 *cpu, unsigned top, unsigned left, unsigned bottom, unsigned right) { + + uint32_t base = bios_video_memory_base (); + unsigned row, col; + + for (row = top; row <= bottom; row++) { + + for (col = left; col <= right; col++) { + + uint32_t address = base + (row * bios_video_columns + col) * 2; + + cpu->mem[address] = bios_video_chars[row][col]; + cpu->mem[address + 1] = bios_video_attrs[row][col]; + + } + + } + +} + +static void bios_video_copy_from_memory (CPU8086 *cpu) { + + uint32_t base = bios_video_memory_base (); + unsigned row, col; + + for (row = 0; row < bios_video_rows; row++) { + + for (col = 0; col < bios_video_columns; col++) { + + uint32_t address = base + (row * bios_video_columns + col) * 2; + + bios_video_chars[row][col] = cpu->mem[address]; + bios_video_attrs[row][col] = cpu->mem[address + 1]; + + } + + } + +} + static void bios_video_initialize (void) { unsigned row; @@ -8143,6 +8192,11 @@ static void int_10 (CPU8086 *cpu) { } } + + bios_video_copy_to_memory (cpu, 0, 0, 24, bios_video_columns - 1); + + } else { + bios_video_copy_from_memory (cpu); } @@ -8335,6 +8389,8 @@ static void int_10 (CPU8086 *cpu) { } + bios_video_copy_to_memory (cpu, top, left, bottom, right); + #if defined (_WIN32) if (bios_windows_legacy_console ()) { @@ -8411,6 +8467,20 @@ static void int_10 (CPU8086 *cpu) { } + if (bios_cursor_row[0] < bios_video_rows && bios_cursor_col[0] < bios_video_columns) { + + unsigned right = (unsigned) bios_cursor_col[0] + (count ? count - 1 : 0); + + if (right >= bios_video_columns) { + right = bios_video_columns - 1; + } + + if (count != 0) { + bios_video_copy_to_memory (cpu, bios_cursor_row[0], bios_cursor_col[0], bios_cursor_row[0], right); + } + + } + #if defined (_WIN32) if (bios_windows_legacy_console ()) { @@ -8481,6 +8551,97 @@ static void int_10 (CPU8086 *cpu) { } +void bios_video_refresh (CPU8086 *cpu) { + + unsigned dirty = mem_video_dirty; + unsigned row, col, first, last; + + unsigned viewport_width, viewport_height; + uint32_t base; + + mem_video_dirty = 0; + + if (!(dirty & (bios_video_mode == 0x07 ? 1 : 2))) { + return; + } + + bios_video_initialize (); + bios_video_viewport (&viewport_width, &viewport_height); + + base = bios_video_memory_base (); + +#if !defined (_WIN32) + fputs ("\0337\033[?7l", stdout); +#else + if (!bios_windows_legacy_console ()) { + fputs ("\0337\033[?7l", stdout); + } +#endif + + for (row = 0; row < bios_video_rows; row++) { + + first = bios_video_columns; + last = 0; + + for (col = 0; col < bios_video_columns; col++) { + + uint32_t address = base + (row * bios_video_columns + col) * 2; + + uint8_t ch = cpu->mem[address]; + uint8_t attr = cpu->mem[address + 1]; + + if (ch != bios_video_chars[row][col] || attr != bios_video_attrs[row][col]) { + + bios_video_chars[row][col] = ch; + bios_video_attrs[row][col] = attr; + + if (first == bios_video_columns) { + first = col; + } + + last = col; + + } + + } + + if (first == bios_video_columns || row >= viewport_height || first >= viewport_width) { + continue; + } + +#if defined (_WIN32) + if (bios_windows_legacy_console ()) { + bios_windows_render (row, first, row, last); + } else +#endif + { + + unsigned end = last < viewport_width ? last : viewport_width - 1; + fprintf (stdout, "\033[%u;%uH", row + 1, first + 1); + + for (col = first; col <= end; col++) { + + bios_video_set_attribute (bios_video_attrs[row][col]); + bios_write_video_char (bios_video_chars[row][col]); + + } + + } + + } + +#if !defined (_WIN32) + fputs ("\033[0m\033[?7h\0338", stdout); +#else + if (!bios_windows_legacy_console ()) { + fputs ("\033[0m\033[?7h\0338", stdout); + } +#endif + + fflush (stdout); + +} + static void int_13 (CPU8086 *cpu) { uint8_t ah = get_reg8 (cpu, 4); @@ -8761,6 +8922,13 @@ void init_ivt (CPU8086 *cpu) { file_handle_references[2] = 1; bios_video_initialize (); + + bios_video_copy_to_memory (cpu, 0, 0, 24, 79); + bios_video_mode = 0x07; + + bios_video_copy_to_memory (cpu, 0, 0, 24, 79); + bios_video_mode = 0x03; + bios_video_update_bda (cpu); } diff --git a/interrupts.h b/interrupts.h index 03fca64..0416af9 100644 --- a/interrupts.h +++ b/interrupts.h @@ -13,5 +13,6 @@ void handle_interrupt (CPU8086 *cpu, uint8_t interrupt_number); void native_interrupt (CPU8086 *cpu, uint8_t interrupt_number); const char *dos_register_initial_program (const char *path); +void bios_video_refresh (CPU8086 *cpu); #endif /* _INTERRUPTS_H */ diff --git a/main.c b/main.c index 0c65571..9322529 100644 --- a/main.c +++ b/main.c @@ -25,8 +25,9 @@ #include "memory.h" #include "psp.h" -#define MEM_SIZE (640UL << 10) +#define CONVENTIONAL_MEM_SIZE (640UL << 10) #define EXE_LOAD_SEG 0x0060 +#define MEM_SIZE (1UL << 20) #if defined (_WIN32) static HANDLE terminal_output_handle; @@ -254,9 +255,9 @@ int main (int argc, char **argv) { cpu_reset (&cpu, mem, MEM_SIZE); dos_path = dos_register_initial_program (path); - if (load_mz_file (path, &cpu, EXE_LOAD_SEG << 4, (uint16_t) ((MEM_SIZE - ((uint32_t) (EXE_LOAD_SEG << 4) << 4)) / 16))) { + if (load_mz_file (path, &cpu, EXE_LOAD_SEG << 4, (uint16_t) ((CONVENTIONAL_MEM_SIZE - ((uint32_t) (EXE_LOAD_SEG << 4) << 4)) / 16))) { init_psp (dos_path, &cpu, cmd_tail, EXE_LOAD_SEG); - } else if (load_com_file (path, &cpu, mem, MEM_SIZE, EXE_LOAD_SEG, &loaded)) { + } else if (load_com_file (path, &cpu, mem, CONVENTIONAL_MEM_SIZE, EXE_LOAD_SEG, &loaded)) { init_psp (dos_path, &cpu, cmd_tail, EXE_LOAD_SEG); } else { @@ -293,6 +294,7 @@ int main (int argc, char **argv) { } rc = cpu_step (&cpu); + bios_video_refresh (&cpu); if (rc <= 0) { diff --git a/memory.c b/memory.c index 4409d98..ed0a069 100644 --- a/memory.c +++ b/memory.c @@ -23,12 +23,21 @@ uint16_t mem_read16 (uint8_t *mem, size_t size, uint32_t addr) { } +unsigned mem_video_dirty = 0; int watch_suppress = 0; void mem_write8 (uint8_t *mem, size_t mem_size, uint32_t addr, uint8_t value) { if ((size_t) addr < mem_size) { + mem[addr] = value; + + if (addr >= 0xB0000 && addr < 0xB8000) { + mem_video_dirty |= 1; + } else if (addr >= 0xB8000 && addr < 0xC0000) { + mem_video_dirty |= 2; + } + } } diff --git a/memory.h b/memory.h index 8c548d2..1e812d6 100644 --- a/memory.h +++ b/memory.h @@ -8,6 +8,8 @@ #include #include "cpu.h" + +extern unsigned mem_video_dirty; extern int watch_suppress; uint8_t mem_read8 (const uint8_t *mem, size_t mem_size, uint32_t addr);