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 void VGA_DeinstallTimer(void)
29 SERVICE_Delete( poll_timer
);
34 static void VGA_InstallTimer(unsigned Rate
)
38 poll_timer
= SERVICE_AddTimer( Rate
, VGA_Poll
, 0 );
41 HANDLE
VGA_AlphaConsole(void)
43 /* this assumes that no Win32 redirection has taken place, but then again,
44 * only 16-bit apps are likely to use this part of Wine... */
45 return GetStdHandle(STD_OUTPUT_HANDLE
);
48 /*** GRAPHICS MODE ***/
50 int VGA_SetMode(unsigned Xres
,unsigned Yres
,unsigned Depth
)
52 if (lpddraw
) VGA_Exit();
54 DirectDrawCreate(NULL
,&lpddraw
,NULL
);
56 ERR("DirectDraw is not available\n");
59 if (IDirectDraw_SetDisplayMode(lpddraw
,Xres
,Yres
,Depth
)) {
60 ERR("DirectDraw does not support requested display mode\n");
61 IDirectDraw_Release(lpddraw
);
65 IDirectDraw_CreatePalette(lpddraw
,DDPCAPS_8BIT
,NULL
,&lpddpal
,NULL
);
66 memset(&sdesc
,0,sizeof(sdesc
));
67 sdesc
.dwSize
=sizeof(sdesc
);
68 sdesc
.dwFlags
= DDSD_CAPS
;
69 sdesc
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
70 if (IDirectDraw_CreateSurface(lpddraw
,&sdesc
,&lpddsurf
,NULL
)||(!lpddsurf
)) {
71 ERR("DirectDraw surface is not available\n");
72 IDirectDraw_Release(lpddraw
);
77 /* poll every 20ms (50fps should provide adequate responsiveness) */
78 VGA_InstallTimer(20000);
83 int VGA_GetMode(unsigned*Height
,unsigned*Width
,unsigned*Depth
)
85 if (!lpddraw
) return 1;
86 if (!lpddsurf
) return 1;
87 if (Height
) *Height
=sdesc
.dwHeight
;
88 if (Width
) *Width
=sdesc
.dwWidth
;
89 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u
.dwRGBBitCount
;
97 IDirectDrawSurface_Release(lpddsurf
);
99 IDirectDraw_Release(lpddraw
);
104 void VGA_SetPalette(PALETTEENTRY
*pal
,int start
,int len
)
106 if (!lpddraw
) return;
107 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
108 IDirectDrawSurface_SetPalette(lpddsurf
,lpddpal
);
111 void VGA_SetQuadPalette(RGBQUAD
*color
,int start
,int len
)
113 PALETTEENTRY pal
[256];
116 if (!lpddraw
) return;
117 for (c
=0; c
<len
; c
++) {
118 pal
[c
].peRed
=color
[c
].rgbRed
;
119 pal
[c
].peGreen
=color
[c
].rgbGreen
;
120 pal
[c
].peBlue
=color
[c
].rgbBlue
;
123 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
124 IDirectDrawSurface_SetPalette(lpddsurf
,lpddpal
);
127 LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
129 if (!lpddraw
) return NULL
;
130 if (!lpddsurf
) return NULL
;
131 if (IDirectDrawSurface_Lock(lpddsurf
,NULL
,&sdesc
,0,0)) {
132 ERR("could not lock surface!\n");
135 if (Pitch
) *Pitch
=sdesc
.lPitch
;
136 if (Height
) *Height
=sdesc
.dwHeight
;
137 if (Width
) *Width
=sdesc
.dwWidth
;
138 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u
.dwRGBBitCount
;
139 return sdesc
.u1
.lpSurface
;
142 void VGA_Unlock(void)
144 IDirectDrawSurface_Unlock(lpddsurf
,sdesc
.u1
.lpSurface
);
149 int VGA_SetAlphaMode(unsigned Xres
,unsigned Yres
)
153 if (lpddraw
) VGA_Exit();
155 /* the xterm is slow, so refresh only every 200ms (5fps) */
156 VGA_InstallTimer(200000);
160 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz
);
164 void VGA_GetAlphaMode(unsigned*Xres
,unsigned*Yres
)
166 CONSOLE_SCREEN_BUFFER_INFO info
;
167 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
168 if (Xres
) *Xres
=info
.dwSize
.x
;
169 if (Yres
) *Yres
=info
.dwSize
.y
;
172 void VGA_SetCursorPos(unsigned X
,unsigned Y
)
178 SetConsoleCursorPosition(VGA_AlphaConsole(),pos
);
181 void VGA_GetCursorPos(unsigned*X
,unsigned*Y
)
183 CONSOLE_SCREEN_BUFFER_INFO info
;
184 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
185 if (X
) *X
=info
.dwCursorPosition
.x
;
186 if (Y
) *Y
=info
.dwCursorPosition
.y
;
191 void CALLBACK
VGA_Poll( ULONG_PTR arg
)
194 unsigned Pitch
,Height
,Width
;
198 if (!InterlockedExchangeAdd(&vga_polling
, 1)) {
199 /* FIXME: optimize by doing this only if the data has actually changed
200 * (in a way similar to DIBSection, perhaps) */
203 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
205 dat
= DOSMEM_MapDosToLinear(0xa0000);
206 /* copy from virtual VGA frame buffer to DirectDraw surface */
207 for (Y
=0; Y
<Height
; Y
++,surf
+=Pitch
,dat
+=Width
) {
208 memcpy(surf
,dat
,Width
);
209 /*for (X=0; X<Width; X++) if (dat[X]) TRACE(ddraw,"data(%d) at (%d,%d)\n",dat[X],X,Y);*/
217 HANDLE con
= VGA_AlphaConsole();
219 VGA_GetAlphaMode(&Width
,&Height
);
220 dat
= DOSMEM_MapDosToLinear(0xb8000);
221 siz
.x
= 80; siz
.y
= 1;
222 off
.x
= 0; off
.y
= 0;
223 /* copy from virtual VGA frame buffer to console */
224 for (Y
=0; Y
<Height
; Y
++) {
225 dest
.Top
=Y
; dest
.Bottom
=Y
;
226 for (X
=0; X
<Width
; X
++) {
227 ch
[X
].Char
.AsciiChar
= *dat
++;
228 ch
[X
].Attributes
= *dat
++;
230 dest
.Left
=0; dest
.Right
=Width
+1;
231 WriteConsoleOutputA(con
, ch
, siz
, off
, &dest
);
236 InterlockedDecrement(&vga_polling
);
239 static BYTE palreg
,palcnt
;
240 static PALETTEENTRY paldat
;
242 void VGA_ioport_out( WORD port
, BYTE val
)
246 palreg
=val
; palcnt
=0; break;
248 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
250 VGA_SetPalette(&paldat
,palreg
++,1);
257 BYTE
VGA_ioport_in( WORD port
)
263 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
264 we need to fake the occurrence of the vertical refresh */
265 ret
=vga_refresh
?0x00:0x08;
277 VGA_DeinstallTimer();