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 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 };
118 /* PRIVATE FUNCTIONS **********************************************************/
120 static inline INT
VgaGetAddressSize(VOID
)
122 if (VgaCrtcRegisters
[VGA_CRTC_UNDERLINE_REG
] & VGA_CRTC_UNDERLINE_DWORD
)
124 /* Double-word addressing */
128 if (VgaCrtcRegisters
[VGA_CRTC_MODE_CONTROL_REG
] & VGA_CRTC_MODE_CONTROL_BYTE
)
130 /* Byte addressing */
134 /* Word addressing */
138 static inline DWORD
VgaTranslateReadAddress(DWORD Address
)
140 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
143 /* Check for chain-4 and odd-even mode */
144 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
146 /* The lowest two bits are the plane number */
150 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
152 /* The LSB is the plane number */
158 /* Use the read mode */
159 Plane
= VgaGcRegisters
[VGA_GC_READ_MAP_SEL_REG
] & 0x03;
162 /* Multiply the offset by the address size */
163 Offset
*= VgaGetAddressSize();
165 return Offset
+ Plane
* VGA_BANK_SIZE
;
168 static inline DWORD
VgaTranslateWriteAddress(DWORD Address
)
170 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
172 /* Check for chain-4 and odd-even mode */
173 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
175 /* Shift the offset to the right by 2 */
178 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
180 /* Shift the offset to the right by 1 */
184 /* Multiply the offset by the address size */
185 Offset
*= VgaGetAddressSize();
187 /* Return the offset on plane 0 */
191 static inline BYTE
VgaTranslateByteForWriting(BYTE Data
, BYTE Plane
)
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
];
200 /* In write mode 1 just return the latch register */
201 return VgaLatchRegisters
[Plane
];
206 /* Write modes 0 and 3 rotate the data to the right first */
207 Data
= LOBYTE(((DWORD
)Data
>> RotateCount
) | ((DWORD
)Data
<< (8 - RotateCount
)));
211 /* Write mode 2 expands the appropriate bit to all 8 bits */
212 Data
= (Data
& (1 << Plane
)) ? 0xFF : 0x00;
218 * In write mode 0, the enable set/reset register decides if the
219 * set/reset bit should be expanded to all 8 bits.
221 if (VgaGcRegisters
[VGA_GC_ENABLE_RESET_REG
] & (1 << Plane
))
223 /* Copy the bit from the set/reset register to all 8 bits */
224 Data
= (VgaGcRegisters
[VGA_GC_RESET_REG
] & (1 << Plane
)) ? 0xFF : 0x00;
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
];
237 /* For write mode 3, we AND the bitmask with the data, which is used as the new bitmask */
240 /* Then we expand the bit in the set/reset field */
241 Data
= (VgaGcRegisters
[VGA_GC_RESET_REG
] & (1 << Plane
)) ? 0xFF : 0x00;
244 /* Bits cleared in the bitmask are replaced with latch register bits */
245 Data
= (Data
& BitMask
) | (VgaLatchRegisters
[Plane
] & (~BitMask
));
247 /* Return the byte */
251 static inline VOID
VgaMarkForUpdate(SHORT Row
, SHORT Column
)
253 DPRINT("VgaMarkForUpdate: Row %d, Column %d\n", Row
, Column
);
255 /* Check if this is the first time the rectangle is updated */
258 UpdateRectangle
.Left
= UpdateRectangle
.Top
= SHRT_MAX
;
259 UpdateRectangle
.Right
= UpdateRectangle
.Bottom
= SHRT_MIN
;
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
);
268 /* Set the update request flag */
272 static VOID
VgaWriteSequencer(BYTE Data
)
274 ASSERT(VgaSeqIndex
< VGA_SEQ_MAX_REG
);
277 VgaSeqRegisters
[VgaSeqIndex
] = Data
;
280 static VOID
VgaWriteGc(BYTE Data
)
282 ASSERT(VgaGcIndex
< VGA_GC_MAX_REG
);
285 VgaGcRegisters
[VgaGcIndex
] = Data
;
287 /* Check the index */
290 case VGA_GC_MISC_REG
:
292 /* The GC misc register decides if it's text or graphics mode */
300 static VOID
VgaWriteCrtc(BYTE Data
)
302 ASSERT(VgaGcIndex
< VGA_CRTC_MAX_REG
);
305 VgaCrtcRegisters
[VgaCrtcIndex
] = Data
;
307 /* Check the index */
308 switch (VgaCrtcIndex
)
310 case VGA_CRTC_END_HORZ_DISP_REG
:
311 case VGA_CRTC_VERT_DISP_END_REG
:
312 case VGA_CRTC_OVERFLOW_REG
:
314 /* The video mode has changed */
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
:
325 /* Set the cursor moved flag */
333 static VOID
VgaWriteDac(BYTE Data
)
339 VgaDacRegisters
[VgaDacIndex
] = Data
;
341 /* Find the palette index */
342 PaletteIndex
= VgaDacIndex
/ 3;
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]);
350 /* Update the palette entry */
351 SetPaletteEntries(PaletteHandle
, PaletteIndex
, 1, &Entry
);
353 /* Set the palette change flag */
354 PaletteChanged
= TRUE
;
356 /* Update the index */
358 VgaDacIndex
%= VGA_PALETTE_SIZE
;
361 static VOID
VgaWriteAc(BYTE Data
)
363 ASSERT(VgaAcIndex
< VGA_AC_MAX_REG
);
366 VgaAcRegisters
[VgaAcIndex
] = Data
;
369 static BOOL
VgaEnterGraphicsMode(PCOORD Resolution
)
372 CONSOLE_GRAPHICS_BUFFER_INFO GraphicsBufferInfo
;
373 BYTE BitmapInfoBuffer
[VGA_BITMAP_INFO_SIZE
];
374 LPBITMAPINFO BitmapInfo
= (LPBITMAPINFO
)BitmapInfoBuffer
;
375 LPWORD PaletteIndex
= (LPWORD
)(BitmapInfo
->bmiColors
);
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 */;
387 /* Fill the palette data */
388 for (i
= 0; i
< (VGA_PALETTE_SIZE
/ 3); i
++) PaletteIndex
[i
] = (WORD
)i
;
390 /* Fill the console graphics buffer info */
391 GraphicsBufferInfo
.dwBitMapInfoLength
= VGA_BITMAP_INFO_SIZE
;
392 GraphicsBufferInfo
.lpBitMapInfo
= BitmapInfo
;
393 GraphicsBufferInfo
.dwUsage
= DIB_PAL_COLORS
;
395 /* Create the buffer */
396 GraphicsConsoleBuffer
= CreateConsoleScreenBuffer(GENERIC_READ
| GENERIC_WRITE
,
397 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
399 CONSOLE_GRAPHICS_BUFFER
,
400 &GraphicsBufferInfo
);
401 if (GraphicsConsoleBuffer
== INVALID_HANDLE_VALUE
) return FALSE
;
403 /* Save the framebuffer address and mutex */
404 ConsoleFramebuffer
= GraphicsBufferInfo
.lpBitMap
;
405 ConsoleMutex
= GraphicsBufferInfo
.hMutex
;
407 /* Clear the framebuffer */
408 ZeroMemory(ConsoleFramebuffer
, BitmapInfo
->bmiHeader
.biSizeImage
);
410 /* Set the active buffer */
411 SetConsoleActiveScreenBuffer(GraphicsConsoleBuffer
);
413 /* Set the graphics mode palette */
414 SetConsolePalette(GraphicsConsoleBuffer
,
418 /* Clear the text mode flag */
424 static VOID
VgaLeaveGraphicsMode(VOID
)
426 /* Release the console framebuffer mutex if needed */
427 ReleaseMutex(ConsoleMutex
);
429 /* Switch back to the text buffer */
430 SetConsoleActiveScreenBuffer(TextConsoleBuffer
);
432 /* Cleanup the video data */
433 CloseHandle(ConsoleMutex
);
435 CloseHandle(GraphicsConsoleBuffer
);
436 GraphicsConsoleBuffer
= NULL
;
439 static BOOL
VgaEnterTextMode(PCOORD Resolution
)
441 /* Resize the console */
442 SetConsoleScreenBufferSize(TextConsoleBuffer
, *Resolution
);
444 /* Allocate a framebuffer */
445 ConsoleFramebuffer
= HeapAlloc(GetProcessHeap(),
447 Resolution
->X
* Resolution
->Y
448 * sizeof(CHAR_INFO
));
449 if (ConsoleFramebuffer
== NULL
)
451 DisplayMessage(L
"An unexpected error occurred!\n");
456 /* Set the text mode flag */
462 static VOID
VgaLeaveTextMode(VOID
)
464 /* Free the old framebuffer */
465 HeapFree(GetProcessHeap(), 0, ConsoleFramebuffer
);
466 ConsoleFramebuffer
= NULL
;
469 static VOID
VgaChangeMode(VOID
)
471 COORD Resolution
= VgaGetDisplayResolution();
473 /* Reset the mode change flag */
474 // ModeChanged = FALSE;
478 /* Leave the current graphics mode */
479 VgaLeaveGraphicsMode();
483 /* Leave the current text mode */
487 /* Check if the new mode is alphanumeric */
488 if (!(VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
))
490 /* Enter new text mode */
491 if (!VgaEnterTextMode(&Resolution
))
493 DisplayMessage(L
"An unexpected VGA error occurred while switching into text mode.");
500 /* Enter 8-bit graphics mode */
501 if (!VgaEnterGraphicsMode(&Resolution
))
503 DisplayMessage(L
"An unexpected VGA error occurred while switching into graphics mode.");
509 /* Trigger a full update of the screen */
511 UpdateRectangle
.Left
= 0;
512 UpdateRectangle
.Top
= 0;
513 UpdateRectangle
.Right
= Resolution
.X
;
514 UpdateRectangle
.Bottom
= Resolution
.Y
;
516 /* Reset the mode change flag */
520 static VOID
VgaUpdateFramebuffer(VOID
)
523 COORD Resolution
= VgaGetDisplayResolution();
524 INT AddressSize
= VgaGetAddressSize();
525 DWORD Address
= (VgaCrtcRegisters
[VGA_CRTC_START_ADDR_HIGH_REG
] << 8)
526 + VgaCrtcRegisters
[VGA_CRTC_START_ADDR_LOW_REG
];
527 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
530 * If console framebuffer is NULL, that means something went wrong
531 * earlier and this is the final display refresh.
533 if (ConsoleFramebuffer
== NULL
) return;
535 /* Check if this is text mode or graphics mode */
536 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
539 PBYTE GraphicsBuffer
= (PBYTE
)ConsoleFramebuffer
;
542 * Synchronize access to the graphics framebuffer
543 * with the console framebuffer mutex.
545 WaitForSingleObject(ConsoleMutex
, INFINITE
);
547 /* Loop through the scanlines */
548 for (i
= 0; i
< Resolution
.Y
; i
++)
550 /* Loop through the pixels */
551 for (j
= 0; j
< Resolution
.X
; j
++)
555 /* Check the shifting mode */
556 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFT256
)
558 /* 4 bits shifted from each plane */
560 /* Check if this is 16 or 256 color mode */
561 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
563 /* One byte per pixel */
564 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
565 + (Address
+ (j
/ VGA_NUM_BANKS
))
570 /* 4-bits per pixel */
572 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
573 + (Address
+ (j
/ (VGA_NUM_BANKS
* 2)))
576 /* Check if we should use the highest 4 bits or lowest 4 */
577 if (((j
/ VGA_NUM_BANKS
) % 2) == 0)
589 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFTREG
)
591 /* Check if this is 16 or 256 color mode */
592 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
594 // TODO: NOT IMPLEMENTED
595 DPRINT1("8-bit interleaved mode is not implemented!\n");
600 * 2 bits shifted from plane 0 and 2 for the first 4 pixels,
601 * then 2 bits shifted from plane 1 and 3 for the next 4
603 BYTE LowPlaneData
= VgaMemory
[((j
/ 4) % 2) * VGA_BANK_SIZE
604 + (Address
+ (j
/ 4)) * AddressSize
];
605 BYTE HighPlaneData
= VgaMemory
[(((j
/ 4) % 2) + 2) * VGA_BANK_SIZE
606 + (Address
+ (j
/ 4)) * AddressSize
];
608 /* Extract the two bits from each plane */
609 LowPlaneData
= (LowPlaneData
>> (6 - ((j
% 4) * 2))) & 3;
610 HighPlaneData
= (HighPlaneData
>> (6 - ((j
% 4) * 2))) & 3;
612 /* Combine them into the pixel */
613 PixelData
= LowPlaneData
| (HighPlaneData
<< 2);
618 /* 1 bit shifted from each plane */
620 /* Check if this is 16 or 256 color mode */
621 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
623 /* 8 bits per pixel, 2 on each plane */
625 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
627 /* The data is on plane k, 4 pixels per byte */
628 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
629 + (Address
+ (j
/ 4)) * AddressSize
];
631 /* The mask of the first bit in the pair */
632 BYTE BitMask
= 1 << (((3 - (j
% 4)) * 2) + 1);
634 /* Bits 0, 1, 2 and 3 come from the first bit of the pair */
635 if (PlaneData
& BitMask
) PixelData
|= 1 << k
;
637 /* Bits 4, 5, 6 and 7 come from the second bit of the pair */
638 if (PlaneData
& (BitMask
>> 1)) PixelData
|= 1 << (k
+ 4);
643 /* 4 bits per pixel, 1 on each plane */
645 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
647 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
648 + (Address
+ (j
/ 8)) * AddressSize
];
650 /* If the bit on that plane is set, set it */
651 if (PlaneData
& (1 << (7 - (j
% 8)))) PixelData
|= 1 << k
;
656 if (!(VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
))
658 /* In 16 color mode, the value is an index to the AC registers */
659 PixelData
= VgaAcRegisters
[PixelData
];
662 /* Now check if the resulting pixel data has changed */
663 if (GraphicsBuffer
[i
* Resolution
.X
+ j
] != PixelData
)
665 /* Yes, write the new value */
666 GraphicsBuffer
[i
* Resolution
.X
+ j
] = PixelData
;
668 /* Mark the specified pixel as changed */
669 VgaMarkForUpdate(i
, j
);
673 /* Move to the next scanline */
674 Address
+= ScanlineSize
;
678 * Release the console framebuffer mutex
679 * so that we allow for repainting.
681 ReleaseMutex(ConsoleMutex
);
686 PCHAR_INFO CharBuffer
= (PCHAR_INFO
)ConsoleFramebuffer
;
688 /* Loop through the scanlines */
689 for (i
= 0; i
< Resolution
.Y
; i
++)
691 /* Loop through the characters */
692 for (j
= 0; j
< Resolution
.X
; j
++)
694 DWORD CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
697 /* Plane 0 holds the character itself */
698 CharInfo
.Char
.AsciiChar
= VgaMemory
[CurrentAddr
];
700 /* Plane 1 holds the attribute */
701 CharInfo
.Attributes
= VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
];
703 /* Now check if the resulting character data has changed */
704 if ((CharBuffer
[i
* Resolution
.X
+ j
].Char
.AsciiChar
!= CharInfo
.Char
.AsciiChar
)
705 || (CharBuffer
[i
* Resolution
.X
+ j
].Attributes
!= CharInfo
.Attributes
))
707 /* Yes, write the new value */
708 CharBuffer
[i
* Resolution
.X
+ j
] = CharInfo
;
710 /* Mark the specified cell as changed */
711 VgaMarkForUpdate(i
, j
);
715 /* Move to the next scanline */
716 Address
+= ScanlineSize
;
721 static VOID
VgaUpdateTextCursor(VOID
)
724 CONSOLE_CURSOR_INFO CursorInfo
;
725 BYTE CursorStart
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_START_REG
] & 0x3F;
726 BYTE CursorEnd
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] & 0x1F;
727 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
728 BYTE TextSize
= 1 + (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & 0x1F);
729 WORD Location
= MAKEWORD(VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_LOW_REG
],
730 VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_HIGH_REG
]);
732 if (CursorStart
< CursorEnd
)
735 CursorInfo
.bVisible
= TRUE
;
736 CursorInfo
.dwSize
= (100 * (CursorEnd
- CursorStart
)) / TextSize
;
741 CursorInfo
.bVisible
= FALSE
;
742 CursorInfo
.dwSize
= 0;
745 /* Add the cursor skew to the location */
746 Location
+= (VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] >> 5) & 3;
748 /* Find the coordinates of the new position */
749 Position
.X
= (WORD
)(Location
% ScanlineSize
);
750 Position
.Y
= (WORD
)(Location
/ ScanlineSize
);
752 /* Update the physical cursor */
753 SetConsoleCursorInfo(TextConsoleBuffer
, &CursorInfo
);
754 SetConsoleCursorPosition(TextConsoleBuffer
, Position
);
756 /* Reset the cursor move flag */
760 /* PUBLIC FUNCTIONS ***********************************************************/
762 DWORD
VgaGetVideoBaseAddress(VOID
)
764 return MemoryBase
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
767 DWORD
VgaGetVideoLimitAddress(VOID
)
769 return MemoryLimit
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
772 COORD
VgaGetDisplayResolution(VOID
)
775 BYTE MaximumScanLine
= 1 + (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & 0x1F);
777 /* The low 8 bits are in the display registers */
778 Resolution
.X
= VgaCrtcRegisters
[VGA_CRTC_END_HORZ_DISP_REG
];
779 Resolution
.Y
= VgaCrtcRegisters
[VGA_CRTC_VERT_DISP_END_REG
];
781 /* Set the top bits from the overflow register */
782 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE8
)
784 Resolution
.Y
|= 1 << 8;
786 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE9
)
788 Resolution
.Y
|= 1 << 9;
791 /* Increase the values by 1 */
795 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
797 /* Multiply the horizontal resolution by the 9/8 dot mode */
798 Resolution
.X
*= (VgaSeqRegisters
[VGA_SEQ_CLOCK_REG
] & VGA_SEQ_CLOCK_98DM
)
801 /* The horizontal resolution is halved in 8-bit mode */
802 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
) Resolution
.X
/= 2;
805 if (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & VGA_CRTC_MAXSCANLINE_DOUBLE
)
807 /* Halve the vertical resolution */
811 /* Divide the vertical resolution by the maximum scan line (== font size in text mode) */
812 Resolution
.Y
/= MaximumScanLine
;
814 /* Return the resolution */
818 VOID
VgaRefreshDisplay(VOID
)
820 COORD Resolution
= VgaGetDisplayResolution();
822 DPRINT("VgaRefreshDisplay\n");
824 /* Change the display mode */
825 if (ModeChanged
) VgaChangeMode();
827 /* Change the text cursor location */
828 if (CursorMoved
) VgaUpdateTextCursor();
832 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
834 /* Trigger a full update of the screen */
836 UpdateRectangle
.Left
= 0;
837 UpdateRectangle
.Top
= 0;
838 UpdateRectangle
.Right
= Resolution
.X
;
839 UpdateRectangle
.Bottom
= Resolution
.Y
;
842 PaletteChanged
= FALSE
;
845 /* Update the contents of the framebuffer */
846 VgaUpdateFramebuffer();
848 /* Set the vertical retrace flag */
849 InVerticalRetrace
= TRUE
;
851 /* Ignore if there's nothing to update */
852 if (!NeedsUpdate
) return;
854 DPRINT("Updating screen rectangle (%d, %d, %d, %d)\n",
855 UpdateRectangle
.Left
,
857 UpdateRectangle
.Right
,
858 UpdateRectangle
.Bottom
);
860 /* Check if this is text mode or graphics mode */
861 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
865 /* Redraw the screen */
866 InvalidateConsoleDIBits(GraphicsConsoleBuffer
, &UpdateRectangle
);
871 COORD Origin
= { UpdateRectangle
.Left
, UpdateRectangle
.Top
};
873 /* Write the data to the console */
874 WriteConsoleOutputA(TextConsoleBuffer
,
875 (PCHAR_INFO
)ConsoleFramebuffer
,
882 /* Clear the update flag */
886 VOID
VgaHorizontalRetrace(VOID
)
889 InHorizontalRetrace
= TRUE
;
892 VOID
VgaReadMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
897 DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n",
901 /* Ignore if video RAM access is disabled */
902 if (!(VgaMiscRegister
& VGA_MISC_RAM_ENABLED
)) return;
904 /* Loop through each byte */
905 for (i
= 0; i
< Size
; i
++)
907 VideoAddress
= VgaTranslateReadAddress(Address
+ i
);
909 /* Load the latch registers */
910 VgaLatchRegisters
[0] = VgaMemory
[LOWORD(VideoAddress
)];
911 VgaLatchRegisters
[1] = VgaMemory
[VGA_BANK_SIZE
+ LOWORD(VideoAddress
)];
912 VgaLatchRegisters
[2] = VgaMemory
[(2 * VGA_BANK_SIZE
) + LOWORD(VideoAddress
)];
913 VgaLatchRegisters
[3] = VgaMemory
[(3 * VGA_BANK_SIZE
) + LOWORD(VideoAddress
)];
915 /* Copy the value to the buffer */
916 Buffer
[i
] = VgaMemory
[VideoAddress
];
920 VOID
VgaWriteMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
925 DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n",
929 /* Ignore if video RAM access is disabled */
930 if (!(VgaMiscRegister
& VGA_MISC_RAM_ENABLED
)) return;
932 /* Also ignore if write access to all planes is disabled */
933 if ((VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & 0x0F) == 0x00) return;
935 /* Loop through each byte */
936 for (i
= 0; i
< Size
; i
++)
938 VideoAddress
= VgaTranslateWriteAddress(Address
+ i
);
940 for (j
= 0; j
< VGA_NUM_BANKS
; j
++)
942 /* Make sure the page is writeable */
943 if (!(VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & (1 << j
))) continue;
945 /* Check if this is chain-4 mode */
946 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
948 if (((Address
+ i
) & 3) != j
)
950 /* This plane will not be accessed */
955 /* Check if this is odd-even mode */
956 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
958 if (((Address
+ i
) & 1) != (j
& 1))
960 /* This plane will not be accessed */
965 /* Copy the value to the VGA memory */
966 VgaMemory
[VideoAddress
+ j
* VGA_BANK_SIZE
] = VgaTranslateByteForWriting(Buffer
[i
], j
);
971 BYTE
VgaReadPort(WORD Port
)
973 DPRINT("VgaReadPort: Port 0x%04X\n", Port
);
984 return VgaAcRegisters
[VgaAcIndex
];
994 return VgaSeqRegisters
[VgaSeqIndex
];
997 case VGA_DAC_READ_INDEX
:
999 /* This returns the read/write state */
1000 return VgaDacReadWrite
? 0 : 3;
1003 case VGA_DAC_WRITE_INDEX
:
1005 return VgaDacIndex
/ 3;
1010 /* Ignore reads in write mode */
1011 if (!VgaDacReadWrite
)
1013 BYTE Data
= VgaDacRegisters
[VgaDacIndex
++];
1014 VgaDacIndex
%= VGA_PALETTE_SIZE
;
1023 return VgaMiscRegister
;
1026 case VGA_CRTC_INDEX
:
1028 return VgaCrtcIndex
;
1033 return VgaCrtcRegisters
[VgaCrtcIndex
];
1043 return VgaGcRegisters
[VgaGcIndex
];
1047 case VGA_STAT_COLOR
:
1051 /* Reset the AC latch */
1054 /* Set a flag if there is a vertical or horizontal retrace */
1055 if (InVerticalRetrace
|| InHorizontalRetrace
) Result
|= VGA_STAT_DD
;
1057 /* Set an additional flag if there was a vertical retrace */
1058 if (InVerticalRetrace
) Result
|= VGA_STAT_VRETRACE
;
1060 /* Clear the flags */
1061 InHorizontalRetrace
= InVerticalRetrace
= FALSE
;
1070 VOID
VgaWritePort(WORD Port
, BYTE Data
)
1072 DPRINT("VgaWritePort: Port 0x%04X, Data 0x%02X\n", Port
, Data
);
1080 /* Change the index */
1081 if (Data
< VGA_AC_MAX_REG
) VgaAcIndex
= Data
;
1085 /* Write the data */
1089 /* Toggle the latch */
1090 VgaAcLatch
= !VgaAcLatch
;
1097 /* Set the sequencer index register */
1098 if (Data
< VGA_SEQ_MAX_REG
) VgaSeqIndex
= Data
;
1105 /* Call the sequencer function */
1106 VgaWriteSequencer(Data
);
1111 case VGA_DAC_READ_INDEX
:
1113 VgaDacReadWrite
= FALSE
;
1114 VgaDacIndex
= Data
* 3;
1119 case VGA_DAC_WRITE_INDEX
:
1121 VgaDacReadWrite
= TRUE
;
1122 VgaDacIndex
= Data
* 3;
1129 /* Ignore writes in read mode */
1130 if (VgaDacReadWrite
) VgaWriteDac(Data
& 0x3F);
1135 case VGA_MISC_WRITE
:
1137 VgaMiscRegister
= Data
;
1142 case VGA_CRTC_INDEX
:
1144 /* Set the CRTC index register */
1145 if (Data
< VGA_CRTC_MAX_REG
) VgaCrtcIndex
= Data
;
1152 /* Call the CRTC function */
1160 /* Set the GC index register */
1161 if (Data
< VGA_GC_MAX_REG
) VgaGcIndex
= Data
;
1167 /* Call the GC function */
1175 VOID
VgaClearMemory(VOID
)
1177 ZeroMemory(VgaMemory
, sizeof(VgaMemory
));
1180 BOOLEAN
VgaInitialize(HANDLE TextHandle
)
1186 COORD Origin
= { 0, 0 };
1187 SMALL_RECT ScreenRect
;
1188 PCHAR_INFO CharBuffer
;
1191 LPLOGPALETTE Palette
;
1193 /* Set the global handle */
1194 TextConsoleBuffer
= TextHandle
;
1196 /* Clear the VGA memory */
1199 /* Set the default video mode */
1200 BiosSetVideoMode(BIOS_DEFAULT_VIDEO_MODE
);
1204 Resolution
= VgaGetDisplayResolution();
1205 CharBuffer
= (PCHAR_INFO
)ConsoleFramebuffer
;
1206 AddressSize
= VgaGetAddressSize();
1207 ScreenRect
.Left
= ScreenRect
.Top
= 0;
1208 ScreenRect
.Right
= Resolution
.X
;
1209 ScreenRect
.Bottom
= Resolution
.Y
;
1210 ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
1212 /* Read the data from the console into the framebuffer */
1213 ReadConsoleOutputA(TextConsoleBuffer
,
1219 /* Loop through the scanlines */
1220 for (i
= 0; i
< Resolution
.Y
; i
++)
1222 /* Loop through the characters */
1223 for (j
= 0; j
< Resolution
.X
; j
++)
1225 CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
1227 /* Store the character in plane 0 */
1228 VgaMemory
[CurrentAddr
] = CharBuffer
[i
* Resolution
.X
+ j
].Char
.AsciiChar
;
1230 /* Store the attribute in plane 1 */
1231 VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
] = (BYTE
)CharBuffer
[i
* Resolution
.X
+ j
].Attributes
;
1234 /* Move to the next scanline */
1235 Address
+= ScanlineSize
;
1238 /* Allocate storage space for the palette */
1239 Palette
= (LPLOGPALETTE
)HeapAlloc(GetProcessHeap(),
1242 + VGA_MAX_COLORS
* sizeof(PALETTEENTRY
));
1243 if (Palette
== NULL
) return FALSE
;
1245 /* Initialize the palette */
1246 Palette
->palVersion
= 0x0300;
1247 Palette
->palNumEntries
= VGA_MAX_COLORS
;
1249 /* Copy the colors of the default palette to the DAC and console palette */
1250 for (i
= 0; i
< VGA_MAX_COLORS
; i
++)
1252 /* Set the palette entries */
1253 Palette
->palPalEntry
[i
].peRed
= GetRValue(VgaDefaultPalette
[i
]);
1254 Palette
->palPalEntry
[i
].peGreen
= GetGValue(VgaDefaultPalette
[i
]);
1255 Palette
->palPalEntry
[i
].peBlue
= GetBValue(VgaDefaultPalette
[i
]);
1256 Palette
->palPalEntry
[i
].peFlags
= 0;
1258 /* Set the DAC registers */
1259 VgaDacRegisters
[i
* 3] = VGA_COLOR_TO_DAC(GetRValue(VgaDefaultPalette
[i
]));
1260 VgaDacRegisters
[i
* 3 + 1] = VGA_COLOR_TO_DAC(GetGValue(VgaDefaultPalette
[i
]));
1261 VgaDacRegisters
[i
* 3 + 2] = VGA_COLOR_TO_DAC(GetBValue(VgaDefaultPalette
[i
]));
1264 /* Create the palette */
1265 PaletteHandle
= CreatePalette(Palette
);
1267 /* Free the palette */
1268 HeapFree(GetProcessHeap(), 0, Palette
);
1270 /* Return success if the palette was successfully created */
1271 return (PaletteHandle
? TRUE
: FALSE
);