winedos: Added missing values to VGA Mode 19 (256 color) palette registers.
[wine/wine-kai.git] / dlls / winedos / vga.c
blob5a568ced969428fe4cd4aecadcf0712477be0913
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 * VGA controller memory is emulated using linear framebuffer.
58 * This frambuffer also acts as an interface
59 * between VGA controller emulation and DirectDraw.
61 * vga_fb_width: Display width in pixels. Can be modified when
62 * display mode is changed.
63 * vga_fb_height: Display height in pixels. Can be modified when
64 * display mode is changed.
65 * vga_fb_depth: Number of bits used to store single pixel color information.
66 * Each pixel uses (vga_fb_depth+7)/8 bytes because
67 * 1-16 color modes are mapped to 256 color mode.
68 * Can be modified when display mode is changed.
69 * vga_fb_pitch: How many bytes to add to pointer in order to move
70 * from one row to another. This is fixed in VGA modes,
71 * but can be modified in SVGA modes.
72 * vga_fb_offset: Offset added to framebuffer start address in order
73 * to find the display origin. Programs use this to do
74 * double buffering and to scroll display. The value can
75 * be modified in VGA and SVGA modes.
76 * vga_fb_size: How many bytes are allocated to framebuffer.
77 * VGA framebuffers are always larger than display size and
78 * SVGA framebuffers may also be.
79 * vga_fb_data: Pointer to framebuffer start.
80 * vga_fb_window: Offset of 64k window 0xa0000 in bytes from framebuffer start.
81 * This value is >= 0, if mode uses linear framebuffer and
82 * -1, if mode uses color planes. This value is fixed
83 * in all modes except 0x13 (256 color VGA) where
84 * 0 means normal mode and -1 means Mode-X (unchained mode).
86 static int vga_fb_width;
87 static int vga_fb_height;
88 static int vga_fb_depth;
89 static int vga_fb_pitch;
90 static int vga_fb_offset;
91 static int vga_fb_size = 0;
92 static char *vga_fb_data = 0;
93 static int vga_fb_window = 0;
96 * VGA text mode data.
98 * vga_text_attr: Current active attribute.
99 * vga_text_old: Last data sent to console.
100 * This is used to optimize console updates.
101 * vga_text_width: Width of the text display in characters.
102 * vga_text_height: Height of the text display in characters.
103 * vga_text_x: Current cursor X-position. Starts from zero.
104 * vga_text_y: Current cursor Y-position. Starts from zero.
105 * vga_text_console: TRUE if stdout is console,
106 * FALSE if it is regular file.
108 static BYTE vga_text_attr;
109 static char *vga_text_old = NULL;
110 static BYTE vga_text_width;
111 static BYTE vga_text_height;
112 static BYTE vga_text_x;
113 static BYTE vga_text_y;
114 static BOOL vga_text_console;
117 * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
118 * indexed registers. These ports are used to select VGA controller
119 * subregister that can be written to or read from using ports 0x3c1,
120 * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
121 * stored in variables vga_index_*.
123 * Port 0x3c0 is special because it is both index and
124 * data-write register. Flip-flop vga_address_3c0 tells whether
125 * the port acts currently as an address register. Reading from port
126 * 0x3da resets the flip-flop to address mode.
128 static BYTE vga_index_3c0;
129 static BYTE vga_index_3c4;
130 static BYTE vga_index_3ce;
131 static BYTE vga_index_3d4;
132 static BOOL vga_address_3c0 = TRUE;
135 * This mutex is used to protect VGA state during asynchronous
136 * screen updates (see VGA_Poll). It makes sure that VGA state changes
137 * are atomic and the user interface is protected from flicker and
138 * corruption.
140 * The mutex actually serializes VGA operations and the screen update.
141 * Which means that whenever VGA_Poll occurs, application stalls if it
142 * tries to modify VGA state. This is not how real VGA adapters work,
143 * but it makes timing and correctness issues much easier to deal with.
145 static CRITICAL_SECTION vga_lock;
146 static CRITICAL_SECTION_DEBUG critsect_debug =
148 0, 0, &vga_lock,
149 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
150 0, 0, { (DWORD_PTR)(__FILE__ ": vga_lock") }
152 static CRITICAL_SECTION vga_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
154 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
155 static DirectDrawCreateProc pDirectDrawCreate;
157 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
159 static HWND vga_hwnd = NULL;
162 * VGA Palette Registers, in actual 18 bit color
163 * port 3C0H - 6 bit rgbRGB format
165 * 16 color accesses will use these pointers and insert
166 * entries from the 64-color palette (mode 18) into the default
167 * palette. --Robert 'Admiral' Coeyman
170 static char vga_16_palette[17]={
171 0x00, /* 0 - Black */
172 0x01, /* 1 - Blue */
173 0x02, /* 2 - Green */
174 0x03, /* 3 - Cyan */
175 0x04, /* 4 - Red */
176 0x05, /* 5 - Magenta */
177 0x14, /* 6 - Brown */
178 0x07, /* 7 - White (Light gray) */
179 0x38, /* 8 - Dark gray */
180 0x39, /* 9 - Light blue */
181 0x3a, /* A - Light green */
182 0x3b, /* B - Light cyan */
183 0x3c, /* C - Light red */
184 0x3d, /* D - Light magenta */
185 0x3e, /* E - Yellow */
186 0x3f, /* F - Bright White (White) */
187 0x00 /* Border Color */
191 * Mode 19 Default Color Register Setting
192 * DAC palette registers, converted from actual 18 bit color to 24.
194 static PALETTEENTRY vga_def_palette[256]={
195 /* red green blue */
196 /* 16 colors in IRGB values */
197 {0x00, 0x00, 0x00}, /* 0 (0) - Black */
198 {0x00, 0x00, 0xAA}, /* 1 (1) - Blue */
199 {0x00, 0xAA, 0x00}, /* 2 (2) - Green */
200 {0x00, 0xAA, 0xAA}, /* 3 (3) - Cyan */
201 {0xAA, 0x00, 0x00}, /* 4 (4) - Red */
202 {0xAA, 0x00, 0xAA}, /* 5 (5) - Magenta */
203 {0xAA, 0x55, 0x00}, /* 6 (6) - Brown */
204 {0xAA, 0xAA, 0xAA}, /* 7 (7) - White (Light gray) */
205 {0x55, 0x55, 0x55}, /* 8 (8) - Dark gray */
206 {0x55, 0x55, 0xFF}, /* 9 (9) - Light blue */
207 {0x55, 0xFF, 0x55}, /* 10 (A) - Light green */
208 {0x55, 0xFF, 0xFF}, /* 11 (B) - Light cyan */
209 {0xFF, 0x55, 0x55}, /* 12 (C) - Light red */
210 {0xFF, 0x55, 0xFF}, /* 13 (D) - Light magenta */
211 {0xFF, 0xFF, 0x55}, /* 14 (E) - Yellow */
212 {0xFF, 0xFF, 0xFF}, /* 15 (F) - Bright White (White) */
213 /* 16 shades of gray */
214 {0x00, 0x00, 0x00}, /* 16 (10) */
215 {0x10, 0x10, 0x10}, /* 17 (11) */
216 {0x20, 0x20, 0x20}, /* 18 (12) */
217 {0x35, 0x35, 0x35}, /* 19 (13) */
218 {0x45, 0x45, 0x45}, /* 20 (14) */
219 {0x55, 0x55, 0x55}, /* 21 (15) */
220 {0x65, 0x65, 0x65}, /* 22 (16) */
221 {0x75, 0x75, 0x75}, /* 23 (17) */
222 {0x8A, 0x8A, 0x8A}, /* 24 (18) */
223 {0x9A, 0x9A, 0x9A}, /* 25 (19) */
224 {0xAA, 0xAA, 0xAA}, /* 26 (1A) */
225 {0xBA, 0xBA, 0xBA}, /* 27 (1B) */
226 {0xCA, 0xCA, 0xCA}, /* 28 (1C) */
227 {0xDF, 0xDF, 0xDF}, /* 29 (1D) */
228 {0xEF, 0xEF, 0xEF}, /* 30 (1E) */
229 {0xFF, 0xFF, 0xFF}, /* 31 (1F) */
230 /* High Intensity group - 72 colors in 1/3 saturation groups (20H-37H high) */
231 {0x00, 0x00, 0xFF}, /* 32 (20) */
232 {0x41, 0x00, 0xFF}, /* 33 (21) */
233 {0x82, 0x00, 0xFF}, /* 34 (22) */
234 {0xBE, 0x00, 0xFF}, /* 35 (23) */
235 {0xFF, 0x00, 0xFF}, /* 36 (24) */
236 {0xFF, 0x00, 0xBE}, /* 37 (25) */
237 {0xFF, 0x00, 0x82}, /* 38 (26) */
238 {0xFF, 0x00, 0x41}, /* 39 (27) */
239 {0xFF, 0x00, 0x00}, /* 40 (28) */
240 {0xFF, 0x41, 0x00}, /* 41 (29) */
241 {0xFF, 0x82, 0x00}, /* 42 (2A) */
242 {0xFF, 0xBE, 0x00}, /* 43 (2B) */
243 {0xFF, 0xFF, 0x00}, /* 44 (2C) */
244 {0xBE, 0xFF, 0x00}, /* 45 (2D) */
245 {0x82, 0xFF, 0x00}, /* 46 (2E) */
246 {0x41, 0xFF, 0x00}, /* 47 (2F) */
247 {0x00, 0xFF, 0x00}, /* 48 (30) */
248 {0x00, 0xFF, 0x41}, /* 49 (31) */
249 {0x00, 0xFF, 0x82}, /* 50 (32) */
250 {0x00, 0xFF, 0xBE}, /* 51 (33) */
251 {0x00, 0xFF, 0xFF}, /* 52 (34) */
252 {0x00, 0xBE, 0xFF}, /* 53 (35) */
253 {0x00, 0x82, 0xFF}, /* 54 (36) */
254 {0x00, 0x41, 0xFF}, /* 55 (37) */
255 /* High Intensity group - 72 colors in 2/3 saturation groups (38H-4FH moderate) */
256 {0x82, 0x82, 0xFF}, /* 56 (38) */
257 {0x9E, 0x82, 0xFF}, /* 57 (39) */
258 {0xBE, 0x82, 0xFF}, /* 58 (3A) */
259 {0xDF, 0x82, 0xFF}, /* 59 (3B) */
260 {0xFF, 0x82, 0xFF}, /* 60 (3C) */
261 {0xFF, 0x82, 0xDF}, /* 61 (3D) */
262 {0xFF, 0x82, 0xBE}, /* 62 (3E) */
263 {0xFF, 0x82, 0x9E}, /* 63 (3F) */
264 {0xFF, 0x82, 0x82}, /* 64 (40) */
265 {0xFF, 0x9E, 0x82}, /* 65 (41) */
266 {0xFF, 0xBE, 0x82}, /* 66 (42) */
267 {0xFF, 0xDF, 0x82}, /* 67 (43) */
268 {0xFF, 0xFF, 0x82}, /* 68 (44) */
269 {0xDF, 0xFF, 0x82}, /* 69 (45) */
270 {0xBE, 0xFF, 0x82}, /* 70 (46) */
271 {0x9E, 0xFF, 0x82}, /* 71 (47) */
272 {0x82, 0xFF, 0x82}, /* 72 (48) */
273 {0x82, 0xFF, 0x9E}, /* 73 (49) */
274 {0x82, 0xFF, 0xBE}, /* 74 (4A) */
275 {0x82, 0xFF, 0xDF}, /* 75 (4B) */
276 {0x82, 0xFF, 0xFF}, /* 76 (4C) */
277 {0x82, 0xDF, 0xFF}, /* 77 (4D) */
278 {0x82, 0xBE, 0xFF}, /* 78 (4E) */
279 {0x82, 0x9E, 0xFF}, /* 79 (4F) */
280 /* High Intensity group - 72 colors in 3/3 saturation groups (50H-67H low) */
281 {0xBA, 0xBA, 0xFF}, /* 80 (50) */
282 {0xCA, 0xBA, 0xFF}, /* 81 (51) */
283 {0xDF, 0xBA, 0xFF}, /* 82 (52) */
284 {0xEF, 0xBA, 0xFF}, /* 83 (53) */
285 {0xFF, 0xBA, 0xFF}, /* 84 (54) */
286 {0xFF, 0xBA, 0xEF}, /* 85 (55) */
287 {0xFF, 0xBA, 0xDF}, /* 86 (56) */
288 {0xFF, 0xBA, 0xCA}, /* 87 (57) */
289 {0xFF, 0xBA, 0xBA}, /* 88 (58) */
290 {0xFF, 0xCA, 0xBA}, /* 89 (59) */
291 {0xFF, 0xDF, 0xBA}, /* 90 (5A) */
292 {0xFF, 0xEF, 0xBA}, /* 91 (5B) */
293 {0xFF, 0xFF, 0xBA}, /* 92 (5C) */
294 {0xEF, 0xFF, 0xBA}, /* 93 (5D) */
295 {0xDF, 0xFF, 0xBA}, /* 94 (5E) */
296 {0xCA, 0xFF, 0xBA}, /* 95 (5F) */
297 {0xBA, 0xFF, 0xBA}, /* 96 (60) */
298 {0xBA, 0xFF, 0xCA}, /* 97 (61) */
299 {0xBA, 0xFF, 0xDF}, /* 98 (62) */
300 {0xBA, 0xFF, 0xEF}, /* 99 (63) */
301 {0xBA, 0xFF, 0xFF}, /* 100 (64) */
302 {0xBA, 0xEF, 0xFF}, /* 101 (65) */
303 {0xBA, 0xDF, 0xFF}, /* 102 (66) */
304 {0xBA, 0xCA, 0xFF}, /* 103 (67) */
305 /* Medium Intensity group - 72 colors in 1/3 saturation groups (68H-7FH high) */
306 {0x00, 0x00, 0x71}, /* 104 (68) */
307 {0x1C, 0x00, 0x71}, /* 105 (69) */
308 {0x39, 0x00, 0x71}, /* 106 (6A) */
309 {0x55, 0x00, 0x71}, /* 107 (6B) */
310 {0x71, 0x00, 0x71}, /* 108 (6C) */
311 {0x71, 0x00, 0x55}, /* 109 (6D) */
312 {0x71, 0x00, 0x39}, /* 110 (6E) */
313 {0x71, 0x00, 0x1C}, /* 111 (6F) */
314 {0x71, 0x00, 0x00}, /* 112 (70) */
315 {0x71, 0x1C, 0x00}, /* 113 (71) */
316 {0x71, 0x39, 0x00}, /* 114 (72) */
317 {0x71, 0x55, 0x00}, /* 115 (73) */
318 {0x71, 0x71, 0x00}, /* 116 (74) */
319 {0x55, 0x71, 0x00}, /* 117 (75) */
320 {0x39, 0x71, 0x00}, /* 118 (76) */
321 {0x1C, 0x71, 0x00}, /* 119 (77) */
322 {0x00, 0x71, 0x00}, /* 120 (78) */
323 {0x00, 0x71, 0x1C}, /* 121 (79) */
324 {0x00, 0x71, 0x39}, /* 122 (7A) */
325 {0x00, 0x71, 0x55}, /* 123 (7B) */
326 {0x00, 0x71, 0x71}, /* 124 (7C) */
327 {0x00, 0x55, 0x71}, /* 125 (7D) */
328 {0x00, 0x39, 0x71}, /* 126 (7E) */
329 {0x00, 0x1C, 0x71}, /* 127 (7F) */
330 /* Medium Intensity group - 72 colors in 2/3 saturation groups (80H-97H moderate) */
331 {0x39, 0x39, 0x71}, /* 128 (80) */
332 {0x45, 0x39, 0x71}, /* 129 (81) */
333 {0x55, 0x39, 0x71}, /* 130 (82) */
334 {0x61, 0x39, 0x71}, /* 131 (83) */
335 {0x71, 0x39, 0x71}, /* 132 (84) */
336 {0x71, 0x39, 0x61}, /* 133 (85) */
337 {0x71, 0x39, 0x55}, /* 134 (86) */
338 {0x71, 0x39, 0x45}, /* 135 (87) */
339 {0x71, 0x39, 0x39}, /* 136 (88) */
340 {0x71, 0x45, 0x39}, /* 137 (89) */
341 {0x71, 0x55, 0x39}, /* 138 (8A) */
342 {0x71, 0x61, 0x39}, /* 139 (8B) */
343 {0x71, 0x71, 0x39}, /* 140 (8C) */
344 {0x61, 0x71, 0x39}, /* 141 (8D) */
345 {0x55, 0x71, 0x39}, /* 142 (8E) */
346 {0x45, 0x71, 0x39}, /* 143 (8F) */
347 {0x39, 0x71, 0x39}, /* 144 (90) */
348 {0x39, 0x71, 0x45}, /* 145 (91) */
349 {0x39, 0x71, 0x55}, /* 146 (92) */
350 {0x39, 0x71, 0x61}, /* 147 (93) */
351 {0x39, 0x71, 0x71}, /* 148 (94) */
352 {0x39, 0x61, 0x71}, /* 149 (95) */
353 {0x39, 0x55, 0x71}, /* 150 (96) */
354 {0x39, 0x45, 0x71}, /* 151 (97) */
355 /* Medium Intensity group - 72 colors in 3/3 saturation groups (98H-AFH low) */
356 {0x51, 0x51, 0x71}, /* 152 (98) */
357 {0x59, 0x51, 0x71}, /* 153 (99) */
358 {0x61, 0x51, 0x71}, /* 154 (9A) */
359 {0x69, 0x51, 0x71}, /* 155 (9B) */
360 {0x71, 0x51, 0x71}, /* 156 (9C) */
361 {0x71, 0x51, 0x69}, /* 157 (9D) */
362 {0x71, 0x51, 0x61}, /* 158 (9E) */
363 {0x71, 0x51, 0x59}, /* 159 (9F) */
364 {0x71, 0x51, 0x51}, /* 160 (A0) */
365 {0x71, 0x59, 0x51}, /* 161 (A1) */
366 {0x71, 0x61, 0x51}, /* 162 (A2) */
367 {0x71, 0x69, 0x51}, /* 163 (A3) */
368 {0x71, 0x71, 0x51}, /* 164 (A4) */
369 {0x69, 0x71, 0x51}, /* 165 (A5) */
370 {0x61, 0x71, 0x51}, /* 166 (A6) */
371 {0x59, 0x71, 0x51}, /* 167 (A7) */
372 {0x51, 0x71, 0x51}, /* 168 (A8) */
373 {0x51, 0x71, 0x59}, /* 169 (A9) */
374 {0x51, 0x71, 0x61}, /* 170 (AA) */
375 {0x51, 0x71, 0x69}, /* 171 (AB) */
376 {0x51, 0x71, 0x71}, /* 172 (AC) */
377 {0x51, 0x69, 0x71}, /* 173 (AD) */
378 {0x51, 0x61, 0x71}, /* 174 (AE) */
379 {0x51, 0x59, 0x71}, /* 175 (AF) */
380 /* Low Intensity group - 72 colors in 1/3 saturation groups (B0H-C7H high) */
381 {0x00, 0x00, 0x41}, /* 176 (B0) */
382 {0x10, 0x00, 0x41}, /* 177 (B1) */
383 {0x20, 0x00, 0x41}, /* 178 (B2) */
384 {0x31, 0x00, 0x41}, /* 179 (B3) */
385 {0x41, 0x00, 0x41}, /* 180 (B4) */
386 {0x41, 0x00, 0x31}, /* 181 (B5) */
387 {0x41, 0x00, 0x20}, /* 182 (B6) */
388 {0x41, 0x00, 0x10}, /* 183 (B7) */
389 {0x41, 0x00, 0x00}, /* 184 (B8) */
390 {0x41, 0x10, 0x00}, /* 185 (B9) */
391 {0x41, 0x20, 0x00}, /* 186 (BA) */
392 {0x41, 0x31, 0x00}, /* 187 (BB) */
393 {0x41, 0x41, 0x00}, /* 188 (BC) */
394 {0x31, 0x41, 0x00}, /* 189 (BD) */
395 {0x20, 0x41, 0x00}, /* 190 (BE) */
396 {0x10, 0x41, 0x00}, /* 191 (BF) */
397 {0x00, 0x41, 0x00}, /* 192 (C0) */
398 {0x00, 0x41, 0x10}, /* 193 (C1) */
399 {0x00, 0x41, 0x20}, /* 194 (C2) */
400 {0x00, 0x41, 0x31}, /* 195 (C3) */
401 {0x00, 0x41, 0x41}, /* 196 (C4) */
402 {0x00, 0x31, 0x41}, /* 197 (C5) */
403 {0x00, 0x20, 0x41}, /* 198 (C6) */
404 {0x00, 0x10, 0x41}, /* 199 (C7) */
405 /* Low Intensity group - 72 colors in 2/3 saturation groups (C8H-DFH moderate) */
406 {0x20, 0x20, 0x41}, /* 200 (C8) */
407 {0x28, 0x20, 0x41}, /* 201 (C9) */
408 {0x31, 0x20, 0x41}, /* 202 (CA) */
409 {0x39, 0x20, 0x41}, /* 203 (CB) */
410 {0x41, 0x20, 0x41}, /* 204 (CC) */
411 {0x41, 0x20, 0x39}, /* 205 (CD) */
412 {0x41, 0x20, 0x31}, /* 206 (CE) */
413 {0x41, 0x20, 0x28}, /* 207 (CF) */
414 {0x41, 0x20, 0x20}, /* 208 (D0) */
415 {0x41, 0x28, 0x20}, /* 209 (D1) */
416 {0x41, 0x31, 0x20}, /* 210 (D2) */
417 {0x41, 0x39, 0x20}, /* 211 (D3) */
418 {0x41, 0x41, 0x20}, /* 212 (D4) */
419 {0x39, 0x41, 0x20}, /* 213 (D5) */
420 {0x31, 0x41, 0x20}, /* 214 (D6) */
421 {0x28, 0x41, 0x20}, /* 215 (D7) */
422 {0x20, 0x41, 0x20}, /* 216 (D8) */
423 {0x20, 0x41, 0x28}, /* 217 (D9) */
424 {0x20, 0x41, 0x31}, /* 218 (DA) */
425 {0x20, 0x41, 0x39}, /* 219 (DB) */
426 {0x20, 0x41, 0x41}, /* 220 (DC) */
427 {0x20, 0x39, 0x41}, /* 221 (DD) */
428 {0x20, 0x31, 0x41}, /* 222 (DE) */
429 {0x20, 0x28, 0x41}, /* 223 (DF) */
430 /* Low Intensity group - 72 colors in 3/3 saturation groups (E0H-F7H low) */
431 {0x2D, 0x2D, 0x41}, /* 223 (E0) */
432 {0x31, 0x2D, 0x41}, /* 224 (E1) */
433 {0x35, 0x2D, 0x41}, /* 225 (E2) */
434 {0x3D, 0x2D, 0x41}, /* 226 (E3) */
435 {0x41, 0x2D, 0x41}, /* 227 (E4) */
436 {0x41, 0x2D, 0x3D}, /* 228 (E5) */
437 {0x41, 0x2D, 0x35}, /* 229 (E6) */
438 {0x41, 0x2D, 0x31}, /* 230 (E7) */
439 {0x41, 0x2D, 0x2D}, /* 231 (E8) */
440 {0x41, 0x31, 0x2D}, /* 232 (E9) */
441 {0x41, 0x35, 0x2D}, /* 233 (EA) */
442 {0x41, 0x3D, 0x2D}, /* 234 (EB) */
443 {0x41, 0x41, 0x2D}, /* 235 (EC) */
444 {0x3D, 0x41, 0x2D}, /* 236 (ED) */
445 {0x35, 0x41, 0x2D}, /* 237 (EE) */
446 {0x31, 0x41, 0x2D}, /* 238 (EF) */
447 {0x2D, 0x41, 0x2D}, /* 239 (F0) */
448 {0x2D, 0x41, 0x31}, /* 240 (F1) */
449 {0x2D, 0x41, 0x35}, /* 241 (F2) */
450 {0x2D, 0x41, 0x3D}, /* 242 (F3) */
451 {0x2D, 0x41, 0x41}, /* 243 (F4) */
452 {0x2D, 0x3D, 0x41}, /* 244 (F5) */
453 {0x2D, 0x35, 0x41}, /* 245 (F6) */
454 {0x2D, 0x31, 0x41}, /* 246 (F7) */
455 /* Fill up remainder of palettes with black */
456 {0,0,0}
460 * Mode 18 Default Color Register Setting
461 * DAC palette registers, converted from actual 18 bit color to 24.
463 * This palette is the dos default, converted from 18 bit color to 24.
464 * It contains only 64 entries of colors--all others are zeros.
465 * --Robert 'Admiral' Coeyman
467 static PALETTEENTRY vga_def64_palette[256]={
468 /* red green blue */
469 {0x00, 0x00, 0x00}, /* 0x00 Black */
470 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
471 {0x00, 0xaa, 0x00}, /* 0x02 Green */
472 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
473 {0xaa, 0x00, 0x00}, /* 0x04 Red */
474 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
475 {0xaa, 0xaa, 0x00}, /* 0x06 */
476 {0xaa, 0xaa, 0xaa}, /* 0x07 White (Light Gray) */
477 {0x00, 0x00, 0x55}, /* 0x08 */
478 {0x00, 0x00, 0xff}, /* 0x09 */
479 {0x00, 0xaa, 0x55}, /* 0x0a */
480 {0x00, 0xaa, 0xff}, /* 0x0b */
481 {0xaa, 0x00, 0x55}, /* 0x0c */
482 {0xaa, 0x00, 0xff}, /* 0x0d */
483 {0xaa, 0xaa, 0x55}, /* 0x0e */
484 {0xaa, 0xaa, 0xff}, /* 0x0f */
485 {0x00, 0x55, 0x00}, /* 0x10 */
486 {0x00, 0x55, 0xaa}, /* 0x11 */
487 {0x00, 0xff, 0x00}, /* 0x12 */
488 {0x00, 0xff, 0xaa}, /* 0x13 */
489 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
490 {0xaa, 0x55, 0xaa}, /* 0x15 */
491 {0xaa, 0xff, 0x00}, /* 0x16 */
492 {0xaa, 0xff, 0xaa}, /* 0x17 */
493 {0x00, 0x55, 0x55}, /* 0x18 */
494 {0x00, 0x55, 0xff}, /* 0x19 */
495 {0x00, 0xff, 0x55}, /* 0x1a */
496 {0x00, 0xff, 0xff}, /* 0x1b */
497 {0xaa, 0x55, 0x55}, /* 0x1c */
498 {0xaa, 0x55, 0xff}, /* 0x1d */
499 {0xaa, 0xff, 0x55}, /* 0x1e */
500 {0xaa, 0xff, 0xff}, /* 0x1f */
501 {0x55, 0x00, 0x00}, /* 0x20 */
502 {0x55, 0x00, 0xaa}, /* 0x21 */
503 {0x55, 0xaa, 0x00}, /* 0x22 */
504 {0x55, 0xaa, 0xaa}, /* 0x23 */
505 {0xff, 0x00, 0x00}, /* 0x24 */
506 {0xff, 0x00, 0xaa}, /* 0x25 */
507 {0xff, 0xaa, 0x00}, /* 0x26 */
508 {0xff, 0xaa, 0xaa}, /* 0x27 */
509 {0x55, 0x00, 0x55}, /* 0x28 */
510 {0x55, 0x00, 0xff}, /* 0x29 */
511 {0x55, 0xaa, 0x55}, /* 0x2a */
512 {0x55, 0xaa, 0xff}, /* 0x2b */
513 {0xff, 0x00, 0x55}, /* 0x2c */
514 {0xff, 0x00, 0xff}, /* 0x2d */
515 {0xff, 0xaa, 0x55}, /* 0x2e */
516 {0xff, 0xaa, 0xff}, /* 0x2f */
517 {0x55, 0x55, 0x00}, /* 0x30 */
518 {0x55, 0x55, 0xaa}, /* 0x31 */
519 {0x55, 0xff, 0x00}, /* 0x32 */
520 {0x55, 0xff, 0xaa}, /* 0x33 */
521 {0xff, 0x55, 0x00}, /* 0x34 */
522 {0xff, 0x55, 0xaa}, /* 0x35 */
523 {0xff, 0xff, 0x00}, /* 0x36 */
524 {0xff, 0xff, 0xaa}, /* 0x37 */
525 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
526 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
527 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
528 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
529 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
530 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
531 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
532 {0xff, 0xff, 0xff}, /* 0x3f White */
533 {0,0,0} /* The next 192 entries are all zeros */
536 static HANDLE VGA_timer;
537 static HANDLE VGA_timer_thread;
539 /* set the timer rate; called in the polling thread context */
540 static void CALLBACK set_timer_rate( ULONG_PTR arg )
542 LARGE_INTEGER when;
544 when.u.LowPart = when.u.HighPart = 0;
545 SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
548 static DWORD CALLBACK VGA_TimerThread( void *dummy )
550 for (;;) SleepEx( INFINITE, TRUE );
551 return 0;
554 static void VGA_DeinstallTimer(void)
556 if (VGA_timer_thread)
559 * Make sure the update thread is not holding
560 * system resources when we kill it.
562 * Now, we only need to worry about update thread
563 * getting terminated while in EnterCriticalSection
564 * or WaitForMultipleObjectsEx.
566 * FIXME: Is this a problem?
568 EnterCriticalSection(&vga_lock);
570 CancelWaitableTimer( VGA_timer );
571 CloseHandle( VGA_timer );
572 TerminateThread( VGA_timer_thread, 0 );
573 CloseHandle( VGA_timer_thread );
574 VGA_timer_thread = 0;
576 LeaveCriticalSection(&vga_lock);
579 * Synchronize display. This makes sure that
580 * changes to display become visible even if program
581 * terminates before update thread had time to run.
583 VGA_Poll( 0, 0, 0 );
587 static void VGA_InstallTimer(unsigned Rate)
589 if (!VGA_timer_thread)
591 VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
592 VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
594 QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
597 static BOOL VGA_IsTimerRunning(void)
599 return VGA_timer_thread ? TRUE : FALSE;
602 static HANDLE VGA_AlphaConsole(void)
604 /* this assumes that no Win32 redirection has taken place, but then again,
605 * only 16-bit apps are likely to use this part of Wine... */
606 return GetStdHandle(STD_OUTPUT_HANDLE);
609 static char*VGA_AlphaBuffer(void)
611 return (char *)0xb8000;
614 /*** GRAPHICS MODE ***/
616 typedef struct {
617 unsigned Xres, Yres, Depth;
618 int ret;
619 } ModeSet;
622 /**********************************************************************
623 * VGA_SyncWindow
625 * Copy VGA window into framebuffer (if argument is TRUE) or
626 * part of framebuffer into VGA window (if argument is FALSE).
628 static void VGA_SyncWindow( BOOL target_is_fb )
630 int size = VGA_WINDOW_SIZE;
632 /* Window does not overlap framebuffer. */
633 if (vga_fb_window >= vga_fb_size)
634 return;
636 /* Check if window overlaps framebuffer only partially. */
637 if (vga_fb_size - vga_fb_window < VGA_WINDOW_SIZE)
638 size = vga_fb_size - vga_fb_window;
640 if (target_is_fb)
641 memmove( vga_fb_data + vga_fb_window, VGA_WINDOW_START, size );
642 else
643 memmove( VGA_WINDOW_START, vga_fb_data + vga_fb_window, size );
647 static void WINAPI VGA_DoExit(ULONG_PTR arg)
649 VGA_DeinstallTimer();
650 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
651 IDirectDrawSurface_Release(lpddsurf);
652 lpddsurf=NULL;
653 IDirectDrawPalette_Release(lpddpal);
654 lpddpal=NULL;
655 IDirectDraw_Release(lpddraw);
656 lpddraw=NULL;
659 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
661 HRESULT res;
662 ModeSet *par = (ModeSet *)arg;
663 par->ret=1;
665 if (lpddraw) VGA_DoExit(0);
666 if (!lpddraw) {
667 if (!pDirectDrawCreate)
669 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
670 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
671 if (!pDirectDrawCreate) {
672 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
673 return;
676 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
677 if (!lpddraw) {
678 ERR("DirectDraw is not available (res = 0x%x)\n",res);
679 return;
681 if (!vga_hwnd) {
682 vga_hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",
683 WS_POPUP|WS_VISIBLE|SS_NOTIFY,0,0,
684 par->Xres,par->Yres,0,0,0,NULL);
685 if (!vga_hwnd) {
686 ERR("Failed to create user window.\n");
687 IDirectDraw_Release(lpddraw);
688 lpddraw=NULL;
689 return;
692 else
693 SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
695 res=IDirectDraw_SetCooperativeLevel(lpddraw,vga_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE);
696 if (res != S_OK) {
697 ERR("Could not set cooperative level to exclusive (0x%x)\n",res);
700 res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth);
701 if (res != S_OK) {
702 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = 0x%x!\n",par->Xres,par->Yres,par->Depth,res);
703 IDirectDraw_Release(lpddraw);
704 lpddraw=NULL;
705 return;
708 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
709 if (res != S_OK) {
710 ERR("Could not create palette (res = 0x%x)\n",res);
711 IDirectDraw_Release(lpddraw);
712 lpddraw=NULL;
713 return;
715 res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette);
716 if (res != S_OK) {
717 ERR("Could not set default palette entries (res = 0x%x)\n", res);
720 memset(&sdesc,0,sizeof(sdesc));
721 sdesc.dwSize=sizeof(sdesc);
722 sdesc.dwFlags = DDSD_CAPS;
723 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
724 res=IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL);
725 if (res != S_OK || !lpddsurf) {
726 ERR("DirectDraw surface is not available\n");
727 IDirectDraw_Release(lpddraw);
728 lpddraw=NULL;
729 return;
731 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
732 vga_retrace_vertical = vga_retrace_horizontal = FALSE;
733 /* poll every 20ms (50fps should provide adequate responsiveness) */
734 VGA_InstallTimer(20);
736 par->ret=0;
737 return;
740 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
742 ModeSet par;
743 int newSize;
745 vga_fb_width = Xres;
746 vga_fb_height = Yres;
747 vga_fb_depth = Depth;
748 vga_fb_offset = 0;
749 vga_fb_pitch = Xres * ((Depth + 7) / 8);
751 newSize = Xres * Yres * ((Depth + 7) / 8);
752 if(newSize < 256 * 1024)
753 newSize = 256 * 1024;
755 if(vga_fb_size < newSize) {
756 HeapFree(GetProcessHeap(), 0, vga_fb_data);
757 vga_fb_data = HeapAlloc(GetProcessHeap(), 0, newSize);
758 vga_fb_size = newSize;
761 if(Xres >= 640 || Yres >= 480) {
762 par.Xres = Xres;
763 par.Yres = Yres;
764 } else {
765 par.Xres = 640;
766 par.Yres = 480;
769 VGA_SetWindowStart((Depth < 8) ? -1 : 0);
771 par.Depth = (Depth < 8) ? 8 : Depth;
773 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
774 return par.ret;
777 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
779 if (!lpddraw) return 1;
780 if (!lpddsurf) return 1;
781 if (Height) *Height=sdesc.dwHeight;
782 if (Width) *Width=sdesc.dwWidth;
783 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
784 return 0;
787 static void VGA_Exit(void)
789 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
792 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
794 if (!lpddraw) return;
795 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
798 /* set a single [char wide] color in 16 color mode. */
799 void VGA_SetColor16(int reg,int color)
801 PALETTEENTRY *pal;
803 if (!lpddraw) return;
804 pal= &vga_def64_palette[color];
805 IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
806 vga_16_palette[reg]=(char)color;
809 /* Get a single [char wide] color in 16 color mode. */
810 char VGA_GetColor16(int reg)
813 if (!lpddraw) return 0;
814 return vga_16_palette[reg];
817 /* set all 17 [char wide] colors at once in 16 color mode. */
818 void VGA_Set16Palette(char *Table)
820 PALETTEENTRY *pal;
821 int c;
823 if (!lpddraw) return; /* return if we're in text only mode */
824 memcpy( Table, vga_16_palette, 17 ); /* copy the entries into the table */
826 for (c=0; c<17; c++) { /* 17 entries */
827 pal= &vga_def64_palette[(int)vga_16_palette[c]]; /* get color */
828 IDirectDrawPalette_SetEntries(lpddpal,0,c,1,pal); /* set entry */
829 TRACE("Palette register %d set to %d\n",c,(int)vga_16_palette[c]);
830 } /* end of the counting loop */
833 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
834 void VGA_Get16Palette(char *Table)
837 if (!lpddraw) return; /* return if we're in text only mode */
838 memcpy( vga_16_palette, Table, 17 ); /* copy the entries into the table */
841 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
843 PALETTEENTRY pal[256];
844 int c;
846 if (!lpddraw) return;
847 for (c=0; c<len; c++) {
848 pal[c].peRed =color[c].rgbRed;
849 pal[c].peGreen=color[c].rgbGreen;
850 pal[c].peBlue =color[c].rgbBlue;
851 pal[c].peFlags=0;
853 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
856 static LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
858 if (!lpddraw) return NULL;
859 if (!lpddsurf) return NULL;
860 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0) != S_OK) {
861 ERR("could not lock surface!\n");
862 return NULL;
864 if (Pitch) *Pitch=sdesc.u1.lPitch;
865 if (Height) *Height=sdesc.dwHeight;
866 if (Width) *Width=sdesc.dwWidth;
867 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
868 return sdesc.lpSurface;
871 static void VGA_Unlock(void)
873 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
877 * Set start of 64k window at 0xa0000 in bytes.
878 * If value is -1, initialize color plane support.
879 * If value is >= 0, window contains direct copy of framebuffer.
881 void VGA_SetWindowStart(int start)
883 if(start == vga_fb_window)
884 return;
886 EnterCriticalSection(&vga_lock);
888 if(vga_fb_window == -1)
889 FIXME("Remove VGA memory emulation.\n");
890 else
891 VGA_SyncWindow( TRUE );
893 vga_fb_window = start;
895 if(vga_fb_window == -1)
896 FIXME("Install VGA memory emulation.\n");
897 else
898 VGA_SyncWindow( FALSE );
900 LeaveCriticalSection(&vga_lock);
904 * Get start of 64k window at 0xa0000 in bytes.
905 * Value is -1 in color plane modes.
907 int VGA_GetWindowStart(void)
909 return vga_fb_window;
913 /**********************************************************************
914 * VGA_DoShowMouse
916 * Callback for VGA_ShowMouse.
918 static void WINAPI VGA_DoShowMouse( ULONG_PTR show )
920 INT rv;
924 rv = ShowCursor( show );
926 while( show ? (rv < 0) : (rv >= 0) );
930 /**********************************************************************
931 * VGA_ShowMouse
933 * If argument is TRUE, unconditionally show mouse cursor.
934 * If argument is FALSE, unconditionally hide mouse cursor.
935 * This only works in graphics mode.
937 void VGA_ShowMouse( BOOL show )
939 if (lpddraw)
940 MZ_RunInThread( VGA_DoShowMouse, (ULONG_PTR)show );
944 /*** TEXT MODE ***/
946 /* prepare the text mode video memory copy that is used to only
947 * update the video memory line that did get updated. */
948 static void VGA_PrepareVideoMemCopy(unsigned Xres, unsigned Yres)
950 char *p, *p2;
951 unsigned int i;
954 * Allocate space for char + attr.
957 if (vga_text_old)
958 vga_text_old = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
959 vga_text_old, Xres * Yres * 2 );
960 else
961 vga_text_old = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
962 Xres * Yres * 2 );
963 p = VGA_AlphaBuffer();
964 p2 = vga_text_old;
966 /* make sure the video mem copy contains the exact opposite of our
967 * actual text mode memory area to make sure the screen
968 * does get updated fully initially */
969 for (i=0; i < Xres*Yres*2; i++)
970 *p2++ = *p++ ^ 0xff; /* XOR it */
973 /**********************************************************************
974 * VGA_SetAlphaMode
976 * Set VGA emulation to text mode.
978 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
980 VGA_Exit();
981 VGA_DeinstallTimer();
983 VGA_PrepareVideoMemCopy(Xres, Yres);
984 vga_text_width = Xres;
985 vga_text_height = Yres;
987 if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
988 VGA_SetCursorPos(0,0);
990 if(vga_text_console) {
991 COORD size;
992 size.X = Xres;
993 size.Y = Yres;
994 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
996 /* poll every 30ms (33fps should provide adequate responsiveness) */
997 VGA_InstallTimer(30);
1001 /**********************************************************************
1002 * VGA_InitAlphaMode
1004 * Initialize VGA text mode handling and return default text mode.
1005 * This function does not set VGA emulation to text mode.
1007 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
1009 CONSOLE_SCREEN_BUFFER_INFO info;
1011 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
1013 vga_text_console = TRUE;
1014 vga_text_x = info.dwCursorPosition.X;
1015 vga_text_y = info.dwCursorPosition.Y;
1016 vga_text_attr = info.wAttributes;
1017 *Xres = info.dwSize.X;
1018 *Yres = info.dwSize.Y;
1020 else
1022 vga_text_console = FALSE;
1023 vga_text_x = 0;
1024 vga_text_y = 0;
1025 vga_text_attr = 0x0f;
1026 *Xres = 80;
1027 *Yres = 25;
1031 /**********************************************************************
1032 * VGA_GetAlphaMode
1034 * Get current text mode. Returns TRUE and sets resolution if
1035 * any VGA text mode has been initialized.
1037 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
1039 if (vga_text_width != 0 && vga_text_height != 0) {
1040 *Xres = vga_text_width;
1041 *Yres = vga_text_height;
1042 return TRUE;
1043 } else
1044 return FALSE;
1047 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
1049 CONSOLE_CURSOR_INFO cci;
1051 /* standard cursor settings:
1052 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
1054 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
1055 cci.dwSize = ((end & 0x1f) - (start_options & 0x1f))/0x0e * 100;
1056 if (!cci.dwSize) cci.dwSize++; /* NULL cursor would make SCCI() fail ! */
1057 cci.bVisible = ((start_options & 0x60) != 0x20); /* invisible ? */
1059 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
1062 void VGA_SetCursorPos(unsigned X,unsigned Y)
1064 vga_text_x = X;
1065 vga_text_y = Y;
1068 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
1070 if (X) *X = vga_text_x;
1071 if (Y) *Y = vga_text_y;
1074 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
1076 char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
1077 dat[0] = ascii;
1078 if (attr>=0)
1079 dat[1] = attr;
1082 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
1084 EnterCriticalSection(&vga_lock);
1086 while (count--)
1087 VGA_PutCharAt(X + count, Y, ch, attr);
1089 LeaveCriticalSection(&vga_lock);
1092 void VGA_PutChar(BYTE ascii)
1094 DWORD w;
1096 EnterCriticalSection(&vga_lock);
1098 switch(ascii) {
1099 case '\b':
1100 if (vga_text_x)
1102 vga_text_x--;
1103 VGA_PutCharAt(vga_text_x, vga_text_y, ' ', 0);
1105 break;
1107 case '\t':
1108 vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
1109 break;
1111 case '\n':
1112 vga_text_y++;
1113 vga_text_x = 0;
1114 break;
1116 case '\a':
1117 break;
1119 case '\r':
1120 vga_text_x = 0;
1121 break;
1123 default:
1124 VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
1125 vga_text_x++;
1128 if (vga_text_x >= vga_text_width)
1130 vga_text_x = 0;
1131 vga_text_y++;
1134 if (vga_text_y >= vga_text_height)
1136 vga_text_y = vga_text_height - 1;
1137 VGA_ScrollUpText( 0, 0,
1138 vga_text_height - 1, vga_text_width - 1,
1139 1, vga_text_attr );
1143 * If we don't have a console, write directly to standard output.
1145 if(!vga_text_console)
1146 WriteFile(VGA_AlphaConsole(), &ascii, 1, &w, NULL);
1148 LeaveCriticalSection(&vga_lock);
1151 void VGA_SetTextAttribute(BYTE attr)
1153 vga_text_attr = attr;
1156 void VGA_ClearText(unsigned row1, unsigned col1,
1157 unsigned row2, unsigned col2,
1158 BYTE attr)
1160 unsigned x, y;
1162 EnterCriticalSection(&vga_lock);
1164 for(y=row1; y<=row2; y++)
1165 for(x=col1; x<=col2; x++)
1166 VGA_PutCharAt(x, y, 0x20, attr);
1168 LeaveCriticalSection(&vga_lock);
1171 void VGA_ScrollUpText(unsigned row1, unsigned col1,
1172 unsigned row2, unsigned col2,
1173 unsigned lines, BYTE attr)
1175 char *buffer = VGA_AlphaBuffer();
1176 unsigned y;
1178 EnterCriticalSection(&vga_lock);
1181 * Scroll buffer.
1183 for (y = row1; y <= row2 - lines; y++)
1184 memmove( buffer + col1 + y * vga_text_width * 2,
1185 buffer + col1 + (y + lines) * vga_text_width * 2,
1186 (col2 - col1 + 1) * 2 );
1189 * Fill exposed lines.
1191 for (y = max(row1, row2 - lines + 1); y <= row2; y++)
1192 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1194 LeaveCriticalSection(&vga_lock);
1197 void VGA_ScrollDownText(unsigned row1, unsigned col1,
1198 unsigned row2, unsigned col2,
1199 unsigned lines, BYTE attr)
1201 char *buffer = VGA_AlphaBuffer();
1202 unsigned y;
1204 EnterCriticalSection(&vga_lock);
1207 * Scroll buffer.
1209 for (y = row2; y >= row1 + lines; y--)
1210 memmove( buffer + col1 + y * vga_text_width * 2,
1211 buffer + col1 + (y - lines) * vga_text_width * 2,
1212 (col2 - col1 + 1) * 2 );
1215 * Fill exposed lines.
1217 for (y = row1; y <= min(row1 + lines - 1, row2); y++)
1218 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1220 LeaveCriticalSection(&vga_lock);
1223 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
1225 char *dat;
1227 dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
1229 *ascii = dat[0];
1230 *attr = dat[1];
1234 /*** CONTROL ***/
1236 /* FIXME: optimize by doing this only if the data has actually changed
1237 * (in a way similar to DIBSection, perhaps) */
1238 static void VGA_Poll_Graphics(void)
1240 unsigned int Pitch, Height, Width, X, Y;
1241 char *surf;
1242 char *dat = vga_fb_data + vga_fb_offset;
1243 int bpp = (vga_fb_depth + 7) / 8;
1245 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
1246 if (!surf) return;
1249 * Synchronize framebuffer contents.
1251 if (vga_fb_window != -1)
1252 VGA_SyncWindow( TRUE );
1255 * Double VGA framebuffer (320x200 -> 640x400), if needed.
1257 if(Height >= 2 * vga_fb_height && Width >= 2 * vga_fb_width && bpp == 1)
1258 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch*2,dat+=vga_fb_pitch)
1259 for (X=0; X<vga_fb_width; X++) {
1260 BYTE value = dat[X];
1261 surf[X*2] = value;
1262 surf[X*2+1] = value;
1263 surf[X*2+Pitch] = value;
1264 surf[X*2+Pitch+1] = value;
1266 else
1267 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch,dat+=vga_fb_pitch)
1268 memcpy(surf, dat, vga_fb_width * bpp);
1270 VGA_Unlock();
1273 static void VGA_Poll_Text(void)
1275 char *dat, *old, *p_line;
1276 unsigned int X, Y;
1277 CHAR_INFO ch[256]; /* that should suffice for the largest text width */
1278 COORD siz, off;
1279 SMALL_RECT dest;
1280 HANDLE con = VGA_AlphaConsole();
1281 BOOL linechanged = FALSE; /* video memory area differs from stored copy? */
1283 /* Synchronize cursor position. */
1284 off.X = vga_text_x;
1285 off.Y = vga_text_y;
1286 SetConsoleCursorPosition(con,off);
1288 dat = VGA_AlphaBuffer();
1289 old = vga_text_old; /* pointer to stored video mem copy */
1290 siz.X = vga_text_width; siz.Y = 1;
1291 off.X = 0; off.Y = 0;
1293 /* copy from virtual VGA frame buffer to console */
1294 for (Y=0; Y<vga_text_height; Y++) {
1295 linechanged = memcmp(dat, old, vga_text_width*2);
1296 if (linechanged)
1298 /*TRACE("line %d changed\n", Y);*/
1299 p_line = dat;
1300 for (X=0; X<vga_text_width; X++) {
1301 ch[X].Char.AsciiChar = *p_line++;
1302 /* WriteConsoleOutputA doesn't like "dead" chars */
1303 if (ch[X].Char.AsciiChar == '\0')
1304 ch[X].Char.AsciiChar = ' ';
1305 ch[X].Attributes = *p_line++;
1307 dest.Top=Y; dest.Bottom=Y;
1308 dest.Left=0; dest.Right=vga_text_width+1;
1309 WriteConsoleOutputA(con, ch, siz, off, &dest);
1310 memcpy(old, dat, vga_text_width*2);
1312 /* advance to next text line */
1313 dat += vga_text_width*2;
1314 old += vga_text_width*2;
1318 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
1320 EnterCriticalSection(&vga_lock);
1322 if (lpddraw)
1323 VGA_Poll_Graphics();
1324 else
1325 VGA_Poll_Text();
1328 * Fake start of retrace.
1330 vga_retrace_vertical = TRUE;
1332 LeaveCriticalSection(&vga_lock);
1335 static BYTE palreg,palcnt;
1336 static PALETTEENTRY paldat;
1338 void VGA_ioport_out( WORD port, BYTE val )
1340 switch (port) {
1341 case 0x3c0:
1342 if (vga_address_3c0)
1343 vga_index_3c0 = val;
1344 else
1345 FIXME("Unsupported index, register 0x3c0: 0x%02x (value 0x%02x)\n",
1346 vga_index_3c0, val);
1347 vga_address_3c0 = !vga_address_3c0;
1348 break;
1349 case 0x3c4:
1350 vga_index_3c4 = val;
1351 break;
1352 case 0x3c5:
1353 switch(vga_index_3c4) {
1354 case 0x04: /* Sequencer: Memory Mode Register */
1355 if(vga_fb_depth == 8)
1356 VGA_SetWindowStart((val & 8) ? 0 : -1);
1357 else
1358 FIXME("Memory Mode Register not supported in this mode.\n");
1359 break;
1360 default:
1361 FIXME("Unsupported index, register 0x3c4: 0x%02x (value 0x%02x)\n",
1362 vga_index_3c4, val);
1364 break;
1365 case 0x3c8:
1366 palreg=val; palcnt=0; break;
1367 case 0x3c9:
1368 ((BYTE*)&paldat)[palcnt++]=val << 2;
1369 if (palcnt==3) {
1370 VGA_SetPalette(&paldat,palreg++,1);
1371 palcnt=0;
1373 break;
1374 case 0x3ce:
1375 vga_index_3ce = val;
1376 break;
1377 case 0x3cf:
1378 FIXME("Unsupported index, register 0x3ce: 0x%02x (value 0x%02x)\n",
1379 vga_index_3ce, val);
1380 break;
1381 case 0x3d4:
1382 vga_index_3d4 = val;
1383 break;
1384 case 0x3d5:
1385 FIXME("Unsupported index, register 0x3d4: 0x%02x (value 0x%02x)\n",
1386 vga_index_3d4, val);
1387 break;
1388 default:
1389 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1393 BYTE VGA_ioport_in( WORD port )
1395 BYTE ret;
1397 switch (port) {
1398 case 0x3c1:
1399 FIXME("Unsupported index, register 0x3c0: 0x%02x\n",
1400 vga_index_3c0);
1401 return 0xff;
1402 case 0x3c5:
1403 switch(vga_index_3c4) {
1404 case 0x04: /* Sequencer: Memory Mode Register */
1405 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1406 default:
1407 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1408 vga_index_3c4);
1409 return 0xff;
1411 case 0x3cf:
1412 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1413 vga_index_3ce);
1414 return 0xff;
1415 case 0x3d5:
1416 FIXME("Unsupported index, register 0x3d4: 0x%02x\n",
1417 vga_index_3d4);
1418 return 0xff;
1420 case 0x3da:
1422 * Read from this register resets register 0x3c0 address flip-flop.
1424 vga_address_3c0 = TRUE;
1427 * Read from this register returns following bits:
1428 * xxxx1xxx = Vertical retrace in progress if set.
1429 * xxxxx1xx = Light pen switched on.
1430 * xxxxxx1x = Light pen trigger set.
1431 * xxxxxxx1 = Either vertical or horizontal retrace
1432 * in progress if set.
1434 ret = 0;
1435 if (vga_retrace_vertical)
1436 ret |= 9;
1437 if (vga_retrace_horizontal)
1438 ret |= 3;
1441 * If VGA mode has been set, vertical retrace is
1442 * turned on once a frame and cleared after each read.
1443 * This might cause applications that synchronize with
1444 * vertical retrace to actually skip one frame but that
1445 * is probably not a problem.
1447 * If no VGA mode has been set, vertical retrace is faked
1448 * by toggling the value after every read.
1450 if (VGA_IsTimerRunning())
1451 vga_retrace_vertical = FALSE;
1452 else
1453 vga_retrace_vertical = !vga_retrace_vertical;
1456 * Toggle horizontal retrace.
1458 vga_retrace_horizontal = !vga_retrace_horizontal;
1459 break;
1461 default:
1462 ret=0xff;
1463 FIXME("Unsupported VGA register: 0x%04x\n", port);
1465 return ret;
1468 void VGA_Clean(void)
1470 VGA_Exit();
1471 VGA_DeinstallTimer();