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;
135 static CRITICAL_SECTION_DEBUG critsect_debug =
138 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
139 0, 0, { 0, (DWORD)(__FILE__ ": vga_lock") }
141 static CRITICAL_SECTION vga_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
143 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
144 static DirectDrawCreateProc pDirectDrawCreate;
146 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
148 static HWND vga_hwnd = NULL;
151 * For simplicity, I'm creating a second palette.
152 * 16 color accesses will use these pointers and insert
153 * entries from the 64-color palette into the default
154 * palette. --Robert 'Admiral' Coeyman
157 static char vga_16_palette[17]={
158 0x00, /* 0 - Black */
160 0x02, /* 2 - Green */
163 0x05, /* 5 - Magenta */
164 0x14, /* 6 - Brown */
165 0x07, /* 7 - Light gray */
166 0x38, /* 8 - Dark gray */
167 0x39, /* 9 - Light blue */
168 0x3a, /* A - Light green */
169 0x3b, /* B - Light cyan */
170 0x3c, /* C - Light red */
171 0x3d, /* D - Light magenta */
172 0x3e, /* E - Yellow */
173 0x3f, /* F - White */
174 0x00 /* Border Color */
177 static PALETTEENTRY vga_def_palette[256]={
179 {0x00, 0x00, 0x00}, /* 0 - Black */
180 {0x00, 0x00, 0x80}, /* 1 - Blue */
181 {0x00, 0x80, 0x00}, /* 2 - Green */
182 {0x00, 0x80, 0x80}, /* 3 - Cyan */
183 {0x80, 0x00, 0x00}, /* 4 - Red */
184 {0x80, 0x00, 0x80}, /* 5 - Magenta */
185 {0x80, 0x80, 0x00}, /* 6 - Brown */
186 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
187 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
188 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
189 {0x00, 0xFF, 0x00}, /* A - Light green */
190 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
191 {0xFF, 0x00, 0x00}, /* C - Light red */
192 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
193 {0xFF, 0xFF, 0x00}, /* E - Yellow */
194 {0xFF, 0xFF, 0xFF}, /* F - White */
195 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
199 * This palette is the dos default, converted from 18 bit color to 24.
200 * It contains only 64 entries of colors--all others are zeros.
201 * --Robert 'Admiral' Coeyman
203 static PALETTEENTRY vga_def64_palette[256]={
205 {0x00, 0x00, 0x00}, /* 0x00 Black */
206 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
207 {0x00, 0xaa, 0x00}, /* 0x02 Green */
208 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
209 {0xaa, 0x00, 0x00}, /* 0x04 Red */
210 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
211 {0xaa, 0xaa, 0x00}, /* 0x06 */
212 {0xaa, 0xaa, 0xaa}, /* 0x07 Light Gray */
213 {0x00, 0x00, 0x55}, /* 0x08 */
214 {0x00, 0x00, 0xff}, /* 0x09 */
215 {0x00, 0xaa, 0x55}, /* 0x0a */
216 {0x00, 0xaa, 0xff}, /* 0x0b */
217 {0xaa, 0x00, 0x55}, /* 0x0c */
218 {0xaa, 0x00, 0xff}, /* 0x0d */
219 {0xaa, 0xaa, 0x55}, /* 0x0e */
220 {0xaa, 0xaa, 0xff}, /* 0x0f */
221 {0x00, 0x55, 0x00}, /* 0x10 */
222 {0x00, 0x55, 0xaa}, /* 0x11 */
223 {0x00, 0xff, 0x00}, /* 0x12 */
224 {0x00, 0xff, 0xaa}, /* 0x13 */
225 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
226 {0xaa, 0x55, 0xaa}, /* 0x15 */
227 {0xaa, 0xff, 0x00}, /* 0x16 */
228 {0xaa, 0xff, 0xaa}, /* 0x17 */
229 {0x00, 0x55, 0x55}, /* 0x18 */
230 {0x00, 0x55, 0xff}, /* 0x19 */
231 {0x00, 0xff, 0x55}, /* 0x1a */
232 {0x00, 0xff, 0xff}, /* 0x1b */
233 {0xaa, 0x55, 0x55}, /* 0x1c */
234 {0xaa, 0x55, 0xff}, /* 0x1d */
235 {0xaa, 0xff, 0x55}, /* 0x1e */
236 {0xaa, 0xff, 0xff}, /* 0x1f */
237 {0x55, 0x00, 0x00}, /* 0x20 */
238 {0x55, 0x00, 0xaa}, /* 0x21 */
239 {0x55, 0xaa, 0x00}, /* 0x22 */
240 {0x55, 0xaa, 0xaa}, /* 0x23 */
241 {0xff, 0x00, 0x00}, /* 0x24 */
242 {0xff, 0x00, 0xaa}, /* 0x25 */
243 {0xff, 0xaa, 0x00}, /* 0x26 */
244 {0xff, 0xaa, 0xaa}, /* 0x27 */
245 {0x55, 0x00, 0x55}, /* 0x28 */
246 {0x55, 0x00, 0xff}, /* 0x29 */
247 {0x55, 0xaa, 0x55}, /* 0x2a */
248 {0x55, 0xaa, 0xff}, /* 0x2b */
249 {0xff, 0x00, 0x55}, /* 0x2c */
250 {0xff, 0x00, 0xff}, /* 0x2d */
251 {0xff, 0xaa, 0x55}, /* 0x2e */
252 {0xff, 0xaa, 0xff}, /* 0x2f */
253 {0x55, 0x55, 0x00}, /* 0x30 */
254 {0x55, 0x55, 0xaa}, /* 0x31 */
255 {0x55, 0xff, 0x00}, /* 0x32 */
256 {0x55, 0xff, 0xaa}, /* 0x33 */
257 {0xff, 0x55, 0x00}, /* 0x34 */
258 {0xff, 0x55, 0xaa}, /* 0x35 */
259 {0xff, 0xff, 0x00}, /* 0x36 */
260 {0xff, 0xff, 0xaa}, /* 0x37 */
261 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
262 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
263 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
264 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
265 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
266 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
267 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
268 {0xff, 0xff, 0xff}, /* 0x3f White */
269 {0,0,0} /* The next 192 entries are all zeros */
272 static HANDLE VGA_timer;
273 static HANDLE VGA_timer_thread;
275 /* set the timer rate; called in the polling thread context */
276 static void CALLBACK set_timer_rate( ULONG_PTR arg )
280 when.s.LowPart = when.s.HighPart = 0;
281 SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
284 static DWORD CALLBACK VGA_TimerThread( void *dummy )
286 for (;;) SleepEx( INFINITE, TRUE );
289 static void VGA_DeinstallTimer(void)
291 if (VGA_timer_thread)
294 * Make sure the update thread is not holding
295 * system resources when we kill it.
297 * Now, we only need to worry about update thread
298 * getting terminated while in EnterCriticalSection
299 * or WaitForMultipleObjectsEx.
301 * FIXME: Is this a problem?
303 EnterCriticalSection(&vga_lock);
305 CancelWaitableTimer( VGA_timer );
306 CloseHandle( VGA_timer );
307 TerminateThread( VGA_timer_thread, 0 );
308 CloseHandle( VGA_timer_thread );
309 VGA_timer_thread = 0;
311 LeaveCriticalSection(&vga_lock);
314 * Synchronize display. This makes sure that
315 * changes to display become visible even if program
316 * terminates before update thread had time to run.
322 static void VGA_InstallTimer(unsigned Rate)
324 if (!VGA_timer_thread)
326 VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
327 VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
329 QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
332 static BOOL VGA_IsTimerRunning(void)
334 return VGA_timer_thread ? TRUE : FALSE;
337 HANDLE VGA_AlphaConsole(void)
339 /* this assumes that no Win32 redirection has taken place, but then again,
340 * only 16-bit apps are likely to use this part of Wine... */
341 return GetStdHandle(STD_OUTPUT_HANDLE);
344 char*VGA_AlphaBuffer(void)
346 return (char *)0xb8000;
349 /*** GRAPHICS MODE ***/
352 unsigned Xres, Yres, Depth;
356 static void WINAPI VGA_DoExit(ULONG_PTR arg)
358 VGA_DeinstallTimer();
359 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
360 IDirectDrawSurface_Release(lpddsurf);
362 IDirectDrawPalette_Release(lpddpal);
364 IDirectDraw_Release(lpddraw);
368 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
371 ModeSet *par = (ModeSet *)arg;
374 if (lpddraw) VGA_DoExit(0);
376 if (!pDirectDrawCreate)
378 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
379 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
380 if (!pDirectDrawCreate) {
381 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
385 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
387 ERR("DirectDraw is not available (res = %lx)\n",res);
391 vga_hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",
392 WS_POPUP|WS_VISIBLE|SS_NOTIFY,0,0,
393 par->Xres,par->Yres,0,0,0,NULL);
395 ERR("Failed to create user window.\n");
396 IDirectDraw_Release(lpddraw);
402 SetWindowPos(vga_hwnd,0,0,0,par->Xres,par->Yres,SWP_NOMOVE|SWP_NOZORDER);
404 if ((res=IDirectDraw_SetCooperativeLevel(lpddraw,vga_hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) {
405 ERR("Could not set cooperative level to exclusive (%lx)\n",res);
408 if ((res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth))) {
409 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par->Xres,par->Yres,par->Depth,res);
410 IDirectDraw_Release(lpddraw);
415 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
417 ERR("Could not create palette (res = %lx)\n",res);
418 IDirectDraw_Release(lpddraw);
422 if ((res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette))) {
423 ERR("Could not set default palette entries (res = %lx)\n", res);
426 memset(&sdesc,0,sizeof(sdesc));
427 sdesc.dwSize=sizeof(sdesc);
428 sdesc.dwFlags = DDSD_CAPS;
429 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
430 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
431 ERR("DirectDraw surface is not available\n");
432 IDirectDraw_Release(lpddraw);
436 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
437 vga_retrace_vertical = vga_retrace_horizontal = FALSE;
438 /* poll every 20ms (50fps should provide adequate responsiveness) */
439 VGA_InstallTimer(20);
445 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
451 vga_fb_height = Yres;
452 vga_fb_depth = Depth;
454 vga_fb_pitch = Xres * ((Depth + 7) / 8);
456 newSize = Xres * Yres * ((Depth + 7) / 8);
457 if(newSize < 256 * 1024)
458 newSize = 256 * 1024;
460 if(vga_fb_size < newSize) {
462 HeapFree(GetProcessHeap(), 0, vga_fb_data);
463 vga_fb_data = HeapAlloc(GetProcessHeap(), 0, newSize);
464 vga_fb_size = newSize;
467 if(Xres >= 640 || Yres >= 480) {
475 VGA_SetWindowStart((Depth < 8) ? -1 : 0);
477 par.Depth = (Depth < 8) ? 8 : Depth;
479 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
483 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
485 if (!lpddraw) return 1;
486 if (!lpddsurf) return 1;
487 if (Height) *Height=sdesc.dwHeight;
488 if (Width) *Width=sdesc.dwWidth;
489 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
495 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
498 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
500 if (!lpddraw) return;
501 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
504 /* set a single [char wide] color in 16 color mode. */
505 void VGA_SetColor16(int reg,int color)
509 if (!lpddraw) return;
510 pal= &vga_def64_palette[color];
511 IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
512 vga_16_palette[reg]=(char)color;
515 /* Get a single [char wide] color in 16 color mode. */
516 char VGA_GetColor16(int reg)
519 if (!lpddraw) return 0;
520 return (char)vga_16_palette[reg];
523 /* set all 17 [char wide] colors at once in 16 color mode. */
524 void VGA_Set16Palette(char *Table)
529 if (!lpddraw) return; /* return if we're in text only mode */
530 memcpy( Table, &vga_16_palette, 17 ); /* copy the entries into the table */
532 for (c=0; c<17; c++) { /* 17 entries */
533 pal= &vga_def64_palette[(int)vga_16_palette[c]]; /* get color */
534 IDirectDrawPalette_SetEntries(lpddpal,0,c,1,pal); /* set entry */
535 TRACE("Palette register %d set to %d\n",c,(int)vga_16_palette[c]);
536 } /* end of the counting loop */
539 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
540 void VGA_Get16Palette(char *Table)
543 if (!lpddraw) return; /* return if we're in text only mode */
544 memcpy( &vga_16_palette, Table, 17 ); /* copy the entries into the table */
547 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
549 PALETTEENTRY pal[256];
552 if (!lpddraw) return;
553 for (c=0; c<len; c++) {
554 pal[c].peRed =color[c].rgbRed;
555 pal[c].peGreen=color[c].rgbGreen;
556 pal[c].peBlue =color[c].rgbBlue;
559 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
562 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
564 if (!lpddraw) return NULL;
565 if (!lpddsurf) return NULL;
566 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
567 ERR("could not lock surface!\n");
570 if (Pitch) *Pitch=sdesc.u1.lPitch;
571 if (Height) *Height=sdesc.dwHeight;
572 if (Width) *Width=sdesc.dwWidth;
573 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
574 return sdesc.lpSurface;
577 void VGA_Unlock(void)
579 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
583 * Set start of 64k window at 0xa0000 in bytes.
584 * If value is -1, initialize color plane support.
585 * If value is >= 0, window contains direct copy of framebuffer.
587 void VGA_SetWindowStart(int start)
589 if(start == vga_fb_window)
592 EnterCriticalSection(&vga_lock);
594 if(vga_fb_window == -1)
595 FIXME("Remove VGA memory emulation.\n");
597 memmove(vga_fb_data + vga_fb_window, (char *)0xa0000, 64 * 1024);
599 vga_fb_window = start;
601 if(vga_fb_window == -1)
602 FIXME("Install VGA memory emulation.\n");
604 memmove( (char *)0xa0000, vga_fb_data + vga_fb_window, 64 * 1024);
606 LeaveCriticalSection(&vga_lock);
610 * Get start of 64k window at 0xa0000 in bytes.
611 * Value is -1 in color plane modes.
613 int VGA_GetWindowStart()
615 return vga_fb_window;
620 /* prepare the text mode video memory copy that is used to only
621 * update the video memory line that did get updated. */
622 void VGA_PrepareVideoMemCopy(unsigned Xres, unsigned Yres)
628 * Allocate space for char + attr.
630 vga_text_old = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
631 vga_text_old, Xres * Yres * 2 );
633 p = VGA_AlphaBuffer();
636 /* make sure the video mem copy contains the exact opposite of our
637 * actual text mode memory area to make sure the screen
638 * does get updated fully initially */
639 for (i=0; i < Xres*Yres*2; i++)
640 *p2++ = *p++ ^ 0xff; /* XOR it */
643 /**********************************************************************
646 * Set VGA emulation to text mode.
648 void VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
651 VGA_DeinstallTimer();
653 VGA_PrepareVideoMemCopy(Xres, Yres);
654 vga_text_width = Xres;
655 vga_text_height = Yres;
657 if (vga_text_x >= vga_text_width || vga_text_y >= vga_text_height)
658 VGA_SetCursorPos(0,0);
660 if(vga_text_console) {
664 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size );
666 /* poll every 30ms (33fps should provide adequate responsiveness) */
667 VGA_InstallTimer(30);
671 /**********************************************************************
674 * Initialize VGA text mode handling and return default text mode.
675 * This function does not set VGA emulation to text mode.
677 void VGA_InitAlphaMode(unsigned*Xres,unsigned*Yres)
679 CONSOLE_SCREEN_BUFFER_INFO info;
681 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info ))
683 vga_text_console = TRUE;
684 vga_text_x = info.dwCursorPosition.X;
685 vga_text_y = info.dwCursorPosition.Y;
686 vga_text_attr = info.wAttributes;
687 *Xres = info.dwSize.X;
688 *Yres = info.dwSize.Y;
692 vga_text_console = FALSE;
695 vga_text_attr = 0x0f;
701 /**********************************************************************
704 * Get current text mode. Returns TRUE and sets resolution if
705 * any VGA text mode has been initialized.
707 BOOL VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
709 if (vga_text_width != 0 && vga_text_height != 0) {
710 *Xres = vga_text_width;
711 *Yres = vga_text_height;
717 void VGA_SetCursorShape(unsigned char start_options, unsigned char end)
719 CONSOLE_CURSOR_INFO cci;
721 /* standard cursor settings:
722 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
724 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
725 cci.dwSize = ((end & 0x1f) - (start_options & 0x1f))/0x0e * 100;
726 if (!cci.dwSize) cci.dwSize++; /* NULL cursor would make SCCI() fail ! */
727 cci.bVisible = ((start_options & 0x60) != 0x20); /* invisible ? */
729 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci);
732 void VGA_SetCursorPos(unsigned X,unsigned Y)
738 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
740 if (X) *X = vga_text_x;
741 if (Y) *Y = vga_text_y;
744 static void VGA_PutCharAt(unsigned x, unsigned y, BYTE ascii, int attr)
746 char *dat = VGA_AlphaBuffer() + ((vga_text_width * y + x) * 2);
752 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
754 EnterCriticalSection(&vga_lock);
757 VGA_PutCharAt(X + count, Y, ch, attr);
759 LeaveCriticalSection(&vga_lock);
762 void VGA_PutChar(BYTE ascii)
764 EnterCriticalSection(&vga_lock);
773 vga_text_x += ((vga_text_x + 8) & ~7) - vga_text_x;
789 VGA_PutCharAt(vga_text_x, vga_text_y, ascii, vga_text_attr);
793 if (vga_text_x >= vga_text_width)
799 if (vga_text_y >= vga_text_height)
801 vga_text_y = vga_text_height - 1;
802 VGA_ScrollUpText( 0, 0,
803 vga_text_height - 1, vga_text_width - 1,
808 * If we don't have a console, write directly to standard output.
810 if(!vga_text_console)
811 WriteFile(VGA_AlphaConsole(), &ascii, 1, NULL, NULL);
813 LeaveCriticalSection(&vga_lock);
816 void VGA_SetTextAttribute(BYTE attr)
818 vga_text_attr = attr;
821 void VGA_ClearText(unsigned row1, unsigned col1,
822 unsigned row2, unsigned col2,
827 EnterCriticalSection(&vga_lock);
829 for(y=row1; y<=row2; y++)
830 for(x=col1; x<=col2; x++)
831 VGA_PutCharAt(x, y, 0x20, attr);
833 LeaveCriticalSection(&vga_lock);
836 void VGA_ScrollUpText(unsigned row1, unsigned col1,
837 unsigned row2, unsigned col2,
838 unsigned lines, BYTE attr)
840 char *buffer = VGA_AlphaBuffer();
843 EnterCriticalSection(&vga_lock);
848 for (y = row1; y <= row2 - lines; y++)
849 memmove( buffer + col1 + y * vga_text_width * 2,
850 buffer + col1 + (y + lines) * vga_text_width * 2,
851 (col2 - col1 + 1) * 2 );
854 * Fill exposed lines.
856 for (y = max(row1, row2 - lines + 1); y <= row2; y++)
857 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
859 LeaveCriticalSection(&vga_lock);
862 void VGA_ScrollDownText(unsigned row1, unsigned col1,
863 unsigned row2, unsigned col2,
864 unsigned lines, BYTE attr)
866 char *buffer = VGA_AlphaBuffer();
869 EnterCriticalSection(&vga_lock);
874 for (y = row2; y >= row1 + lines; y--)
875 memmove( buffer + col1 + y * vga_text_width * 2,
876 buffer + col1 + (y - lines) * vga_text_width * 2,
877 (col2 - col1 + 1) * 2 );
880 * Fill exposed lines.
882 for (y = row1; y <= min(row1 + lines - 1, row2); y++)
883 VGA_WriteChars( col1, y, ' ', attr, col2 - col1 + 1 );
885 LeaveCriticalSection(&vga_lock);
888 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
892 dat = VGA_AlphaBuffer() + ((vga_text_width * vga_text_y + vga_text_x) * 2);
901 /* FIXME: optimize by doing this only if the data has actually changed
902 * (in a way similar to DIBSection, perhaps) */
903 static void VGA_Poll_Graphics(void)
905 unsigned int Pitch, Height, Width, X, Y;
907 char *dat = vga_fb_data + vga_fb_offset;
908 int bpp = (vga_fb_depth + 7) / 8;
910 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
914 * Synchronize framebuffer contents.
916 if(vga_fb_window != -1)
917 memmove(vga_fb_data + vga_fb_window, (char *)0xa0000, 64 * 1024);
920 * Double VGA framebuffer (320x200 -> 640x400), if needed.
922 if(Height >= 2 * vga_fb_height && Width >= 2 * vga_fb_width && bpp == 1)
923 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch*2,dat+=vga_fb_pitch)
924 for (X=0; X<vga_fb_width; X++) {
928 surf[X*2+Pitch] = value;
929 surf[X*2+Pitch+1] = value;
932 for (Y=0; Y<vga_fb_height; Y++,surf+=Pitch,dat+=vga_fb_pitch)
933 memcpy(surf, dat, vga_fb_width * bpp);
938 static void VGA_Poll_Text(void)
940 char *dat, *old, *p_line;
942 CHAR_INFO ch[256]; /* that should suffice for the largest text width */
945 HANDLE con = VGA_AlphaConsole();
946 BOOL linechanged = FALSE; /* video memory area differs from stored copy? */
948 /* Synchronize cursor position. */
951 SetConsoleCursorPosition(con,off);
953 dat = VGA_AlphaBuffer();
954 old = vga_text_old; /* pointer to stored video mem copy */
955 siz.X = vga_text_width; siz.Y = 1;
956 off.X = 0; off.Y = 0;
958 /* copy from virtual VGA frame buffer to console */
959 for (Y=0; Y<vga_text_height; Y++) {
960 linechanged = memcmp(dat, old, vga_text_width*2);
963 /*TRACE("line %d changed\n", Y);*/
965 for (X=0; X<vga_text_width; X++) {
966 ch[X].Char.AsciiChar = *p_line++;
967 /* WriteConsoleOutputA doesn't like "dead" chars */
968 if (ch[X].Char.AsciiChar == '\0')
969 ch[X].Char.AsciiChar = ' ';
970 ch[X].Attributes = *p_line++;
972 dest.Top=Y; dest.Bottom=Y;
973 dest.Left=0; dest.Right=vga_text_width+1;
974 WriteConsoleOutputA(con, ch, siz, off, &dest);
975 memcpy(old, dat, vga_text_width*2);
977 /* advance to next text line */
978 dat += vga_text_width*2;
979 old += vga_text_width*2;
983 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
985 EnterCriticalSection(&vga_lock);
993 * Fake start of retrace.
995 vga_retrace_vertical = TRUE;
997 LeaveCriticalSection(&vga_lock);
1000 static BYTE palreg,palcnt;
1001 static PALETTEENTRY paldat;
1003 void VGA_ioport_out( WORD port, BYTE val )
1007 if (vga_address_3c0)
1008 vga_index_3c0 = val;
1010 FIXME("Unsupported index, register 0x3c0: 0x%02x (value 0x%02x)\n",
1011 vga_index_3c0, val);
1012 vga_address_3c0 = !vga_address_3c0;
1015 vga_index_3c4 = val;
1018 switch(vga_index_3c4) {
1019 case 0x04: /* Sequencer: Memory Mode Register */
1020 if(vga_fb_depth == 8)
1021 VGA_SetWindowStart((val & 8) ? 0 : -1);
1023 FIXME("Memory Mode Register not supported in this mode.\n");
1026 FIXME("Unsupported index, register 0x3c4: 0x%02x (value 0x%02x)\n",
1027 vga_index_3c4, val);
1031 palreg=val; palcnt=0; break;
1033 ((BYTE*)&paldat)[palcnt++]=val << 2;
1035 VGA_SetPalette(&paldat,palreg++,1);
1040 vga_index_3ce = val;
1043 FIXME("Unsupported index, register 0x3ce: 0x%02x (value 0x%02x)\n",
1044 vga_index_3ce, val);
1047 vga_index_3d4 = val;
1050 FIXME("Unsupported index, register 0x3d4: 0x%02x (value 0x%02x)\n",
1051 vga_index_3d4, val);
1054 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port, val);
1058 BYTE VGA_ioport_in( WORD port )
1064 FIXME("Unsupported index, register 0x3c0: 0x%02x\n",
1068 switch(vga_index_3c4) {
1069 case 0x04: /* Sequencer: Memory Mode Register */
1070 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1072 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1077 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1081 FIXME("Unsupported index, register 0x3d4: 0x%02x\n",
1087 * Read from this register resets register 0x3c0 address flip-flop.
1089 vga_address_3c0 = TRUE;
1092 * Read from this register returns following bits:
1093 * xxxx1xxx = Vertical retrace in progress if set.
1094 * xxxxx1xx = Light pen switched on.
1095 * xxxxxx1x = Light pen trigger set.
1096 * xxxxxxx1 = Either vertical or horizontal retrace
1097 * in progress if set.
1100 if (vga_retrace_vertical)
1102 if (vga_retrace_horizontal)
1106 * If VGA mode has been set, vertical retrace is
1107 * turned on once a frame and cleared after each read.
1108 * This might cause applications that synchronize with
1109 * vertical retrace to actually skip one frame but that
1110 * is probably not a problem.
1112 * If no VGA mode has been set, vertical retrace is faked
1113 * by toggling the value after every read.
1115 if (VGA_IsTimerRunning())
1116 vga_retrace_vertical = FALSE;
1118 vga_retrace_vertical = !vga_retrace_vertical;
1121 * Toggle horizontal retrace.
1123 vga_retrace_horizontal = !vga_retrace_horizontal;
1128 FIXME("Unsupported VGA register: 0x%04x\n", port);
1133 void VGA_Clean(void)
1136 VGA_DeinstallTimer();