break;
- case 0x3D8: case 0x3D9:
+ case 0x3D8: /* CGA mode control */
- /* CGA mode control / colour select - not yet modelled */
+ cpu->cga_mode_control = value;
+
+ mem_video_dirty |= 2;
+ break;
+
+ case 0x3D9: /* CGA colour select */
+
+ cpu->cga_color_select = value;
+
+ mem_video_dirty |= 2;
break;
default:
uint8_t crtc_index, crtc_registers[25];
uint16_t crtc_base;
+ uint8_t cga_mode_control, cga_color_select;
uint8_t vga_misc_output, vga_enable, vga_graphics_mode;
uint8_t vga_seq_index, vga_seq_registers[5];
uint8_t vga_gc_index, vga_gc_registers[9];
unsigned weight = width * height;
uint8_t index;
+
const uint8_t *rgb;
+ uint8_t cga_rgb[3];
if (bios_video_mode == 0x13) {
index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask;
rgb = cpu->vga_dac_palette[index];
+ } else if (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) {
+
+ uint32_t color = video_native_cga_pixel (cpu, bios_video_mode == 0x06 ? x * 2 : x, y, bios_video_mode);
+
+ cga_rgb[0] = (uint8_t) ((color >> 16) / 4);
+ cga_rgb[1] = (uint8_t) (((color >> 8) & 0xFF) / 4);
+ cga_rgb[2] = (uint8_t) ((color & 0xFF) / 4);
+
+ rgb = cga_rgb;
+
} else {
uint32_t address = 0xB8000 + ((y / 2) * 80 + (x / 4)) * 2;
struct timeval now;
#endif
- if (video_native_present (cpu, bios_video_mode != 0x13, bios_video_blink_enabled)) {
+ if (video_native_present (cpu, bios_video_mode, bios_video_blink_enabled)) {
return;
}
return;
}
- full_redraw = !bios_graphics_visible || columns != bios_graphics_columns || rows != bios_graphics_rows || bios_graphics_text_mode != (bios_video_mode != 0x13);
+ full_redraw = !bios_graphics_visible || columns != bios_graphics_columns || rows != bios_graphics_rows || bios_graphics_text_mode != bios_video_mode;
if (bios_video_mode == 0x13 && (memcmp (bios_graphics_palette, cpu->vga_dac_palette, sizeof (bios_graphics_palette)) != 0 || bios_graphics_dac_mask != cpu->vga_dac_mask)) {
full_redraw = 1;
bios_graphics_columns = columns;
bios_graphics_rows = rows;
- bios_graphics_text_mode = bios_video_mode != 0x13;
+ bios_graphics_text_mode = bios_video_mode;
memcpy (bios_graphics_palette, cpu->vga_dac_palette, sizeof (bios_graphics_palette));
bios_graphics_dac_mask = cpu->vga_dac_mask;
#endif
static unsigned bios_video_page_size (void) {
+
+ if (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) {
+ return 0x4000;
+ }
+
return bios_video_columns == 40 ? 0x0800 : 0x1000;
+
}
static unsigned bios_video_page_count (void) {
- return bios_video_mode == 0x07 ? 1 : (bios_video_columns == 40 ? 8 : 4);
+ return bios_video_mode == 0x07 || (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) ? 1 : (bios_video_columns == 40 ? 8 : 4);
}
static uint32_t bios_video_page_base (unsigned page) {
mem_write8 (cpu->mem, cpu->mem_size, 0x466, 0);
mem_write8 (cpu->mem, cpu->mem_size, 0x484, (uint8_t) (bios_video_rows - 1));
- mem_write16 (cpu->mem, cpu->mem_size, 0x485, bios_video_mode == 0x13 ? 8 : 16);
+ mem_write16 (cpu->mem, cpu->mem_size, 0x485, (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) || bios_video_mode == 0x13 ? 8 : 16);
cpu->vga_attr_registers[0x10] = (cpu->vga_attr_registers[0x10] & ~0x08) | (bios_video_blink_enabled ? 0x08 : 0);
cpu->crtc_base = bios_video_mode == 7 ? 0x3B4 : 0x3D4;
unsigned row, col;
- if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x07 && mode != 0x13) {
+ if (mode != 0x00 && mode != 0x01 && mode != 0x02 && mode != 0x03 && mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x07 && mode != 0x13) {
fprintf (stderr, "Unsupported Video Mode 0x%02X\n", mode);
break;
}
- bios_video_columns = mode == 0x13 ? 40 : (mode <= 1 ? 40 : 80);
+ bios_video_columns = mode == 0x13 || mode == 0x04 || mode == 0x05 ? 40 : (mode <= 1 ? 40 : 80);
bios_video_mode = mode;
bios_video_page = 0;
}
+ if (mode >= 0x04 && mode <= 0x06) {
+
+ cpu->cga_mode_control = mode == 0x06 ? 0x1A : 0x0A;
+ cpu->cga_color_select = mode == 0x06 ? 0x0F : 0x30;
+
+ if (!(requested & 0x80)) {
+ memset (cpu->mem + 0xB8000, 0, 0x4000);
+ }
+
+ bios_video_update_bda (cpu);
+
+ mem_video_dirty |= 2;
+ bios_video_force_redraw = 1;
+
+ break;
+
+ }
+
if (!(requested & 0x80)) {
for (row = 0; row < bios_video_rows; row++) {
}
- /* BH=01h selects a CGA graphics palette; graphics modes are not supported. */
+ if (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) {
+
+ if (bh == 0) {
+ cpu->cga_color_select = (cpu->cga_color_select & 0xF0) | (get_reg8 (cpu, REG_BX) & 0x0F);
+ } else if (bh == 1) {
+ cpu->cga_color_select = (cpu->cga_color_select & ~0x20) | ((get_reg8 (cpu, REG_BX) & 1) << 5);
+ }
+
+ mem_video_dirty |= 2;
+
+ }
+
break;
}
int full_redraw = bios_video_force_redraw;
uint32_t base;
- if (bios_video_mode == 0x13 || (bios_video_mode == 0x03 && bios_video_columns == 80 && (cpu->crtc_registers[9] & 0x1F) == 3)) {
+ if (bios_video_mode == 0x13 || (bios_video_mode >= 0x04 && bios_video_mode <= 0x06) || (bios_video_mode == 0x03 && bios_video_columns == 80 && (cpu->crtc_registers[9] & 0x1F) == 3)) {
bios_graphics_refresh (cpu);
return;
#include "cpu.h"
-int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled);
+int video_native_present (CPU8086 *cpu, int mode, int blink_enabled);
+
+uint32_t video_native_cga_pixel (CPU8086 *cpu, unsigned x, unsigned y, unsigned mode);
+void video_native_cga_render (CPU8086 *cpu, uint32_t *pixels, unsigned mode);
+
void video_native_text_render (CPU8086 *cpu, uint32_t *pixels, int blink_enabled);
int video_native_active (void);
}
-int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) {
+int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) {
+ int text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x13;
+ unsigned dirty_mask = mode == 0x13 ? 4u : 2u;
+
int palette_changed;
uint64_t now;
palette_changed = memcmp (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette)) || previous_mask != cpu->vga_dac_mask;
- if (!(mem_video_dirty & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled) && (backend != 1 || !x11.redraw)) {
+ if (!(mem_video_dirty & dirty_mask) && (text_mode || !palette_changed) && video_last_text_mode == mode && (!text_mode || video_last_blink_enabled == blink_enabled) && (backend != 1 || !x11.redraw)) {
return 1;
}
if (text_mode) {
video_native_text_render (cpu, video_framebuffer, blink_enabled);
+ } else if (mode != 0x13) {
+ video_native_cga_render (cpu, video_framebuffer, (unsigned) mode);
} else {
for (y = 0; y < 200; y++) {
memcpy (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette));
previous_mask = cpu->vga_dac_mask;
- mem_video_dirty &= ~(text_mode ? 2u : 4u);
+ mem_video_dirty &= ~dirty_mask;
- video_last_text_mode = text_mode;
+ video_last_text_mode = mode;
video_last_blink_enabled = blink_enabled;
return 1;
}
-int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) {
+int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) {
+ int text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x13;
+ unsigned dirty_mask = mode == 0x13 ? 4u : 2u;
+
unsigned x, y;
NSTimeInterval now;
video_native_poll (cpu);
palette_changed = memcmp (video_palette, cpu->vga_dac_palette, sizeof (video_palette)) || video_mask != cpu->vga_dac_mask;
- if (!(mem_video_dirty & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled)) {
+ if (!(mem_video_dirty & dirty_mask) && (text_mode || !palette_changed) && video_last_text_mode == mode && (!text_mode || video_last_blink_enabled == blink_enabled)) {
return 1;
}
if (text_mode) {
video_native_text_render (cpu, video_pixels, blink_enabled);
+ } else if (mode != 0x13) {
+ video_native_cga_render (cpu, video_pixels, (unsigned) mode);
} else {
for (y = 0; y < 200; y++) {
memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette));
video_mask = cpu->vga_dac_mask;
- mem_video_dirty &= ~(text_mode ? 2u : 4u);
+ mem_video_dirty &= ~dirty_mask;
- video_last_text_mode = text_mode;
+ video_last_text_mode = mode;
video_last_blink_enabled = blink_enabled;
[video_view setNeedsDisplay: YES];
}
}
+
+uint32_t video_native_cga_pixel (CPU8086 *cpu, unsigned x, unsigned y, unsigned mode) {
+
+ static const uint32_t colors[16] = {
+
+ 0x000000, 0x0000AA, 0x00AA00, 0x00AAAA,
+ 0xAA0000, 0xAA00AA, 0xAA5500, 0xAAAAAA,
+ 0x555555, 0x5555FF, 0x55FF55, 0x55FFFF,
+ 0xFF5555, 0xFF55FF, 0xFFFF55, 0xFFFFFF
+
+ };
+
+ uint32_t offset = 0xB8000 + (y & 1) * 0x2000 + (y >> 1) * 80;
+
+ uint8_t color;
+ uint8_t value;
+ uint8_t pixel;
+
+ if (mode == 0x06) {
+
+ value = cpu->mem[offset + (x >> 3)];
+ color = (value & (0x80 >> (x & 7))) ? (cpu->cga_color_select & 0x0F) : 0;
+
+ } else {
+
+ value = cpu->mem[offset + (x >> 2)];
+
+ pixel = (value >> (6 - (x & 3) * 2)) & 3;
+ color = pixel;
+
+ if (pixel == 0) {
+ color = cpu->cga_color_select & 0x0F;
+ } else if (mode == 0x05) {
+ color = (uint8_t) ((const uint8_t[]) { 0, 3, 4, 7 })[color];
+ } else if (cpu->cga_color_select & 0x20) {
+ color = (uint8_t) ((const uint8_t[]) { 0, 3, 5, 7 })[color];
+ } else {
+ color = (uint8_t) (color * 2);
+ }
+
+ if ((cpu->cga_color_select & 0x10) && pixel != 0) {
+ color |= 8;
+ }
+
+ }
+
+ return colors[color];
+
+}
+
+void video_native_cga_render (CPU8086 *cpu, uint32_t *pixels, unsigned mode) {
+
+ unsigned x, y;
+
+ for (y = 0; y < 200; y++) {
+
+ for (x = 0; x < (mode == 0x06 ? 640u : 320u); x++) {
+
+ uint32_t color = video_native_cga_pixel (cpu, x, y, mode);
+
+ unsigned output_x = mode == 0x06 ? x : x * 2;
+ unsigned width = mode == 0x06 ? 1 : 2;
+
+ unsigned i;
+
+ for (i = 0; i < width; i++) {
+
+ pixels[y * 2 * 640 + output_x + i] = color;
+ pixels[(y * 2 + 1) * 640 + output_x + i] = color;
+
+ }
+
+ }
+
+ }
+
+}
}
-int video_native_present (CPU8086 *cpu, int text_mode, int blink_enabled) {
+int video_native_present (CPU8086 *cpu, int mode, int blink_enabled) {
+
+ int text_mode = mode != 0x04 && mode != 0x05 && mode != 0x06 && mode != 0x13;
+ unsigned dirty_mask = mode == 0x13 ? 4u : 2u;
unsigned x, y;
DWORD now;
video_native_poll (cpu);
palette_changed = memcmp (video_palette, cpu->vga_dac_palette, sizeof (video_palette)) || video_mask != cpu->vga_dac_mask;
- if (!(mem_video_dirty & (text_mode ? 2u : 4u)) && (text_mode || !palette_changed) && video_last_text_mode == text_mode && (!text_mode || video_last_blink_enabled == blink_enabled)) {
+ if (!(mem_video_dirty & dirty_mask) && (text_mode || !palette_changed) && video_last_text_mode == mode && (!text_mode || video_last_blink_enabled == blink_enabled)) {
return 1;
}
if (text_mode) {
video_native_text_render (cpu, video_pixels, blink_enabled);
+ } else if (mode != 0x13) {
+ video_native_cga_render (cpu, video_pixels, (unsigned) mode);
} else {
for (y = 0; y < 200; y++) {
memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette));
video_mask = cpu->vga_dac_mask;
- mem_video_dirty &= ~(text_mode ? 2u : 4u);
+ mem_video_dirty &= ~dirty_mask;
- video_last_text_mode = text_mode;
+ video_last_text_mode = mode;
video_last_blink_enabled = blink_enabled;
InvalidateRect (video_window, NULL, FALSE);