quartz: Sign-compare warnings fix.
[wine] / dlls / winedos / vga.c
1 /*
2  * VGA hardware emulation
3  *
4  * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <string.h>
23
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"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
37
38 static IDirectDraw *lpddraw = NULL;
39 static IDirectDrawSurface *lpddsurf;
40 static IDirectDrawPalette *lpddpal;
41 static DDSURFACEDESC sdesc;
42
43 static BOOL vga_retrace_vertical;
44 static BOOL vga_retrace_horizontal;
45
46 /*
47  * Size and location of VGA controller window to framebuffer.
48  *
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.
52  */
53 #define VGA_WINDOW_SIZE  (64 * 1024)
54 #define VGA_WINDOW_START ((char *)0xa0000)
55
56 /*
57  * VGA controller memory is emulated using linear framebuffer.
58  * This frambuffer also acts as an interface
59  * between VGA controller emulation and DirectDraw.
60  *
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).
85  */
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;
94
95 /*
96  * VGA text mode data.
97  *
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.
107  */
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;
115
116 /*
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_*.
122  *
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.
127  */
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;
133
134 /*
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.
139  *
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.
144  */
145 static CRITICAL_SECTION vga_lock;
146 static CRITICAL_SECTION_DEBUG critsect_debug =
147 {
148     0, 0, &vga_lock,
149     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
150       0, 0, { (DWORD_PTR)(__FILE__ ": vga_lock") }
151 };
152 static CRITICAL_SECTION vga_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
153
154 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
155 static DirectDrawCreateProc pDirectDrawCreate;
156
157 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
158
159 static HWND vga_hwnd = NULL;
160
161 /*
162  *  VGA Palette Registers, in actual 18 bit color
163  *  port 3C0H - 6 bit rgbRGB format
164  *
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
168  */
169
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      */
188 };
189
190 /*
191  *  Mode 19 Default Color Register Setting
192  *  DAC palette registers, converted from actual 18 bit color to 24.
193  */
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}
457 };
458
459 /*
460  *  Mode 18 Default Color Register Setting
461  *  DAC palette registers, converted from actual 18 bit color to 24.
462  *
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
466  */
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  */
534 };
535
536 static HANDLE VGA_timer;
537 static HANDLE VGA_timer_thread;
538
539 /* set the timer rate; called in the polling thread context */
540 static void CALLBACK set_timer_rate( ULONG_PTR arg )
541 {
542     LARGE_INTEGER when;
543
544     when.u.LowPart = when.u.HighPart = 0;
545     SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
546 }
547
548 static DWORD CALLBACK VGA_TimerThread( void *dummy )
549 {
550     for (;;) SleepEx( INFINITE, TRUE );
551     return 0;
552 }
553
554 static void VGA_DeinstallTimer(void)
555 {
556     if (VGA_timer_thread)
557     {
558         /*
559          * Make sure the update thread is not holding
560          * system resources when we kill it.
561          *
562          * Now, we only need to worry about update thread
563          * getting terminated while in EnterCriticalSection 
564          * or WaitForMultipleObjectsEx.
565          *
566          * FIXME: Is this a problem?
567          */
568         EnterCriticalSection(&vga_lock);
569
570         CancelWaitableTimer( VGA_timer );
571         CloseHandle( VGA_timer );
572         TerminateThread( VGA_timer_thread, 0 );
573         CloseHandle( VGA_timer_thread );
574         VGA_timer_thread = 0;
575
576         LeaveCriticalSection(&vga_lock);
577
578         /*
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.
582          */
583         VGA_Poll( 0, 0, 0 );
584     }
585 }
586
587 static void VGA_InstallTimer(unsigned Rate)
588 {
589     if (!VGA_timer_thread)
590     {
591         VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
592         VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
593     }
594     QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
595 }
596
597 static BOOL VGA_IsTimerRunning(void)
598 {
599     return VGA_timer_thread ? TRUE : FALSE;
600 }
601
602 static HANDLE VGA_AlphaConsole(void)
603 {
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);
607 }
608
609 static char*VGA_AlphaBuffer(void)
610 {
611     return (char *)0xb8000;
612 }
613
614 /*** GRAPHICS MODE ***/
615
616 typedef struct {
617   unsigned Xres, Yres, Depth;
618   int ret;
619 } ModeSet;
620
621
622 /**********************************************************************
623  *         VGA_SyncWindow
624  *
625  * Copy VGA window into framebuffer (if argument is TRUE) or
626  * part of framebuffer into VGA window (if argument is FALSE).
627  */
628 static void VGA_SyncWindow( BOOL target_is_fb )
629 {
630     int size = VGA_WINDOW_SIZE;
631
632     /* Window does not overlap framebuffer. */
633     if (vga_fb_window >= vga_fb_size)
634         return;
635
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;
639
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 );
644 }
645
646
647 static void WINAPI VGA_DoExit(ULONG_PTR arg)
648 {
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;
657 }
658
659 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
660 {
661     HRESULT     res;
662     ModeSet *par = (ModeSet *)arg;
663     par->ret=1;
664
665     if (lpddraw) VGA_DoExit(0);
666     if (!lpddraw) {
667         if (!pDirectDrawCreate)
668         {
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;
674             }
675         }
676         res = pDirectDrawCreate(NULL,&lpddraw,NULL);
677         if (!lpddraw) {
678             ERR("DirectDraw is not available (res = 0x%x)\n",res);
679             return;
680         }
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;
690             }
691         }
692         else
693             SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
694
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);
698         }
699
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;
706         }
707
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;
714         }
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);
718         }
719
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;
730         }
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);
735     }
736     par->ret=0;
737     return;
738 }
739
740 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
741 {
742     ModeSet par;
743     int     newSize;
744
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);
750
751     newSize = Xres * Yres * ((Depth + 7) / 8);
752     if(newSize < 256 * 1024)
753       newSize = 256 * 1024;
754
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;
759     }
760
761     if(Xres >= 640 || Yres >= 480) {
762       par.Xres = Xres;
763       par.Yres = Yres;
764     } else {
765       par.Xres = 640;
766       par.Yres = 480;
767     }
768
769     VGA_SetWindowStart((Depth < 8) ? -1 : 0);
770
771     par.Depth = (Depth < 8) ? 8 : Depth;
772
773     MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
774     return par.ret;
775 }
776
777 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
778 {
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;
785 }
786
787 static void VGA_Exit(void)
788 {
789     if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
790 }
791
792 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
793 {
794     if (!lpddraw) return;
795     IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
796 }
797
798 /* set a single [char wide] color in 16 color mode. */
799 void VGA_SetColor16(int reg,int color)
800 {
801         PALETTEENTRY *pal;
802
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;
807 }
808
809 /* Get a single [char wide] color in 16 color mode. */
810 char VGA_GetColor16(int reg)
811 {
812
813     if (!lpddraw) return 0;
814         return vga_16_palette[reg];
815 }
816
817 /* set all 17 [char wide] colors at once in 16 color mode. */
818 void VGA_Set16Palette(char *Table)
819 {
820         PALETTEENTRY *pal;
821         int c;
822
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 */
825
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 */
831 }
832
833 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
834 void VGA_Get16Palette(char *Table)
835 {
836
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 */
839 }
840
841 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
842 {
843     PALETTEENTRY pal[256];
844     int c;
845
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;
852     }
853     IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
854 }
855
856 static LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
857 {
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;
863     }
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;
869 }
870
871 static void VGA_Unlock(void)
872 {
873     IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
874 }
875
876 /*
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.
880  */
881 void VGA_SetWindowStart(int start)
882 {
883     if(start == vga_fb_window)
884         return;
885
886     EnterCriticalSection(&vga_lock);
887
888     if(vga_fb_window == -1)
889         FIXME("Remove VGA memory emulation.\n");
890     else
891         VGA_SyncWindow( TRUE );
892
893     vga_fb_window = start;
894
895     if(vga_fb_window == -1)
896         FIXME("Install VGA memory emulation.\n");
897     else
898         VGA_SyncWindow( FALSE );
899
900     LeaveCriticalSection(&vga_lock);
901 }
902
903 /*
904  * Get start of 64k window at 0xa0000 in bytes.
905  * Value is -1 in color plane modes.
906  */
907 int VGA_GetWindowStart(void)
908 {
909     return vga_fb_window;
910 }
911
912
913 /**********************************************************************
914  *         VGA_DoShowMouse
915  *
916  * Callback for VGA_ShowMouse.
917  */
918 static void WINAPI VGA_DoShowMouse( ULONG_PTR show )
919 {
920     INT rv;
921
922     do
923     {
924         rv = ShowCursor( show );
925     }
926     while( show ? (rv < 0) : (rv >= 0) );
927 }
928
929
930 /**********************************************************************
931  *         VGA_ShowMouse
932  *
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.
936  */
937 void VGA_ShowMouse( BOOL show )
938 {
939     if (lpddraw)
940         MZ_RunInThread( VGA_DoShowMouse, (ULONG_PTR)show );
941 }
942
943
944 /*** TEXT MODE ***/
945
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)
949 {
950     char *p, *p2;
951     unsigned int i;
952
953     /*
954      * Allocate space for char + attr.
955      */
956
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;
965
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 */
971 }
972
973 /**********************************************************************
974  *         VGA_SetAlphaMode
975  *
976  * Set VGA emulation to text mode.
977  */
978 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
979 {
980     VGA_Exit();
981     VGA_DeinstallTimer();
982     
983     VGA_PrepareVideoMemCopy(Xres, Yres);
984     vga_text_width = Xres;
985     vga_text_height = Yres;
986
987     if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
988         VGA_SetCursorPos(0,0);
989
990     if(vga_text_console) {
991         COORD size;
992         size.X = Xres;
993         size.Y = Yres;
994         SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
995
996         /* poll every 30ms (33fps should provide adequate responsiveness) */
997         VGA_InstallTimer(30);
998     }
999 }
1000
1001 /**********************************************************************
1002  *         VGA_InitAlphaMode
1003  *
1004  * Initialize VGA text mode handling and return default text mode.
1005  * This function does not set VGA emulation to text mode.
1006  */
1007 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
1008 {
1009     CONSOLE_SCREEN_BUFFER_INFO info;
1010
1011     if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
1012     {
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;
1019     } 
1020     else
1021     {
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;
1028     }
1029 }
1030
1031 /**********************************************************************
1032  *         VGA_GetAlphaMode
1033  *
1034  * Get current text mode. Returns TRUE and sets resolution if
1035  * any VGA text mode has been initialized.
1036  */
1037 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
1038 {
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;
1045 }
1046
1047 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
1048 {
1049     CONSOLE_CURSOR_INFO cci;
1050
1051     /* standard cursor settings:
1052      * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
1053
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 ? */
1058
1059     SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
1060 }
1061
1062 void VGA_SetCursorPos(unsigned X,unsigned Y)
1063 {
1064     vga_text_x = X;
1065     vga_text_y = Y;
1066 }
1067
1068 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
1069 {
1070     if (X) *X = vga_text_x;
1071     if (Y) *Y = vga_text_y;
1072 }
1073
1074 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
1075 {
1076     char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
1077     dat[0] = ascii;
1078     if (attr>=0)
1079         dat[1] = attr;
1080 }
1081
1082 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
1083 {
1084     EnterCriticalSection(&vga_lock);
1085
1086     while (count--) 
1087         VGA_PutCharAt(X + count, Y, ch, attr);
1088
1089     LeaveCriticalSection(&vga_lock);
1090 }
1091
1092 void VGA_PutChar(BYTE ascii)
1093 {
1094     DWORD w;
1095
1096     EnterCriticalSection(&vga_lock);
1097
1098     switch(ascii) {
1099     case '\b':
1100         if (vga_text_x)
1101         {
1102             vga_text_x--;
1103             VGA_PutCharAt(vga_text_x, vga_text_y, ' ', 0);
1104         }
1105         break;
1106
1107     case '\t':
1108         vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
1109         break;
1110
1111     case '\n':
1112         vga_text_y++;
1113         vga_text_x = 0;
1114         break;
1115
1116     case '\a':
1117         break;
1118
1119     case '\r':
1120         vga_text_x = 0;
1121         break;
1122
1123     default:
1124         VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
1125         vga_text_x++;
1126     }
1127
1128     if (vga_text_x >= vga_text_width)
1129     {
1130         vga_text_x = 0;
1131         vga_text_y++;
1132     }
1133
1134     if (vga_text_y >= vga_text_height)
1135     {
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 );
1140     }
1141
1142     /*
1143      * If we don't have a console, write directly to standard output.
1144      */
1145     if(!vga_text_console)
1146         WriteFile(VGA_AlphaConsole(), &ascii, 1, &w, NULL);
1147
1148     LeaveCriticalSection(&vga_lock);
1149 }
1150
1151 void VGA_SetTextAttribute(BYTE attr)
1152 {
1153     vga_text_attr = attr;
1154 }
1155
1156 void VGA_ClearText(unsigned row1, unsigned col1,
1157                    unsigned row2, unsigned col2,
1158                    BYTE attr)
1159 {
1160     unsigned x, y;
1161
1162     EnterCriticalSection(&vga_lock);
1163
1164     for(y=row1; y<=row2; y++)
1165         for(x=col1; x<=col2; x++)
1166             VGA_PutCharAt(x, y, 0x20, attr);
1167
1168     LeaveCriticalSection(&vga_lock);
1169 }
1170
1171 void VGA_ScrollUpText(unsigned row1,  unsigned col1,
1172                       unsigned row2,  unsigned col2,
1173                       unsigned lines, BYTE attr)
1174 {
1175     char    *buffer = VGA_AlphaBuffer();
1176     unsigned y;
1177
1178     EnterCriticalSection(&vga_lock);
1179
1180     /*
1181      * Scroll buffer.
1182      */
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 );
1187
1188     /*
1189      * Fill exposed lines.
1190      */
1191     for (y = max(row1, row2 - lines + 1); y <= row2; y++)
1192         VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1193
1194     LeaveCriticalSection(&vga_lock);
1195 }
1196
1197 void VGA_ScrollDownText(unsigned row1,  unsigned col1,
1198                         unsigned row2,  unsigned col2,
1199                         unsigned lines, BYTE attr)
1200 {
1201     char    *buffer = VGA_AlphaBuffer();
1202     unsigned y;
1203
1204     EnterCriticalSection(&vga_lock);
1205
1206     /*
1207      * Scroll buffer.
1208      */
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 );
1213
1214     /*
1215      * Fill exposed lines.
1216      */
1217     for (y = row1; y <= min(row1 + lines - 1, row2); y++)
1218         VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1219
1220     LeaveCriticalSection(&vga_lock);
1221 }
1222
1223 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
1224 {
1225     char *dat;
1226
1227     dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
1228
1229     *ascii = dat[0];
1230     *attr = dat[1];
1231 }
1232
1233
1234 /*** CONTROL ***/
1235
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)
1239 {
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;
1244
1245   surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
1246   if (!surf) return;
1247
1248   /*
1249    * Synchronize framebuffer contents.
1250    */
1251   if (vga_fb_window != -1)
1252       VGA_SyncWindow( TRUE );
1253
1254   /*
1255    * Double VGA framebuffer (320x200 -> 640x400), if needed.
1256    */
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;
1265       }
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);
1269
1270   VGA_Unlock();
1271 }
1272
1273 static void VGA_Poll_Text(void)
1274 {
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? */
1282
1283     /* Synchronize cursor position. */
1284     off.X = vga_text_x;
1285     off.Y = vga_text_y;
1286     SetConsoleCursorPosition(con,off);
1287
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;
1292
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)
1297         {
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++;
1306             }
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);
1311         }
1312         /* advance to next text line */
1313         dat += vga_text_width*2;
1314         old += vga_text_width*2;
1315     }
1316 }
1317
1318 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
1319 {
1320     EnterCriticalSection(&vga_lock);
1321
1322     if (lpddraw)
1323         VGA_Poll_Graphics();
1324     else
1325         VGA_Poll_Text();
1326
1327     /*
1328      * Fake start of retrace.
1329      */
1330     vga_retrace_vertical = TRUE;
1331
1332     LeaveCriticalSection(&vga_lock);
1333 }
1334
1335 static BYTE palreg,palcnt;
1336 static PALETTEENTRY paldat;
1337
1338 void VGA_ioport_out( WORD port, BYTE val )
1339 {
1340     switch (port) {
1341         /* General Register - Feature Control */
1342         case 0x3ba:
1343            FIXME("Unsupported VGA register: general register - feature control 0x%04x (value 0x%02x)\n", port, val);
1344            break;
1345         /* Attribute Controller - Address/Other */
1346         case 0x3c0:
1347            if (vga_address_3c0)
1348                vga_index_3c0 = val;
1349            else
1350                FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x (value 0x%02x)\n",
1351                      vga_index_3c0, val);
1352            vga_address_3c0 = !vga_address_3c0;
1353            break;
1354         /* General Register - Misc output */
1355         case 0x3c2:
1356            FIXME("Unsupported VGA register: general register - misc output 0x%04x (value 0x%02x)\n", port, val);
1357            break;
1358         /* General Register - Video subsystem enable */
1359         case 0x3c3:
1360            FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x (value 0x%02x)\n", port, val);
1361            break;
1362         /* Sequencer Register - Address */
1363         case 0x3c4:
1364            vga_index_3c4 = val;
1365            break;
1366         /* Sequencer Register - Other */
1367         case 0x3c5:
1368           switch(vga_index_3c4) {
1369                case 0x04: /* Sequencer: Memory Mode Register */
1370                   if(vga_fb_depth == 8)
1371                       VGA_SetWindowStart((val & 8) ? 0 : -1);
1372                   else
1373                       FIXME("Memory Mode Register not supported in this mode.\n");
1374                   break;
1375                default:
1376                   FIXME("Unsupported index, VGA sequencer register 0x3c4: 0x%02x (value 0x%02x)\n",
1377                         vga_index_3c4, val);
1378            }
1379            break;
1380         case 0x3c8:
1381             palreg=val; palcnt=0; break;
1382         case 0x3c9:
1383             ((BYTE*)&paldat)[palcnt++]=val << 2;
1384             if (palcnt==3) {
1385                 VGA_SetPalette(&paldat,palreg++,1);
1386                 palcnt=0;
1387             }
1388             break;
1389         /* Graphics Controller Register - Address */
1390         case 0x3ce:
1391             vga_index_3ce = val;
1392            break;
1393         /* Graphics Controller Register - Other */
1394         case 0x3cf:
1395            FIXME("Unsupported index, VGA graphics controller register - other 0x3ce: 0x%02x (value 0x%02x)\n",
1396                  vga_index_3ce, val);
1397            break;
1398         /* CRT Controller Register - Index (MDA) */
1399         case 0x3b4:
1400         /* CRT Controller Register - Index (CGA) */
1401         case 0x3d4:
1402            vga_index_3d4 = val;
1403            break;
1404         /* CRT Controller Register - Other (MDA) */
1405         case 0x3b5:
1406         /* CRT Controller Register - Other (CGA) */
1407         case 0x3d5:
1408            FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x (value 0x%02x)\n",
1409                  vga_index_3d4, val);
1410            break;
1411         default:
1412             FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1413     }
1414 }
1415
1416 BYTE VGA_ioport_in( WORD port )
1417 {
1418     BYTE ret;
1419
1420     switch (port) {
1421         /* Attribute Controller - Other */
1422         case 0x3c1:
1423            FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x\n",
1424                  vga_index_3c0);
1425            return 0xff;
1426         /* General Register - Input status 0 */
1427         case 0x3c2:
1428            ret=0xff;
1429            FIXME("Unsupported VGA register: general register - input status 0 0x%04x\n", port);
1430            break;
1431         /* General Register - Video subsystem enable */
1432         case 0x3c3:
1433            ret=0xff;
1434            FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x\n", port);
1435            break;
1436         /* Sequencer Register - Other */
1437         case 0x3c5:
1438            switch(vga_index_3c4) {
1439                case 0x04: /* Sequencer: Memory Mode Register */
1440                     return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1441                default:
1442                    FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1443                          vga_index_3c4);
1444                    return 0xff;
1445            }
1446         /* General Register -  DAC State */
1447         case 0x3c7:
1448            ret=0xff;
1449            FIXME("Unsupported VGA register: general register - DAC State 0x%04x\n", port);
1450            break;
1451         /* General Register - Feature control */
1452         case 0x3ca:
1453            ret=0xff;
1454            FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port);
1455            break;
1456         /* General Register - Misc output */
1457         case 0x3cc:
1458            ret=0xff;
1459            FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port);
1460            break;
1461         /* Graphics Controller Register - Other */
1462         case 0x3cf:
1463            FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1464                  vga_index_3ce);
1465            return 0xff;
1466         /* CRT Controller Register - Other (MDA) */
1467         case 0x3b5:
1468         /* CRT Controller Register - Other (CGA) */
1469         case 0x3d5:
1470            FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x\n",
1471                  vga_index_3d4);
1472            return 0xff;
1473         /* General Register - Input status 1 (MDA) */
1474         case 0x3ba:
1475         /* General Register - Input status 1 (CGA) */
1476         case 0x3da:
1477             /*
1478              * Read from this register resets register 0x3c0 address flip-flop.
1479              */
1480             vga_address_3c0 = TRUE;
1481
1482             /*
1483              * Read from this register returns following bits:
1484              *   xxxx1xxx = Vertical retrace in progress if set.
1485              *   xxxxx1xx = Light pen switched on.
1486              *   xxxxxx1x = Light pen trigger set.
1487              *   xxxxxxx1 = Either vertical or horizontal retrace 
1488              *              in progress if set.
1489              */
1490             ret = 0;
1491             if (vga_retrace_vertical)
1492                 ret |= 9;
1493             if (vga_retrace_horizontal)
1494                 ret |= 3;
1495             
1496             /*
1497              * If VGA mode has been set, vertical retrace is
1498              * turned on once a frame and cleared after each read.
1499              * This might cause applications that synchronize with
1500              * vertical retrace to actually skip one frame but that
1501              * is probably not a problem.
1502              * 
1503              * If no VGA mode has been set, vertical retrace is faked
1504              * by toggling the value after every read.
1505              */
1506             if (VGA_IsTimerRunning())
1507                 vga_retrace_vertical = FALSE;
1508             else
1509                 vga_retrace_vertical = !vga_retrace_vertical;
1510
1511             /*
1512              * Toggle horizontal retrace.
1513              */
1514             vga_retrace_horizontal = !vga_retrace_horizontal;
1515             break;
1516
1517         default:
1518             ret=0xff;
1519             FIXME("Unsupported VGA register: 0x%04x\n", port);
1520     }
1521     return ret;
1522 }
1523
1524 void VGA_Clean(void)
1525 {
1526     VGA_Exit();
1527     VGA_DeinstallTimer();
1528 }