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 static HRESULT
WINAPI (*pDirectDrawCreate
)(LPGUID
,LPDIRECTDRAW
*,LPUNKNOWN
);
28 static void VGA_DeinstallTimer(void)
31 SERVICE_Delete( poll_timer
);
36 static void VGA_InstallTimer(unsigned Rate
)
40 poll_timer
= SERVICE_AddTimer( Rate
, VGA_Poll
, 0 );
43 HANDLE
VGA_AlphaConsole(void)
45 /* this assumes that no Win32 redirection has taken place, but then again,
46 * only 16-bit apps are likely to use this part of Wine... */
47 return GetStdHandle(STD_OUTPUT_HANDLE
);
50 char*VGA_AlphaBuffer(void)
52 return DOSMEM_MapDosToLinear(0xb8000);
55 /*** GRAPHICS MODE ***/
57 int VGA_SetMode(unsigned Xres
,unsigned Yres
,unsigned Depth
)
59 if (lpddraw
) VGA_Exit();
61 if (!pDirectDrawCreate
)
63 HMODULE hmod
= LoadLibraryA( "ddraw.dll" );
64 if (hmod
) pDirectDrawCreate
= GetProcAddress( hmod
, "DirectDrawCreate" );
66 if (pDirectDrawCreate
) pDirectDrawCreate(NULL
,&lpddraw
,NULL
);
68 ERR("DirectDraw is not available\n");
71 if (IDirectDraw_SetDisplayMode(lpddraw
,Xres
,Yres
,Depth
)) {
72 ERR("DirectDraw does not support requested display mode\n");
73 IDirectDraw_Release(lpddraw
);
77 IDirectDraw_CreatePalette(lpddraw
,DDPCAPS_8BIT
,NULL
,&lpddpal
,NULL
);
78 memset(&sdesc
,0,sizeof(sdesc
));
79 sdesc
.dwSize
=sizeof(sdesc
);
80 sdesc
.dwFlags
= DDSD_CAPS
;
81 sdesc
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
82 if (IDirectDraw_CreateSurface(lpddraw
,&sdesc
,&lpddsurf
,NULL
)||(!lpddsurf
)) {
83 ERR("DirectDraw surface is not available\n");
84 IDirectDraw_Release(lpddraw
);
88 FIXME("no default palette entries\n");
89 IDirectDrawSurface_SetPalette(lpddsurf
,lpddpal
);
91 /* poll every 20ms (50fps should provide adequate responsiveness) */
97 int VGA_GetMode(unsigned*Height
,unsigned*Width
,unsigned*Depth
)
99 if (!lpddraw
) return 1;
100 if (!lpddsurf
) return 1;
101 if (Height
) *Height
=sdesc
.dwHeight
;
102 if (Width
) *Width
=sdesc
.dwWidth
;
103 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u
.dwRGBBitCount
;
110 VGA_DeinstallTimer();
111 IDirectDrawSurface_SetPalette(lpddsurf
,NULL
);
112 IDirectDrawSurface_Release(lpddsurf
);
114 IDirectDrawPalette_Release(lpddpal
);
116 IDirectDraw_Release(lpddraw
);
121 void VGA_SetPalette(PALETTEENTRY
*pal
,int start
,int len
)
123 if (!lpddraw
) return;
124 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
127 void VGA_SetQuadPalette(RGBQUAD
*color
,int start
,int len
)
129 PALETTEENTRY pal
[256];
132 if (!lpddraw
) return;
133 for (c
=0; c
<len
; c
++) {
134 pal
[c
].peRed
=color
[c
].rgbRed
;
135 pal
[c
].peGreen
=color
[c
].rgbGreen
;
136 pal
[c
].peBlue
=color
[c
].rgbBlue
;
139 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
142 LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
144 if (!lpddraw
) return NULL
;
145 if (!lpddsurf
) return NULL
;
146 if (IDirectDrawSurface_Lock(lpddsurf
,NULL
,&sdesc
,0,0)) {
147 ERR("could not lock surface!\n");
150 if (Pitch
) *Pitch
=sdesc
.lPitch
;
151 if (Height
) *Height
=sdesc
.dwHeight
;
152 if (Width
) *Width
=sdesc
.dwWidth
;
153 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u
.dwRGBBitCount
;
154 return sdesc
.u1
.lpSurface
;
157 void VGA_Unlock(void)
159 IDirectDrawSurface_Unlock(lpddsurf
,sdesc
.u1
.lpSurface
);
164 int VGA_SetAlphaMode(unsigned Xres
,unsigned Yres
)
168 if (lpddraw
) VGA_Exit();
170 /* the xterm is slow, so refresh only every 200ms (5fps) */
171 VGA_InstallTimer(200);
175 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz
);
179 void VGA_GetAlphaMode(unsigned*Xres
,unsigned*Yres
)
181 CONSOLE_SCREEN_BUFFER_INFO info
;
182 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
183 if (Xres
) *Xres
=info
.dwSize
.X
;
184 if (Yres
) *Yres
=info
.dwSize
.Y
;
187 void VGA_SetCursorPos(unsigned X
,unsigned Y
)
193 SetConsoleCursorPosition(VGA_AlphaConsole(),pos
);
196 void VGA_GetCursorPos(unsigned*X
,unsigned*Y
)
198 CONSOLE_SCREEN_BUFFER_INFO info
;
199 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
200 if (X
) *X
=info
.dwCursorPosition
.X
;
201 if (Y
) *Y
=info
.dwCursorPosition
.Y
;
204 void VGA_WriteChars(unsigned X
,unsigned Y
,unsigned ch
,int attr
,int count
)
209 VGA_GetAlphaMode(&XR
, &YR
);
210 dat
= VGA_AlphaBuffer() + ((XR
*Y
+ X
) * 2);
211 /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
214 if (attr
>=0) *dat
= attr
;
221 void CALLBACK
VGA_Poll( ULONG_PTR arg
)
224 unsigned int Pitch
,Height
,Width
,Y
,X
;
227 if (!InterlockedExchangeAdd(&vga_polling
, 1)) {
228 /* FIXME: optimize by doing this only if the data has actually changed
229 * (in a way similar to DIBSection, perhaps) */
232 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
234 dat
= DOSMEM_MapDosToLinear(0xa0000);
235 /* copy from virtual VGA frame buffer to DirectDraw surface */
236 for (Y
=0; Y
<Height
; Y
++,surf
+=Pitch
,dat
+=Width
) {
237 memcpy(surf
,dat
,Width
);
238 /*for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);*/
246 HANDLE con
= VGA_AlphaConsole();
248 VGA_GetAlphaMode(&Width
,&Height
);
249 dat
= VGA_AlphaBuffer();
250 siz
.X
= 80; siz
.Y
= 1;
251 off
.X
= 0; off
.Y
= 0;
252 /* copy from virtual VGA frame buffer to console */
253 for (Y
=0; Y
<Height
; Y
++) {
254 dest
.Top
=Y
; dest
.Bottom
=Y
;
255 for (X
=0; X
<Width
; X
++) {
256 ch
[X
].Char
.AsciiChar
= *dat
++;
257 /* WriteConsoleOutputA doesn't like "dead" chars */
258 if (ch
[X
].Char
.AsciiChar
== '\0')
259 ch
[X
].Char
.AsciiChar
= ' ';
260 ch
[X
].Attributes
= *dat
++;
262 dest
.Left
=0; dest
.Right
=Width
+1;
263 WriteConsoleOutputA(con
, ch
, siz
, off
, &dest
);
268 InterlockedDecrement(&vga_polling
);
271 static BYTE palreg
,palcnt
;
272 static PALETTEENTRY paldat
;
274 void VGA_ioport_out( WORD port
, BYTE val
)
278 palreg
=val
; palcnt
=0; break;
280 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
282 VGA_SetPalette(&paldat
,palreg
++,1);
289 BYTE
VGA_ioport_in( WORD port
)
295 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
296 we need to fake the occurrence of the vertical refresh */
297 ret
=vga_refresh
?0x00:0x08;
309 VGA_DeinstallTimer();