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
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
35 static IDirectDraw *lpddraw = NULL;
36 static IDirectDrawSurface *lpddsurf;
37 static IDirectDrawPalette *lpddpal;
38 static DDSURFACEDESC sdesc;
39 static LONG vga_polling,vga_refresh;
40 static HANDLE poll_timer;
43 static int vga_height;
46 typedef HRESULT (WINAPI *DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
47 static DirectDrawCreateProc pDirectDrawCreate;
49 static PALETTEENTRY vga_def_palette[256]={
51 {0x00, 0x00, 0x00}, /* 0 - Black */
52 {0x00, 0x00, 0x80}, /* 1 - Blue */
53 {0x00, 0x80, 0x00}, /* 2 - Green */
54 {0x00, 0x80, 0x80}, /* 3 - Cyan */
55 {0x80, 0x00, 0x00}, /* 4 - Red */
56 {0x80, 0x00, 0x80}, /* 5 - Magenta */
57 {0x80, 0x80, 0x00}, /* 6 - Brown */
58 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
59 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
60 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
61 {0x00, 0xFF, 0x00}, /* A - Light green */
62 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
63 {0xFF, 0x00, 0x00}, /* C - Light red */
64 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
65 {0xFF, 0xFF, 0x00}, /* E - Yellow */
66 {0xFF, 0xFF, 0xFF}, /* F - White */
67 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
70 static void VGA_DeinstallTimer(void)
73 SERVICE_Delete( poll_timer );
78 static void VGA_InstallTimer(unsigned Rate)
82 poll_timer = SERVICE_AddTimer( Rate, VGA_Poll, 0 );
85 HANDLE VGA_AlphaConsole(void)
87 /* this assumes that no Win32 redirection has taken place, but then again,
88 * only 16-bit apps are likely to use this part of Wine... */
89 return GetStdHandle(STD_OUTPUT_HANDLE);
92 char*VGA_AlphaBuffer(void)
94 return DOSMEM_MapDosToLinear(0xb8000);
97 /*** GRAPHICS MODE ***/
100 unsigned Xres, Yres, Depth;
104 static void WINAPI VGA_DoSetMode(ULONG_PTR arg)
108 ModeSet *par = (ModeSet *)arg;
111 if (lpddraw) VGA_Exit();
113 if (!pDirectDrawCreate)
115 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
116 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
117 if (!pDirectDrawCreate) {
118 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
122 res = pDirectDrawCreate(NULL,&lpddraw,NULL);
124 ERR("DirectDraw is not available (res = %lx)\n",res);
127 hwnd = CreateWindowExA(0,"STATIC","WINEDOS VGA",WS_POPUP|WS_BORDER|WS_CAPTION|WS_SYSMENU,0,0,par->Xres,par->Yres,0,0,0,NULL);
129 ERR("Failed to create user window.\n");
131 if ((res=IDirectDraw_SetCooperativeLevel(lpddraw,hwnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE))) {
132 ERR("Could not set cooperative level to exclusive (%lx)\n",res);
135 if ((res=IDirectDraw_SetDisplayMode(lpddraw,par->Xres,par->Yres,par->Depth))) {
136 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par->Xres,par->Yres,par->Depth,res);
137 IDirectDraw_Release(lpddraw);
142 res=IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
144 ERR("Could not create palette (res = %lx)\n",res);
145 IDirectDraw_Release(lpddraw);
149 if ((res=IDirectDrawPalette_SetEntries(lpddpal,0,0,256,vga_def_palette))) {
150 ERR("Could not set default palette entries (res = %lx)\n", res);
153 memset(&sdesc,0,sizeof(sdesc));
154 sdesc.dwSize=sizeof(sdesc);
155 sdesc.dwFlags = DDSD_CAPS;
156 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
157 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
158 ERR("DirectDraw surface is not available\n");
159 IDirectDraw_Release(lpddraw);
163 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
165 /* poll every 20ms (50fps should provide adequate responsiveness) */
166 VGA_InstallTimer(20);
172 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
180 if(Xres >= 640 || Yres >= 480) {
188 par.Depth = (Depth < 8) ? 8 : Depth;
190 MZ_RunInThread(VGA_DoSetMode, (ULONG_PTR)&par);
194 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
196 if (!lpddraw) return 1;
197 if (!lpddsurf) return 1;
198 if (Height) *Height=sdesc.dwHeight;
199 if (Width) *Width=sdesc.dwWidth;
200 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
204 static void WINAPI VGA_DoExit(ULONG_PTR arg)
206 VGA_DeinstallTimer();
207 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
208 IDirectDrawSurface_Release(lpddsurf);
210 IDirectDrawPalette_Release(lpddpal);
212 IDirectDraw_Release(lpddraw);
218 if (lpddraw) MZ_RunInThread(VGA_DoExit, 0);
221 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
223 if (!lpddraw) return;
224 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
227 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
229 PALETTEENTRY pal[256];
232 if (!lpddraw) return;
233 for (c=0; c<len; c++) {
234 pal[c].peRed =color[c].rgbRed;
235 pal[c].peGreen=color[c].rgbGreen;
236 pal[c].peBlue =color[c].rgbBlue;
239 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
242 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
244 if (!lpddraw) return NULL;
245 if (!lpddsurf) return NULL;
246 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
247 ERR("could not lock surface!\n");
250 if (Pitch) *Pitch=sdesc.u1.lPitch;
251 if (Height) *Height=sdesc.dwHeight;
252 if (Width) *Width=sdesc.dwWidth;
253 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
254 return sdesc.lpSurface;
257 void VGA_Unlock(void)
259 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
264 int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
268 if (lpddraw) VGA_Exit();
270 /* the xterm is slow, so refresh only every 200ms (5fps) */
271 VGA_InstallTimer(200);
275 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
279 void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
281 CONSOLE_SCREEN_BUFFER_INFO info;
282 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
283 if (Xres) *Xres=info.dwSize.X;
284 if (Yres) *Yres=info.dwSize.Y;
287 void VGA_SetCursorPos(unsigned X,unsigned Y)
291 if (!poll_timer) VGA_SetAlphaMode(80, 25);
294 SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
297 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
299 CONSOLE_SCREEN_BUFFER_INFO info;
300 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
301 if (X) *X=info.dwCursorPosition.X;
302 if (Y) *Y=info.dwCursorPosition.Y;
305 void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
310 VGA_GetAlphaMode(&XR, &YR);
311 dat = VGA_AlphaBuffer() + ((XR*Y + X) * 2);
312 /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
315 if (attr>=0) *dat = attr;
322 static void VGA_Poll_Graphics(void)
324 unsigned int Pitch, Height, Width, X, Y;
326 char *dat = DOSMEM_MapDosToLinear(0xa0000);
328 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
331 if(vga_width == 320 && vga_depth <= 4)
332 for (Y=0; Y<vga_height; Y++,surf+=Pitch*2,dat+=vga_width/8) {
333 for(X=0; X<vga_width; X+=8) {
337 int b0 = (dat[offset] >> Z) & 0x1;
339 surf[(X+index)*2] = b0;
340 surf[(X+index)*2+1] = b0;
341 surf[(X+index)*2+Pitch] = b0;
342 surf[(X+index)*2+Pitch+1] = b0;
347 if(vga_width == 320 && vga_depth == 8)
348 for (Y=0; Y<vga_height; Y++,surf+=Pitch*2,dat+=vga_width) {
349 for(X=0; X<vga_width; X++) {
353 surf[X*2+Pitch] = b0;
354 surf[X*2+Pitch+1] = b0;
359 for (Y=0; Y<vga_height; Y++,surf+=Pitch,dat+=vga_width/8) {
360 for(X=0; X<vga_width; X+=8) {
364 int b0 = (dat[offset] >> Z) & 0x1;
375 void CALLBACK VGA_Poll( ULONG_PTR arg )
378 unsigned int Height,Width,Y,X;
380 if (!InterlockedExchangeAdd(&vga_polling, 1)) {
381 /* FIXME: optimize by doing this only if the data has actually changed
382 * (in a way similar to DIBSection, perhaps) */
390 HANDLE con = VGA_AlphaConsole();
392 VGA_GetAlphaMode(&Width,&Height);
393 dat = VGA_AlphaBuffer();
394 siz.X = 80; siz.Y = 1;
395 off.X = 0; off.Y = 0;
396 /* copy from virtual VGA frame buffer to console */
397 for (Y=0; Y<Height; Y++) {
398 dest.Top=Y; dest.Bottom=Y;
399 for (X=0; X<Width; X++) {
400 ch[X].Char.AsciiChar = *dat++;
401 /* WriteConsoleOutputA doesn't like "dead" chars */
402 if (ch[X].Char.AsciiChar == '\0')
403 ch[X].Char.AsciiChar = ' ';
404 ch[X].Attributes = *dat++;
406 dest.Left=0; dest.Right=Width+1;
407 WriteConsoleOutputA(con, ch, siz, off, &dest);
412 InterlockedDecrement(&vga_polling);
415 static BYTE palreg,palcnt;
416 static PALETTEENTRY paldat;
418 void VGA_ioport_out( WORD port, BYTE val )
422 palreg=val; palcnt=0; break;
424 ((BYTE*)&paldat)[palcnt++]=val << 2;
426 VGA_SetPalette(&paldat,palreg++,1);
433 BYTE VGA_ioport_in( WORD port )
439 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
440 we need to fake the occurrence of the vertical refresh */
441 ret=vga_refresh?0x00:0x08;
453 VGA_DeinstallTimer();