No longer directly accessing debuggee memory.
[wine] / graphics / vga.c
1 /*
2  * VGA hardware emulation
3  * 
4  * Copyright 1998 Ove Kåven (with some help from Marcus Meissner)
5  *
6  */
7
8 #include <string.h>
9 #include "winbase.h"
10 #include "wincon.h"
11 #include "miscemu.h"
12 #include "vga.h"
13 #include "ddraw.h"
14 #include "services.h"
15 #include "debugtools.h"
16
17 DEFAULT_DEBUG_CHANNEL(ddraw)
18
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;
25
26 static void VGA_DeinstallTimer(void)
27 {
28     if (poll_timer) {
29         SERVICE_Delete( poll_timer );
30         poll_timer = 0;
31     }
32 }
33
34 static void VGA_InstallTimer(unsigned Rate)
35 {
36     VGA_DeinstallTimer();
37     if (!poll_timer)
38         poll_timer = SERVICE_AddTimer( Rate, VGA_Poll, 0 );
39 }
40
41 HANDLE VGA_AlphaConsole(void)
42 {
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);
46 }
47
48 /*** GRAPHICS MODE ***/
49
50 int VGA_SetMode(unsigned Xres,unsigned Yres,unsigned Depth)
51 {
52     if (lpddraw) VGA_Exit();
53     if (!lpddraw) {
54         DirectDrawCreate(NULL,&lpddraw,NULL);
55         if (!lpddraw) {
56             ERR("DirectDraw is not available\n");
57             return 1;
58         }
59         if (IDirectDraw_SetDisplayMode(lpddraw,Xres,Yres,Depth)) {
60             ERR("DirectDraw does not support requested display mode\n");
61             IDirectDraw_Release(lpddraw);
62             lpddraw=NULL;
63             return 1;
64         }
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);
73             lpddraw=NULL;
74             return 1;
75         }
76         vga_refresh=0;
77         /* poll every 20ms (50fps should provide adequate responsiveness) */
78         VGA_InstallTimer(20000);
79     }
80     return 0;
81 }
82
83 int VGA_GetMode(unsigned*Height,unsigned*Width,unsigned*Depth)
84 {
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;
90     return 0;
91 }
92
93 void VGA_Exit(void)
94 {
95     if (lpddraw) {
96         VGA_DeinstallTimer();
97         IDirectDrawSurface_Release(lpddsurf);
98         lpddsurf=NULL;
99         IDirectDraw_Release(lpddraw);
100         lpddraw=NULL;
101     }
102 }
103
104 void VGA_SetPalette(PALETTEENTRY*pal,int start,int len)
105 {
106     if (!lpddraw) return;
107     IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
108     IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
109 }
110
111 void VGA_SetQuadPalette(RGBQUAD*color,int start,int len)
112 {
113     PALETTEENTRY pal[256];
114     int c;
115
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;
121         pal[c].peFlags=0;
122     }
123     IDirectDrawPalette_SetEntries(lpddpal,0,start,len,pal);
124     IDirectDrawSurface_SetPalette(lpddsurf,lpddpal);
125 }
126
127 LPSTR VGA_Lock(unsigned*Pitch,unsigned*Height,unsigned*Width,unsigned*Depth)
128 {
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");
133         return NULL;
134     }
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;
140 }
141
142 void VGA_Unlock(void)
143 {
144     IDirectDrawSurface_Unlock(lpddsurf,sdesc.u1.lpSurface);
145 }
146
147 /*** TEXT MODE ***/
148
149 int VGA_SetAlphaMode(unsigned Xres,unsigned Yres)
150 {
151     COORD siz;
152
153     if (lpddraw) VGA_Exit();
154
155     /* the xterm is slow, so refresh only every 200ms (5fps) */
156     VGA_InstallTimer(200000);
157
158     siz.x = Xres;
159     siz.y = Yres;
160     SetConsoleScreenBufferSize(VGA_AlphaConsole(),siz);
161     return 0;
162 }
163
164 void VGA_GetAlphaMode(unsigned*Xres,unsigned*Yres)
165 {
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;
170 }
171
172 void VGA_SetCursorPos(unsigned X,unsigned Y)
173 {
174     COORD pos;
175     
176     pos.x = X;
177     pos.y = Y;
178     SetConsoleCursorPosition(VGA_AlphaConsole(),pos);
179 }
180
181 void VGA_GetCursorPos(unsigned*X,unsigned*Y)
182 {
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;
187 }
188
189 /*** CONTROL ***/
190
191 void CALLBACK VGA_Poll( ULONG_PTR arg )
192 {
193     char *dat;
194     unsigned Pitch,Height,Width;
195     char *surf;
196     int Y,X;
197
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) */
201         if (lpddraw) {
202           /* graphics mode */
203           surf = VGA_Lock(&Pitch,&Height,&Width,NULL);
204           if (!surf) return;
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);*/
210           }
211           VGA_Unlock();
212         } else {
213           /* text mode */
214           CHAR_INFO ch[80];
215           COORD siz, off;
216           SMALL_RECT dest;
217           HANDLE con = VGA_AlphaConsole();
218
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++;
229               }
230               dest.Left=0; dest.Right=Width+1;
231               WriteConsoleOutputA(con, ch, siz, off, &dest);
232           }
233         }
234         vga_refresh=1;
235     }
236     InterlockedDecrement(&vga_polling);
237 }
238
239 static BYTE palreg,palcnt;
240 static PALETTEENTRY paldat;
241
242 void VGA_ioport_out( WORD port, BYTE val )
243 {
244     switch (port) {
245         case 0x3c8:
246             palreg=val; palcnt=0; break;
247         case 0x3c9:
248             ((BYTE*)&paldat)[palcnt++]=val << 2;
249             if (palcnt==3) {
250                 VGA_SetPalette(&paldat,palreg++,1);
251                 palcnt=0;
252             }
253             break;
254     }
255 }
256
257 BYTE VGA_ioport_in( WORD port )
258 {
259     BYTE ret;
260
261     switch (port) {
262         case 0x3da:
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;
266             vga_refresh=0;
267             break;
268         default:
269             ret=0xff;
270     }
271     return ret;
272 }
273
274 void VGA_Clean(void)
275 {
276     VGA_Exit();
277     VGA_DeinstallTimer();
278 }