2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: VGA hardware emulation
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
16 /* PRIVATE VARIABLES **********************************************************/
18 static CONST DWORD MemoryBase
[] = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
19 static CONST DWORD MemoryLimit
[] = { 0xAFFFF, 0xAFFFF, 0xB7FFF, 0xBFFFF };
21 static CONST COLORREF VgaDefaultPalette
[VGA_MAX_COLORS
] =
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)
89 static BYTE VgaMemory
[VGA_NUM_BANKS
* VGA_BANK_SIZE
];
90 static LPVOID ConsoleFramebuffer
= NULL
;
91 static HANDLE TextConsoleBuffer
= NULL
;
92 static HANDLE GraphicsConsoleBuffer
= NULL
;
93 static HANDLE ConsoleMutex
= NULL
;
94 static HPALETTE PaletteHandle
= NULL
;
95 static BOOLEAN DoubleVision
= FALSE
;
97 static BYTE VgaLatchRegisters
[VGA_NUM_BANKS
] = {0, 0, 0, 0};
98 static BYTE VgaMiscRegister
;
100 static BYTE VgaSeqIndex
= VGA_SEQ_RESET_REG
;
101 static BYTE VgaSeqRegisters
[VGA_SEQ_MAX_REG
];
103 static BYTE VgaGcIndex
= VGA_GC_RESET_REG
;
104 static BYTE VgaGcRegisters
[VGA_GC_MAX_REG
];
106 static BYTE VgaCrtcIndex
= VGA_CRTC_HORZ_TOTAL_REG
;
107 static BYTE VgaCrtcRegisters
[VGA_CRTC_MAX_REG
];
109 static BYTE VgaAcIndex
= VGA_AC_PAL_0_REG
;
110 static BOOLEAN VgaAcLatch
= FALSE
;
111 static BYTE VgaAcRegisters
[VGA_AC_MAX_REG
];
113 static WORD VgaDacIndex
= 0;
114 static BOOLEAN VgaDacReadWrite
= FALSE
;
115 static BYTE VgaDacRegisters
[VGA_PALETTE_SIZE
];
117 static BOOLEAN InVerticalRetrace
= FALSE
;
118 static BOOLEAN InHorizontalRetrace
= FALSE
;
120 static BOOLEAN NeedsUpdate
= FALSE
;
121 static BOOLEAN ModeChanged
= TRUE
;
122 static BOOLEAN CursorMoved
= FALSE
;
123 static BOOLEAN PaletteChanged
= FALSE
;
124 static BOOLEAN TextMode
= TRUE
;
126 static SMALL_RECT UpdateRectangle
= { 0, 0, 0, 0 };
128 /* PRIVATE FUNCTIONS **********************************************************/
130 static inline INT
VgaGetAddressSize(VOID
)
132 if (VgaCrtcRegisters
[VGA_CRTC_UNDERLINE_REG
] & VGA_CRTC_UNDERLINE_DWORD
)
134 /* Double-word addressing */
135 return 4; // sizeof(DWORD)
138 if (VgaCrtcRegisters
[VGA_CRTC_MODE_CONTROL_REG
] & VGA_CRTC_MODE_CONTROL_BYTE
)
140 /* Byte addressing */
141 return 1; // sizeof(BYTE)
144 /* Word addressing */
145 return 2; // sizeof(WORD)
148 static inline DWORD
VgaTranslateReadAddress(DWORD Address
)
150 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
153 /* Check for chain-4 and odd-even mode */
154 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
156 /* The lowest two bits are the plane number */
160 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
162 /* The LSB is the plane number */
168 /* Use the read mode */
169 Plane
= VgaGcRegisters
[VGA_GC_READ_MAP_SEL_REG
] & 0x03;
172 /* Multiply the offset by the address size */
173 Offset
*= VgaGetAddressSize();
175 return Offset
+ Plane
* VGA_BANK_SIZE
;
178 static inline DWORD
VgaTranslateWriteAddress(DWORD Address
)
180 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
182 /* Check for chain-4 and odd-even mode */
183 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
185 /* Shift the offset to the right by 2 */
188 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
190 /* Shift the offset to the right by 1 */
194 /* Multiply the offset by the address size */
195 Offset
*= VgaGetAddressSize();
197 /* Return the offset on plane 0 */
201 static inline BYTE
VgaTranslateByteForWriting(BYTE Data
, BYTE Plane
)
203 BYTE WriteMode
= VgaGcRegisters
[VGA_GC_MODE_REG
] & 3;
204 BYTE LogicalOperation
= (VgaGcRegisters
[VGA_GC_ROTATE_REG
] >> 3) & 3;
205 BYTE RotateCount
= VgaGcRegisters
[VGA_GC_ROTATE_REG
] & 7;
206 BYTE BitMask
= VgaGcRegisters
[VGA_GC_BITMASK_REG
];
210 /* In write mode 1 just return the latch register */
211 return VgaLatchRegisters
[Plane
];
216 /* Write modes 0 and 3 rotate the data to the right first */
217 Data
= LOBYTE(((DWORD
)Data
>> RotateCount
) | ((DWORD
)Data
<< (8 - RotateCount
)));
221 /* Write mode 2 expands the appropriate bit to all 8 bits */
222 Data
= (Data
& (1 << Plane
)) ? 0xFF : 0x00;
228 * In write mode 0, the enable set/reset register decides if the
229 * set/reset bit should be expanded to all 8 bits.
231 if (VgaGcRegisters
[VGA_GC_ENABLE_RESET_REG
] & (1 << Plane
))
233 /* Copy the bit from the set/reset register to all 8 bits */
234 Data
= (VgaGcRegisters
[VGA_GC_RESET_REG
] & (1 << Plane
)) ? 0xFF : 0x00;
240 /* Write modes 0 and 2 then perform a logical operation on the data and latch */
241 if (LogicalOperation
== 1) Data
&= VgaLatchRegisters
[Plane
];
242 else if (LogicalOperation
== 2) Data
|= VgaLatchRegisters
[Plane
];
243 else if (LogicalOperation
== 3) Data
^= VgaLatchRegisters
[Plane
];
247 /* For write mode 3, we AND the bitmask with the data, which is used as the new bitmask */
250 /* Then we expand the bit in the set/reset field */
251 Data
= (VgaGcRegisters
[VGA_GC_RESET_REG
] & (1 << Plane
)) ? 0xFF : 0x00;
254 /* Bits cleared in the bitmask are replaced with latch register bits */
255 Data
= (Data
& BitMask
) | (VgaLatchRegisters
[Plane
] & (~BitMask
));
257 /* Return the byte */
261 static inline VOID
VgaMarkForUpdate(SHORT Row
, SHORT Column
)
263 DPRINT("VgaMarkForUpdate: Row %d, Column %d\n", Row
, Column
);
265 /* Check if this is the first time the rectangle is updated */
268 UpdateRectangle
.Left
= UpdateRectangle
.Top
= MAXSHORT
;
269 UpdateRectangle
.Right
= UpdateRectangle
.Bottom
= MINSHORT
;
272 /* Expand the rectangle to include the point */
273 UpdateRectangle
.Left
= min(UpdateRectangle
.Left
, Column
);
274 UpdateRectangle
.Right
= max(UpdateRectangle
.Right
, Column
);
275 UpdateRectangle
.Top
= min(UpdateRectangle
.Top
, Row
);
276 UpdateRectangle
.Bottom
= max(UpdateRectangle
.Bottom
, Row
);
278 /* Set the update request flag */
282 static VOID
VgaWriteSequencer(BYTE Data
)
284 ASSERT(VgaSeqIndex
< VGA_SEQ_MAX_REG
);
287 VgaSeqRegisters
[VgaSeqIndex
] = Data
;
290 static VOID
VgaWriteGc(BYTE Data
)
292 ASSERT(VgaGcIndex
< VGA_GC_MAX_REG
);
295 VgaGcRegisters
[VgaGcIndex
] = Data
;
297 /* Check the index */
300 case VGA_GC_MISC_REG
:
302 /* The GC misc register decides if it's text or graphics mode */
310 static VOID
VgaWriteCrtc(BYTE Data
)
312 ASSERT(VgaGcIndex
< VGA_CRTC_MAX_REG
);
315 VgaCrtcRegisters
[VgaCrtcIndex
] = Data
;
317 /* Check the index */
318 switch (VgaCrtcIndex
)
320 case VGA_CRTC_END_HORZ_DISP_REG
:
321 case VGA_CRTC_VERT_DISP_END_REG
:
322 case VGA_CRTC_OVERFLOW_REG
:
324 /* The video mode has changed */
330 case VGA_CRTC_CURSOR_LOC_LOW_REG
:
331 case VGA_CRTC_CURSOR_LOC_HIGH_REG
:
332 case VGA_CRTC_CURSOR_START_REG
:
333 case VGA_CRTC_CURSOR_END_REG
:
335 /* Set the cursor moved flag */
343 static VOID
VgaWriteDac(BYTE Data
)
349 VgaDacRegisters
[VgaDacIndex
] = Data
;
351 /* Find the palette index */
352 PaletteIndex
= VgaDacIndex
/ 3;
354 /* Fill the entry structure */
355 Entry
.peRed
= VGA_DAC_TO_COLOR(VgaDacRegisters
[PaletteIndex
* 3]);
356 Entry
.peGreen
= VGA_DAC_TO_COLOR(VgaDacRegisters
[PaletteIndex
* 3 + 1]);
357 Entry
.peBlue
= VGA_DAC_TO_COLOR(VgaDacRegisters
[PaletteIndex
* 3 + 2]);
360 /* Update the palette entry */
361 SetPaletteEntries(PaletteHandle
, PaletteIndex
, 1, &Entry
);
363 /* Set the palette change flag */
364 PaletteChanged
= TRUE
;
366 /* Update the index */
368 VgaDacIndex
%= VGA_PALETTE_SIZE
;
371 static VOID
VgaWriteAc(BYTE Data
)
373 ASSERT(VgaAcIndex
< VGA_AC_MAX_REG
);
376 VgaAcRegisters
[VgaAcIndex
] = Data
;
378 if (VgaAcIndex
<= VGA_AC_PAL_F_REG
)
380 /* Set the palette change flag */
381 PaletteChanged
= TRUE
;
385 static VOID
VgaRestoreDefaultPalette(PPALETTEENTRY Entries
, USHORT NumOfEntries
)
389 /* Copy the colors of the default palette to the DAC and console palette */
390 for (i
= 0; i
< NumOfEntries
; i
++)
392 /* Set the palette entries */
393 Entries
[i
].peRed
= GetRValue(VgaDefaultPalette
[i
]);
394 Entries
[i
].peGreen
= GetGValue(VgaDefaultPalette
[i
]);
395 Entries
[i
].peBlue
= GetBValue(VgaDefaultPalette
[i
]);
396 Entries
[i
].peFlags
= 0;
398 /* Set the DAC registers */
399 VgaDacRegisters
[i
* 3] = VGA_COLOR_TO_DAC(GetRValue(VgaDefaultPalette
[i
]));
400 VgaDacRegisters
[i
* 3 + 1] = VGA_COLOR_TO_DAC(GetGValue(VgaDefaultPalette
[i
]));
401 VgaDacRegisters
[i
* 3 + 2] = VGA_COLOR_TO_DAC(GetBValue(VgaDefaultPalette
[i
]));
405 static BOOLEAN
VgaInitializePalette(VOID
)
407 LPLOGPALETTE Palette
;
409 /* Allocate storage space for the palette */
410 Palette
= (LPLOGPALETTE
)HeapAlloc(GetProcessHeap(),
413 VGA_MAX_COLORS
* sizeof(PALETTEENTRY
));
414 if (Palette
== NULL
) return FALSE
;
416 /* Initialize the palette */
417 Palette
->palVersion
= 0x0300;
418 Palette
->palNumEntries
= VGA_MAX_COLORS
;
420 /* Restore the default palette */
421 VgaRestoreDefaultPalette(Palette
->palPalEntry
, Palette
->palNumEntries
);
423 /* Create the palette */
424 PaletteHandle
= CreatePalette(Palette
);
426 /* Free the palette */
427 HeapFree(GetProcessHeap(), 0, Palette
);
429 /* Fail if the palette wasn't successfully created... */
430 if (PaletteHandle
== NULL
) return FALSE
;
432 /* ... otherwise return success */
436 static BOOL
VgaEnterGraphicsMode(PCOORD Resolution
)
439 CONSOLE_GRAPHICS_BUFFER_INFO GraphicsBufferInfo
;
440 BYTE BitmapInfoBuffer
[VGA_BITMAP_INFO_SIZE
];
441 LPBITMAPINFO BitmapInfo
= (LPBITMAPINFO
)BitmapInfoBuffer
;
442 LPWORD PaletteIndex
= (LPWORD
)(BitmapInfo
->bmiColors
);
444 if ((Resolution
->X
< VGA_MINIMUM_WIDTH
) && (Resolution
->Y
< VGA_MINIMUM_HEIGHT
))
451 /* Fill the bitmap info header */
452 ZeroMemory(&BitmapInfo
->bmiHeader
, sizeof(BITMAPINFOHEADER
));
453 BitmapInfo
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
454 BitmapInfo
->bmiHeader
.biWidth
= Resolution
->X
;
455 BitmapInfo
->bmiHeader
.biHeight
= Resolution
->Y
;
456 BitmapInfo
->bmiHeader
.biBitCount
= 8;
457 BitmapInfo
->bmiHeader
.biPlanes
= 1;
458 BitmapInfo
->bmiHeader
.biCompression
= BI_RGB
;
459 BitmapInfo
->bmiHeader
.biSizeImage
= Resolution
->X
* Resolution
->Y
/* * 1 == biBitCount / 8 */;
461 /* Fill the palette data */
462 for (i
= 0; i
< (VGA_PALETTE_SIZE
/ 3); i
++) PaletteIndex
[i
] = (WORD
)i
;
464 /* Fill the console graphics buffer info */
465 GraphicsBufferInfo
.dwBitMapInfoLength
= VGA_BITMAP_INFO_SIZE
;
466 GraphicsBufferInfo
.lpBitMapInfo
= BitmapInfo
;
467 GraphicsBufferInfo
.dwUsage
= DIB_PAL_COLORS
;
469 /* Create the buffer */
470 GraphicsConsoleBuffer
= CreateConsoleScreenBuffer(GENERIC_READ
| GENERIC_WRITE
,
471 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
473 CONSOLE_GRAPHICS_BUFFER
,
474 &GraphicsBufferInfo
);
475 if (GraphicsConsoleBuffer
== INVALID_HANDLE_VALUE
) return FALSE
;
477 /* Save the framebuffer address and mutex */
478 ConsoleFramebuffer
= GraphicsBufferInfo
.lpBitMap
;
479 ConsoleMutex
= GraphicsBufferInfo
.hMutex
;
481 /* Clear the framebuffer */
482 ZeroMemory(ConsoleFramebuffer
, BitmapInfo
->bmiHeader
.biSizeImage
);
484 /* Set the active buffer */
485 SetConsoleActiveScreenBuffer(GraphicsConsoleBuffer
);
487 /* Set the graphics mode palette */
488 SetConsolePalette(GraphicsConsoleBuffer
,
492 /* Clear the text mode flag */
498 static VOID
VgaLeaveGraphicsMode(VOID
)
500 /* Release the console framebuffer mutex if needed */
501 ReleaseMutex(ConsoleMutex
);
503 /* Switch back to the text buffer */
504 SetConsoleActiveScreenBuffer(TextConsoleBuffer
);
506 /* Cleanup the video data */
507 CloseHandle(ConsoleMutex
);
509 CloseHandle(GraphicsConsoleBuffer
);
510 GraphicsConsoleBuffer
= NULL
;
511 DoubleVision
= FALSE
;
514 static BOOL
VgaEnterTextMode(PCOORD Resolution
)
516 /* Resize the console */
517 SetConsoleScreenBufferSize(TextConsoleBuffer
, *Resolution
);
519 /* Allocate a framebuffer */
520 ConsoleFramebuffer
= HeapAlloc(GetProcessHeap(),
522 Resolution
->X
* Resolution
->Y
523 * sizeof(CHAR_INFO
));
524 if (ConsoleFramebuffer
== NULL
)
526 DisplayMessage(L
"An unexpected error occurred!\n");
532 * Set the text mode palette.
534 * WARNING: This call should fail on Windows (and therefore
535 * we get the default palette and our external behaviour is
536 * just like Windows' one), but it should success on ReactOS
537 * (so that we get console palette changes even for text-mode
538 * screen-buffers, which is a new feature on ReactOS).
540 SetConsolePalette(TextConsoleBuffer
,
544 /* Set the text mode flag */
550 static VOID
VgaLeaveTextMode(VOID
)
552 /* Free the old framebuffer */
553 HeapFree(GetProcessHeap(), 0, ConsoleFramebuffer
);
554 ConsoleFramebuffer
= NULL
;
557 static VOID
VgaChangeMode(VOID
)
559 COORD Resolution
= VgaGetDisplayResolution();
561 /* Reset the mode change flag */
562 // ModeChanged = FALSE;
566 /* Leave the current graphics mode */
567 VgaLeaveGraphicsMode();
571 /* Leave the current text mode */
575 /* Check if the new mode is alphanumeric */
576 if (!(VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
))
578 /* Enter new text mode */
579 if (!VgaEnterTextMode(&Resolution
))
581 DisplayMessage(L
"An unexpected VGA error occurred while switching into text mode.");
588 /* Enter 8-bit graphics mode */
589 if (!VgaEnterGraphicsMode(&Resolution
))
591 DisplayMessage(L
"An unexpected VGA error occurred while switching into graphics mode.");
597 /* Trigger a full update of the screen */
599 UpdateRectangle
.Left
= 0;
600 UpdateRectangle
.Top
= 0;
601 UpdateRectangle
.Right
= Resolution
.X
;
602 UpdateRectangle
.Bottom
= Resolution
.Y
;
604 /* Reset the mode change flag */
608 static VOID
VgaUpdateFramebuffer(VOID
)
611 COORD Resolution
= VgaGetDisplayResolution();
612 INT AddressSize
= VgaGetAddressSize();
613 DWORD Address
= (VgaCrtcRegisters
[VGA_CRTC_START_ADDR_HIGH_REG
] << 8)
614 + VgaCrtcRegisters
[VGA_CRTC_START_ADDR_LOW_REG
];
615 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
618 * If console framebuffer is NULL, that means something went wrong
619 * earlier and this is the final display refresh.
621 if (ConsoleFramebuffer
== NULL
) return;
623 /* Check if this is text mode or graphics mode */
624 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
627 PBYTE GraphicsBuffer
= (PBYTE
)ConsoleFramebuffer
;
630 * Synchronize access to the graphics framebuffer
631 * with the console framebuffer mutex.
633 WaitForSingleObject(ConsoleMutex
, INFINITE
);
635 /* Loop through the scanlines */
636 for (i
= 0; i
< Resolution
.Y
; i
++)
638 /* Loop through the pixels */
639 for (j
= 0; j
< Resolution
.X
; j
++)
643 /* Check the shifting mode */
644 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFT256
)
646 /* 4 bits shifted from each plane */
648 /* Check if this is 16 or 256 color mode */
649 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
651 /* One byte per pixel */
652 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
653 + (Address
+ (j
/ VGA_NUM_BANKS
))
658 /* 4-bits per pixel */
660 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
661 + (Address
+ (j
/ (VGA_NUM_BANKS
* 2)))
664 /* Check if we should use the highest 4 bits or lowest 4 */
665 if (((j
/ VGA_NUM_BANKS
) % 2) == 0)
677 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFTREG
)
679 /* Check if this is 16 or 256 color mode */
680 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
682 // TODO: NOT IMPLEMENTED
683 DPRINT1("8-bit interleaved mode is not implemented!\n");
688 * 2 bits shifted from plane 0 and 2 for the first 4 pixels,
689 * then 2 bits shifted from plane 1 and 3 for the next 4
691 BYTE LowPlaneData
= VgaMemory
[((j
/ 4) % 2) * VGA_BANK_SIZE
692 + (Address
+ (j
/ 4)) * AddressSize
];
693 BYTE HighPlaneData
= VgaMemory
[(((j
/ 4) % 2) + 2) * VGA_BANK_SIZE
694 + (Address
+ (j
/ 4)) * AddressSize
];
696 /* Extract the two bits from each plane */
697 LowPlaneData
= (LowPlaneData
>> (6 - ((j
% 4) * 2))) & 3;
698 HighPlaneData
= (HighPlaneData
>> (6 - ((j
% 4) * 2))) & 3;
700 /* Combine them into the pixel */
701 PixelData
= LowPlaneData
| (HighPlaneData
<< 2);
706 /* 1 bit shifted from each plane */
708 /* Check if this is 16 or 256 color mode */
709 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
711 /* 8 bits per pixel, 2 on each plane */
713 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
715 /* The data is on plane k, 4 pixels per byte */
716 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
717 + (Address
+ (j
/ 4)) * AddressSize
];
719 /* The mask of the first bit in the pair */
720 BYTE BitMask
= 1 << (((3 - (j
% 4)) * 2) + 1);
722 /* Bits 0, 1, 2 and 3 come from the first bit of the pair */
723 if (PlaneData
& BitMask
) PixelData
|= 1 << k
;
725 /* Bits 4, 5, 6 and 7 come from the second bit of the pair */
726 if (PlaneData
& (BitMask
>> 1)) PixelData
|= 1 << (k
+ 4);
731 /* 4 bits per pixel, 1 on each plane */
733 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
735 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
736 + (Address
+ (j
/ 8)) * AddressSize
];
738 /* If the bit on that plane is set, set it */
739 if (PlaneData
& (1 << (7 - (j
% 8)))) PixelData
|= 1 << k
;
744 if (!(VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
))
746 /* In 16 color mode, the value is an index to the AC registers */
747 PixelData
= VgaAcRegisters
[PixelData
];
752 /* Now check if the resulting pixel data has changed */
753 if (GraphicsBuffer
[(i
* Resolution
.X
* 4) + (j
* 2)] != PixelData
)
755 /* Yes, write the new value */
756 GraphicsBuffer
[(i
* Resolution
.X
* 4) + (j
* 2)] = PixelData
;
757 GraphicsBuffer
[(i
* Resolution
.X
* 4) + (j
* 2 + 1)] = PixelData
;
758 GraphicsBuffer
[((i
* 2 + 1) * Resolution
.X
* 2) + (j
* 2)] = PixelData
;
759 GraphicsBuffer
[((i
* 2 + 1) * Resolution
.X
* 2) + (j
* 2 + 1)] = PixelData
;
761 /* Mark the specified pixel as changed */
762 VgaMarkForUpdate(i
, j
);
767 /* Now check if the resulting pixel data has changed */
768 if (GraphicsBuffer
[i
* Resolution
.X
+ j
] != PixelData
)
770 /* Yes, write the new value */
771 GraphicsBuffer
[i
* Resolution
.X
+ j
] = PixelData
;
773 /* Mark the specified pixel as changed */
774 VgaMarkForUpdate(i
, j
);
779 /* Move to the next scanline */
780 Address
+= ScanlineSize
;
784 * Release the console framebuffer mutex
785 * so that we allow for repainting.
787 ReleaseMutex(ConsoleMutex
);
792 PCHAR_INFO CharBuffer
= (PCHAR_INFO
)ConsoleFramebuffer
;
794 /* Loop through the scanlines */
795 for (i
= 0; i
< Resolution
.Y
; i
++)
797 /* Loop through the characters */
798 for (j
= 0; j
< Resolution
.X
; j
++)
800 DWORD CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
803 /* Plane 0 holds the character itself */
804 CharInfo
.Char
.AsciiChar
= VgaMemory
[CurrentAddr
];
806 /* Plane 1 holds the attribute */
807 CharInfo
.Attributes
= VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
];
809 /* Now check if the resulting character data has changed */
810 if ((CharBuffer
[i
* Resolution
.X
+ j
].Char
.AsciiChar
!= CharInfo
.Char
.AsciiChar
)
811 || (CharBuffer
[i
* Resolution
.X
+ j
].Attributes
!= CharInfo
.Attributes
))
813 /* Yes, write the new value */
814 CharBuffer
[i
* Resolution
.X
+ j
] = CharInfo
;
816 /* Mark the specified cell as changed */
817 VgaMarkForUpdate(i
, j
);
821 /* Move to the next scanline */
822 Address
+= ScanlineSize
;
827 static VOID
VgaUpdateTextCursor(VOID
)
830 CONSOLE_CURSOR_INFO CursorInfo
;
831 BYTE CursorStart
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_START_REG
] & 0x3F;
832 BYTE CursorEnd
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] & 0x1F;
833 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
834 BYTE TextSize
= 1 + (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & 0x1F);
835 WORD Location
= MAKEWORD(VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_LOW_REG
],
836 VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_HIGH_REG
]);
838 if (CursorStart
< CursorEnd
)
841 CursorInfo
.bVisible
= TRUE
;
842 CursorInfo
.dwSize
= (100 * (CursorEnd
- CursorStart
)) / TextSize
;
847 CursorInfo
.bVisible
= FALSE
;
848 CursorInfo
.dwSize
= 0;
851 /* Add the cursor skew to the location */
852 Location
+= (VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] >> 5) & 3;
854 /* Find the coordinates of the new position */
855 Position
.X
= (WORD
)(Location
% ScanlineSize
);
856 Position
.Y
= (WORD
)(Location
/ ScanlineSize
);
858 /* Update the physical cursor */
859 SetConsoleCursorInfo(TextConsoleBuffer
, &CursorInfo
);
860 SetConsoleCursorPosition(TextConsoleBuffer
, Position
);
862 /* Reset the cursor move flag */
866 /* PUBLIC FUNCTIONS ***********************************************************/
868 DWORD
VgaGetVideoBaseAddress(VOID
)
870 return MemoryBase
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
873 DWORD
VgaGetVideoLimitAddress(VOID
)
875 return MemoryLimit
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
878 COORD
VgaGetDisplayResolution(VOID
)
881 BYTE MaximumScanLine
= 1 + (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & 0x1F);
883 /* The low 8 bits are in the display registers */
884 Resolution
.X
= VgaCrtcRegisters
[VGA_CRTC_END_HORZ_DISP_REG
];
885 Resolution
.Y
= VgaCrtcRegisters
[VGA_CRTC_VERT_DISP_END_REG
];
887 /* Set the top bits from the overflow register */
888 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE8
)
890 Resolution
.Y
|= 1 << 8;
892 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE9
)
894 Resolution
.Y
|= 1 << 9;
897 /* Increase the values by 1 */
901 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
903 /* Multiply the horizontal resolution by the 9/8 dot mode */
904 Resolution
.X
*= (VgaSeqRegisters
[VGA_SEQ_CLOCK_REG
] & VGA_SEQ_CLOCK_98DM
)
907 /* The horizontal resolution is halved in 8-bit mode */
908 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
) Resolution
.X
/= 2;
911 if (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & VGA_CRTC_MAXSCANLINE_DOUBLE
)
913 /* Halve the vertical resolution */
917 /* Divide the vertical resolution by the maximum scan line (== font size in text mode) */
918 Resolution
.Y
/= MaximumScanLine
;
920 /* Return the resolution */
924 VOID
VgaRefreshDisplay(VOID
)
926 HANDLE ConsoleBufferHandle
= NULL
;
927 COORD Resolution
= VgaGetDisplayResolution();
929 DPRINT("VgaRefreshDisplay\n");
931 /* Change the display mode */
932 if (ModeChanged
) VgaChangeMode();
934 /* Change the text cursor location */
935 if (CursorMoved
) VgaUpdateTextCursor();
939 /* Trigger a full update of the screen */
941 UpdateRectangle
.Left
= 0;
942 UpdateRectangle
.Top
= 0;
943 UpdateRectangle
.Right
= Resolution
.X
;
944 UpdateRectangle
.Bottom
= Resolution
.Y
;
946 PaletteChanged
= FALSE
;
949 /* Update the contents of the framebuffer */
950 VgaUpdateFramebuffer();
952 /* Set the vertical retrace flag */
953 InVerticalRetrace
= TRUE
;
955 /* Ignore if there's nothing to update */
956 if (!NeedsUpdate
) return;
958 DPRINT("Updating screen rectangle (%d, %d, %d, %d)\n",
959 UpdateRectangle
.Left
,
961 UpdateRectangle
.Right
,
962 UpdateRectangle
.Bottom
);
964 /* Check if this is text mode or graphics mode */
965 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
968 ConsoleBufferHandle
= GraphicsConsoleBuffer
;
973 COORD Origin
= { UpdateRectangle
.Left
, UpdateRectangle
.Top
};
974 ConsoleBufferHandle
= TextConsoleBuffer
;
976 /* Write the data to the console */
977 WriteConsoleOutputA(TextConsoleBuffer
,
978 (PCHAR_INFO
)ConsoleFramebuffer
,
986 /* Scale the update rectangle */
987 UpdateRectangle
.Left
*= 2;
988 UpdateRectangle
.Top
*= 2;
989 UpdateRectangle
.Right
= UpdateRectangle
.Right
* 2 + 1;
990 UpdateRectangle
.Bottom
= UpdateRectangle
.Bottom
* 2 + 1;
993 /* Redraw the screen */
994 InvalidateConsoleDIBits(ConsoleBufferHandle
, &UpdateRectangle
);
996 /* Clear the update flag */
1000 VOID
VgaHorizontalRetrace(VOID
)
1003 InHorizontalRetrace
= TRUE
;
1006 VOID
VgaReadMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
1011 DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n",
1015 /* Ignore if video RAM access is disabled */
1016 if (!(VgaMiscRegister
& VGA_MISC_RAM_ENABLED
)) return;
1018 /* Loop through each byte */
1019 for (i
= 0; i
< Size
; i
++)
1021 VideoAddress
= VgaTranslateReadAddress(Address
+ i
);
1023 /* Load the latch registers */
1024 VgaLatchRegisters
[0] = VgaMemory
[LOWORD(VideoAddress
)];
1025 VgaLatchRegisters
[1] = VgaMemory
[VGA_BANK_SIZE
+ LOWORD(VideoAddress
)];
1026 VgaLatchRegisters
[2] = VgaMemory
[(2 * VGA_BANK_SIZE
) + LOWORD(VideoAddress
)];
1027 VgaLatchRegisters
[3] = VgaMemory
[(3 * VGA_BANK_SIZE
) + LOWORD(VideoAddress
)];
1029 /* Copy the value to the buffer */
1030 Buffer
[i
] = VgaMemory
[VideoAddress
];
1034 VOID
VgaWriteMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
1039 DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n",
1043 /* Ignore if video RAM access is disabled */
1044 if (!(VgaMiscRegister
& VGA_MISC_RAM_ENABLED
)) return;
1046 /* Also ignore if write access to all planes is disabled */
1047 if ((VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & 0x0F) == 0x00) return;
1049 /* Loop through each byte */
1050 for (i
= 0; i
< Size
; i
++)
1052 VideoAddress
= VgaTranslateWriteAddress(Address
+ i
);
1054 for (j
= 0; j
< VGA_NUM_BANKS
; j
++)
1056 /* Make sure the page is writeable */
1057 if (!(VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & (1 << j
))) continue;
1059 /* Check if this is chain-4 mode */
1060 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
1062 if (((Address
+ i
) & 3) != j
)
1064 /* This plane will not be accessed */
1069 /* Check if this is odd-even mode */
1070 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
1072 if (((Address
+ i
) & 1) != (j
& 1))
1074 /* This plane will not be accessed */
1079 /* Copy the value to the VGA memory */
1080 VgaMemory
[VideoAddress
+ j
* VGA_BANK_SIZE
] = VgaTranslateByteForWriting(Buffer
[i
], j
);
1085 BYTE
VgaReadPort(WORD Port
)
1087 DPRINT("VgaReadPort: Port 0x%04X\n", Port
);
1098 return VgaAcRegisters
[VgaAcIndex
];
1108 return VgaSeqRegisters
[VgaSeqIndex
];
1111 case VGA_DAC_READ_INDEX
:
1113 /* This returns the read/write state */
1114 return VgaDacReadWrite
? 0 : 3;
1117 case VGA_DAC_WRITE_INDEX
:
1119 return VgaDacIndex
/ 3;
1124 /* Ignore reads in write mode */
1125 if (!VgaDacReadWrite
)
1127 BYTE Data
= VgaDacRegisters
[VgaDacIndex
++];
1128 VgaDacIndex
%= VGA_PALETTE_SIZE
;
1137 return VgaMiscRegister
;
1140 case VGA_CRTC_INDEX
:
1142 return VgaCrtcIndex
;
1147 return VgaCrtcRegisters
[VgaCrtcIndex
];
1157 return VgaGcRegisters
[VgaGcIndex
];
1161 case VGA_STAT_COLOR
:
1165 /* Reset the AC latch */
1168 /* Set a flag if there is a vertical or horizontal retrace */
1169 if (InVerticalRetrace
|| InHorizontalRetrace
) Result
|= VGA_STAT_DD
;
1171 /* Set an additional flag if there was a vertical retrace */
1172 if (InVerticalRetrace
) Result
|= VGA_STAT_VRETRACE
;
1174 /* Clear the flags */
1175 InHorizontalRetrace
= InVerticalRetrace
= FALSE
;
1184 VOID
VgaWritePort(WORD Port
, BYTE Data
)
1186 DPRINT("VgaWritePort: Port 0x%04X, Data 0x%02X\n", Port
, Data
);
1194 /* Change the index */
1195 if (Data
< VGA_AC_MAX_REG
) VgaAcIndex
= Data
;
1199 /* Write the data */
1203 /* Toggle the latch */
1204 VgaAcLatch
= !VgaAcLatch
;
1211 /* Set the sequencer index register */
1212 if (Data
< VGA_SEQ_MAX_REG
) VgaSeqIndex
= Data
;
1218 /* Call the sequencer function */
1219 VgaWriteSequencer(Data
);
1223 case VGA_DAC_READ_INDEX
:
1225 VgaDacReadWrite
= FALSE
;
1226 VgaDacIndex
= Data
* 3;
1230 case VGA_DAC_WRITE_INDEX
:
1232 VgaDacReadWrite
= TRUE
;
1233 VgaDacIndex
= Data
* 3;
1239 /* Ignore writes in read mode */
1240 if (VgaDacReadWrite
) VgaWriteDac(Data
& 0x3F);
1244 case VGA_MISC_WRITE
:
1246 VgaMiscRegister
= Data
;
1250 case VGA_CRTC_INDEX
:
1252 /* Set the CRTC index register */
1253 if (Data
< VGA_CRTC_MAX_REG
) VgaCrtcIndex
= Data
;
1259 /* Call the CRTC function */
1266 /* Set the GC index register */
1267 if (Data
< VGA_GC_MAX_REG
) VgaGcIndex
= Data
;
1273 /* Call the GC function */
1280 VOID
VgaClearMemory(VOID
)
1282 ZeroMemory(VgaMemory
, sizeof(VgaMemory
));
1285 VOID
VgaResetPalette(VOID
)
1287 PALETTEENTRY Entries
[VGA_MAX_COLORS
];
1289 /* Restore the default palette */
1290 VgaRestoreDefaultPalette(Entries
, VGA_MAX_COLORS
);
1292 SetPaletteEntries(PaletteHandle
, 0, VGA_MAX_COLORS
, Entries
);
1293 PaletteChanged
= TRUE
;
1296 BOOLEAN
VgaInitialize(HANDLE TextHandle
)
1302 COORD Origin
= { 0, 0 };
1303 SMALL_RECT ScreenRect
;
1304 PCHAR_INFO CharBuffer
;
1308 /* Initialize the VGA palette and fail if it isn't successfully created */
1309 if (!VgaInitializePalette()) return FALSE
;
1311 /* Set the global handle */
1312 TextConsoleBuffer
= TextHandle
;
1314 /* Clear the VGA memory */
1317 /* Set the default video mode */
1318 BiosSetVideoMode(BIOS_DEFAULT_VIDEO_MODE
);
1322 Resolution
= VgaGetDisplayResolution();
1323 CharBuffer
= (PCHAR_INFO
)ConsoleFramebuffer
;
1324 AddressSize
= VgaGetAddressSize();
1325 ScreenRect
.Left
= ScreenRect
.Top
= 0;
1326 ScreenRect
.Right
= Resolution
.X
;
1327 ScreenRect
.Bottom
= Resolution
.Y
;
1328 ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
1330 /* Read the data from the console into the framebuffer */
1331 ReadConsoleOutputA(TextConsoleBuffer
,
1337 /* Loop through the scanlines */
1338 for (i
= 0; i
< Resolution
.Y
; i
++)
1340 /* Loop through the characters */
1341 for (j
= 0; j
< Resolution
.X
; j
++)
1343 CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
1345 /* Store the character in plane 0 */
1346 VgaMemory
[CurrentAddr
] = CharBuffer
[i
* Resolution
.X
+ j
].Char
.AsciiChar
;
1348 /* Store the attribute in plane 1 */
1349 VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
] = (BYTE
)CharBuffer
[i
* Resolution
.X
+ j
].Attributes
;
1352 /* Move to the next scanline */
1353 Address
+= ScanlineSize
;
1356 /* Return success */