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
23 #define NONAMELESSUNION
24 #define NONAMELESSSTRUCT
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 * VGA controller memory is emulated using linear framebuffer.
47 * This frambuffer also acts as an interface
48 * between VGA controller emulation and DirectDraw.
50 * vga_fb_width: Display width in pixels. Can be modified when
51 * display mode is changed.
52 * vga_fb_height: Display height in pixels. Can be modified when
53 * display mode is changed.
54 * vga_fb_depth: Number of bits used to store single pixel color information.
55 * Each pixel uses (vga_fb_depth+7)/8 bytes because
56 * 1-16 color modes are mapped to 256 color mode.
57 * Can be modified when display mode is changed.
58 * vga_fb_pitch: How many bytes to add to pointer in order to move
59 * from one row to another. This is fixed in VGA modes,
60 * but can be modified in SVGA modes.
61 * vga_fb_offset: Offset added to framebuffer start address in order
62 * to find the display origin. Programs use this to do
63 * double buffering and to scroll display. The value can
64 * be modified in VGA and SVGA modes.
65 * vga_fb_size: How many bytes are allocated to framebuffer.
66 * VGA framebuffers are always larger than display size and
67 * SVGA framebuffers may also be.
68 * vga_fb_data: Pointer to framebuffer start.
69 * vga_fb_window: Offset of 64k window 0xa0000 in bytes from framebuffer start.
70 * This value is >= 0, if mode uses linear framebuffer and
71 * -1, if mode uses color planes. This value is fixed
72 * in all modes except 0x13 (256 color VGA) where
73 * 0 means normal mode and -1 means Mode-X (unchained mode).
75 static int vga_fb_width;
76 static int vga_fb_height;
77 static int vga_fb_depth;
78 static int vga_fb_pitch;
79 static int vga_fb_offset;
80 static int vga_fb_size = 0;
81 static char *vga_fb_data = 0;
82 static int vga_fb_window = 0;
87 * vga_text_attr: Current active attribute.
88 * vga_text_old: Last data sent to console.
89 * This is used to optimize console updates.
90 * vga_text_width: Width of the text display in characters.
91 * vga_text_height: Height of the text display in characters.
92 * vga_text_x: Current cursor X-position. Starts from zero.
93 * vga_text_y: Current cursor Y-position. Starts from zero.
94 * vga_text_console: TRUE if stdout is console,
95 * FALSE if it is regular file.
97 static BYTE vga_text_attr;
98 static char *vga_text_old = NULL;
99 static BYTE vga_text_width;
100 static BYTE vga_text_height;
101 static BYTE vga_text_x;
102 static BYTE vga_text_y;
103 static BOOL vga_text_console;
106 * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
107 * indexed registers. These ports are used to select VGA controller
108 * subregister that can be written to or read from using ports 0x3c1,
109 * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
110 * stored in variables vga_index_*.
112 * Port 0x3c0 is special because it is both index and
113 * data-write register. Flip-flop vga_address_3c0 tells whether
114 * the port acts currently as an address register. Reading from port
115 * 0x3da resets the flip-flop to address mode.
117 static BYTE vga_index_3c0;
118 static BYTE vga_index_3c4;
119 static BYTE vga_index_3ce;
120 static BYTE vga_index_3d4;
121 static BOOL vga_address_3c0 = TRUE;
124 * This mutex is used to protect VGA state during asynchronous
125 * screen updates (see VGA_Poll). It makes sure that VGA state changes
126 * are atomic and the user interface is protected from flicker and
129 * The mutex actually serializes VGA operations and the screen update.
130 * Which means that whenever VGA_Poll occurs, application stalls if it
131 * tries to modify VGA state. This is not how real VGA adapters work,
132 * but it makes timing and correctness issues much easier to deal with.
134 static CRITICAL_SECTION vga_lock = CRITICAL_SECTION_INIT("VGA");
136 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
137 static DirectDrawCreateProc pDirectDrawCreate;
139 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
141 static HWND vga_hwnd = NULL;
144 * For simplicity, I'm creating a second palette.
145 * 16 color accesses will use these pointers and insert
146 * entries from the 64-color palette into the default
147 * palette. --Robert 'Admiral' Coeyman
150 static char vga_16_palette[17]={
151 0x00, /* 0 - Black */
153 0x02, /* 2 - Green */
156 0x05, /* 5 - Magenta */
157 0x14, /* 6 - Brown */
158 0x07, /* 7 - Light gray */
159 0x38, /* 8 - Dark gray */
160 0x39, /* 9 - Light blue */
161 0x3a, /* A - Light green */
162 0x3b, /* B - Light cyan */
163 0x3c, /* C - Light red */
164 0x3d, /* D - Light magenta */
165 0x3e, /* E - Yellow */
166 0x3f, /* F - White */
167 0x00 /* Border Color */
170 static PALETTEENTRY vga_def_palette[256]={
172 {0x00, 0x00, 0x00}, /* 0 - Black */
173 {0x00, 0x00, 0x80}, /* 1 - Blue */
174 {0x00, 0x80, 0x00}, /* 2 - Green */
175 {0x00, 0x80, 0x80}, /* 3 - Cyan */
176 {0x80, 0x00, 0x00}, /* 4 - Red */
177 {0x80, 0x00, 0x80}, /* 5 - Magenta */
178 {0x80, 0x80, 0x00}, /* 6 - Brown */
179 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
180 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
181 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
182 {0x00, 0xFF, 0x00}, /* A - Light green */
183 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
184 {0xFF, 0x00, 0x00}, /* C - Light red */
185 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
186 {0xFF, 0xFF, 0x00}, /* E - Yellow */
187 {0xFF, 0xFF, 0xFF}, /* F - White */
188 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
192 * This palette is the dos default, converted from 18 bit color to 24.
193 * It contains only 64 entries of colors--all others are zeros.
194 * --Robert 'Admiral' Coeyman
196 static PALETTEENTRY vga_def64_palette[256]={
198 {0x00, 0x00, 0x00}, /* 0x00 Black */
199 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
200 {0x00, 0xaa, 0x00}, /* 0x02 Green */
201 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
202 {0xaa, 0x00, 0x00}, /* 0x04 Red */
203 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
204 {0xaa, 0xaa, 0x00}, /* 0x06 */
205 {0xaa, 0xaa, 0xaa}, /* 0x07 Light Gray */
206 {0x00, 0x00, 0x55}, /* 0x08 */
207 {0x00, 0x00, 0xff}, /* 0x09 */
208 {0x00, 0xaa, 0x55}, /* 0x0a */
209 {0x00, 0xaa, 0xff}, /* 0x0b */
210 {0xaa, 0x00, 0x55}, /* 0x0c */
211 {0xaa, 0x00, 0xff}, /* 0x0d */
212 {0xaa, 0xaa, 0x55}, /* 0x0e */
213 {0xaa, 0xaa, 0xff}, /* 0x0f */
214 {0x00, 0x55, 0x00}, /* 0x10 */
215 {0x00, 0x55, 0xaa}, /* 0x11 */
216 {0x00, 0xff, 0x00}, /* 0x12 */
217 {0x00, 0xff, 0xaa}, /* 0x13 */
218 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
219 {0xaa, 0x55, 0xaa}, /* 0x15 */
220 {0xaa, 0xff, 0x00}, /* 0x16 */
221 {0xaa, 0xff, 0xaa}, /* 0x17 */
222 {0x00, 0x55, 0x55}, /* 0x18 */
223 {0x00, 0x55, 0xff}, /* 0x19 */
224 {0x00, 0xff, 0x55}, /* 0x1a */
225 {0x00, 0xff, 0xff}, /* 0x1b */
226 {0xaa, 0x55, 0x55}, /* 0x1c */
227 {0xaa, 0x55, 0xff}, /* 0x1d */
228 {0xaa, 0xff, 0x55}, /* 0x1e */
229 {0xaa, 0xff, 0xff}, /* 0x1f */
230 {0x55, 0x00, 0x00}, /* 0x20 */
231 {0x55, 0x00, 0xaa}, /* 0x21 */
232 {0x55, 0xaa, 0x00}, /* 0x22 */
233 {0x55, 0xaa, 0xaa}, /* 0x23 */
234 {0xff, 0x00, 0x00}, /* 0x24 */
235 {0xff, 0x00, 0xaa}, /* 0x25 */
236 {0xff, 0xaa, 0x00}, /* 0x26 */
237 {0xff, 0xaa, 0xaa}, /* 0x27 */
238 {0x55, 0x00, 0x55}, /* 0x28 */
239 {0x55, 0x00, 0xff}, /* 0x29 */
240 {0x55, 0xaa, 0x55}, /* 0x2a */
241 {0x55, 0xaa, 0xff}, /* 0x2b */
242 {0xff, 0x00, 0x55}, /* 0x2c */
243 {0xff, 0x00, 0xff}, /* 0x2d */
244 {0xff, 0xaa, 0x55}, /* 0x2e */
245 {0xff, 0xaa, 0xff}, /* 0x2f */
246 {0x55, 0x55, 0x00}, /* 0x30 */
247 {0x55, 0x55, 0xaa}, /* 0x31 */
248 {0x55, 0xff, 0x00}, /* 0x32 */
249 {0x55, 0xff, 0xaa}, /* 0x33 */
250 {0xff, 0x55, 0x00}, /* 0x34 */
251 {0xff, 0x55, 0xaa}, /* 0x35 */
252 {0xff, 0xff, 0x00}, /* 0x36 */
253 {0xff, 0xff, 0xaa}, /* 0x37 */
254 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
255 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
256 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
257 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
258 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
259 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
260 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
261 {0xff, 0xff, 0xff}, /* 0x3f White */
262 {0,0,0} /* The next 192 entries are all zeros */
265 static HANDLE VGA_timer;
266 static HANDLE VGA_timer_thread;
268 /* set the timer rate; called in the polling thread context */
269 static void CALLBACK set_timer_rate( ULONG_PTR arg )
273 when.s.LowPart = when.s.HighPart = 0;
274 SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
277 static DWORD CALLBACK VGA_TimerThread( void *dummy )
279 for (;;) WaitForMultipleObjectsEx( 0, NULL, FALSE, INFINITE, TRUE );
282 static void VGA_DeinstallTimer(void)
284 if (VGA_timer_thread)
287 * Make sure the update thread is not holding
288 * system resources when we kill it.
290 * Now, we only need to worry about update thread
291 * getting terminated while in EnterCriticalSection
292 * or WaitForMultipleObjectsEx.
294 * FIXME: Is this a problem?
296 EnterCriticalSection(&vga_lock);
298 CancelWaitableTimer( VGA_timer );
299 CloseHandle( VGA_timer );
300 TerminateThread( VGA_timer_thread, 0 );
301 CloseHandle( VGA_timer_thread );
302 VGA_timer_thread = 0;
304 LeaveCriticalSection(&vga_lock);
307 * Synchronize display. This makes sure that
308 * changes to display become visible even if program
309 * terminates before update thread had time to run.
315 static void VGA_InstallTimer(unsigned Rate)
317 if (!VGA_timer_thread)
319 VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
320 VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
322 QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
325 static BOOL VGA_IsTimerRunning(void)
327 return VGA_timer_thread ? TRUE : FALSE;
330 HANDLE VGA_AlphaConsole(void)
332 /* this assumes that no Win32 redirection has taken place, but then again,
333 * only 16-bit apps are likely to use this part of Wine... */
334 return GetStdHandle(STD_OUTPUT_HANDLE);
337 char*VGA_AlphaBuffer(void)
339 return (char *)0xb8000;
342 /*** GRAPHICS MODE ***/
345 unsigned Xres, Yres, Depth;
349 static void WINAPI VGA_DoExit(ULONG_PTR arg)
351 VGA_DeinstallTimer();
352 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
353 IDirectDrawSurface_Release(lpddsurf);
355 IDirectDrawPalette_Release(lpddpal);
357 IDirectDraw_Release(lpddraw);
361 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
364 ModeSet *par = (ModeSet *)arg;
367 if (lpddraw) VGA_DoExit(0);
369 if (!pDirectDrawCreate)
371 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
372 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
373 if (!pDirectDrawCreate) {
374 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
378 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
380 ERR("DirectDraw is not available (res = %lx)\n",res);
384 vga_hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",WS_POPUP|WS_VISIBLE,0,0,par->Xres,par->Yres,0,0,0,NULL);
386 ERR("Failed to create user window.\n");
387 IDirectDraw_Release(lpddraw);
393 SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
395 if ((res=IDirectDraw_SetCooperativeLevel(lpddraw,vga_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) {
396 ERR("Could not set cooperative level to exclusive (%lx)\n",res);
399 if ((res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth))) {
400 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par->Xres,par->Yres,par->Depth,res);
401 IDirectDraw_Release(lpddraw);
406 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
408 ERR("Could not create palette (res = %lx)\n",res);
409 IDirectDraw_Release(lpddraw);
413 if ((res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette))) {
414 ERR("Could not set default palette entries (res = %lx)\n", res);
417 memset(&sdesc,0,sizeof(sdesc));
418 sdesc.dwSize=sizeof(sdesc);
419 sdesc.dwFlags = DDSD_CAPS;
420 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
421 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
422 ERR("DirectDraw surface is not available\n");
423 IDirectDraw_Release(lpddraw);
427 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
428 vga_retrace_vertical = vga_retrace_horizontal = FALSE;
429 /* poll every 20ms (50fps should provide adequate responsiveness) */
430 VGA_InstallTimer(20);
436 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
442 vga_fb_height = Yres;
443 vga_fb_depth = Depth;
445 vga_fb_pitch = Xres * ((Depth + 7) / 8);
447 newSize = Xres * Yres * ((Depth + 7) / 8);
448 if(newSize < 256 * 1024)
449 newSize = 256 * 1024;
451 if(vga_fb_size < newSize) {
453 HeapFree(GetProcessHeap(), 0, vga_fb_data);
454 vga_fb_data = HeapAlloc(GetProcessHeap(), 0, newSize);
455 vga_fb_size = newSize;
458 if(Xres >= 640 || Yres >= 480) {
466 VGA_SetWindowStart((Depth < 8) ? -1 : 0);
468 par.Depth = (Depth < 8) ? 8 : Depth;
470 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
474 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
476 if (!lpddraw) return 1;
477 if (!lpddsurf) return 1;
478 if (Height) *Height=sdesc.dwHeight;
479 if (Width) *Width=sdesc.dwWidth;
480 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
486 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
489 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
491 if (!lpddraw) return;
492 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
495 /* set a single [char wide] color in 16 color mode. */
496 void VGA_SetColor16(int reg,int color)
500 if (!lpddraw) return;
501 pal= &vga_def64_palette[color];
502 IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
503 vga_16_palette[reg]=(char)color;
506 /* Get a single [char wide] color in 16 color mode. */
507 char VGA_GetColor16(int reg)
510 if (!lpddraw) return 0;
511 return (char)vga_16_palette[reg];
514 /* set all 17 [char wide] colors at once in 16 color mode. */
515 void VGA_Set16Palette(char *Table)
520 if (!lpddraw) return; /* return if we're in text only mode */
521 memcpy( Table, &vga_16_palette, 17 ); /* copy the entries into the table */
523 for (c=0; c<17; c++) { /* 17 entries */
524 pal= &vga_def64_palette[(int)vga_16_palette[c]]; /* get color */
525 IDirectDrawPalette_SetEntries(lpddpal,0,c,1,pal); /* set entry */
526 TRACE("Palette register %d set to %d\n",c,(int)vga_16_palette[c]);
527 } /* end of the counting loop */
530 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
531 void VGA_Get16Palette(char *Table)
534 if (!lpddraw) return; /* return if we're in text only mode */
535 memcpy( &vga_16_palette, Table, 17 ); /* copy the entries into the table */
538 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
540 PALETTEENTRY pal[256];
543 if (!lpddraw) return;
544 for (c=0; c<len; c++) {
545 pal[c].peRed =color[c].rgbRed;
546 pal[c].peGreen=color[c].rgbGreen;
547 pal[c].peBlue =color[c].rgbBlue;
550 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
553 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
555 if (!lpddraw) return NULL;
556 if (!lpddsurf) return NULL;
557 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
558 ERR("could not lock surface!\n");
561 if (Pitch) *Pitch=sdesc.u1.lPitch;
562 if (Height) *Height=sdesc.dwHeight;
563 if (Width) *Width=sdesc.dwWidth;
564 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
565 return sdesc.lpSurface;
568 void VGA_Unlock(void)
570 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
574 * Set start of 64k window at 0xa0000 in bytes.
575 * If value is -1, initialize color plane support.
576 * If value is >= 0, window contains direct copy of framebuffer.
578 void VGA_SetWindowStart(int start)
580 if(start == vga_fb_window)
583 EnterCriticalSection(&vga_lock);
585 if(vga_fb_window == -1)
586 FIXME("Remove VGA memory emulation.\n");
588 memmove(vga_fb_data + vga_fb_window, (char *)0xa0000, 64 * 1024);
590 vga_fb_window = start;
592 if(vga_fb_window == -1)
593 FIXME("Install VGA memory emulation.\n");
595 memmove( (char *)0xa0000, vga_fb_data + vga_fb_window, 64 * 1024);
597 LeaveCriticalSection(&vga_lock);
601 * Get start of 64k window at 0xa0000 in bytes.
602 * Value is -1 in color plane modes.
604 int VGA_GetWindowStart()
606 return vga_fb_window;
611 /* prepare the text mode video memory copy that is used to only
612 * update the video memory line that did get updated. */
613 void VGA_PrepareVideoMemCopy(unsigned Xres, unsigned Yres)
619 * Allocate space for char + attr.
621 vga_text_old = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
622 vga_text_old, Xres * Yres * 2 );
624 p = VGA_AlphaBuffer();
627 /* make sure the video mem copy contains the exact opposite of our
628 * actual text mode memory area to make sure the screen
629 * does get updated fully initially */
630 for (i=0; i < Xres*Yres*2; i++)
631 *p2++ = *p++ ^ 0xff; /* XOR it */
634 /**********************************************************************
637 * Set VGA emulation to text mode.
639 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
642 VGA_DeinstallTimer();
644 VGA_PrepareVideoMemCopy(Xres, Yres);
645 vga_text_width = Xres;
646 vga_text_height = Yres;
648 if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
649 VGA_SetCursorPos(0,0);
651 if(vga_text_console) {
655 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
657 /* poll every 30ms (33fps should provide adequate responsiveness) */
658 VGA_InstallTimer(30);
662 /**********************************************************************
665 * Initialize VGA text mode handling and return default text mode.
666 * This function does not set VGA emulation to text mode.
668 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
670 CONSOLE_SCREEN_BUFFER_INFO info;
672 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
674 vga_text_console = TRUE;
675 vga_text_x = info.dwCursorPosition.X;
676 vga_text_y = info.dwCursorPosition.Y;
677 vga_text_attr = info.wAttributes;
678 *Xres = info.dwSize.X;
679 *Yres = info.dwSize.Y;
683 vga_text_console = FALSE;
686 vga_text_attr = 0x0f;
692 /**********************************************************************
695 * Get current text mode. Returns TRUE and sets resolution if
696 * any VGA text mode has been initialized.
698 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
700 if (vga_text_width != 0 && vga_text_height != 0) {
701 *Xres = vga_text_width;
702 *Yres = vga_text_height;
708 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
710 CONSOLE_CURSOR_INFO cci;
712 /* standard cursor settings:
713 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
715 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
716 cci.dwSize = ((end & 0x1f) - (start_options & 0x1f))/0x0e * 100;
717 if (!cci.dwSize) cci.dwSize++; /* NULL cursor would make SCCI() fail ! */
718 cci.bVisible = ((start_options & 0x60) != 0x20); /* invisible ? */
720 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
723 void VGA_SetCursorPos(unsigned X,unsigned Y)
729 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
731 if (X) *X = vga_text_x;
732 if (Y) *Y = vga_text_y;
735 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
737 char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
743 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
745 EnterCriticalSection(&vga_lock);
748 VGA_PutCharAt(X + count, Y, ch, attr);
750 LeaveCriticalSection(&vga_lock);
753 void VGA_PutChar(BYTE ascii)
755 EnterCriticalSection(&vga_lock);
764 vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
780 VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
785 * FIXME: add line wrapping and scrolling
789 * If we don't have a console, write directly to standard output.
791 if(!vga_text_console)
792 WriteFile(VGA_AlphaConsole(), &ascii, 1, NULL, NULL);
794 LeaveCriticalSection(&vga_lock);
797 void VGA_SetTextAttribute(BYTE attr)
799 vga_text_attr = attr;
802 void VGA_ClearText(unsigned row1, unsigned col1,
803 unsigned row2, unsigned col2,
808 EnterCriticalSection(&vga_lock);
810 for(y=row1; y<=row2; y++)
811 for(x=col1; x<=col2; x++)
812 VGA_PutCharAt(x, y, 0x20, attr);
814 LeaveCriticalSection(&vga_lock);
817 void VGA_ScrollUpText(unsigned row1, unsigned col1,
818 unsigned row2, unsigned col2,
819 unsigned lines, BYTE attr)
821 FIXME("not implemented\n");
824 void VGA_ScrollDownText(unsigned row1, unsigned col1,
825 unsigned row2, unsigned col2,
826 unsigned lines, BYTE attr)
828 FIXME("not implemented\n");
831 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
835 dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
844 /* FIXME: optimize by doing this only if the data has actually changed
845 * (in a way similar to DIBSection, perhaps) */
846 static void VGA_Poll_Graphics(void)
848 unsigned int Pitch, Height, Width, X, Y;
850 char *dat = vga_fb_data + vga_fb_offset;
851 int bpp = (vga_fb_depth + 7) / 8;
853 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
857 * Synchronize framebuffer contents.
859 if(vga_fb_window != -1)
860 memmove(vga_fb_data + vga_fb_window, (char *)0xa0000, 64 * 1024);
863 * Double VGA framebuffer (320x200 -> 640x400), if needed.
865 if(Height >= 2 * vga_fb_height && Width >= 2 * vga_fb_width && bpp == 1)
866 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch*2,dat+=vga_fb_pitch)
867 for (X=0; X<vga_fb_width; X++) {
871 surf[X*2+Pitch] = value;
872 surf[X*2+Pitch+1] = value;
875 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch,dat+=vga_fb_pitch)
876 memcpy(surf, dat, vga_fb_width * bpp);
881 static void VGA_Poll_Text(void)
883 char *dat, *old, *p_line;
885 CHAR_INFO ch[256]; /* that should suffice for the largest text width */
888 HANDLE con = VGA_AlphaConsole();
889 BOOL linechanged = FALSE; /* video memory area differs from stored copy? */
891 /* Synchronize cursor position. */
894 SetConsoleCursorPosition(con,off);
896 dat = VGA_AlphaBuffer();
897 old = vga_text_old; /* pointer to stored video mem copy */
898 siz.X = vga_text_width; siz.Y = 1;
899 off.X = 0; off.Y = 0;
901 /* copy from virtual VGA frame buffer to console */
902 for (Y=0; Y<vga_text_height; Y++) {
903 linechanged = memcmp(dat, old, vga_text_width*2);
906 /*TRACE("line %d changed\n", Y);*/
908 for (X=0; X<vga_text_width; X++) {
909 ch[X].Char.AsciiChar = *p_line++;
910 /* WriteConsoleOutputA doesn't like "dead" chars */
911 if (ch[X].Char.AsciiChar == '\0')
912 ch[X].Char.AsciiChar = ' ';
913 ch[X].Attributes = *p_line++;
915 dest.Top=Y; dest.Bottom=Y;
916 dest.Left=0; dest.Right=vga_text_width+1;
917 WriteConsoleOutputA(con, ch, siz, off, &dest);
918 memcpy(old, dat, vga_text_width*2);
920 /* advance to next text line */
921 dat += vga_text_width*2;
922 old += vga_text_width*2;
926 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
928 EnterCriticalSection(&vga_lock);
936 * Fake start of retrace.
938 vga_retrace_vertical = TRUE;
940 LeaveCriticalSection(&vga_lock);
943 static BYTE palreg,palcnt;
944 static PALETTEENTRY paldat;
946 void VGA_ioport_out( WORD port, BYTE val )
953 FIXME("Unsupported index, register 0x3c0: 0x%02x (value 0x%02x)\n",
955 vga_address_3c0 = !vga_address_3c0;
961 switch(vga_index_3c4) {
962 case 0x04: /* Sequencer: Memory Mode Register */
963 if(vga_fb_depth == 8)
964 VGA_SetWindowStart((val & 8) ? 0 : -1);
966 FIXME("Memory Mode Register not supported in this mode.\n");
969 FIXME("Unsupported index, register 0x3c4: 0x%02x (value 0x%02x)\n",
974 palreg=val; palcnt=0; break;
976 ((BYTE*)&paldat)[palcnt++]=val << 2;
978 VGA_SetPalette(&paldat,palreg++,1);
986 FIXME("Unsupported index, register 0x3ce: 0x%02x (value 0x%02x)\n",
993 FIXME("Unsupported index, register 0x3d4: 0x%02x (value 0x%02x)\n",
997 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1001 BYTE VGA_ioport_in( WORD port )
1007 FIXME("Unsupported index, register 0x3c0: 0x%02x\n",
1011 switch(vga_index_3c4) {
1012 case 0x04: /* Sequencer: Memory Mode Register */
1013 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1015 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1020 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1024 FIXME("Unsupported index, register 0x3d4: 0x%02x\n",
1030 * Read from this register resets register 0x3c0 address flip-flop.
1032 vga_address_3c0 = TRUE;
1035 * Read from this register returns following bits:
1036 * xxxx1xxx = Vertical retrace in progress if set.
1037 * xxxxx1xx = Light pen switched on.
1038 * xxxxxx1x = Light pen trigger set.
1039 * xxxxxxx1 = Either vertical or horizontal retrace
1040 * in progress if set.
1043 if (vga_retrace_vertical)
1045 if (vga_retrace_horizontal)
1049 * If VGA mode has been set, vertical retrace is
1050 * turned on once a frame and cleared after each read.
1051 * This might cause applications that synchronize with
1052 * vertical retrace to actually skip one frame but that
1053 * is probably not a problem.
1055 * If no VGA mode has been set, vertical retrace is faked
1056 * by toggling the value after every read.
1058 if (VGA_IsTimerRunning())
1059 vga_retrace_vertical = FALSE;
1061 vga_retrace_vertical = !vga_retrace_vertical;
1064 * Toggle horizontal retrace.
1066 vga_retrace_horizontal = !vga_retrace_horizontal;
1071 FIXME("Unsupported VGA register: 0x%04x\n", port);
1076 void VGA_Clean(void)
1079 VGA_DeinstallTimer();