blob: 339618527e88fb190a81a96b2a95b609264f8c35 [file] [log] [blame]
Ove Kaaven1499a841998-11-01 12:51:47 +00001/*
2 * VGA hardware emulation
3 *
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
5 *
6 */
7
8#include <string.h>
Ove Kaaven1499a841998-11-01 12:51:47 +00009#include "winbase.h"
Ove Kaavenc9307ed2000-02-03 00:46:29 +000010#include "wincon.h"
Ove Kaaven1499a841998-11-01 12:51:47 +000011#include "miscemu.h"
12#include "vga.h"
Ove Kaaven1499a841998-11-01 12:51:47 +000013#include "ddraw.h"
Ove Kaavenc9307ed2000-02-03 00:46:29 +000014#include "services.h"
Alexandre Julliard61fece01999-06-26 19:09:08 +000015#include "debugtools.h"
Ove Kaaven1499a841998-11-01 12:51:47 +000016
Dimitrie O. Paun529da542000-11-27 23:54:25 +000017DEFAULT_DEBUG_CHANNEL(ddraw);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000018
Ove Kaaven1499a841998-11-01 12:51:47 +000019static IDirectDraw *lpddraw = NULL;
20static IDirectDrawSurface *lpddsurf;
21static IDirectDrawPalette *lpddpal;
22static DDSURFACEDESC sdesc;
Ove Kaavenc9307ed2000-02-03 00:46:29 +000023static LONG vga_polling,vga_refresh;
24static HANDLE poll_timer;
25
François Gougetc583b682001-01-05 03:44:40 +000026typedef HRESULT WINAPI (*DirectDrawCreateProc)(LPGUID,LPDIRECTDRAW *,LPUNKNOWN);
27static DirectDrawCreateProc pDirectDrawCreate;
Alexandre Julliardddce6522000-03-17 16:58:10 +000028
Ove Kaavenc9307ed2000-02-03 00:46:29 +000029static void VGA_DeinstallTimer(void)
30{
31 if (poll_timer) {
32 SERVICE_Delete( poll_timer );
33 poll_timer = 0;
34 }
35}
36
37static void VGA_InstallTimer(unsigned Rate)
38{
39 VGA_DeinstallTimer();
40 if (!poll_timer)
41 poll_timer = SERVICE_AddTimer( Rate, VGA_Poll, 0 );
42}
43
44HANDLE VGA_AlphaConsole(void)
45{
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);
49}
50
Ove Kaaven3be104e2000-05-23 21:14:11 +000051char*VGA_AlphaBuffer(void)
52{
53 return DOSMEM_MapDosToLinear(0xb8000);
54}
55
Ove Kaavenc9307ed2000-02-03 00:46:29 +000056/*** GRAPHICS MODE ***/
Ove Kaaven1499a841998-11-01 12:51:47 +000057
58int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
59{
60 if (lpddraw) VGA_Exit();
61 if (!lpddraw) {
Alexandre Julliardddce6522000-03-17 16:58:10 +000062 if (!pDirectDrawCreate)
63 {
64 HMODULE hmod = LoadLibraryA( "ddraw.dll" );
François Gougetc583b682001-01-05 03:44:40 +000065 if (hmod) pDirectDrawCreate = (DirectDrawCreateProc)GetProcAddress( hmod, "DirectDrawCreate" );
Alexandre Julliardddce6522000-03-17 16:58:10 +000066 }
67 if (pDirectDrawCreate) pDirectDrawCreate(NULL,&lpddraw,NULL);
Ove Kaaven1499a841998-11-01 12:51:47 +000068 if (!lpddraw) {
Alexandre Julliard61fece01999-06-26 19:09:08 +000069 ERR("DirectDraw is not available\n");
Ove Kaaven1499a841998-11-01 12:51:47 +000070 return 1;
71 }
Francois Gougetf44e3e91999-03-27 16:49:55 +000072 if (IDirectDraw_SetDisplayMode(lpddraw,Xres,Yres,Depth)) {
Alexandre Julliard61fece01999-06-26 19:09:08 +000073 ERR("DirectDraw does not support requested display mode\n");
Francois Gougetf44e3e91999-03-27 16:49:55 +000074 IDirectDraw_Release(lpddraw);
Ove Kaaven1499a841998-11-01 12:51:47 +000075 lpddraw=NULL;
76 return 1;
77 }
Francois Gougetf44e3e91999-03-27 16:49:55 +000078 IDirectDraw_CreatePalette(lpddraw,DDPCAPS_8BIT,NULL,&lpddpal,NULL);
Ove Kaaven1499a841998-11-01 12:51:47 +000079 memset(&sdesc,0,sizeof(sdesc));
80 sdesc.dwSize=sizeof(sdesc);
Alexandre Julliard8da12c41999-01-17 16:55:11 +000081 sdesc.dwFlags = DDSD_CAPS;
82 sdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
Francois Gougetf44e3e91999-03-27 16:49:55 +000083 if (IDirectDraw_CreateSurface(lpddraw,&sdesc,&lpddsurf,NULL)||(!lpddsurf)) {
Alexandre Julliard61fece01999-06-26 19:09:08 +000084 ERR("DirectDraw surface is not available\n");
Francois Gougetf44e3e91999-03-27 16:49:55 +000085 IDirectDraw_Release(lpddraw);
Ove Kaaven1499a841998-11-01 12:51:47 +000086 lpddraw=NULL;
87 return 1;
88 }
Ove Kaaven3be104e2000-05-23 21:14:11 +000089 FIXME("no default palette entries\n");
90 IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
Ove Kaaven194cfed1998-11-08 15:06:31 +000091 vga_refresh=0;
Ove Kaaven1499a841998-11-01 12:51:47 +000092 /* poll every 20ms (50fps should provide adequate responsiveness) */
Alexandre Julliard8c8237b2000-05-10 04:43:32 +000093 VGA_InstallTimer(20);
Ove Kaaven1499a841998-11-01 12:51:47 +000094 }
95 return 0;
96}
97
98int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
99{
100 if (!lpddraw) return 1;
101 if (!lpddsurf) return 1;
102 if (Height) *Height=sdesc.dwHeight;
103 if (Width) *Width=sdesc.dwWidth;
Alexandre Julliardd6c0f9f2001-01-04 22:44:55 +0000104 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
Ove Kaaven1499a841998-11-01 12:51:47 +0000105 return 0;
106}
107
108void VGA_Exit(void)
109{
110 if (lpddraw) {
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000111 VGA_DeinstallTimer();
Ove Kaaven3be104e2000-05-23 21:14:11 +0000112 IDirectDrawSurface_SetPalette(lpddsurf,NULL);
Francois Gougetf44e3e91999-03-27 16:49:55 +0000113 IDirectDrawSurface_Release(lpddsurf);
Ove Kaaven1499a841998-11-01 12:51:47 +0000114 lpddsurf=NULL;
Ove Kaaven3be104e2000-05-23 21:14:11 +0000115 IDirectDrawPalette_Release(lpddpal);
116 lpddpal=NULL;
Francois Gougetf44e3e91999-03-27 16:49:55 +0000117 IDirectDraw_Release(lpddraw);
Ove Kaaven1499a841998-11-01 12:51:47 +0000118 lpddraw=NULL;
119 }
120}
121
122void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
123{
124 if (!lpddraw) return;
Francois Gougetf44e3e91999-03-27 16:49:55 +0000125 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
Ove Kaaven1499a841998-11-01 12:51:47 +0000126}
127
128void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
129{
130 PALETTEENTRY pal[256];
131 int c;
132
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;
138 pal[c].peFlags=0;
139 }
Francois Gougetf44e3e91999-03-27 16:49:55 +0000140 IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
Ove Kaaven1499a841998-11-01 12:51:47 +0000141}
142
143LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
144{
145 if (!lpddraw) return NULL;
146 if (!lpddsurf) return NULL;
Francois Gougetf44e3e91999-03-27 16:49:55 +0000147 if (IDirectDrawSurface_Lock(lpddsurf,NULL,&sdesc,0,0)) {
Alexandre Julliard61fece01999-06-26 19:09:08 +0000148 ERR("could not lock surface!\n");
Ove Kaaven1499a841998-11-01 12:51:47 +0000149 return NULL;
150 }
Alexandre Julliardd6c0f9f2001-01-04 22:44:55 +0000151 if (Pitch) *Pitch=sdesc.u1.lPitch;
Ove Kaaven1499a841998-11-01 12:51:47 +0000152 if (Height) *Height=sdesc.dwHeight;
153 if (Width) *Width=sdesc.dwWidth;
Alexandre Julliardd6c0f9f2001-01-04 22:44:55 +0000154 if (Depth) *Depth=sdesc.ddpfPixelFormat.u1.dwRGBBitCount;
155 return sdesc.lpSurface;
Ove Kaaven1499a841998-11-01 12:51:47 +0000156}
157
158void VGA_Unlock(void)
159{
Alexandre Julliardd6c0f9f2001-01-04 22:44:55 +0000160 IDirectDrawSurface_Unlock(lpddsurf,sdesc.lpSurface);
Ove Kaaven1499a841998-11-01 12:51:47 +0000161}
162
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000163/*** TEXT MODE ***/
164
165int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
166{
167 COORD siz;
168
169 if (lpddraw) VGA_Exit();
170
171 /* the xterm is slow, so refresh only every 200ms (5fps) */
Alexandre Julliard8c8237b2000-05-10 04:43:32 +0000172 VGA_InstallTimer(200);
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000173
Alexandre Julliardf818d422000-05-03 17:48:21 +0000174 siz.X = Xres;
175 siz.Y = Yres;
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000176 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
177 return 0;
178}
179
180void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
181{
182 CONSOLE_SCREEN_BUFFER_INFO info;
183 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
Alexandre Julliardf818d422000-05-03 17:48:21 +0000184 if (Xres) *Xres=info.dwSize.X;
185 if (Yres) *Yres=info.dwSize.Y;
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000186}
187
188void VGA_SetCursorPos(unsigned X,unsigned Y)
189{
190 COORD pos;
191
Alexandre Julliardf818d422000-05-03 17:48:21 +0000192 pos.X = X;
193 pos.Y = Y;
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000194 SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
195}
196
197void VGA_GetCursorPos(unsigned*X,unsigned*Y)
198{
199 CONSOLE_SCREEN_BUFFER_INFO info;
200 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info);
Alexandre Julliardf818d422000-05-03 17:48:21 +0000201 if (X) *X=info.dwCursorPosition.X;
202 if (Y) *Y=info.dwCursorPosition.Y;
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000203}
204
Ove Kaaven3be104e2000-05-23 21:14:11 +0000205void VGA_WriteChars(unsigned X,unsigned Y,unsigned ch,int attr,int count)
206{
207 unsigned XR, YR;
208 char*dat;
209
210 VGA_GetAlphaMode(&XR, &YR);
211 dat = VGA_AlphaBuffer() + ((XR*Y + X) * 2);
212 /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
213 while (count--) {
214 *dat++ = ch;
215 if (attr>=0) *dat = attr;
216 dat++;
217 }
218}
219
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000220/*** CONTROL ***/
221
222void CALLBACK VGA_Poll( ULONG_PTR arg )
Ove Kaaven1499a841998-11-01 12:51:47 +0000223{
224 char *dat;
Alexandre Julliard908464d2000-11-01 03:11:12 +0000225 unsigned int Pitch,Height,Width,Y,X;
Ove Kaaven1499a841998-11-01 12:51:47 +0000226 char *surf;
Ove Kaaven1499a841998-11-01 12:51:47 +0000227
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000228 if (!InterlockedExchangeAdd(&vga_polling, 1)) {
Ove Kaaven1499a841998-11-01 12:51:47 +0000229 /* FIXME: optimize by doing this only if the data has actually changed
230 * (in a way similar to DIBSection, perhaps) */
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000231 if (lpddraw) {
232 /* graphics mode */
233 surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
234 if (!surf) return;
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);*/
240 }
241 VGA_Unlock();
242 } else {
243 /* text mode */
244 CHAR_INFO ch[80];
245 COORD siz, off;
246 SMALL_RECT dest;
247 HANDLE con = VGA_AlphaConsole();
248
249 VGA_GetAlphaMode(&Width,&Height);
Ove Kaaven3be104e2000-05-23 21:14:11 +0000250 dat = VGA_AlphaBuffer();
Alexandre Julliardf818d422000-05-03 17:48:21 +0000251 siz.X = 80; siz.Y = 1;
252 off.X = 0; off.Y = 0;
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000253 /* 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++;
Andreas Mohr744134e2000-10-12 20:41:29 +0000258 /* WriteConsoleOutputA doesn't like "dead" chars */
259 if (ch[X].Char.AsciiChar == '\0')
260 ch[X].Char.AsciiChar = ' ';
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000261 ch[X].Attributes = *dat++;
262 }
263 dest.Left=0; dest.Right=Width+1;
264 WriteConsoleOutputA(con, ch, siz, off, &dest);
265 }
Ove Kaaven1499a841998-11-01 12:51:47 +0000266 }
Ove Kaaven194cfed1998-11-08 15:06:31 +0000267 vga_refresh=1;
Ove Kaaven1499a841998-11-01 12:51:47 +0000268 }
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000269 InterlockedDecrement(&vga_polling);
Ove Kaaven1499a841998-11-01 12:51:47 +0000270}
271
272static BYTE palreg,palcnt;
273static PALETTEENTRY paldat;
274
275void VGA_ioport_out( WORD port, BYTE val )
276{
277 switch (port) {
278 case 0x3c8:
279 palreg=val; palcnt=0; break;
280 case 0x3c9:
281 ((BYTE*)&paldat)[palcnt++]=val << 2;
282 if (palcnt==3) {
Ove Kaavene1848e51999-03-14 12:23:17 +0000283 VGA_SetPalette(&paldat,palreg++,1);
284 palcnt=0;
Ove Kaaven1499a841998-11-01 12:51:47 +0000285 }
Ove Kaaven194cfed1998-11-08 15:06:31 +0000286 break;
Ove Kaaven1499a841998-11-01 12:51:47 +0000287 }
288}
Ove Kaaven194cfed1998-11-08 15:06:31 +0000289
290BYTE VGA_ioport_in( WORD port )
291{
292 BYTE ret;
293
294 switch (port) {
295 case 0x3da:
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 */
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000298 ret=vga_refresh?0x00:0x08;
299 vga_refresh=0;
Ove Kaaven194cfed1998-11-08 15:06:31 +0000300 break;
301 default:
302 ret=0xff;
303 }
304 return ret;
305}
Ove Kaavenc9307ed2000-02-03 00:46:29 +0000306
307void VGA_Clean(void)
308{
309 VGA_Exit();
310 VGA_DeinstallTimer();
311}