d3dxof: Support also xfile version 3.03.
[wine/hacks.git] / dlls / winedos / vga.c
blob772038c24aa31ae11b92b150acf22d1c71185ca8
1 /*
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
21 #include <stdarg.h>
22 #include <string.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "wincon.h"
31 #include "dosexe.h"
32 #include "vga.h"
33 #include "ddraw.h"
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 BOOL vga_fb_bright;
107 * VGA text mode data.
109 * vga_text_attr: Current active attribute.
110 * vga_text_old: Last data sent to console.
111 * This is used to optimize console updates.
112 * vga_text_width: Width of the text display in characters.
113 * vga_text_height: Height of the text display in characters.
114 * vga_text_x: Current cursor X-position. Starts from zero.
115 * vga_text_y: Current cursor Y-position. Starts from zero.
116 * vga_text_console: TRUE if stdout is console,
117 * FALSE if it is regular file.
119 static BYTE vga_text_attr;
120 static char *vga_text_old = NULL;
121 static BYTE vga_text_width;
122 static BYTE vga_text_height;
123 static BYTE vga_text_x;
124 static BYTE vga_text_y;
125 static BOOL vga_text_console;
128 * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
129 * indexed registers. These ports are used to select VGA controller
130 * subregister that can be written to or read from using ports 0x3c1,
131 * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
132 * stored in variables vga_index_*.
134 * Port 0x3c0 is special because it is both index and
135 * data-write register. Flip-flop vga_address_3c0 tells whether
136 * the port acts currently as an address register. Reading from port
137 * 0x3da resets the flip-flop to address mode.
139 static BYTE vga_index_3c0;
140 static BYTE vga_index_3c4;
141 static BYTE vga_index_3ce;
142 static BYTE vga_index_3d4;
143 static BOOL vga_address_3c0 = TRUE;
146 * This mutex is used to protect VGA state during asynchronous
147 * screen updates (see VGA_Poll). It makes sure that VGA state changes
148 * are atomic and the user interface is protected from flicker and
149 * corruption.
151 * The mutex actually serializes VGA operations and the screen update.
152 * Which means that whenever VGA_Poll occurs, application stalls if it
153 * tries to modify VGA state. This is not how real VGA adapters work,
154 * but it makes timing and correctness issues much easier to deal with.
156 static CRITICAL_SECTION vga_lock;
157 static CRITICAL_SECTION_DEBUG critsect_debug =
159 0, 0, &vga_lock,
160 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
161 0, 0, { (DWORD_PTR)(__FILE__ ": vga_lock") }
163 static CRITICAL_SECTION vga_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
165 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
166 static DirectDrawCreateProc pDirectDrawCreate;
168 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
170 static HWND vga_hwnd = NULL;
173 * CGA palette 1
175 static PALETTEENTRY cga_palette1[] = {
176 {0x00, 0x00, 0x00}, /* 0 - Black */
177 {0x00, 0xAA, 0xAA}, /* 1 - Cyan */
178 {0xAA, 0x00, 0xAA}, /* 2 - Magenta */
179 {0xAA, 0xAA, 0xAA} /* 3 - White */
183 * CGA palette 1 in the bright variant
184 * intensities, when signalled to do so
185 * in register 0x3d9
187 static PALETTEENTRY cga_palette1_bright[] = {
188 {0x00, 0x00, 0x00}, /* 0 - Black */
189 {0x55, 0xFF, 0xFF}, /* 1 - Light cyan */
190 {0xFF, 0x55, 0xFF}, /* 2 - Light magenta */
191 {0xFF, 0xFF, 0xFF}, /* 3 - Bright White */
195 * CGA palette 2
197 static PALETTEENTRY cga_palette2[] = {
198 {0x00, 0x00, 0x00}, /* 0 - Black */
199 {0x00, 0xAA, 0x00}, /* 1 - Green */
200 {0xAA, 0x00, 0x00}, /* 2 - Red */
201 {0xAA, 0x55, 0x00} /* 3 - Brown */
205 * CGA palette 2 in the bright variant
206 * intensities, when signalled to do so
207 * in register 0x3d9
209 static PALETTEENTRY cga_palette2_bright[] = {
210 {0x00, 0x00, 0x00}, /* 0 - Black */
211 {0x55, 0xFF, 0x55}, /* 1 - Light green */
212 {0xFF, 0x55, 0x55}, /* 2 - Light red */
213 {0xFF, 0xFF, 0x55}, /* 3 - Yellow */
217 * VGA Palette Registers, in actual 18 bit color
218 * port 3C0H - 6 bit rgbRGB format
220 * 16 color accesses will use these pointers and insert
221 * entries from the 64-color palette (mode 18) into the default
222 * palette. --Robert 'Admiral' Coeyman
225 static char vga_16_palette[17]={
226 0x00, /* 0 - Black */
227 0x01, /* 1 - Blue */
228 0x02, /* 2 - Green */
229 0x03, /* 3 - Cyan */
230 0x04, /* 4 - Red */
231 0x05, /* 5 - Magenta */
232 0x14, /* 6 - Brown */
233 0x07, /* 7 - White (Light gray) */
234 0x38, /* 8 - Dark gray */
235 0x39, /* 9 - Light blue */
236 0x3a, /* A - Light green */
237 0x3b, /* B - Light cyan */
238 0x3c, /* C - Light red */
239 0x3d, /* D - Light magenta */
240 0x3e, /* E - Yellow */
241 0x3f, /* F - Bright White (White) */
242 0x00 /* Border Color */
246 * Mode 19 Default Color Register Setting
247 * DAC palette registers, converted from actual 18 bit color to 24.
249 static PALETTEENTRY vga_def_palette[256]={
250 /* red green blue */
251 /* 16 colors in IRGB values */
252 {0x00, 0x00, 0x00}, /* 0 (0) - Black */
253 {0x00, 0x00, 0xAA}, /* 1 (1) - Blue */
254 {0x00, 0xAA, 0x00}, /* 2 (2) - Green */
255 {0x00, 0xAA, 0xAA}, /* 3 (3) - Cyan */
256 {0xAA, 0x00, 0x00}, /* 4 (4) - Red */
257 {0xAA, 0x00, 0xAA}, /* 5 (5) - Magenta */
258 {0xAA, 0x55, 0x00}, /* 6 (6) - Brown */
259 {0xAA, 0xAA, 0xAA}, /* 7 (7) - White (Light gray) */
260 {0x55, 0x55, 0x55}, /* 8 (8) - Dark gray */
261 {0x55, 0x55, 0xFF}, /* 9 (9) - Light blue */
262 {0x55, 0xFF, 0x55}, /* 10 (A) - Light green */
263 {0x55, 0xFF, 0xFF}, /* 11 (B) - Light cyan */
264 {0xFF, 0x55, 0x55}, /* 12 (C) - Light red */
265 {0xFF, 0x55, 0xFF}, /* 13 (D) - Light magenta */
266 {0xFF, 0xFF, 0x55}, /* 14 (E) - Yellow */
267 {0xFF, 0xFF, 0xFF}, /* 15 (F) - Bright White (White) */
268 /* 16 shades of gray */
269 {0x00, 0x00, 0x00}, /* 16 (10) */
270 {0x10, 0x10, 0x10}, /* 17 (11) */
271 {0x20, 0x20, 0x20}, /* 18 (12) */
272 {0x35, 0x35, 0x35}, /* 19 (13) */
273 {0x45, 0x45, 0x45}, /* 20 (14) */
274 {0x55, 0x55, 0x55}, /* 21 (15) */
275 {0x65, 0x65, 0x65}, /* 22 (16) */
276 {0x75, 0x75, 0x75}, /* 23 (17) */
277 {0x8A, 0x8A, 0x8A}, /* 24 (18) */
278 {0x9A, 0x9A, 0x9A}, /* 25 (19) */
279 {0xAA, 0xAA, 0xAA}, /* 26 (1A) */
280 {0xBA, 0xBA, 0xBA}, /* 27 (1B) */
281 {0xCA, 0xCA, 0xCA}, /* 28 (1C) */
282 {0xDF, 0xDF, 0xDF}, /* 29 (1D) */
283 {0xEF, 0xEF, 0xEF}, /* 30 (1E) */
284 {0xFF, 0xFF, 0xFF}, /* 31 (1F) */
285 /* High Intensity group - 72 colors in 1/3 saturation groups (20H-37H high) */
286 {0x00, 0x00, 0xFF}, /* 32 (20) */
287 {0x41, 0x00, 0xFF}, /* 33 (21) */
288 {0x82, 0x00, 0xFF}, /* 34 (22) */
289 {0xBE, 0x00, 0xFF}, /* 35 (23) */
290 {0xFF, 0x00, 0xFF}, /* 36 (24) */
291 {0xFF, 0x00, 0xBE}, /* 37 (25) */
292 {0xFF, 0x00, 0x82}, /* 38 (26) */
293 {0xFF, 0x00, 0x41}, /* 39 (27) */
294 {0xFF, 0x00, 0x00}, /* 40 (28) */
295 {0xFF, 0x41, 0x00}, /* 41 (29) */
296 {0xFF, 0x82, 0x00}, /* 42 (2A) */
297 {0xFF, 0xBE, 0x00}, /* 43 (2B) */
298 {0xFF, 0xFF, 0x00}, /* 44 (2C) */
299 {0xBE, 0xFF, 0x00}, /* 45 (2D) */
300 {0x82, 0xFF, 0x00}, /* 46 (2E) */
301 {0x41, 0xFF, 0x00}, /* 47 (2F) */
302 {0x00, 0xFF, 0x00}, /* 48 (30) */
303 {0x00, 0xFF, 0x41}, /* 49 (31) */
304 {0x00, 0xFF, 0x82}, /* 50 (32) */
305 {0x00, 0xFF, 0xBE}, /* 51 (33) */
306 {0x00, 0xFF, 0xFF}, /* 52 (34) */
307 {0x00, 0xBE, 0xFF}, /* 53 (35) */
308 {0x00, 0x82, 0xFF}, /* 54 (36) */
309 {0x00, 0x41, 0xFF}, /* 55 (37) */
310 /* High Intensity group - 72 colors in 2/3 saturation groups (38H-4FH moderate) */
311 {0x82, 0x82, 0xFF}, /* 56 (38) */
312 {0x9E, 0x82, 0xFF}, /* 57 (39) */
313 {0xBE, 0x82, 0xFF}, /* 58 (3A) */
314 {0xDF, 0x82, 0xFF}, /* 59 (3B) */
315 {0xFF, 0x82, 0xFF}, /* 60 (3C) */
316 {0xFF, 0x82, 0xDF}, /* 61 (3D) */
317 {0xFF, 0x82, 0xBE}, /* 62 (3E) */
318 {0xFF, 0x82, 0x9E}, /* 63 (3F) */
319 {0xFF, 0x82, 0x82}, /* 64 (40) */
320 {0xFF, 0x9E, 0x82}, /* 65 (41) */
321 {0xFF, 0xBE, 0x82}, /* 66 (42) */
322 {0xFF, 0xDF, 0x82}, /* 67 (43) */
323 {0xFF, 0xFF, 0x82}, /* 68 (44) */
324 {0xDF, 0xFF, 0x82}, /* 69 (45) */
325 {0xBE, 0xFF, 0x82}, /* 70 (46) */
326 {0x9E, 0xFF, 0x82}, /* 71 (47) */
327 {0x82, 0xFF, 0x82}, /* 72 (48) */
328 {0x82, 0xFF, 0x9E}, /* 73 (49) */
329 {0x82, 0xFF, 0xBE}, /* 74 (4A) */
330 {0x82, 0xFF, 0xDF}, /* 75 (4B) */
331 {0x82, 0xFF, 0xFF}, /* 76 (4C) */
332 {0x82, 0xDF, 0xFF}, /* 77 (4D) */
333 {0x82, 0xBE, 0xFF}, /* 78 (4E) */
334 {0x82, 0x9E, 0xFF}, /* 79 (4F) */
335 /* High Intensity group - 72 colors in 3/3 saturation groups (50H-67H low) */
336 {0xBA, 0xBA, 0xFF}, /* 80 (50) */
337 {0xCA, 0xBA, 0xFF}, /* 81 (51) */
338 {0xDF, 0xBA, 0xFF}, /* 82 (52) */
339 {0xEF, 0xBA, 0xFF}, /* 83 (53) */
340 {0xFF, 0xBA, 0xFF}, /* 84 (54) */
341 {0xFF, 0xBA, 0xEF}, /* 85 (55) */
342 {0xFF, 0xBA, 0xDF}, /* 86 (56) */
343 {0xFF, 0xBA, 0xCA}, /* 87 (57) */
344 {0xFF, 0xBA, 0xBA}, /* 88 (58) */
345 {0xFF, 0xCA, 0xBA}, /* 89 (59) */
346 {0xFF, 0xDF, 0xBA}, /* 90 (5A) */
347 {0xFF, 0xEF, 0xBA}, /* 91 (5B) */
348 {0xFF, 0xFF, 0xBA}, /* 92 (5C) */
349 {0xEF, 0xFF, 0xBA}, /* 93 (5D) */
350 {0xDF, 0xFF, 0xBA}, /* 94 (5E) */
351 {0xCA, 0xFF, 0xBA}, /* 95 (5F) */
352 {0xBA, 0xFF, 0xBA}, /* 96 (60) */
353 {0xBA, 0xFF, 0xCA}, /* 97 (61) */
354 {0xBA, 0xFF, 0xDF}, /* 98 (62) */
355 {0xBA, 0xFF, 0xEF}, /* 99 (63) */
356 {0xBA, 0xFF, 0xFF}, /* 100 (64) */
357 {0xBA, 0xEF, 0xFF}, /* 101 (65) */
358 {0xBA, 0xDF, 0xFF}, /* 102 (66) */
359 {0xBA, 0xCA, 0xFF}, /* 103 (67) */
360 /* Medium Intensity group - 72 colors in 1/3 saturation groups (68H-7FH high) */
361 {0x00, 0x00, 0x71}, /* 104 (68) */
362 {0x1C, 0x00, 0x71}, /* 105 (69) */
363 {0x39, 0x00, 0x71}, /* 106 (6A) */
364 {0x55, 0x00, 0x71}, /* 107 (6B) */
365 {0x71, 0x00, 0x71}, /* 108 (6C) */
366 {0x71, 0x00, 0x55}, /* 109 (6D) */
367 {0x71, 0x00, 0x39}, /* 110 (6E) */
368 {0x71, 0x00, 0x1C}, /* 111 (6F) */
369 {0x71, 0x00, 0x00}, /* 112 (70) */
370 {0x71, 0x1C, 0x00}, /* 113 (71) */
371 {0x71, 0x39, 0x00}, /* 114 (72) */
372 {0x71, 0x55, 0x00}, /* 115 (73) */
373 {0x71, 0x71, 0x00}, /* 116 (74) */
374 {0x55, 0x71, 0x00}, /* 117 (75) */
375 {0x39, 0x71, 0x00}, /* 118 (76) */
376 {0x1C, 0x71, 0x00}, /* 119 (77) */
377 {0x00, 0x71, 0x00}, /* 120 (78) */
378 {0x00, 0x71, 0x1C}, /* 121 (79) */
379 {0x00, 0x71, 0x39}, /* 122 (7A) */
380 {0x00, 0x71, 0x55}, /* 123 (7B) */
381 {0x00, 0x71, 0x71}, /* 124 (7C) */
382 {0x00, 0x55, 0x71}, /* 125 (7D) */
383 {0x00, 0x39, 0x71}, /* 126 (7E) */
384 {0x00, 0x1C, 0x71}, /* 127 (7F) */
385 /* Medium Intensity group - 72 colors in 2/3 saturation groups (80H-97H moderate) */
386 {0x39, 0x39, 0x71}, /* 128 (80) */
387 {0x45, 0x39, 0x71}, /* 129 (81) */
388 {0x55, 0x39, 0x71}, /* 130 (82) */
389 {0x61, 0x39, 0x71}, /* 131 (83) */
390 {0x71, 0x39, 0x71}, /* 132 (84) */
391 {0x71, 0x39, 0x61}, /* 133 (85) */
392 {0x71, 0x39, 0x55}, /* 134 (86) */
393 {0x71, 0x39, 0x45}, /* 135 (87) */
394 {0x71, 0x39, 0x39}, /* 136 (88) */
395 {0x71, 0x45, 0x39}, /* 137 (89) */
396 {0x71, 0x55, 0x39}, /* 138 (8A) */
397 {0x71, 0x61, 0x39}, /* 139 (8B) */
398 {0x71, 0x71, 0x39}, /* 140 (8C) */
399 {0x61, 0x71, 0x39}, /* 141 (8D) */
400 {0x55, 0x71, 0x39}, /* 142 (8E) */
401 {0x45, 0x71, 0x39}, /* 143 (8F) */
402 {0x39, 0x71, 0x39}, /* 144 (90) */
403 {0x39, 0x71, 0x45}, /* 145 (91) */
404 {0x39, 0x71, 0x55}, /* 146 (92) */
405 {0x39, 0x71, 0x61}, /* 147 (93) */
406 {0x39, 0x71, 0x71}, /* 148 (94) */
407 {0x39, 0x61, 0x71}, /* 149 (95) */
408 {0x39, 0x55, 0x71}, /* 150 (96) */
409 {0x39, 0x45, 0x71}, /* 151 (97) */
410 /* Medium Intensity group - 72 colors in 3/3 saturation groups (98H-AFH low) */
411 {0x51, 0x51, 0x71}, /* 152 (98) */
412 {0x59, 0x51, 0x71}, /* 153 (99) */
413 {0x61, 0x51, 0x71}, /* 154 (9A) */
414 {0x69, 0x51, 0x71}, /* 155 (9B) */
415 {0x71, 0x51, 0x71}, /* 156 (9C) */
416 {0x71, 0x51, 0x69}, /* 157 (9D) */
417 {0x71, 0x51, 0x61}, /* 158 (9E) */
418 {0x71, 0x51, 0x59}, /* 159 (9F) */
419 {0x71, 0x51, 0x51}, /* 160 (A0) */
420 {0x71, 0x59, 0x51}, /* 161 (A1) */
421 {0x71, 0x61, 0x51}, /* 162 (A2) */
422 {0x71, 0x69, 0x51}, /* 163 (A3) */
423 {0x71, 0x71, 0x51}, /* 164 (A4) */
424 {0x69, 0x71, 0x51}, /* 165 (A5) */
425 {0x61, 0x71, 0x51}, /* 166 (A6) */
426 {0x59, 0x71, 0x51}, /* 167 (A7) */
427 {0x51, 0x71, 0x51}, /* 168 (A8) */
428 {0x51, 0x71, 0x59}, /* 169 (A9) */
429 {0x51, 0x71, 0x61}, /* 170 (AA) */
430 {0x51, 0x71, 0x69}, /* 171 (AB) */
431 {0x51, 0x71, 0x71}, /* 172 (AC) */
432 {0x51, 0x69, 0x71}, /* 173 (AD) */
433 {0x51, 0x61, 0x71}, /* 174 (AE) */
434 {0x51, 0x59, 0x71}, /* 175 (AF) */
435 /* Low Intensity group - 72 colors in 1/3 saturation groups (B0H-C7H high) */
436 {0x00, 0x00, 0x41}, /* 176 (B0) */
437 {0x10, 0x00, 0x41}, /* 177 (B1) */
438 {0x20, 0x00, 0x41}, /* 178 (B2) */
439 {0x31, 0x00, 0x41}, /* 179 (B3) */
440 {0x41, 0x00, 0x41}, /* 180 (B4) */
441 {0x41, 0x00, 0x31}, /* 181 (B5) */
442 {0x41, 0x00, 0x20}, /* 182 (B6) */
443 {0x41, 0x00, 0x10}, /* 183 (B7) */
444 {0x41, 0x00, 0x00}, /* 184 (B8) */
445 {0x41, 0x10, 0x00}, /* 185 (B9) */
446 {0x41, 0x20, 0x00}, /* 186 (BA) */
447 {0x41, 0x31, 0x00}, /* 187 (BB) */
448 {0x41, 0x41, 0x00}, /* 188 (BC) */
449 {0x31, 0x41, 0x00}, /* 189 (BD) */
450 {0x20, 0x41, 0x00}, /* 190 (BE) */
451 {0x10, 0x41, 0x00}, /* 191 (BF) */
452 {0x00, 0x41, 0x00}, /* 192 (C0) */
453 {0x00, 0x41, 0x10}, /* 193 (C1) */
454 {0x00, 0x41, 0x20}, /* 194 (C2) */
455 {0x00, 0x41, 0x31}, /* 195 (C3) */
456 {0x00, 0x41, 0x41}, /* 196 (C4) */
457 {0x00, 0x31, 0x41}, /* 197 (C5) */
458 {0x00, 0x20, 0x41}, /* 198 (C6) */
459 {0x00, 0x10, 0x41}, /* 199 (C7) */
460 /* Low Intensity group - 72 colors in 2/3 saturation groups (C8H-DFH moderate) */
461 {0x20, 0x20, 0x41}, /* 200 (C8) */
462 {0x28, 0x20, 0x41}, /* 201 (C9) */
463 {0x31, 0x20, 0x41}, /* 202 (CA) */
464 {0x39, 0x20, 0x41}, /* 203 (CB) */
465 {0x41, 0x20, 0x41}, /* 204 (CC) */
466 {0x41, 0x20, 0x39}, /* 205 (CD) */
467 {0x41, 0x20, 0x31}, /* 206 (CE) */
468 {0x41, 0x20, 0x28}, /* 207 (CF) */
469 {0x41, 0x20, 0x20}, /* 208 (D0) */
470 {0x41, 0x28, 0x20}, /* 209 (D1) */
471 {0x41, 0x31, 0x20}, /* 210 (D2) */
472 {0x41, 0x39, 0x20}, /* 211 (D3) */
473 {0x41, 0x41, 0x20}, /* 212 (D4) */
474 {0x39, 0x41, 0x20}, /* 213 (D5) */
475 {0x31, 0x41, 0x20}, /* 214 (D6) */
476 {0x28, 0x41, 0x20}, /* 215 (D7) */
477 {0x20, 0x41, 0x20}, /* 216 (D8) */
478 {0x20, 0x41, 0x28}, /* 217 (D9) */
479 {0x20, 0x41, 0x31}, /* 218 (DA) */
480 {0x20, 0x41, 0x39}, /* 219 (DB) */
481 {0x20, 0x41, 0x41}, /* 220 (DC) */
482 {0x20, 0x39, 0x41}, /* 221 (DD) */
483 {0x20, 0x31, 0x41}, /* 222 (DE) */
484 {0x20, 0x28, 0x41}, /* 223 (DF) */
485 /* Low Intensity group - 72 colors in 3/3 saturation groups (E0H-F7H low) */
486 {0x2D, 0x2D, 0x41}, /* 223 (E0) */
487 {0x31, 0x2D, 0x41}, /* 224 (E1) */
488 {0x35, 0x2D, 0x41}, /* 225 (E2) */
489 {0x3D, 0x2D, 0x41}, /* 226 (E3) */
490 {0x41, 0x2D, 0x41}, /* 227 (E4) */
491 {0x41, 0x2D, 0x3D}, /* 228 (E5) */
492 {0x41, 0x2D, 0x35}, /* 229 (E6) */
493 {0x41, 0x2D, 0x31}, /* 230 (E7) */
494 {0x41, 0x2D, 0x2D}, /* 231 (E8) */
495 {0x41, 0x31, 0x2D}, /* 232 (E9) */
496 {0x41, 0x35, 0x2D}, /* 233 (EA) */
497 {0x41, 0x3D, 0x2D}, /* 234 (EB) */
498 {0x41, 0x41, 0x2D}, /* 235 (EC) */
499 {0x3D, 0x41, 0x2D}, /* 236 (ED) */
500 {0x35, 0x41, 0x2D}, /* 237 (EE) */
501 {0x31, 0x41, 0x2D}, /* 238 (EF) */
502 {0x2D, 0x41, 0x2D}, /* 239 (F0) */
503 {0x2D, 0x41, 0x31}, /* 240 (F1) */
504 {0x2D, 0x41, 0x35}, /* 241 (F2) */
505 {0x2D, 0x41, 0x3D}, /* 242 (F3) */
506 {0x2D, 0x41, 0x41}, /* 243 (F4) */
507 {0x2D, 0x3D, 0x41}, /* 244 (F5) */
508 {0x2D, 0x35, 0x41}, /* 245 (F6) */
509 {0x2D, 0x31, 0x41}, /* 246 (F7) */
510 /* Fill up remainder of palettes with black */
511 {0,0,0}
515 * Mode 18 Default Color Register Setting
516 * DAC palette registers, converted from actual 18 bit color to 24.
518 * This palette is the dos default, converted from 18 bit color to 24.
519 * It contains only 64 entries of colors--all others are zeros.
520 * --Robert 'Admiral' Coeyman
522 static PALETTEENTRY vga_def64_palette[256]={
523 /* red green blue */
524 {0x00, 0x00, 0x00}, /* 0x00 Black */
525 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
526 {0x00, 0xaa, 0x00}, /* 0x02 Green */
527 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
528 {0xaa, 0x00, 0x00}, /* 0x04 Red */
529 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
530 {0xaa, 0xaa, 0x00}, /* 0x06 */
531 {0xaa, 0xaa, 0xaa}, /* 0x07 White (Light Gray) */
532 {0x00, 0x00, 0x55}, /* 0x08 */
533 {0x00, 0x00, 0xff}, /* 0x09 */
534 {0x00, 0xaa, 0x55}, /* 0x0a */
535 {0x00, 0xaa, 0xff}, /* 0x0b */
536 {0xaa, 0x00, 0x55}, /* 0x0c */
537 {0xaa, 0x00, 0xff}, /* 0x0d */
538 {0xaa, 0xaa, 0x55}, /* 0x0e */
539 {0xaa, 0xaa, 0xff}, /* 0x0f */
540 {0x00, 0x55, 0x00}, /* 0x10 */
541 {0x00, 0x55, 0xaa}, /* 0x11 */
542 {0x00, 0xff, 0x00}, /* 0x12 */
543 {0x00, 0xff, 0xaa}, /* 0x13 */
544 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
545 {0xaa, 0x55, 0xaa}, /* 0x15 */
546 {0xaa, 0xff, 0x00}, /* 0x16 */
547 {0xaa, 0xff, 0xaa}, /* 0x17 */
548 {0x00, 0x55, 0x55}, /* 0x18 */
549 {0x00, 0x55, 0xff}, /* 0x19 */
550 {0x00, 0xff, 0x55}, /* 0x1a */
551 {0x00, 0xff, 0xff}, /* 0x1b */
552 {0xaa, 0x55, 0x55}, /* 0x1c */
553 {0xaa, 0x55, 0xff}, /* 0x1d */
554 {0xaa, 0xff, 0x55}, /* 0x1e */
555 {0xaa, 0xff, 0xff}, /* 0x1f */
556 {0x55, 0x00, 0x00}, /* 0x20 */
557 {0x55, 0x00, 0xaa}, /* 0x21 */
558 {0x55, 0xaa, 0x00}, /* 0x22 */
559 {0x55, 0xaa, 0xaa}, /* 0x23 */
560 {0xff, 0x00, 0x00}, /* 0x24 */
561 {0xff, 0x00, 0xaa}, /* 0x25 */
562 {0xff, 0xaa, 0x00}, /* 0x26 */
563 {0xff, 0xaa, 0xaa}, /* 0x27 */
564 {0x55, 0x00, 0x55}, /* 0x28 */
565 {0x55, 0x00, 0xff}, /* 0x29 */
566 {0x55, 0xaa, 0x55}, /* 0x2a */
567 {0x55, 0xaa, 0xff}, /* 0x2b */
568 {0xff, 0x00, 0x55}, /* 0x2c */
569 {0xff, 0x00, 0xff}, /* 0x2d */
570 {0xff, 0xaa, 0x55}, /* 0x2e */
571 {0xff, 0xaa, 0xff}, /* 0x2f */
572 {0x55, 0x55, 0x00}, /* 0x30 */
573 {0x55, 0x55, 0xaa}, /* 0x31 */
574 {0x55, 0xff, 0x00}, /* 0x32 */
575 {0x55, 0xff, 0xaa}, /* 0x33 */
576 {0xff, 0x55, 0x00}, /* 0x34 */
577 {0xff, 0x55, 0xaa}, /* 0x35 */
578 {0xff, 0xff, 0x00}, /* 0x36 */
579 {0xff, 0xff, 0xaa}, /* 0x37 */
580 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
581 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
582 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
583 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
584 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
585 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
586 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
587 {0xff, 0xff, 0xff}, /* 0x3f White */
588 {0,0,0} /* The next 192 entries are all zeros */
591 static HANDLE VGA_timer;
592 static HANDLE VGA_timer_thread;
594 /* set the timer rate; called in the polling thread context */
595 static void CALLBACK set_timer_rate( ULONG_PTR arg )
597 LARGE_INTEGER when;
599 when.u.LowPart = when.u.HighPart = 0;
600 SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
603 static DWORD CALLBACK VGA_TimerThread( void *dummy )
605 for (;;) SleepEx( INFINITE, TRUE );
606 return 0;
609 static void VGA_DeinstallTimer(void)
611 if (VGA_timer_thread)
614 * Make sure the update thread is not holding
615 * system resources when we kill it.
617 * Now, we only need to worry about update thread
618 * getting terminated while in EnterCriticalSection
619 * or WaitForMultipleObjectsEx.
621 * FIXME: Is this a problem?
623 EnterCriticalSection(&vga_lock);
625 CancelWaitableTimer( VGA_timer );
626 CloseHandle( VGA_timer );
627 TerminateThread( VGA_timer_thread, 0 );
628 CloseHandle( VGA_timer_thread );
629 VGA_timer_thread = 0;
631 LeaveCriticalSection(&vga_lock);
634 * Synchronize display. This makes sure that
635 * changes to display become visible even if program
636 * terminates before update thread had time to run.
638 VGA_Poll( 0, 0, 0 );
642 static void VGA_InstallTimer(unsigned Rate)
644 if (!VGA_timer_thread)
646 VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
647 VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
649 QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
652 static BOOL VGA_IsTimerRunning(void)
654 return VGA_timer_thread ? TRUE : FALSE;
657 static HANDLE VGA_AlphaConsole(void)
659 /* this assumes that no Win32 redirection has taken place, but then again,
660 * only 16-bit apps are likely to use this part of Wine... */
661 return GetStdHandle(STD_OUTPUT_HANDLE);
664 static char*VGA_AlphaBuffer(void)
666 return (char *)0xb8000;
669 /*** GRAPHICS MODE ***/
671 typedef struct {
672 unsigned Xres, Yres, Depth;
673 int ret;
674 } ModeSet;
677 /**********************************************************************
678 * VGA_SyncWindow
680 * Copy VGA window into framebuffer (if argument is TRUE) or
681 * part of framebuffer into VGA window (if argument is FALSE).
683 static void VGA_SyncWindow( BOOL target_is_fb )
685 int size = vga_fb_window_size;
687 /* Window does not overlap framebuffer. */
688 if (vga_fb_window >= vga_fb_size)
689 return;
691 /* Check if window overlaps framebuffer only partially. */
692 if (vga_fb_size - vga_fb_window < vga_fb_window_size)
693 size = vga_fb_size - vga_fb_window;
695 if (target_is_fb)
696 memmove( vga_fb_data + vga_fb_window, vga_fb_window_data, size );
697 else
698 memmove( vga_fb_window_data, vga_fb_data + vga_fb_window, size );
702 static void WINAPI VGA_DoExit(ULONG_PTR arg)
704 VGA_DeinstallTimer();
705 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
706 IDirectDrawSurface_Release(lpddsurf);
707 lpddsurf=NULL;
708 IDirectDrawPalette_Release(lpddpal);
709 lpddpal=NULL;
710 IDirectDraw_Release(lpddraw);
711 lpddraw=NULL;
714 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
716 HRESULT res;
717 ModeSet *par = (ModeSet *)arg;
718 par->ret=1;
720 if (lpddraw) VGA_DoExit(0);
721 if (!lpddraw) {
722 if (!pDirectDrawCreate)
724 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
725 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
726 if (!pDirectDrawCreate) {
727 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
728 return;
731 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
732 if (!lpddraw) {
733 ERR("DirectDraw is not available (res = 0x%x)\n",res);
734 return;
736 if (!vga_hwnd) {
737 vga_hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",
738 WS_POPUP|WS_VISIBLE|SS_NOTIFY,0,0,
739 par->Xres,par->Yres,0,0,0,NULL);
740 if (!vga_hwnd) {
741 ERR("Failed to create user window.\n");
742 IDirectDraw_Release(lpddraw);
743 lpddraw=NULL;
744 return;
747 else
748 SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
750 res=IDirectDraw_SetCooperativeLevel(lpddraw,vga_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE);
751 if (res != S_OK) {
752 ERR("Could not set cooperative level to exclusive (0x%x)\n",res);
755 res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth);
756 if (res != S_OK) {
757 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = 0x%x!\n",par->Xres,par->Yres,par->Depth,res);
758 IDirectDraw_Release(lpddraw);
759 lpddraw=NULL;
760 return;
763 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
764 if (res != S_OK) {
765 ERR("Could not create palette (res = 0x%x)\n",res);
766 IDirectDraw_Release(lpddraw);
767 lpddraw=NULL;
768 return;
771 res=IDirectDrawPalette_SetEntries(lpddpal,0,0,4,vga_fb_palette);
772 if (res != S_OK) {
773 ERR("Could not set default palette entries (res = 0x%x)\n", res);
776 memset(&sdesc,0,sizeof(sdesc));
777 sdesc.dwSize=sizeof(sdesc);
778 sdesc.dwFlags = DDSD_CAPS;
779 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
780 res=IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL);
781 if (res != S_OK || !lpddsurf) {
782 ERR("DirectDraw surface is not available\n");
783 IDirectDraw_Release(lpddraw);
784 lpddraw=NULL;
785 return;
787 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
788 vga_retrace_vertical = vga_retrace_horizontal = FALSE;
789 /* poll every 20ms (50fps should provide adequate responsiveness) */
790 VGA_InstallTimer(20);
792 par->ret=0;
793 return;
796 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
798 ModeSet par;
799 int newSize;
801 vga_fb_width = Xres;
802 vga_fb_height = Yres;
803 vga_fb_depth = Depth;
804 vga_fb_offset = 0;
805 vga_fb_pitch = Xres * ((Depth + 7) / 8);
807 newSize = Xres * Yres * ((Depth + 7) / 8);
808 if(newSize < 256 * 1024)
809 newSize = 256 * 1024;
811 if(vga_fb_size < newSize) {
812 HeapFree(GetProcessHeap(), 0, vga_fb_data);
813 vga_fb_data = HeapAlloc(GetProcessHeap(), 0, newSize);
814 vga_fb_size = newSize;
817 if(Xres >= 640 || Yres >= 480) {
818 par.Xres = Xres;
819 par.Yres = Yres;
820 } else {
821 par.Xres = 640;
822 par.Yres = 480;
825 /* Setup window */
826 if(vga_fb_depth >= 8)
828 vga_fb_window_data = VGA_WINDOW_START;
829 vga_fb_window_size = VGA_WINDOW_SIZE;
830 vga_fb_palette = vga_def_palette;
832 else
834 vga_fb_window_data = CGA_WINDOW_START;
835 vga_fb_window_size = CGA_WINDOW_SIZE;
836 vga_fb_palette = cga_palette1;
837 vga_fb_palette_index = 0;
838 vga_fb_bright = 0;
841 /* Clean the HW buffer */
842 memset(vga_fb_window_data, 0x00, vga_fb_window_size);
844 /* Reset window start */
845 VGA_SetWindowStart(0);
847 par.Depth = (Depth < 8) ? 8 : Depth;
849 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
850 return par.ret;
853 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
855 if (!lpddraw) return 1;
856 if (!lpddsurf) return 1;
857 if (Height) *Height=sdesc.dwHeight;
858 if (Width) *Width=sdesc.dwWidth;
859 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
860 return 0;
863 static void VGA_Exit(void)
865 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
868 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
870 if (!lpddraw) return;
871 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
874 /* set a single [char wide] color in 16 color mode. */
875 void VGA_SetColor16(int reg,int color)
877 PALETTEENTRY *pal;
879 if (!lpddraw) return;
880 pal= &vga_def64_palette[color];
881 IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
882 vga_16_palette[reg]=(char)color;
885 /* Get a single [char wide] color in 16 color mode. */
886 char VGA_GetColor16(int reg)
889 if (!lpddraw) return 0;
890 return vga_16_palette[reg];
893 /* set all 17 [char wide] colors at once in 16 color mode. */
894 void VGA_Set16Palette(char *Table)
896 PALETTEENTRY *pal;
897 int c;
899 if (!lpddraw) return; /* return if we're in text only mode */
900 memcpy( Table, vga_16_palette, 17 ); /* copy the entries into the table */
902 for (c=0; c<17; c++) { /* 17 entries */
903 pal= &vga_def64_palette[(int)vga_16_palette[c]]; /* get color */
904 IDirectDrawPalette_SetEntries(lpddpal,0,c,1,pal); /* set entry */
905 TRACE("Palette register %d set to %d\n",c,(int)vga_16_palette[c]);
906 } /* end of the counting loop */
909 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
910 void VGA_Get16Palette(char *Table)
913 if (!lpddraw) return; /* return if we're in text only mode */
914 memcpy( vga_16_palette, Table, 17 ); /* copy the entries into the table */
917 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
919 PALETTEENTRY pal[256];
920 int c;
922 if (!lpddraw) return;
923 for (c=0; c<len; c++) {
924 pal[c].peRed =color[c].rgbRed;
925 pal[c].peGreen=color[c].rgbGreen;
926 pal[c].peBlue =color[c].rgbBlue;
927 pal[c].peFlags=0;
929 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
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");
938 return NULL;
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)
960 return;
962 EnterCriticalSection(&vga_lock);
964 if(vga_fb_window == -1)
965 FIXME("Remove VGA memory emulation.\n");
966 else
967 VGA_SyncWindow( TRUE );
969 vga_fb_window = start;
971 if(vga_fb_window == -1)
972 FIXME("Install VGA memory emulation.\n");
973 else
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 /**********************************************************************
990 * VGA_DoShowMouse
992 * Callback for VGA_ShowMouse.
994 static void WINAPI VGA_DoShowMouse( ULONG_PTR show )
996 INT rv;
1000 rv = ShowCursor( show );
1002 while( show ? (rv < 0) : (rv >= 0) );
1006 /**********************************************************************
1007 * VGA_ShowMouse
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 )
1015 if (lpddraw)
1016 MZ_RunInThread( VGA_DoShowMouse, (ULONG_PTR)show );
1020 /**********************************************************************
1021 * VGA_UpdatePalette
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;
1042 else
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 /**********************************************************************
1059 * VGA_SetBright
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 /**********************************************************************
1073 * VGA_SetPaletteIndex
1075 * Select the index of the palette which is currently in use
1076 * This is a property of the CGA controller
1078 void VGA_SetPaletteIndex(unsigned index)
1080 TRACE("%i\n", index);
1082 /* Remember the palette index, which is only used by CGA for now */
1083 vga_fb_palette_index = index;
1086 /**********************************************************************
1087 * VGA_WritePixel
1089 * Write data to the framebuffer
1090 * This is a property of the CGA controller, but might be supported
1091 * later by other framebuffer types
1093 void VGA_WritePixel(unsigned color, unsigned page, unsigned col, unsigned row)
1095 int off;
1096 int bits;
1097 int pos;
1099 /* Calculate CGA byte offset */
1100 char *data = vga_fb_window_data;
1101 off = row & 1 ? (8 * 1024) : 0;
1102 off += (80 * (row/2));
1103 off += col/4;
1105 /* Calculate bits offset */
1106 pos = 6 - (col%4 * 2);
1108 /* Clear current data */
1109 bits = 0x03 << pos;
1110 data[off] &= ~bits;
1112 /* Set new data */
1113 bits = color << pos;
1114 data[off] |= bits;
1117 /*** TEXT MODE ***/
1119 /* prepare the text mode video memory copy that is used to only
1120 * update the video memory line that did get updated. */
1121 static void VGA_PrepareVideoMemCopy(unsigned Xres, unsigned Yres)
1123 char *p, *p2;
1124 unsigned int i;
1127 * Allocate space for char + attr.
1130 if (vga_text_old)
1131 vga_text_old = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1132 vga_text_old, Xres * Yres * 2 );
1133 else
1134 vga_text_old = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
1135 Xres * Yres * 2 );
1136 p = VGA_AlphaBuffer();
1137 p2 = vga_text_old;
1139 /* make sure the video mem copy contains the exact opposite of our
1140 * actual text mode memory area to make sure the screen
1141 * does get updated fully initially */
1142 for (i=0; i < Xres*Yres*2; i++)
1143 *p2++ = *p++ ^ 0xff; /* XOR it */
1146 /**********************************************************************
1147 * VGA_SetAlphaMode
1149 * Set VGA emulation to text mode.
1151 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
1153 VGA_Exit();
1154 VGA_DeinstallTimer();
1156 VGA_PrepareVideoMemCopy(Xres, Yres);
1157 vga_text_width = Xres;
1158 vga_text_height = Yres;
1160 if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
1161 VGA_SetCursorPos(0,0);
1163 if(vga_text_console) {
1164 COORD size;
1165 size.X = Xres;
1166 size.Y = Yres;
1167 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
1169 /* poll every 30ms (33fps should provide adequate responsiveness) */
1170 VGA_InstallTimer(30);
1174 /**********************************************************************
1175 * VGA_InitAlphaMode
1177 * Initialize VGA text mode handling and return default text mode.
1178 * This function does not set VGA emulation to text mode.
1180 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
1182 CONSOLE_SCREEN_BUFFER_INFO info;
1184 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
1186 vga_text_console = TRUE;
1187 vga_text_x = info.dwCursorPosition.X;
1188 vga_text_y = info.dwCursorPosition.Y;
1189 vga_text_attr = info.wAttributes;
1190 *Xres = info.dwSize.X;
1191 *Yres = info.dwSize.Y;
1193 else
1195 vga_text_console = FALSE;
1196 vga_text_x = 0;
1197 vga_text_y = 0;
1198 vga_text_attr = 0x0f;
1199 *Xres = 80;
1200 *Yres = 25;
1204 /**********************************************************************
1205 * VGA_GetAlphaMode
1207 * Get current text mode. Returns TRUE and sets resolution if
1208 * any VGA text mode has been initialized.
1210 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
1212 if (vga_text_width != 0 && vga_text_height != 0) {
1213 *Xres = vga_text_width;
1214 *Yres = vga_text_height;
1215 return TRUE;
1216 } else
1217 return FALSE;
1220 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
1222 CONSOLE_CURSOR_INFO cci;
1224 /* standard cursor settings:
1225 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
1227 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
1228 cci.dwSize = ((end & 0x1f) - (start_options & 0x1f))/0x0e * 100;
1229 if (!cci.dwSize) cci.dwSize++; /* NULL cursor would make SCCI() fail ! */
1230 cci.bVisible = ((start_options & 0x60) != 0x20); /* invisible ? */
1232 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
1235 void VGA_SetCursorPos(unsigned X,unsigned Y)
1237 vga_text_x = X;
1238 vga_text_y = Y;
1241 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
1243 if (X) *X = vga_text_x;
1244 if (Y) *Y = vga_text_y;
1247 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
1249 char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
1250 dat[0] = ascii;
1251 if (attr>=0)
1252 dat[1] = attr;
1255 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
1257 EnterCriticalSection(&vga_lock);
1259 while (count--)
1260 VGA_PutCharAt(X + count, Y, ch, attr);
1262 LeaveCriticalSection(&vga_lock);
1265 void VGA_PutChar(BYTE ascii)
1267 DWORD w;
1269 EnterCriticalSection(&vga_lock);
1271 switch(ascii) {
1272 case '\b':
1273 if (vga_text_x)
1275 vga_text_x--;
1276 VGA_PutCharAt(vga_text_x, vga_text_y, ' ', 0);
1278 break;
1280 case '\t':
1281 vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
1282 break;
1284 case '\n':
1285 vga_text_y++;
1286 vga_text_x = 0;
1287 break;
1289 case '\a':
1290 break;
1292 case '\r':
1293 vga_text_x = 0;
1294 break;
1296 default:
1297 VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
1298 vga_text_x++;
1301 if (vga_text_x >= vga_text_width)
1303 vga_text_x = 0;
1304 vga_text_y++;
1307 if (vga_text_y >= vga_text_height)
1309 vga_text_y = vga_text_height - 1;
1310 VGA_ScrollUpText( 0, 0,
1311 vga_text_height - 1, vga_text_width - 1,
1312 1, vga_text_attr );
1316 * If we don't have a console, write directly to standard output.
1318 if(!vga_text_console)
1319 WriteFile(VGA_AlphaConsole(), &ascii, 1, &w, NULL);
1321 LeaveCriticalSection(&vga_lock);
1324 void VGA_SetTextAttribute(BYTE attr)
1326 vga_text_attr = attr;
1329 void VGA_ClearText(unsigned row1, unsigned col1,
1330 unsigned row2, unsigned col2,
1331 BYTE attr)
1333 unsigned x, y;
1335 EnterCriticalSection(&vga_lock);
1337 for(y=row1; y<=row2; y++)
1338 for(x=col1; x<=col2; x++)
1339 VGA_PutCharAt(x, y, 0x20, attr);
1341 LeaveCriticalSection(&vga_lock);
1344 void VGA_ScrollUpText(unsigned row1, unsigned col1,
1345 unsigned row2, unsigned col2,
1346 unsigned lines, BYTE attr)
1348 char *buffer = VGA_AlphaBuffer();
1349 unsigned y;
1351 EnterCriticalSection(&vga_lock);
1354 * Scroll buffer.
1356 for (y = row1; y <= row2 - lines; y++)
1357 memmove( buffer + col1 + y * vga_text_width * 2,
1358 buffer + col1 + (y + lines) * vga_text_width * 2,
1359 (col2 - col1 + 1) * 2 );
1362 * Fill exposed lines.
1364 for (y = max(row1, row2 - lines + 1); y <= row2; y++)
1365 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1367 LeaveCriticalSection(&vga_lock);
1370 void VGA_ScrollDownText(unsigned row1, unsigned col1,
1371 unsigned row2, unsigned col2,
1372 unsigned lines, BYTE attr)
1374 char *buffer = VGA_AlphaBuffer();
1375 unsigned y;
1377 EnterCriticalSection(&vga_lock);
1380 * Scroll buffer.
1382 for (y = row2; y >= row1 + lines; y--)
1383 memmove( buffer + col1 + y * vga_text_width * 2,
1384 buffer + col1 + (y - lines) * vga_text_width * 2,
1385 (col2 - col1 + 1) * 2 );
1388 * Fill exposed lines.
1390 for (y = row1; y <= min(row1 + lines - 1, row2); y++)
1391 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1393 LeaveCriticalSection(&vga_lock);
1396 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
1398 char *dat;
1400 dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
1402 *ascii = dat[0];
1403 *attr = dat[1];
1407 /*** CONTROL ***/
1409 /* FIXME: optimize by doing this only if the data has actually changed
1410 * (in a way similar to DIBSection, perhaps) */
1411 static void VGA_Poll_Graphics(void)
1413 unsigned int Pitch, Height, Width, X, Y;
1414 char *surf;
1415 char *dat = vga_fb_data + vga_fb_offset;
1416 int bpp = (vga_fb_depth + 7) / 8;
1418 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
1419 if (!surf) return;
1422 * Synchronize framebuffer contents.
1424 if (vga_fb_window != -1)
1425 VGA_SyncWindow( TRUE );
1428 * CGA framebuffer
1430 if(vga_fb_depth == 2 && vga_fb_width == 320 && vga_fb_height == 200){
1431 WORD off = 0;
1432 BYTE bits = 6;
1433 BYTE value;
1434 /* Go thru rows */
1435 for(Y=0; Y<vga_fb_height; Y++, surf+=(Pitch*2)){
1436 for(X=0; X<vga_fb_width; X++){
1437 off = Y & 1 ? (8 * 1024) : 0;
1438 off += (80 * (Y/2));
1439 off += X/4;
1440 value = (dat[off] >> bits) & 0x3;
1441 surf[(X*2)] = value;
1442 surf[(X*2)+1] = value;
1443 surf[(X*2)+Pitch] = value;
1444 surf[(X*2)+Pitch+1] = value;
1445 bits -= 2;
1446 bits &= 7;
1452 * Double VGA framebuffer (320x200 -> 640x400), if needed.
1454 else if(Height >= 2 * vga_fb_height && Width >= 2 * vga_fb_width && bpp == 1)
1455 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch*2,dat+=vga_fb_pitch)
1456 for (X=0; X<vga_fb_width; X++) {
1457 BYTE value = dat[X];
1458 surf[X*2] = value;
1459 surf[X*2+1] = value;
1460 surf[X*2+Pitch] = value;
1461 surf[X*2+Pitch+1] = value;
1463 else
1464 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch,dat+=vga_fb_pitch)
1465 memcpy(surf, dat, vga_fb_width * bpp);
1467 VGA_Unlock();
1470 static void VGA_Poll_Text(void)
1472 char *dat, *old, *p_line;
1473 unsigned int X, Y;
1474 CHAR_INFO ch[256]; /* that should suffice for the largest text width */
1475 COORD siz, off;
1476 SMALL_RECT dest;
1477 HANDLE con = VGA_AlphaConsole();
1478 BOOL linechanged = FALSE; /* video memory area differs from stored copy? */
1480 /* Synchronize cursor position. */
1481 off.X = vga_text_x;
1482 off.Y = vga_text_y;
1483 SetConsoleCursorPosition(con,off);
1485 dat = VGA_AlphaBuffer();
1486 old = vga_text_old; /* pointer to stored video mem copy */
1487 siz.X = vga_text_width; siz.Y = 1;
1488 off.X = 0; off.Y = 0;
1490 /* copy from virtual VGA frame buffer to console */
1491 for (Y=0; Y<vga_text_height; Y++) {
1492 linechanged = memcmp(dat, old, vga_text_width*2);
1493 if (linechanged)
1495 /*TRACE("line %d changed\n", Y);*/
1496 p_line = dat;
1497 for (X=0; X<vga_text_width; X++) {
1498 ch[X].Char.AsciiChar = *p_line++;
1499 /* WriteConsoleOutputA doesn't like "dead" chars */
1500 if (ch[X].Char.AsciiChar == '\0')
1501 ch[X].Char.AsciiChar = ' ';
1502 ch[X].Attributes = *p_line++;
1504 dest.Top=Y; dest.Bottom=Y;
1505 dest.Left=0; dest.Right=vga_text_width+1;
1506 WriteConsoleOutputA(con, ch, siz, off, &dest);
1507 memcpy(old, dat, vga_text_width*2);
1509 /* advance to next text line */
1510 dat += vga_text_width*2;
1511 old += vga_text_width*2;
1515 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
1517 EnterCriticalSection(&vga_lock);
1519 if (lpddraw)
1520 VGA_Poll_Graphics();
1521 else
1522 VGA_Poll_Text();
1525 * Fake start of retrace.
1527 vga_retrace_vertical = TRUE;
1529 LeaveCriticalSection(&vga_lock);
1532 static BYTE palreg,palcnt;
1533 static PALETTEENTRY paldat;
1535 void VGA_ioport_out( WORD port, BYTE val )
1537 switch (port) {
1538 /* General Register - Feature Control */
1539 case 0x3ba:
1540 FIXME("Unsupported VGA register: general register - feature control 0x%04x (value 0x%02x)\n", port, val);
1541 break;
1542 /* Attribute Controller - Address/Other */
1543 case 0x3c0:
1544 if (vga_address_3c0)
1545 vga_index_3c0 = val;
1546 else
1547 FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x (value 0x%02x)\n",
1548 vga_index_3c0, val);
1549 vga_address_3c0 = !vga_address_3c0;
1550 break;
1551 /* General Register - Misc output */
1552 case 0x3c2:
1553 FIXME("Unsupported VGA register: general register - misc output 0x%04x (value 0x%02x)\n", port, val);
1554 break;
1555 /* General Register - Video subsystem enable */
1556 case 0x3c3:
1557 FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x (value 0x%02x)\n", port, val);
1558 break;
1559 /* Sequencer Register - Address */
1560 case 0x3c4:
1561 vga_index_3c4 = val;
1562 break;
1563 /* Sequencer Register - Other */
1564 case 0x3c5:
1565 switch(vga_index_3c4) {
1566 case 0x04: /* Sequencer: Memory Mode Register */
1567 if(vga_fb_depth == 8)
1568 VGA_SetWindowStart((val & 8) ? 0 : -1);
1569 else
1570 FIXME("Memory Mode Register not supported in this mode.\n");
1571 break;
1572 default:
1573 FIXME("Unsupported index, VGA sequencer register 0x3c4: 0x%02x (value 0x%02x)\n",
1574 vga_index_3c4, val);
1576 break;
1577 case 0x3c8:
1578 palreg=val; palcnt=0; break;
1579 case 0x3c9:
1580 ((BYTE*)&paldat)[palcnt++]=val << 2;
1581 if (palcnt==3) {
1582 VGA_SetPalette(&paldat,palreg++,1);
1583 palcnt=0;
1585 break;
1586 /* Graphics Controller Register - Address */
1587 case 0x3ce:
1588 vga_index_3ce = val;
1589 break;
1590 /* Graphics Controller Register - Other */
1591 case 0x3cf:
1592 FIXME("Unsupported index, VGA graphics controller register - other 0x3ce: 0x%02x (value 0x%02x)\n",
1593 vga_index_3ce, val);
1594 break;
1595 /* CRT Controller Register - Index (MDA) */
1596 case 0x3b4:
1597 /* CRT Controller Register - Index (CGA) */
1598 case 0x3d4:
1599 vga_index_3d4 = val;
1600 break;
1601 /* CRT Controller Register - Other (MDA) */
1602 case 0x3b5:
1603 /* CRT Controller Register - Other (CGA) */
1604 case 0x3d5:
1605 FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x (value 0x%02x)\n",
1606 vga_index_3d4, val);
1607 break;
1608 /* Colour control register (CGA) */
1609 case 0x3d9:
1610 /* Set bright */
1611 VGA_SetBright((val & 0x10) && 1);
1613 /* Set palette index */
1614 VGA_SetPaletteIndex((val & 0x20) && 1);
1616 /* Now update the palette */
1617 VGA_UpdatePalette();
1618 break;
1619 default:
1620 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1624 BYTE VGA_ioport_in( WORD port )
1626 BYTE ret;
1628 switch (port) {
1629 /* Attribute Controller - Other */
1630 case 0x3c1:
1631 FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x\n",
1632 vga_index_3c0);
1633 return 0xff;
1634 /* General Register - Input status 0 */
1635 case 0x3c2:
1636 ret=0xff;
1637 FIXME("Unsupported VGA register: general register - input status 0 0x%04x\n", port);
1638 break;
1639 /* General Register - Video subsystem enable */
1640 case 0x3c3:
1641 ret=0xff;
1642 FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x\n", port);
1643 break;
1644 /* Sequencer Register - Other */
1645 case 0x3c5:
1646 switch(vga_index_3c4) {
1647 case 0x04: /* Sequencer: Memory Mode Register */
1648 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1649 default:
1650 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1651 vga_index_3c4);
1652 return 0xff;
1654 /* General Register - DAC State */
1655 case 0x3c7:
1656 ret=0xff;
1657 FIXME("Unsupported VGA register: general register - DAC State 0x%04x\n", port);
1658 break;
1659 /* General Register - Feature control */
1660 case 0x3ca:
1661 ret=0xff;
1662 FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port);
1663 break;
1664 /* General Register - Misc output */
1665 case 0x3cc:
1666 ret=0xff;
1667 FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port);
1668 break;
1669 /* Graphics Controller Register - Other */
1670 case 0x3cf:
1671 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1672 vga_index_3ce);
1673 return 0xff;
1674 /* CRT Controller Register - Other (MDA) */
1675 case 0x3b5:
1676 /* CRT Controller Register - Other (CGA) */
1677 case 0x3d5:
1678 FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x\n",
1679 vga_index_3d4);
1680 return 0xff;
1681 /* General Register - Input status 1 (MDA) */
1682 case 0x3ba:
1683 /* General Register - Input status 1 (CGA) */
1684 case 0x3da:
1686 * Read from this register resets register 0x3c0 address flip-flop.
1688 vga_address_3c0 = TRUE;
1691 * Read from this register returns following bits:
1692 * xxxx1xxx = Vertical retrace in progress if set.
1693 * xxxxx1xx = Light pen switched on.
1694 * xxxxxx1x = Light pen trigger set.
1695 * xxxxxxx1 = Either vertical or horizontal retrace
1696 * in progress if set.
1698 ret = 0;
1699 if (vga_retrace_vertical)
1700 ret |= 9;
1701 if (vga_retrace_horizontal)
1702 ret |= 3;
1705 * If VGA mode has been set, vertical retrace is
1706 * turned on once a frame and cleared after each read.
1707 * This might cause applications that synchronize with
1708 * vertical retrace to actually skip one frame but that
1709 * is probably not a problem.
1711 * If no VGA mode has been set, vertical retrace is faked
1712 * by toggling the value after every read.
1714 if (VGA_IsTimerRunning())
1715 vga_retrace_vertical = FALSE;
1716 else
1717 vga_retrace_vertical = !vga_retrace_vertical;
1720 * Toggle horizontal retrace.
1722 vga_retrace_horizontal = !vga_retrace_horizontal;
1723 break;
1725 default:
1726 ret=0xff;
1727 FIXME("Unsupported VGA register: 0x%04x\n", port);
1729 return ret;
1732 void VGA_Clean(void)
1734 VGA_Exit();
1735 VGA_DeinstallTimer();