Allow application to use VGA window that overlaps framebuffer only
[wine/multimedia.git] / dlls / winedos / vga.c
blobf54c42768873f252b6c333142d944ee662e69387
1 /*
2 * VGA hardware emulation
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <string.h>
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "wincon.h"
29 #include "miscemu.h"
30 #include "dosexe.h"
31 #include "vga.h"
32 #include "ddraw.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
37 static IDirectDraw *lpddraw = NULL;
38 static IDirectDrawSurface *lpddsurf;
39 static IDirectDrawPalette *lpddpal;
40 static DDSURFACEDESC sdesc;
42 static BOOL vga_retrace_vertical;
43 static BOOL vga_retrace_horizontal;
46 * Size and location of VGA controller window to framebuffer.
48 * Note: We support only single window even though some
49 * controllers support two. This should not be changed unless
50 * there are programs that depend on having two windows.
52 #define VGA_WINDOW_SIZE (64 * 1024)
53 #define VGA_WINDOW_START ((char *)0xa0000)
56 * VGA controller memory is emulated using linear framebuffer.
57 * This frambuffer also acts as an interface
58 * between VGA controller emulation and DirectDraw.
60 * vga_fb_width: Display width in pixels. Can be modified when
61 * display mode is changed.
62 * vga_fb_height: Display height in pixels. Can be modified when
63 * display mode is changed.
64 * vga_fb_depth: Number of bits used to store single pixel color information.
65 * Each pixel uses (vga_fb_depth+7)/8 bytes because
66 * 1-16 color modes are mapped to 256 color mode.
67 * Can be modified when display mode is changed.
68 * vga_fb_pitch: How many bytes to add to pointer in order to move
69 * from one row to another. This is fixed in VGA modes,
70 * but can be modified in SVGA modes.
71 * vga_fb_offset: Offset added to framebuffer start address in order
72 * to find the display origin. Programs use this to do
73 * double buffering and to scroll display. The value can
74 * be modified in VGA and SVGA modes.
75 * vga_fb_size: How many bytes are allocated to framebuffer.
76 * VGA framebuffers are always larger than display size and
77 * SVGA framebuffers may also be.
78 * vga_fb_data: Pointer to framebuffer start.
79 * vga_fb_window: Offset of 64k window 0xa0000 in bytes from framebuffer start.
80 * This value is >= 0, if mode uses linear framebuffer and
81 * -1, if mode uses color planes. This value is fixed
82 * in all modes except 0x13 (256 color VGA) where
83 * 0 means normal mode and -1 means Mode-X (unchained mode).
85 static int vga_fb_width;
86 static int vga_fb_height;
87 static int vga_fb_depth;
88 static int vga_fb_pitch;
89 static int vga_fb_offset;
90 static int vga_fb_size = 0;
91 static char *vga_fb_data = 0;
92 static int vga_fb_window = 0;
95 * VGA text mode data.
97 * vga_text_attr: Current active attribute.
98 * vga_text_old: Last data sent to console.
99 * This is used to optimize console updates.
100 * vga_text_width: Width of the text display in characters.
101 * vga_text_height: Height of the text display in characters.
102 * vga_text_x: Current cursor X-position. Starts from zero.
103 * vga_text_y: Current cursor Y-position. Starts from zero.
104 * vga_text_console: TRUE if stdout is console,
105 * FALSE if it is regular file.
107 static BYTE vga_text_attr;
108 static char *vga_text_old = NULL;
109 static BYTE vga_text_width;
110 static BYTE vga_text_height;
111 static BYTE vga_text_x;
112 static BYTE vga_text_y;
113 static BOOL vga_text_console;
116 * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
117 * indexed registers. These ports are used to select VGA controller
118 * subregister that can be written to or read from using ports 0x3c1,
119 * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
120 * stored in variables vga_index_*.
122 * Port 0x3c0 is special because it is both index and
123 * data-write register. Flip-flop vga_address_3c0 tells whether
124 * the port acts currently as an address register. Reading from port
125 * 0x3da resets the flip-flop to address mode.
127 static BYTE vga_index_3c0;
128 static BYTE vga_index_3c4;
129 static BYTE vga_index_3ce;
130 static BYTE vga_index_3d4;
131 static BOOL vga_address_3c0 = TRUE;
134 * This mutex is used to protect VGA state during asynchronous
135 * screen updates (see VGA_Poll). It makes sure that VGA state changes
136 * are atomic and the user interface is protected from flicker and
137 * corruption.
139 * The mutex actually serializes VGA operations and the screen update.
140 * Which means that whenever VGA_Poll occurs, application stalls if it
141 * tries to modify VGA state. This is not how real VGA adapters work,
142 * but it makes timing and correctness issues much easier to deal with.
144 static CRITICAL_SECTION vga_lock;
145 static CRITICAL_SECTION_DEBUG critsect_debug =
147 0, 0, &vga_lock,
148 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
149 0, 0, { 0, (DWORD)(__FILE__ ": vga_lock") }
151 static CRITICAL_SECTION vga_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
153 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
154 static DirectDrawCreateProc pDirectDrawCreate;
156 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
158 static HWND vga_hwnd = NULL;
161 * For simplicity, I'm creating a second palette.
162 * 16 color accesses will use these pointers and insert
163 * entries from the 64-color palette into the default
164 * palette. --Robert 'Admiral' Coeyman
167 static char vga_16_palette[17]={
168 0x00, /* 0 - Black */
169 0x01, /* 1 - Blue */
170 0x02, /* 2 - Green */
171 0x03, /* 3 - Cyan */
172 0x04, /* 4 - Red */
173 0x05, /* 5 - Magenta */
174 0x14, /* 6 - Brown */
175 0x07, /* 7 - Light gray */
176 0x38, /* 8 - Dark gray */
177 0x39, /* 9 - Light blue */
178 0x3a, /* A - Light green */
179 0x3b, /* B - Light cyan */
180 0x3c, /* C - Light red */
181 0x3d, /* D - Light magenta */
182 0x3e, /* E - Yellow */
183 0x3f, /* F - White */
184 0x00 /* Border Color */
187 static PALETTEENTRY vga_def_palette[256]={
188 /* red green blue */
189 {0x00, 0x00, 0x00}, /* 0 - Black */
190 {0x00, 0x00, 0x80}, /* 1 - Blue */
191 {0x00, 0x80, 0x00}, /* 2 - Green */
192 {0x00, 0x80, 0x80}, /* 3 - Cyan */
193 {0x80, 0x00, 0x00}, /* 4 - Red */
194 {0x80, 0x00, 0x80}, /* 5 - Magenta */
195 {0x80, 0x80, 0x00}, /* 6 - Brown */
196 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
197 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
198 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
199 {0x00, 0xFF, 0x00}, /* A - Light green */
200 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
201 {0xFF, 0x00, 0x00}, /* C - Light red */
202 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
203 {0xFF, 0xFF, 0x00}, /* E - Yellow */
204 {0xFF, 0xFF, 0xFF}, /* F - White */
205 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
209 * This palette is the dos default, converted from 18 bit color to 24.
210 * It contains only 64 entries of colors--all others are zeros.
211 * --Robert 'Admiral' Coeyman
213 static PALETTEENTRY vga_def64_palette[256]={
214 /* red green blue */
215 {0x00, 0x00, 0x00}, /* 0x00 Black */
216 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
217 {0x00, 0xaa, 0x00}, /* 0x02 Green */
218 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
219 {0xaa, 0x00, 0x00}, /* 0x04 Red */
220 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
221 {0xaa, 0xaa, 0x00}, /* 0x06 */
222 {0xaa, 0xaa, 0xaa}, /* 0x07 Light Gray */
223 {0x00, 0x00, 0x55}, /* 0x08 */
224 {0x00, 0x00, 0xff}, /* 0x09 */
225 {0x00, 0xaa, 0x55}, /* 0x0a */
226 {0x00, 0xaa, 0xff}, /* 0x0b */
227 {0xaa, 0x00, 0x55}, /* 0x0c */
228 {0xaa, 0x00, 0xff}, /* 0x0d */
229 {0xaa, 0xaa, 0x55}, /* 0x0e */
230 {0xaa, 0xaa, 0xff}, /* 0x0f */
231 {0x00, 0x55, 0x00}, /* 0x10 */
232 {0x00, 0x55, 0xaa}, /* 0x11 */
233 {0x00, 0xff, 0x00}, /* 0x12 */
234 {0x00, 0xff, 0xaa}, /* 0x13 */
235 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
236 {0xaa, 0x55, 0xaa}, /* 0x15 */
237 {0xaa, 0xff, 0x00}, /* 0x16 */
238 {0xaa, 0xff, 0xaa}, /* 0x17 */
239 {0x00, 0x55, 0x55}, /* 0x18 */
240 {0x00, 0x55, 0xff}, /* 0x19 */
241 {0x00, 0xff, 0x55}, /* 0x1a */
242 {0x00, 0xff, 0xff}, /* 0x1b */
243 {0xaa, 0x55, 0x55}, /* 0x1c */
244 {0xaa, 0x55, 0xff}, /* 0x1d */
245 {0xaa, 0xff, 0x55}, /* 0x1e */
246 {0xaa, 0xff, 0xff}, /* 0x1f */
247 {0x55, 0x00, 0x00}, /* 0x20 */
248 {0x55, 0x00, 0xaa}, /* 0x21 */
249 {0x55, 0xaa, 0x00}, /* 0x22 */
250 {0x55, 0xaa, 0xaa}, /* 0x23 */
251 {0xff, 0x00, 0x00}, /* 0x24 */
252 {0xff, 0x00, 0xaa}, /* 0x25 */
253 {0xff, 0xaa, 0x00}, /* 0x26 */
254 {0xff, 0xaa, 0xaa}, /* 0x27 */
255 {0x55, 0x00, 0x55}, /* 0x28 */
256 {0x55, 0x00, 0xff}, /* 0x29 */
257 {0x55, 0xaa, 0x55}, /* 0x2a */
258 {0x55, 0xaa, 0xff}, /* 0x2b */
259 {0xff, 0x00, 0x55}, /* 0x2c */
260 {0xff, 0x00, 0xff}, /* 0x2d */
261 {0xff, 0xaa, 0x55}, /* 0x2e */
262 {0xff, 0xaa, 0xff}, /* 0x2f */
263 {0x55, 0x55, 0x00}, /* 0x30 */
264 {0x55, 0x55, 0xaa}, /* 0x31 */
265 {0x55, 0xff, 0x00}, /* 0x32 */
266 {0x55, 0xff, 0xaa}, /* 0x33 */
267 {0xff, 0x55, 0x00}, /* 0x34 */
268 {0xff, 0x55, 0xaa}, /* 0x35 */
269 {0xff, 0xff, 0x00}, /* 0x36 */
270 {0xff, 0xff, 0xaa}, /* 0x37 */
271 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
272 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
273 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
274 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
275 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
276 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
277 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
278 {0xff, 0xff, 0xff}, /* 0x3f White */
279 {0,0,0} /* The next 192 entries are all zeros */
282 static HANDLE VGA_timer;
283 static HANDLE VGA_timer_thread;
285 /* set the timer rate; called in the polling thread context */
286 static void CALLBACK set_timer_rate( ULONG_PTR arg )
288 LARGE_INTEGER when;
290 when.s.LowPart = when.s.HighPart = 0;
291 SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
294 static DWORD CALLBACK VGA_TimerThread( void *dummy )
296 for (;;) SleepEx( INFINITE, TRUE );
299 static void VGA_DeinstallTimer(void)
301 if (VGA_timer_thread)
304 * Make sure the update thread is not holding
305 * system resources when we kill it.
307 * Now, we only need to worry about update thread
308 * getting terminated while in EnterCriticalSection
309 * or WaitForMultipleObjectsEx.
311 * FIXME: Is this a problem?
313 EnterCriticalSection(&vga_lock);
315 CancelWaitableTimer( VGA_timer );
316 CloseHandle( VGA_timer );
317 TerminateThread( VGA_timer_thread, 0 );
318 CloseHandle( VGA_timer_thread );
319 VGA_timer_thread = 0;
321 LeaveCriticalSection(&vga_lock);
324 * Synchronize display. This makes sure that
325 * changes to display become visible even if program
326 * terminates before update thread had time to run.
328 VGA_Poll( 0, 0, 0 );
332 static void VGA_InstallTimer(unsigned Rate)
334 if (!VGA_timer_thread)
336 VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
337 VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
339 QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
342 static BOOL VGA_IsTimerRunning(void)
344 return VGA_timer_thread ? TRUE : FALSE;
347 HANDLE VGA_AlphaConsole(void)
349 /* this assumes that no Win32 redirection has taken place, but then again,
350 * only 16-bit apps are likely to use this part of Wine... */
351 return GetStdHandle(STD_OUTPUT_HANDLE);
354 char*VGA_AlphaBuffer(void)
356 return (char *)0xb8000;
359 /*** GRAPHICS MODE ***/
361 typedef struct {
362 unsigned Xres, Yres, Depth;
363 int ret;
364 } ModeSet;
367 /**********************************************************************
368 * VGA_SyncWindow
370 * Copy VGA window into framebuffer (if argument is TRUE) or
371 * part of framebuffer into VGA window (if argument is FALSE).
373 static void VGA_SyncWindow( BOOL target_is_fb )
375 int size = VGA_WINDOW_SIZE;
377 /* Window does not overlap framebuffer. */
378 if (vga_fb_window >= vga_fb_size)
379 return;
381 /* Check if window overlaps framebuffer only partially. */
382 if (vga_fb_size - vga_fb_window < VGA_WINDOW_SIZE)
383 size = vga_fb_size - vga_fb_window;
385 if (target_is_fb)
386 memmove( vga_fb_data + vga_fb_window, VGA_WINDOW_START, size );
387 else
388 memmove( VGA_WINDOW_START, vga_fb_data + vga_fb_window, size );
392 static void WINAPI VGA_DoExit(ULONG_PTR arg)
394 VGA_DeinstallTimer();
395 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
396 IDirectDrawSurface_Release(lpddsurf);
397 lpddsurf=NULL;
398 IDirectDrawPalette_Release(lpddpal);
399 lpddpal=NULL;
400 IDirectDraw_Release(lpddraw);
401 lpddraw=NULL;
404 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
406 LRESULT res;
407 ModeSet *par = (ModeSet *)arg;
408 par->ret=1;
410 if (lpddraw) VGA_DoExit(0);
411 if (!lpddraw) {
412 if (!pDirectDrawCreate)
414 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
415 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
416 if (!pDirectDrawCreate) {
417 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
418 return;
421 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
422 if (!lpddraw) {
423 ERR("DirectDraw is not available (res = %lx)\n",res);
424 return;
426 if (!vga_hwnd) {
427 vga_hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",
428 WS_POPUP|WS_VISIBLE|SS_NOTIFY,0,0,
429 par->Xres,par->Yres,0,0,0,NULL);
430 if (!vga_hwnd) {
431 ERR("Failed to create user window.\n");
432 IDirectDraw_Release(lpddraw);
433 lpddraw=NULL;
434 return;
437 else
438 SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
440 if ((res=IDirectDraw_SetCooperativeLevel(lpddraw,vga_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) {
441 ERR("Could not set cooperative level to exclusive (%lx)\n",res);
444 if ((res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth))) {
445 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par->Xres,par->Yres,par->Depth,res);
446 IDirectDraw_Release(lpddraw);
447 lpddraw=NULL;
448 return;
451 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
452 if (res) {
453 ERR("Could not create palette (res = %lx)\n",res);
454 IDirectDraw_Release(lpddraw);
455 lpddraw=NULL;
456 return;
458 if ((res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette))) {
459 ERR("Could not set default palette entries (res = %lx)\n", res);
462 memset(&sdesc,0,sizeof(sdesc));
463 sdesc.dwSize=sizeof(sdesc);
464 sdesc.dwFlags = DDSD_CAPS;
465 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
466 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
467 ERR("DirectDraw surface is not available\n");
468 IDirectDraw_Release(lpddraw);
469 lpddraw=NULL;
470 return;
472 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
473 vga_retrace_vertical = vga_retrace_horizontal = FALSE;
474 /* poll every 20ms (50fps should provide adequate responsiveness) */
475 VGA_InstallTimer(20);
477 par->ret=0;
478 return;
481 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
483 ModeSet par;
484 int newSize;
486 vga_fb_width = Xres;
487 vga_fb_height = Yres;
488 vga_fb_depth = Depth;
489 vga_fb_offset = 0;
490 vga_fb_pitch = Xres * ((Depth + 7) / 8);
492 newSize = Xres * Yres * ((Depth + 7) / 8);
493 if(newSize < 256 * 1024)
494 newSize = 256 * 1024;
496 if(vga_fb_size < newSize) {
497 if(vga_fb_data)
498 HeapFree(GetProcessHeap(), 0, vga_fb_data);
499 vga_fb_data = HeapAlloc(GetProcessHeap(), 0, newSize);
500 vga_fb_size = newSize;
503 if(Xres >= 640 || Yres >= 480) {
504 par.Xres = Xres;
505 par.Yres = Yres;
506 } else {
507 par.Xres = 640;
508 par.Yres = 480;
511 VGA_SetWindowStart((Depth < 8) ? -1 : 0);
513 par.Depth = (Depth < 8) ? 8 : Depth;
515 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
516 return par.ret;
519 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
521 if (!lpddraw) return 1;
522 if (!lpddsurf) return 1;
523 if (Height) *Height=sdesc.dwHeight;
524 if (Width) *Width=sdesc.dwWidth;
525 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
526 return 0;
529 void VGA_Exit(void)
531 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
534 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
536 if (!lpddraw) return;
537 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
540 /* set a single [char wide] color in 16 color mode. */
541 void VGA_SetColor16(int reg,int color)
543 PALETTEENTRY *pal;
545 if (!lpddraw) return;
546 pal= &vga_def64_palette[color];
547 IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
548 vga_16_palette[reg]=(char)color;
551 /* Get a single [char wide] color in 16 color mode. */
552 char VGA_GetColor16(int reg)
555 if (!lpddraw) return 0;
556 return (char)vga_16_palette[reg];
559 /* set all 17 [char wide] colors at once in 16 color mode. */
560 void VGA_Set16Palette(char *Table)
562 PALETTEENTRY *pal;
563 int c;
565 if (!lpddraw) return; /* return if we're in text only mode */
566 memcpy( Table, &vga_16_palette, 17 ); /* copy the entries into the table */
568 for (c=0; c<17; c++) { /* 17 entries */
569 pal= &vga_def64_palette[(int)vga_16_palette[c]]; /* get color */
570 IDirectDrawPalette_SetEntries(lpddpal,0,c,1,pal); /* set entry */
571 TRACE("Palette register %d set to %d\n",c,(int)vga_16_palette[c]);
572 } /* end of the counting loop */
575 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
576 void VGA_Get16Palette(char *Table)
579 if (!lpddraw) return; /* return if we're in text only mode */
580 memcpy( &vga_16_palette, Table, 17 ); /* copy the entries into the table */
583 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
585 PALETTEENTRY pal[256];
586 int c;
588 if (!lpddraw) return;
589 for (c=0; c<len; c++) {
590 pal[c].peRed =color[c].rgbRed;
591 pal[c].peGreen=color[c].rgbGreen;
592 pal[c].peBlue =color[c].rgbBlue;
593 pal[c].peFlags=0;
595 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
598 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
600 if (!lpddraw) return NULL;
601 if (!lpddsurf) return NULL;
602 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
603 ERR("could not lock surface!\n");
604 return NULL;
606 if (Pitch) *Pitch=sdesc.u1.lPitch;
607 if (Height) *Height=sdesc.dwHeight;
608 if (Width) *Width=sdesc.dwWidth;
609 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
610 return sdesc.lpSurface;
613 void VGA_Unlock(void)
615 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
619 * Set start of 64k window at 0xa0000 in bytes.
620 * If value is -1, initialize color plane support.
621 * If value is >= 0, window contains direct copy of framebuffer.
623 void VGA_SetWindowStart(int start)
625 if(start == vga_fb_window)
626 return;
628 EnterCriticalSection(&vga_lock);
630 if(vga_fb_window == -1)
631 FIXME("Remove VGA memory emulation.\n");
632 else
633 VGA_SyncWindow( TRUE );
635 vga_fb_window = start;
637 if(vga_fb_window == -1)
638 FIXME("Install VGA memory emulation.\n");
639 else
640 VGA_SyncWindow( FALSE );
642 LeaveCriticalSection(&vga_lock);
646 * Get start of 64k window at 0xa0000 in bytes.
647 * Value is -1 in color plane modes.
649 int VGA_GetWindowStart()
651 return vga_fb_window;
654 /*** TEXT MODE ***/
656 /* prepare the text mode video memory copy that is used to only
657 * update the video memory line that did get updated. */
658 void VGA_PrepareVideoMemCopy(unsigned Xres, unsigned Yres)
660 char *p, *p2;
661 int i;
664 * Allocate space for char + attr.
666 vga_text_old = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
667 vga_text_old, Xres * Yres * 2 );
669 p = VGA_AlphaBuffer();
670 p2 = vga_text_old;
672 /* make sure the video mem copy contains the exact opposite of our
673 * actual text mode memory area to make sure the screen
674 * does get updated fully initially */
675 for (i=0; i < Xres*Yres*2; i++)
676 *p2++ = *p++ ^ 0xff; /* XOR it */
679 /**********************************************************************
680 * VGA_SetAlphaMode
682 * Set VGA emulation to text mode.
684 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
686 VGA_Exit();
687 VGA_DeinstallTimer();
689 VGA_PrepareVideoMemCopy(Xres, Yres);
690 vga_text_width = Xres;
691 vga_text_height = Yres;
693 if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
694 VGA_SetCursorPos(0,0);
696 if(vga_text_console) {
697 COORD size;
698 size.X = Xres;
699 size.Y = Yres;
700 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
702 /* poll every 30ms (33fps should provide adequate responsiveness) */
703 VGA_InstallTimer(30);
707 /**********************************************************************
708 * VGA_InitAlphaMode
710 * Initialize VGA text mode handling and return default text mode.
711 * This function does not set VGA emulation to text mode.
713 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
715 CONSOLE_SCREEN_BUFFER_INFO info;
717 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
719 vga_text_console = TRUE;
720 vga_text_x = info.dwCursorPosition.X;
721 vga_text_y = info.dwCursorPosition.Y;
722 vga_text_attr = info.wAttributes;
723 *Xres = info.dwSize.X;
724 *Yres = info.dwSize.Y;
726 else
728 vga_text_console = FALSE;
729 vga_text_x = 0;
730 vga_text_y = 0;
731 vga_text_attr = 0x0f;
732 *Xres = 80;
733 *Yres = 25;
737 /**********************************************************************
738 * VGA_GetAlphaMode
740 * Get current text mode. Returns TRUE and sets resolution if
741 * any VGA text mode has been initialized.
743 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
745 if (vga_text_width != 0 && vga_text_height != 0) {
746 *Xres = vga_text_width;
747 *Yres = vga_text_height;
748 return TRUE;
749 } else
750 return FALSE;
753 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
755 CONSOLE_CURSOR_INFO cci;
757 /* standard cursor settings:
758 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
760 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
761 cci.dwSize = ((end & 0x1f) - (start_options & 0x1f))/0x0e * 100;
762 if (!cci.dwSize) cci.dwSize++; /* NULL cursor would make SCCI() fail ! */
763 cci.bVisible = ((start_options & 0x60) != 0x20); /* invisible ? */
765 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
768 void VGA_SetCursorPos(unsigned X,unsigned Y)
770 vga_text_x = X;
771 vga_text_y = Y;
774 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
776 if (X) *X = vga_text_x;
777 if (Y) *Y = vga_text_y;
780 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
782 char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
783 dat[0] = ascii;
784 if (attr>=0)
785 dat[1] = attr;
788 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
790 EnterCriticalSection(&vga_lock);
792 while (count--)
793 VGA_PutCharAt(X + count, Y, ch, attr);
795 LeaveCriticalSection(&vga_lock);
798 void VGA_PutChar(BYTE ascii)
800 EnterCriticalSection(&vga_lock);
802 switch(ascii) {
803 case '\b':
804 if (vga_text_x)
805 vga_text_x--;
806 break;
808 case '\t':
809 vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
810 break;
812 case '\n':
813 vga_text_y++;
814 vga_text_x = 0;
815 break;
817 case '\a':
818 break;
820 case '\r':
821 vga_text_x = 0;
822 break;
824 default:
825 VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
826 vga_text_x++;
829 if (vga_text_x >= vga_text_width)
831 vga_text_x = 0;
832 vga_text_y++;
835 if (vga_text_y >= vga_text_height)
837 vga_text_y = vga_text_height - 1;
838 VGA_ScrollUpText( 0, 0,
839 vga_text_height - 1, vga_text_width - 1,
840 1, vga_text_attr );
844 * If we don't have a console, write directly to standard output.
846 if(!vga_text_console)
847 WriteFile(VGA_AlphaConsole(), &ascii, 1, NULL, NULL);
849 LeaveCriticalSection(&vga_lock);
852 void VGA_SetTextAttribute(BYTE attr)
854 vga_text_attr = attr;
857 void VGA_ClearText(unsigned row1, unsigned col1,
858 unsigned row2, unsigned col2,
859 BYTE attr)
861 unsigned x, y;
863 EnterCriticalSection(&vga_lock);
865 for(y=row1; y<=row2; y++)
866 for(x=col1; x<=col2; x++)
867 VGA_PutCharAt(x, y, 0x20, attr);
869 LeaveCriticalSection(&vga_lock);
872 void VGA_ScrollUpText(unsigned row1, unsigned col1,
873 unsigned row2, unsigned col2,
874 unsigned lines, BYTE attr)
876 char *buffer = VGA_AlphaBuffer();
877 unsigned y;
879 EnterCriticalSection(&vga_lock);
882 * Scroll buffer.
884 for (y = row1; y <= row2 - lines; y++)
885 memmove( buffer + col1 + y * vga_text_width * 2,
886 buffer + col1 + (y + lines) * vga_text_width * 2,
887 (col2 - col1 + 1) * 2 );
890 * Fill exposed lines.
892 for (y = max(row1, row2 - lines + 1); y <= row2; y++)
893 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
895 LeaveCriticalSection(&vga_lock);
898 void VGA_ScrollDownText(unsigned row1, unsigned col1,
899 unsigned row2, unsigned col2,
900 unsigned lines, BYTE attr)
902 char *buffer = VGA_AlphaBuffer();
903 unsigned y;
905 EnterCriticalSection(&vga_lock);
908 * Scroll buffer.
910 for (y = row2; y >= row1 + lines; y--)
911 memmove( buffer + col1 + y * vga_text_width * 2,
912 buffer + col1 + (y - lines) * vga_text_width * 2,
913 (col2 - col1 + 1) * 2 );
916 * Fill exposed lines.
918 for (y = row1; y <= min(row1 + lines - 1, row2); y++)
919 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
921 LeaveCriticalSection(&vga_lock);
924 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
926 char *dat;
928 dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
930 *ascii = dat[0];
931 *attr = dat[1];
935 /*** CONTROL ***/
937 /* FIXME: optimize by doing this only if the data has actually changed
938 * (in a way similar to DIBSection, perhaps) */
939 static void VGA_Poll_Graphics(void)
941 unsigned int Pitch, Height, Width, X, Y;
942 char *surf;
943 char *dat = vga_fb_data + vga_fb_offset;
944 int bpp = (vga_fb_depth + 7) / 8;
946 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
947 if (!surf) return;
950 * Synchronize framebuffer contents.
952 if (vga_fb_window != -1)
953 VGA_SyncWindow( TRUE );
956 * Double VGA framebuffer (320x200 -> 640x400), if needed.
958 if(Height >= 2 * vga_fb_height && Width >= 2 * vga_fb_width && bpp == 1)
959 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch*2,dat+=vga_fb_pitch)
960 for (X=0; X<vga_fb_width; X++) {
961 BYTE value = dat[X];
962 surf[X*2] = value;
963 surf[X*2+1] = value;
964 surf[X*2+Pitch] = value;
965 surf[X*2+Pitch+1] = value;
967 else
968 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch,dat+=vga_fb_pitch)
969 memcpy(surf, dat, vga_fb_width * bpp);
971 VGA_Unlock();
974 static void VGA_Poll_Text(void)
976 char *dat, *old, *p_line;
977 unsigned int X, Y;
978 CHAR_INFO ch[256]; /* that should suffice for the largest text width */
979 COORD siz, off;
980 SMALL_RECT dest;
981 HANDLE con = VGA_AlphaConsole();
982 BOOL linechanged = FALSE; /* video memory area differs from stored copy? */
984 /* Synchronize cursor position. */
985 off.X = vga_text_x;
986 off.Y = vga_text_y;
987 SetConsoleCursorPosition(con,off);
989 dat = VGA_AlphaBuffer();
990 old = vga_text_old; /* pointer to stored video mem copy */
991 siz.X = vga_text_width; siz.Y = 1;
992 off.X = 0; off.Y = 0;
994 /* copy from virtual VGA frame buffer to console */
995 for (Y=0; Y<vga_text_height; Y++) {
996 linechanged = memcmp(dat, old, vga_text_width*2);
997 if (linechanged)
999 /*TRACE("line %d changed\n", Y);*/
1000 p_line = dat;
1001 for (X=0; X<vga_text_width; X++) {
1002 ch[X].Char.AsciiChar = *p_line++;
1003 /* WriteConsoleOutputA doesn't like "dead" chars */
1004 if (ch[X].Char.AsciiChar == '\0')
1005 ch[X].Char.AsciiChar = ' ';
1006 ch[X].Attributes = *p_line++;
1008 dest.Top=Y; dest.Bottom=Y;
1009 dest.Left=0; dest.Right=vga_text_width+1;
1010 WriteConsoleOutputA(con, ch, siz, off, &dest);
1011 memcpy(old, dat, vga_text_width*2);
1013 /* advance to next text line */
1014 dat += vga_text_width*2;
1015 old += vga_text_width*2;
1019 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
1021 EnterCriticalSection(&vga_lock);
1023 if (lpddraw)
1024 VGA_Poll_Graphics();
1025 else
1026 VGA_Poll_Text();
1029 * Fake start of retrace.
1031 vga_retrace_vertical = TRUE;
1033 LeaveCriticalSection(&vga_lock);
1036 static BYTE palreg,palcnt;
1037 static PALETTEENTRY paldat;
1039 void VGA_ioport_out( WORD port, BYTE val )
1041 switch (port) {
1042 case 0x3c0:
1043 if (vga_address_3c0)
1044 vga_index_3c0 = val;
1045 else
1046 FIXME("Unsupported index, register 0x3c0: 0x%02x (value 0x%02x)\n",
1047 vga_index_3c0, val);
1048 vga_address_3c0 = !vga_address_3c0;
1049 break;
1050 case 0x3c4:
1051 vga_index_3c4 = val;
1052 break;
1053 case 0x3c5:
1054 switch(vga_index_3c4) {
1055 case 0x04: /* Sequencer: Memory Mode Register */
1056 if(vga_fb_depth == 8)
1057 VGA_SetWindowStart((val & 8) ? 0 : -1);
1058 else
1059 FIXME("Memory Mode Register not supported in this mode.\n");
1060 break;
1061 default:
1062 FIXME("Unsupported index, register 0x3c4: 0x%02x (value 0x%02x)\n",
1063 vga_index_3c4, val);
1065 break;
1066 case 0x3c8:
1067 palreg=val; palcnt=0; break;
1068 case 0x3c9:
1069 ((BYTE*)&paldat)[palcnt++]=val << 2;
1070 if (palcnt==3) {
1071 VGA_SetPalette(&paldat,palreg++,1);
1072 palcnt=0;
1074 break;
1075 case 0x3ce:
1076 vga_index_3ce = val;
1077 break;
1078 case 0x3cf:
1079 FIXME("Unsupported index, register 0x3ce: 0x%02x (value 0x%02x)\n",
1080 vga_index_3ce, val);
1081 break;
1082 case 0x3d4:
1083 vga_index_3d4 = val;
1084 break;
1085 case 0x3d5:
1086 FIXME("Unsupported index, register 0x3d4: 0x%02x (value 0x%02x)\n",
1087 vga_index_3d4, val);
1088 break;
1089 default:
1090 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1094 BYTE VGA_ioport_in( WORD port )
1096 BYTE ret;
1098 switch (port) {
1099 case 0x3c1:
1100 FIXME("Unsupported index, register 0x3c0: 0x%02x\n",
1101 vga_index_3c0);
1102 return 0xff;
1103 case 0x3c5:
1104 switch(vga_index_3c4) {
1105 case 0x04: /* Sequencer: Memory Mode Register */
1106 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1107 default:
1108 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1109 vga_index_3c4);
1110 return 0xff;
1112 case 0x3cf:
1113 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1114 vga_index_3ce);
1115 return 0xff;
1116 case 0x3d5:
1117 FIXME("Unsupported index, register 0x3d4: 0x%02x\n",
1118 vga_index_3d4);
1119 return 0xff;
1121 case 0x3da:
1123 * Read from this register resets register 0x3c0 address flip-flop.
1125 vga_address_3c0 = TRUE;
1128 * Read from this register returns following bits:
1129 * xxxx1xxx = Vertical retrace in progress if set.
1130 * xxxxx1xx = Light pen switched on.
1131 * xxxxxx1x = Light pen trigger set.
1132 * xxxxxxx1 = Either vertical or horizontal retrace
1133 * in progress if set.
1135 ret = 0;
1136 if (vga_retrace_vertical)
1137 ret |= 9;
1138 if (vga_retrace_horizontal)
1139 ret |= 3;
1142 * If VGA mode has been set, vertical retrace is
1143 * turned on once a frame and cleared after each read.
1144 * This might cause applications that synchronize with
1145 * vertical retrace to actually skip one frame but that
1146 * is probably not a problem.
1148 * If no VGA mode has been set, vertical retrace is faked
1149 * by toggling the value after every read.
1151 if (VGA_IsTimerRunning())
1152 vga_retrace_vertical = FALSE;
1153 else
1154 vga_retrace_vertical = !vga_retrace_vertical;
1157 * Toggle horizontal retrace.
1159 vga_retrace_horizontal = !vga_retrace_horizontal;
1160 break;
1162 default:
1163 ret=0xff;
1164 FIXME("Unsupported VGA register: 0x%04x\n", port);
1166 return ret;
1169 void VGA_Clean(void)
1171 VGA_Exit();
1172 VGA_DeinstallTimer();