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
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
34 static IDirectDraw *lpddraw = NULL;
35 static IDirectDrawSurface *lpddsurf;
36 static IDirectDrawPalette *lpddpal;
37 static DDSURFACEDESC sdesc;
38 static LONG vga_refresh;
39 static HANDLE poll_timer;
42 static int vga_height;
44 static BYTE vga_text_attr;
46 static BOOL vga_mode_initialized = FALSE;
48 static CRITICAL_SECTION vga_lock = CRITICAL_SECTION_INIT("VGA");
50 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
51 static DirectDrawCreateProc pDirectDrawCreate;
53 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high );
56 * For simplicity, I'm creating a second palette.
57 * 16 color accesses will use these pointers and insert
58 * entries from the 64-color palette into the default
59 * palette. --Robert 'Admiral' Coeyman
62 static char vga_16_palette[17]={
68 0x05, /* 5 - Magenta */
70 0x07, /* 7 - Light gray */
71 0x38, /* 8 - Dark gray */
72 0x39, /* 9 - Light blue */
73 0x3a, /* A - Light green */
74 0x3b, /* B - Light cyan */
75 0x3c, /* C - Light red */
76 0x3d, /* D - Light magenta */
77 0x3e, /* E - Yellow */
79 0x00 /* Border Color */
82 static PALETTEENTRY vga_def_palette[256]={
84 {0x00, 0x00, 0x00}, /* 0 - Black */
85 {0x00, 0x00, 0x80}, /* 1 - Blue */
86 {0x00, 0x80, 0x00}, /* 2 - Green */
87 {0x00, 0x80, 0x80}, /* 3 - Cyan */
88 {0x80, 0x00, 0x00}, /* 4 - Red */
89 {0x80, 0x00, 0x80}, /* 5 - Magenta */
90 {0x80, 0x80, 0x00}, /* 6 - Brown */
91 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
92 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
93 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
94 {0x00, 0xFF, 0x00}, /* A - Light green */
95 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
96 {0xFF, 0x00, 0x00}, /* C - Light red */
97 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
98 {0xFF, 0xFF, 0x00}, /* E - Yellow */
99 {0xFF, 0xFF, 0xFF}, /* F - White */
100 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
104 * This palette is the dos default, converted from 18 bit color to 24.
105 * It contains only 64 entries of colors--all others are zeros.
106 * --Robert 'Admiral' Coeyman
108 static PALETTEENTRY vga_def64_palette[256]={
110 {0x00, 0x00, 0x00}, /* 0x00 Black */
111 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
112 {0x00, 0xaa, 0x00}, /* 0x02 Green */
113 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
114 {0xaa, 0x00, 0x00}, /* 0x04 Red */
115 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
116 {0xaa, 0xaa, 0x00}, /* 0x06 */
117 {0xaa, 0xaa, 0xaa}, /* 0x07 Light Gray */
118 {0x00, 0x00, 0x55}, /* 0x08 */
119 {0x00, 0x00, 0xff}, /* 0x09 */
120 {0x00, 0xaa, 0x55}, /* 0x0a */
121 {0x00, 0xaa, 0xff}, /* 0x0b */
122 {0xaa, 0x00, 0x55}, /* 0x0c */
123 {0xaa, 0x00, 0xff}, /* 0x0d */
124 {0xaa, 0xaa, 0x55}, /* 0x0e */
125 {0xaa, 0xaa, 0xff}, /* 0x0f */
126 {0x00, 0x55, 0x00}, /* 0x10 */
127 {0x00, 0x55, 0xaa}, /* 0x11 */
128 {0x00, 0xff, 0x00}, /* 0x12 */
129 {0x00, 0xff, 0xaa}, /* 0x13 */
130 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
131 {0xaa, 0x55, 0xaa}, /* 0x15 */
132 {0xaa, 0xff, 0x00}, /* 0x16 */
133 {0xaa, 0xff, 0xaa}, /* 0x17 */
134 {0x00, 0x55, 0x55}, /* 0x18 */
135 {0x00, 0x55, 0xff}, /* 0x19 */
136 {0x00, 0xff, 0x55}, /* 0x1a */
137 {0x00, 0xff, 0xff}, /* 0x1b */
138 {0xaa, 0x55, 0x55}, /* 0x1c */
139 {0xaa, 0x55, 0xff}, /* 0x1d */
140 {0xaa, 0xff, 0x55}, /* 0x1e */
141 {0xaa, 0xff, 0xff}, /* 0x1f */
142 {0x55, 0x00, 0x00}, /* 0x20 */
143 {0x55, 0x00, 0xaa}, /* 0x21 */
144 {0x55, 0xaa, 0x00}, /* 0x22 */
145 {0x55, 0xaa, 0xaa}, /* 0x23 */
146 {0xff, 0x00, 0x00}, /* 0x24 */
147 {0xff, 0x00, 0xaa}, /* 0x25 */
148 {0xff, 0xaa, 0x00}, /* 0x26 */
149 {0xff, 0xaa, 0xaa}, /* 0x27 */
150 {0x55, 0x00, 0x55}, /* 0x28 */
151 {0x55, 0x00, 0xff}, /* 0x29 */
152 {0x55, 0xaa, 0x55}, /* 0x2a */
153 {0x55, 0xaa, 0xff}, /* 0x2b */
154 {0xff, 0x00, 0x55}, /* 0x2c */
155 {0xff, 0x00, 0xff}, /* 0x2d */
156 {0xff, 0xaa, 0x55}, /* 0x2e */
157 {0xff, 0xaa, 0xff}, /* 0x2f */
158 {0x55, 0x55, 0x00}, /* 0x30 */
159 {0x55, 0x55, 0xaa}, /* 0x31 */
160 {0x55, 0xff, 0x00}, /* 0x32 */
161 {0x55, 0xff, 0xaa}, /* 0x33 */
162 {0xff, 0x55, 0x00}, /* 0x34 */
163 {0xff, 0x55, 0xaa}, /* 0x35 */
164 {0xff, 0xff, 0x00}, /* 0x36 */
165 {0xff, 0xff, 0xaa}, /* 0x37 */
166 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
167 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
168 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
169 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
170 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
171 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
172 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
173 {0xff, 0xff, 0xff}, /* 0x3f White */
174 {0,0,0} /* The next 192 entries are all zeros */
177 static HANDLE VGA_timer;
178 static HANDLE VGA_timer_thread;
180 /* set the timer rate; called in the polling thread context */
181 static void CALLBACK set_timer_rate( ULONG_PTR arg )
185 when.s.LowPart = when.s.HighPart = 0;
186 SetWaitableTimer( VGA_timer, &when, arg, VGA_Poll, 0, FALSE );
189 static DWORD CALLBACK VGA_TimerThread( void *dummy )
191 for (;;) WaitForMultipleObjectsEx( 0, NULL, FALSE, INFINITE, TRUE );
194 static void VGA_DeinstallTimer(void)
196 if (VGA_timer_thread)
198 CancelWaitableTimer( VGA_timer );
199 CloseHandle( VGA_timer );
200 TerminateThread( VGA_timer_thread, 0 );
201 CloseHandle( VGA_timer_thread );
202 VGA_timer_thread = 0;
206 static void VGA_InstallTimer(unsigned Rate)
208 if (!VGA_timer_thread)
210 VGA_timer = CreateWaitableTimerA( NULL, FALSE, NULL );
211 VGA_timer_thread = CreateThread( NULL, 0, VGA_TimerThread, NULL, 0, NULL );
213 QueueUserAPC( set_timer_rate, VGA_timer_thread, (ULONG_PTR)Rate );
216 HANDLE VGA_AlphaConsole(void)
218 /* this assumes that no Win32 redirection has taken place, but then again,
219 * only 16-bit apps are likely to use this part of Wine... */
220 return GetStdHandle(STD_OUTPUT_HANDLE);
223 char*VGA_AlphaBuffer(void)
225 return DOSMEM_MapDosToLinear(0xb8000);
228 /*** GRAPHICS MODE ***/
231 unsigned Xres, Yres, Depth;
235 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
239 ModeSet *par = (ModeSet *)arg;
242 if (lpddraw) VGA_Exit();
244 if (!pDirectDrawCreate)
246 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
247 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
248 if (!pDirectDrawCreate) {
249 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
253 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
255 ERR("DirectDraw is not available (res = %lx)\n",res);
258 hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",WS_POPUP|WS_BORDER|WS_CAPTION|WS_SYSMENU,0,0,par->Xres,par->Yres,0,0,0,NULL);
260 ERR("Failed to create user window.\n");
262 if ((res=IDirectDraw_SetCooperativeLevel(lpddraw,hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) {
263 ERR("Could not set cooperative level to exclusive (%lx)\n",res);
266 if ((res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth))) {
267 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par->Xres,par->Yres,par->Depth,res);
268 IDirectDraw_Release(lpddraw);
273 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
275 ERR("Could not create palette (res = %lx)\n",res);
276 IDirectDraw_Release(lpddraw);
280 if ((res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette))) {
281 ERR("Could not set default palette entries (res = %lx)\n", res);
284 memset(&sdesc,0,sizeof(sdesc));
285 sdesc.dwSize=sizeof(sdesc);
286 sdesc.dwFlags = DDSD_CAPS;
287 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
288 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
289 ERR("DirectDraw surface is not available\n");
290 IDirectDraw_Release(lpddraw);
294 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
296 /* poll every 20ms (50fps should provide adequate responsiveness) */
297 VGA_InstallTimer(20);
303 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
311 if(Xres >= 640 || Yres >= 480) {
319 par.Depth = (Depth < 8) ? 8 : Depth;
321 vga_mode_initialized = TRUE;
323 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
327 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
329 if (!lpddraw) return 1;
330 if (!lpddsurf) return 1;
331 if (Height) *Height=sdesc.dwHeight;
332 if (Width) *Width=sdesc.dwWidth;
333 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
337 static void WINAPI VGA_DoExit(ULONG_PTR arg)
339 VGA_DeinstallTimer();
340 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
341 IDirectDrawSurface_Release(lpddsurf);
343 IDirectDrawPalette_Release(lpddpal);
345 IDirectDraw_Release(lpddraw);
351 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
354 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
356 if (!lpddraw) return;
357 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
360 /* set a single color in 16 color mode. */
361 void VGA_SetColor16(int reg,int color)
365 if (!lpddraw) return;
366 pal= &vga_def64_palette[color];
367 IDirectDrawPalette_SetEntries(lpddpal,0,reg,1,pal);
368 vga_16_palette[reg]=(char)color;
371 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
373 PALETTEENTRY pal[256];
376 if (!lpddraw) return;
377 for (c=0; c<len; c++) {
378 pal[c].peRed =color[c].rgbRed;
379 pal[c].peGreen=color[c].rgbGreen;
380 pal[c].peBlue =color[c].rgbBlue;
383 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
386 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
388 if (!lpddraw) return NULL;
389 if (!lpddsurf) return NULL;
390 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
391 ERR("could not lock surface!\n");
394 if (Pitch) *Pitch=sdesc.u1.lPitch;
395 if (Height) *Height=sdesc.dwHeight;
396 if (Width) *Width=sdesc.dwWidth;
397 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
398 return sdesc.lpSurface;
401 void VGA_Unlock(void)
403 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
408 int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
412 if (lpddraw) VGA_Exit();
414 /* FIXME: Where to initialize text attributes? */
415 VGA_SetTextAttribute(0xf);
417 /* the xterm is slow, so refresh only every 200ms (5fps) */
418 VGA_InstallTimer(200);
422 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
426 void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
428 CONSOLE_SCREEN_BUFFER_INFO info;
429 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
430 if (Xres) *Xres=info.dwSize.X;
431 if (Yres) *Yres=info.dwSize.Y;
434 void VGA_SetCursorPos(unsigned X,unsigned Y)
438 if (!poll_timer) VGA_SetAlphaMode(80, 25);
441 SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
444 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
446 CONSOLE_SCREEN_BUFFER_INFO info;
447 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
448 if (X) *X=info.dwCursorPosition.X;
449 if (Y) *Y=info.dwCursorPosition.Y;
452 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
460 EnterCriticalSection(&vga_lock);
462 info.Char.AsciiChar = ch;
463 info.Attributes = (WORD)attr;
471 VGA_GetAlphaMode(&XR, &YR);
472 dat = VGA_AlphaBuffer() + ((XR*Y + X) * 2);
474 dest.Left = X + count;
475 dest.Right = X + count;
481 info.Attributes = *dat;
484 WriteConsoleOutputA(VGA_AlphaConsole(), &info, siz, off, &dest);
487 LeaveCriticalSection(&vga_lock);
490 static void VGA_PutCharAt(BYTE ascii, unsigned x, unsigned y)
492 unsigned width, height;
495 VGA_GetAlphaMode(&width, &height);
496 dat = VGA_AlphaBuffer() + ((width*y + x) * 2);
498 dat[1] = vga_text_attr;
501 void VGA_PutChar(BYTE ascii)
503 unsigned width, height, x, y, nx, ny;
505 EnterCriticalSection(&vga_lock);
507 VGA_GetAlphaMode(&width, &height);
508 VGA_GetCursorPos(&x, &y);
512 VGA_PutCharAt(' ', x, y);
517 x += ((x + 8) & ~7) - x;
533 VGA_PutCharAt(ascii, x, y);
538 * FIXME: add line wrapping and scrolling
541 WriteFile(VGA_AlphaConsole(), &ascii, 1, NULL, NULL);
544 * The following is just a sanity check.
546 VGA_GetCursorPos(&nx, &ny);
547 if(nx != x || ny != y)
548 WARN("VGA emulator and text console have become unsynchronized.\n");
550 LeaveCriticalSection(&vga_lock);
553 void VGA_SetTextAttribute(BYTE attr)
555 vga_text_attr = attr;
556 SetConsoleTextAttribute(VGA_AlphaConsole(), attr);
559 void VGA_ClearText(unsigned row1, unsigned col1,
560 unsigned row2, unsigned col2,
563 unsigned width, height, x, y;
565 char *dat = VGA_AlphaBuffer();
566 HANDLE con = VGA_AlphaConsole();
567 VGA_GetAlphaMode(&width, &height);
569 EnterCriticalSection(&vga_lock);
571 for(y=row1; y<=row2; y++) {
574 FillConsoleOutputCharacterA(con, ' ', col2-col1+1, off, NULL);
575 FillConsoleOutputAttribute(con, attr, col2-col1+1, off, NULL);
577 for(x=col1; x<=col2; x++) {
578 char *ptr = dat + ((width*y + x) * 2);
584 LeaveCriticalSection(&vga_lock);
587 void VGA_ScrollUpText(unsigned row1, unsigned col1,
588 unsigned row2, unsigned col2,
589 unsigned lines, BYTE attr)
591 FIXME("not implemented\n");
594 void VGA_ScrollDownText(unsigned row1, unsigned col1,
595 unsigned row2, unsigned col2,
596 unsigned lines, BYTE attr)
598 FIXME("not implemented\n");
601 void VGA_GetCharacterAtCursor(BYTE *ascii, BYTE *attr)
603 unsigned width, height, x, y;
606 VGA_GetAlphaMode(&width, &height);
607 VGA_GetCursorPos(&x, &y);
608 dat = VGA_AlphaBuffer() + ((width*y + x) * 2);
617 static void VGA_Poll_Graphics(void)
619 unsigned int Pitch, Height, Width, X, Y;
621 char *dat = DOSMEM_MapDosToLinear(0xa0000);
623 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
626 if(vga_width == 320 && vga_depth <= 4)
627 for (Y=0; Y<vga_height; Y++,surf+=Pitch*2,dat+=vga_width/8) {
628 for(X=0; X<vga_width; X+=8) {
632 int b0 = (dat[offset] >> Z) & 0x1;
634 surf[(X+index)*2] = b0;
635 surf[(X+index)*2+1] = b0;
636 surf[(X+index)*2+Pitch] = b0;
637 surf[(X+index)*2+Pitch+1] = b0;
642 if(vga_width == 320 && vga_depth == 8)
643 for (Y=0; Y<vga_height; Y++,surf+=Pitch*2,dat+=vga_width) {
644 for(X=0; X<vga_width; X++) {
648 surf[X*2+Pitch] = b0;
649 surf[X*2+Pitch+1] = b0;
654 for (Y=0; Y<vga_height; Y++,surf+=Pitch,dat+=vga_width/8) {
655 for(X=0; X<vga_width; X+=8) {
659 int b0 = (dat[offset] >> Z) & 0x1;
669 static void VGA_Poll_Text(void)
672 unsigned int Height,Width,Y,X;
676 HANDLE con = VGA_AlphaConsole();
678 VGA_GetAlphaMode(&Width,&Height);
679 dat = VGA_AlphaBuffer();
680 siz.X = 80; siz.Y = 1;
681 off.X = 0; off.Y = 0;
682 /* copy from virtual VGA frame buffer to console */
683 for (Y=0; Y<Height; Y++) {
684 dest.Top=Y; dest.Bottom=Y;
685 for (X=0; X<Width; X++) {
686 ch[X].Char.AsciiChar = *dat++;
687 /* WriteConsoleOutputA doesn't like "dead" chars */
688 if (ch[X].Char.AsciiChar == '\0')
689 ch[X].Char.AsciiChar = ' ';
690 ch[X].Attributes = *dat++;
692 dest.Left=0; dest.Right=Width+1;
693 WriteConsoleOutputA(con, ch, siz, off, &dest);
697 static void CALLBACK VGA_Poll( LPVOID arg, DWORD low, DWORD high )
699 if(!TryEnterCriticalSection(&vga_lock))
702 /* FIXME: optimize by doing this only if the data has actually changed
703 * (in a way similar to DIBSection, perhaps) */
711 LeaveCriticalSection(&vga_lock);
714 static BYTE palreg,palcnt;
715 static PALETTEENTRY paldat;
717 void VGA_ioport_out( WORD port, BYTE val )
721 palreg=val; palcnt=0; break;
723 ((BYTE*)&paldat)[palcnt++]=val << 2;
725 VGA_SetPalette(&paldat,palreg++,1);
732 BYTE VGA_ioport_in( WORD port )
738 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
739 we need to fake the occurrence of the vertical refresh */
740 ret=vga_refresh?0x00:0x08;
741 if (vga_mode_initialized)
744 /* Also fake the occurence of the vertical refresh when no graphic
746 vga_refresh=!vga_refresh;
757 VGA_DeinstallTimer();