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 static LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
934 if (!lpddraw
) return NULL
;
935 if (!lpddsurf
) return NULL
;
936 if (IDirectDrawSurface_Lock(lpddsurf
,NULL
,&sdesc
,0,0) != S_OK
) {
937 ERR("could not lock surface!\n");
940 if (Pitch
) *Pitch
=sdesc
.u1
.lPitch
;
941 if (Height
) *Height
=sdesc
.dwHeight
;
942 if (Width
) *Width
=sdesc
.dwWidth
;
943 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
944 return sdesc
.lpSurface
;
947 static void VGA_Unlock(void)
949 IDirectDrawSurface_Unlock(lpddsurf
,sdesc
.lpSurface
);
953 * Set start of 64k window at 0xa0000 in bytes.
954 * If value is -1, initialize color plane support.
955 * If value is >= 0, window contains direct copy of framebuffer.
957 void VGA_SetWindowStart(int start
)
959 if(start
== vga_fb_window
)
962 EnterCriticalSection(&vga_lock
);
964 if(vga_fb_window
== -1)
965 FIXME("Remove VGA memory emulation.\n");
967 VGA_SyncWindow( TRUE
);
969 vga_fb_window
= start
;
971 if(vga_fb_window
== -1)
972 FIXME("Install VGA memory emulation.\n");
974 VGA_SyncWindow( FALSE
);
976 LeaveCriticalSection(&vga_lock
);
980 * Get start of 64k window at 0xa0000 in bytes.
981 * Value is -1 in color plane modes.
983 int VGA_GetWindowStart(void)
985 return vga_fb_window
;
989 /**********************************************************************
992 * Callback for VGA_ShowMouse.
994 static void WINAPI
VGA_DoShowMouse( ULONG_PTR show
)
1000 rv
= ShowCursor( show
);
1002 while( show
? (rv
< 0) : (rv
>= 0) );
1006 /**********************************************************************
1009 * If argument is TRUE, unconditionally show mouse cursor.
1010 * If argument is FALSE, unconditionally hide mouse cursor.
1011 * This only works in graphics mode.
1013 void VGA_ShowMouse( BOOL show
)
1016 MZ_RunInThread( VGA_DoShowMouse
, (ULONG_PTR
)show
);
1020 /**********************************************************************
1023 * Update the current palette
1025 * Note: When updating the current CGA palette, palette index 0
1026 * refers to palette2, and index 1 is palette1 (default palette)
1028 void VGA_UpdatePalette(void)
1030 /* Figure out which palette is used now */
1031 if(vga_fb_bright
== TRUE
)
1033 if(vga_fb_palette_index
== 0)
1035 vga_fb_palette
= cga_palette2_bright
;
1037 else if(vga_fb_palette_index
== 1)
1039 vga_fb_palette
= cga_palette1_bright
;
1044 if(vga_fb_palette_index
== 0)
1046 vga_fb_palette
= cga_palette2
;
1048 else if(vga_fb_palette_index
== 1)
1050 vga_fb_palette
= cga_palette1
;
1054 /* Now update the palette */
1055 VGA_SetPalette(vga_fb_palette
,0,4);
1058 /**********************************************************************
1061 * Select if using a "bright" palette or not.
1062 * This is a property of the CGA controller
1064 void VGA_SetBright(BOOL bright
)
1066 TRACE("%i\n", bright
);
1068 /* Remember the "bright" value used by the CGA controller */
1069 vga_fb_bright
= bright
;
1072 /**********************************************************************
1075 * Select if output is enabled or disabled
1076 * This is a property of the CGA controller
1078 static void VGA_SetEnabled(BOOL enabled
)
1080 TRACE("%i\n", enabled
);
1082 /* Check if going from enabled to disabled */
1083 if(vga_fb_enabled
== TRUE
&& enabled
== FALSE
)
1085 /* Clear frame buffer */
1086 memset(vga_fb_window_data
, 0x00, vga_fb_window_size
);
1089 /* Remember the "enabled" value */
1090 vga_fb_enabled
= enabled
;
1093 /**********************************************************************
1094 * VGA_SetPaletteIndex
1096 * Select the index of the palette which is currently in use
1097 * This is a property of the CGA controller
1099 void VGA_SetPaletteIndex(unsigned index
)
1101 TRACE("%i\n", index
);
1103 /* Remember the palette index, which is only used by CGA for now */
1104 vga_fb_palette_index
= index
;
1107 /**********************************************************************
1110 * Write data to the framebuffer
1111 * This is a property of the CGA controller, but might be supported
1112 * later by other framebuffer types
1114 void VGA_WritePixel(unsigned color
, unsigned page
, unsigned col
, unsigned row
)
1120 /* Calculate CGA byte offset */
1121 char *data
= vga_fb_window_data
;
1122 off
= row
& 1 ? (8 * 1024) : 0;
1123 off
+= (80 * (row
/2));
1126 /* Calculate bits offset */
1127 pos
= 6 - (col
%4 * 2);
1129 /* Clear current data */
1134 bits
= color
<< pos
;
1140 /* prepare the text mode video memory copy that is used to only
1141 * update the video memory line that did get updated. */
1142 static void VGA_PrepareVideoMemCopy(unsigned Xres
, unsigned Yres
)
1148 * Allocate space for char + attr.
1152 vga_text_old
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
1153 vga_text_old
, Xres
* Yres
* 2 );
1155 vga_text_old
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
1157 p
= VGA_AlphaBuffer();
1160 /* make sure the video mem copy contains the exact opposite of our
1161 * actual text mode memory area to make sure the screen
1162 * does get updated fully initially */
1163 for (i
=0; i
< Xres
*Yres
*2; i
++)
1164 *p2
++ = *p
++ ^ 0xff; /* XOR it */
1167 /**********************************************************************
1170 * Set VGA emulation to text mode.
1172 void VGA_SetAlphaMode(unsigned Xres
,unsigned Yres
)
1175 VGA_DeinstallTimer();
1177 VGA_PrepareVideoMemCopy(Xres
, Yres
);
1178 vga_text_width
= Xres
;
1179 vga_text_height
= Yres
;
1181 if (vga_text_x
>= vga_text_width
|| vga_text_y
>= vga_text_height
)
1182 VGA_SetCursorPos(0,0);
1184 if(vga_text_console
) {
1188 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size
);
1190 /* poll every 30ms (33fps should provide adequate responsiveness) */
1191 VGA_InstallTimer(30);
1195 /**********************************************************************
1198 * Initialize VGA text mode handling and return default text mode.
1199 * This function does not set VGA emulation to text mode.
1201 void VGA_InitAlphaMode(unsigned*Xres
,unsigned*Yres
)
1203 CONSOLE_SCREEN_BUFFER_INFO info
;
1205 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info
))
1207 vga_text_console
= TRUE
;
1208 vga_text_x
= info
.dwCursorPosition
.X
;
1209 vga_text_y
= info
.dwCursorPosition
.Y
;
1210 vga_text_attr
= info
.wAttributes
;
1211 *Xres
= info
.dwSize
.X
;
1212 *Yres
= info
.dwSize
.Y
;
1216 vga_text_console
= FALSE
;
1219 vga_text_attr
= 0x0f;
1225 /**********************************************************************
1228 * Get current text mode. Returns TRUE and sets resolution if
1229 * any VGA text mode has been initialized.
1231 BOOL
VGA_GetAlphaMode(unsigned*Xres
,unsigned*Yres
)
1233 if (vga_text_width
!= 0 && vga_text_height
!= 0) {
1234 *Xres
= vga_text_width
;
1235 *Yres
= vga_text_height
;
1241 void VGA_SetCursorShape(unsigned char start_options
, unsigned char end
)
1243 CONSOLE_CURSOR_INFO cci
;
1245 /* standard cursor settings:
1246 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
1248 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
1249 cci
.dwSize
= ((end
& 0x1f) - (start_options
& 0x1f))/0x0e * 100;
1250 if (!cci
.dwSize
) cci
.dwSize
++; /* NULL cursor would make SCCI() fail ! */
1251 cci
.bVisible
= ((start_options
& 0x60) != 0x20); /* invisible ? */
1253 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci
);
1256 void VGA_SetCursorPos(unsigned X
,unsigned Y
)
1262 void VGA_GetCursorPos(unsigned*X
,unsigned*Y
)
1264 if (X
) *X
= vga_text_x
;
1265 if (Y
) *Y
= vga_text_y
;
1268 static void VGA_PutCharAt(unsigned x
, unsigned y
, BYTE ascii
, int attr
)
1270 char *dat
= VGA_AlphaBuffer() + ((vga_text_width
* y
+ x
) * 2);
1276 void VGA_WriteChars(unsigned X
,unsigned Y
,unsigned ch
,int attr
,int count
)
1278 EnterCriticalSection(&vga_lock
);
1281 VGA_PutCharAt(X
+ count
, Y
, ch
, attr
);
1283 LeaveCriticalSection(&vga_lock
);
1286 void VGA_PutChar(BYTE ascii
)
1290 EnterCriticalSection(&vga_lock
);
1297 VGA_PutCharAt(vga_text_x
, vga_text_y
, ' ', 0);
1302 vga_text_x
+= ((vga_text_x
+ 8) & ~7) - vga_text_x
;
1318 VGA_PutCharAt(vga_text_x
, vga_text_y
, ascii
, vga_text_attr
);
1322 if (vga_text_x
>= vga_text_width
)
1328 if (vga_text_y
>= vga_text_height
)
1330 vga_text_y
= vga_text_height
- 1;
1331 VGA_ScrollUpText( 0, 0,
1332 vga_text_height
- 1, vga_text_width
- 1,
1337 * If we don't have a console, write directly to standard output.
1339 if(!vga_text_console
)
1340 WriteFile(VGA_AlphaConsole(), &ascii
, 1, &w
, NULL
);
1342 LeaveCriticalSection(&vga_lock
);
1345 void VGA_ClearText(unsigned row1
, unsigned col1
,
1346 unsigned row2
, unsigned col2
,
1351 EnterCriticalSection(&vga_lock
);
1353 for(y
=row1
; y
<=row2
; y
++)
1354 for(x
=col1
; x
<=col2
; x
++)
1355 VGA_PutCharAt(x
, y
, 0x20, attr
);
1357 LeaveCriticalSection(&vga_lock
);
1360 void VGA_ScrollUpText(unsigned row1
, unsigned col1
,
1361 unsigned row2
, unsigned col2
,
1362 unsigned lines
, BYTE attr
)
1364 char *buffer
= VGA_AlphaBuffer();
1367 EnterCriticalSection(&vga_lock
);
1372 for (y
= row1
; y
<= row2
- lines
; y
++)
1373 memmove( buffer
+ col1
+ y
* vga_text_width
* 2,
1374 buffer
+ col1
+ (y
+ lines
) * vga_text_width
* 2,
1375 (col2
- col1
+ 1) * 2 );
1378 * Fill exposed lines.
1380 for (y
= max(row1
, row2
- lines
+ 1); y
<= row2
; y
++)
1381 VGA_WriteChars( col1
, y
, ' ', attr
, col2
- col1
+ 1 );
1383 LeaveCriticalSection(&vga_lock
);
1386 void VGA_ScrollDownText(unsigned row1
, unsigned col1
,
1387 unsigned row2
, unsigned col2
,
1388 unsigned lines
, BYTE attr
)
1390 char *buffer
= VGA_AlphaBuffer();
1393 EnterCriticalSection(&vga_lock
);
1398 for (y
= row2
; y
>= row1
+ lines
; y
--)
1399 memmove( buffer
+ col1
+ y
* vga_text_width
* 2,
1400 buffer
+ col1
+ (y
- lines
) * vga_text_width
* 2,
1401 (col2
- col1
+ 1) * 2 );
1404 * Fill exposed lines.
1406 for (y
= row1
; y
<= min(row1
+ lines
- 1, row2
); y
++)
1407 VGA_WriteChars( col1
, y
, ' ', attr
, col2
- col1
+ 1 );
1409 LeaveCriticalSection(&vga_lock
);
1412 void VGA_GetCharacterAtCursor(BYTE
*ascii
, BYTE
*attr
)
1416 dat
= VGA_AlphaBuffer() + ((vga_text_width
* vga_text_y
+ vga_text_x
) * 2);
1425 /* FIXME: optimize by doing this only if the data has actually changed
1426 * (in a way similar to DIBSection, perhaps) */
1427 static void VGA_Poll_Graphics(void)
1429 unsigned int Pitch
, Height
, Width
, X
, Y
;
1431 char *dat
= vga_fb_data
+ vga_fb_offset
;
1432 int bpp
= (vga_fb_depth
+ 7) / 8;
1434 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
1438 * Synchronize framebuffer contents.
1440 if (vga_fb_window
!= -1)
1441 VGA_SyncWindow( TRUE
);
1444 * CGA framebuffer (160x200)
1445 * This buffer is encoded as following:
1446 * - 4 bit pr. pixel, 2 pixels per byte
1447 * - 80 bytes per row
1448 * - Every second line has an offset of 8096
1450 if(vga_fb_depth
== 4 && vga_fb_width
== 160 && vga_fb_height
== 200){
1454 for(Y
=0; Y
<vga_fb_height
; Y
++, surf
+=(Pitch
*2)){
1455 for(X
=0; X
<vga_fb_width
; X
++){
1456 off
= Y
& 1 ? (8 * 1024) : 0;
1457 off
+= (80 * (Y
/2));
1459 value
= (dat
[off
] >> bits
) & 0xF;
1460 surf
[(X
*4)+0] = value
;
1461 surf
[(X
*4)+1] = value
;
1462 surf
[(X
*4)+2] = value
;
1463 surf
[(X
*4)+3] = value
;
1464 surf
[(X
*4)+Pitch
+0] = value
;
1465 surf
[(X
*4)+Pitch
+1] = value
;
1466 surf
[(X
*4)+Pitch
+2] = value
;
1467 surf
[(X
*4)+Pitch
+3] = value
;
1475 * CGA framebuffer (320x200)
1476 * This buffer is encoded as following:
1477 * - 2 bits per color, 4 pixels per byte
1478 * - 80 bytes per row
1479 * - Every second line has an offset of 8096
1481 else if(vga_fb_depth
== 2 && vga_fb_width
== 320 && vga_fb_height
== 200){
1486 for(Y
=0; Y
<vga_fb_height
; Y
++, surf
+=(Pitch
*2)){
1487 for(X
=0; X
<vga_fb_width
; X
++){
1488 off
= Y
& 1 ? (8 * 1024) : 0;
1489 off
+= (80 * (Y
/2));
1491 value
= (dat
[off
] >> bits
) & 0x3;
1492 surf
[(X
*2)] = value
;
1493 surf
[(X
*2)+1] = value
;
1494 surf
[(X
*2)+Pitch
] = value
;
1495 surf
[(X
*2)+Pitch
+1] = value
;
1503 * Double VGA framebuffer (320x200 -> 640x400), if needed.
1505 else if(Height
>= 2 * vga_fb_height
&& Width
>= 2 * vga_fb_width
&& bpp
== 1)
1506 for (Y
=0; Y
<vga_fb_height
; Y
++,surf
+=Pitch
*2,dat
+=vga_fb_pitch
)
1507 for (X
=0; X
<vga_fb_width
; X
++) {
1508 BYTE value
= dat
[X
];
1510 surf
[X
*2+1] = value
;
1511 surf
[X
*2+Pitch
] = value
;
1512 surf
[X
*2+Pitch
+1] = value
;
1515 for (Y
=0; Y
<vga_fb_height
; Y
++,surf
+=Pitch
,dat
+=vga_fb_pitch
)
1516 memcpy(surf
, dat
, vga_fb_width
* bpp
);
1521 static void VGA_Poll_Text(void)
1523 char *dat
, *old
, *p_line
;
1525 CHAR_INFO ch
[256]; /* that should suffice for the largest text width */
1528 HANDLE con
= VGA_AlphaConsole();
1529 BOOL linechanged
= FALSE
; /* video memory area differs from stored copy? */
1531 /* Synchronize cursor position. */
1534 SetConsoleCursorPosition(con
,off
);
1536 dat
= VGA_AlphaBuffer();
1537 old
= vga_text_old
; /* pointer to stored video mem copy */
1538 siz
.X
= vga_text_width
; siz
.Y
= 1;
1539 off
.X
= 0; off
.Y
= 0;
1541 /* copy from virtual VGA frame buffer to console */
1542 for (Y
=0; Y
<vga_text_height
; Y
++) {
1543 linechanged
= memcmp(dat
, old
, vga_text_width
*2);
1546 /*TRACE("line %d changed\n", Y);*/
1548 for (X
=0; X
<vga_text_width
; X
++) {
1549 ch
[X
].Char
.AsciiChar
= *p_line
++;
1550 /* WriteConsoleOutputA doesn't like "dead" chars */
1551 if (ch
[X
].Char
.AsciiChar
== '\0')
1552 ch
[X
].Char
.AsciiChar
= ' ';
1553 ch
[X
].Attributes
= *p_line
++;
1555 dest
.Top
=Y
; dest
.Bottom
=Y
;
1556 dest
.Left
=0; dest
.Right
=vga_text_width
+1;
1557 WriteConsoleOutputA(con
, ch
, siz
, off
, &dest
);
1558 memcpy(old
, dat
, vga_text_width
*2);
1560 /* advance to next text line */
1561 dat
+= vga_text_width
*2;
1562 old
+= vga_text_width
*2;
1566 static void CALLBACK
VGA_Poll( LPVOID arg
, DWORD low
, DWORD high
)
1568 EnterCriticalSection(&vga_lock
);
1571 VGA_Poll_Graphics();
1576 * Fake start of retrace.
1578 vga_retrace_vertical
= TRUE
;
1580 LeaveCriticalSection(&vga_lock
);
1583 static BYTE palreg
,palcnt
;
1584 static PALETTEENTRY paldat
;
1586 void VGA_ioport_out( WORD port
, BYTE val
)
1589 /* General Register - Feature Control */
1591 FIXME("Unsupported VGA register: general register - feature control 0x%04x (value 0x%02x)\n", port
, val
);
1593 /* Attribute Controller - Address/Other */
1595 if (vga_address_3c0
)
1596 vga_index_3c0
= val
;
1598 FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x (value 0x%02x)\n",
1599 vga_index_3c0
, val
);
1600 vga_address_3c0
= !vga_address_3c0
;
1602 /* General Register - Misc output */
1604 FIXME("Unsupported VGA register: general register - misc output 0x%04x (value 0x%02x)\n", port
, val
);
1606 /* General Register - Video subsystem enable */
1608 FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x (value 0x%02x)\n", port
, val
);
1610 /* Sequencer Register - Address */
1612 vga_index_3c4
= val
;
1614 /* Sequencer Register - Other */
1616 switch(vga_index_3c4
) {
1617 case 0x04: /* Sequencer: Memory Mode Register */
1618 if(vga_fb_depth
== 8)
1619 VGA_SetWindowStart((val
& 8) ? 0 : -1);
1621 FIXME("Memory Mode Register not supported in this mode.\n");
1624 FIXME("Unsupported index, VGA sequencer register 0x3c4: 0x%02x (value 0x%02x)\n",
1625 vga_index_3c4
, val
);
1629 palreg
=val
; palcnt
=0; break;
1631 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
1633 VGA_SetPalette(&paldat
,palreg
++,1);
1637 /* Graphics Controller Register - Address */
1639 vga_index_3ce
= val
;
1641 /* Graphics Controller Register - Other */
1643 FIXME("Unsupported index, VGA graphics controller register - other 0x3ce: 0x%02x (value 0x%02x)\n",
1644 vga_index_3ce
, val
);
1646 /* CRT Controller Register - Index (MDA) */
1648 /* CRT Controller Register - Index (CGA) */
1650 vga_index_3d4
= val
;
1652 /* CRT Controller Register - Other (MDA) */
1654 /* CRT Controller Register - Other (CGA) */
1656 FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x (value 0x%02x)\n",
1657 vga_index_3d4
, val
);
1660 /* Mode control register (CGA) */
1663 /* Detect 160x200, 16 color mode (composite) */
1664 if((val
& 0x02) && (val
& 0x10))
1666 /* Switch to 160x200x4 composite mode */
1667 VGA_SetMode(160, 200, 4);
1670 /* Set the enabled bit */
1671 VGA_SetEnabled((val
& 0x08) && 1);
1674 /* Colour control register (CGA) */
1677 VGA_SetBright((val
& 0x10) && 1);
1679 /* Set palette index */
1680 VGA_SetPaletteIndex((val
& 0x20) && 1);
1682 /* Now update the palette */
1683 VGA_UpdatePalette();
1686 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port
, val
);
1690 BYTE
VGA_ioport_in( WORD port
)
1695 /* Attribute Controller - Other */
1697 FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x\n",
1700 /* General Register - Input status 0 */
1703 FIXME("Unsupported VGA register: general register - input status 0 0x%04x\n", port
);
1705 /* General Register - Video subsystem enable */
1708 FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x\n", port
);
1710 /* Sequencer Register - Other */
1712 switch(vga_index_3c4
) {
1713 case 0x04: /* Sequencer: Memory Mode Register */
1714 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1716 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1720 /* General Register - DAC State */
1723 FIXME("Unsupported VGA register: general register - DAC State 0x%04x\n", port
);
1725 /* General Register - Feature control */
1728 FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port
);
1730 /* General Register - Misc output */
1733 FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port
);
1735 /* Graphics Controller Register - Other */
1737 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1740 /* CRT Controller Register - Other (MDA) */
1742 /* CRT Controller Register - Other (CGA) */
1744 FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x\n",
1747 /* General Register - Input status 1 (MDA) */
1749 /* General Register - Input status 1 (CGA) */
1752 * Read from this register resets register 0x3c0 address flip-flop.
1754 vga_address_3c0
= TRUE
;
1757 * Read from this register returns following bits:
1758 * xxxx1xxx = Vertical retrace in progress if set.
1759 * xxxxx1xx = Light pen switched on.
1760 * xxxxxx1x = Light pen trigger set.
1761 * xxxxxxx1 = Either vertical or horizontal retrace
1762 * in progress if set.
1765 if (vga_retrace_vertical
)
1767 if (vga_retrace_horizontal
)
1771 * If VGA mode has been set, vertical retrace is
1772 * turned on once a frame and cleared after each read.
1773 * This might cause applications that synchronize with
1774 * vertical retrace to actually skip one frame but that
1775 * is probably not a problem.
1777 * If no VGA mode has been set, vertical retrace is faked
1778 * by toggling the value after every read.
1780 if (VGA_IsTimerRunning())
1781 vga_retrace_vertical
= FALSE
;
1783 vga_retrace_vertical
= !vga_retrace_vertical
;
1786 * Toggle horizontal retrace.
1788 vga_retrace_horizontal
= !vga_retrace_horizontal
;
1793 FIXME("Unsupported VGA register: 0x%04x\n", port
);
1798 void VGA_Clean(void)
1801 VGA_DeinstallTimer();