17f6f18ae57d1627ae8ececd869d80b8f06d6aba
[reactos.git] / subsystems / ntvdm / vga.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: vga.c
5 * PURPOSE: VGA hardware emulation
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "vga.h"
14 #include "bios.h"
15
16 /* PRIVATE VARIABLES **********************************************************/
17
18 static CONST DWORD MemoryBase[] = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
19 static CONST DWORD MemoryLimit[] = { 0xAFFFF, 0xAFFFF, 0xB7FFF, 0xBFFFF };
20
21 static CONST COLORREF VgaDefaultPalette[VGA_MAX_COLORS] =
22 {
23 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0xAA), RGB(0x00, 0xAA, 0x00), RGB(0x00, 0xAA, 0xAA),
24 RGB(0xAA, 0x00, 0x00), RGB(0xAA, 0x00, 0xAA), RGB(0xAA, 0x55, 0x00), RGB(0xAA, 0xAA, 0xAA),
25 RGB(0x55, 0x55, 0x55), RGB(0x55, 0x55, 0xFF), RGB(0x55, 0xFF, 0x55), RGB(0x55, 0xFF, 0xFF),
26 RGB(0xFF, 0x55, 0x55), RGB(0xFF, 0x55, 0xFF), RGB(0xFF, 0xFF, 0x55), RGB(0xFF, 0xFF, 0xFF),
27 RGB(0x00, 0x00, 0x00), RGB(0x10, 0x10, 0x10), RGB(0x20, 0x20, 0x20), RGB(0x35, 0x35, 0x35),
28 RGB(0x45, 0x45, 0x45), RGB(0x55, 0x55, 0x55), RGB(0x65, 0x65, 0x65), RGB(0x75, 0x75, 0x75),
29 RGB(0x8A, 0x8A, 0x8A), RGB(0x9A, 0x9A, 0x9A), RGB(0xAA, 0xAA, 0xAA), RGB(0xBA, 0xBA, 0xBA),
30 RGB(0xCA, 0xCA, 0xCA), RGB(0xDF, 0xDF, 0xDF), RGB(0xEF, 0xEF, 0xEF), RGB(0xFF, 0xFF, 0xFF),
31 RGB(0x00, 0x00, 0xFF), RGB(0x41, 0x00, 0xFF), RGB(0x82, 0x00, 0xFF), RGB(0xBE, 0x00, 0xFF),
32 RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0x00, 0xBE), RGB(0xFF, 0x00, 0x82), RGB(0xFF, 0x00, 0x41),
33 RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x41, 0x00), RGB(0xFF, 0x82, 0x00), RGB(0xFF, 0xBE, 0x00),
34 RGB(0xFF, 0xFF, 0x00), RGB(0xBE, 0xFF, 0x00), RGB(0x82, 0xFF, 0x00), RGB(0x41, 0xFF, 0x00),
35 RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0x41), RGB(0x00, 0xFF, 0x82), RGB(0x00, 0xFF, 0xBE),
36 RGB(0x00, 0xFF, 0xFF), RGB(0x00, 0xBE, 0xFF), RGB(0x00, 0x82, 0xFF), RGB(0x00, 0x41, 0xFF),
37 RGB(0x82, 0x82, 0xFF), RGB(0x9E, 0x82, 0xFF), RGB(0xBE, 0x82, 0xFF), RGB(0xDF, 0x82, 0xFF),
38 RGB(0xFF, 0x82, 0xFF), RGB(0xFF, 0x82, 0xDF), RGB(0xFF, 0x82, 0xBE), RGB(0xFF, 0x82, 0x9E),
39 RGB(0xFF, 0x82, 0x82), RGB(0xFF, 0x9E, 0x82), RGB(0xFF, 0xBE, 0x82), RGB(0xFF, 0xDF, 0x82),
40 RGB(0xFF, 0xFF, 0x82), RGB(0xDF, 0xFF, 0x82), RGB(0xBE, 0xFF, 0x82), RGB(0x9E, 0xFF, 0x82),
41 RGB(0x82, 0xFF, 0x82), RGB(0x82, 0xFF, 0x9E), RGB(0x82, 0xFF, 0xBE), RGB(0x82, 0xFF, 0xDF),
42 RGB(0x82, 0xFF, 0xFF), RGB(0x82, 0xDF, 0xFF), RGB(0x82, 0xBE, 0xFF), RGB(0x82, 0x9E, 0xFF),
43 RGB(0xBA, 0xBA, 0xFF), RGB(0xCA, 0xBA, 0xFF), RGB(0xDF, 0xBA, 0xFF), RGB(0xEF, 0xBA, 0xFF),
44 RGB(0xFF, 0xBA, 0xFF), RGB(0xFF, 0xBA, 0xEF), RGB(0xFF, 0xBA, 0xDF), RGB(0xFF, 0xBA, 0xCA),
45 RGB(0xFF, 0xBA, 0xBA), RGB(0xFF, 0xCA, 0xBA), RGB(0xFF, 0xDF, 0xBA), RGB(0xFF, 0xEF, 0xBA),
46 RGB(0xFF, 0xFF, 0xBA), RGB(0xEF, 0xFF, 0xBA), RGB(0xDF, 0xFF, 0xBA), RGB(0xCA, 0xFF, 0xBA),
47 RGB(0xBA, 0xFF, 0xBA), RGB(0xBA, 0xFF, 0xCA), RGB(0xBA, 0xFF, 0xDF), RGB(0xBA, 0xFF, 0xEF),
48 RGB(0xBA, 0xFF, 0xFF), RGB(0xBA, 0xEF, 0xFF), RGB(0xBA, 0xDF, 0xFF), RGB(0xBA, 0xCA, 0xFF),
49 RGB(0x00, 0x00, 0x71), RGB(0x1C, 0x00, 0x71), RGB(0x39, 0x00, 0x71), RGB(0x55, 0x00, 0x71),
50 RGB(0x71, 0x00, 0x71), RGB(0x71, 0x00, 0x55), RGB(0x71, 0x00, 0x39), RGB(0x71, 0x00, 0x1C),
51 RGB(0x71, 0x00, 0x00), RGB(0x71, 0x1C, 0x00), RGB(0x71, 0x39, 0x00), RGB(0x71, 0x55, 0x00),
52 RGB(0x71, 0x71, 0x00), RGB(0x55, 0x71, 0x00), RGB(0x39, 0x71, 0x00), RGB(0x1C, 0x71, 0x00),
53 RGB(0x00, 0x71, 0x00), RGB(0x00, 0x71, 0x1C), RGB(0x00, 0x71, 0x39), RGB(0x00, 0x71, 0x55),
54 RGB(0x00, 0x71, 0x71), RGB(0x00, 0x55, 0x71), RGB(0x00, 0x39, 0x71), RGB(0x00, 0x1C, 0x71),
55 RGB(0x39, 0x39, 0x71), RGB(0x45, 0x39, 0x71), RGB(0x55, 0x39, 0x71), RGB(0x61, 0x39, 0x71),
56 RGB(0x71, 0x39, 0x71), RGB(0x71, 0x39, 0x61), RGB(0x71, 0x39, 0x55), RGB(0x71, 0x39, 0x45),
57 RGB(0x71, 0x39, 0x39), RGB(0x71, 0x45, 0x39), RGB(0x71, 0x55, 0x39), RGB(0x71, 0x61, 0x39),
58 RGB(0x71, 0x71, 0x39), RGB(0x61, 0x71, 0x39), RGB(0x55, 0x71, 0x39), RGB(0x45, 0x71, 0x39),
59 RGB(0x39, 0x71, 0x39), RGB(0x39, 0x71, 0x45), RGB(0x39, 0x71, 0x55), RGB(0x39, 0x71, 0x61),
60 RGB(0x39, 0x71, 0x71), RGB(0x39, 0x61, 0x71), RGB(0x39, 0x55, 0x71), RGB(0x39, 0x45, 0x71),
61 RGB(0x51, 0x51, 0x71), RGB(0x59, 0x51, 0x71), RGB(0x61, 0x51, 0x71), RGB(0x69, 0x51, 0x71),
62 RGB(0x71, 0x51, 0x71), RGB(0x71, 0x51, 0x69), RGB(0x71, 0x51, 0x61), RGB(0x71, 0x51, 0x59),
63 RGB(0x71, 0x51, 0x51), RGB(0x71, 0x59, 0x51), RGB(0x71, 0x61, 0x51), RGB(0x71, 0x69, 0x51),
64 RGB(0x71, 0x71, 0x51), RGB(0x69, 0x71, 0x51), RGB(0x61, 0x71, 0x51), RGB(0x59, 0x71, 0x51),
65 RGB(0x51, 0x71, 0x51), RGB(0x51, 0x71, 0x59), RGB(0x51, 0x71, 0x61), RGB(0x51, 0x71, 0x69),
66 RGB(0x51, 0x71, 0x71), RGB(0x51, 0x69, 0x71), RGB(0x51, 0x61, 0x71), RGB(0x51, 0x59, 0x71),
67 RGB(0x00, 0x00, 0x41), RGB(0x10, 0x00, 0x41), RGB(0x20, 0x00, 0x41), RGB(0x31, 0x00, 0x41),
68 RGB(0x41, 0x00, 0x41), RGB(0x41, 0x00, 0x31), RGB(0x41, 0x00, 0x20), RGB(0x41, 0x00, 0x10),
69 RGB(0x41, 0x00, 0x00), RGB(0x41, 0x10, 0x00), RGB(0x41, 0x20, 0x00), RGB(0x41, 0x31, 0x00),
70 RGB(0x41, 0x41, 0x00), RGB(0x31, 0x41, 0x00), RGB(0x20, 0x41, 0x00), RGB(0x10, 0x41, 0x00),
71 RGB(0x00, 0x41, 0x00), RGB(0x00, 0x41, 0x10), RGB(0x00, 0x41, 0x20), RGB(0x00, 0x41, 0x31),
72 RGB(0x00, 0x41, 0x41), RGB(0x00, 0x31, 0x41), RGB(0x00, 0x20, 0x41), RGB(0x00, 0x10, 0x41),
73 RGB(0x20, 0x20, 0x41), RGB(0x28, 0x20, 0x41), RGB(0x31, 0x20, 0x41), RGB(0x39, 0x20, 0x41),
74 RGB(0x41, 0x20, 0x41), RGB(0x41, 0x20, 0x39), RGB(0x41, 0x20, 0x31), RGB(0x41, 0x20, 0x28),
75 RGB(0x41, 0x20, 0x20), RGB(0x41, 0x28, 0x20), RGB(0x41, 0x31, 0x20), RGB(0x41, 0x39, 0x20),
76 RGB(0x41, 0x41, 0x20), RGB(0x39, 0x41, 0x20), RGB(0x31, 0x41, 0x20), RGB(0x28, 0x41, 0x20),
77 RGB(0x20, 0x41, 0x20), RGB(0x20, 0x41, 0x28), RGB(0x20, 0x41, 0x31), RGB(0x20, 0x41, 0x39),
78 RGB(0x20, 0x41, 0x41), RGB(0x20, 0x39, 0x41), RGB(0x20, 0x31, 0x41), RGB(0x20, 0x28, 0x41),
79 RGB(0x2D, 0x2D, 0x41), RGB(0x31, 0x2D, 0x41), RGB(0x35, 0x2D, 0x41), RGB(0x3D, 0x2D, 0x41),
80 RGB(0x41, 0x2D, 0x41), RGB(0x41, 0x2D, 0x3D), RGB(0x41, 0x2D, 0x35), RGB(0x41, 0x2D, 0x31),
81 RGB(0x41, 0x2D, 0x2D), RGB(0x41, 0x31, 0x2D), RGB(0x41, 0x35, 0x2D), RGB(0x41, 0x3D, 0x2D),
82 RGB(0x41, 0x41, 0x2D), RGB(0x3D, 0x41, 0x2D), RGB(0x35, 0x41, 0x2D), RGB(0x31, 0x41, 0x2D),
83 RGB(0x2D, 0x41, 0x2D), RGB(0x2D, 0x41, 0x31), RGB(0x2D, 0x41, 0x35), RGB(0x2D, 0x41, 0x3D),
84 RGB(0x2D, 0x41, 0x41), RGB(0x2D, 0x3D, 0x41), RGB(0x2D, 0x35, 0x41), RGB(0x2D, 0x31, 0x41),
85 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00),
86 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00)
87 };
88
89 static BYTE VgaMemory[VGA_NUM_BANKS * VGA_BANK_SIZE];
90 static BYTE VgaLatchRegisters[VGA_NUM_BANKS] = {0, 0, 0, 0};
91 static BYTE VgaMiscRegister;
92 static BYTE VgaSeqIndex = VGA_SEQ_RESET_REG;
93 static BYTE VgaSeqRegisters[VGA_SEQ_MAX_REG];
94 static BYTE VgaGcIndex = VGA_GC_RESET_REG;
95 static BYTE VgaGcRegisters[VGA_GC_MAX_REG];
96 static BYTE VgaCrtcIndex = VGA_CRTC_HORZ_TOTAL_REG;
97 static BYTE VgaCrtcRegisters[VGA_CRTC_MAX_REG];
98 static BYTE VgaAcIndex = VGA_AC_PAL_0_REG;
99 static BOOLEAN VgaAcLatch = FALSE;
100 static BYTE VgaAcRegisters[VGA_AC_MAX_REG];
101 static WORD VgaDacIndex = 0;
102 static BOOLEAN VgaDacReadWrite = FALSE;
103 static BYTE VgaDacRegisters[VGA_PALETTE_SIZE];
104 static HPALETTE PaletteHandle = NULL;
105 static BOOLEAN InVerticalRetrace = FALSE;
106 static BOOLEAN InHorizontalRetrace = FALSE;
107 static HANDLE TextConsoleBuffer = NULL;
108 static HANDLE GraphicsConsoleBuffer = NULL;
109 static LPVOID ConsoleFramebuffer = NULL;
110 static HANDLE ConsoleMutex = NULL;
111 static BOOLEAN NeedsUpdate = FALSE;
112 static BOOLEAN ModeChanged = TRUE;
113 static BOOLEAN CursorMoved = FALSE;
114 static BOOLEAN PaletteChanged = FALSE;
115 static BOOLEAN TextMode = TRUE;
116 static SMALL_RECT UpdateRectangle = { 0, 0, 0, 0 };
117
118 /* PRIVATE FUNCTIONS **********************************************************/
119
120 static inline INT VgaGetAddressSize(VOID)
121 {
122 if (VgaCrtcRegisters[VGA_CRTC_UNDERLINE_REG] & VGA_CRTC_UNDERLINE_DWORD)
123 {
124 /* Double-word addressing */
125 return 4;
126 }
127
128 if (VgaCrtcRegisters[VGA_CRTC_MODE_CONTROL_REG] & VGA_CRTC_MODE_CONTROL_BYTE)
129 {
130 /* Byte addressing */
131 return 1;
132 }
133
134 /* Word addressing */
135 return 2;
136 }
137
138 static inline DWORD VgaTranslateReadAddress(DWORD Address)
139 {
140 DWORD Offset = Address - VgaGetVideoBaseAddress();
141 BYTE Plane;
142
143 /* Check for chain-4 and odd-even mode */
144 if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
145 {
146 /* The lowest two bits are the plane number */
147 Plane = Offset & 3;
148 Offset >>= 2;
149 }
150 else if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_OE)
151 {
152 /* The LSB is the plane number */
153 Plane = Offset & 1;
154 Offset >>= 1;
155 }
156 else
157 {
158 /* Use the read mode */
159 Plane = VgaGcRegisters[VGA_GC_READ_MAP_SEL_REG] & 0x03;
160 }
161
162 /* Multiply the offset by the address size */
163 Offset *= VgaGetAddressSize();
164
165 return Offset + Plane * VGA_BANK_SIZE;
166 }
167
168 static inline DWORD VgaTranslateWriteAddress(DWORD Address)
169 {
170 DWORD Offset = Address - VgaGetVideoBaseAddress();
171
172 /* Check for chain-4 and odd-even mode */
173 if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
174 {
175 /* Shift the offset to the right by 2 */
176 Offset >>= 2;
177 }
178 else if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_OE)
179 {
180 /* Shift the offset to the right by 1 */
181 Offset >>= 1;
182 }
183
184 /* Multiply the offset by the address size */
185 Offset *= VgaGetAddressSize();
186
187 /* Return the offset on plane 0 */
188 return Offset;
189 }
190
191 static inline BYTE VgaTranslateByteForWriting(BYTE Data, BYTE Plane)
192 {
193 BYTE WriteMode = VgaGcRegisters[VGA_GC_MODE_REG] & 3;
194 BYTE LogicalOperation = (VgaGcRegisters[VGA_GC_ROTATE_REG] >> 3) & 3;
195 BYTE RotateCount = VgaGcRegisters[VGA_GC_ROTATE_REG] & 7;
196 BYTE BitMask = VgaGcRegisters[VGA_GC_BITMASK_REG];
197
198 if (WriteMode == 1)
199 {
200 /* In write mode 1 just return the latch register */
201 return VgaLatchRegisters[Plane];
202 }
203
204 if (WriteMode != 2)
205 {
206 /* Write modes 0 and 3 rotate the data to the right first */
207 Data = LOBYTE(((DWORD)Data >> RotateCount) | ((DWORD)Data << (8 - RotateCount)));
208 }
209 else
210 {
211 /* Write mode 2 expands the appropriate bit to all 8 bits */
212 Data = (Data & (1 << Plane)) ? 0xFF : 0x00;
213 }
214
215 if (WriteMode == 0)
216 {
217 /*
218 * In write mode 0, the enable set/reset register decides if the
219 * set/reset bit should be expanded to all 8 bits.
220 */
221 if (VgaGcRegisters[VGA_GC_ENABLE_RESET_REG] & (1 << Plane))
222 {
223 /* Copy the bit from the set/reset register to all 8 bits */
224 Data = (VgaGcRegisters[VGA_GC_RESET_REG] & (1 << Plane)) ? 0xFF : 0x00;
225 }
226 }
227
228 if (WriteMode != 3)
229 {
230 /* Write modes 0 and 2 then perform a logical operation on the data and latch */
231 if (LogicalOperation == 1) Data &= VgaLatchRegisters[Plane];
232 else if (LogicalOperation == 2) Data |= VgaLatchRegisters[Plane];
233 else if (LogicalOperation == 3) Data ^= VgaLatchRegisters[Plane];
234 }
235 else
236 {
237 /* For write mode 3, we AND the bitmask with the data, which is used as the new bitmask */
238 BitMask &= Data;
239
240 /* Then we expand the bit in the set/reset field */
241 Data = (VgaGcRegisters[VGA_GC_RESET_REG] & (1 << Plane)) ? 0xFF : 0x00;
242 }
243
244 /* Bits cleared in the bitmask are replaced with latch register bits */
245 Data = (Data & BitMask) | (VgaLatchRegisters[Plane] & (~BitMask));
246
247 /* Return the byte */
248 return Data;
249 }
250
251 static inline VOID VgaMarkForUpdate(SHORT Row, SHORT Column)
252 {
253 DPRINT("VgaMarkForUpdate: Row %d, Column %d\n", Row, Column);
254
255 /* Check if this is the first time the rectangle is updated */
256 if (!NeedsUpdate)
257 {
258 UpdateRectangle.Left = UpdateRectangle.Top = SHRT_MAX;
259 UpdateRectangle.Right = UpdateRectangle.Bottom = SHRT_MIN;
260 }
261
262 /* Expand the rectangle to include the point */
263 UpdateRectangle.Left = min(UpdateRectangle.Left, Column);
264 UpdateRectangle.Right = max(UpdateRectangle.Right, Column);
265 UpdateRectangle.Top = min(UpdateRectangle.Top, Row);
266 UpdateRectangle.Bottom = max(UpdateRectangle.Bottom, Row);
267
268 /* Set the update request flag */
269 NeedsUpdate = TRUE;
270 }
271
272 static VOID VgaWriteSequencer(BYTE Data)
273 {
274 ASSERT(VgaSeqIndex < VGA_SEQ_MAX_REG);
275
276 /* Save the value */
277 VgaSeqRegisters[VgaSeqIndex] = Data;
278 }
279
280 static VOID VgaWriteGc(BYTE Data)
281 {
282 ASSERT(VgaGcIndex < VGA_GC_MAX_REG);
283
284 /* Save the value */
285 VgaGcRegisters[VgaGcIndex] = Data;
286
287 /* Check the index */
288 switch (VgaGcIndex)
289 {
290 case VGA_GC_MISC_REG:
291 {
292 /* The GC misc register decides if it's text or graphics mode */
293 ModeChanged = TRUE;
294
295 break;
296 }
297 }
298 }
299
300 static VOID VgaWriteCrtc(BYTE Data)
301 {
302 ASSERT(VgaGcIndex < VGA_CRTC_MAX_REG);
303
304 /* Save the value */
305 VgaCrtcRegisters[VgaCrtcIndex] = Data;
306
307 /* Check the index */
308 switch (VgaCrtcIndex)
309 {
310 case VGA_CRTC_END_HORZ_DISP_REG:
311 case VGA_CRTC_VERT_DISP_END_REG:
312 case VGA_CRTC_OVERFLOW_REG:
313 {
314 /* The video mode has changed */
315 ModeChanged = TRUE;
316
317 break;
318 }
319
320 case VGA_CRTC_CURSOR_LOC_LOW_REG:
321 case VGA_CRTC_CURSOR_LOC_HIGH_REG:
322 case VGA_CRTC_CURSOR_START_REG:
323 case VGA_CRTC_CURSOR_END_REG:
324 {
325 /* Set the cursor moved flag */
326 CursorMoved = TRUE;
327
328 break;
329 }
330 }
331 }
332
333 static VOID VgaWriteDac(BYTE Data)
334 {
335 INT PaletteIndex;
336 PALETTEENTRY Entry;
337
338 /* Set the value */
339 VgaDacRegisters[VgaDacIndex] = Data;
340
341 /* Find the palette index */
342 PaletteIndex = VgaDacIndex / 3;
343
344 /* Fill the entry structure */
345 Entry.peRed = VGA_DAC_TO_COLOR(VgaDacRegisters[PaletteIndex * 3]);
346 Entry.peGreen = VGA_DAC_TO_COLOR(VgaDacRegisters[PaletteIndex * 3 + 1]);
347 Entry.peBlue = VGA_DAC_TO_COLOR(VgaDacRegisters[PaletteIndex * 3 + 2]);
348 Entry.peFlags = 0;
349
350 /* Update the palette entry */
351 SetPaletteEntries(PaletteHandle, PaletteIndex, 1, &Entry);
352
353 /* Set the palette change flag */
354 PaletteChanged = TRUE;
355
356 /* Update the index */
357 VgaDacIndex++;
358 VgaDacIndex %= VGA_PALETTE_SIZE;
359 }
360
361 static VOID VgaWriteAc(BYTE Data)
362 {
363 ASSERT(VgaAcIndex < VGA_AC_MAX_REG);
364
365 /* Save the value */
366 VgaAcRegisters[VgaAcIndex] = Data;
367 }
368
369 static BOOL VgaEnterGraphicsMode(PCOORD Resolution)
370 {
371 DWORD i;
372 CONSOLE_GRAPHICS_BUFFER_INFO GraphicsBufferInfo;
373 BYTE BitmapInfoBuffer[VGA_BITMAP_INFO_SIZE];
374 LPBITMAPINFO BitmapInfo = (LPBITMAPINFO)BitmapInfoBuffer;
375 LPWORD PaletteIndex = (LPWORD)(BitmapInfo->bmiColors);
376
377 /* Fill the bitmap info header */
378 ZeroMemory(&BitmapInfo->bmiHeader, sizeof(BITMAPINFOHEADER));
379 BitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
380 BitmapInfo->bmiHeader.biWidth = Resolution->X;
381 BitmapInfo->bmiHeader.biHeight = Resolution->Y;
382 BitmapInfo->bmiHeader.biBitCount = 8;
383 BitmapInfo->bmiHeader.biPlanes = 1;
384 BitmapInfo->bmiHeader.biCompression = BI_RGB;
385 BitmapInfo->bmiHeader.biSizeImage = Resolution->X * Resolution->Y /* * 1 == biBitCount / 8 */;
386
387 /* Fill the palette data */
388 for (i = 0; i < (VGA_PALETTE_SIZE / 3); i++) PaletteIndex[i] = (WORD)i;
389
390 /* Fill the console graphics buffer info */
391 GraphicsBufferInfo.dwBitMapInfoLength = VGA_BITMAP_INFO_SIZE;
392 GraphicsBufferInfo.lpBitMapInfo = BitmapInfo;
393 GraphicsBufferInfo.dwUsage = DIB_PAL_COLORS;
394
395 /* Create the buffer */
396 GraphicsConsoleBuffer = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
397 FILE_SHARE_READ | FILE_SHARE_WRITE,
398 NULL,
399 CONSOLE_GRAPHICS_BUFFER,
400 &GraphicsBufferInfo);
401 if (GraphicsConsoleBuffer == INVALID_HANDLE_VALUE) return FALSE;
402
403 /* Save the framebuffer address and mutex */
404 ConsoleFramebuffer = GraphicsBufferInfo.lpBitMap;
405 ConsoleMutex = GraphicsBufferInfo.hMutex;
406
407 /* Clear the framebuffer */
408 ZeroMemory(ConsoleFramebuffer, BitmapInfo->bmiHeader.biSizeImage);
409
410 /* Set the active buffer */
411 SetConsoleActiveScreenBuffer(GraphicsConsoleBuffer);
412
413 /* Set the graphics mode palette */
414 SetConsolePalette(GraphicsConsoleBuffer,
415 PaletteHandle,
416 SYSPAL_NOSTATIC256);
417
418 /* Clear the text mode flag */
419 TextMode = FALSE;
420
421 return TRUE;
422 }
423
424 static VOID VgaLeaveGraphicsMode(VOID)
425 {
426 /* Release the console framebuffer mutex if needed */
427 ReleaseMutex(ConsoleMutex);
428
429 /* Switch back to the text buffer */
430 SetConsoleActiveScreenBuffer(TextConsoleBuffer);
431
432 /* Cleanup the video data */
433 CloseHandle(ConsoleMutex);
434 ConsoleMutex = NULL;
435 CloseHandle(GraphicsConsoleBuffer);
436 GraphicsConsoleBuffer = NULL;
437 }
438
439 static BOOL VgaEnterTextMode(PCOORD Resolution)
440 {
441 /* Resize the console */
442 SetConsoleScreenBufferSize(TextConsoleBuffer, *Resolution);
443
444 /* Allocate a framebuffer */
445 ConsoleFramebuffer = HeapAlloc(GetProcessHeap(),
446 HEAP_ZERO_MEMORY,
447 Resolution->X * Resolution->Y
448 * sizeof(CHAR_INFO));
449 if (ConsoleFramebuffer == NULL)
450 {
451 DisplayMessage(L"An unexpected error occurred!\n");
452 VdmRunning = FALSE;
453 return FALSE;
454 }
455
456 /* Set the text mode flag */
457 TextMode = TRUE;
458
459 return TRUE;
460 }
461
462 static VOID VgaLeaveTextMode(VOID)
463 {
464 /* Free the old framebuffer */
465 HeapFree(GetProcessHeap(), 0, ConsoleFramebuffer);
466 ConsoleFramebuffer = NULL;
467 }
468
469 static VOID VgaChangeMode(VOID)
470 {
471 COORD Resolution = VgaGetDisplayResolution();
472
473 /* Reset the mode change flag */
474 // ModeChanged = FALSE;
475
476 if (!TextMode)
477 {
478 /* Leave the current graphics mode */
479 VgaLeaveGraphicsMode();
480 }
481 else
482 {
483 /* Leave the current text mode */
484 VgaLeaveTextMode();
485 }
486
487 /* Check if the new mode is alphanumeric */
488 if (!(VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA))
489 {
490 /* Enter new text mode */
491 if (!VgaEnterTextMode(&Resolution)) return;
492 }
493 else
494 {
495 /* Enter 8-bit graphics mode */
496 if (!VgaEnterGraphicsMode(&Resolution)) return;
497 }
498
499 /* Trigger a full update of the screen */
500 NeedsUpdate = TRUE;
501 UpdateRectangle.Left = 0;
502 UpdateRectangle.Top = 0;
503 UpdateRectangle.Right = Resolution.X;
504 UpdateRectangle.Bottom = Resolution.Y;
505
506 /* Reset the mode change flag */
507 ModeChanged = FALSE;
508 }
509
510 static VOID VgaUpdateFramebuffer(VOID)
511 {
512 INT i, j, k;
513 COORD Resolution = VgaGetDisplayResolution();
514 INT AddressSize = VgaGetAddressSize();
515 DWORD Address = (VgaCrtcRegisters[VGA_CRTC_START_ADDR_HIGH_REG] << 8)
516 + VgaCrtcRegisters[VGA_CRTC_START_ADDR_LOW_REG];
517 DWORD ScanlineSize = (DWORD)VgaCrtcRegisters[VGA_CRTC_OFFSET_REG] * 2;
518
519 /* Check if this is text mode or graphics mode */
520 if (VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA)
521 {
522 /* Graphics mode */
523 PBYTE GraphicsBuffer = (PBYTE)ConsoleFramebuffer;
524
525 /*
526 * Synchronize access to the graphics framebuffer
527 * with the console framebuffer mutex.
528 */
529 WaitForSingleObject(ConsoleMutex, INFINITE);
530
531 /* Loop through the scanlines */
532 for (i = 0; i < Resolution.Y; i++)
533 {
534 /* Loop through the pixels */
535 for (j = 0; j < Resolution.X; j++)
536 {
537 BYTE PixelData = 0;
538
539 /* Check the shifting mode */
540 if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_SHIFT256)
541 {
542 /* 4 bits shifted from each plane */
543
544 /* Check if this is 16 or 256 color mode */
545 if (VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT)
546 {
547 /* One byte per pixel */
548 PixelData = VgaMemory[(j % VGA_NUM_BANKS) * VGA_BANK_SIZE
549 + (Address + (j / VGA_NUM_BANKS))
550 * AddressSize];
551 }
552 else
553 {
554 /* 4-bits per pixel */
555
556 PixelData = VgaMemory[(j % VGA_NUM_BANKS) * VGA_BANK_SIZE
557 + (Address + (j / (VGA_NUM_BANKS * 2)))
558 * AddressSize];
559
560 /* Check if we should use the highest 4 bits or lowest 4 */
561 if (((j / VGA_NUM_BANKS) % 2) == 0)
562 {
563 /* Highest 4 */
564 PixelData >>= 4;
565 }
566 else
567 {
568 /* Lowest 4 */
569 PixelData &= 0x0F;
570 }
571 }
572 }
573 else if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_SHIFTREG)
574 {
575 /* Check if this is 16 or 256 color mode */
576 if (VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT)
577 {
578 /*
579 * 2 bits shifted from plane 0 and 2 for the first 4 pixels,
580 * then 2 bits shifted from plane 1 and 3 for the next 4
581 */
582 BYTE LowPlaneData = VgaMemory[((j / 4) % 2) * VGA_BANK_SIZE
583 + (Address + (j / 4)) * AddressSize];
584 BYTE HighPlaneData = VgaMemory[(((j / 4) % 2) + 1) * VGA_BANK_SIZE
585 + (Address + (j / 4)) * AddressSize];
586
587 /* Extract the two bits from each plane */
588 LowPlaneData = (LowPlaneData >> (6 - ((j % 4) * 2))) & 3;
589 HighPlaneData = (HighPlaneData >> (6 - ((j % 4) * 2))) & 3;
590
591 /* Combine them into the pixel */
592 PixelData = LowPlaneData | (HighPlaneData << 2);
593 }
594 else
595 {
596 // TODO: NOT IMPLEMENTED
597 DPRINT1("8-bit interleaved mode is not implemented!\n");
598 }
599 }
600 else
601 {
602 /* 1 bit shifted from each plane */
603
604 /* Check if this is 16 or 256 color mode */
605 if (VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT)
606 {
607 /* 8 bits per pixel, 2 on each plane */
608
609 for (k = 0; k < VGA_NUM_BANKS; k++)
610 {
611 /* The data is on plane k, 4 pixels per byte */
612 BYTE PlaneData = VgaMemory[k * VGA_BANK_SIZE
613 + (Address + (j / 4)) * AddressSize];
614
615 /* The mask of the first bit in the pair */
616 BYTE BitMask = 1 << (((3 - (j % 4)) * 2) + 1);
617
618 /* Bits 0, 1, 2 and 3 come from the first bit of the pair */
619 if (PlaneData & BitMask) PixelData |= 1 << k;
620
621 /* Bits 4, 5, 6 and 7 come from the second bit of the pair */
622 if (PlaneData & (BitMask >> 1)) PixelData |= 1 << (k + 4);
623 }
624 }
625 else
626 {
627 /* 4 bits per pixel, 1 on each plane */
628
629 for (k = 0; k < VGA_NUM_BANKS; k++)
630 {
631 BYTE PlaneData = VgaMemory[k * VGA_BANK_SIZE
632 + (Address + (j / 8)) * AddressSize];
633
634 /* If the bit on that plane is set, set it */
635 if (PlaneData & (1 << (7 - (j % 8)))) PixelData |= 1 << k;
636 }
637 }
638 }
639
640 if (!(VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT))
641 {
642 /* In 16 color mode, the value is an index to the AC registers */
643 PixelData = VgaAcRegisters[PixelData];
644 }
645
646 /* Now check if the resulting pixel data has changed */
647 if (GraphicsBuffer[i * Resolution.X + j] != PixelData)
648 {
649 /* Yes, write the new value */
650 GraphicsBuffer[i * Resolution.X + j] = PixelData;
651
652 /* Mark the specified pixel as changed */
653 VgaMarkForUpdate(i, j);
654 }
655 }
656
657 /* Move to the next scanline */
658 Address += ScanlineSize;
659 }
660
661 /*
662 * Release the console framebuffer mutex
663 * so that we allow for repainting.
664 */
665 ReleaseMutex(ConsoleMutex);
666 }
667 else
668 {
669 /* Text mode */
670 PCHAR_INFO CharBuffer = (PCHAR_INFO)ConsoleFramebuffer;
671
672 /* Loop through the scanlines */
673 for (i = 0; i < Resolution.Y; i++)
674 {
675 /* Loop through the characters */
676 for (j = 0; j < Resolution.X; j++)
677 {
678 DWORD CurrentAddr = LOWORD((Address + j) * AddressSize);
679 CHAR_INFO CharInfo;
680
681 /* Plane 0 holds the character itself */
682 CharInfo.Char.AsciiChar = VgaMemory[CurrentAddr];
683
684 /* Plane 1 holds the attribute */
685 CharInfo.Attributes = VgaMemory[CurrentAddr + VGA_BANK_SIZE];
686
687 /* Now check if the resulting character data has changed */
688 if ((CharBuffer[i * Resolution.X + j].Char.AsciiChar != CharInfo.Char.AsciiChar)
689 || (CharBuffer[i * Resolution.X + j].Attributes != CharInfo.Attributes))
690 {
691 /* Yes, write the new value */
692 CharBuffer[i * Resolution.X + j] = CharInfo;
693
694 /* Mark the specified cell as changed */
695 VgaMarkForUpdate(i, j);
696 }
697 }
698
699 /* Move to the next scanline */
700 Address += ScanlineSize;
701 }
702 }
703 }
704
705 static VOID VgaUpdateTextCursor(VOID)
706 {
707 COORD Position;
708 CONSOLE_CURSOR_INFO CursorInfo;
709 BYTE CursorStart = VgaCrtcRegisters[VGA_CRTC_CURSOR_START_REG] & 0x3F;
710 BYTE CursorEnd = VgaCrtcRegisters[VGA_CRTC_CURSOR_END_REG] & 0x1F;
711 DWORD ScanlineSize = (DWORD)VgaCrtcRegisters[VGA_CRTC_OFFSET_REG] * 2;
712 BYTE TextSize = 1 + (VgaCrtcRegisters[VGA_CRTC_MAX_SCAN_LINE_REG] & 0x1F);
713 WORD Location = MAKEWORD(VgaCrtcRegisters[VGA_CRTC_CURSOR_LOC_LOW_REG],
714 VgaCrtcRegisters[VGA_CRTC_CURSOR_LOC_HIGH_REG]);
715
716 if (CursorStart < CursorEnd)
717 {
718 /* Visible cursor */
719 CursorInfo.bVisible = TRUE;
720 CursorInfo.dwSize = (100 * (CursorEnd - CursorStart)) / TextSize;
721 }
722 else
723 {
724 /* No cursor */
725 CursorInfo.bVisible = FALSE;
726 CursorInfo.dwSize = 0;
727 }
728
729 /* Add the cursor skew to the location */
730 Location += (VgaCrtcRegisters[VGA_CRTC_CURSOR_END_REG] >> 5) & 3;
731
732 /* Find the coordinates of the new position */
733 Position.X = (WORD)(Location % ScanlineSize);
734 Position.Y = (WORD)(Location / ScanlineSize);
735
736 /* Update the physical cursor */
737 SetConsoleCursorInfo(TextConsoleBuffer, &CursorInfo);
738 SetConsoleCursorPosition(TextConsoleBuffer, Position);
739
740 /* Reset the cursor move flag */
741 CursorMoved = FALSE;
742 }
743
744 /* PUBLIC FUNCTIONS ***********************************************************/
745
746 DWORD VgaGetVideoBaseAddress(VOID)
747 {
748 return MemoryBase[(VgaGcRegisters[VGA_GC_MISC_REG] >> 2) & 0x03];
749 }
750
751 DWORD VgaGetVideoLimitAddress(VOID)
752 {
753 return MemoryLimit[(VgaGcRegisters[VGA_GC_MISC_REG] >> 2) & 0x03];
754 }
755
756 COORD VgaGetDisplayResolution(VOID)
757 {
758 COORD Resolution;
759 BYTE MaximumScanLine = 1 + (VgaCrtcRegisters[VGA_CRTC_MAX_SCAN_LINE_REG] & 0x1F);
760
761 /* The low 8 bits are in the display registers */
762 Resolution.X = VgaCrtcRegisters[VGA_CRTC_END_HORZ_DISP_REG];
763 Resolution.Y = VgaCrtcRegisters[VGA_CRTC_VERT_DISP_END_REG];
764
765 /* Set the top bits from the overflow register */
766 if (VgaCrtcRegisters[VGA_CRTC_OVERFLOW_REG] & VGA_CRTC_OVERFLOW_VDE8)
767 {
768 Resolution.Y |= 1 << 8;
769 }
770 if (VgaCrtcRegisters[VGA_CRTC_OVERFLOW_REG] & VGA_CRTC_OVERFLOW_VDE9)
771 {
772 Resolution.Y |= 1 << 9;
773 }
774
775 /* Increase the values by 1 */
776 Resolution.X++;
777 Resolution.Y++;
778
779 if (VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA)
780 {
781 /* Multiply the horizontal resolution by the 9/8 dot mode */
782 Resolution.X *= (VgaSeqRegisters[VGA_SEQ_CLOCK_REG] & VGA_SEQ_CLOCK_98DM)
783 ? 8 : 9;
784
785 /* The horizontal resolution is halved in 8-bit mode */
786 if (VgaAcRegisters[VGA_AC_CONTROL_REG] & VGA_AC_CONTROL_8BIT) Resolution.X /= 2;
787 }
788
789 /* Divide the vertical resolution by the maximum scan line (== font size in text mode) */
790 Resolution.Y /= MaximumScanLine;
791
792 /* Return the resolution */
793 return Resolution;
794 }
795
796 VOID VgaRefreshDisplay(VOID)
797 {
798 COORD Resolution = VgaGetDisplayResolution();
799
800 DPRINT("VgaRefreshDisplay\n");
801
802 /* Change the display mode */
803 if (ModeChanged) VgaChangeMode();
804
805 /* Change the text cursor location */
806 if (CursorMoved) VgaUpdateTextCursor();
807
808 if (PaletteChanged)
809 {
810 if (VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA)
811 {
812 /* Trigger a full update of the screen */
813 NeedsUpdate = TRUE;
814 UpdateRectangle.Left = 0;
815 UpdateRectangle.Top = 0;
816 UpdateRectangle.Right = Resolution.X;
817 UpdateRectangle.Bottom = Resolution.Y;
818 }
819
820 PaletteChanged = FALSE;
821 }
822
823 /* Update the contents of the framebuffer */
824 VgaUpdateFramebuffer();
825
826 /* Set the vertical retrace flag */
827 InVerticalRetrace = TRUE;
828
829 /* Ignore if there's nothing to update */
830 if (!NeedsUpdate) return;
831
832 DPRINT("Updating screen rectangle (%d, %d, %d, %d)\n",
833 UpdateRectangle.Left,
834 UpdateRectangle.Top,
835 UpdateRectangle.Right,
836 UpdateRectangle.Bottom);
837
838 /* Check if this is text mode or graphics mode */
839 if (VgaGcRegisters[VGA_GC_MISC_REG] & VGA_GC_MISC_NOALPHA)
840 {
841 /* Graphics mode */
842
843 /* Redraw the screen */
844 InvalidateConsoleDIBits(GraphicsConsoleBuffer, &UpdateRectangle);
845 }
846 else
847 {
848 /* Text mode */
849 COORD Origin = { UpdateRectangle.Left, UpdateRectangle.Top };
850
851 /* Write the data to the console */
852 WriteConsoleOutputA(TextConsoleBuffer,
853 (PCHAR_INFO)ConsoleFramebuffer,
854 Resolution,
855 Origin,
856 &UpdateRectangle);
857
858 }
859
860 /* Clear the update flag */
861 NeedsUpdate = FALSE;
862 }
863
864 VOID VgaHorizontalRetrace(VOID)
865 {
866 /* Set the flag */
867 InHorizontalRetrace = TRUE;
868 }
869
870 VOID VgaReadMemory(DWORD Address, LPBYTE Buffer, DWORD Size)
871 {
872 DWORD i;
873 DWORD VideoAddress;
874
875 DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n",
876 Address,
877 Size);
878
879 /* Ignore if video RAM access is disabled */
880 if (!(VgaMiscRegister & VGA_MISC_RAM_ENABLED)) return;
881
882 /* Loop through each byte */
883 for (i = 0; i < Size; i++)
884 {
885 VideoAddress = VgaTranslateReadAddress(Address + i);
886
887 /* Load the latch registers */
888 VgaLatchRegisters[0] = VgaMemory[LOWORD(VideoAddress)];
889 VgaLatchRegisters[1] = VgaMemory[VGA_BANK_SIZE + LOWORD(VideoAddress)];
890 VgaLatchRegisters[2] = VgaMemory[(2 * VGA_BANK_SIZE) + LOWORD(VideoAddress)];
891 VgaLatchRegisters[3] = VgaMemory[(3 * VGA_BANK_SIZE) + LOWORD(VideoAddress)];
892
893 /* Copy the value to the buffer */
894 Buffer[i] = VgaMemory[VideoAddress];
895 }
896 }
897
898 VOID VgaWriteMemory(DWORD Address, LPBYTE Buffer, DWORD Size)
899 {
900 DWORD i, j;
901 DWORD VideoAddress;
902
903 DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n",
904 Address,
905 Size);
906
907 /* Ignore if video RAM access is disabled */
908 if (!(VgaMiscRegister & VGA_MISC_RAM_ENABLED)) return;
909
910 /* Also ignore if write access to all planes is disabled */
911 if ((VgaSeqRegisters[VGA_SEQ_MASK_REG] & 0x0F) == 0x00) return;
912
913 /* Loop through each byte */
914 for (i = 0; i < Size; i++)
915 {
916 VideoAddress = VgaTranslateWriteAddress(Address + i);
917
918 for (j = 0; j < VGA_NUM_BANKS; j++)
919 {
920 /* Make sure the page is writeable */
921 if (!(VgaSeqRegisters[VGA_SEQ_MASK_REG] & (1 << j))) continue;
922
923 /* Check if this is chain-4 mode */
924 if (VgaSeqRegisters[VGA_SEQ_MEM_REG] & VGA_SEQ_MEM_C4)
925 {
926 if (((Address + i) & 3) != j)
927 {
928 /* This plane will not be accessed */
929 continue;
930 }
931 }
932
933 /* Check if this is odd-even mode */
934 if (VgaGcRegisters[VGA_GC_MODE_REG] & VGA_GC_MODE_OE)
935 {
936 if (((Address + i) & 1) != (j & 1))
937 {
938 /* This plane will not be accessed */
939 continue;
940 }
941 }
942
943 /* Copy the value to the VGA memory */
944 VgaMemory[VideoAddress + j * VGA_BANK_SIZE] = VgaTranslateByteForWriting(Buffer[i], j);
945 }
946 }
947 }
948
949 BYTE VgaReadPort(WORD Port)
950 {
951 DPRINT("VgaReadPort: Port 0x%04X\n", Port);
952
953 switch (Port)
954 {
955 case VGA_AC_INDEX:
956 {
957 return VgaAcIndex;
958 }
959
960 case VGA_AC_READ:
961 {
962 return VgaAcRegisters[VgaAcIndex];
963 }
964
965 case VGA_SEQ_INDEX:
966 {
967 return VgaSeqIndex;
968 }
969
970 case VGA_SEQ_DATA:
971 {
972 return VgaSeqRegisters[VgaSeqIndex];
973 }
974
975 case VGA_DAC_READ_INDEX:
976 {
977 /* This returns the read/write state */
978 return VgaDacReadWrite ? 0 : 3;
979 }
980
981 case VGA_DAC_WRITE_INDEX:
982 {
983 return VgaDacIndex / 3;
984 }
985
986 case VGA_DAC_DATA:
987 {
988 /* Ignore reads in write mode */
989 if (!VgaDacReadWrite)
990 {
991 BYTE Data = VgaDacRegisters[VgaDacIndex++];
992 VgaDacIndex %= VGA_PALETTE_SIZE;
993 return Data;
994 }
995
996 break;
997 }
998
999 case VGA_MISC_READ:
1000 {
1001 return VgaMiscRegister;
1002 }
1003
1004 case VGA_CRTC_INDEX:
1005 {
1006 return VgaCrtcIndex;
1007 }
1008
1009 case VGA_CRTC_DATA:
1010 {
1011 return VgaCrtcRegisters[VgaCrtcIndex];
1012 }
1013
1014 case VGA_GC_INDEX:
1015 {
1016 return VgaGcIndex;
1017 }
1018
1019 case VGA_GC_DATA:
1020 {
1021 return VgaGcRegisters[VgaGcIndex];
1022 }
1023
1024 case VGA_STAT_MONO:
1025 case VGA_STAT_COLOR:
1026 {
1027 BYTE Result = 0;
1028
1029 /* Reset the AC latch */
1030 VgaAcLatch = FALSE;
1031
1032 /* Set a flag if there is a vertical or horizontal retrace */
1033 if (InVerticalRetrace || InHorizontalRetrace) Result |= VGA_STAT_DD;
1034
1035 /* Set an additional flag if there was a vertical retrace */
1036 if (InVerticalRetrace) Result |= VGA_STAT_VRETRACE;
1037
1038 /* Clear the flags */
1039 InHorizontalRetrace = InVerticalRetrace = FALSE;
1040
1041 return Result;
1042 }
1043 }
1044
1045 return 0;
1046 }
1047
1048 VOID VgaWritePort(WORD Port, BYTE Data)
1049 {
1050 DPRINT("VgaWritePort: Port 0x%04X, Data 0x%02X\n", Port, Data);
1051
1052 switch (Port)
1053 {
1054 case VGA_AC_INDEX:
1055 {
1056 if (!VgaAcLatch)
1057 {
1058 /* Change the index */
1059 if (Data < VGA_AC_MAX_REG) VgaAcIndex = Data;
1060 }
1061 else
1062 {
1063 /* Write the data */
1064 VgaWriteAc(Data);
1065 }
1066
1067 /* Toggle the latch */
1068 VgaAcLatch = !VgaAcLatch;
1069
1070 break;
1071 }
1072
1073 case VGA_SEQ_INDEX:
1074 {
1075 /* Set the sequencer index register */
1076 if (Data < VGA_SEQ_MAX_REG) VgaSeqIndex = Data;
1077
1078 break;
1079 }
1080
1081 case VGA_SEQ_DATA:
1082 {
1083 /* Call the sequencer function */
1084 VgaWriteSequencer(Data);
1085
1086 break;
1087 }
1088
1089 case VGA_DAC_READ_INDEX:
1090 {
1091 VgaDacReadWrite = FALSE;
1092 VgaDacIndex = Data * 3;
1093
1094 break;
1095 }
1096
1097 case VGA_DAC_WRITE_INDEX:
1098 {
1099 VgaDacReadWrite = TRUE;
1100 VgaDacIndex = Data * 3;
1101
1102 break;
1103 }
1104
1105 case VGA_DAC_DATA:
1106 {
1107 /* Ignore writes in read mode */
1108 if (VgaDacReadWrite) VgaWriteDac(Data & 0x3F);
1109
1110 break;
1111 }
1112
1113 case VGA_MISC_WRITE:
1114 {
1115 VgaMiscRegister = Data;
1116
1117 break;
1118 }
1119
1120 case VGA_CRTC_INDEX:
1121 {
1122 /* Set the CRTC index register */
1123 if (Data < VGA_CRTC_MAX_REG) VgaCrtcIndex = Data;
1124
1125 break;
1126 }
1127
1128 case VGA_CRTC_DATA:
1129 {
1130 /* Call the CRTC function */
1131 VgaWriteCrtc(Data);
1132
1133 break;
1134 }
1135
1136 case VGA_GC_INDEX:
1137 {
1138 /* Set the GC index register */
1139 if (Data < VGA_GC_MAX_REG) VgaGcIndex = Data;
1140 break;
1141 }
1142
1143 case VGA_GC_DATA:
1144 {
1145 /* Call the GC function */
1146 VgaWriteGc(Data);
1147
1148 break;
1149 }
1150 }
1151 }
1152
1153 VOID VgaClearMemory(VOID)
1154 {
1155 ZeroMemory(VgaMemory, sizeof(VgaMemory));
1156 }
1157
1158 BOOLEAN VgaInitialize(HANDLE TextHandle)
1159 {
1160 INT i, j;
1161 COORD Resolution;
1162 INT AddressSize;
1163 DWORD ScanlineSize;
1164 COORD Origin = { 0, 0 };
1165 SMALL_RECT ScreenRect;
1166 PCHAR_INFO CharBuffer;
1167 DWORD Address = 0;
1168 DWORD CurrentAddr;
1169 LPLOGPALETTE Palette;
1170
1171 /* Set the global handle */
1172 TextConsoleBuffer = TextHandle;
1173
1174 /* Clear the VGA memory */
1175 VgaClearMemory();
1176
1177 /* Set the default video mode */
1178 BiosSetVideoMode(BIOS_DEFAULT_VIDEO_MODE);
1179 VgaChangeMode();
1180
1181 /* Get the data */
1182 Resolution = VgaGetDisplayResolution();
1183 CharBuffer = (PCHAR_INFO)ConsoleFramebuffer;
1184 AddressSize = VgaGetAddressSize();
1185 ScreenRect.Left = ScreenRect.Top = 0;
1186 ScreenRect.Right = Resolution.X;
1187 ScreenRect.Bottom = Resolution.Y;
1188 ScanlineSize = (DWORD)VgaCrtcRegisters[VGA_CRTC_OFFSET_REG] * 2;
1189
1190 /* Read the data from the console into the framebuffer */
1191 ReadConsoleOutputA(TextConsoleBuffer,
1192 CharBuffer,
1193 Resolution,
1194 Origin,
1195 &ScreenRect);
1196
1197 /* Loop through the scanlines */
1198 for (i = 0; i < Resolution.Y; i++)
1199 {
1200 /* Loop through the characters */
1201 for (j = 0; j < Resolution.X; j++)
1202 {
1203 CurrentAddr = LOWORD((Address + j) * AddressSize);
1204
1205 /* Store the character in plane 0 */
1206 VgaMemory[CurrentAddr] = CharBuffer[i * Resolution.X + j].Char.AsciiChar;
1207
1208 /* Store the attribute in plane 1 */
1209 VgaMemory[CurrentAddr + VGA_BANK_SIZE] = (BYTE)CharBuffer[i * Resolution.X + j].Attributes;
1210 }
1211
1212 /* Move to the next scanline */
1213 Address += ScanlineSize;
1214 }
1215
1216 /* Allocate storage space for the palette */
1217 Palette = (LPLOGPALETTE)HeapAlloc(GetProcessHeap(),
1218 HEAP_ZERO_MEMORY,
1219 sizeof(LOGPALETTE)
1220 + VGA_MAX_COLORS * sizeof(PALETTEENTRY));
1221 if (Palette == NULL) return FALSE;
1222
1223 /* Initialize the palette */
1224 Palette->palVersion = 0x0300;
1225 Palette->palNumEntries = VGA_MAX_COLORS;
1226
1227 /* Copy the colors of the default palette to the DAC and console palette */
1228 for (i = 0; i < VGA_MAX_COLORS; i++)
1229 {
1230 /* Set the palette entries */
1231 Palette->palPalEntry[i].peRed = GetRValue(VgaDefaultPalette[i]);
1232 Palette->palPalEntry[i].peGreen = GetGValue(VgaDefaultPalette[i]);
1233 Palette->palPalEntry[i].peBlue = GetBValue(VgaDefaultPalette[i]);
1234 Palette->palPalEntry[i].peFlags = 0;
1235
1236 /* Set the DAC registers */
1237 VgaDacRegisters[i * 3] = VGA_COLOR_TO_DAC(GetRValue(VgaDefaultPalette[i]));
1238 VgaDacRegisters[i * 3 + 1] = VGA_COLOR_TO_DAC(GetGValue(VgaDefaultPalette[i]));
1239 VgaDacRegisters[i * 3 + 2] = VGA_COLOR_TO_DAC(GetBValue(VgaDefaultPalette[i]));
1240 }
1241
1242 /* Create the palette */
1243 PaletteHandle = CreatePalette(Palette);
1244
1245 /* Free the palette */
1246 HeapFree(GetProcessHeap(), 0, Palette);
1247
1248 /* Return success if the palette was successfully created */
1249 return (PaletteHandle ? TRUE : FALSE);
1250 }
1251
1252 /* EOF */