2 * VGA hardware emulation
4 * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(ddraw
);
34 static IDirectDraw
*lpddraw
= NULL
;
35 static IDirectDrawSurface
*lpddsurf
;
36 static IDirectDrawPalette
*lpddpal
;
37 static DDSURFACEDESC sdesc
;
38 static LONG vga_polling
,vga_refresh
;
39 static HANDLE poll_timer
;
42 static int vga_height
;
45 typedef HRESULT (WINAPI
*DirectDrawCreateProc
)(LPGUID
,LPDIRECTDRAW
*,LPUNKNOWN
);
46 static DirectDrawCreateProc pDirectDrawCreate
;
48 static void CALLBACK
VGA_Poll( LPVOID arg
, DWORD low
, DWORD high
);
51 * For simplicity, I'm creating a second palette.
52 * 16 color accesses will use these pointers and insert
53 * entries from the 64-color palette into the default
54 * palette. --Robert 'Admiral' Coeyman
57 static char vga_16_palette
[17]={
63 0x05, /* 5 - Magenta */
65 0x07, /* 7 - Light gray */
66 0x38, /* 8 - Dark gray */
67 0x39, /* 9 - Light blue */
68 0x3a, /* A - Light green */
69 0x3b, /* B - Light cyan */
70 0x3c, /* C - Light red */
71 0x3d, /* D - Light magenta */
72 0x3e, /* E - Yellow */
74 0x00 /* Border Color */
77 static PALETTEENTRY vga_def_palette
[256]={
79 {0x00, 0x00, 0x00}, /* 0 - Black */
80 {0x00, 0x00, 0x80}, /* 1 - Blue */
81 {0x00, 0x80, 0x00}, /* 2 - Green */
82 {0x00, 0x80, 0x80}, /* 3 - Cyan */
83 {0x80, 0x00, 0x00}, /* 4 - Red */
84 {0x80, 0x00, 0x80}, /* 5 - Magenta */
85 {0x80, 0x80, 0x00}, /* 6 - Brown */
86 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
87 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
88 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
89 {0x00, 0xFF, 0x00}, /* A - Light green */
90 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
91 {0xFF, 0x00, 0x00}, /* C - Light red */
92 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
93 {0xFF, 0xFF, 0x00}, /* E - Yellow */
94 {0xFF, 0xFF, 0xFF}, /* F - White */
95 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
99 * This palette is the dos default, converted from 18 bit color to 24.
100 * It contains only 64 entries of colors--all others are zeros.
101 * --Robert 'Admiral' Coeyman
103 static PALETTEENTRY vga_def64_palette
[256]={
105 {0x00, 0x00, 0x00}, /* 0x00 Black */
106 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
107 {0x00, 0xaa, 0x00}, /* 0x02 Green */
108 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
109 {0xaa, 0x00, 0x00}, /* 0x04 Red */
110 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
111 {0xaa, 0xaa, 0x00}, /* 0x06 */
112 {0xaa, 0xaa, 0xaa}, /* 0x07 Light Gray */
113 {0x00, 0x00, 0x55}, /* 0x08 */
114 {0x00, 0x00, 0xff}, /* 0x09 */
115 {0x00, 0xaa, 0x55}, /* 0x0a */
116 {0x00, 0xaa, 0xff}, /* 0x0b */
117 {0xaa, 0x00, 0x55}, /* 0x0c */
118 {0xaa, 0x00, 0xff}, /* 0x0d */
119 {0xaa, 0xaa, 0x55}, /* 0x0e */
120 {0xaa, 0xaa, 0xff}, /* 0x0f */
121 {0x00, 0x55, 0x00}, /* 0x10 */
122 {0x00, 0x55, 0xaa}, /* 0x11 */
123 {0x00, 0xff, 0x00}, /* 0x12 */
124 {0x00, 0xff, 0xaa}, /* 0x13 */
125 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
126 {0xaa, 0x55, 0xaa}, /* 0x15 */
127 {0xaa, 0xff, 0x00}, /* 0x16 */
128 {0xaa, 0xff, 0xaa}, /* 0x17 */
129 {0x00, 0x55, 0x55}, /* 0x18 */
130 {0x00, 0x55, 0xff}, /* 0x19 */
131 {0x00, 0xff, 0x55}, /* 0x1a */
132 {0x00, 0xff, 0xff}, /* 0x1b */
133 {0xaa, 0x55, 0x55}, /* 0x1c */
134 {0xaa, 0x55, 0xff}, /* 0x1d */
135 {0xaa, 0xff, 0x55}, /* 0x1e */
136 {0xaa, 0xff, 0xff}, /* 0x1f */
137 {0x55, 0x00, 0x00}, /* 0x20 */
138 {0x55, 0x00, 0xaa}, /* 0x21 */
139 {0x55, 0xaa, 0x00}, /* 0x22 */
140 {0x55, 0xaa, 0xaa}, /* 0x23 */
141 {0xff, 0x00, 0x00}, /* 0x24 */
142 {0xff, 0x00, 0xaa}, /* 0x25 */
143 {0xff, 0xaa, 0x00}, /* 0x26 */
144 {0xff, 0xaa, 0xaa}, /* 0x27 */
145 {0x55, 0x00, 0x55}, /* 0x28 */
146 {0x55, 0x00, 0xff}, /* 0x29 */
147 {0x55, 0xaa, 0x55}, /* 0x2a */
148 {0x55, 0xaa, 0xff}, /* 0x2b */
149 {0xff, 0x00, 0x55}, /* 0x2c */
150 {0xff, 0x00, 0xff}, /* 0x2d */
151 {0xff, 0xaa, 0x55}, /* 0x2e */
152 {0xff, 0xaa, 0xff}, /* 0x2f */
153 {0x55, 0x55, 0x00}, /* 0x30 */
154 {0x55, 0x55, 0xaa}, /* 0x31 */
155 {0x55, 0xff, 0x00}, /* 0x32 */
156 {0x55, 0xff, 0xaa}, /* 0x33 */
157 {0xff, 0x55, 0x00}, /* 0x34 */
158 {0xff, 0x55, 0xaa}, /* 0x35 */
159 {0xff, 0xff, 0x00}, /* 0x36 */
160 {0xff, 0xff, 0xaa}, /* 0x37 */
161 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
162 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
163 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
164 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
165 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
166 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
167 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
168 {0xff, 0xff, 0xff}, /* 0x3f White */
169 {0,0,0} /* The next 192 entries are all zeros */
172 static HANDLE VGA_timer
;
173 static HANDLE VGA_timer_thread
;
175 /* set the timer rate; called in the polling thread context */
176 static void CALLBACK
set_timer_rate( ULONG_PTR arg
)
180 when
.s
.LowPart
= when
.s
.HighPart
= 0;
181 SetWaitableTimer( VGA_timer
, &when
, arg
, VGA_Poll
, 0, FALSE
);
184 static DWORD CALLBACK
VGA_TimerThread( void *dummy
)
186 for (;;) WaitForMultipleObjectsEx( 0, NULL
, FALSE
, INFINITE
, TRUE
);
189 static void VGA_DeinstallTimer(void)
191 if (VGA_timer_thread
)
193 CancelWaitableTimer( VGA_timer
);
194 CloseHandle( VGA_timer
);
195 TerminateThread( VGA_timer_thread
, 0 );
196 CloseHandle( VGA_timer_thread
);
197 VGA_timer_thread
= 0;
201 static void VGA_InstallTimer(unsigned Rate
)
203 if (!VGA_timer_thread
)
205 VGA_timer
= CreateWaitableTimerA( NULL
, FALSE
, NULL
);
206 VGA_timer_thread
= CreateThread( NULL
, 0, VGA_TimerThread
, NULL
, 0, NULL
);
208 QueueUserAPC( set_timer_rate
, VGA_timer_thread
, (ULONG_PTR
)Rate
);
211 HANDLE
VGA_AlphaConsole(void)
213 /* this assumes that no Win32 redirection has taken place, but then again,
214 * only 16-bit apps are likely to use this part of Wine... */
215 return GetStdHandle(STD_OUTPUT_HANDLE
);
218 char*VGA_AlphaBuffer(void)
220 return DOSMEM_MapDosToLinear(0xb8000);
223 /*** GRAPHICS MODE ***/
226 unsigned Xres
, Yres
, Depth
;
230 static void WINAPI
VGA_DoSetMode(ULONG_PTR arg
)
234 ModeSet
*par
= (ModeSet
*)arg
;
237 if (lpddraw
) VGA_Exit();
239 if (!pDirectDrawCreate
)
241 HMODULE hmod
= LoadLibraryA( "ddraw.dll" );
242 if (hmod
) pDirectDrawCreate
= (DirectDrawCreateProc
)GetProcAddress( hmod
, "DirectDrawCreate" );
243 if (!pDirectDrawCreate
) {
244 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
248 res
= pDirectDrawCreate(NULL
,&lpddraw
,NULL
);
250 ERR("DirectDraw is not available (res = %lx)\n",res
);
253 hwnd
= CreateWindowExA(0,"STATIC","WINEDOS VGA",WS_POPUP
|WS_BORDER
|WS_CAPTION
|WS_SYSMENU
,0,0,par
->Xres
,par
->Yres
,0,0,0,NULL
);
255 ERR("Failed to create user window.\n");
257 if ((res
=IDirectDraw_SetCooperativeLevel(lpddraw
,hwnd
,DDSCL_FULLSCREEN
|DDSCL_EXCLUSIVE
))) {
258 ERR("Could not set cooperative level to exclusive (%lx)\n",res
);
261 if ((res
=IDirectDraw_SetDisplayMode(lpddraw
,par
->Xres
,par
->Yres
,par
->Depth
))) {
262 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par
->Xres
,par
->Yres
,par
->Depth
,res
);
263 IDirectDraw_Release(lpddraw
);
268 res
=IDirectDraw_CreatePalette(lpddraw
,DDPCAPS_8BIT
,NULL
,&lpddpal
,NULL
);
270 ERR("Could not create palette (res = %lx)\n",res
);
271 IDirectDraw_Release(lpddraw
);
275 if ((res
=IDirectDrawPalette_SetEntries(lpddpal
,0,0,256,vga_def_palette
))) {
276 ERR("Could not set default palette entries (res = %lx)\n", res
);
279 memset(&sdesc
,0,sizeof(sdesc
));
280 sdesc
.dwSize
=sizeof(sdesc
);
281 sdesc
.dwFlags
= DDSD_CAPS
;
282 sdesc
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
283 if (IDirectDraw_CreateSurface(lpddraw
,&sdesc
,&lpddsurf
,NULL
)||(!lpddsurf
)) {
284 ERR("DirectDraw surface is not available\n");
285 IDirectDraw_Release(lpddraw
);
289 IDirectDrawSurface_SetPalette(lpddsurf
,lpddpal
);
291 /* poll every 20ms (50fps should provide adequate responsiveness) */
292 VGA_InstallTimer(20);
298 int VGA_SetMode(unsigned Xres
,unsigned Yres
,unsigned Depth
)
306 if(Xres
>= 640 || Yres
>= 480) {
314 par
.Depth
= (Depth
< 8) ? 8 : Depth
;
316 MZ_RunInThread(VGA_DoSetMode
, (ULONG_PTR
)&par
);
320 int VGA_GetMode(unsigned*Height
,unsigned*Width
,unsigned*Depth
)
322 if (!lpddraw
) return 1;
323 if (!lpddsurf
) return 1;
324 if (Height
) *Height
=sdesc
.dwHeight
;
325 if (Width
) *Width
=sdesc
.dwWidth
;
326 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
330 static void WINAPI
VGA_DoExit(ULONG_PTR arg
)
332 VGA_DeinstallTimer();
333 IDirectDrawSurface_SetPalette(lpddsurf
,NULL
);
334 IDirectDrawSurface_Release(lpddsurf
);
336 IDirectDrawPalette_Release(lpddpal
);
338 IDirectDraw_Release(lpddraw
);
344 if (lpddraw
) MZ_RunInThread(VGA_DoExit
, 0);
347 void VGA_SetPalette(PALETTEENTRY
*pal
,int start
,int len
)
349 if (!lpddraw
) return;
350 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
353 /* set a single color in 16 color mode. */
354 void VGA_SetColor16(int reg
,int color
)
358 if (!lpddraw
) return;
359 pal
= &vga_def64_palette
[color
];
360 IDirectDrawPalette_SetEntries(lpddpal
,0,reg
,1,pal
);
361 vga_16_palette
[reg
]=(char)color
;
364 void VGA_SetQuadPalette(RGBQUAD
*color
,int start
,int len
)
366 PALETTEENTRY pal
[256];
369 if (!lpddraw
) return;
370 for (c
=0; c
<len
; c
++) {
371 pal
[c
].peRed
=color
[c
].rgbRed
;
372 pal
[c
].peGreen
=color
[c
].rgbGreen
;
373 pal
[c
].peBlue
=color
[c
].rgbBlue
;
376 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
379 LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
381 if (!lpddraw
) return NULL
;
382 if (!lpddsurf
) return NULL
;
383 if (IDirectDrawSurface_Lock(lpddsurf
,NULL
,&sdesc
,0,0)) {
384 ERR("could not lock surface!\n");
387 if (Pitch
) *Pitch
=sdesc
.u1
.lPitch
;
388 if (Height
) *Height
=sdesc
.dwHeight
;
389 if (Width
) *Width
=sdesc
.dwWidth
;
390 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
391 return sdesc
.lpSurface
;
394 void VGA_Unlock(void)
396 IDirectDrawSurface_Unlock(lpddsurf
,sdesc
.lpSurface
);
401 int VGA_SetAlphaMode(unsigned Xres
,unsigned Yres
)
405 if (lpddraw
) VGA_Exit();
407 /* the xterm is slow, so refresh only every 200ms (5fps) */
408 VGA_InstallTimer(200);
412 SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz
);
416 void VGA_GetAlphaMode(unsigned*Xres
,unsigned*Yres
)
418 CONSOLE_SCREEN_BUFFER_INFO info
;
419 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
420 if (Xres
) *Xres
=info
.dwSize
.X
;
421 if (Yres
) *Yres
=info
.dwSize
.Y
;
424 void VGA_SetCursorPos(unsigned X
,unsigned Y
)
428 if (!poll_timer
) VGA_SetAlphaMode(80, 25);
431 SetConsoleCursorPosition(VGA_AlphaConsole(),pos
);
434 void VGA_GetCursorPos(unsigned*X
,unsigned*Y
)
436 CONSOLE_SCREEN_BUFFER_INFO info
;
437 GetConsoleScreenBufferInfo(VGA_AlphaConsole(),&info
);
438 if (X
) *X
=info
.dwCursorPosition
.X
;
439 if (Y
) *Y
=info
.dwCursorPosition
.Y
;
442 void VGA_WriteChars(unsigned X
,unsigned Y
,unsigned ch
,int attr
,int count
)
447 VGA_GetAlphaMode(&XR
, &YR
);
448 dat
= VGA_AlphaBuffer() + ((XR
*Y
+ X
) * 2);
449 /* FIXME: also call WriteConsoleOutputA, for better responsiveness */
452 if (attr
>=0) *dat
= attr
;
459 static void VGA_Poll_Graphics(void)
461 unsigned int Pitch
, Height
, Width
, X
, Y
;
463 char *dat
= DOSMEM_MapDosToLinear(0xa0000);
465 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
468 if(vga_width
== 320 && vga_depth
<= 4)
469 for (Y
=0; Y
<vga_height
; Y
++,surf
+=Pitch
*2,dat
+=vga_width
/8) {
470 for(X
=0; X
<vga_width
; X
+=8) {
474 int b0
= (dat
[offset
] >> Z
) & 0x1;
476 surf
[(X
+index
)*2] = b0
;
477 surf
[(X
+index
)*2+1] = b0
;
478 surf
[(X
+index
)*2+Pitch
] = b0
;
479 surf
[(X
+index
)*2+Pitch
+1] = b0
;
484 if(vga_width
== 320 && vga_depth
== 8)
485 for (Y
=0; Y
<vga_height
; Y
++,surf
+=Pitch
*2,dat
+=vga_width
) {
486 for(X
=0; X
<vga_width
; X
++) {
490 surf
[X
*2+Pitch
] = b0
;
491 surf
[X
*2+Pitch
+1] = b0
;
496 for (Y
=0; Y
<vga_height
; Y
++,surf
+=Pitch
,dat
+=vga_width
/8) {
497 for(X
=0; X
<vga_width
; X
+=8) {
501 int b0
= (dat
[offset
] >> Z
) & 0x1;
512 static void CALLBACK
VGA_Poll( LPVOID arg
, DWORD low
, DWORD high
)
515 unsigned int Height
,Width
,Y
,X
;
517 if (!InterlockedExchangeAdd(&vga_polling
, 1)) {
518 /* FIXME: optimize by doing this only if the data has actually changed
519 * (in a way similar to DIBSection, perhaps) */
527 HANDLE con
= VGA_AlphaConsole();
529 VGA_GetAlphaMode(&Width
,&Height
);
530 dat
= VGA_AlphaBuffer();
531 siz
.X
= 80; siz
.Y
= 1;
532 off
.X
= 0; off
.Y
= 0;
533 /* copy from virtual VGA frame buffer to console */
534 for (Y
=0; Y
<Height
; Y
++) {
535 dest
.Top
=Y
; dest
.Bottom
=Y
;
536 for (X
=0; X
<Width
; X
++) {
537 ch
[X
].Char
.AsciiChar
= *dat
++;
538 /* WriteConsoleOutputA doesn't like "dead" chars */
539 if (ch
[X
].Char
.AsciiChar
== '\0')
540 ch
[X
].Char
.AsciiChar
= ' ';
541 ch
[X
].Attributes
= *dat
++;
543 dest
.Left
=0; dest
.Right
=Width
+1;
544 WriteConsoleOutputA(con
, ch
, siz
, off
, &dest
);
549 InterlockedDecrement(&vga_polling
);
552 static BYTE palreg
,palcnt
;
553 static PALETTEENTRY paldat
;
555 void VGA_ioport_out( WORD port
, BYTE val
)
559 palreg
=val
; palcnt
=0; break;
561 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
563 VGA_SetPalette(&paldat
,palreg
++,1);
570 BYTE
VGA_ioport_in( WORD port
)
576 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
577 we need to fake the occurrence of the vertical refresh */
578 ret
=vga_refresh
?0x00:0x08;
590 VGA_DeinstallTimer();