wined3d: Mark the draw buffer as dirty in context_create().
[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  * Size and location of CGA controller window to framebuffer.
58  */
59 #define CGA_WINDOW_SIZE  (32 * 1024)
60 #define CGA_WINDOW_START ((char *)0xb8000)
61
62 /*
63  * VGA controller memory is emulated using linear framebuffer.
64  * This frambuffer also acts as an interface
65  * between VGA controller emulation and DirectDraw.
66  *
67  * vga_fb_width: Display width in pixels. Can be modified when
68  *               display mode is changed.
69  * vga_fb_height: Display height in pixels. Can be modified when
70  *                display mode is changed.
71  * vga_fb_depth: Number of bits used to store single pixel color information.
72  *               Each pixel uses (vga_fb_depth+7)/8 bytes because
73  *               1-16 color modes are mapped to 256 color mode.
74  *               Can be modified when display mode is changed.
75  * vga_fb_pitch: How many bytes to add to pointer in order to move
76  *               from one row to another. This is fixed in VGA modes,
77  *               but can be modified in SVGA modes.
78  * vga_fb_offset: Offset added to framebuffer start address in order
79  *                to find the display origin. Programs use this to do
80  *                double buffering and to scroll display. The value can
81  *                be modified in VGA and SVGA modes.
82  * vga_fb_size: How many bytes are allocated to framebuffer.
83  *              VGA framebuffers are always larger than display size and
84  *              SVGA framebuffers may also be.
85  * vga_fb_data: Pointer to framebuffer start.
86  * vga_fb_window: Offset of 64k window 0xa0000 in bytes from framebuffer start.
87  *                This value is >= 0, if mode uses linear framebuffer and
88  *                -1, if mode uses color planes. This value is fixed
89  *                in all modes except 0x13 (256 color VGA) where
90  *                0 means normal mode and -1 means Mode-X (unchained mode).
91  */
92 static int   vga_fb_width;
93 static int   vga_fb_height;
94 static int   vga_fb_depth;
95 static int   vga_fb_pitch;
96 static int   vga_fb_offset;
97 static int   vga_fb_size = 0;
98 static char *vga_fb_data = 0;
99 static int   vga_fb_window = 0;
100 static int   vga_fb_window_size;
101 static char *vga_fb_window_data;
102 static PALETTEENTRY *vga_fb_palette;
103 static unsigned vga_fb_palette_index;
104 static unsigned vga_fb_palette_size;
105 static BOOL  vga_fb_bright;
106 static BOOL  vga_fb_enabled;
107
108 /*
109  * VGA text mode data.
110  *
111  * vga_text_attr: Current active attribute.
112  * vga_text_old: Last data sent to console. 
113  *               This is used to optimize console updates.
114  * vga_text_width:  Width of the text display in characters.
115  * vga_text_height: Height of the text display in characters.
116  * vga_text_x: Current cursor X-position. Starts from zero.
117  * vga_text_y: Current cursor Y-position. Starts from zero.
118  * vga_text_console: TRUE if stdout is console, 
119  *                   FALSE if it is regular file.
120  */
121 static BYTE  vga_text_attr;
122 static char *vga_text_old = NULL;
123 static BYTE  vga_text_width;
124 static BYTE  vga_text_height;
125 static BYTE  vga_text_x;
126 static BYTE  vga_text_y;
127 static BOOL  vga_text_console;
128
129 /*
130  * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
131  * indexed registers. These ports are used to select VGA controller
132  * subregister that can be written to or read from using ports 0x3c1,
133  * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
134  * stored in variables vga_index_*.
135  *
136  * Port 0x3c0 is special because it is both index and
137  * data-write register. Flip-flop vga_address_3c0 tells whether
138  * the port acts currently as an address register. Reading from port
139  * 0x3da resets the flip-flop to address mode.
140  */
141 static BYTE vga_index_3c0;
142 static BYTE vga_index_3c4;
143 static BYTE vga_index_3ce;
144 static BYTE vga_index_3d4;
145 static BOOL vga_address_3c0 = TRUE;
146
147 /*
148  * List of supported video modes.
149  *
150  * should be expanded to contain most or all information
151  * as required for SVGA VESA mode info block for VESA BIOS subsystem 1 (see _ModeInfoBlock)
152  * this will allow proper support for FIXME items in VGA/VESA mode configuration
153  *
154  * FIXME - verify and define support for VESA modes
155  * FIXME - add # bit planes, # video memory banks, & memory model
156  */
157 static WORD VGA_CurrentMode;
158 static BOOL CGA_ColorComposite = FALSE;  /* behave like composite monitor */
159
160 const VGA_MODE VGA_modelist[] =
161 {
162     /* Mode, ModeType, TextCols, TextRows, CharWidth, CharHeight, Width, Height, Depth, Colors, ScreenPages, Supported*/
163     /* VGA modes */
164     {0x0000,    TEXT, 40, 25,  9, 16,  360,  400,  0,  16, 8, TRUE},   /* VGA/CGA text mode 0 */
165     {0x0001,    TEXT, 40, 25,  9, 16,  360,  400,  0,  16, 8, TRUE},   /* VGA/CGA text mode 1 */
166     {0x0002,    TEXT, 80, 25,  9, 16,  360,  400,  0,  16, 8, TRUE},   /* VGA/CGA text mode 2 */
167     {0x0003,    TEXT, 80, 25,  9, 16,  360,  400,  0,  16, 8, TRUE},   /* VGA/CGA text mode 3 */
168     {0x0004, GRAPHIC, 40, 25,  8,  8,  320,  200,  2,   4, 1, TRUE},   /* VGA/CGA graphics mode 4 */
169     {0x0005, GRAPHIC, 40, 25,  8,  8,  320,  200,  2,   4, 1, TRUE},   /* VGA/CGA graphics mode 5 */
170     {0x0006, GRAPHIC, 80, 25,  8,  8,  640,  200,  1,   2, 1, TRUE},   /* VGA/CGA graphics mode 6 */
171     {0x0007,    TEXT, 80, 25,  9, 16,  720,  400,  0,   0, 8, FALSE},   /* VGA text mode 7 - FIXME bad default address */
172     {0x000d, GRAPHIC, 40, 25,  8,  8,  320,  200,  4,  16, 8, FALSE},   /* VGA graphics mode 13 */
173     {0x000e, GRAPHIC, 80, 25,  8,  8,  640,  200,  4,  16, 4, FALSE},   /* VGA graphics mode 14 */
174     {0x000f, GRAPHIC, 80, 25,  8, 14,  640,  350,  0,   0, 2, FALSE},   /* VGA graphics mode 15 */
175     {0x0010, GRAPHIC, 80, 25,  8, 14,  640,  350,  4,  16, 2, FALSE},   /* VGA graphics mode 16 */
176     {0x0012, GRAPHIC, 80, 30,  8, 16,  640,  480,  1,   2, 1, FALSE},   /* VGA graphics mode 17 */
177     {0x0012, GRAPHIC, 80, 30,  8, 16,  640,  480,  4,  16, 1, FALSE},   /* VGA graphics mode 18 */
178     {0x0013, GRAPHIC, 40, 25,  8,  8,  320,  200,  8, 256, 1, TRUE},   /* VGA graphics mode 19 */
179     /* VESA 7-bit modes */
180     {0x006a, GRAPHIC,  0,  0,  0,  0,  800,  600,  4,  16, 1, TRUE},   /* VESA graphics mode, same as 0x102 */
181     /* VESA 15-bit modes */
182     {0x0100, GRAPHIC,  0,  0,  0,  0,  640,  400,  8, 256, 1, TRUE},   /* VESA graphics mode */
183     {0x0101, GRAPHIC,  0,  0,  0,  0,  640,  480,  8, 256, 1, TRUE},   /* VESA graphics mode */
184     {0x0102, GRAPHIC,  0,  0,  0,  0,  800,  600,  4,  16, 1, TRUE},   /* VESA graphics mode */
185     {0x0103, GRAPHIC,  0,  0,  0,  0,  800,  600,  8, 256, 1, TRUE},   /* VESA graphics mode */
186     {0x0104, GRAPHIC,  0,  0,  0,  0, 1024,  768,  4,  16, 1, TRUE},   /* VESA graphics mode */
187     {0x0105, GRAPHIC,  0,  0,  0,  0, 1024,  768,  8, 256, 1, TRUE},   /* VESA graphics mode */
188     {0x0106, GRAPHIC,  0,  0,  0,  0, 1280, 1024,  4,  16, 1, TRUE},   /* VESA graphics mode */
189     {0x0107, GRAPHIC,  0,  0,  0,  0, 1280, 1024,  8, 256, 1, TRUE},   /* VESA graphics mode */
190     {0x0108,    TEXT,  0,  0,  0,  0,   80,   60,  0,   0, 1, TRUE},   /* VESA text mode */
191     {0x0109,    TEXT,  0,  0,  0,  0,  132,   25,  0,   0, 1, TRUE},   /* VESA text mode */
192     {0x010a,    TEXT,  0,  0,  0,  0,  132,   43,  0,   0, 1, TRUE},   /* VESA text mode */
193     {0x010b,    TEXT,  0,  0,  0,  0,  132,   50,  0,   0, 1, TRUE},   /* VESA text mode */
194     {0x010c,    TEXT,  0,  0,  0,  0,  132,   60,  0,   0, 1, TRUE},   /* VESA text mode */
195     /* VESA 1.2 modes */
196     {0x010d, GRAPHIC,  0,  0,  0,  0,  320,  200, 15,   0, 1, TRUE},   /* VESA graphics mode, 32K colors */
197     {0x010e, GRAPHIC,  0,  0,  0,  0,  320,  200, 16,   0, 1, TRUE},   /* VESA graphics mode, 64K colors */
198     {0x010f, GRAPHIC,  0,  0,  0,  0,  320,  200, 24,   0, 1, TRUE},   /* VESA graphics mode, 16.8 Million colors */
199     {0x0110, GRAPHIC,  0,  0,  0,  0,  640,  480, 15,   0, 1, TRUE},   /* VESA graphics mode, 32K colors */
200     {0x0111, GRAPHIC,  0,  0,  0,  0,  640,  480, 16,   0, 1, TRUE},   /* VESA graphics mode, 64K colors */
201     {0x0112, GRAPHIC,  0,  0,  0,  0,  640,  480, 24,   0, 1, TRUE},   /* VESA graphics mode, 16.8 Million colors */
202     {0x0113, GRAPHIC,  0,  0,  0,  0,  800,  600, 15,   0, 1, TRUE},   /* VESA graphics mode, 32K colors */
203     {0x0114, GRAPHIC,  0,  0,  0,  0,  800,  600, 16,   0, 1, TRUE},   /* VESA graphics mode, 64K colors */
204     {0x0115, GRAPHIC,  0,  0,  0,  0,  800,  600, 24,   0, 1, TRUE},   /* VESA graphics mode, 16.8 Million colors */
205     {0x0116, GRAPHIC,  0,  0,  0,  0, 1024,  768, 15,   0, 1, TRUE},   /* VESA graphics mode, 32K colors */
206     {0x0117, GRAPHIC,  0,  0,  0,  0, 1024,  768, 16,   0, 1, TRUE},   /* VESA graphics mode, 64K colors */
207     {0x0118, GRAPHIC,  0,  0,  0,  0, 1024,  768, 24,   0, 1, TRUE},   /* VESA graphics mode, 16.8 Million colors */
208     {0x0119, GRAPHIC,  0,  0,  0,  0, 1280, 1024, 15,   0, 1, TRUE},   /* VESA graphics mode, 32K colors */
209     {0x011a, GRAPHIC,  0,  0,  0,  0, 1280, 1024, 16,   0, 1, TRUE},   /* VESA graphics mode, 64K colors */
210     {0x011b, GRAPHIC,  0,  0,  0,  0, 1280, 1024, 24,   0, 1, TRUE},   /* VESA graphics mode, 16.8 Million colors */
211     {0xffff,    TEXT,  0,  0,  0,  0,    0,    0,  0,   0, 1, FALSE}
212 };
213
214 /*
215  * This mutex is used to protect VGA state during asynchronous
216  * screen updates (see VGA_Poll). It makes sure that VGA state changes
217  * are atomic and the user interface is protected from flicker and
218  * corruption.
219  *
220  * The mutex actually serializes VGA operations and the screen update. 
221  * Which means that whenever VGA_Poll occurs, application stalls if it 
222  * tries to modify VGA state. This is not how real VGA adapters work,
223  * but it makes timing and correctness issues much easier to deal with.
224  */
225 static CRITICAL_SECTION vga_lock;
226 static CRITICAL_SECTION_DEBUG critsect_debug =
227 {
228     0, 0, &vga_lock,
229     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
230       0, 0, { (DWORD_PTR)(__FILE__ ": vga_lock") }
231 };
232 static CRITICAL_SECTION vga_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
233
234 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
235 static DirectDrawCreateProc pDirectDrawCreate;
236
237 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
238
239 static HWND vga_hwnd = NULL;
240
241 /*
242  * CGA palette 1
243  */
244 static PALETTEENTRY cga_palette1[] = {
245   {0x00, 0x00, 0x00}, /* 0 - Black */
246   {0x00, 0xAA, 0xAA}, /* 1 - Cyan */
247   {0xAA, 0x00, 0xAA}, /* 2 - Magenta */
248   {0xAA, 0xAA, 0xAA}  /* 3 - White */
249 };
250
251 /*
252  * CGA palette 1 in the bright variant
253  * intensities, when signalled to do so
254  * in register 0x3d9
255  */
256 static PALETTEENTRY cga_palette1_bright[] = {
257   {0x00, 0x00, 0x00}, /* 0 - Black */
258   {0x55, 0xFF, 0xFF}, /* 1 - Light cyan */
259   {0xFF, 0x55, 0xFF}, /* 2 - Light magenta */
260   {0xFF, 0xFF, 0xFF}, /* 3 - Bright White */
261 };
262
263 /*
264  * CGA palette 2
265  */
266 static PALETTEENTRY cga_palette2[] = {
267   {0x00, 0x00, 0x00}, /* 0 - Black */
268   {0x00, 0xAA, 0x00}, /* 1 - Green */
269   {0xAA, 0x00, 0x00}, /* 2 - Red */
270   {0xAA, 0x55, 0x00}  /* 3 - Brown */
271 };
272
273 /*
274  * CGA palette 2 in the bright variant
275  * intensities, when signalled to do so
276  * in register 0x3d9
277  */
278 static PALETTEENTRY cga_palette2_bright[] = {
279   {0x00, 0x00, 0x00}, /* 0 - Black */
280   {0x55, 0xFF, 0x55}, /* 1 - Light green */
281   {0xFF, 0x55, 0x55}, /* 2 - Light red */
282   {0xFF, 0xFF, 0x55}, /* 3 - Yellow */
283 };
284
285 /*
286  *  VGA Palette Registers, in actual 16 bit color
287  *  port 3C0H - 6 bit rgbRGB format
288  *
289  *  16 color accesses will use these pointers and insert
290  *  entries from the 64-color palette (mode 18) into the default
291  *  palette.   --Robert 'Admiral' Coeyman
292  */
293 static char vga_16_palette[17]={
294   0x00,  /* 0 - Black         */
295   0x01,  /* 1 - Blue          */
296   0x02,  /* 2 - Green         */
297   0x03,  /* 3 - Cyan          */
298   0x04,  /* 4 - Red           */
299   0x05,  /* 5 - Magenta       */
300   0x14,  /* 6 - Brown         */
301   0x07,  /* 7 - White (Light gray)        */
302   0x38,  /* 8 - Dark gray     */
303   0x39,  /* 9 - Light blue    */
304   0x3a,  /* A - Light green   */
305   0x3b,  /* B - Light cyan    */
306   0x3c,  /* C - Light red     */
307   0x3d,  /* D - Light magenta */
308   0x3e,  /* E - Yellow        */
309   0x3f,  /* F - Bright White (White) */
310   0x00   /* Border Color      */
311 };
312
313 /*
314  *  Mode 19 Default Color Register Setting
315  *  DAC palette registers, converted from actual 18 bit color to 24.
316  */
317 static PALETTEENTRY vga_def_palette[256]={
318 /* red  green  blue */
319   /* 16 colors in IRGB values */
320   {0x00, 0x00, 0x00}, /* 0 (0) - Black */
321   {0x00, 0x00, 0xAA}, /* 1 (1) - Blue */
322   {0x00, 0xAA, 0x00}, /* 2 (2) - Green */
323   {0x00, 0xAA, 0xAA}, /* 3 (3) - Cyan */
324   {0xAA, 0x00, 0x00}, /* 4 (4) - Red */
325   {0xAA, 0x00, 0xAA}, /* 5 (5) - Magenta */
326   {0xAA, 0x55, 0x00}, /* 6 (6) - Brown */
327   {0xAA, 0xAA, 0xAA}, /* 7 (7) - White (Light gray) */
328   {0x55, 0x55, 0x55}, /* 8 (8) - Dark gray */
329   {0x55, 0x55, 0xFF}, /* 9 (9) - Light blue */
330   {0x55, 0xFF, 0x55}, /* 10 (A) - Light green */
331   {0x55, 0xFF, 0xFF}, /* 11 (B) - Light cyan */
332   {0xFF, 0x55, 0x55}, /* 12 (C) - Light red */
333   {0xFF, 0x55, 0xFF}, /* 13 (D) - Light magenta */
334   {0xFF, 0xFF, 0x55}, /* 14 (E) -  Yellow */
335   {0xFF, 0xFF, 0xFF}, /* 15 (F) - Bright White (White) */
336   /* 16 shades of gray */
337   {0x00, 0x00, 0x00}, /* 16 (10) */
338   {0x10, 0x10, 0x10}, /* 17 (11) */
339   {0x20, 0x20, 0x20}, /* 18 (12) */
340   {0x35, 0x35, 0x35}, /* 19 (13) */
341   {0x45, 0x45, 0x45}, /* 20 (14) */
342   {0x55, 0x55, 0x55}, /* 21 (15) */
343   {0x65, 0x65, 0x65}, /* 22 (16) */
344   {0x75, 0x75, 0x75}, /* 23 (17) */
345   {0x8A, 0x8A, 0x8A}, /* 24 (18) */
346   {0x9A, 0x9A, 0x9A}, /* 25 (19) */
347   {0xAA, 0xAA, 0xAA}, /* 26 (1A) */
348   {0xBA, 0xBA, 0xBA}, /* 27 (1B) */
349   {0xCA, 0xCA, 0xCA}, /* 28 (1C) */
350   {0xDF, 0xDF, 0xDF}, /* 29 (1D) */
351   {0xEF, 0xEF, 0xEF}, /* 30 (1E) */
352   {0xFF, 0xFF, 0xFF}, /* 31 (1F) */
353   /* High Intensity group - 72 colors in 1/3 saturation groups (20H-37H high) */
354   {0x00, 0x00, 0xFF}, /* 32 (20) */
355   {0x41, 0x00, 0xFF}, /* 33 (21) */
356   {0x82, 0x00, 0xFF}, /* 34 (22) */
357   {0xBE, 0x00, 0xFF}, /* 35 (23) */
358   {0xFF, 0x00, 0xFF}, /* 36 (24) */
359   {0xFF, 0x00, 0xBE}, /* 37 (25) */
360   {0xFF, 0x00, 0x82}, /* 38 (26) */
361   {0xFF, 0x00, 0x41}, /* 39 (27) */
362   {0xFF, 0x00, 0x00}, /* 40 (28) */
363   {0xFF, 0x41, 0x00}, /* 41 (29) */
364   {0xFF, 0x82, 0x00}, /* 42 (2A) */
365   {0xFF, 0xBE, 0x00}, /* 43 (2B) */
366   {0xFF, 0xFF, 0x00}, /* 44 (2C) */
367   {0xBE, 0xFF, 0x00}, /* 45 (2D) */
368   {0x82, 0xFF, 0x00}, /* 46 (2E) */
369   {0x41, 0xFF, 0x00}, /* 47 (2F) */
370   {0x00, 0xFF, 0x00}, /* 48 (30) */
371   {0x00, 0xFF, 0x41}, /* 49 (31) */
372   {0x00, 0xFF, 0x82}, /* 50 (32) */
373   {0x00, 0xFF, 0xBE}, /* 51 (33) */
374   {0x00, 0xFF, 0xFF}, /* 52 (34) */
375   {0x00, 0xBE, 0xFF}, /* 53 (35) */
376   {0x00, 0x82, 0xFF}, /* 54 (36) */
377   {0x00, 0x41, 0xFF}, /* 55 (37) */
378   /* High Intensity group - 72 colors in 2/3 saturation groups (38H-4FH moderate) */
379   {0x82, 0x82, 0xFF}, /* 56 (38) */
380   {0x9E, 0x82, 0xFF}, /* 57 (39) */
381   {0xBE, 0x82, 0xFF}, /* 58 (3A) */
382   {0xDF, 0x82, 0xFF}, /* 59 (3B) */
383   {0xFF, 0x82, 0xFF}, /* 60 (3C) */
384   {0xFF, 0x82, 0xDF}, /* 61 (3D) */
385   {0xFF, 0x82, 0xBE}, /* 62 (3E) */
386   {0xFF, 0x82, 0x9E}, /* 63 (3F) */
387   {0xFF, 0x82, 0x82}, /* 64 (40) */
388   {0xFF, 0x9E, 0x82}, /* 65 (41) */
389   {0xFF, 0xBE, 0x82}, /* 66 (42) */
390   {0xFF, 0xDF, 0x82}, /* 67 (43) */
391   {0xFF, 0xFF, 0x82}, /* 68 (44) */
392   {0xDF, 0xFF, 0x82}, /* 69 (45) */
393   {0xBE, 0xFF, 0x82}, /* 70 (46) */
394   {0x9E, 0xFF, 0x82}, /* 71 (47) */
395   {0x82, 0xFF, 0x82}, /* 72 (48) */
396   {0x82, 0xFF, 0x9E}, /* 73 (49) */
397   {0x82, 0xFF, 0xBE}, /* 74 (4A) */
398   {0x82, 0xFF, 0xDF}, /* 75 (4B) */
399   {0x82, 0xFF, 0xFF}, /* 76 (4C) */
400   {0x82, 0xDF, 0xFF}, /* 77 (4D) */
401   {0x82, 0xBE, 0xFF}, /* 78 (4E) */
402   {0x82, 0x9E, 0xFF}, /* 79 (4F) */
403   /* High Intensity group - 72 colors in 3/3 saturation groups (50H-67H low) */
404   {0xBA, 0xBA, 0xFF}, /* 80 (50) */
405   {0xCA, 0xBA, 0xFF}, /* 81 (51) */
406   {0xDF, 0xBA, 0xFF}, /* 82 (52) */
407   {0xEF, 0xBA, 0xFF}, /* 83 (53) */
408   {0xFF, 0xBA, 0xFF}, /* 84 (54) */
409   {0xFF, 0xBA, 0xEF}, /* 85 (55) */
410   {0xFF, 0xBA, 0xDF}, /* 86 (56) */
411   {0xFF, 0xBA, 0xCA}, /* 87 (57) */
412   {0xFF, 0xBA, 0xBA}, /* 88 (58) */
413   {0xFF, 0xCA, 0xBA}, /* 89 (59) */
414   {0xFF, 0xDF, 0xBA}, /* 90 (5A) */
415   {0xFF, 0xEF, 0xBA}, /* 91 (5B) */
416   {0xFF, 0xFF, 0xBA}, /* 92 (5C) */
417   {0xEF, 0xFF, 0xBA}, /* 93 (5D) */
418   {0xDF, 0xFF, 0xBA}, /* 94 (5E) */
419   {0xCA, 0xFF, 0xBA}, /* 95 (5F) */
420   {0xBA, 0xFF, 0xBA}, /* 96 (60) */
421   {0xBA, 0xFF, 0xCA}, /* 97 (61) */
422   {0xBA, 0xFF, 0xDF}, /* 98 (62) */
423   {0xBA, 0xFF, 0xEF}, /* 99 (63) */
424   {0xBA, 0xFF, 0xFF}, /* 100 (64) */
425   {0xBA, 0xEF, 0xFF}, /* 101 (65) */
426   {0xBA, 0xDF, 0xFF}, /* 102 (66) */
427   {0xBA, 0xCA, 0xFF}, /* 103 (67) */
428   /* Medium Intensity group - 72 colors in 1/3 saturation groups (68H-7FH high) */
429   {0x00, 0x00, 0x71}, /* 104 (68) */
430   {0x1C, 0x00, 0x71}, /* 105 (69) */
431   {0x39, 0x00, 0x71}, /* 106 (6A) */
432   {0x55, 0x00, 0x71}, /* 107 (6B) */
433   {0x71, 0x00, 0x71}, /* 108 (6C) */
434   {0x71, 0x00, 0x55}, /* 109 (6D) */
435   {0x71, 0x00, 0x39}, /* 110 (6E) */
436   {0x71, 0x00, 0x1C}, /* 111 (6F) */
437   {0x71, 0x00, 0x00}, /* 112 (70) */
438   {0x71, 0x1C, 0x00}, /* 113 (71) */
439   {0x71, 0x39, 0x00}, /* 114 (72) */
440   {0x71, 0x55, 0x00}, /* 115 (73) */
441   {0x71, 0x71, 0x00}, /* 116 (74) */
442   {0x55, 0x71, 0x00}, /* 117 (75) */
443   {0x39, 0x71, 0x00}, /* 118 (76) */
444   {0x1C, 0x71, 0x00}, /* 119 (77) */
445   {0x00, 0x71, 0x00}, /* 120 (78) */
446   {0x00, 0x71, 0x1C}, /* 121 (79) */
447   {0x00, 0x71, 0x39}, /* 122 (7A) */
448   {0x00, 0x71, 0x55}, /* 123 (7B) */
449   {0x00, 0x71, 0x71}, /* 124 (7C) */
450   {0x00, 0x55, 0x71}, /* 125 (7D) */
451   {0x00, 0x39, 0x71}, /* 126 (7E) */
452   {0x00, 0x1C, 0x71}, /* 127 (7F) */
453   /* Medium Intensity group - 72 colors in 2/3 saturation groups (80H-97H moderate) */
454   {0x39, 0x39, 0x71}, /* 128 (80) */
455   {0x45, 0x39, 0x71}, /* 129 (81) */
456   {0x55, 0x39, 0x71}, /* 130 (82) */
457   {0x61, 0x39, 0x71}, /* 131 (83) */
458   {0x71, 0x39, 0x71}, /* 132 (84) */
459   {0x71, 0x39, 0x61}, /* 133 (85) */
460   {0x71, 0x39, 0x55}, /* 134 (86) */
461   {0x71, 0x39, 0x45}, /* 135 (87) */
462   {0x71, 0x39, 0x39}, /* 136 (88) */
463   {0x71, 0x45, 0x39}, /* 137 (89) */
464   {0x71, 0x55, 0x39}, /* 138 (8A) */
465   {0x71, 0x61, 0x39}, /* 139 (8B) */
466   {0x71, 0x71, 0x39}, /* 140 (8C) */
467   {0x61, 0x71, 0x39}, /* 141 (8D) */
468   {0x55, 0x71, 0x39}, /* 142 (8E) */
469   {0x45, 0x71, 0x39}, /* 143 (8F) */
470   {0x39, 0x71, 0x39}, /* 144 (90) */
471   {0x39, 0x71, 0x45}, /* 145 (91) */
472   {0x39, 0x71, 0x55}, /* 146 (92) */
473   {0x39, 0x71, 0x61}, /* 147 (93) */
474   {0x39, 0x71, 0x71}, /* 148 (94) */
475   {0x39, 0x61, 0x71}, /* 149 (95) */
476   {0x39, 0x55, 0x71}, /* 150 (96) */
477   {0x39, 0x45, 0x71}, /* 151 (97) */
478   /* Medium Intensity group - 72 colors in 3/3 saturation groups (98H-AFH low) */
479   {0x51, 0x51, 0x71}, /* 152 (98) */
480   {0x59, 0x51, 0x71}, /* 153 (99) */
481   {0x61, 0x51, 0x71}, /* 154 (9A) */
482   {0x69, 0x51, 0x71}, /* 155 (9B) */
483   {0x71, 0x51, 0x71}, /* 156 (9C) */
484   {0x71, 0x51, 0x69}, /* 157 (9D) */
485   {0x71, 0x51, 0x61}, /* 158 (9E) */
486   {0x71, 0x51, 0x59}, /* 159 (9F) */
487   {0x71, 0x51, 0x51}, /* 160 (A0) */
488   {0x71, 0x59, 0x51}, /* 161 (A1) */
489   {0x71, 0x61, 0x51}, /* 162 (A2) */
490   {0x71, 0x69, 0x51}, /* 163 (A3) */
491   {0x71, 0x71, 0x51}, /* 164 (A4) */
492   {0x69, 0x71, 0x51}, /* 165 (A5) */
493   {0x61, 0x71, 0x51}, /* 166 (A6) */
494   {0x59, 0x71, 0x51}, /* 167 (A7) */
495   {0x51, 0x71, 0x51}, /* 168 (A8) */
496   {0x51, 0x71, 0x59}, /* 169 (A9) */
497   {0x51, 0x71, 0x61}, /* 170 (AA) */
498   {0x51, 0x71, 0x69}, /* 171 (AB) */
499   {0x51, 0x71, 0x71}, /* 172 (AC) */
500   {0x51, 0x69, 0x71}, /* 173 (AD) */
501   {0x51, 0x61, 0x71}, /* 174 (AE) */
502   {0x51, 0x59, 0x71}, /* 175 (AF) */
503   /* Low Intensity group - 72 colors in 1/3 saturation groups (B0H-C7H high) */
504   {0x00, 0x00, 0x41}, /* 176 (B0) */
505   {0x10, 0x00, 0x41}, /* 177 (B1) */
506   {0x20, 0x00, 0x41}, /* 178 (B2) */
507   {0x31, 0x00, 0x41}, /* 179 (B3) */
508   {0x41, 0x00, 0x41}, /* 180 (B4) */
509   {0x41, 0x00, 0x31}, /* 181 (B5) */
510   {0x41, 0x00, 0x20}, /* 182 (B6) */
511   {0x41, 0x00, 0x10}, /* 183 (B7) */
512   {0x41, 0x00, 0x00}, /* 184 (B8) */
513   {0x41, 0x10, 0x00}, /* 185 (B9) */
514   {0x41, 0x20, 0x00}, /* 186 (BA) */
515   {0x41, 0x31, 0x00}, /* 187 (BB) */
516   {0x41, 0x41, 0x00}, /* 188 (BC) */
517   {0x31, 0x41, 0x00}, /* 189 (BD) */
518   {0x20, 0x41, 0x00}, /* 190 (BE) */
519   {0x10, 0x41, 0x00}, /* 191 (BF) */
520   {0x00, 0x41, 0x00}, /* 192 (C0) */
521   {0x00, 0x41, 0x10}, /* 193 (C1) */
522   {0x00, 0x41, 0x20}, /* 194 (C2) */
523   {0x00, 0x41, 0x31}, /* 195 (C3) */
524   {0x00, 0x41, 0x41}, /* 196 (C4) */
525   {0x00, 0x31, 0x41}, /* 197 (C5) */
526   {0x00, 0x20, 0x41}, /* 198 (C6) */
527   {0x00, 0x10, 0x41}, /* 199 (C7) */
528   /* Low Intensity group - 72 colors in 2/3 saturation groups (C8H-DFH moderate) */
529   {0x20, 0x20, 0x41}, /* 200 (C8) */
530   {0x28, 0x20, 0x41}, /* 201 (C9) */
531   {0x31, 0x20, 0x41}, /* 202 (CA) */
532   {0x39, 0x20, 0x41}, /* 203 (CB) */
533   {0x41, 0x20, 0x41}, /* 204 (CC) */
534   {0x41, 0x20, 0x39}, /* 205 (CD) */
535   {0x41, 0x20, 0x31}, /* 206 (CE) */
536   {0x41, 0x20, 0x28}, /* 207 (CF) */
537   {0x41, 0x20, 0x20}, /* 208 (D0) */
538   {0x41, 0x28, 0x20}, /* 209 (D1) */
539   {0x41, 0x31, 0x20}, /* 210 (D2) */
540   {0x41, 0x39, 0x20}, /* 211 (D3) */
541   {0x41, 0x41, 0x20}, /* 212 (D4) */
542   {0x39, 0x41, 0x20}, /* 213 (D5) */
543   {0x31, 0x41, 0x20}, /* 214 (D6) */
544   {0x28, 0x41, 0x20}, /* 215 (D7) */
545   {0x20, 0x41, 0x20}, /* 216 (D8) */
546   {0x20, 0x41, 0x28}, /* 217 (D9) */
547   {0x20, 0x41, 0x31}, /* 218 (DA) */
548   {0x20, 0x41, 0x39}, /* 219 (DB) */
549   {0x20, 0x41, 0x41}, /* 220 (DC) */
550   {0x20, 0x39, 0x41}, /* 221 (DD) */
551   {0x20, 0x31, 0x41}, /* 222 (DE) */
552   {0x20, 0x28, 0x41}, /* 223 (DF) */
553   /* Low Intensity group - 72 colors in 3/3 saturation groups (E0H-F7H low) */
554   {0x2D, 0x2D, 0x41}, /* 223 (E0) */
555   {0x31, 0x2D, 0x41}, /* 224 (E1) */
556   {0x35, 0x2D, 0x41}, /* 225 (E2) */
557   {0x3D, 0x2D, 0x41}, /* 226 (E3) */
558   {0x41, 0x2D, 0x41}, /* 227 (E4) */
559   {0x41, 0x2D, 0x3D}, /* 228 (E5) */
560   {0x41, 0x2D, 0x35}, /* 229 (E6) */
561   {0x41, 0x2D, 0x31}, /* 230 (E7) */
562   {0x41, 0x2D, 0x2D}, /* 231 (E8) */
563   {0x41, 0x31, 0x2D}, /* 232 (E9) */
564   {0x41, 0x35, 0x2D}, /* 233 (EA) */
565   {0x41, 0x3D, 0x2D}, /* 234 (EB) */
566   {0x41, 0x41, 0x2D}, /* 235 (EC) */
567   {0x3D, 0x41, 0x2D}, /* 236 (ED) */
568   {0x35, 0x41, 0x2D}, /* 237 (EE) */
569   {0x31, 0x41, 0x2D}, /* 238 (EF) */
570   {0x2D, 0x41, 0x2D}, /* 239 (F0) */
571   {0x2D, 0x41, 0x31}, /* 240 (F1) */
572   {0x2D, 0x41, 0x35}, /* 241 (F2) */
573   {0x2D, 0x41, 0x3D}, /* 242 (F3) */
574   {0x2D, 0x41, 0x41}, /* 243 (F4) */
575   {0x2D, 0x3D, 0x41}, /* 244 (F5) */
576   {0x2D, 0x35, 0x41}, /* 245 (F6) */
577   {0x2D, 0x31, 0x41}, /* 246 (F7) */
578   /* Fill up remainder of palettes with black */
579   {0,0,0}
580 };
581
582 /*
583  *  Mode 18 Default Color Register Setting
584  *  DAC palette registers, converted from actual 18 bit color to 24.
585  *
586  *  This palette is the dos default, converted from 18 bit color to 24.
587  *  It contains only 64 entries of colors--all others are zeros.
588  *      --Robert 'Admiral' Coeyman
589  */
590 static PALETTEENTRY vga_def64_palette[256]={
591 /* red  green  blue */
592   {0x00, 0x00, 0x00}, /* 0x00      Black      */
593   {0x00, 0x00, 0xaa}, /* 0x01      Blue       */
594   {0x00, 0xaa, 0x00}, /* 0x02      Green      */
595   {0x00, 0xaa, 0xaa}, /* 0x03      Cyan       */
596   {0xaa, 0x00, 0x00}, /* 0x04      Red        */
597   {0xaa, 0x00, 0xaa}, /* 0x05      Magenta    */
598   {0xaa, 0xaa, 0x00}, /* 0x06      */
599   {0xaa, 0xaa, 0xaa}, /* 0x07      White (Light Gray) */
600   {0x00, 0x00, 0x55}, /* 0x08      */
601   {0x00, 0x00, 0xff}, /* 0x09      */
602   {0x00, 0xaa, 0x55}, /* 0x0a      */
603   {0x00, 0xaa, 0xff}, /* 0x0b      */
604   {0xaa, 0x00, 0x55}, /* 0x0c      */
605   {0xaa, 0x00, 0xff}, /* 0x0d      */
606   {0xaa, 0xaa, 0x55}, /* 0x0e      */
607   {0xaa, 0xaa, 0xff}, /* 0x0f      */
608   {0x00, 0x55, 0x00}, /* 0x10      */
609   {0x00, 0x55, 0xaa}, /* 0x11      */
610   {0x00, 0xff, 0x00}, /* 0x12      */
611   {0x00, 0xff, 0xaa}, /* 0x13      */
612   {0xaa, 0x55, 0x00}, /* 0x14      Brown      */
613   {0xaa, 0x55, 0xaa}, /* 0x15      */
614   {0xaa, 0xff, 0x00}, /* 0x16      */
615   {0xaa, 0xff, 0xaa}, /* 0x17      */
616   {0x00, 0x55, 0x55}, /* 0x18      */
617   {0x00, 0x55, 0xff}, /* 0x19      */
618   {0x00, 0xff, 0x55}, /* 0x1a      */
619   {0x00, 0xff, 0xff}, /* 0x1b      */
620   {0xaa, 0x55, 0x55}, /* 0x1c      */
621   {0xaa, 0x55, 0xff}, /* 0x1d      */
622   {0xaa, 0xff, 0x55}, /* 0x1e      */
623   {0xaa, 0xff, 0xff}, /* 0x1f      */
624   {0x55, 0x00, 0x00}, /* 0x20      */
625   {0x55, 0x00, 0xaa}, /* 0x21      */
626   {0x55, 0xaa, 0x00}, /* 0x22      */
627   {0x55, 0xaa, 0xaa}, /* 0x23      */
628   {0xff, 0x00, 0x00}, /* 0x24      */
629   {0xff, 0x00, 0xaa}, /* 0x25      */
630   {0xff, 0xaa, 0x00}, /* 0x26      */
631   {0xff, 0xaa, 0xaa}, /* 0x27      */
632   {0x55, 0x00, 0x55}, /* 0x28      */
633   {0x55, 0x00, 0xff}, /* 0x29      */
634   {0x55, 0xaa, 0x55}, /* 0x2a      */
635   {0x55, 0xaa, 0xff}, /* 0x2b      */
636   {0xff, 0x00, 0x55}, /* 0x2c      */
637   {0xff, 0x00, 0xff}, /* 0x2d      */
638   {0xff, 0xaa, 0x55}, /* 0x2e      */
639   {0xff, 0xaa, 0xff}, /* 0x2f      */
640   {0x55, 0x55, 0x00}, /* 0x30      */
641   {0x55, 0x55, 0xaa}, /* 0x31      */
642   {0x55, 0xff, 0x00}, /* 0x32      */
643   {0x55, 0xff, 0xaa}, /* 0x33      */
644   {0xff, 0x55, 0x00}, /* 0x34      */
645   {0xff, 0x55, 0xaa}, /* 0x35      */
646   {0xff, 0xff, 0x00}, /* 0x36      */
647   {0xff, 0xff, 0xaa}, /* 0x37      */
648   {0x55, 0x55, 0x55}, /* 0x38      Dark Gray     */
649   {0x55, 0x55, 0xff}, /* 0x39      Light Blue    */
650   {0x55, 0xff, 0x55}, /* 0x3a      Light Green   */
651   {0x55, 0xff, 0xff}, /* 0x3b      Light Cyan    */
652   {0xff, 0x55, 0x55}, /* 0x3c      Light Red     */
653   {0xff, 0x55, 0xff}, /* 0x3d      Light Magenta */
654   {0xff, 0xff, 0x55}, /* 0x3e      Yellow        */
655   {0xff, 0xff, 0xff}, /* 0x3f      White         */
656   {0,0,0} /* The next 192 entries are all zeros  */
657 };
658
659 static HANDLE VGA_timer;
660 static HANDLE VGA_timer_thread;
661
662 /* set the timer rate; called in the polling thread context */
663 static void CALLBACK set_timer_rate( ULONG_PTR arg )
664 {
665     LARGE_INTEGER when;
666
667     when.u.LowPart = when.u.HighPart = 0;
668     SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
669 }
670
671 static DWORD CALLBACK VGA_TimerThread( void *dummy )
672 {
673     for (;;) SleepEx( INFINITE, TRUE );
674     return 0;
675 }
676
677 static void VGA_DeinstallTimer(void)
678 {
679     if (VGA_timer_thread)
680     {
681         /*
682          * Make sure the update thread is not holding
683          * system resources when we kill it.
684          *
685          * Now, we only need to worry about update thread
686          * getting terminated while in EnterCriticalSection 
687          * or WaitForMultipleObjectsEx.
688          *
689          * FIXME: Is this a problem?
690          */
691         EnterCriticalSection(&vga_lock);
692
693         CancelWaitableTimer( VGA_timer );
694         CloseHandle( VGA_timer );
695         TerminateThread( VGA_timer_thread, 0 );
696         CloseHandle( VGA_timer_thread );
697         VGA_timer_thread = 0;
698
699         LeaveCriticalSection(&vga_lock);
700
701         /*
702          * Synchronize display. This makes sure that
703          * changes to display become visible even if program 
704          * terminates before update thread had time to run.
705          */
706         VGA_Poll( 0, 0, 0 );
707     }
708 }
709
710 static void VGA_InstallTimer(unsigned Rate)
711 {
712     if (!VGA_timer_thread)
713     {
714         VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
715         VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
716     }
717     QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
718 }
719
720 static BOOL VGA_IsTimerRunning(void)
721 {
722     return VGA_timer_thread ? TRUE : FALSE;
723 }
724
725 static HANDLE VGA_AlphaConsole(void)
726 {
727     /* this assumes that no Win32 redirection has taken place, but then again,
728      * only 16-bit apps are likely to use this part of Wine... */
729     return GetStdHandle(STD_OUTPUT_HANDLE);
730 }
731
732 static char*VGA_AlphaBuffer(void)
733 {
734     return (char *)0xb8000;
735 }
736
737 /*** GRAPHICS MODE ***/
738
739 typedef struct {
740   unsigned Xres, Yres, Depth;
741   int ret;
742 } ModeSet;
743
744
745 /**********************************************************************
746  *         VGA_SyncWindow
747  *
748  * Copy VGA window into framebuffer (if argument is TRUE) or
749  * part of framebuffer into VGA window (if argument is FALSE).
750  */
751 static void VGA_SyncWindow( BOOL target_is_fb )
752 {
753     int size = vga_fb_window_size;
754
755     /* Window does not overlap framebuffer. */
756     if (vga_fb_window >= vga_fb_size)
757         return;
758
759     /* Check if window overlaps framebuffer only partially. */
760     if (vga_fb_size - vga_fb_window < vga_fb_window_size)
761         size = vga_fb_size - vga_fb_window;
762
763     if (target_is_fb)
764         memmove( vga_fb_data + vga_fb_window, vga_fb_window_data, size );
765     else
766         memmove( vga_fb_window_data, vga_fb_data + vga_fb_window, size );
767 }
768
769
770 static void WINAPI VGA_DoExit(ULONG_PTR arg)
771 {
772     VGA_DeinstallTimer();
773     IDirectDrawSurface_SetPalette(lpddsurf,NULL);
774     IDirectDrawSurface_Release(lpddsurf);
775     lpddsurf=NULL;
776     IDirectDrawPalette_Release(lpddpal);
777     lpddpal=NULL;
778     IDirectDraw_Release(lpddraw);
779     lpddraw=NULL;
780 }
781
782 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
783 {
784     HRESULT     res;
785     ModeSet *par = (ModeSet *)arg;
786     par->ret=1;
787
788     if (lpddraw) VGA_DoExit(0);
789     if (!lpddraw) {
790         if (!pDirectDrawCreate)
791         {
792             HMODULE hmod = LoadLibraryA( "ddraw.dll" );
793             if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
794             if (!pDirectDrawCreate) {
795                 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
796                 return;
797             }
798         }
799         res = pDirectDrawCreate(NULL,&lpddraw,NULL);
800         if (!lpddraw) {
801             ERR("DirectDraw is not available (res = 0x%x)\n",res);
802             return;
803         }
804         if (!vga_hwnd) {
805             vga_hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",
806                                        WS_POPUP|WS_VISIBLE|SS_NOTIFY,0,0,
807                                        par->Xres,par->Yres,0,0,0,NULL);
808             if (!vga_hwnd) {
809                 ERR("Failed to create user window.\n");
810                 IDirectDraw_Release(lpddraw);
811                 lpddraw=NULL;
812                 return;
813             }
814         }
815         else
816             SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
817
818         res=IDirectDraw_SetCooperativeLevel(lpddraw,vga_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE);
819         if (res != S_OK) {
820             ERR("Could not set cooperative level to exclusive (0x%x)\n",res);
821         }
822
823         res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth);
824         if (res != S_OK) {
825             ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = 0x%x!\n",par->Xres,par->Yres,par->Depth,res);
826             IDirectDraw_Release(lpddraw);
827             lpddraw=NULL;
828             return;
829         }
830
831         res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
832         if (res != S_OK) {
833             ERR("Could not create palette (res = 0x%x)\n",res);
834             IDirectDraw_Release(lpddraw);
835             lpddraw=NULL;
836             return;
837         }
838
839         res=IDirectDrawPalette_SetEntries(lpddpal,0,0,vga_fb_palette_size,vga_fb_palette);
840         if (res != S_OK) {
841            ERR("Could not set default palette entries (res = 0x%x)\n", res);
842         }
843
844         memset(&sdesc,0,sizeof(sdesc));
845         sdesc.dwSize=sizeof(sdesc);
846         sdesc.dwFlags = DDSD_CAPS;
847         sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
848         res=IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL);
849         if (res != S_OK || !lpddsurf) {
850             ERR("DirectDraw surface is not available\n");
851             IDirectDraw_Release(lpddraw);
852             lpddraw=NULL;
853             return;
854         }
855         IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
856         vga_retrace_vertical = vga_retrace_horizontal = FALSE;
857         /* poll every 20ms (50fps should provide adequate responsiveness) */
858         VGA_InstallTimer(20);
859     }
860     par->ret=0;
861     return;
862 }
863
864 /**********************************************************************\
865 * VGA_GetModeInfo
866 *
867 * Returns mode information, given mode number
868 \**********************************************************************/
869 const VGA_MODE *VGA_GetModeInfo(WORD mode)
870 {
871     const VGA_MODE *ModeInfo = VGA_modelist;
872
873     /*
874      * Filter out flags.
875      */
876     mode &= 0x17f;
877
878     while (ModeInfo->Mode != 0xffff)
879     {
880         if (ModeInfo->Mode == mode)
881             return ModeInfo;
882         ModeInfo++;
883     }
884     return NULL;
885 }
886
887 static int VGA_SetGraphicMode(WORD mode)
888 {
889     ModeSet par;
890     int     newSize;
891
892     /* get info on VGA mode & set appropriately */
893     const VGA_MODE *ModeInfo = VGA_GetModeInfo(VGA_CurrentMode);
894     /* check if we're assuming composite display */
895     if ((mode == 6) && (CGA_ColorComposite))
896     {
897        vga_fb_width = (ModeInfo->Width / 4);
898        vga_fb_height = ModeInfo->Height;
899        vga_fb_depth = (ModeInfo->Depth * 4);
900     }
901     else
902     {
903        vga_fb_width = ModeInfo->Width;
904        vga_fb_height = ModeInfo->Height;
905        vga_fb_depth = ModeInfo->Depth;
906     }
907     vga_fb_offset = 0;
908     vga_fb_pitch = vga_fb_width * ((vga_fb_depth + 7) / 8);
909
910     newSize = vga_fb_width * vga_fb_height * ((vga_fb_depth + 7) / 8);
911     if(newSize < 256 * 1024)
912       newSize = 256 * 1024;
913
914     if(vga_fb_size < newSize) {
915       HeapFree(GetProcessHeap(), 0, vga_fb_data);
916       vga_fb_data = HeapAlloc(GetProcessHeap(), 0, newSize);
917       vga_fb_size = newSize;
918     }
919
920     if(vga_fb_width >= 640 || vga_fb_height >= 480) {
921       par.Xres = vga_fb_width;
922       par.Yres = vga_fb_height;
923     } else {
924       par.Xres = 640;
925       par.Yres = 480;
926     }
927
928     /* Setup window */
929     if(vga_fb_depth >= 8)
930     {
931       vga_fb_window_data = VGA_WINDOW_START;
932       vga_fb_window_size = VGA_WINDOW_SIZE;
933       vga_fb_palette = vga_def_palette;
934       vga_fb_palette_size = 256;
935     }
936     else
937     {
938       vga_fb_window_data = CGA_WINDOW_START;
939       vga_fb_window_size = CGA_WINDOW_SIZE;
940       if(vga_fb_depth == 2)
941       {
942         /* Select default 2 bit CGA palette */
943         vga_fb_palette = cga_palette1;
944         vga_fb_palette_size = 4;
945       }
946       else
947       {
948         /* Top of VGA palette is same as 4 bit CGA palette */
949         vga_fb_palette = vga_def_palette;
950         vga_fb_palette_size = 16;
951       }
952
953       vga_fb_palette_index = 0;
954       vga_fb_bright = 0;
955     }
956
957     /* Clean the HW buffer */
958     memset(vga_fb_window_data, 0x00, vga_fb_window_size);
959
960     /* Reset window start */
961     VGA_SetWindowStart(0);
962
963     par.Depth = (vga_fb_depth < 8) ? 8 : vga_fb_depth;
964
965     MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
966     return par.ret;
967 }
968
969 int VGA_SetMode(WORD mode)
970 {
971     const VGA_MODE *ModeInfo;
972     /* get info on VGA mode & set appropriately */
973     VGA_CurrentMode = mode;
974     ModeInfo = VGA_GetModeInfo(VGA_CurrentMode);
975
976     /* check if mode is supported */
977     if (ModeInfo->Supported)
978     {
979        FIXME("Setting VGA mode %i - Supported mode - Improve reporting of missing capabilities for modes & modetypes.\n", mode);
980     }
981     else
982     {
983        FIXME("Setting VGA mode %i - Unsupported mode - Will doubtfully work at all, but we'll try anyways.\n", mode);
984     }
985
986     /* set up graphic or text display */
987     if (ModeInfo->ModeType == TEXT)
988     {
989        VGA_SetAlphaMode(ModeInfo->TextCols, ModeInfo->TextRows);
990     }
991     else
992     {
993        return VGA_SetGraphicMode(mode);
994     }
995     return 0; /* assume all good & return zero */
996 }
997
998 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
999 {
1000     if (!lpddraw) return 1;
1001     if (!lpddsurf) return 1;
1002     if (Height) *Height=sdesc.dwHeight;
1003     if (Width) *Width=sdesc.dwWidth;
1004     if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
1005     return 0;
1006 }
1007
1008 static void VGA_Exit(void)
1009 {
1010     if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
1011 }
1012
1013 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
1014 {
1015     if (!lpddraw) return;
1016     IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
1017 }
1018
1019 /* set a single [char wide] color in 16 color mode. */
1020 void VGA_SetColor16(int reg,int color)
1021 {
1022         PALETTEENTRY *pal;
1023
1024     if (!lpddraw) return;
1025         pal= &vga_def64_palette[color];
1026         IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
1027         vga_16_palette[reg]=(char)color;
1028 }
1029
1030 /* Get a single [char wide] color in 16 color mode. */
1031 char VGA_GetColor16(int reg)
1032 {
1033
1034     if (!lpddraw) return 0;
1035         return vga_16_palette[reg];
1036 }
1037
1038 /* set all 17 [char wide] colors at once in 16 color mode. */
1039 void VGA_Set16Palette(char *Table)
1040 {
1041         PALETTEENTRY *pal;
1042         int c;
1043
1044     if (!lpddraw) return;         /* return if we're in text only mode */
1045     memcpy( Table, vga_16_palette, 17 ); /* copy the entries into the table */
1046
1047     for (c=0; c<17; c++) {                                /* 17 entries */
1048         pal= &vga_def64_palette[(int)vga_16_palette[c]];  /* get color  */
1049         IDirectDrawPalette_SetEntries(lpddpal,0,c,1,pal); /* set entry  */
1050         TRACE("Palette register %d set to %d\n",c,(int)vga_16_palette[c]);
1051    } /* end of the counting loop */
1052 }
1053
1054 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
1055 void VGA_Get16Palette(char *Table)
1056 {
1057
1058     if (!lpddraw) return;         /* return if we're in text only mode */
1059     memcpy( vga_16_palette, Table, 17 ); /* copy the entries into the table */
1060 }
1061
1062 static LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
1063 {
1064     if (!lpddraw) return NULL;
1065     if (!lpddsurf) return NULL;
1066     if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0) != S_OK) {
1067         ERR("could not lock surface!\n");
1068         return NULL;
1069     }
1070     if (Pitch) *Pitch=sdesc.u1.lPitch;
1071     if (Height) *Height=sdesc.dwHeight;
1072     if (Width) *Width=sdesc.dwWidth;
1073     if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
1074     return sdesc.lpSurface;
1075 }
1076
1077 static void VGA_Unlock(void)
1078 {
1079     IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
1080 }
1081
1082 /*
1083  * Set start of 64k window at 0xa0000 in bytes.
1084  * If value is -1, initialize color plane support.
1085  * If value is >= 0, window contains direct copy of framebuffer.
1086  */
1087 void VGA_SetWindowStart(int start)
1088 {
1089     if(start == vga_fb_window)
1090         return;
1091
1092     EnterCriticalSection(&vga_lock);
1093
1094     if(vga_fb_window == -1)
1095         FIXME("Remove VGA memory emulation.\n");
1096     else
1097         VGA_SyncWindow( TRUE );
1098
1099     vga_fb_window = start;
1100
1101     if(vga_fb_window == -1)
1102         FIXME("Install VGA memory emulation.\n");
1103     else
1104         VGA_SyncWindow( FALSE );
1105
1106     LeaveCriticalSection(&vga_lock);
1107 }
1108
1109 /*
1110  * Get start of 64k window at 0xa0000 in bytes.
1111  * Value is -1 in color plane modes.
1112  */
1113 int VGA_GetWindowStart(void)
1114 {
1115     return vga_fb_window;
1116 }
1117
1118
1119 /**********************************************************************
1120  *         VGA_DoShowMouse
1121  *
1122  * Callback for VGA_ShowMouse.
1123  */
1124 static void WINAPI VGA_DoShowMouse( ULONG_PTR show )
1125 {
1126     INT rv;
1127
1128     do
1129     {
1130         rv = ShowCursor( show );
1131     }
1132     while( show ? (rv < 0) : (rv >= 0) );
1133 }
1134
1135
1136 /**********************************************************************
1137  *         VGA_ShowMouse
1138  *
1139  * If argument is TRUE, unconditionally show mouse cursor.
1140  * If argument is FALSE, unconditionally hide mouse cursor.
1141  * This only works in graphics mode.
1142  */
1143 void VGA_ShowMouse( BOOL show )
1144 {
1145     if (lpddraw)
1146         MZ_RunInThread( VGA_DoShowMouse, (ULONG_PTR)show );
1147 }
1148
1149
1150 /**********************************************************************
1151  *         VGA_UpdatePalette
1152  *
1153  * Update the current palette
1154  *
1155  * Note: When updating the current CGA palette, palette index 0
1156  * refers to palette2, and index 1 is palette1 (default palette)
1157  */
1158 void VGA_UpdatePalette(void)
1159 {
1160   /* Figure out which palette is used now */
1161   if(vga_fb_bright)
1162   {
1163     if(vga_fb_palette_index == 0)
1164     {
1165       vga_fb_palette = cga_palette2_bright;
1166     }
1167     else if(vga_fb_palette_index == 1)
1168     {
1169       vga_fb_palette = cga_palette1_bright;
1170     }
1171   }
1172   else
1173   {
1174     if(vga_fb_palette_index == 0)
1175     {
1176       vga_fb_palette = cga_palette2;
1177     }
1178     else if(vga_fb_palette_index == 1)
1179     {
1180       vga_fb_palette = cga_palette1;
1181     }
1182   }
1183
1184   /* Now update the palette */
1185   VGA_SetPalette(vga_fb_palette,0,4);
1186 }
1187
1188 /**********************************************************************
1189  *         VGA_SetBright
1190  *
1191  * Select if using a "bright" palette or not.
1192  * This is a property of the CGA controller
1193  */
1194 void VGA_SetBright(BOOL bright)
1195 {
1196   TRACE("%i\n", bright);
1197
1198   /* Remember the "bright" value used by the CGA controller */
1199   vga_fb_bright = bright;
1200 }
1201
1202 /**********************************************************************
1203  *         VGA_SetEnabled
1204  *
1205  * Select if output is enabled or disabled
1206  * This is a property of the CGA controller
1207  */
1208 static void VGA_SetEnabled(BOOL enabled)
1209 {
1210   TRACE("%i\n", enabled);
1211
1212   /* Check if going from enabled to disabled */
1213   if(vga_fb_enabled && !enabled)
1214   {
1215     /* Clear frame buffer */
1216     memset(vga_fb_window_data, 0x00, vga_fb_window_size);
1217   }
1218
1219   /* Remember the "enabled" value */
1220   vga_fb_enabled = enabled;
1221 }
1222
1223 /**********************************************************************
1224  *         VGA_SetPaletteIndex
1225  *
1226  * Select the index of the palette which is currently in use
1227  * This is a property of the CGA controller
1228  */
1229 void VGA_SetPaletteIndex(unsigned index)
1230 {
1231   TRACE("%i\n", index);
1232
1233   /* Remember the palette index, which is only used by CGA for now */
1234   vga_fb_palette_index = index;
1235 }
1236
1237 /**********************************************************************
1238  *         VGA_WritePixel
1239  *
1240  * Write data to the framebuffer
1241  * This is a property of the CGA controller, but might be supported
1242  * later by other framebuffer types
1243  */
1244 void VGA_WritePixel(unsigned color, unsigned page, unsigned col, unsigned row)
1245 {
1246   int off;
1247   int bits;
1248   int pos;
1249
1250   /* Calculate CGA byte offset */
1251   char *data = vga_fb_window_data;
1252   off = row & 1 ? (8 * 1024) : 0;
1253   off += (80 * (row/2));
1254   off += col/4;
1255
1256   /* Calculate bits offset */
1257   pos = 6 - (col%4 * 2);
1258
1259   /* Clear current data */
1260   bits = 0x03 << pos;
1261   data[off] &= ~bits;
1262
1263   /* Set new data */
1264   bits = color << pos;
1265   data[off] |= bits;
1266 }
1267
1268 /*** TEXT MODE ***/
1269
1270 /* prepare the text mode video memory copy that is used to only
1271  * update the video memory line that did get updated. */
1272 static void VGA_PrepareVideoMemCopy(unsigned Xres, unsigned Yres)
1273 {
1274     char *p, *p2;
1275     unsigned int i;
1276
1277     /*
1278      * Allocate space for char + attr.
1279      */
1280
1281     if (vga_text_old)
1282         vga_text_old = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
1283                                 vga_text_old, Xres * Yres * 2 );
1284     else
1285         vga_text_old = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
1286                                  Xres * Yres * 2 );
1287     p = VGA_AlphaBuffer();
1288     p2 = vga_text_old;
1289
1290     /* make sure the video mem copy contains the exact opposite of our
1291      * actual text mode memory area to make sure the screen
1292      * does get updated fully initially */
1293     for (i=0; i < Xres*Yres*2; i++)
1294         *p2++ = *p++ ^ 0xff; /* XOR it */
1295 }
1296
1297 /**********************************************************************
1298  *         VGA_SetAlphaMode
1299  *
1300  * Set VGA emulation to text mode.
1301  */
1302 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
1303 {
1304     VGA_Exit();
1305     VGA_DeinstallTimer();
1306     
1307     VGA_PrepareVideoMemCopy(Xres, Yres);
1308     vga_text_width = Xres;
1309     vga_text_height = Yres;
1310
1311     if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
1312         VGA_SetCursorPos(0,0);
1313
1314     if(vga_text_console) {
1315         COORD size;
1316         size.X = Xres;
1317         size.Y = Yres;
1318         SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
1319
1320         /* poll every 30ms (33fps should provide adequate responsiveness) */
1321         VGA_InstallTimer(30);
1322     }
1323 }
1324
1325 /**********************************************************************
1326  *         VGA_InitAlphaMode
1327  *
1328  * Initialize VGA text mode handling and return default text mode.
1329  * This function does not set VGA emulation to text mode.
1330  */
1331 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
1332 {
1333     CONSOLE_SCREEN_BUFFER_INFO info;
1334
1335     if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
1336     {
1337         vga_text_console = TRUE;
1338         vga_text_x = info.dwCursorPosition.X;
1339         vga_text_y = info.dwCursorPosition.Y;
1340         vga_text_attr = info.wAttributes;
1341         *Xres = info.dwSize.X;
1342         *Yres = info.dwSize.Y;
1343     } 
1344     else
1345     {
1346         vga_text_console = FALSE;
1347         vga_text_x = 0;
1348         vga_text_y = 0;
1349         vga_text_attr = 0x0f;
1350         *Xres = 80;
1351         *Yres = 25;
1352     }
1353 }
1354
1355 /**********************************************************************
1356  *         VGA_GetAlphaMode
1357  *
1358  * Get current text mode. Returns TRUE and sets resolution if
1359  * any VGA text mode has been initialized.
1360  */
1361 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
1362 {
1363     if (vga_text_width != 0 && vga_text_height != 0) {
1364         *Xres = vga_text_width;
1365         *Yres = vga_text_height;
1366         return TRUE;
1367     } else
1368         return FALSE;
1369 }
1370
1371 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
1372 {
1373     CONSOLE_CURSOR_INFO cci;
1374
1375     /* standard cursor settings:
1376      * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
1377
1378     /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
1379     cci.dwSize = ((end & 0x1f) - (start_options & 0x1f))/0x0e * 100;
1380     if (!cci.dwSize) cci.dwSize++; /* NULL cursor would make SCCI() fail ! */
1381     cci.bVisible = ((start_options & 0x60) != 0x20); /* invisible ? */
1382
1383     SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
1384 }
1385
1386 void VGA_SetCursorPos(unsigned X,unsigned Y)
1387 {
1388     vga_text_x = X;
1389     vga_text_y = Y;
1390 }
1391
1392 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
1393 {
1394     if (X) *X = vga_text_x;
1395     if (Y) *Y = vga_text_y;
1396 }
1397
1398 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
1399 {
1400     const VGA_MODE *ModeInfo = VGA_GetModeInfo(VGA_CurrentMode);
1401     if ( ModeInfo->ModeType == TEXT )
1402     {
1403        char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
1404        dat[0] = ascii;
1405        if (attr>=0)
1406           dat[1] = attr;
1407     }
1408     else
1409     {
1410        FIXME("Write %c at (%i,%i) - not yet supported in graphic modes.\n", (char)ascii, x, y);
1411     }
1412 }
1413
1414 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
1415 {
1416     EnterCriticalSection(&vga_lock);
1417
1418     while (count--) 
1419         VGA_PutCharAt(X + count, Y, ch, attr);
1420
1421     LeaveCriticalSection(&vga_lock);
1422 }
1423
1424 void VGA_PutChar(BYTE ascii)
1425 {
1426     DWORD w;
1427
1428     EnterCriticalSection(&vga_lock);
1429
1430     switch(ascii) {
1431     case '\b':
1432         if (vga_text_x)
1433         {
1434             vga_text_x--;
1435             VGA_PutCharAt(vga_text_x, vga_text_y, ' ', 0);
1436         }
1437         break;
1438
1439     case '\t':
1440         vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
1441         break;
1442
1443     case '\n':
1444         vga_text_y++;
1445         vga_text_x = 0;
1446         break;
1447
1448     case '\a':
1449         break;
1450
1451     case '\r':
1452         vga_text_x = 0;
1453         break;
1454
1455     default:
1456         VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
1457         vga_text_x++;
1458     }
1459
1460     if (vga_text_x >= vga_text_width)
1461     {
1462         vga_text_x = 0;
1463         vga_text_y++;
1464     }
1465
1466     if (vga_text_y >= vga_text_height)
1467     {
1468         vga_text_y = vga_text_height - 1;
1469         VGA_ScrollUpText( 0, 0, 
1470                           vga_text_height - 1, vga_text_width - 1, 
1471                           1, vga_text_attr );
1472     }
1473
1474     /*
1475      * If we don't have a console, write directly to standard output.
1476      */
1477     if(!vga_text_console)
1478         WriteFile(VGA_AlphaConsole(), &ascii, 1, &w, NULL);
1479
1480     LeaveCriticalSection(&vga_lock);
1481 }
1482
1483 void VGA_ClearText(unsigned row1, unsigned col1,
1484                    unsigned row2, unsigned col2,
1485                    BYTE attr)
1486 {
1487     unsigned x, y;
1488
1489     EnterCriticalSection(&vga_lock);
1490
1491     for(y=row1; y<=row2; y++)
1492         for(x=col1; x<=col2; x++)
1493             VGA_PutCharAt(x, y, 0x20, attr);
1494
1495     LeaveCriticalSection(&vga_lock);
1496 }
1497
1498 void VGA_ScrollUpText(unsigned row1,  unsigned col1,
1499                       unsigned row2,  unsigned col2,
1500                       unsigned lines, BYTE attr)
1501 {
1502     char    *buffer = VGA_AlphaBuffer();
1503     unsigned y;
1504
1505     EnterCriticalSection(&vga_lock);
1506
1507     /*
1508      * Scroll buffer.
1509      */
1510     for (y = row1; y <= row2 - lines; y++)
1511         memmove( buffer + col1 + y * vga_text_width * 2,
1512                  buffer + col1 + (y + lines) * vga_text_width * 2,
1513                  (col2 - col1 + 1) * 2 );
1514
1515     /*
1516      * Fill exposed lines.
1517      */
1518     for (y = max(row1, row2 - lines + 1); y <= row2; y++)
1519         VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1520
1521     LeaveCriticalSection(&vga_lock);
1522 }
1523
1524 void VGA_ScrollDownText(unsigned row1,  unsigned col1,
1525                         unsigned row2,  unsigned col2,
1526                         unsigned lines, BYTE attr)
1527 {
1528     char    *buffer = VGA_AlphaBuffer();
1529     unsigned y;
1530
1531     EnterCriticalSection(&vga_lock);
1532
1533     /*
1534      * Scroll buffer.
1535      */
1536     for (y = row2; y >= row1 + lines; y--)
1537         memmove( buffer + col1 + y * vga_text_width * 2,
1538                  buffer + col1 + (y - lines) * vga_text_width * 2,
1539                  (col2 - col1 + 1) * 2 );
1540
1541     /*
1542      * Fill exposed lines.
1543      */
1544     for (y = row1; y <= min(row1 + lines - 1, row2); y++)
1545         VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
1546
1547     LeaveCriticalSection(&vga_lock);
1548 }
1549
1550 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
1551 {
1552     char *dat;
1553
1554     dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
1555
1556     *ascii = dat[0];
1557     *attr = dat[1];
1558 }
1559
1560
1561 /*** CONTROL ***/
1562
1563 /* FIXME: optimize by doing this only if the data has actually changed
1564  *        (in a way similar to DIBSection, perhaps) */
1565 static void VGA_Poll_Graphics(void)
1566 {
1567   unsigned int Pitch, Height, Width, X, Y;
1568   char *surf;
1569   char *dat = vga_fb_data + vga_fb_offset;
1570   int   bpp = (vga_fb_depth + 7) / 8;
1571
1572   surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
1573   if (!surf) return;
1574
1575   /*
1576    * Synchronize framebuffer contents.
1577    */
1578   if (vga_fb_window != -1)
1579       VGA_SyncWindow( TRUE );
1580
1581   /*
1582    * CGA framebuffer (160x200) - CGA_ColorComposite, special subtype of mode 6
1583    * This buffer is encoded as following:
1584    * - 4 bit pr. pixel, 2 pixels per byte
1585    * - 80 bytes per row
1586    * - Every second line has an offset of 8096
1587    */
1588   if(vga_fb_depth == 4 && vga_fb_width == 160 && vga_fb_height == 200){
1589     WORD off = 0;
1590     BYTE bits = 4;
1591     BYTE value;
1592     for(Y=0; Y<vga_fb_height; Y++, surf+=(Pitch*2)){
1593       for(X=0; X<vga_fb_width; X++){
1594         off = Y & 1 ? (8 * 1024) : 0;
1595         off += (80 * (Y/2));
1596         off += X/2;
1597         value = (dat[off] >> bits) & 0xF;
1598         surf[(X*4)+0] = value;
1599         surf[(X*4)+1] = value;
1600         surf[(X*4)+2] = value;
1601         surf[(X*4)+3] = value;
1602         surf[(X*4)+Pitch+0] = value;
1603         surf[(X*4)+Pitch+1] = value;
1604         surf[(X*4)+Pitch+2] = value;
1605         surf[(X*4)+Pitch+3] = value;
1606         bits -= 4;
1607         bits &= 7;
1608       }
1609     }
1610   }
1611
1612   /*
1613    * CGA framebuffer (320x200)
1614    * This buffer is encoded as following:
1615    * - 2 bits per color, 4 pixels per byte
1616    * - 80 bytes per row
1617    * - Every second line has an offset of 8096
1618    */
1619   else if(vga_fb_depth == 2 && vga_fb_width == 320 && vga_fb_height == 200){
1620     WORD off = 0;
1621     BYTE bits = 6;
1622     BYTE value;
1623     /* Go thru rows */
1624     for(Y=0; Y<vga_fb_height; Y++, surf+=(Pitch*2)){
1625       for(X=0; X<vga_fb_width; X++){
1626         off = Y & 1 ? (8 * 1024) : 0;
1627         off += (80 * (Y/2));
1628         off += X/4;
1629         value = (dat[off] >> bits) & 0x3;
1630         surf[(X*2)] = value;
1631         surf[(X*2)+1] = value;
1632         surf[(X*2)+Pitch] = value;
1633         surf[(X*2)+Pitch+1] = value;
1634         bits -= 2;
1635         bits &= 7;
1636       }
1637     }
1638   }
1639
1640   /*
1641    * Double VGA framebuffer (320x200 -> 640x400), if needed.
1642    */
1643   else if(Height >= 2 * vga_fb_height && Width >= 2 * vga_fb_width && bpp == 1)
1644     for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch*2,dat+=vga_fb_pitch)
1645       for (X=0; X<vga_fb_width; X++) {
1646        BYTE value = dat[X];
1647        surf[X*2] = value;
1648        surf[X*2+1] = value;
1649        surf[X*2+Pitch] = value;
1650        surf[X*2+Pitch+1] = value;
1651       }
1652   /*
1653    * Linear Buffer, including mode 19
1654    */
1655   else
1656     for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch,dat+=vga_fb_pitch)
1657       memcpy(surf, dat, vga_fb_width * bpp);
1658
1659   VGA_Unlock();
1660 }
1661
1662 static void VGA_Poll_Text(void)
1663 {
1664     char *dat, *old, *p_line;
1665     unsigned int X, Y;
1666     CHAR_INFO ch[256]; /* that should suffice for the largest text width */
1667     COORD siz, off;
1668     SMALL_RECT dest;
1669     HANDLE con = VGA_AlphaConsole();
1670     BOOL linechanged = FALSE; /* video memory area differs from stored copy? */
1671
1672     /* Synchronize cursor position. */
1673     off.X = vga_text_x;
1674     off.Y = vga_text_y;
1675     SetConsoleCursorPosition(con,off);
1676
1677     dat = VGA_AlphaBuffer();
1678     old = vga_text_old; /* pointer to stored video mem copy */
1679     siz.X = vga_text_width; siz.Y = 1;
1680     off.X = 0; off.Y = 0;
1681
1682     /* copy from virtual VGA frame buffer to console */
1683     for (Y=0; Y<vga_text_height; Y++) {
1684         linechanged = memcmp(dat, old, vga_text_width*2);
1685         if (linechanged)
1686         {
1687             /*TRACE("line %d changed\n", Y);*/
1688             p_line = dat;
1689             for (X=0; X<vga_text_width; X++) {
1690                 ch[X].Char.AsciiChar = *p_line++;
1691                 /* WriteConsoleOutputA doesn't like "dead" chars */
1692                 if (ch[X].Char.AsciiChar == '\0')
1693                     ch[X].Char.AsciiChar = ' ';
1694                 ch[X].Attributes = *p_line++;
1695             }
1696             dest.Top=Y; dest.Bottom=Y;
1697             dest.Left=0; dest.Right=vga_text_width+1;
1698             WriteConsoleOutputA(con, ch, siz, off, &dest);
1699             memcpy(old, dat, vga_text_width*2);
1700         }
1701         /* advance to next text line */
1702         dat += vga_text_width*2;
1703         old += vga_text_width*2;
1704     }
1705 }
1706
1707 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
1708 {
1709     EnterCriticalSection(&vga_lock);
1710
1711     if (lpddraw)
1712         VGA_Poll_Graphics();
1713     else
1714         VGA_Poll_Text();
1715
1716     /*
1717      * Fake start of retrace.
1718      */
1719     vga_retrace_vertical = TRUE;
1720
1721     LeaveCriticalSection(&vga_lock);
1722 }
1723
1724 static BYTE palreg,palcnt;
1725 static PALETTEENTRY paldat;
1726
1727 void VGA_ioport_out( WORD port, BYTE val )
1728 {
1729     switch (port) {
1730         /* General Register - Feature Control */
1731         case 0x3ba:
1732            FIXME("Unsupported VGA register: general register - feature control 0x%04x (value 0x%02x)\n", port, val);
1733            break;
1734         /* Attribute Controller - Address/Other */
1735         case 0x3c0:
1736            if (vga_address_3c0)
1737                vga_index_3c0 = val;
1738            else
1739                FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x (value 0x%02x)\n",
1740                      vga_index_3c0, val);
1741            vga_address_3c0 = !vga_address_3c0;
1742            break;
1743         /* General Register - Misc output */
1744         case 0x3c2:
1745            FIXME("Unsupported VGA register: general register - misc output 0x%04x (value 0x%02x)\n", port, val);
1746            break;
1747         /* General Register - Video subsystem enable */
1748         case 0x3c3:
1749            FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x (value 0x%02x)\n", port, val);
1750            break;
1751         /* Sequencer Register - Address */
1752         case 0x3c4:
1753            vga_index_3c4 = val;
1754            break;
1755         /* Sequencer Register - Other */
1756         case 0x3c5:
1757           switch(vga_index_3c4) {
1758                case 0x04: /* Sequencer: Memory Mode Register */
1759                   if(vga_fb_depth == 8)
1760                       VGA_SetWindowStart((val & 8) ? 0 : -1);
1761                   else
1762                       FIXME("Memory Mode Register not supported in this mode.\n");
1763                   break;
1764                default:
1765                   FIXME("Unsupported index, VGA sequencer register 0x3c4: 0x%02x (value 0x%02x)\n",
1766                         vga_index_3c4, val);
1767            }
1768            break;
1769         case 0x3c8:
1770             palreg=val; palcnt=0; break;
1771         case 0x3c9:
1772             ((BYTE*)&paldat)[palcnt++]=val << 2;
1773             if (palcnt==3) {
1774                 VGA_SetPalette(&paldat,palreg++,1);
1775                 palcnt=0;
1776             }
1777             break;
1778         /* Graphics Controller Register - Address */
1779         case 0x3ce:
1780             vga_index_3ce = val;
1781            break;
1782         /* Graphics Controller Register - Other */
1783         case 0x3cf:
1784            FIXME("Unsupported index, VGA graphics controller register - other 0x3ce: 0x%02x (value 0x%02x)\n",
1785                  vga_index_3ce, val);
1786            break;
1787         /* CRT Controller Register - Index (MDA) */
1788         case 0x3b4:
1789         /* CRT Controller Register - Index (CGA) */
1790         case 0x3d4:
1791            vga_index_3d4 = val;
1792            break;
1793         /* CRT Controller Register - Other (MDA) */
1794         case 0x3b5:
1795         /* CRT Controller Register - Other (CGA) */
1796         case 0x3d5:
1797            FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x (value 0x%02x)\n",
1798                  vga_index_3d4, val);
1799            break;
1800         /* Mode control register - 6845 Motorola (MDA) */
1801         case 0x3b8:
1802         /* Mode control register - 6845 Motorola (CGA) */
1803         case 0x3d8:
1804            /*
1805             *  xxxxxxx1 = 80x25 text
1806             *  xxxxxxx0 = 40x25 text (default)
1807             *  xxxxxx1x = graphics (320x200)
1808             *  xxxxxx0x = text
1809             *  xxxxx1xx = B/W
1810             *  xxxxx0xx = color
1811             *  xxxx1xxx = enable video signal
1812             *  xxx1xxxx = 640x200 B/W graphics
1813             *  xx1xxxxx = blink
1814             */
1815
1816            /* check bits 6 and 7 */
1817            if (val & 0xC0)
1818            {
1819               FIXME("Unsupported value, VGA register 0x3d8: 0x%02x - bits 7 and 6 not supported.\n", val);
1820            }
1821            /* check bits 5 - blink on */
1822            if (val & 0x20)
1823            {
1824               FIXME("Unsupported value, VGA register 0x3d8: 0x%02x (bit 5) - blink is not supported.\n", val);
1825            }
1826            /* Enable Video Signal (bit 3) - Set the enabled bit */
1827            VGA_SetEnabled((val & 0x08) != 0);
1828
1829            /* xxx1x010 - Detect 160x200, 16 color mode (CGA composite) */
1830            if( (val & 0x17) == 0x12 )
1831            {
1832              /* Switch to 160x200x4 composite mode - special case of mode 6 */
1833              CGA_ColorComposite = TRUE;
1834              VGA_SetMode(6);
1835            }
1836            else
1837            {
1838              /* turn off composite colors otherwise, including 320x200 (80x200 16 color) */
1839              CGA_ColorComposite = FALSE;
1840            }
1841
1842            /* xxx0x100 - Detect VGA mode 0 */
1843            if( (val & 0x17) == 0x04 )
1844            {
1845              VGA_SetMode(0);
1846            }
1847            /* xxx0x000 - Detect VGA mode 1 */
1848            else if( (val & 0x17) == 0x00 )
1849            {
1850              VGA_SetMode(1);
1851            }
1852            /* xxx0x101 - Detect VGA mode 2 */
1853            else if( (val & 0x17) == 0x05 )
1854            {
1855              VGA_SetMode(2);
1856            }
1857            /* xxx0x001 - Detect VGA mode 3 */
1858            else if( (val & 0x17) == 0x01 )
1859            {
1860              VGA_SetMode(3);
1861            }
1862            /* xxx0x010 - Detect VGA mode 4 */
1863            else if( (val & 0x17) == 0x02 )
1864            {
1865              VGA_SetMode(4);
1866            }
1867            /* xxx0x110 - Detect VGA mode 5 */
1868            else if( (val & 0x17) == 0x06 )
1869            {
1870              VGA_SetMode(5);
1871            }
1872            /* xxx1x110 - Detect VGA mode 6 */
1873            else if( (val & 0x17) == 0x16 )
1874            {
1875              VGA_SetMode(6);
1876            }
1877            /* unsupported mode */
1878            else
1879            {
1880              FIXME("Unsupported value, VGA register 0x3d8: 0x%02x - unrecognized MDA/CGA mode\n", val); /* Set the enabled bit */
1881            }
1882
1883            break;
1884
1885         /* Colour control register (CGA) */
1886         case 0x3d9:
1887            /* Set bright */
1888            VGA_SetBright((val & 0x10) != 0);
1889
1890            /* Set palette index */
1891            VGA_SetPaletteIndex((val & 0x20) != 0);
1892
1893            /* Now update the palette */
1894            VGA_UpdatePalette();
1895            break;
1896         default:
1897             FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1898     }
1899 }
1900
1901 BYTE VGA_ioport_in( WORD port )
1902 {
1903     BYTE ret;
1904
1905     switch (port) {
1906         /* Attribute Controller - Other */
1907         case 0x3c1:
1908            FIXME("Unsupported index, VGA attribute controller register 0x3c0: 0x%02x\n",
1909                  vga_index_3c0);
1910            return 0xff;
1911         /* General Register - Input status 0 */
1912         case 0x3c2:
1913            ret=0xff;
1914            FIXME("Unsupported VGA register: general register - input status 0 0x%04x\n", port);
1915            break;
1916         /* General Register - Video subsystem enable */
1917         case 0x3c3:
1918            ret=0xff;
1919            FIXME("Unsupported VGA register: general register - video subsystem enable 0x%04x\n", port);
1920            break;
1921         /* Sequencer Register - Other */
1922         case 0x3c5:
1923            switch(vga_index_3c4) {
1924                case 0x04: /* Sequencer: Memory Mode Register */
1925                     return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1926                default:
1927                    FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1928                          vga_index_3c4);
1929                    return 0xff;
1930            }
1931         /* General Register -  DAC State */
1932         case 0x3c7:
1933            ret=0xff;
1934            FIXME("Unsupported VGA register: general register - DAC State 0x%04x\n", port);
1935            break;
1936         /* General Register - Feature control */
1937         case 0x3ca:
1938            ret=0xff;
1939            FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port);
1940            break;
1941         /* General Register - Misc output */
1942         case 0x3cc:
1943            ret=0xff;
1944            FIXME("Unsupported VGA register: general register - Feature control 0x%04x\n", port);
1945            break;
1946         /* Graphics Controller Register - Other */
1947         case 0x3cf:
1948            FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1949                  vga_index_3ce);
1950            return 0xff;
1951         /* CRT Controller Register - Other (MDA) */
1952         case 0x3b5:
1953         /* CRT Controller Register - Other (CGA) */
1954         case 0x3d5:
1955            FIXME("Unsupported index, VGA crt controller register 0x3b4/0x3d4: 0x%02x\n",
1956                  vga_index_3d4);
1957            return 0xff;
1958         /* General Register - Input status 1 (MDA) */
1959         case 0x3ba:
1960         /* General Register - Input status 1 (CGA) */
1961         case 0x3da:
1962             /*
1963              * Read from this register resets register 0x3c0 address flip-flop.
1964              */
1965             vga_address_3c0 = TRUE;
1966
1967             /*
1968              * Read from this register returns following bits:
1969              *   xxxx1xxx = Vertical retrace in progress if set.
1970              *   xxxxx1xx = Light pen switched on.
1971              *   xxxxxx1x = Light pen trigger set.
1972              *   xxxxxxx1 = Either vertical or horizontal retrace 
1973              *              in progress if set.
1974              */
1975             ret = 0;
1976             if (vga_retrace_vertical)
1977                 ret |= 9;
1978             if (vga_retrace_horizontal)
1979                 ret |= 3;
1980             
1981             /*
1982              * If VGA mode has been set, vertical retrace is
1983              * turned on once a frame and cleared after each read.
1984              * This might cause applications that synchronize with
1985              * vertical retrace to actually skip one frame but that
1986              * is probably not a problem.
1987              * 
1988              * If no VGA mode has been set, vertical retrace is faked
1989              * by toggling the value after every read.
1990              */
1991             if (VGA_IsTimerRunning())
1992                 vga_retrace_vertical = FALSE;
1993             else
1994                 vga_retrace_vertical = !vga_retrace_vertical;
1995
1996             /*
1997              * Toggle horizontal retrace.
1998              */
1999             vga_retrace_horizontal = !vga_retrace_horizontal;
2000             break;
2001
2002         default:
2003             ret=0xff;
2004             FIXME("Unsupported VGA register: 0x%04x\n", port);
2005     }
2006     return ret;
2007 }
2008
2009 void VGA_Clean(void)
2010 {
2011     VGA_Exit();
2012     VGA_DeinstallTimer();
2013 }