2 * VGA hardware emulation
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
14 #include "interfaces.h"
18 static IDirectDraw
*lpddraw
= NULL
;
19 static IDirectDrawSurface
*lpddsurf
;
20 static IDirectDrawPalette
*lpddpal
;
21 static DDSURFACEDESC sdesc
;
22 static WORD poll_timer
;
23 static CRITICAL_SECTION vga_crit
;
24 static int vga_polling
,vga_refresh
;
26 int VGA_SetMode(unsigned Xres
,unsigned Yres
,unsigned Depth
)
28 if (lpddraw
) VGA_Exit();
30 DirectDrawCreate(NULL
,&lpddraw
,NULL
);
32 ERR(ddraw
,"DirectDraw is not available\n");
35 if (lpddraw
->lpvtbl
->fnSetDisplayMode(lpddraw
,Xres
,Yres
,Depth
)) {
36 ERR(ddraw
,"DirectDraw does not support requested display mode\n");
37 lpddraw
->lpvtbl
->fnRelease(lpddraw
);
41 lpddraw
->lpvtbl
->fnCreatePalette(lpddraw
,0,NULL
,&lpddpal
,NULL
);
42 memset(&sdesc
,0,sizeof(sdesc
));
43 sdesc
.dwSize
=sizeof(sdesc
);
44 if (lpddraw
->lpvtbl
->fnCreateSurface(lpddraw
,&sdesc
,&lpddsurf
,NULL
)||(!lpddsurf
)) {
45 ERR(ddraw
,"DirectDraw surface is not available\n");
46 lpddraw
->lpvtbl
->fnRelease(lpddraw
);
51 InitializeCriticalSection(&vga_crit
);
52 /* poll every 20ms (50fps should provide adequate responsiveness) */
53 poll_timer
= CreateSystemTimer( 20, VGA_Poll
);
58 int VGA_GetMode(unsigned*Height
,unsigned*Width
,unsigned*Depth
)
60 if (!lpddraw
) return 1;
61 if (!lpddsurf
) return 1;
62 if (Height
) *Height
=sdesc
.dwHeight
;
63 if (Width
) *Width
=sdesc
.dwWidth
;
64 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.x
.dwRGBBitCount
;
71 SYSTEM_KillSystemTimer(poll_timer
);
72 DeleteCriticalSection(&vga_crit
);
73 lpddsurf
->lpvtbl
->fnRelease(lpddsurf
);
75 lpddraw
->lpvtbl
->fnRelease(lpddraw
);
80 void VGA_SetPalette(PALETTEENTRY
*pal
,int start
,int len
)
83 lpddpal
->lpvtbl
->fnSetEntries(lpddpal
,0,start
,len
,pal
);
84 lpddsurf
->lpvtbl
->fnSetPalette(lpddsurf
,lpddpal
);
87 void VGA_SetQuadPalette(RGBQUAD
*color
,int start
,int len
)
89 PALETTEENTRY pal
[256];
93 for (c
=0; c
<len
; c
++) {
94 pal
[c
].peRed
=color
[c
].rgbRed
;
95 pal
[c
].peGreen
=color
[c
].rgbGreen
;
96 pal
[c
].peBlue
=color
[c
].rgbBlue
;
99 lpddpal
->lpvtbl
->fnSetEntries(lpddpal
,0,start
,len
,pal
);
100 lpddsurf
->lpvtbl
->fnSetPalette(lpddsurf
,lpddpal
);
103 LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
105 if (!lpddraw
) return NULL
;
106 if (!lpddsurf
) return NULL
;
107 if (lpddsurf
->lpvtbl
->fnLock(lpddsurf
,NULL
,&sdesc
,0,0)) {
108 ERR(ddraw
,"could not lock surface!\n");
111 if (Pitch
) *Pitch
=sdesc
.lPitch
;
112 if (Height
) *Height
=sdesc
.dwHeight
;
113 if (Width
) *Width
=sdesc
.dwWidth
;
114 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.x
.dwRGBBitCount
;
115 return sdesc
.y
.lpSurface
;
118 void VGA_Unlock(void)
120 lpddsurf
->lpvtbl
->fnUnlock(lpddsurf
,sdesc
.y
.lpSurface
);
123 void VGA_Poll( WORD timer
)
126 unsigned Pitch
,Height
,Width
;
130 EnterCriticalSection(&vga_crit
);
133 LeaveCriticalSection(&vga_crit
);
134 /* FIXME: optimize by doing this only if the data has actually changed
135 * (in a way similar to DIBSection, perhaps) */
136 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
138 dat
= DOSMEM_MapDosToLinear(0xa0000);
139 /* copy from virtual VGA frame buffer to DirectDraw surface */
140 for (Y
=0; Y
<Height
; Y
++,surf
+=Pitch
,dat
+=Width
) {
141 memcpy(surf
,dat
,Width
);
142 for (X
=0; X
<Width
; X
++) if (dat
[X
]) TRACE(ddraw
,"data(%d) at (%d,%d)\n",dat
[X
],X
,Y
);
146 EnterCriticalSection(&vga_crit
);
149 LeaveCriticalSection(&vga_crit
);
152 static BYTE palreg
,palcnt
;
153 static PALETTEENTRY paldat
;
155 void VGA_ioport_out( WORD port
, BYTE val
)
159 palreg
=val
; palcnt
=0; break;
161 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
163 VGA_SetPalette(&paldat
,palreg
,1);
170 BYTE
VGA_ioport_in( WORD port
)
176 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
177 we need to fake the occurrence of the vertical refresh */
179 ret
=vga_refresh
?0x00:0x08;