2 * VGA hardware emulation
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(ddraw
);
19 static IDirectDraw
*lpddraw
= NULL
;
20 static IDirectDrawSurface
*lpddsurf
;
21 static IDirectDrawPalette
*lpddpal
;
22 static DDSURFACEDESC sdesc
;
23 static LONG vga_polling
,vga_refresh
;
24 static HANDLE poll_timer
;
26 typedef HRESULT
WINAPI (*DirectDrawCreateProc
)(LPGUID
,LPDIRECTDRAW
*,LPUNKNOWN
);
27 static DirectDrawCreateProc pDirectDrawCreate
;
29 static void VGA_DeinstallTimer(void)
32 SERVICE_Delete( poll_timer
);
37 static void VGA_InstallTimer(unsigned Rate
)
41 poll_timer
= SERVICE_AddTimer( Rate
, VGA_Poll
, 0 );
44 HANDLE
VGA_AlphaConsole(void)
46 /* this assumes that no Win32 redirection has taken place, but then again,
47 * only 16-bit apps are likely to use this part of Wine... */
48 return GetStdHandle(STD_OUTPUT_HANDLE
);
51 char*VGA_AlphaBuffer(void)
53 return DOSMEM_MapDosToLinear(0xb8000);
56 /*** GRAPHICS MODE ***/
58 int VGA_SetMode(unsigned Xres
,unsigned Yres
,unsigned Depth
)
60 if (lpddraw
) VGA_Exit();
62 if (!pDirectDrawCreate
)
64 HMODULE hmod
= LoadLibraryA( "ddraw.dll" );
65 if (hmod
) pDirectDrawCreate
= (DirectDrawCreateProc
)GetProcAddress( hmod
, "DirectDrawCreate" );
67 if (pDirectDrawCreate
) pDirectDrawCreate(NULL
,&lpddraw
,NULL
);
69 ERR("DirectDraw is not available\n");
72 if (IDirectDraw_SetDisplayMode(lpddraw
,Xres
,Yres
,Depth
)) {
73 ERR("DirectDraw does not support requested display mode\n");
74 IDirectDraw_Release(lpddraw
);
78 IDirectDraw_CreatePalette(lpddraw
,DDPCAPS_8BIT
,NULL
,&lpddpal
,NULL
);
79 memset(&sdesc
,0,sizeof(sdesc
));
80 sdesc
.dwSize
=sizeof(sdesc
);
81 sdesc
.dwFlags
= DDSD_CAPS
;
82 sdesc
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
83 if (IDirectDraw_CreateSurface(lpddraw
,&sdesc
,&lpddsurf
,NULL
)||(!lpddsurf
)) {
84 ERR("DirectDraw surface is not available\n");
85 IDirectDraw_Release(lpddraw
);
89 FIXME("no default palette entries\n");
90 IDirectDrawSurface_SetPalette(lpddsurf
,lpddpal
);
92 /* poll every 20ms (50fps should provide adequate responsiveness) */
98 int VGA_GetMode(unsigned*Height
,unsigned*Width
,unsigned*Depth
)
100 if (!lpddraw
) return 1;
101 if (!lpddsurf
) return 1;
102 if (Height
) *Height
=sdesc
.dwHeight
;
103 if (Width
) *Width
=sdesc
.dwWidth
;
104 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
111 VGA_DeinstallTimer();
112 IDirectDrawSurface_SetPalette(lpddsurf
,NULL
);
113 IDirectDrawSurface_Release(lpddsurf
);
115 IDirectDrawPalette_Release(lpddpal
);
117 IDirectDraw_Release(lpddraw
);
122 void VGA_SetPalette(PALETTEENTRY
*pal
,int start
,int len
)
124 if (!lpddraw
) return;
125 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
128 void VGA_SetQuadPalette(RGBQUAD
*color
,int start
,int len
)
130 PALETTEENTRY pal
[256];
133 if (!lpddraw
) return;
134 for (c
=0; c
<len
; c
++) {
135 pal
[c
].peRed
=color
[c
].rgbRed
;
136 pal
[c
].peGreen
=color
[c
].rgbGreen
;
137 pal
[c
].peBlue
=color
[c
].rgbBlue
;
140 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
143 LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
145 if (!lpddraw
) return NULL
;
146 if (!lpddsurf
) return NULL
;
147 if (IDirectDrawSurface_Lock(lpddsurf
,NULL
,&sdesc
,0,0)) {
148 ERR("could not lock surface!\n");
151 if (Pitch
) *Pitch
=sdesc
.u1
.lPitch
;
152 if (Height
) *Height
=sdesc
.dwHeight
;
153 if (Width
) *Width
=sdesc
.dwWidth
;
154 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
155 return sdesc
.lpSurface
;
158 void VGA_Unlock(void)
160 IDirectDrawSurface_Unlock(lpddsurf
,sdesc
.lpSurface
);
165 int VGA_SetAlphaMode(unsigned Xres
,unsigned Yres
)
169 if (lpddraw
) VGA_Exit();
171 /* the xterm is slow, so refresh only every 200ms (5fps) */
172 VGA_InstallTimer(200);
176 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz
);
180 void VGA_GetAlphaMode(unsigned*Xres
,unsigned*Yres
)
182 CONSOLE_SCREEN_BUFFER_INFO info
;
183 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
184 if (Xres
) *Xres
=info
.dwSize
.X
;
185 if (Yres
) *Yres
=info
.dwSize
.Y
;
188 void VGA_SetCursorPos(unsigned X
,unsigned Y
)
194 SetConsoleCursorPosition(VGA_AlphaConsole(),pos
);
197 void VGA_GetCursorPos(unsigned*X
,unsigned*Y
)
199 CONSOLE_SCREEN_BUFFER_INFO info
;
200 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
201 if (X
) *X
=info
.dwCursorPosition
.X
;
202 if (Y
) *Y
=info
.dwCursorPosition
.Y
;
205 void VGA_WriteChars(unsigned X
,unsigned Y
,unsigned ch
,int attr
,int count
)
210 VGA_GetAlphaMode(&XR
, &YR
);
211 dat
= VGA_AlphaBuffer() + ((XR
*Y
+ X
) * 2);
212 /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
215 if (attr
>=0) *dat
= attr
;
222 void CALLBACK
VGA_Poll( ULONG_PTR arg
)
225 unsigned int Pitch
,Height
,Width
,Y
,X
;
228 if (!InterlockedExchangeAdd(&vga_polling
, 1)) {
229 /* FIXME: optimize by doing this only if the data has actually changed
230 * (in a way similar to DIBSection, perhaps) */
233 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
235 dat
= DOSMEM_MapDosToLinear(0xa0000);
236 /* copy from virtual VGA frame buffer to DirectDraw surface */
237 for (Y
=0; Y
<Height
; Y
++,surf
+=Pitch
,dat
+=Width
) {
238 memcpy(surf
,dat
,Width
);
239 /*for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);*/
247 HANDLE con
= VGA_AlphaConsole();
249 VGA_GetAlphaMode(&Width
,&Height
);
250 dat
= VGA_AlphaBuffer();
251 siz
.X
= 80; siz
.Y
= 1;
252 off
.X
= 0; off
.Y
= 0;
253 /* copy from virtual VGA frame buffer to console */
254 for (Y
=0; Y
<Height
; Y
++) {
255 dest
.Top
=Y
; dest
.Bottom
=Y
;
256 for (X
=0; X
<Width
; X
++) {
257 ch
[X
].Char
.AsciiChar
= *dat
++;
258 /* WriteConsoleOutputA doesn't like "dead" chars */
259 if (ch
[X
].Char
.AsciiChar
== '\0')
260 ch
[X
].Char
.AsciiChar
= ' ';
261 ch
[X
].Attributes
= *dat
++;
263 dest
.Left
=0; dest
.Right
=Width
+1;
264 WriteConsoleOutputA(con
, ch
, siz
, off
, &dest
);
269 InterlockedDecrement(&vga_polling
);
272 static BYTE palreg
,palcnt
;
273 static PALETTEENTRY paldat
;
275 void VGA_ioport_out( WORD port
, BYTE val
)
279 palreg
=val
; palcnt
=0; break;
281 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
283 VGA_SetPalette(&paldat
,palreg
++,1);
290 BYTE
VGA_ioport_in( WORD port
)
296 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
297 we need to fake the occurrence of the vertical refresh */
298 ret
=vga_refresh
?0x00:0x08;
310 VGA_DeinstallTimer();