2 * VGA hardware emulation
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
38 static IDirectDraw
*lpddraw
= NULL
;
39 static IDirectDrawSurface
*lpddsurf
;
40 static IDirectDrawPalette
*lpddpal
;
41 static DDSURFACEDESC sdesc
;
43 static BOOL vga_retrace_vertical
;
44 static BOOL vga_retrace_horizontal
;
47 * Size and location of VGA controller window to framebuffer.
49 * Note: We support only single window even though some
50 * controllers support two. This should not be changed unless
51 * there are programs that depend on having two windows.
53 #define VGA_WINDOW_SIZE (64 * 1024)
54 #define VGA_WINDOW_START ((char *)0xa0000)
57 * Size and location of CGA controller window to framebuffer.
59 #define CGA_WINDOW_SIZE (32 * 1024)
60 #define CGA_WINDOW_START ((char *)0xb8000)
63 * VGA controller memory is emulated using linear framebuffer.
64 * This frambuffer also acts as an interface
65 * between VGA controller emulation and DirectDraw.
67 * vga_fb_width: Display width in pixels. Can be modified when
68 * display mode is changed.
69 * vga_fb_height: Display height in pixels. Can be modified when
70 * display mode is changed.
71 * vga_fb_depth: Number of bits used to store single pixel color information.
72 * Each pixel uses (vga_fb_depth+7)/8 bytes because
73 * 1-16 color modes are mapped to 256 color mode.
74 * Can be modified when display mode is changed.
75 * vga_fb_pitch: How many bytes to add to pointer in order to move
76 * from one row to another. This is fixed in VGA modes,
77 * but can be modified in SVGA modes.
78 * vga_fb_offset: Offset added to framebuffer start address in order
79 * to find the display origin. Programs use this to do
80 * double buffering and to scroll display. The value can
81 * be modified in VGA and SVGA modes.
82 * vga_fb_size: How many bytes are allocated to framebuffer.
83 * VGA framebuffers are always larger than display size and
84 * SVGA framebuffers may also be.
85 * vga_fb_data: Pointer to framebuffer start.
86 * vga_fb_window: Offset of 64k window 0xa0000 in bytes from framebuffer start.
87 * This value is >= 0, if mode uses linear framebuffer and
88 * -1, if mode uses color planes. This value is fixed
89 * in all modes except 0x13 (256 color VGA) where
90 * 0 means normal mode and -1 means Mode-X (unchained mode).
92 static int vga_fb_width
;
93 static int vga_fb_height
;
94 static int vga_fb_depth
;
95 static int vga_fb_pitch
;
96 static int vga_fb_offset
;
97 static int vga_fb_size
= 0;
98 static char *vga_fb_data
= 0;
99 static int vga_fb_window
= 0;
100 static int vga_fb_window_size
;
101 static char *vga_fb_window_data
;
102 static PALETTEENTRY
*vga_fb_palette
;
103 static unsigned vga_fb_palette_index
;
104 static unsigned vga_fb_palette_size
;
105 static BOOL vga_fb_bright
;
106 static BOOL vga_fb_enabled
;
109 * VGA text mode data.
111 * vga_text_attr: Current active attribute.
112 * vga_text_old: Last data sent to console.
113 * This is used to optimize console updates.
114 * vga_text_width: Width of the text display in characters.
115 * vga_text_height: Height of the text display in characters.
116 * vga_text_x: Current cursor X-position. Starts from zero.
117 * vga_text_y: Current cursor Y-position. Starts from zero.
118 * vga_text_console: TRUE if stdout is console,
119 * FALSE if it is regular file.
121 static BYTE vga_text_attr
;
122 static char *vga_text_old
= NULL
;
123 static BYTE vga_text_width
;
124 static BYTE vga_text_height
;
125 static BYTE vga_text_x
;
126 static BYTE vga_text_y
;
127 static BOOL vga_text_console
;
130 * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
131 * indexed registers. These ports are used to select VGA controller
132 * subregister that can be written to or read from using ports 0x3c1,
133 * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
134 * stored in variables vga_index_*.
136 * Port 0x3c0 is special because it is both index and
137 * data-write register. Flip-flop vga_address_3c0 tells whether
138 * the port acts currently as an address register. Reading from port
139 * 0x3da resets the flip-flop to address mode.
141 static BYTE vga_index_3c0
;
142 static BYTE vga_index_3c4
;
143 static BYTE vga_index_3ce
;
144 static BYTE vga_index_3d4
;
145 static BOOL vga_address_3c0
= TRUE
;
148 * This mutex is used to protect VGA state during asynchronous
149 * screen updates (see VGA_Poll). It makes sure that VGA state changes
150 * are atomic and the user interface is protected from flicker and
153 * The mutex actually serializes VGA operations and the screen update.
154 * Which means that whenever VGA_Poll occurs, application stalls if it
155 * tries to modify VGA state. This is not how real VGA adapters work,
156 * but it makes timing and correctness issues much easier to deal with.
158 static CRITICAL_SECTION vga_lock
;
159 static CRITICAL_SECTION_DEBUG critsect_debug
=
162 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
163 0, 0, { (DWORD_PTR
)(__FILE__
": vga_lock") }
165 static CRITICAL_SECTION vga_lock
= { &critsect_debug
, -1, 0, 0, 0, 0 };
167 typedef HRESULT (WINAPI
*DirectDrawCreateProc
)(LPGUID
,LPDIRECTDRAW
*,LPUNKNOWN
);
168 static DirectDrawCreateProc pDirectDrawCreate
;
170 static void CALLBACK
VGA_Poll( LPVOID arg
, DWORD low
, DWORD high
);
172 static HWND vga_hwnd
= NULL
;
177 static PALETTEENTRY cga_palette1
[] = {
178 {0x00, 0x00, 0x00}, /* 0 - Black */
179 {0x00, 0xAA, 0xAA}, /* 1 - Cyan */
180 {0xAA, 0x00, 0xAA}, /* 2 - Magenta */
181 {0xAA, 0xAA, 0xAA} /* 3 - White */
185 * CGA palette 1 in the bright variant
186 * intensities, when signalled to do so
189 static PALETTEENTRY cga_palette1_bright
[] = {
190 {0x00, 0x00, 0x00}, /* 0 - Black */
191 {0x55, 0xFF, 0xFF}, /* 1 - Light cyan */
192 {0xFF, 0x55, 0xFF}, /* 2 - Light magenta */
193 {0xFF, 0xFF, 0xFF}, /* 3 - Bright White */
199 static PALETTEENTRY cga_palette2
[] = {
200 {0x00, 0x00, 0x00}, /* 0 - Black */
201 {0x00, 0xAA, 0x00}, /* 1 - Green */
202 {0xAA, 0x00, 0x00}, /* 2 - Red */
203 {0xAA, 0x55, 0x00} /* 3 - Brown */
207 * CGA palette 2 in the bright variant
208 * intensities, when signalled to do so
211 static PALETTEENTRY cga_palette2_bright
[] = {
212 {0x00, 0x00, 0x00}, /* 0 - Black */
213 {0x55, 0xFF, 0x55}, /* 1 - Light green */
214 {0xFF, 0x55, 0x55}, /* 2 - Light red */
215 {0xFF, 0xFF, 0x55}, /* 3 - Yellow */
219 * VGA Palette Registers, in actual 18 bit color
220 * port 3C0H - 6 bit rgbRGB format
222 * 16 color accesses will use these pointers and insert
223 * entries from the 64-color palette (mode 18) into the default
224 * palette. --Robert 'Admiral' Coeyman
227 static char vga_16_palette
[17]={
228 0x00, /* 0 - Black */
230 0x02, /* 2 - Green */
233 0x05, /* 5 - Magenta */
234 0x14, /* 6 - Brown */
235 0x07, /* 7 - White (Light gray) */
236 0x38, /* 8 - Dark gray */
237 0x39, /* 9 - Light blue */
238 0x3a, /* A - Light green */
239 0x3b, /* B - Light cyan */
240 0x3c, /* C - Light red */
241 0x3d, /* D - Light magenta */
242 0x3e, /* E - Yellow */
243 0x3f, /* F - Bright White (White) */
244 0x00 /* Border Color */
248 * Mode 19 Default Color Register Setting
249 * DAC palette registers, converted from actual 18 bit color to 24.
251 static PALETTEENTRY vga_def_palette
[256]={
253 /* 16 colors in IRGB values */
254 {0x00, 0x00, 0x00}, /* 0 (0) - Black */
255 {0x00, 0x00, 0xAA}, /* 1 (1) - Blue */
256 {0x00, 0xAA, 0x00}, /* 2 (2) - Green */
257 {0x00, 0xAA, 0xAA}, /* 3 (3) - Cyan */
258 {0xAA, 0x00, 0x00}, /* 4 (4) - Red */
259 {0xAA, 0x00, 0xAA}, /* 5 (5) - Magenta */
260 {0xAA, 0x55, 0x00}, /* 6 (6) - Brown */
261 {0xAA, 0xAA, 0xAA}, /* 7 (7) - White (Light gray) */
262 {0x55, 0x55, 0x55}, /* 8 (8) - Dark gray */
263 {0x55, 0x55, 0xFF}, /* 9 (9) - Light blue */
264 {0x55, 0xFF, 0x55}, /* 10 (A) - Light green */
265 {0x55, 0xFF, 0xFF}, /* 11 (B) - Light cyan */
266 {0xFF, 0x55, 0x55}, /* 12 (C) - Light red */
267 {0xFF, 0x55, 0xFF}, /* 13 (D) - Light magenta */
268 {0xFF, 0xFF, 0x55}, /* 14 (E) - Yellow */
269 {0xFF, 0xFF, 0xFF}, /* 15 (F) - Bright White (White) */
270 /* 16 shades of gray */
271 {0x00, 0x00, 0x00}, /* 16 (10) */
272 {0x10, 0x10, 0x10}, /* 17 (11) */
273 {0x20, 0x20, 0x20}, /* 18 (12) */
274 {0x35, 0x35, 0x35}, /* 19 (13) */
275 {0x45, 0x45, 0x45}, /* 20 (14) */
276 {0x55, 0x55, 0x55}, /* 21 (15) */
277 {0x65, 0x65, 0x65}, /* 22 (16) */
278 {0x75, 0x75, 0x75}, /* 23 (17) */
279 {0x8A, 0x8A, 0x8A}, /* 24 (18) */
280 {0x9A, 0x9A, 0x9A}, /* 25 (19) */
281 {0xAA, 0xAA, 0xAA}, /* 26 (1A) */
282 {0xBA, 0xBA, 0xBA}, /* 27 (1B) */
283 {0xCA, 0xCA, 0xCA}, /* 28 (1C) */
284 {0xDF, 0xDF, 0xDF}, /* 29 (1D) */
285 {0xEF, 0xEF, 0xEF}, /* 30 (1E) */
286 {0xFF, 0xFF, 0xFF}, /* 31 (1F) */
287 /* High Intensity group - 72 colors in 1/3 saturation groups (20H-37H high) */
288 {0x00, 0x00, 0xFF}, /* 32 (20) */
289 {0x41, 0x00, 0xFF}, /* 33 (21) */
290 {0x82, 0x00, 0xFF}, /* 34 (22) */
291 {0xBE, 0x00, 0xFF}, /* 35 (23) */
292 {0xFF, 0x00, 0xFF}, /* 36 (24) */
293 {0xFF, 0x00, 0xBE}, /* 37 (25) */
294 {0xFF, 0x00, 0x82}, /* 38 (26) */
295 {0xFF, 0x00, 0x41}, /* 39 (27) */
296 {0xFF, 0x00, 0x00}, /* 40 (28) */
297 {0xFF, 0x41, 0x00}, /* 41 (29) */
298 {0xFF, 0x82, 0x00}, /* 42 (2A) */
299 {0xFF, 0xBE, 0x00}, /* 43 (2B) */
300 {0xFF, 0xFF, 0x00}, /* 44 (2C) */
301 {0xBE, 0xFF, 0x00}, /* 45 (2D) */
302 {0x82, 0xFF, 0x00}, /* 46 (2E) */
303 {0x41, 0xFF, 0x00}, /* 47 (2F) */
304 {0x00, 0xFF, 0x00}, /* 48 (30) */
305 {0x00, 0xFF, 0x41}, /* 49 (31) */
306 {0x00, 0xFF, 0x82}, /* 50 (32) */
307 {0x00, 0xFF, 0xBE}, /* 51 (33) */
308 {0x00, 0xFF, 0xFF}, /* 52 (34) */
309 {0x00, 0xBE, 0xFF}, /* 53 (35) */
310 {0x00, 0x82, 0xFF}, /* 54 (36) */
311 {0x00, 0x41, 0xFF}, /* 55 (37) */
312 /* High Intensity group - 72 colors in 2/3 saturation groups (38H-4FH moderate) */
313 {0x82, 0x82, 0xFF}, /* 56 (38) */
314 {0x9E, 0x82, 0xFF}, /* 57 (39) */
315 {0xBE, 0x82, 0xFF}, /* 58 (3A) */
316 {0xDF, 0x82, 0xFF}, /* 59 (3B) */
317 {0xFF, 0x82, 0xFF}, /* 60 (3C) */
318 {0xFF, 0x82, 0xDF}, /* 61 (3D) */
319 {0xFF, 0x82, 0xBE}, /* 62 (3E) */
320 {0xFF, 0x82, 0x9E}, /* 63 (3F) */
321 {0xFF, 0x82, 0x82}, /* 64 (40) */
322 {0xFF, 0x9E, 0x82}, /* 65 (41) */
323 {0xFF, 0xBE, 0x82}, /* 66 (42) */
324 {0xFF, 0xDF, 0x82}, /* 67 (43) */
325 {0xFF, 0xFF, 0x82}, /* 68 (44) */
326 {0xDF, 0xFF, 0x82}, /* 69 (45) */
327 {0xBE, 0xFF, 0x82}, /* 70 (46) */
328 {0x9E, 0xFF, 0x82}, /* 71 (47) */
329 {0x82, 0xFF, 0x82}, /* 72 (48) */
330 {0x82, 0xFF, 0x9E}, /* 73 (49) */
331 {0x82, 0xFF, 0xBE}, /* 74 (4A) */
332 {0x82, 0xFF, 0xDF}, /* 75 (4B) */
333 {0x82, 0xFF, 0xFF}, /* 76 (4C) */
334 {0x82, 0xDF, 0xFF}, /* 77 (4D) */
335 {0x82, 0xBE, 0xFF}, /* 78 (4E) */
336 {0x82, 0x9E, 0xFF}, /* 79 (4F) */
337 /* High Intensity group - 72 colors in 3/3 saturation groups (50H-67H low) */
338 {0xBA, 0xBA, 0xFF}, /* 80 (50) */
339 {0xCA, 0xBA, 0xFF}, /* 81 (51) */
340 {0xDF, 0xBA, 0xFF}, /* 82 (52) */
341 {0xEF, 0xBA, 0xFF}, /* 83 (53) */
342 {0xFF, 0xBA, 0xFF}, /* 84 (54) */
343 {0xFF, 0xBA, 0xEF}, /* 85 (55) */
344 {0xFF, 0xBA, 0xDF}, /* 86 (56) */
345 {0xFF, 0xBA, 0xCA}, /* 87 (57) */
346 {0xFF, 0xBA, 0xBA}, /* 88 (58) */
347 {0xFF, 0xCA, 0xBA}, /* 89 (59) */
348 {0xFF, 0xDF, 0xBA}, /* 90 (5A) */
349 {0xFF, 0xEF, 0xBA}, /* 91 (5B) */
350 {0xFF, 0xFF, 0xBA}, /* 92 (5C) */
351 {0xEF, 0xFF, 0xBA}, /* 93 (5D) */
352 {0xDF, 0xFF, 0xBA}, /* 94 (5E) */
353 {0xCA, 0xFF, 0xBA}, /* 95 (5F) */
354 {0xBA, 0xFF, 0xBA}, /* 96 (60) */
355 {0xBA, 0xFF, 0xCA}, /* 97 (61) */
356 {0xBA, 0xFF, 0xDF}, /* 98 (62) */
357 {0xBA, 0xFF, 0xEF}, /* 99 (63) */
358 {0xBA, 0xFF, 0xFF}, /* 100 (64) */
359 {0xBA, 0xEF, 0xFF}, /* 101 (65) */
360 {0xBA, 0xDF, 0xFF}, /* 102 (66) */
361 {0xBA, 0xCA, 0xFF}, /* 103 (67) */
362 /* Medium Intensity group - 72 colors in 1/3 saturation groups (68H-7FH high) */
363 {0x00, 0x00, 0x71}, /* 104 (68) */
364 {0x1C, 0x00, 0x71}, /* 105 (69) */
365 {0x39, 0x00, 0x71}, /* 106 (6A) */
366 {0x55, 0x00, 0x71}, /* 107 (6B) */
367 {0x71, 0x00, 0x71}, /* 108 (6C) */
368 {0x71, 0x00, 0x55}, /* 109 (6D) */
369 {0x71, 0x00, 0x39}, /* 110 (6E) */
370 {0x71, 0x00, 0x1C}, /* 111 (6F) */
371 {0x71, 0x00, 0x00}, /* 112 (70) */
372 {0x71, 0x1C, 0x00}, /* 113 (71) */
373 {0x71, 0x39, 0x00}, /* 114 (72) */
374 {0x71, 0x55, 0x00}, /* 115 (73) */
375 {0x71, 0x71, 0x00}, /* 116 (74) */
376 {0x55, 0x71, 0x00}, /* 117 (75) */
377 {0x39, 0x71, 0x00}, /* 118 (76) */
378 {0x1C, 0x71, 0x00}, /* 119 (77) */
379 {0x00, 0x71, 0x00}, /* 120 (78) */
380 {0x00, 0x71, 0x1C}, /* 121 (79) */
381 {0x00, 0x71, 0x39}, /* 122 (7A) */
382 {0x00, 0x71, 0x55}, /* 123 (7B) */
383 {0x00, 0x71, 0x71}, /* 124 (7C) */
384 {0x00, 0x55, 0x71}, /* 125 (7D) */
385 {0x00, 0x39, 0x71}, /* 126 (7E) */
386 {0x00, 0x1C, 0x71}, /* 127 (7F) */
387 /* Medium Intensity group - 72 colors in 2/3 saturation groups (80H-97H moderate) */
388 {0x39, 0x39, 0x71}, /* 128 (80) */
389 {0x45, 0x39, 0x71}, /* 129 (81) */
390 {0x55, 0x39, 0x71}, /* 130 (82) */
391 {0x61, 0x39, 0x71}, /* 131 (83) */
392 {0x71, 0x39, 0x71}, /* 132 (84) */
393 {0x71, 0x39, 0x61}, /* 133 (85) */
394 {0x71, 0x39, 0x55}, /* 134 (86) */
395 {0x71, 0x39, 0x45}, /* 135 (87) */
396 {0x71, 0x39, 0x39}, /* 136 (88) */
397 {0x71, 0x45, 0x39}, /* 137 (89) */
398 {0x71, 0x55, 0x39}, /* 138 (8A) */
399 {0x71, 0x61, 0x39}, /* 139 (8B) */
400 {0x71, 0x71, 0x39}, /* 140 (8C) */
401 {0x61, 0x71, 0x39}, /* 141 (8D) */
402 {0x55, 0x71, 0x39}, /* 142 (8E) */
403 {0x45, 0x71, 0x39}, /* 143 (8F) */
404 {0x39, 0x71, 0x39}, /* 144 (90) */
405 {0x39, 0x71, 0x45}, /* 145 (91) */
406 {0x39, 0x71, 0x55}, /* 146 (92) */
407 {0x39, 0x71, 0x61}, /* 147 (93) */
408 {0x39, 0x71, 0x71}, /* 148 (94) */
409 {0x39, 0x61, 0x71}, /* 149 (95) */
410 {0x39, 0x55, 0x71}, /* 150 (96) */
411 {0x39, 0x45, 0x71}, /* 151 (97) */
412 /* Medium Intensity group - 72 colors in 3/3 saturation groups (98H-AFH low) */
413 {0x51, 0x51, 0x71}, /* 152 (98) */
414 {0x59, 0x51, 0x71}, /* 153 (99) */
415 {0x61, 0x51, 0x71}, /* 154 (9A) */
416 {0x69, 0x51, 0x71}, /* 155 (9B) */
417 {0x71, 0x51, 0x71}, /* 156 (9C) */
418 {0x71, 0x51, 0x69}, /* 157 (9D) */
419 {0x71, 0x51, 0x61}, /* 158 (9E) */
420 {0x71, 0x51, 0x59}, /* 159 (9F) */
421 {0x71, 0x51, 0x51}, /* 160 (A0) */
422 {0x71, 0x59, 0x51}, /* 161 (A1) */
423 {0x71, 0x61, 0x51}, /* 162 (A2) */
424 {0x71, 0x69, 0x51}, /* 163 (A3) */
425 {0x71, 0x71, 0x51}, /* 164 (A4) */
426 {0x69, 0x71, 0x51}, /* 165 (A5) */
427 {0x61, 0x71, 0x51}, /* 166 (A6) */
428 {0x59, 0x71, 0x51}, /* 167 (A7) */
429 {0x51, 0x71, 0x51}, /* 168 (A8) */
430 {0x51, 0x71, 0x59}, /* 169 (A9) */
431 {0x51, 0x71, 0x61}, /* 170 (AA) */
432 {0x51, 0x71, 0x69}, /* 171 (AB) */
433 {0x51, 0x71, 0x71}, /* 172 (AC) */
434 {0x51, 0x69, 0x71}, /* 173 (AD) */
435 {0x51, 0x61, 0x71}, /* 174 (AE) */
436 {0x51, 0x59, 0x71}, /* 175 (AF) */
437 /* Low Intensity group - 72 colors in 1/3 saturation groups (B0H-C7H high) */
438 {0x00, 0x00, 0x41}, /* 176 (B0) */
439 {0x10, 0x00, 0x41}, /* 177 (B1) */
440 {0x20, 0x00, 0x41}, /* 178 (B2) */
441 {0x31, 0x00, 0x41}, /* 179 (B3) */
442 {0x41, 0x00, 0x41}, /* 180 (B4) */
443 {0x41, 0x00, 0x31}, /* 181 (B5) */
444 {0x41, 0x00, 0x20}, /* 182 (B6) */
445 {0x41, 0x00, 0x10}, /* 183 (B7) */
446 {0x41, 0x00, 0x00}, /* 184 (B8) */
447 {0x41, 0x10, 0x00}, /* 185 (B9) */
448 {0x41, 0x20, 0x00}, /* 186 (BA) */
449 {0x41, 0x31, 0x00}, /* 187 (BB) */
450 {0x41, 0x41, 0x00}, /* 188 (BC) */
451 {0x31, 0x41, 0x00}, /* 189 (BD) */
452 {0x20, 0x41, 0x00}, /* 190 (BE) */
453 {0x10, 0x41, 0x00}, /* 191 (BF) */
454 {0x00, 0x41, 0x00}, /* 192 (C0) */
455 {0x00, 0x41, 0x10}, /* 193 (C1) */
456 {0x00, 0x41, 0x20}, /* 194 (C2) */
457 {0x00, 0x41, 0x31}, /* 195 (C3) */
458 {0x00, 0x41, 0x41}, /* 196 (C4) */
459 {0x00, 0x31, 0x41}, /* 197 (C5) */
460 {0x00, 0x20, 0x41}, /* 198 (C6) */
461 {0x00, 0x10, 0x41}, /* 199 (C7) */
462 /* Low Intensity group - 72 colors in 2/3 saturation groups (C8H-DFH moderate) */
463 {0x20, 0x20, 0x41}, /* 200 (C8) */
464 {0x28, 0x20, 0x41}, /* 201 (C9) */
465 {0x31, 0x20, 0x41}, /* 202 (CA) */
466 {0x39, 0x20, 0x41}, /* 203 (CB) */
467 {0x41, 0x20, 0x41}, /* 204 (CC) */
468 {0x41, 0x20, 0x39}, /* 205 (CD) */
469 {0x41, 0x20, 0x31}, /* 206 (CE) */
470 {0x41, 0x20, 0x28}, /* 207 (CF) */
471 {0x41, 0x20, 0x20}, /* 208 (D0) */
472 {0x41, 0x28, 0x20}, /* 209 (D1) */
473 {0x41, 0x31, 0x20}, /* 210 (D2) */
474 {0x41, 0x39, 0x20}, /* 211 (D3) */
475 {0x41, 0x41, 0x20}, /* 212 (D4) */
476 {0x39, 0x41, 0x20}, /* 213 (D5) */
477 {0x31, 0x41, 0x20}, /* 214 (D6) */
478 {0x28, 0x41, 0x20}, /* 215 (D7) */
479 {0x20, 0x41, 0x20}, /* 216 (D8) */
480 {0x20, 0x41, 0x28}, /* 217 (D9) */
481 {0x20, 0x41, 0x31}, /* 218 (DA) */
482 {0x20, 0x41, 0x39}, /* 219 (DB) */
483 {0x20, 0x41, 0x41}, /* 220 (DC) */
484 {0x20, 0x39, 0x41}, /* 221 (DD) */
485 {0x20, 0x31, 0x41}, /* 222 (DE) */
486 {0x20, 0x28, 0x41}, /* 223 (DF) */
487 /* Low Intensity group - 72 colors in 3/3 saturation groups (E0H-F7H low) */
488 {0x2D, 0x2D, 0x41}, /* 223 (E0) */
489 {0x31, 0x2D, 0x41}, /* 224 (E1) */
490 {0x35, 0x2D, 0x41}, /* 225 (E2) */
491 {0x3D, 0x2D, 0x41}, /* 226 (E3) */
492 {0x41, 0x2D, 0x41}, /* 227 (E4) */
493 {0x41, 0x2D, 0x3D}, /* 228 (E5) */
494 {0x41, 0x2D, 0x35}, /* 229 (E6) */
495 {0x41, 0x2D, 0x31}, /* 230 (E7) */
496 {0x41, 0x2D, 0x2D}, /* 231 (E8) */
497 {0x41, 0x31, 0x2D}, /* 232 (E9) */
498 {0x41, 0x35, 0x2D}, /* 233 (EA) */
499 {0x41, 0x3D, 0x2D}, /* 234 (EB) */
500 {0x41, 0x41, 0x2D}, /* 235 (EC) */
501 {0x3D, 0x41, 0x2D}, /* 236 (ED) */
502 {0x35, 0x41, 0x2D}, /* 237 (EE) */
503 {0x31, 0x41, 0x2D}, /* 238 (EF) */
504 {0x2D, 0x41, 0x2D}, /* 239 (F0) */
505 {0x2D, 0x41, 0x31}, /* 240 (F1) */
506 {0x2D, 0x41, 0x35}, /* 241 (F2) */
507 {0x2D, 0x41, 0x3D}, /* 242 (F3) */
508 {0x2D, 0x41, 0x41}, /* 243 (F4) */
509 {0x2D, 0x3D, 0x41}, /* 244 (F5) */
510 {0x2D, 0x35, 0x41}, /* 245 (F6) */
511 {0x2D, 0x31, 0x41}, /* 246 (F7) */
512 /* Fill up remainder of palettes with black */
517 * Mode 18 Default Color Register Setting
518 * DAC palette registers, converted from actual 18 bit color to 24.
520 * This palette is the dos default, converted from 18 bit color to 24.
521 * It contains only 64 entries of colors--all others are zeros.
522 * --Robert 'Admiral' Coeyman
524 static PALETTEENTRY vga_def64_palette
[256]={
526 {0x00, 0x00, 0x00}, /* 0x00 Black */
527 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
528 {0x00, 0xaa, 0x00}, /* 0x02 Green */
529 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
530 {0xaa, 0x00, 0x00}, /* 0x04 Red */
531 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
532 {0xaa, 0xaa, 0x00}, /* 0x06 */
533 {0xaa, 0xaa, 0xaa}, /* 0x07 White (Light Gray) */
534 {0x00, 0x00, 0x55}, /* 0x08 */
535 {0x00, 0x00, 0xff}, /* 0x09 */
536 {0x00, 0xaa, 0x55}, /* 0x0a */
537 {0x00, 0xaa, 0xff}, /* 0x0b */
538 {0xaa, 0x00, 0x55}, /* 0x0c */
539 {0xaa, 0x00, 0xff}, /* 0x0d */
540 {0xaa, 0xaa, 0x55}, /* 0x0e */
541 {0xaa, 0xaa, 0xff}, /* 0x0f */
542 {0x00, 0x55, 0x00}, /* 0x10 */
543 {0x00, 0x55, 0xaa}, /* 0x11 */
544 {0x00, 0xff, 0x00}, /* 0x12 */
545 {0x00, 0xff, 0xaa}, /* 0x13 */
546 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
547 {0xaa, 0x55, 0xaa}, /* 0x15 */
548 {0xaa, 0xff, 0x00}, /* 0x16 */
549 {0xaa, 0xff, 0xaa}, /* 0x17 */
550 {0x00, 0x55, 0x55}, /* 0x18 */
551 {0x00, 0x55, 0xff}, /* 0x19 */
552 {0x00, 0xff, 0x55}, /* 0x1a */
553 {0x00, 0xff, 0xff}, /* 0x1b */
554 {0xaa, 0x55, 0x55}, /* 0x1c */
555 {0xaa, 0x55, 0xff}, /* 0x1d */
556 {0xaa, 0xff, 0x55}, /* 0x1e */
557 {0xaa, 0xff, 0xff}, /* 0x1f */
558 {0x55, 0x00, 0x00}, /* 0x20 */
559 {0x55, 0x00, 0xaa}, /* 0x21 */
560 {0x55, 0xaa, 0x00}, /* 0x22 */
561 {0x55, 0xaa, 0xaa}, /* 0x23 */
562 {0xff, 0x00, 0x00}, /* 0x24 */
563 {0xff, 0x00, 0xaa}, /* 0x25 */
564 {0xff, 0xaa, 0x00}, /* 0x26 */
565 {0xff, 0xaa, 0xaa}, /* 0x27 */
566 {0x55, 0x00, 0x55}, /* 0x28 */
567 {0x55, 0x00, 0xff}, /* 0x29 */
568 {0x55, 0xaa, 0x55}, /* 0x2a */
569 {0x55, 0xaa, 0xff}, /* 0x2b */
570 {0xff, 0x00, 0x55}, /* 0x2c */
571 {0xff, 0x00, 0xff}, /* 0x2d */
572 {0xff, 0xaa, 0x55}, /* 0x2e */
573 {0xff, 0xaa, 0xff}, /* 0x2f */
574 {0x55, 0x55, 0x00}, /* 0x30 */
575 {0x55, 0x55, 0xaa}, /* 0x31 */
576 {0x55, 0xff, 0x00}, /* 0x32 */
577 {0x55, 0xff, 0xaa}, /* 0x33 */
578 {0xff, 0x55, 0x00}, /* 0x34 */
579 {0xff, 0x55, 0xaa}, /* 0x35 */
580 {0xff, 0xff, 0x00}, /* 0x36 */
581 {0xff, 0xff, 0xaa}, /* 0x37 */
582 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
583 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
584 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
585 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
586 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
587 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
588 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
589 {0xff, 0xff, 0xff}, /* 0x3f White */
590 {0,0,0} /* The next 192 entries are all zeros */
593 static HANDLE VGA_timer
;
594 static HANDLE VGA_timer_thread
;
596 /* set the timer rate; called in the polling thread context */
597 static void CALLBACK
set_timer_rate( ULONG_PTR arg
)
601 when
.u
.LowPart
= when
.u
.HighPart
= 0;
602 SetWaitableTimer( VGA_timer
, &when
, arg
, VGA_Poll
, 0, FALSE
);
605 static DWORD CALLBACK
VGA_TimerThread( void *dummy
)
607 for (;;) SleepEx( INFINITE
, TRUE
);
611 static void VGA_DeinstallTimer(void)
613 if (VGA_timer_thread
)
616 * Make sure the update thread is not holding
617 * system resources when we kill it.
619 * Now, we only need to worry about update thread
620 * getting terminated while in EnterCriticalSection
621 * or WaitForMultipleObjectsEx.
623 * FIXME: Is this a problem?
625 EnterCriticalSection(&vga_lock
);
627 CancelWaitableTimer( VGA_timer
);
628 CloseHandle( VGA_timer
);
629 TerminateThread( VGA_timer_thread
, 0 );
630 CloseHandle( VGA_timer_thread
);
631 VGA_timer_thread
= 0;
633 LeaveCriticalSection(&vga_lock
);
636 * Synchronize display. This makes sure that
637 * changes to display become visible even if program
638 * terminates before update thread had time to run.
644 static void VGA_InstallTimer(unsigned Rate
)
646 if (!VGA_timer_thread
)
648 VGA_timer
= CreateWaitableTimerA( NULL
, FALSE
, NULL
);
649 VGA_timer_thread
= CreateThread( NULL
, 0, VGA_TimerThread
, NULL
, 0, NULL
);
651 QueueUserAPC( set_timer_rate
, VGA_timer_thread
, (ULONG_PTR
)Rate
);
654 static BOOL
VGA_IsTimerRunning(void)
656 return VGA_timer_thread
? TRUE
: FALSE
;
659 static HANDLE
VGA_AlphaConsole(void)
661 /* this assumes that no Win32 redirection has taken place, but then again,
662 * only 16-bit apps are likely to use this part of Wine... */
663 return GetStdHandle(STD_OUTPUT_HANDLE
);
666 static char*VGA_AlphaBuffer(void)
668 return (char *)0xb8000;
671 /*** GRAPHICS MODE ***/
674 unsigned Xres
, Yres
, Depth
;
679 /**********************************************************************
682 * Copy VGA window into framebuffer (if argument is TRUE) or
683 * part of framebuffer into VGA window (if argument is FALSE).
685 static void VGA_SyncWindow( BOOL target_is_fb
)
687 int size
= vga_fb_window_size
;
689 /* Window does not overlap framebuffer. */
690 if (vga_fb_window
>= vga_fb_size
)
693 /* Check if window overlaps framebuffer only partially. */
694 if (vga_fb_size
- vga_fb_window
< vga_fb_window_size
)
695 size
= vga_fb_size
- vga_fb_window
;
698 memmove( vga_fb_data
+ vga_fb_window
, vga_fb_window_data
, size
);
700 memmove( vga_fb_window_data
, vga_fb_data
+ vga_fb_window
, size
);
704 static void WINAPI
VGA_DoExit(ULONG_PTR arg
)
706 VGA_DeinstallTimer();
707 IDirectDrawSurface_SetPalette(lpddsurf
,NULL
);
708 IDirectDrawSurface_Release(lpddsurf
);
710 IDirectDrawPalette_Release(lpddpal
);
712 IDirectDraw_Release(lpddraw
);
716 static void WINAPI
VGA_DoSetMode(ULONG_PTR arg
)
719 ModeSet
*par
= (ModeSet
*)arg
;
722 if (lpddraw
) VGA_DoExit(0);
724 if (!pDirectDrawCreate
)
726 HMODULE hmod
= LoadLibraryA( "ddraw.dll" );
727 if (hmod
) pDirectDrawCreate
= (DirectDrawCreateProc
)GetProcAddress( hmod
, "DirectDrawCreate" );
728 if (!pDirectDrawCreate
) {
729 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
733 res
= pDirectDrawCreate(NULL
,&lpddraw
,NULL
);
735 ERR("DirectDraw is not available (res = 0x%x)\n",res
);
739 vga_hwnd
= CreateWindowExA(0,"STATIC","WINEDOS VGA",
740 WS_POPUP
|WS_VISIBLE
|SS_NOTIFY
,0,0,
741 par
->Xres
,par
->Yres
,0,0,0,NULL
);
743 ERR("Failed to create user window.\n");
744 IDirectDraw_Release(lpddraw
);
750 SetWindowPos(vga_hwnd
,0,0,0,par
->Xres
,par
->Yres
,SWP_NOMOVE
|SWP_NOZORDER
);
752 res
=IDirectDraw_SetCooperativeLevel(lpddraw
,vga_hwnd
,DDSCL_FULLSCREEN
|DDSCL_EXCLUSIVE
);
754 ERR("Could not set cooperative level to exclusive (0x%x)\n",res
);
757 res
=IDirectDraw_SetDisplayMode(lpddraw
,par
->Xres
,par
->Yres
,par
->Depth
);
759 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = 0x%x!\n",par
->Xres
,par
->Yres
,par
->Depth
,res
);
760 IDirectDraw_Release(lpddraw
);
765 res
=IDirectDraw_CreatePalette(lpddraw
,DDPCAPS_8BIT
,NULL
,&lpddpal
,NULL
);
767 ERR("Could not create palette (res = 0x%x)\n",res
);
768 IDirectDraw_Release(lpddraw
);
773 res
=IDirectDrawPalette_SetEntries(lpddpal
,0,0,vga_fb_palette_size
,vga_fb_palette
);
775 ERR("Could not set default palette entries (res = 0x%x)\n", res
);
778 memset(&sdesc
,0,sizeof(sdesc
));
779 sdesc
.dwSize
=sizeof(sdesc
);
780 sdesc
.dwFlags
= DDSD_CAPS
;
781 sdesc
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
782 res
=IDirectDraw_CreateSurface(lpddraw
,&sdesc
,&lpddsurf
,NULL
);
783 if (res
!= S_OK
|| !lpddsurf
) {
784 ERR("DirectDraw surface is not available\n");
785 IDirectDraw_Release(lpddraw
);
789 IDirectDrawSurface_SetPalette(lpddsurf
,lpddpal
);
790 vga_retrace_vertical
= vga_retrace_horizontal
= FALSE
;
791 /* poll every 20ms (50fps should provide adequate responsiveness) */
792 VGA_InstallTimer(20);
798 int VGA_SetMode(unsigned Xres
,unsigned Yres
,unsigned Depth
)
804 vga_fb_height
= Yres
;
805 vga_fb_depth
= Depth
;
807 vga_fb_pitch
= Xres
* ((Depth
+ 7) / 8);
809 newSize
= Xres
* Yres
* ((Depth
+ 7) / 8);
810 if(newSize
< 256 * 1024)
811 newSize
= 256 * 1024;
813 if(vga_fb_size
< newSize
) {
814 HeapFree(GetProcessHeap(), 0, vga_fb_data
);
815 vga_fb_data
= HeapAlloc(GetProcessHeap(), 0, newSize
);
816 vga_fb_size
= newSize
;
819 if(Xres
>= 640 || Yres
>= 480) {
828 if(vga_fb_depth
>= 8)
830 vga_fb_window_data
= VGA_WINDOW_START
;
831 vga_fb_window_size
= VGA_WINDOW_SIZE
;
832 vga_fb_palette
= vga_def_palette
;
833 vga_fb_palette_size
= 256;
837 vga_fb_window_data
= CGA_WINDOW_START
;
838 vga_fb_window_size
= CGA_WINDOW_SIZE
;
841 /* Select default 2 bit CGA palette */
842 vga_fb_palette
= cga_palette1
;
843 vga_fb_palette_size
= 4;
847 /* Top of VGA palette is same as 4 bit CGA palette */
848 vga_fb_palette
= vga_def_palette
;
849 vga_fb_palette_size
= 16;
852 vga_fb_palette_index
= 0;
856 /* Clean the HW buffer */
857 memset(vga_fb_window_data
, 0x00, vga_fb_window_size
);
859 /* Reset window start */
860 VGA_SetWindowStart(0);
862 par
.Depth
= (Depth
< 8) ? 8 : Depth
;
864 MZ_RunInThread(VGA_DoSetMode
, (ULONG_PTR
)&par
);
868 int VGA_GetMode(unsigned*Height
,unsigned*Width
,unsigned*Depth
)
870 if (!lpddraw
) return 1;
871 if (!lpddsurf
) return 1;
872 if (Height
) *Height
=sdesc
.dwHeight
;
873 if (Width
) *Width
=sdesc
.dwWidth
;
874 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
878 static void VGA_Exit(void)
880 if (lpddraw
) MZ_RunInThread(VGA_DoExit
, 0);
883 void VGA_SetPalette(PALETTEENTRY
*pal
,int start
,int len
)
885 if (!lpddraw
) return;
886 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
889 /* set a single [char wide] color in 16 color mode. */
890 void VGA_SetColor16(int reg
,int color
)
894 if (!lpddraw
) return;
895 pal
= &vga_def64_palette
[color
];
896 IDirectDrawPalette_SetEntries(lpddpal
,0,reg
,1,pal
);
897 vga_16_palette
[reg
]=(char)color
;
900 /* Get a single [char wide] color in 16 color mode. */
901 char VGA_GetColor16(int reg
)
904 if (!lpddraw
) return 0;
905 return vga_16_palette
[reg
];
908 /* set all 17 [char wide] colors at once in 16 color mode. */
909 void VGA_Set16Palette(char *Table
)
914 if (!lpddraw
) return; /* return if we're in text only mode */
915 memcpy( Table
, vga_16_palette
, 17 ); /* copy the entries into the table */
917 for (c
=0; c
<17; c
++) { /* 17 entries */
918 pal
= &vga_def64_palette
[(int)vga_16_palette
[c
]]; /* get color */
919 IDirectDrawPalette_SetEntries(lpddpal
,0,c
,1,pal
); /* set entry */
920 TRACE("Palette register %d set to %d\n",c
,(int)vga_16_palette
[c
]);
921 } /* end of the counting loop */
924 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
925 void VGA_Get16Palette(char *Table
)
928 if (!lpddraw
) return; /* return if we're in text only mode */
929 memcpy( vga_16_palette
, Table
, 17 ); /* copy the entries into the table */
932 void VGA_SetQuadPalette(RGBQUAD
*color
,int start
,int len
)
934 PALETTEENTRY pal
[256];
937 if (!lpddraw
) return;
938 for (c
=0; c
<len
; c
++) {
939 pal
[c
].peRed
=color
[c
].rgbRed
;
940 pal
[c
].peGreen
=color
[c
].rgbGreen
;
941 pal
[c
].peBlue
=color
[c
].rgbBlue
;
944 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
947 static LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
949 if (!lpddraw
) return NULL
;
950 if (!lpddsurf
) return NULL
;
951 if (IDirectDrawSurface_Lock(lpddsurf
,NULL
,&sdesc
,0,0) != S_OK
) {
952 ERR("could not lock surface!\n");
955 if (Pitch
) *Pitch
=sdesc
.u1
.lPitch
;
956 if (Height
) *Height
=sdesc
.dwHeight
;
957 if (Width
) *Width
=sdesc
.dwWidth
;
958 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
959 return sdesc
.lpSurface
;
962 static void VGA_Unlock(void)
964 IDirectDrawSurface_Unlock(lpddsurf
,sdesc
.lpSurface
);
968 * Set start of 64k window at 0xa0000 in bytes.
969 * If value is -1, initialize color plane support.
970 * If value is >= 0, window contains direct copy of framebuffer.
972 void VGA_SetWindowStart(int start
)
974 if(start
== vga_fb_window
)
977 EnterCriticalSection(&vga_lock
);
979 if(vga_fb_window
== -1)
980 FIXME("Remove VGA memory emulation.\n");
982 VGA_SyncWindow( TRUE
);
984 vga_fb_window
= start
;
986 if(vga_fb_window
== -1)
987 FIXME("Install VGA memory emulation.\n");
989 VGA_SyncWindow( FALSE
);
991 LeaveCriticalSection(&vga_lock
);
995 * Get start of 64k window at 0xa0000 in bytes.
996 * Value is -1 in color plane modes.
998 int VGA_GetWindowStart(void)
1000 return vga_fb_window
;
1004 /**********************************************************************
1007 * Callback for VGA_ShowMouse.
1009 static void WINAPI
VGA_DoShowMouse( ULONG_PTR show
)
1015 rv
= ShowCursor( show
);
1017 while( show
? (rv
< 0) : (rv
>= 0) );
1021 /**********************************************************************
1024 * If argument is TRUE, unconditionally show mouse cursor.
1025 * If argument is FALSE, unconditionally hide mouse cursor.
1026 * This only works in graphics mode.
1028 void VGA_ShowMouse( BOOL show
)
1031 MZ_RunInThread( VGA_DoShowMouse
, (ULONG_PTR
)show
);
1035 /**********************************************************************
1038 * Update the current palette
1040 * Note: When updating the current CGA palette, palette index 0
1041 * refers to palette2, and index 1 is palette1 (default palette)
1043 void VGA_UpdatePalette(void)
1045 /* Figure out which palette is used now */
1046 if(vga_fb_bright
== TRUE
)
1048 if(vga_fb_palette_index
== 0)
1050 vga_fb_palette
= cga_palette2_bright
;
1052 else if(vga_fb_palette_index
== 1)
1054 vga_fb_palette
= cga_palette1_bright
;
1059 if(vga_fb_palette_index
== 0)
1061 vga_fb_palette
= cga_palette2
;
1063 else if(vga_fb_palette_index
== 1)
1065 vga_fb_palette
= cga_palette1
;
1069 /* Now update the palette */
1070 VGA_SetPalette(vga_fb_palette
,0,4);
1073 /**********************************************************************
1076 * Select if using a "bright" palette or not.
1077 * This is a property of the CGA controller
1079 void VGA_SetBright(BOOL bright
)
1081 TRACE("%i\n", bright
);
1083 /* Remember the "bright" value used by the CGA controller */
1084 vga_fb_bright
= bright
;
1087 /**********************************************************************
1090 * Select if output is enabled or disabled
1091 * This is a property of the CGA controller
1093 void VGA_SetEnabled(BOOL enabled
)
1095 TRACE("%i\n", enabled
);
1097 /* Check if going from enabled to disabled */
1098 if(vga_fb_enabled
== TRUE
&& enabled
== FALSE
)
1100 /* Clear frame buffer */
1101 memset(vga_fb_window_data
, 0x00, vga_fb_window_size
);
1104 /* Remember the "enabled" value */
1105 vga_fb_enabled
= enabled
;
1108 /**********************************************************************
1109 * VGA_SetPaletteIndex
1111 * Select the index of the palette which is currently in use
1112 * This is a property of the CGA controller
1114 void VGA_SetPaletteIndex(unsigned index
)
1116 TRACE("%i\n", index
);
1118 /* Remember the palette index, which is only used by CGA for now */
1119 vga_fb_palette_index
= index
;
1122 /**********************************************************************
1125 * Write data to the framebuffer
1126 * This is a property of the CGA controller, but might be supported
1127 * later by other framebuffer types
1129 void VGA_WritePixel(unsigned color
, unsigned page
, unsigned col
, unsigned row
)
1135 /* Calculate CGA byte offset */
1136 char *data
= vga_fb_window_data
;
1137 off
= row
& 1 ? (8 * 1024) : 0;
1138 off
+= (80 * (row
/2));
1141 /* Calculate bits offset */
1142 pos
= 6 - (col
%4 * 2);
1144 /* Clear current data */
1149 bits
= color
<< pos
;
1155 /* prepare the text mode video memory copy that is used to only
1156 * update the video memory line that did get updated. */
1157 static void VGA_PrepareVideoMemCopy(unsigned Xres
, unsigned Yres
)
1163 * Allocate space for char + attr.
1167 vga_text_old
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
1168 vga_text_old
, Xres
* Yres
* 2 );
1170 vga_text_old
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
1172 p
= VGA_AlphaBuffer();
1175 /* make sure the video mem copy contains the exact opposite of our
1176 * actual text mode memory area to make sure the screen
1177 * does get updated fully initially */
1178 for (i
=0; i
< Xres
*Yres
*2; i
++)
1179 *p2
++ = *p
++ ^ 0xff; /* XOR it */
1182 /**********************************************************************
1185 * Set VGA emulation to text mode.
1187 void VGA_SetAlphaMode(unsigned Xres
,unsigned Yres
)
1190 VGA_DeinstallTimer();
1192 VGA_PrepareVideoMemCopy(Xres
, Yres
);
1193 vga_text_width
= Xres
;
1194 vga_text_height
= Yres
;
1196 if (vga_text_x
>= vga_text_width
|| vga_text_y
>= vga_text_height
)
1197 VGA_SetCursorPos(0,0);
1199 if(vga_text_console
) {
1203 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size
);
1205 /* poll every 30ms (33fps should provide adequate responsiveness) */
1206 VGA_InstallTimer(30);
1210 /**********************************************************************
1213 * Initialize VGA text mode handling and return default text mode.
1214 * This function does not set VGA emulation to text mode.
1216 void VGA_InitAlphaMode(unsigned*Xres
,unsigned*Yres
)
1218 CONSOLE_SCREEN_BUFFER_INFO info
;
1220 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info
))
1222 vga_text_console
= TRUE
;
1223 vga_text_x
= info
.dwCursorPosition
.X
;
1224 vga_text_y
= info
.dwCursorPosition
.Y
;
1225 vga_text_attr
= info
.wAttributes
;
1226 *Xres
= info
.dwSize
.X
;
1227 *Yres
= info
.dwSize
.Y
;
1231 vga_text_console
= FALSE
;
1234 vga_text_attr
= 0x0f;
1240 /**********************************************************************
1243 * Get current text mode. Returns TRUE and sets resolution if
1244 * any VGA text mode has been initialized.
1246 BOOL
VGA_GetAlphaMode(unsigned*Xres
,unsigned*Yres
)
1248 if (vga_text_width
!= 0 && vga_text_height
!= 0) {
1249 *Xres
= vga_text_width
;
1250 *Yres
= vga_text_height
;
1256 void VGA_SetCursorShape(unsigned char start_options
, unsigned char end
)
1258 CONSOLE_CURSOR_INFO cci
;
1260 /* standard cursor settings:
1261 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
1263 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
1264 cci
.dwSize
= ((end
& 0x1f) - (start_options
& 0x1f))/0x0e * 100;
1265 if (!cci
.dwSize
) cci
.dwSize
++; /* NULL cursor would make SCCI() fail ! */
1266 cci
.bVisible
= ((start_options
& 0x60) != 0x20); /* invisible ? */
1268 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci
);
1271 void VGA_SetCursorPos(unsigned X
,unsigned Y
)
1277 void VGA_GetCursorPos(unsigned*X
,unsigned*Y
)
1279 if (X
) *X
= vga_text_x
;
1280 if (Y
) *Y
= vga_text_y
;
1283 static void VGA_PutCharAt(unsigned x
, unsigned y
, BYTE ascii
, int attr
)
1285 char *dat
= VGA_AlphaBuffer() + ((vga_text_width
* y
+ x
) * 2);
1291 void VGA_WriteChars(unsigned X
,unsigned Y
,unsigned ch
,int attr
,int count
)
1293 EnterCriticalSection(&vga_lock
);
1296 VGA_PutCharAt(X
+ count
, Y
, ch
, attr
);
1298 LeaveCriticalSection(&vga_lock
);
1301 void VGA_PutChar(BYTE ascii
)
1305 EnterCriticalSection(&vga_lock
);
1312 VGA_PutCharAt(vga_text_x
, vga_text_y
, ' ', 0);
1317 vga_text_x
+= ((vga_text_x
+ 8) & ~7) - vga_text_x
;
1333 VGA_PutCharAt(vga_text_x
, vga_text_y
, ascii
, vga_text_attr
);
1337 if (vga_text_x
>= vga_text_width
)
1343 if (vga_text_y
>= vga_text_height
)
1345 vga_text_y
= vga_text_height
- 1;
1346 VGA_ScrollUpText( 0, 0,
1347 vga_text_height
- 1, vga_text_width
- 1,
1352 * If we don't have a console, write directly to standard output.
1354 if(!vga_text_console
)
1355 WriteFile(VGA_AlphaConsole(), &ascii
, 1, &w
, NULL
);
1357 LeaveCriticalSection(&vga_lock
);
1360 void VGA_SetTextAttribute(BYTE attr
)
1362 vga_text_attr
= attr
;
1365 void VGA_ClearText(unsigned row1
, unsigned col1
,
1366 unsigned row2
, unsigned col2
,
1371 EnterCriticalSection(&vga_lock
);
1373 for(y
=row1
; y
<=row2
; y
++)
1374 for(x
=col1
; x
<=col2
; x
++)
1375 VGA_PutCharAt(x
, y
, 0x20, attr
);
1377 LeaveCriticalSection(&vga_lock
);
1380 void VGA_ScrollUpText(unsigned row1
, unsigned col1
,
1381 unsigned row2
, unsigned col2
,
1382 unsigned lines
, BYTE attr
)
1384 char *buffer
= VGA_AlphaBuffer();
1387 EnterCriticalSection(&vga_lock
);
1392 for (y
= row1
; y
<= row2
- lines
; y
++)
1393 memmove( buffer
+ col1
+ y
* vga_text_width
* 2,
1394 buffer
+ col1
+ (y
+ lines
) * vga_text_width
* 2,
1395 (col2
- col1
+ 1) * 2 );
1398 * Fill exposed lines.
1400 for (y
= max(row1
, row2
- lines
+ 1); y
<= row2
; y
++)
1401 VGA_WriteChars( col1
, y
, ' ', attr
, col2
- col1
+ 1 );
1403 LeaveCriticalSection(&vga_lock
);
1406 void VGA_ScrollDownText(unsigned row1
, unsigned col1
,
1407 unsigned row2
, unsigned col2
,
1408 unsigned lines
, BYTE attr
)
1410 char *buffer
= VGA_AlphaBuffer();
1413 EnterCriticalSection(&vga_lock
);
1418 for (y
= row2
; y
>= row1
+ lines
; y
--)
1419 memmove( buffer
+ col1
+ y
* vga_text_width
* 2,
1420 buffer
+ col1
+ (y
- lines
) * vga_text_width
* 2,
1421 (col2
- col1
+ 1) * 2 );
1424 * Fill exposed lines.
1426 for (y
= row1
; y
<= min(row1
+ lines
- 1, row2
); y
++)
1427 VGA_WriteChars( col1
, y
, ' ', attr
, col2
- col1
+ 1 );
1429 LeaveCriticalSection(&vga_lock
);
1432 void VGA_GetCharacterAtCursor(BYTE
*ascii
, BYTE
*attr
)
1436 dat
= VGA_AlphaBuffer() + ((vga_text_width
* vga_text_y
+ vga_text_x
) * 2);
1445 /* FIXME: optimize by doing this only if the data has actually changed
1446 * (in a way similar to DIBSection, perhaps) */
1447 static void VGA_Poll_Graphics(void)
1449 unsigned int Pitch
, Height
, Width
, X
, Y
;
1451 char *dat
= vga_fb_data
+ vga_fb_offset
;
1452 int bpp
= (vga_fb_depth
+ 7) / 8;
1454 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
1458 * Synchronize framebuffer contents.
1460 if (vga_fb_window
!= -1)
1461 VGA_SyncWindow( TRUE
);
1464 * CGA framebuffer (160x200)
1465 * This buffer is encoded as following:
1466 * - 4 bit pr. pixel, 2 pixels per byte
1467 * - 80 bytes per row
1468 * - Every second line has an offset of 8096
1470 if(vga_fb_depth
== 4 && vga_fb_width
== 160 && vga_fb_height
== 200){
1474 for(Y
=0; Y
<vga_fb_height
; Y
++, surf
+=(Pitch
*2)){
1475 for(X
=0; X
<vga_fb_width
; X
++){
1476 off
= Y
& 1 ? (8 * 1024) : 0;
1477 off
+= (80 * (Y
/2));
1479 value
= (dat
[off
] >> bits
) & 0xF;
1480 surf
[(X
*4)+0] = value
;
1481 surf
[(X
*4)+1] = value
;
1482 surf
[(X
*4)+2] = value
;
1483 surf
[(X
*4)+3] = value
;
1484 surf
[(X
*4)+Pitch
+0] = value
;
1485 surf
[(X
*4)+Pitch
+1] = value
;
1486 surf
[(X
*4)+Pitch
+2] = value
;
1487 surf
[(X
*4)+Pitch
+3] = value
;
1495 * CGA framebuffer (320x200)
1496 * This buffer is encoded as following:
1497 * - 2 bits per color, 4 pixels per byte
1498 * - 80 bytes per row
1499 * - Every second line has an offset of 8096
1501 else if(vga_fb_depth
== 2 && vga_fb_width
== 320 && vga_fb_height
== 200){
1506 for(Y
=0; Y
<vga_fb_height
; Y
++, surf
+=(Pitch
*2)){
1507 for(X
=0; X
<vga_fb_width
; X
++){
1508 off
= Y
& 1 ? (8 * 1024) : 0;
1509 off
+= (80 * (Y
/2));
1511 value
= (dat
[off
] >> bits
) & 0x3;
1512 surf
[(X
*2)] = value
;
1513 surf
[(X
*2)+1] = value
;
1514 surf
[(X
*2)+Pitch
] = value
;
1515 surf
[(X
*2)+Pitch
+1] = value
;
1523 * Double VGA framebuffer (320x200 -> 640x400), if needed.
1525 else if(Height
>= 2 * vga_fb_height
&& Width
>= 2 * vga_fb_width
&& bpp
== 1)
1526 for (Y
=0; Y
<vga_fb_height
; Y
++,surf
+=Pitch
*2,dat
+=vga_fb_pitch
)
1527 for (X
=0; X
<vga_fb_width
; X
++) {
1528 BYTE value
= dat
[X
];
1530 surf
[X
*2+1] = value
;
1531 surf
[X
*2+Pitch
] = value
;
1532 surf
[X
*2+Pitch
+1] = value
;
1535 for (Y
=0; Y
<vga_fb_height
; Y
++,surf
+=Pitch
,dat
+=vga_fb_pitch
)
1536 memcpy(surf
, dat
, vga_fb_width
* bpp
);
1541 static void VGA_Poll_Text(void)
1543 char *dat
, *old
, *p_line
;
1545 CHAR_INFO ch
[256]; /* that should suffice for the largest text width */
1548 HANDLE con
= VGA_AlphaConsole();
1549 BOOL linechanged
= FALSE
; /* video memory area differs from stored copy? */
1551 /* Synchronize cursor position. */
1554 SetConsoleCursorPosition(con
,off
);
1556 dat
= VGA_AlphaBuffer();
1557 old
= vga_text_old
; /* pointer to stored video mem copy */
1558 siz
.X
= vga_text_width
; siz
.Y
= 1;
1559 off
.X
= 0; off
.Y
= 0;
1561 /* copy from virtual VGA frame buffer to console */
1562 for (Y
=0; Y
<vga_text_height
; Y
++) {
1563 linechanged
= memcmp(dat
, old
, vga_text_width
*2);
1566 /*TRACE("line %d changed\n", Y);*/
1568 for (X
=0; X
<vga_text_width
; X
++) {
1569 ch
[X
].Char
.AsciiChar
= *p_line
++;
1570 /* WriteConsoleOutputA doesn't like "dead" chars */
1571 if (ch
[X
].Char
.AsciiChar
== '\0')
1572 ch
[X
].Char
.AsciiChar
= ' ';
1573 ch
[X
].Attributes
= *p_line
++;
1575 dest
.Top
=Y
; dest
.Bottom
=Y
;
1576 dest
.Left
=0; dest
.Right
=vga_text_width
+1;
1577 WriteConsoleOutputA(con
, ch
, siz
, off
, &dest
);
1578 memcpy(old
, dat
, vga_text_width
*2);
1580 /* advance to next text line */
1581 dat
+= vga_text_width
*2;
1582 old
+= vga_text_width
*2;
1586 static void CALLBACK
VGA_Poll( LPVOID arg
, DWORD low
, DWORD high
)
1588 EnterCriticalSection(&vga_lock
);
1591 VGA_Poll_Graphics();
1596 * Fake start of retrace.
1598 vga_retrace_vertical
= TRUE
;
1600 LeaveCriticalSection(&vga_lock
);
1603 static BYTE palreg
,palcnt
;
1604 static PALETTEENTRY paldat
;
1606 void VGA_ioport_out( WORD port
, BYTE val
)
1609 /* General Register - Feature Control */
1611 FIXME("Unsupported VGA register: general register - feature control 0x%04x (value 0x%02x)\n", port
, val
);
1613 /* Attribute Controller - Address/Other */
1615 if (vga_address_3c0
)
1616 vga_index_3c0
= val
;
1618 FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x (value 0x%02x)\n",
1619 vga_index_3c0
, val
);
1620 vga_address_3c0
= !vga_address_3c0
;
1622 /* General Register - Misc output */
1624 FIXME("Unsupported VGA register: general register - misc output 0x%04x (value 0x%02x)\n", port
, val
);
1626 /* General Register - Video subsystem enable */
1628 FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x (value 0x%02x)\n", port
, val
);
1630 /* Sequencer Register - Address */
1632 vga_index_3c4
= val
;
1634 /* Sequencer Register - Other */
1636 switch(vga_index_3c4
) {
1637 case 0x04: /* Sequencer: Memory Mode Register */
1638 if(vga_fb_depth
== 8)
1639 VGA_SetWindowStart((val
& 8) ? 0 : -1);
1641 FIXME("Memory Mode Register not supported in this mode.\n");
1644 FIXME("Unsupported index, VGA sequencer register 0x3c4: 0x%02x (value 0x%02x)\n",
1645 vga_index_3c4
, val
);
1649 palreg
=val
; palcnt
=0; break;
1651 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
1653 VGA_SetPalette(&paldat
,palreg
++,1);
1657 /* Graphics Controller Register - Address */
1659 vga_index_3ce
= val
;
1661 /* Graphics Controller Register - Other */
1663 FIXME("Unsupported index, VGA graphics controller register - other 0x3ce: 0x%02x (value 0x%02x)\n",
1664 vga_index_3ce
, val
);
1666 /* CRT Controller Register - Index (MDA) */
1668 /* CRT Controller Register - Index (CGA) */
1670 vga_index_3d4
= val
;
1672 /* CRT Controller Register - Other (MDA) */
1674 /* CRT Controller Register - Other (CGA) */
1676 FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x (value 0x%02x)\n",
1677 vga_index_3d4
, val
);
1680 /* Mode control register (CGA) */
1683 /* Detect 160x200, 16 color mode (composite) */
1684 if((val
& 0x02) && (val
& 0x10))
1686 /* Switch to 160x200x4 composite mode */
1687 VGA_SetMode(160, 200, 4);
1690 /* Set the enabled bit */
1691 VGA_SetEnabled((val
& 0x08) && 1);
1694 /* Colour control register (CGA) */
1697 VGA_SetBright((val
& 0x10) && 1);
1699 /* Set palette index */
1700 VGA_SetPaletteIndex((val
& 0x20) && 1);
1702 /* Now update the palette */
1703 VGA_UpdatePalette();
1706 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port
, val
);
1710 BYTE
VGA_ioport_in( WORD port
)
1715 /* Attribute Controller - Other */
1717 FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x\n",
1720 /* General Register - Input status 0 */
1723 FIXME("Unsupported VGA register: general register - input status 0 0x%04x\n", port
);
1725 /* General Register - Video subsystem enable */
1728 FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x\n", port
);
1730 /* Sequencer Register - Other */
1732 switch(vga_index_3c4
) {
1733 case 0x04: /* Sequencer: Memory Mode Register */
1734 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1736 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1740 /* General Register - DAC State */
1743 FIXME("Unsupported VGA register: general register - DAC State 0x%04x\n", port
);
1745 /* General Register - Feature control */
1748 FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port
);
1750 /* General Register - Misc output */
1753 FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port
);
1755 /* Graphics Controller Register - Other */
1757 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1760 /* CRT Controller Register - Other (MDA) */
1762 /* CRT Controller Register - Other (CGA) */
1764 FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x\n",
1767 /* General Register - Input status 1 (MDA) */
1769 /* General Register - Input status 1 (CGA) */
1772 * Read from this register resets register 0x3c0 address flip-flop.
1774 vga_address_3c0
= TRUE
;
1777 * Read from this register returns following bits:
1778 * xxxx1xxx = Vertical retrace in progress if set.
1779 * xxxxx1xx = Light pen switched on.
1780 * xxxxxx1x = Light pen trigger set.
1781 * xxxxxxx1 = Either vertical or horizontal retrace
1782 * in progress if set.
1785 if (vga_retrace_vertical
)
1787 if (vga_retrace_horizontal
)
1791 * If VGA mode has been set, vertical retrace is
1792 * turned on once a frame and cleared after each read.
1793 * This might cause applications that synchronize with
1794 * vertical retrace to actually skip one frame but that
1795 * is probably not a problem.
1797 * If no VGA mode has been set, vertical retrace is faked
1798 * by toggling the value after every read.
1800 if (VGA_IsTimerRunning())
1801 vga_retrace_vertical
= FALSE
;
1803 vga_retrace_vertical
= !vga_retrace_vertical
;
1806 * Toggle horizontal retrace.
1808 vga_retrace_horizontal
= !vga_retrace_horizontal
;
1813 FIXME("Unsupported VGA register: 0x%04x\n", port
);
1818 void VGA_Clean(void)
1821 VGA_DeinstallTimer();