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_refresh
;
41 * VGA controller memory is emulated using linear framebuffer.
42 * This frambuffer also acts as an interface
43 * between VGA controller emulation and DirectDraw.
45 * vga_fb_width: Display width in pixels. Can be modified when
46 * display mode is changed.
47 * vga_fb_height: Display height in pixels. Can be modified when
48 * display mode is changed.
49 * vga_fb_depth: Number of bits used to store single pixel color information.
50 * Each pixel uses (vga_fb_depth+7)/8 bytes because
51 * 1-16 color modes are mapped to 256 color mode.
52 * Can be modified when display mode is changed.
53 * vga_fb_pitch: How many bytes to add to pointer in order to move
54 * from one row to another. This is fixed in VGA modes,
55 * but can be modified in SVGA modes.
56 * vga_fb_offset: Offset added to framebuffer start address in order
57 * to find the display origin. Programs use this to do
58 * double buffering and to scroll display. The value can
59 * be modified in VGA and SVGA modes.
60 * vga_fb_size: How many bytes are allocated to framebuffer.
61 * VGA framebuffers are always larger than display size and
62 * SVGA framebuffers may also be.
63 * vga_fb_data: Pointer to framebuffer start.
64 * vga_fb_window: Offset of 64k window 0xa0000 in bytes from framebuffer start.
65 * This value is >= 0, if mode uses linear framebuffer and
66 * -1, if mode uses color planes. This value is fixed
67 * in all modes except 0x13 (256 color VGA) where
68 * 0 means normal mode and -1 means Mode-X (unchained mode).
70 static int vga_fb_width
;
71 static int vga_fb_height
;
72 static int vga_fb_depth
;
73 static int vga_fb_pitch
;
74 static int vga_fb_offset
;
75 static int vga_fb_size
= 0;
76 static void *vga_fb_data
= 0;
77 static int vga_fb_window
= 0;
82 * vga_text_attr: Current active attribute.
83 * vga_text_old: Last data sent to console.
84 * This is used to optimize console updates.
85 * vga_text_width: Width of the text display in characters.
86 * vga_text_height: Height of the text display in characters.
87 * vga_text_x: Current cursor X-position. Starts from zero.
88 * vga_text_y: Current cursor Y-position. Starts from zero.
89 * vga_text_console: TRUE if stdout is console,
90 * FALSE if it is regular file.
92 static BYTE vga_text_attr
;
93 static char *vga_text_old
= NULL
;
94 static BYTE vga_text_width
;
95 static BYTE vga_text_height
;
96 static BYTE vga_text_x
;
97 static BYTE vga_text_y
;
98 static BOOL vga_text_console
;
101 * VGA controller ports 0x3c0, 0x3c4, 0x3ce and 0x3d4 are
102 * indexed registers. These ports are used to select VGA controller
103 * subregister that can be written to or read from using ports 0x3c1,
104 * 0x3c5, 0x3cf or 0x3d5. Selected subregister indexes are
105 * stored in variables vga_index_*.
107 * Port 0x3c0 is special because it is both index and
108 * data-write register. Flip-flop vga_address_3c0 tells whether
109 * the port acts currently as an address register. Reading from port
110 * 0x3da resets the flip-flop to address mode.
112 static BYTE vga_index_3c0
;
113 static BYTE vga_index_3c4
;
114 static BYTE vga_index_3ce
;
115 static BYTE vga_index_3d4
;
116 static BOOL vga_address_3c0
= TRUE
;
118 static CRITICAL_SECTION vga_lock
= CRITICAL_SECTION_INIT("VGA");
120 typedef HRESULT (WINAPI
*DirectDrawCreateProc
)(LPGUID
,LPDIRECTDRAW
*,LPUNKNOWN
);
121 static DirectDrawCreateProc pDirectDrawCreate
;
123 static void CALLBACK
VGA_Poll( LPVOID arg
, DWORD low
, DWORD high
);
125 static HWND vga_hwnd
= NULL
;
128 * For simplicity, I'm creating a second palette.
129 * 16 color accesses will use these pointers and insert
130 * entries from the 64-color palette into the default
131 * palette. --Robert 'Admiral' Coeyman
134 static char vga_16_palette
[17]={
135 0x00, /* 0 - Black */
137 0x02, /* 2 - Green */
140 0x05, /* 5 - Magenta */
141 0x14, /* 6 - Brown */
142 0x07, /* 7 - Light gray */
143 0x38, /* 8 - Dark gray */
144 0x39, /* 9 - Light blue */
145 0x3a, /* A - Light green */
146 0x3b, /* B - Light cyan */
147 0x3c, /* C - Light red */
148 0x3d, /* D - Light magenta */
149 0x3e, /* E - Yellow */
150 0x3f, /* F - White */
151 0x00 /* Border Color */
154 static PALETTEENTRY vga_def_palette
[256]={
156 {0x00, 0x00, 0x00}, /* 0 - Black */
157 {0x00, 0x00, 0x80}, /* 1 - Blue */
158 {0x00, 0x80, 0x00}, /* 2 - Green */
159 {0x00, 0x80, 0x80}, /* 3 - Cyan */
160 {0x80, 0x00, 0x00}, /* 4 - Red */
161 {0x80, 0x00, 0x80}, /* 5 - Magenta */
162 {0x80, 0x80, 0x00}, /* 6 - Brown */
163 {0xC0, 0xC0, 0xC0}, /* 7 - Light gray */
164 {0x80, 0x80, 0x80}, /* 8 - Dark gray */
165 {0x00, 0x00, 0xFF}, /* 9 - Light blue */
166 {0x00, 0xFF, 0x00}, /* A - Light green */
167 {0x00, 0xFF, 0xFF}, /* B - Light cyan */
168 {0xFF, 0x00, 0x00}, /* C - Light red */
169 {0xFF, 0x00, 0xFF}, /* D - Light magenta */
170 {0xFF, 0xFF, 0x00}, /* E - Yellow */
171 {0xFF, 0xFF, 0xFF}, /* F - White */
172 {0,0,0} /* FIXME: a series of continuous rainbow hues should follow */
176 * This palette is the dos default, converted from 18 bit color to 24.
177 * It contains only 64 entries of colors--all others are zeros.
178 * --Robert 'Admiral' Coeyman
180 static PALETTEENTRY vga_def64_palette
[256]={
182 {0x00, 0x00, 0x00}, /* 0x00 Black */
183 {0x00, 0x00, 0xaa}, /* 0x01 Blue */
184 {0x00, 0xaa, 0x00}, /* 0x02 Green */
185 {0x00, 0xaa, 0xaa}, /* 0x03 Cyan */
186 {0xaa, 0x00, 0x00}, /* 0x04 Red */
187 {0xaa, 0x00, 0xaa}, /* 0x05 Magenta */
188 {0xaa, 0xaa, 0x00}, /* 0x06 */
189 {0xaa, 0xaa, 0xaa}, /* 0x07 Light Gray */
190 {0x00, 0x00, 0x55}, /* 0x08 */
191 {0x00, 0x00, 0xff}, /* 0x09 */
192 {0x00, 0xaa, 0x55}, /* 0x0a */
193 {0x00, 0xaa, 0xff}, /* 0x0b */
194 {0xaa, 0x00, 0x55}, /* 0x0c */
195 {0xaa, 0x00, 0xff}, /* 0x0d */
196 {0xaa, 0xaa, 0x55}, /* 0x0e */
197 {0xaa, 0xaa, 0xff}, /* 0x0f */
198 {0x00, 0x55, 0x00}, /* 0x10 */
199 {0x00, 0x55, 0xaa}, /* 0x11 */
200 {0x00, 0xff, 0x00}, /* 0x12 */
201 {0x00, 0xff, 0xaa}, /* 0x13 */
202 {0xaa, 0x55, 0x00}, /* 0x14 Brown */
203 {0xaa, 0x55, 0xaa}, /* 0x15 */
204 {0xaa, 0xff, 0x00}, /* 0x16 */
205 {0xaa, 0xff, 0xaa}, /* 0x17 */
206 {0x00, 0x55, 0x55}, /* 0x18 */
207 {0x00, 0x55, 0xff}, /* 0x19 */
208 {0x00, 0xff, 0x55}, /* 0x1a */
209 {0x00, 0xff, 0xff}, /* 0x1b */
210 {0xaa, 0x55, 0x55}, /* 0x1c */
211 {0xaa, 0x55, 0xff}, /* 0x1d */
212 {0xaa, 0xff, 0x55}, /* 0x1e */
213 {0xaa, 0xff, 0xff}, /* 0x1f */
214 {0x55, 0x00, 0x00}, /* 0x20 */
215 {0x55, 0x00, 0xaa}, /* 0x21 */
216 {0x55, 0xaa, 0x00}, /* 0x22 */
217 {0x55, 0xaa, 0xaa}, /* 0x23 */
218 {0xff, 0x00, 0x00}, /* 0x24 */
219 {0xff, 0x00, 0xaa}, /* 0x25 */
220 {0xff, 0xaa, 0x00}, /* 0x26 */
221 {0xff, 0xaa, 0xaa}, /* 0x27 */
222 {0x55, 0x00, 0x55}, /* 0x28 */
223 {0x55, 0x00, 0xff}, /* 0x29 */
224 {0x55, 0xaa, 0x55}, /* 0x2a */
225 {0x55, 0xaa, 0xff}, /* 0x2b */
226 {0xff, 0x00, 0x55}, /* 0x2c */
227 {0xff, 0x00, 0xff}, /* 0x2d */
228 {0xff, 0xaa, 0x55}, /* 0x2e */
229 {0xff, 0xaa, 0xff}, /* 0x2f */
230 {0x55, 0x55, 0x00}, /* 0x30 */
231 {0x55, 0x55, 0xaa}, /* 0x31 */
232 {0x55, 0xff, 0x00}, /* 0x32 */
233 {0x55, 0xff, 0xaa}, /* 0x33 */
234 {0xff, 0x55, 0x00}, /* 0x34 */
235 {0xff, 0x55, 0xaa}, /* 0x35 */
236 {0xff, 0xff, 0x00}, /* 0x36 */
237 {0xff, 0xff, 0xaa}, /* 0x37 */
238 {0x55, 0x55, 0x55}, /* 0x38 Dark Gray */
239 {0x55, 0x55, 0xff}, /* 0x39 Light Blue */
240 {0x55, 0xff, 0x55}, /* 0x3a Light Green */
241 {0x55, 0xff, 0xff}, /* 0x3b Light Cyan */
242 {0xff, 0x55, 0x55}, /* 0x3c Light Red */
243 {0xff, 0x55, 0xff}, /* 0x3d Light Magenta */
244 {0xff, 0xff, 0x55}, /* 0x3e Yellow */
245 {0xff, 0xff, 0xff}, /* 0x3f White */
246 {0,0,0} /* The next 192 entries are all zeros */
249 static HANDLE VGA_timer
;
250 static HANDLE VGA_timer_thread
;
252 /* set the timer rate; called in the polling thread context */
253 static void CALLBACK
set_timer_rate( ULONG_PTR arg
)
257 when
.s
.LowPart
= when
.s
.HighPart
= 0;
258 SetWaitableTimer( VGA_timer
, &when
, arg
, VGA_Poll
, 0, FALSE
);
261 static DWORD CALLBACK
VGA_TimerThread( void *dummy
)
263 for (;;) WaitForMultipleObjectsEx( 0, NULL
, FALSE
, INFINITE
, TRUE
);
266 static void VGA_DeinstallTimer(void)
268 if (VGA_timer_thread
)
270 CancelWaitableTimer( VGA_timer
);
271 CloseHandle( VGA_timer
);
272 TerminateThread( VGA_timer_thread
, 0 );
273 CloseHandle( VGA_timer_thread
);
274 VGA_timer_thread
= 0;
278 static void VGA_InstallTimer(unsigned Rate
)
280 if (!VGA_timer_thread
)
282 VGA_timer
= CreateWaitableTimerA( NULL
, FALSE
, NULL
);
283 VGA_timer_thread
= CreateThread( NULL
, 0, VGA_TimerThread
, NULL
, 0, NULL
);
285 QueueUserAPC( set_timer_rate
, VGA_timer_thread
, (ULONG_PTR
)Rate
);
288 static BOOL
VGA_IsTimerRunning(void)
290 return VGA_timer_thread
? TRUE
: FALSE
;
293 HANDLE
VGA_AlphaConsole(void)
295 /* this assumes that no Win32 redirection has taken place, but then again,
296 * only 16-bit apps are likely to use this part of Wine... */
297 return GetStdHandle(STD_OUTPUT_HANDLE
);
300 char*VGA_AlphaBuffer(void)
302 return (char *)0xb8000;
305 /*** GRAPHICS MODE ***/
308 unsigned Xres
, Yres
, Depth
;
312 static void WINAPI
VGA_DoExit(ULONG_PTR arg
)
314 VGA_DeinstallTimer();
315 IDirectDrawSurface_SetPalette(lpddsurf
,NULL
);
316 IDirectDrawSurface_Release(lpddsurf
);
318 IDirectDrawPalette_Release(lpddpal
);
320 IDirectDraw_Release(lpddraw
);
324 static void WINAPI
VGA_DoSetMode(ULONG_PTR arg
)
327 ModeSet
*par
= (ModeSet
*)arg
;
330 if (lpddraw
) VGA_DoExit(0);
332 if (!pDirectDrawCreate
)
334 HMODULE hmod
= LoadLibraryA( "ddraw.dll" );
335 if (hmod
) pDirectDrawCreate
= (DirectDrawCreateProc
)GetProcAddress( hmod
, "DirectDrawCreate" );
336 if (!pDirectDrawCreate
) {
337 ERR("Can't lookup DirectDrawCreate from ddraw.dll.\n");
341 res
= pDirectDrawCreate(NULL
,&lpddraw
,NULL
);
343 ERR("DirectDraw is not available (res = %lx)\n",res
);
347 vga_hwnd
= CreateWindowExA(0,"STATIC","WINEDOS VGA",WS_POPUP
|WS_VISIBLE
,0,0,par
->Xres
,par
->Yres
,0,0,0,NULL
);
349 ERR("Failed to create user window.\n");
350 IDirectDraw_Release(lpddraw
);
356 SetWindowPos(vga_hwnd
,0,0,0,par
->Xres
,par
->Yres
,SWP_NOMOVE
|SWP_NOZORDER
);
358 if ((res
=IDirectDraw_SetCooperativeLevel(lpddraw
,vga_hwnd
,DDSCL_FULLSCREEN
|DDSCL_EXCLUSIVE
))) {
359 ERR("Could not set cooperative level to exclusive (%lx)\n",res
);
362 if ((res
=IDirectDraw_SetDisplayMode(lpddraw
,par
->Xres
,par
->Yres
,par
->Depth
))) {
363 ERR("DirectDraw does not support requested display mode (%dx%dx%d), res = %lx!\n",par
->Xres
,par
->Yres
,par
->Depth
,res
);
364 IDirectDraw_Release(lpddraw
);
369 res
=IDirectDraw_CreatePalette(lpddraw
,DDPCAPS_8BIT
,NULL
,&lpddpal
,NULL
);
371 ERR("Could not create palette (res = %lx)\n",res
);
372 IDirectDraw_Release(lpddraw
);
376 if ((res
=IDirectDrawPalette_SetEntries(lpddpal
,0,0,256,vga_def_palette
))) {
377 ERR("Could not set default palette entries (res = %lx)\n", res
);
380 memset(&sdesc
,0,sizeof(sdesc
));
381 sdesc
.dwSize
=sizeof(sdesc
);
382 sdesc
.dwFlags
= DDSD_CAPS
;
383 sdesc
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
384 if (IDirectDraw_CreateSurface(lpddraw
,&sdesc
,&lpddsurf
,NULL
)||(!lpddsurf
)) {
385 ERR("DirectDraw surface is not available\n");
386 IDirectDraw_Release(lpddraw
);
390 IDirectDrawSurface_SetPalette(lpddsurf
,lpddpal
);
392 /* poll every 20ms (50fps should provide adequate responsiveness) */
393 VGA_InstallTimer(20);
399 int VGA_SetMode(unsigned Xres
,unsigned Yres
,unsigned Depth
)
405 vga_fb_height
= Yres
;
406 vga_fb_depth
= Depth
;
408 vga_fb_pitch
= Xres
* ((Depth
+ 7) / 8);
410 newSize
= Xres
* Yres
* ((Depth
+ 7) / 8);
411 if(newSize
< 256 * 1024)
412 newSize
= 256 * 1024;
414 if(vga_fb_size
< newSize
) {
416 HeapFree(GetProcessHeap(), 0, vga_fb_data
);
417 vga_fb_data
= HeapAlloc(GetProcessHeap(), 0, newSize
);
418 vga_fb_size
= newSize
;
421 if(Xres
>= 640 || Yres
>= 480) {
429 VGA_SetWindowStart((Depth
< 8) ? -1 : 0);
431 par
.Depth
= (Depth
< 8) ? 8 : Depth
;
433 MZ_RunInThread(VGA_DoSetMode
, (ULONG_PTR
)&par
);
437 int VGA_GetMode(unsigned*Height
,unsigned*Width
,unsigned*Depth
)
439 if (!lpddraw
) return 1;
440 if (!lpddsurf
) return 1;
441 if (Height
) *Height
=sdesc
.dwHeight
;
442 if (Width
) *Width
=sdesc
.dwWidth
;
443 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
449 if (lpddraw
) MZ_RunInThread(VGA_DoExit
, 0);
452 void VGA_SetPalette(PALETTEENTRY
*pal
,int start
,int len
)
454 if (!lpddraw
) return;
455 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
458 /* set a single [char wide] color in 16 color mode. */
459 void VGA_SetColor16(int reg
,int color
)
463 if (!lpddraw
) return;
464 pal
= &vga_def64_palette
[color
];
465 IDirectDrawPalette_SetEntries(lpddpal
,0,reg
,1,pal
);
466 vga_16_palette
[reg
]=(char)color
;
469 /* Get a single [char wide] color in 16 color mode. */
470 char VGA_GetColor16(int reg
)
473 if (!lpddraw
) return 0;
474 return (char)vga_16_palette
[reg
];
477 /* set all 17 [char wide] colors at once in 16 color mode. */
478 void VGA_Set16Palette(char *Table
)
483 if (!lpddraw
) return; /* return if we're in text only mode */
484 bcopy((void *)&vga_16_palette
,(void *)Table
,17);
485 /* copy the entries into the table */
486 for (c
=0; c
<17; c
++) { /* 17 entries */
487 pal
= &vga_def64_palette
[(int)vga_16_palette
[c
]]; /* get color */
488 IDirectDrawPalette_SetEntries(lpddpal
,0,c
,1,pal
); /* set entry */
489 TRACE("Palette register %d set to %d\n",c
,(int)vga_16_palette
[c
]);
490 } /* end of the counting loop */
493 /* Get all 17 [ char wide ] colors at once in 16 color mode. */
494 void VGA_Get16Palette(char *Table
)
497 if (!lpddraw
) return; /* return if we're in text only mode */
498 bcopy((void *)Table
,(void *)&vga_16_palette
,17);
499 /* copy the entries into the table */
502 void VGA_SetQuadPalette(RGBQUAD
*color
,int start
,int len
)
504 PALETTEENTRY pal
[256];
507 if (!lpddraw
) return;
508 for (c
=0; c
<len
; c
++) {
509 pal
[c
].peRed
=color
[c
].rgbRed
;
510 pal
[c
].peGreen
=color
[c
].rgbGreen
;
511 pal
[c
].peBlue
=color
[c
].rgbBlue
;
514 IDirectDrawPalette_SetEntries(lpddpal
,0,start
,len
,pal
);
517 LPSTR
VGA_Lock(unsigned*Pitch
,unsigned*Height
,unsigned*Width
,unsigned*Depth
)
519 if (!lpddraw
) return NULL
;
520 if (!lpddsurf
) return NULL
;
521 if (IDirectDrawSurface_Lock(lpddsurf
,NULL
,&sdesc
,0,0)) {
522 ERR("could not lock surface!\n");
525 if (Pitch
) *Pitch
=sdesc
.u1
.lPitch
;
526 if (Height
) *Height
=sdesc
.dwHeight
;
527 if (Width
) *Width
=sdesc
.dwWidth
;
528 if (Depth
) *Depth
=sdesc
.ddpfPixelFormat
.u1
.dwRGBBitCount
;
529 return sdesc
.lpSurface
;
532 void VGA_Unlock(void)
534 IDirectDrawSurface_Unlock(lpddsurf
,sdesc
.lpSurface
);
538 * Set start of 64k window at 0xa0000 in bytes.
539 * If value is -1, initialize color plane support.
540 * If value is >= 0, window contains direct copy of framebuffer.
542 void VGA_SetWindowStart(int start
)
544 if(start
== vga_fb_window
)
547 if(vga_fb_window
== -1)
548 FIXME("Remove VGA memory emulation.\n");
550 memmove(vga_fb_data
+ vga_fb_window
, (char *)0xa0000, 64 * 1024);
552 vga_fb_window
= start
;
554 if(vga_fb_window
== -1)
555 FIXME("Install VGA memory emulation.\n");
557 memmove( (char *)0xa0000, vga_fb_data
+ vga_fb_window
, 64 * 1024);
561 * Get start of 64k window at 0xa0000 in bytes.
562 * Value is -1 in color plane modes.
564 int VGA_GetWindowStart()
566 return vga_fb_window
;
571 /* prepare the text mode video memory copy that is used to only
572 * update the video memory line that did get updated. */
573 void VGA_PrepareVideoMemCopy(unsigned Xres
, unsigned Yres
)
579 * Allocate space for char + attr.
581 vga_text_old
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
582 vga_text_old
, Xres
* Yres
* 2 );
584 p
= VGA_AlphaBuffer();
587 /* make sure the video mem copy contains the exact opposite of our
588 * actual text mode memory area to make sure the screen
589 * does get updated fully initially */
590 for (i
=0; i
< Xres
*Yres
*2; i
++)
591 *p2
++ ^= *p
++; /* XOR it */
594 /**********************************************************************
597 * Set VGA emulation to text mode.
599 void VGA_SetAlphaMode(unsigned Xres
,unsigned Yres
)
602 VGA_DeinstallTimer();
604 VGA_PrepareVideoMemCopy(Xres
, Yres
);
605 vga_text_width
= Xres
;
606 vga_text_height
= Yres
;
608 if (vga_text_x
>= vga_text_width
|| vga_text_y
>= vga_text_height
)
609 VGA_SetCursorPos(0,0);
611 if(vga_text_console
) {
615 SetConsoleScreenBufferSize( VGA_AlphaConsole(), size
);
617 /* poll every 30ms (33fps should provide adequate responsiveness) */
618 VGA_InstallTimer(30);
622 /**********************************************************************
625 * Initialize VGA text mode handling and return default text mode.
626 * This function does not set VGA emulation to text mode.
628 void VGA_InitAlphaMode(unsigned*Xres
,unsigned*Yres
)
630 CONSOLE_SCREEN_BUFFER_INFO info
;
632 if(GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info
))
634 vga_text_console
= TRUE
;
635 vga_text_x
= info
.dwCursorPosition
.X
;
636 vga_text_y
= info
.dwCursorPosition
.Y
;
637 vga_text_attr
= info
.wAttributes
;
638 *Xres
= info
.dwSize
.X
;
639 *Yres
= info
.dwSize
.Y
;
643 vga_text_console
= FALSE
;
646 vga_text_attr
= 0x0f;
652 /**********************************************************************
655 * Get current text mode. Returns TRUE and sets resolution if
656 * any VGA text mode has been initialized.
658 BOOL
VGA_GetAlphaMode(unsigned*Xres
,unsigned*Yres
)
660 if (vga_text_width
!= 0 && vga_text_height
!= 0) {
661 *Xres
= vga_text_width
;
662 *Yres
= vga_text_height
;
668 void VGA_SetCursorShape(unsigned char start_options
, unsigned char end
)
670 CONSOLE_CURSOR_INFO cci
;
672 /* standard cursor settings:
673 * 0x0607 == CGA, 0x0b0c == monochrome, 0x0d0e == EGA/VGA */
675 /* calculate percentage from bottom - assuming VGA (bottom 0x0e) */
676 cci
.dwSize
= ((end
& 0x1f) - (start_options
& 0x1f))/0x0e * 100;
677 if (!cci
.dwSize
) cci
.dwSize
++; /* NULL cursor would make SCCI() fail ! */
678 cci
.bVisible
= ((start_options
& 0x60) != 0x20); /* invisible ? */
680 SetConsoleCursorInfo(VGA_AlphaConsole(),&cci
);
683 void VGA_SetCursorPos(unsigned X
,unsigned Y
)
688 if (vga_text_console
) {
692 SetConsoleCursorPosition(VGA_AlphaConsole(),pos
);
696 void VGA_GetCursorPos(unsigned*X
,unsigned*Y
)
698 if (X
) *X
= vga_text_x
;
699 if (Y
) *Y
= vga_text_y
;
702 static void VGA_PutCharAt(unsigned x
, unsigned y
, BYTE ascii
, BYTE attr
)
706 dat
= VGA_AlphaBuffer() + ((vga_text_width
* y
+ x
) * 2);
710 dat
= vga_text_old
+ ((vga_text_width
* y
+ x
) * 2);
715 void VGA_WriteChars(unsigned X
,unsigned Y
,unsigned ch
,int attr
,int count
)
721 EnterCriticalSection(&vga_lock
);
723 info
.Char
.AsciiChar
= ch
;
724 info
.Attributes
= (WORD
)attr
;
736 char *dat
= VGA_AlphaBuffer() + ((vga_text_width
* Y
+ X
+ count
) * 2);
741 VGA_PutCharAt(X
+ count
, Y
, ch
, realattr
);
743 if (vga_text_console
) {
744 dest
.Left
= X
+ count
;
745 dest
.Right
= X
+ count
;
746 info
.Attributes
= realattr
;
747 WriteConsoleOutputA(VGA_AlphaConsole(), &info
, siz
, off
, &dest
);
751 LeaveCriticalSection(&vga_lock
);
754 void VGA_PutChar(BYTE ascii
)
756 EnterCriticalSection(&vga_lock
);
760 VGA_PutCharAt(vga_text_x
, vga_text_y
, ' ', vga_text_attr
);
765 vga_text_x
+= ((vga_text_x
+ 8) & ~7) - vga_text_x
;
781 VGA_PutCharAt(vga_text_x
, vga_text_y
, ascii
, vga_text_attr
);
786 * FIXME: add line wrapping and scrolling
789 WriteFile(VGA_AlphaConsole(), &ascii
, 1, NULL
, NULL
);
792 * The following is just a sanity check.
794 if(vga_text_console
) {
795 CONSOLE_SCREEN_BUFFER_INFO info
;
796 GetConsoleScreenBufferInfo( VGA_AlphaConsole(), &info
);
798 if( info
.dwCursorPosition
.X
!= vga_text_x
||
799 info
.dwCursorPosition
.Y
!= vga_text_y
)
800 WARN("VGA emulator and text console have become unsynchronized.\n");
803 LeaveCriticalSection(&vga_lock
);
806 void VGA_SetTextAttribute(BYTE attr
)
808 vga_text_attr
= attr
;
810 SetConsoleTextAttribute(VGA_AlphaConsole(), attr
);
813 void VGA_ClearText(unsigned row1
, unsigned col1
,
814 unsigned row2
, unsigned col2
,
819 EnterCriticalSection(&vga_lock
);
821 for(y
=row1
; y
<=row2
; y
++) {
822 if(vga_text_console
) {
823 HANDLE con
= VGA_AlphaConsole();
828 FillConsoleOutputCharacterA(con
, ' ', col2
-col1
+1, off
, NULL
);
829 FillConsoleOutputAttribute(con
, attr
, col2
-col1
+1, off
, NULL
);
832 for(x
=col1
; x
<=col2
; x
++)
833 VGA_PutCharAt(x
, y
, ' ', attr
);
836 LeaveCriticalSection(&vga_lock
);
839 void VGA_ScrollUpText(unsigned row1
, unsigned col1
,
840 unsigned row2
, unsigned col2
,
841 unsigned lines
, BYTE attr
)
843 FIXME("not implemented\n");
846 void VGA_ScrollDownText(unsigned row1
, unsigned col1
,
847 unsigned row2
, unsigned col2
,
848 unsigned lines
, BYTE attr
)
850 FIXME("not implemented\n");
853 void VGA_GetCharacterAtCursor(BYTE
*ascii
, BYTE
*attr
)
857 dat
= VGA_AlphaBuffer() + ((vga_text_width
* vga_text_y
+ vga_text_x
) * 2);
866 /* FIXME: optimize by doing this only if the data has actually changed
867 * (in a way similar to DIBSection, perhaps) */
868 static void VGA_Poll_Graphics(void)
870 unsigned int Pitch
, Height
, Width
, X
, Y
;
872 char *dat
= vga_fb_data
+ vga_fb_offset
;
873 int bpp
= (vga_fb_depth
+ 7) / 8;
875 surf
= VGA_Lock(&Pitch
,&Height
,&Width
,NULL
);
879 * Synchronize framebuffer contents.
881 if(vga_fb_window
!= -1)
882 memmove(vga_fb_data
+ vga_fb_window
, (char *)0xa0000, 64 * 1024);
885 * Double VGA framebuffer (320x200 -> 640x400), if needed.
887 if(Height
>= 2 * vga_fb_height
&& Width
>= 2 * vga_fb_width
&& bpp
== 1)
888 for (Y
=0; Y
<vga_fb_height
; Y
++,surf
+=Pitch
*2,dat
+=vga_fb_pitch
)
889 for (X
=0; X
<vga_fb_width
; X
++) {
893 surf
[X
*2+Pitch
] = value
;
894 surf
[X
*2+Pitch
+1] = value
;
897 for (Y
=0; Y
<vga_fb_height
; Y
++,surf
+=Pitch
,dat
+=vga_fb_pitch
)
898 memcpy(surf
, dat
, vga_fb_width
* bpp
);
903 static void VGA_Poll_Text(void)
905 char *dat
, *old
, *p_line
;
907 CHAR_INFO ch
[256]; /* that should suffice for the largest text width */
910 HANDLE con
= VGA_AlphaConsole();
911 BOOL linechanged
= FALSE
; /* video memory area differs from stored copy ? */
913 dat
= VGA_AlphaBuffer();
914 old
= vga_text_old
; /* pointer to stored video mem copy */
915 siz
.X
= vga_text_width
; siz
.Y
= 1;
916 off
.X
= 0; off
.Y
= 0;
917 /* copy from virtual VGA frame buffer to console */
918 for (Y
=0; Y
<vga_text_height
; Y
++) {
919 linechanged
= memcmp(dat
, old
, vga_text_width
*2);
922 /*TRACE("line %d changed\n", Y);*/
924 for (X
=0; X
<vga_text_width
; X
++) {
925 ch
[X
].Char
.AsciiChar
= *p_line
++;
926 /* WriteConsoleOutputA doesn't like "dead" chars */
927 if (ch
[X
].Char
.AsciiChar
== '\0')
928 ch
[X
].Char
.AsciiChar
= ' ';
929 ch
[X
].Attributes
= *p_line
++;
931 dest
.Top
=Y
; dest
.Bottom
=Y
;
932 dest
.Left
=0; dest
.Right
=vga_text_width
+1;
933 WriteConsoleOutputA(con
, ch
, siz
, off
, &dest
);
934 memcpy(old
, dat
, vga_text_width
*2);
936 /* advance to next text line */
937 dat
+= vga_text_width
*2;
938 old
+= vga_text_width
*2;
942 static void CALLBACK
VGA_Poll( LPVOID arg
, DWORD low
, DWORD high
)
944 if(!TryEnterCriticalSection(&vga_lock
))
954 LeaveCriticalSection(&vga_lock
);
957 static BYTE palreg
,palcnt
;
958 static PALETTEENTRY paldat
;
960 void VGA_ioport_out( WORD port
, BYTE val
)
967 FIXME("Unsupported index, register 0x3c0: 0x%02x (value 0x%02x)\n",
969 vga_address_3c0
= !vga_address_3c0
;
975 switch(vga_index_3c4
) {
976 case 0x04: /* Sequencer: Memory Mode Register */
977 if(vga_fb_depth
== 8)
978 VGA_SetWindowStart((val
& 8) ? 0 : -1);
980 FIXME("Memory Mode Register not supported in this mode.\n");
983 FIXME("Unsupported index, register 0x3c4: 0x%02x (value 0x%02x)\n",
988 FIXME("Unsupported index, register 0x3c4: 0x%02x (value 0x%02x)\n",
992 palreg
=val
; palcnt
=0; break;
994 ((BYTE
*)&paldat
)[palcnt
++]=val
<< 2;
996 VGA_SetPalette(&paldat
,palreg
++,1);
1001 vga_index_3ce
= val
;
1004 FIXME("Unsupported index, register 0x3ce: 0x%02x (value 0x%02x)\n",
1005 vga_index_3ce
, val
);
1008 vga_index_3d4
= val
;
1011 FIXME("Unsupported index, register 0x3d4: 0x%02x (value 0x%02x)\n",
1012 vga_index_3d4
, val
);
1015 FIXME("Unsupported VGA register: 0x%04x (value 0x%02x)\n", port
, val
);
1019 BYTE
VGA_ioport_in( WORD port
)
1025 FIXME("Unsupported index, register 0x3c0: 0x%02x\n",
1029 switch(vga_index_3c4
) {
1030 case 0x04: /* Sequencer: Memory Mode Register */
1031 return (VGA_GetWindowStart() == -1) ? 0xf7 : 0xff;
1033 FIXME("Unsupported index, register 0x3c4: 0x%02x\n",
1038 FIXME("Unsupported index, register 0x3ce: 0x%02x\n",
1042 FIXME("Unsupported index, register 0x3d4: 0x%02x\n",
1047 * Read from this register resets register 0x3c0 address flip-flop.
1049 vga_address_3c0
= TRUE
;
1050 /* since we don't (yet?) serve DOS VM requests while VGA_Poll is running,
1051 we need to fake the occurrence of the vertical refresh */
1052 ret
=vga_refresh
?0x00:0x0b; /* toggle video RAM and lightpen and VGA refresh bits ! */
1053 if (VGA_IsTimerRunning())
1056 /* Also fake the occurence of the vertical refresh when no graphic
1057 mode has been set */
1058 vga_refresh
=!vga_refresh
;
1062 FIXME("Unsupported VGA register: 0x%04x\n", port
);
1067 void VGA_Clean(void)
1070 VGA_DeinstallTimer();