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 *******************************************************************/
15 #include "../bios/vidbios.h"
19 /* PRIVATE VARIABLES **********************************************************/
21 static CONST DWORD MemoryBase
[] = { 0xA0000, 0xA0000, 0xB0000, 0xB8000 };
22 static CONST DWORD MemoryLimit
[] = { 0xAFFFF, 0xAFFFF, 0xB7FFF, 0xBFFFF };
25 * Activate this line if you want to use the real
26 * RegisterConsoleVDM API of ReactOS/Windows.
28 // #define USE_REAL_REGISTERCONSOLEVDM
30 #define USE_REACTOS_COLORS
31 // #define USE_DOSBOX_COLORS
33 #if defined(USE_REACTOS_COLORS)
36 static CONST COLORREF VgaDefaultPalette
[VGA_MAX_COLORS
] =
38 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0xAA), RGB(0x00, 0xAA, 0x00), RGB(0x00, 0xAA, 0xAA),
39 RGB(0xAA, 0x00, 0x00), RGB(0xAA, 0x00, 0xAA), RGB(0xAA, 0x55, 0x00), RGB(0xAA, 0xAA, 0xAA),
40 RGB(0x55, 0x55, 0x55), RGB(0x55, 0x55, 0xFF), RGB(0x55, 0xFF, 0x55), RGB(0x55, 0xFF, 0xFF),
41 RGB(0xFF, 0x55, 0x55), RGB(0xFF, 0x55, 0xFF), RGB(0xFF, 0xFF, 0x55), RGB(0xFF, 0xFF, 0xFF),
42 RGB(0x00, 0x00, 0x00), RGB(0x10, 0x10, 0x10), RGB(0x20, 0x20, 0x20), RGB(0x35, 0x35, 0x35),
43 RGB(0x45, 0x45, 0x45), RGB(0x55, 0x55, 0x55), RGB(0x65, 0x65, 0x65), RGB(0x75, 0x75, 0x75),
44 RGB(0x8A, 0x8A, 0x8A), RGB(0x9A, 0x9A, 0x9A), RGB(0xAA, 0xAA, 0xAA), RGB(0xBA, 0xBA, 0xBA),
45 RGB(0xCA, 0xCA, 0xCA), RGB(0xDF, 0xDF, 0xDF), RGB(0xEF, 0xEF, 0xEF), RGB(0xFF, 0xFF, 0xFF),
46 RGB(0x00, 0x00, 0xFF), RGB(0x41, 0x00, 0xFF), RGB(0x82, 0x00, 0xFF), RGB(0xBE, 0x00, 0xFF),
47 RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0x00, 0xBE), RGB(0xFF, 0x00, 0x82), RGB(0xFF, 0x00, 0x41),
48 RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x41, 0x00), RGB(0xFF, 0x82, 0x00), RGB(0xFF, 0xBE, 0x00),
49 RGB(0xFF, 0xFF, 0x00), RGB(0xBE, 0xFF, 0x00), RGB(0x82, 0xFF, 0x00), RGB(0x41, 0xFF, 0x00),
50 RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0x41), RGB(0x00, 0xFF, 0x82), RGB(0x00, 0xFF, 0xBE),
51 RGB(0x00, 0xFF, 0xFF), RGB(0x00, 0xBE, 0xFF), RGB(0x00, 0x82, 0xFF), RGB(0x00, 0x41, 0xFF),
52 RGB(0x82, 0x82, 0xFF), RGB(0x9E, 0x82, 0xFF), RGB(0xBE, 0x82, 0xFF), RGB(0xDF, 0x82, 0xFF),
53 RGB(0xFF, 0x82, 0xFF), RGB(0xFF, 0x82, 0xDF), RGB(0xFF, 0x82, 0xBE), RGB(0xFF, 0x82, 0x9E),
54 RGB(0xFF, 0x82, 0x82), RGB(0xFF, 0x9E, 0x82), RGB(0xFF, 0xBE, 0x82), RGB(0xFF, 0xDF, 0x82),
55 RGB(0xFF, 0xFF, 0x82), RGB(0xDF, 0xFF, 0x82), RGB(0xBE, 0xFF, 0x82), RGB(0x9E, 0xFF, 0x82),
56 RGB(0x82, 0xFF, 0x82), RGB(0x82, 0xFF, 0x9E), RGB(0x82, 0xFF, 0xBE), RGB(0x82, 0xFF, 0xDF),
57 RGB(0x82, 0xFF, 0xFF), RGB(0x82, 0xDF, 0xFF), RGB(0x82, 0xBE, 0xFF), RGB(0x82, 0x9E, 0xFF),
58 RGB(0xBA, 0xBA, 0xFF), RGB(0xCA, 0xBA, 0xFF), RGB(0xDF, 0xBA, 0xFF), RGB(0xEF, 0xBA, 0xFF),
59 RGB(0xFF, 0xBA, 0xFF), RGB(0xFF, 0xBA, 0xEF), RGB(0xFF, 0xBA, 0xDF), RGB(0xFF, 0xBA, 0xCA),
60 RGB(0xFF, 0xBA, 0xBA), RGB(0xFF, 0xCA, 0xBA), RGB(0xFF, 0xDF, 0xBA), RGB(0xFF, 0xEF, 0xBA),
61 RGB(0xFF, 0xFF, 0xBA), RGB(0xEF, 0xFF, 0xBA), RGB(0xDF, 0xFF, 0xBA), RGB(0xCA, 0xFF, 0xBA),
62 RGB(0xBA, 0xFF, 0xBA), RGB(0xBA, 0xFF, 0xCA), RGB(0xBA, 0xFF, 0xDF), RGB(0xBA, 0xFF, 0xEF),
63 RGB(0xBA, 0xFF, 0xFF), RGB(0xBA, 0xEF, 0xFF), RGB(0xBA, 0xDF, 0xFF), RGB(0xBA, 0xCA, 0xFF),
64 RGB(0x00, 0x00, 0x71), RGB(0x1C, 0x00, 0x71), RGB(0x39, 0x00, 0x71), RGB(0x55, 0x00, 0x71),
65 RGB(0x71, 0x00, 0x71), RGB(0x71, 0x00, 0x55), RGB(0x71, 0x00, 0x39), RGB(0x71, 0x00, 0x1C),
66 RGB(0x71, 0x00, 0x00), RGB(0x71, 0x1C, 0x00), RGB(0x71, 0x39, 0x00), RGB(0x71, 0x55, 0x00),
67 RGB(0x71, 0x71, 0x00), RGB(0x55, 0x71, 0x00), RGB(0x39, 0x71, 0x00), RGB(0x1C, 0x71, 0x00),
68 RGB(0x00, 0x71, 0x00), RGB(0x00, 0x71, 0x1C), RGB(0x00, 0x71, 0x39), RGB(0x00, 0x71, 0x55),
69 RGB(0x00, 0x71, 0x71), RGB(0x00, 0x55, 0x71), RGB(0x00, 0x39, 0x71), RGB(0x00, 0x1C, 0x71),
70 RGB(0x39, 0x39, 0x71), RGB(0x45, 0x39, 0x71), RGB(0x55, 0x39, 0x71), RGB(0x61, 0x39, 0x71),
71 RGB(0x71, 0x39, 0x71), RGB(0x71, 0x39, 0x61), RGB(0x71, 0x39, 0x55), RGB(0x71, 0x39, 0x45),
72 RGB(0x71, 0x39, 0x39), RGB(0x71, 0x45, 0x39), RGB(0x71, 0x55, 0x39), RGB(0x71, 0x61, 0x39),
73 RGB(0x71, 0x71, 0x39), RGB(0x61, 0x71, 0x39), RGB(0x55, 0x71, 0x39), RGB(0x45, 0x71, 0x39),
74 RGB(0x39, 0x71, 0x39), RGB(0x39, 0x71, 0x45), RGB(0x39, 0x71, 0x55), RGB(0x39, 0x71, 0x61),
75 RGB(0x39, 0x71, 0x71), RGB(0x39, 0x61, 0x71), RGB(0x39, 0x55, 0x71), RGB(0x39, 0x45, 0x71),
76 RGB(0x51, 0x51, 0x71), RGB(0x59, 0x51, 0x71), RGB(0x61, 0x51, 0x71), RGB(0x69, 0x51, 0x71),
77 RGB(0x71, 0x51, 0x71), RGB(0x71, 0x51, 0x69), RGB(0x71, 0x51, 0x61), RGB(0x71, 0x51, 0x59),
78 RGB(0x71, 0x51, 0x51), RGB(0x71, 0x59, 0x51), RGB(0x71, 0x61, 0x51), RGB(0x71, 0x69, 0x51),
79 RGB(0x71, 0x71, 0x51), RGB(0x69, 0x71, 0x51), RGB(0x61, 0x71, 0x51), RGB(0x59, 0x71, 0x51),
80 RGB(0x51, 0x71, 0x51), RGB(0x51, 0x71, 0x59), RGB(0x51, 0x71, 0x61), RGB(0x51, 0x71, 0x69),
81 RGB(0x51, 0x71, 0x71), RGB(0x51, 0x69, 0x71), RGB(0x51, 0x61, 0x71), RGB(0x51, 0x59, 0x71),
82 RGB(0x00, 0x00, 0x41), RGB(0x10, 0x00, 0x41), RGB(0x20, 0x00, 0x41), RGB(0x31, 0x00, 0x41),
83 RGB(0x41, 0x00, 0x41), RGB(0x41, 0x00, 0x31), RGB(0x41, 0x00, 0x20), RGB(0x41, 0x00, 0x10),
84 RGB(0x41, 0x00, 0x00), RGB(0x41, 0x10, 0x00), RGB(0x41, 0x20, 0x00), RGB(0x41, 0x31, 0x00),
85 RGB(0x41, 0x41, 0x00), RGB(0x31, 0x41, 0x00), RGB(0x20, 0x41, 0x00), RGB(0x10, 0x41, 0x00),
86 RGB(0x00, 0x41, 0x00), RGB(0x00, 0x41, 0x10), RGB(0x00, 0x41, 0x20), RGB(0x00, 0x41, 0x31),
87 RGB(0x00, 0x41, 0x41), RGB(0x00, 0x31, 0x41), RGB(0x00, 0x20, 0x41), RGB(0x00, 0x10, 0x41),
88 RGB(0x20, 0x20, 0x41), RGB(0x28, 0x20, 0x41), RGB(0x31, 0x20, 0x41), RGB(0x39, 0x20, 0x41),
89 RGB(0x41, 0x20, 0x41), RGB(0x41, 0x20, 0x39), RGB(0x41, 0x20, 0x31), RGB(0x41, 0x20, 0x28),
90 RGB(0x41, 0x20, 0x20), RGB(0x41, 0x28, 0x20), RGB(0x41, 0x31, 0x20), RGB(0x41, 0x39, 0x20),
91 RGB(0x41, 0x41, 0x20), RGB(0x39, 0x41, 0x20), RGB(0x31, 0x41, 0x20), RGB(0x28, 0x41, 0x20),
92 RGB(0x20, 0x41, 0x20), RGB(0x20, 0x41, 0x28), RGB(0x20, 0x41, 0x31), RGB(0x20, 0x41, 0x39),
93 RGB(0x20, 0x41, 0x41), RGB(0x20, 0x39, 0x41), RGB(0x20, 0x31, 0x41), RGB(0x20, 0x28, 0x41),
94 RGB(0x2D, 0x2D, 0x41), RGB(0x31, 0x2D, 0x41), RGB(0x35, 0x2D, 0x41), RGB(0x3D, 0x2D, 0x41),
95 RGB(0x41, 0x2D, 0x41), RGB(0x41, 0x2D, 0x3D), RGB(0x41, 0x2D, 0x35), RGB(0x41, 0x2D, 0x31),
96 RGB(0x41, 0x2D, 0x2D), RGB(0x41, 0x31, 0x2D), RGB(0x41, 0x35, 0x2D), RGB(0x41, 0x3D, 0x2D),
97 RGB(0x41, 0x41, 0x2D), RGB(0x3D, 0x41, 0x2D), RGB(0x35, 0x41, 0x2D), RGB(0x31, 0x41, 0x2D),
98 RGB(0x2D, 0x41, 0x2D), RGB(0x2D, 0x41, 0x31), RGB(0x2D, 0x41, 0x35), RGB(0x2D, 0x41, 0x3D),
99 RGB(0x2D, 0x41, 0x41), RGB(0x2D, 0x3D, 0x41), RGB(0x2D, 0x35, 0x41), RGB(0x2D, 0x31, 0x41),
100 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00),
101 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00)
104 #elif defined(USE_DOSBOX_COLORS)
107 static CONST COLORREF VgaDefaultPalette
[VGA_MAX_COLORS
] =
109 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0xAA), RGB(0x00, 0xAA, 0x00), RGB(0x00, 0xAA, 0xAA),
110 RGB(0xAA, 0x00, 0x00), RGB(0xAA, 0x00, 0xAA), RGB(0xAA, 0x55, 0x00), RGB(0xAA, 0xAA, 0xAA),
111 RGB(0x55, 0x55, 0x55), RGB(0x55, 0x55, 0xFF), RGB(0x55, 0xFF, 0x55), RGB(0x55, 0xFF, 0xFF),
112 RGB(0xFF, 0x55, 0x55), RGB(0xFF, 0x55, 0xFF), RGB(0xFF, 0xFF, 0x55), RGB(0xFF, 0xFF, 0xFF),
113 RGB(0x00, 0x00, 0x00), RGB(0x14, 0x14, 0x14), RGB(0x20, 0x20, 0x20), RGB(0x2C, 0x2C, 0x2C),
114 RGB(0x38, 0x38, 0x38), RGB(0x45, 0x45, 0x45), RGB(0x51, 0x51, 0x51), RGB(0x61, 0x61, 0x61),
115 RGB(0x71, 0x71, 0x71), RGB(0x82, 0x82, 0x82), RGB(0x92, 0x92, 0x92), RGB(0xA2, 0xA2, 0xA2),
116 RGB(0xB6, 0xB6, 0xB6), RGB(0xCB, 0xCB, 0xCB), RGB(0xE3, 0xE3, 0xE3), RGB(0xFF, 0xFF, 0xFF),
117 RGB(0x00, 0x00, 0xFF), RGB(0x41, 0x00, 0xFF), RGB(0x7D, 0x00, 0xFF), RGB(0xBE, 0x00, 0xFF),
118 RGB(0xFF, 0x00, 0xFF), RGB(0xFF, 0x00, 0xBE), RGB(0xFF, 0x00, 0x7D), RGB(0xFF, 0x00, 0x41),
119 RGB(0xFF, 0x00, 0x00), RGB(0xFF, 0x41, 0x00), RGB(0xFF, 0x7D, 0x00), RGB(0xFF, 0xBE, 0x00),
120 RGB(0xFF, 0xFF, 0x00), RGB(0xBE, 0xFF, 0x00), RGB(0x7D, 0xFF, 0x00), RGB(0x41, 0xFF, 0x00),
121 RGB(0x00, 0xFF, 0x00), RGB(0x00, 0xFF, 0x41), RGB(0x00, 0xFF, 0x7D), RGB(0x00, 0xFF, 0xBE),
122 RGB(0x00, 0xFF, 0xFF), RGB(0x00, 0xBE, 0xFF), RGB(0x00, 0x7D, 0xFF), RGB(0x00, 0x41, 0xFF),
123 RGB(0x7D, 0x7D, 0xFF), RGB(0x9E, 0x7D, 0xFF), RGB(0xBE, 0x7D, 0xFF), RGB(0xDF, 0x7D, 0xFF),
124 RGB(0xFF, 0x7D, 0xFF), RGB(0xFF, 0x7D, 0xDF), RGB(0xFF, 0x7D, 0xBE), RGB(0xFF, 0x7D, 0x9E),
126 RGB(0xFF, 0x7D, 0x7D), RGB(0xFF, 0x9E, 0x7D), RGB(0xFF, 0xBE, 0x7D), RGB(0xFF, 0xDF, 0x7D),
127 RGB(0xFF, 0xFF, 0x7D), RGB(0xDF, 0xFF, 0x7D), RGB(0xBE, 0xFF, 0x7D), RGB(0x9E, 0xFF, 0x7D),
128 RGB(0x7D, 0xFF, 0x7D), RGB(0x7D, 0xFF, 0x9E), RGB(0x7D, 0xFF, 0xBE), RGB(0x7D, 0xFF, 0xDF),
129 RGB(0x7D, 0xFF, 0xFF), RGB(0x7D, 0xDF, 0xFF), RGB(0x7D, 0xBE, 0xFF), RGB(0x7D, 0x9E, 0xFF),
130 RGB(0xB6, 0xB6, 0xFF), RGB(0xC7, 0xB6, 0xFF), RGB(0xDB, 0xB6, 0xFF), RGB(0xEB, 0xB6, 0xFF),
131 RGB(0xFF, 0xB6, 0xFF), RGB(0xFF, 0xB6, 0xEB), RGB(0xFF, 0xB6, 0xDB), RGB(0xFF, 0xB6, 0xC7),
132 RGB(0xFF, 0xB6, 0xB6), RGB(0xFF, 0xC7, 0xB6), RGB(0xFF, 0xDB, 0xB6), RGB(0xFF, 0xEB, 0xB6),
133 RGB(0xFF, 0xFF, 0xB6), RGB(0xEB, 0xFF, 0xB6), RGB(0xDB, 0xFF, 0xB6), RGB(0xC7, 0xFF, 0xB6),
134 RGB(0xB6, 0xFF, 0xB6), RGB(0xB6, 0xFF, 0xC7), RGB(0xB6, 0xFF, 0xDB), RGB(0xB6, 0xFF, 0xEB),
135 RGB(0xB6, 0xFF, 0xFF), RGB(0xB6, 0xEB, 0xFF), RGB(0xB6, 0xDB, 0xFF), RGB(0xB6, 0xC7, 0xFF),
136 RGB(0x00, 0x00, 0x71), RGB(0x1C, 0x00, 0x71), RGB(0x38, 0x00, 0x71), RGB(0x55, 0x00, 0x71),
137 RGB(0x71, 0x00, 0x71), RGB(0x71, 0x00, 0x55), RGB(0x71, 0x00, 0x38), RGB(0x71, 0x00, 0x1C),
138 RGB(0x71, 0x00, 0x00), RGB(0x71, 0x1C, 0x00), RGB(0x71, 0x38, 0x00), RGB(0x71, 0x55, 0x00),
139 RGB(0x71, 0x71, 0x00), RGB(0x55, 0x71, 0x00), RGB(0x38, 0x71, 0x00), RGB(0x1C, 0x71, 0x00),
140 RGB(0x00, 0x71, 0x00), RGB(0x00, 0x71, 0x1C), RGB(0x00, 0x71, 0x38), RGB(0x00, 0x71, 0x55),
141 RGB(0x00, 0x71, 0x71), RGB(0x00, 0x55, 0x71), RGB(0x00, 0x38, 0x71), RGB(0x00, 0x1C, 0x71),
143 RGB(0x38, 0x38, 0x71), RGB(0x45, 0x38, 0x71), RGB(0x55, 0x38, 0x71), RGB(0x61, 0x38, 0x71),
144 RGB(0x71, 0x38, 0x71), RGB(0x71, 0x38, 0x61), RGB(0x71, 0x38, 0x55), RGB(0x71, 0x38, 0x45),
145 RGB(0x71, 0x38, 0x38), RGB(0x71, 0x45, 0x38), RGB(0x71, 0x55, 0x38), RGB(0x71, 0x61, 0x38),
146 RGB(0x71, 0x71, 0x38), RGB(0x61, 0x71, 0x38), RGB(0x55, 0x71, 0x38), RGB(0x45, 0x71, 0x38),
147 RGB(0x38, 0x71, 0x38), RGB(0x38, 0x71, 0x45), RGB(0x38, 0x71, 0x55), RGB(0x38, 0x71, 0x61),
148 RGB(0x38, 0x71, 0x71), RGB(0x38, 0x61, 0x71), RGB(0x38, 0x55, 0x71), RGB(0x38, 0x45, 0x71),
149 RGB(0x51, 0x51, 0x71), RGB(0x59, 0x51, 0x71), RGB(0x61, 0x51, 0x71), RGB(0x69, 0x51, 0x71),
150 RGB(0x71, 0x51, 0x71), RGB(0x71, 0x51, 0x69), RGB(0x71, 0x51, 0x61), RGB(0x71, 0x51, 0x59),
151 RGB(0x71, 0x51, 0x51), RGB(0x71, 0x59, 0x51), RGB(0x71, 0x61, 0x51), RGB(0x71, 0x69, 0x51),
152 RGB(0x71, 0x71, 0x51), RGB(0x69, 0x71, 0x51), RGB(0x61, 0x71, 0x51), RGB(0x59, 0x71, 0x51),
153 RGB(0x51, 0x71, 0x51), RGB(0x51, 0x71, 0x59), RGB(0x51, 0x71, 0x61), RGB(0x51, 0x71, 0x69),
154 RGB(0x51, 0x71, 0x71), RGB(0x51, 0x69, 0x71), RGB(0x51, 0x61, 0x71), RGB(0x51, 0x59, 0x71),
155 RGB(0x00, 0x00, 0x41), RGB(0x10, 0x00, 0x41), RGB(0x20, 0x00, 0x41), RGB(0x30, 0x00, 0x41),
156 RGB(0x41, 0x00, 0x41), RGB(0x41, 0x00, 0x30), RGB(0x41, 0x00, 0x20), RGB(0x41, 0x00, 0x10),
157 RGB(0x41, 0x00, 0x00), RGB(0x41, 0x10, 0x00), RGB(0x41, 0x20, 0x00), RGB(0x41, 0x30, 0x00),
158 RGB(0x41, 0x41, 0x00), RGB(0x30, 0x41, 0x00), RGB(0x20, 0x41, 0x00), RGB(0x10, 0x41, 0x00),
160 RGB(0x00, 0x41, 0x00), RGB(0x00, 0x41, 0x10), RGB(0x00, 0x41, 0x20), RGB(0x00, 0x41, 0x30),
161 RGB(0x00, 0x41, 0x41), RGB(0x00, 0x30, 0x41), RGB(0x00, 0x20, 0x41), RGB(0x00, 0x10, 0x41),
162 RGB(0x20, 0x20, 0x41), RGB(0x28, 0x20, 0x41), RGB(0x30, 0x20, 0x41), RGB(0x38, 0x20, 0x41),
163 RGB(0x41, 0x20, 0x41), RGB(0x41, 0x20, 0x38), RGB(0x41, 0x20, 0x30), RGB(0x41, 0x20, 0x28),
164 RGB(0x41, 0x20, 0x20), RGB(0x41, 0x28, 0x20), RGB(0x41, 0x30, 0x20), RGB(0x41, 0x38, 0x20),
165 RGB(0x41, 0x41, 0x20), RGB(0x38, 0x41, 0x20), RGB(0x30, 0x41, 0x20), RGB(0x28, 0x41, 0x20),
166 RGB(0x20, 0x41, 0x20), RGB(0x20, 0x41, 0x28), RGB(0x20, 0x41, 0x30), RGB(0x20, 0x41, 0x38),
167 RGB(0x20, 0x41, 0x41), RGB(0x20, 0x38, 0x41), RGB(0x20, 0x30, 0x41), RGB(0x20, 0x28, 0x41),
168 RGB(0x2C, 0x2C, 0x41), RGB(0x30, 0x2C, 0x41), RGB(0x34, 0x2C, 0x41), RGB(0x3C, 0x2C, 0x41),
169 RGB(0x41, 0x2C, 0x41), RGB(0x41, 0x2C, 0x3C), RGB(0x41, 0x2C, 0x34), RGB(0x41, 0x2C, 0x30),
170 RGB(0x41, 0x2C, 0x2C), RGB(0x41, 0x30, 0x2C), RGB(0x41, 0x34, 0x2C), RGB(0x41, 0x3C, 0x2C),
171 RGB(0x41, 0x41, 0x2C), RGB(0x3C, 0x41, 0x2C), RGB(0x34, 0x41, 0x2C), RGB(0x30, 0x41, 0x2C),
172 RGB(0x2C, 0x41, 0x2C), RGB(0x2C, 0x41, 0x30), RGB(0x2C, 0x41, 0x34), RGB(0x2C, 0x41, 0x3C),
173 RGB(0x2C, 0x41, 0x41), RGB(0x2C, 0x3C, 0x41), RGB(0x2C, 0x34, 0x41), RGB(0x2C, 0x30, 0x41),
174 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00),
175 RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00), RGB(0x00, 0x00, 0x00)
181 * Default 16-color palette for foreground and background
182 * (corresponding flags in comments).
183 * Taken from subsystems/win32/winsrv/consrv/frontends/gui/conwnd.c
185 static const COLORREF ConsoleColors
[16] =
187 RGB(0, 0, 0), // (Black)
188 RGB(0, 0, 128), // BLUE
189 RGB(0, 128, 0), // GREEN
190 RGB(0, 128, 128), // BLUE | GREEN
191 RGB(128, 0, 0), // RED
192 RGB(128, 0, 128), // BLUE | RED
193 RGB(128, 128, 0), // GREEN | RED
194 RGB(192, 192, 192), // BLUE | GREEN | RED
196 RGB(128, 128, 128), // (Grey) INTENSITY
197 RGB(0, 0, 255), // BLUE | INTENSITY
198 RGB(0, 255, 0), // GREEN | INTENSITY
199 RGB(0, 255, 255), // BLUE | GREEN | INTENSITY
200 RGB(255, 0, 0), // RED | INTENSITY
201 RGB(255, 0, 255), // BLUE | RED | INTENSITY
202 RGB(255, 255, 0), // GREEN | RED | INTENSITY
203 RGB(255, 255, 255) // BLUE | GREEN | RED | INTENSITY
207 * Console interface -- VGA-mode-agnostic
209 typedef struct _CHAR_CELL
213 } CHAR_CELL
, *PCHAR_CELL
;
214 C_ASSERT(sizeof(CHAR_CELL
) == 2);
216 static LPVOID ConsoleFramebuffer
= NULL
; // Active framebuffer, points to
217 // either TextFramebuffer or a valid
218 // graphics framebuffer.
219 static HPALETTE TextPaletteHandle
= NULL
;
220 static HPALETTE PaletteHandle
= NULL
;
222 static HANDLE StartEvent
= NULL
;
223 static HANDLE EndEvent
= NULL
;
224 static HANDLE AnotherEvent
= NULL
;
226 static CONSOLE_CURSOR_INFO OrgConsoleCursorInfo
;
227 static CONSOLE_SCREEN_BUFFER_INFO OrgConsoleBufferInfo
;
231 * Text mode -- we always keep a valid text mode framebuffer
232 * even if we are in graphics mode. This is needed in order to
233 * keep a consistent VGA state.
235 static CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo
;
236 static COORD TextResolution
= {0};
237 static PCHAR_CELL TextFramebuffer
= NULL
;
238 static HANDLE TextConsoleBuffer
= NULL
;
241 static HANDLE GraphicsConsoleBuffer
= NULL
;
242 static HANDLE ConsoleMutex
= NULL
;
243 /* DoubleVision support */
244 static BOOLEAN DoubleWidth
= FALSE
;
245 static BOOLEAN DoubleHeight
= FALSE
;
250 static BYTE VgaMemory
[VGA_NUM_BANKS
* VGA_BANK_SIZE
];
252 static BYTE VgaLatchRegisters
[VGA_NUM_BANKS
] = {0, 0, 0, 0};
254 static BYTE VgaMiscRegister
;
255 static BYTE VgaFeatureRegister
;
257 static BYTE VgaSeqIndex
= VGA_SEQ_RESET_REG
;
258 static BYTE VgaSeqRegisters
[VGA_SEQ_MAX_REG
];
260 static BYTE VgaCrtcIndex
= VGA_CRTC_HORZ_TOTAL_REG
;
261 static BYTE VgaCrtcRegisters
[VGA_CRTC_MAX_REG
];
263 static BYTE VgaGcIndex
= VGA_GC_RESET_REG
;
264 static BYTE VgaGcRegisters
[VGA_GC_MAX_REG
];
266 static BOOLEAN VgaAcLatch
= FALSE
;
267 static BOOLEAN VgaAcPalDisable
= TRUE
;
268 static BYTE VgaAcIndex
= VGA_AC_PAL_0_REG
;
269 static BYTE VgaAcRegisters
[VGA_AC_MAX_REG
];
271 // static VGA_REGISTERS VgaRegisters;
273 static BYTE VgaDacMask
= 0xFF;
274 static WORD VgaDacIndex
= 0;
275 static BOOLEAN VgaDacReadWrite
= FALSE
;
276 static BYTE VgaDacRegisters
[VGA_PALETTE_SIZE
];
278 static BOOLEAN InVerticalRetrace
= FALSE
;
279 static BOOLEAN InHorizontalRetrace
= FALSE
;
281 static BOOLEAN NeedsUpdate
= FALSE
;
282 static BOOLEAN ModeChanged
= FALSE
;
283 static BOOLEAN CursorMoved
= FALSE
;
284 static BOOLEAN PaletteChanged
= FALSE
;
291 } ScreenMode
= TEXT_MODE
;
293 static SMALL_RECT UpdateRectangle
= { 0, 0, 0, 0 };
295 /* RegisterConsoleVDM EMULATION ***********************************************/
297 #include <ntddvdeo.h>
299 #ifdef USE_REAL_REGISTERCONSOLEVDM
301 #define __RegisterConsoleVDM RegisterConsoleVDM
302 #define __InvalidateConsoleDIBits InvalidateConsoleDIBits
307 * This private buffer, per-console, is used by
308 * RegisterConsoleVDM and InvalidateConsoleDIBits.
310 static COORD VDMBufferSize
= {0};
311 static PCHAR_CELL VDMBuffer
= NULL
;
313 static PCHAR_INFO CharBuff
= NULL
; // This is a hack, which is unneeded
314 // for the real RegisterConsoleVDM and
315 // InvalidateConsoleDIBits
319 __RegisterConsoleVDM(IN DWORD dwRegisterFlags
,
320 IN HANDLE hStartHardwareEvent
,
321 IN HANDLE hEndHardwareEvent
,
322 IN HANDLE hErrorHardwareEvent
,
323 IN DWORD dwUnusedVar
,
324 OUT LPDWORD lpVideoStateLength
,
325 OUT PVOID
* lpVideoState
, // PVIDEO_HARDWARE_STATE_HEADER*
326 IN PVOID lpUnusedBuffer
,
327 IN DWORD dwUnusedBufferLength
,
328 IN COORD dwVDMBufferSize
,
329 OUT PVOID
* lpVDMBuffer
)
331 UNREFERENCED_PARAMETER(hErrorHardwareEvent
);
332 UNREFERENCED_PARAMETER(dwUnusedVar
);
333 UNREFERENCED_PARAMETER(lpVideoStateLength
);
334 UNREFERENCED_PARAMETER(lpVideoState
);
335 UNREFERENCED_PARAMETER(lpUnusedBuffer
);
336 UNREFERENCED_PARAMETER(dwUnusedBufferLength
);
339 DPRINT1("__RegisterConsoleVDM(%d)\n", dwRegisterFlags
);
341 if (lpVDMBuffer
== NULL
) return FALSE
;
343 if (dwRegisterFlags
!= 0)
345 // if (hStartHardwareEvent == NULL || hEndHardwareEvent == NULL) return FALSE;
346 if (VDMBuffer
!= NULL
) return FALSE
;
348 VDMBufferSize
= dwVDMBufferSize
;
350 /* HACK: Cache -- to be removed in the real implementation */
351 CharBuff
= HeapAlloc(GetProcessHeap(),
353 VDMBufferSize
.X
* VDMBufferSize
.Y
354 * sizeof(CHAR_INFO
));
357 VDMBuffer
= HeapAlloc(GetProcessHeap(),
359 VDMBufferSize
.X
* VDMBufferSize
.Y
360 * sizeof(CHAR_CELL
));
361 *lpVDMBuffer
= VDMBuffer
;
362 return (VDMBuffer
!= NULL
);
366 /* HACK: Cache -- to be removed in the real implementation */
367 if (CharBuff
) HeapFree(GetProcessHeap(), 0, CharBuff
);
370 if (VDMBuffer
) HeapFree(GetProcessHeap(), 0, VDMBuffer
);
373 VDMBufferSize
.X
= VDMBufferSize
.Y
= 0;
380 __InvalidateConsoleDIBits(IN HANDLE hConsoleOutput
,
381 IN PSMALL_RECT lpRect
)
383 if ((hConsoleOutput
== TextConsoleBuffer
) && (VDMBuffer
!= NULL
))
385 /* HACK: Write the cached data to the console */
387 COORD Origin
= { lpRect
->Left
, lpRect
->Top
};
392 for (i
= 0; i
< VDMBufferSize
.Y
; i
++)
394 for (j
= 0; j
< VDMBufferSize
.X
; j
++)
396 CharBuff
[i
* VDMBufferSize
.X
+ j
].Char
.AsciiChar
= VDMBuffer
[i
* VDMBufferSize
.X
+ j
].Char
;
397 CharBuff
[i
* VDMBufferSize
.X
+ j
].Attributes
= VDMBuffer
[i
* VDMBufferSize
.X
+ j
].Attributes
;
401 WriteConsoleOutputA(hConsoleOutput
,
408 return InvalidateConsoleDIBits(hConsoleOutput
, lpRect
);
413 /* PRIVATE FUNCTIONS **********************************************************/
415 static inline DWORD
VgaGetAddressSize(VOID
);
416 static VOID
VgaUpdateTextCursor(VOID
);
418 static VOID
VgaUpdateCursorPosition(VOID
)
421 * Update the cursor position in the VGA registers.
423 WORD Offset
= ConsoleInfo
.dwCursorPosition
.Y
* TextResolution
.X
+
424 ConsoleInfo
.dwCursorPosition
.X
;
426 VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_LOW_REG
] = LOBYTE(Offset
);
427 VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_HIGH_REG
] = HIBYTE(Offset
);
429 // VidBiosSyncCursorPosition();
430 VgaUpdateTextCursor();
433 static BOOL
VgaAttachToConsoleInternal(PCOORD Resolution
)
437 PVIDEO_HARDWARE_STATE_HEADER State
;
439 #ifdef USE_REAL_REGISTERCONSOLEVDM
440 PCHAR_INFO CharBuff
= NULL
;
443 DWORD AddressSize
, ScanlineSize
;
447 COORD Origin
= { 0, 0 };
449 ASSERT(TextFramebuffer
== NULL
);
451 TextResolution
= *Resolution
;
454 * Windows 2k3 winsrv.dll calls NtVdmControl(VdmQueryVdmProcess == 14, &ConsoleHandle);
455 * in the two following APIs:
456 * SrvRegisterConsoleVDM (corresponding Win32 API: RegisterConsoleVDM)
457 * SrvVDMConsoleOperation (corresponding Win32 API: )
458 * to check whether the current process is a VDM process, and fails otherwise with the
459 * error 0xC0000022 ().
461 * It is worth it to notice that also basesrv.dll does the same only for the
462 * BaseSrvIsFirstVDM API...
466 __RegisterConsoleVDM(1,
469 AnotherEvent
, // NULL,
471 &Length
, // NULL, <-- putting this (and null in the next var) makes the API returning error 12 "ERROR_INVALID_ACCESS"
472 (PVOID
*)&State
, // NULL,
476 (PVOID
*)&TextFramebuffer
);
479 DisplayMessage(L
"RegisterConsoleVDM failed with error %d\n", GetLastError());
484 #ifdef USE_REAL_REGISTERCONSOLEVDM
485 CharBuff
= HeapAlloc(GetProcessHeap(),
487 TextResolution
.X
* TextResolution
.Y
488 * sizeof(CHAR_INFO
));
496 ConRect
.Top
= ConsoleInfo
.srWindow
.Top
;
497 ConRect
.Right
= ConRect
.Left
+ Resolution
->X
- 1;
498 ConRect
.Bottom
= ConRect
.Top
+ Resolution
->Y
- 1;
500 * Use this trick to effectively resize the console buffer and window,
502 * - SetConsoleScreenBufferSize fails if the new console screen buffer size
503 * is smaller than the current console window size, and:
504 * - SetConsoleWindowInfo fails if the new console window size is larger
505 * than the current console screen buffer size.
507 SetConsoleScreenBufferSize(TextConsoleBuffer
, *Resolution
);
508 SetConsoleWindowInfo(TextConsoleBuffer
, TRUE
, &ConRect
);
509 SetConsoleScreenBufferSize(TextConsoleBuffer
, *Resolution
);
510 /* Update the saved console information */
511 GetConsoleScreenBufferInfo(TextConsoleBuffer
, &ConsoleInfo
);
514 * Copy console data into VGA memory
518 AddressSize
= VgaGetAddressSize();
519 ConRect
.Left
= ConRect
.Top
= 0;
520 ConRect
.Right
= TextResolution
.X
;
521 ConRect
.Bottom
= TextResolution
.Y
;
522 ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
524 /* Read the data from the console into the framebuffer... */
525 ReadConsoleOutputA(TextConsoleBuffer
,
531 /* ... and copy the framebuffer into the VGA memory */
533 /* Loop through the scanlines */
534 for (i
= 0; i
< TextResolution
.Y
; i
++)
536 /* Loop through the characters */
537 for (j
= 0; j
< TextResolution
.X
; j
++)
539 CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
541 /* Store the character in plane 0 */
542 VgaMemory
[CurrentAddr
] = CharBuff
[i
* TextResolution
.X
+ j
].Char
.AsciiChar
;
544 /* Store the attribute in plane 1 */
545 VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
] = (BYTE
)CharBuff
[i
* TextResolution
.X
+ j
].Attributes
;
548 /* Move to the next scanline */
549 Address
+= ScanlineSize
;
552 #ifdef USE_REAL_REGISTERCONSOLEVDM
553 if (CharBuff
) HeapFree(GetProcessHeap(), 0, CharBuff
);
556 VgaUpdateCursorPosition();
561 static BOOL
IsConsoleHandle(HANDLE hHandle
)
565 /* Check whether the handle may be that of a console... */
566 if ((GetFileType(hHandle
) & ~FILE_TYPE_REMOTE
) != FILE_TYPE_CHAR
)
570 * It may be. Perform another test... The idea comes from the
571 * MSDN description of the WriteConsole API:
573 * "WriteConsole fails if it is used with a standard handle
574 * that is redirected to a file. If an application processes
575 * multilingual output that can be redirected, determine whether
576 * the output handle is a console handle (one method is to call
577 * the GetConsoleMode function and check whether it succeeds).
578 * If the handle is a console handle, call WriteConsole. If the
579 * handle is not a console handle, the output is redirected and
580 * you should call WriteFile to perform the I/O."
582 return GetConsoleMode(hHandle
, &dwMode
);
585 static inline DWORD
VgaGetAddressSize(VOID
)
587 if (VgaCrtcRegisters
[VGA_CRTC_UNDERLINE_REG
] & VGA_CRTC_UNDERLINE_DWORD
)
589 /* Double-word addressing */
590 return 4; // sizeof(DWORD)
592 else if (VgaCrtcRegisters
[VGA_CRTC_MODE_CONTROL_REG
] & VGA_CRTC_MODE_CONTROL_BYTE
)
594 /* Byte addressing */
595 return 1; // sizeof(BYTE)
599 /* Word addressing */
600 return 2; // sizeof(WORD)
604 static inline DWORD
VgaTranslateReadAddress(DWORD Address
)
606 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
609 /* Check for chain-4 and odd-even mode */
610 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
612 /* The lowest two bits are the plane number */
616 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
618 /* The LSB is the plane number */
624 /* Use the read mode */
625 Plane
= VgaGcRegisters
[VGA_GC_READ_MAP_SEL_REG
] & 0x03;
628 /* Multiply the offset by the address size */
629 Offset
*= VgaGetAddressSize();
631 return Offset
+ Plane
* VGA_BANK_SIZE
;
634 static inline DWORD
VgaTranslateWriteAddress(DWORD Address
)
636 DWORD Offset
= Address
- VgaGetVideoBaseAddress();
638 /* Check for chain-4 and odd-even mode */
639 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
641 /* Shift the offset to the right by 2 */
644 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
646 /* Shift the offset to the right by 1 */
650 /* Multiply the offset by the address size */
651 Offset
*= VgaGetAddressSize();
653 /* Return the offset on plane 0 */
657 static inline BYTE
VgaTranslateByteForWriting(BYTE Data
, BYTE Plane
)
659 BYTE WriteMode
= VgaGcRegisters
[VGA_GC_MODE_REG
] & 3;
660 BYTE BitMask
= VgaGcRegisters
[VGA_GC_BITMASK_REG
];
664 /* In write mode 1 just return the latch register */
665 return VgaLatchRegisters
[Plane
];
670 /* Write modes 0 and 3 rotate the data to the right first */
671 BYTE RotateCount
= VgaGcRegisters
[VGA_GC_ROTATE_REG
] & 7;
672 Data
= LOBYTE(((DWORD
)Data
>> RotateCount
) | ((DWORD
)Data
<< (8 - RotateCount
)));
676 /* Write mode 2 expands the appropriate bit to all 8 bits */
677 Data
= (Data
& (1 << Plane
)) ? 0xFF : 0x00;
683 * In write mode 0, the enable set/reset register decides if the
684 * set/reset bit should be expanded to all 8 bits.
686 if (VgaGcRegisters
[VGA_GC_ENABLE_RESET_REG
] & (1 << Plane
))
688 /* Copy the bit from the set/reset register to all 8 bits */
689 Data
= (VgaGcRegisters
[VGA_GC_RESET_REG
] & (1 << Plane
)) ? 0xFF : 0x00;
695 /* Write modes 0 and 2 then perform a logical operation on the data and latch */
696 BYTE LogicalOperation
= (VgaGcRegisters
[VGA_GC_ROTATE_REG
] >> 3) & 3;
698 if (LogicalOperation
== 1) Data
&= VgaLatchRegisters
[Plane
];
699 else if (LogicalOperation
== 2) Data
|= VgaLatchRegisters
[Plane
];
700 else if (LogicalOperation
== 3) Data
^= VgaLatchRegisters
[Plane
];
704 /* For write mode 3, we AND the bitmask with the data, which is used as the new bitmask */
707 /* Then we expand the bit in the set/reset field */
708 Data
= (VgaGcRegisters
[VGA_GC_RESET_REG
] & (1 << Plane
)) ? 0xFF : 0x00;
711 /* Bits cleared in the bitmask are replaced with latch register bits */
712 Data
= (Data
& BitMask
) | (VgaLatchRegisters
[Plane
] & (~BitMask
));
714 /* Return the byte */
718 static inline VOID
VgaMarkForUpdate(SHORT Row
, SHORT Column
)
720 /* Check if this is the first time the rectangle is updated */
723 UpdateRectangle
.Left
= UpdateRectangle
.Top
= MAXSHORT
;
724 UpdateRectangle
.Right
= UpdateRectangle
.Bottom
= MINSHORT
;
727 /* Expand the rectangle to include the point */
728 UpdateRectangle
.Left
= min(UpdateRectangle
.Left
, Column
);
729 UpdateRectangle
.Right
= max(UpdateRectangle
.Right
, Column
);
730 UpdateRectangle
.Top
= min(UpdateRectangle
.Top
, Row
);
731 UpdateRectangle
.Bottom
= max(UpdateRectangle
.Bottom
, Row
);
733 /* Set the update request flag */
737 static VOID
VgaWriteSequencer(BYTE Data
)
739 ASSERT(VgaSeqIndex
< VGA_SEQ_MAX_REG
);
742 VgaSeqRegisters
[VgaSeqIndex
] = Data
;
745 static VOID
VgaWriteGc(BYTE Data
)
747 ASSERT(VgaGcIndex
< VGA_GC_MAX_REG
);
750 VgaGcRegisters
[VgaGcIndex
] = Data
;
752 /* Check the index */
755 case VGA_GC_MISC_REG
:
757 /* The GC misc register decides if it's text or graphics mode */
764 static VOID
VgaWriteCrtc(BYTE Data
)
766 ASSERT(VgaGcIndex
< VGA_CRTC_MAX_REG
);
769 VgaCrtcRegisters
[VgaCrtcIndex
] = Data
;
771 /* Check the index */
772 switch (VgaCrtcIndex
)
774 case VGA_CRTC_END_HORZ_DISP_REG
:
775 case VGA_CRTC_VERT_DISP_END_REG
:
776 case VGA_CRTC_OVERFLOW_REG
:
778 /* The video mode has changed */
783 case VGA_CRTC_CURSOR_LOC_LOW_REG
:
784 case VGA_CRTC_CURSOR_LOC_HIGH_REG
:
785 case VGA_CRTC_CURSOR_START_REG
:
786 case VGA_CRTC_CURSOR_END_REG
:
788 /* Set the cursor moved flag */
795 static VOID
VgaWriteDac(BYTE Data
)
801 VgaDacRegisters
[VgaDacIndex
] = Data
;
803 /* Find the palette index */
804 PaletteIndex
= VgaDacIndex
/ 3;
806 /* Fill the entry structure */
807 Entry
.peRed
= VGA_DAC_TO_COLOR(VgaDacRegisters
[PaletteIndex
* 3]);
808 Entry
.peGreen
= VGA_DAC_TO_COLOR(VgaDacRegisters
[PaletteIndex
* 3 + 1]);
809 Entry
.peBlue
= VGA_DAC_TO_COLOR(VgaDacRegisters
[PaletteIndex
* 3 + 2]);
812 /* Update the palette entry */
813 SetPaletteEntries(PaletteHandle
, PaletteIndex
, 1, &Entry
);
815 /* Check which text palette entries are affected */
816 for (i
= 0; i
<= VGA_AC_PAL_F_REG
; i
++)
818 if (VgaAcRegisters
[i
] == PaletteIndex
)
820 /* Update the text palette entry */
821 SetPaletteEntries(TextPaletteHandle
, i
, 1, &Entry
);
825 /* Set the palette changed flag */
826 PaletteChanged
= TRUE
;
828 /* Update the index */
830 VgaDacIndex
%= VGA_PALETTE_SIZE
;
833 static VOID
VgaWriteAc(BYTE Data
)
837 ASSERT(VgaAcIndex
< VGA_AC_MAX_REG
);
840 if (VgaAcIndex
<= VGA_AC_PAL_F_REG
)
842 if (VgaAcPalDisable
) return;
844 // DbgPrint(" AC Palette Writing %d to index %d\n", Data, VgaAcIndex);
845 if (VgaAcRegisters
[VgaAcIndex
] != Data
)
847 /* Update the AC register */
848 VgaAcRegisters
[VgaAcIndex
] = Data
;
850 /* Fill the entry structure */
851 Entry
.peRed
= VGA_DAC_TO_COLOR(VgaDacRegisters
[Data
* 3]);
852 Entry
.peGreen
= VGA_DAC_TO_COLOR(VgaDacRegisters
[Data
* 3 + 1]);
853 Entry
.peBlue
= VGA_DAC_TO_COLOR(VgaDacRegisters
[Data
* 3 + 2]);
856 /* Update the palette entry and set the palette change flag */
857 SetPaletteEntries(TextPaletteHandle
, VgaAcIndex
, 1, &Entry
);
858 PaletteChanged
= TRUE
;
863 VgaAcRegisters
[VgaAcIndex
] = Data
;
867 static VOID
VgaRestoreDefaultPalette(PPALETTEENTRY Entries
, USHORT NumOfEntries
)
871 /* Copy the colors of the default palette to the DAC and console palette */
872 for (i
= 0; i
< NumOfEntries
; i
++)
874 /* Set the palette entries */
875 Entries
[i
].peRed
= GetRValue(VgaDefaultPalette
[i
]);
876 Entries
[i
].peGreen
= GetGValue(VgaDefaultPalette
[i
]);
877 Entries
[i
].peBlue
= GetBValue(VgaDefaultPalette
[i
]);
878 Entries
[i
].peFlags
= 0;
880 /* Set the DAC registers */
881 VgaDacRegisters
[i
* 3] = VGA_COLOR_TO_DAC(GetRValue(VgaDefaultPalette
[i
]));
882 VgaDacRegisters
[i
* 3 + 1] = VGA_COLOR_TO_DAC(GetGValue(VgaDefaultPalette
[i
]));
883 VgaDacRegisters
[i
* 3 + 2] = VGA_COLOR_TO_DAC(GetBValue(VgaDefaultPalette
[i
]));
887 static BOOLEAN
VgaInitializePalette(VOID
)
890 BOOLEAN Result
= FALSE
;
891 LPLOGPALETTE Palette
, TextPalette
;
893 /* Allocate storage space for the palettes */
894 Palette
= (LPLOGPALETTE
)HeapAlloc(GetProcessHeap(),
897 VGA_MAX_COLORS
* sizeof(PALETTEENTRY
));
898 TextPalette
= (LPLOGPALETTE
)HeapAlloc(GetProcessHeap(),
901 (VGA_AC_PAL_F_REG
+ 1) * sizeof(PALETTEENTRY
));
902 if ((Palette
== NULL
) || (TextPalette
== NULL
)) goto Cleanup
;
904 /* Initialize the palettes */
905 Palette
->palVersion
= TextPalette
->palVersion
= 0x0300;
906 Palette
->palNumEntries
= VGA_MAX_COLORS
;
907 TextPalette
->palNumEntries
= VGA_AC_PAL_F_REG
+ 1;
909 /* Restore the default graphics palette */
910 VgaRestoreDefaultPalette(Palette
->palPalEntry
, Palette
->palNumEntries
);
912 /* Set the default text palette */
913 for (i
= 0; i
< TextPalette
->palNumEntries
; i
++)
915 /* Set the palette entries */
916 TextPalette
->palPalEntry
[i
].peRed
= GetRValue(ConsoleColors
[i
]);
917 TextPalette
->palPalEntry
[i
].peGreen
= GetGValue(ConsoleColors
[i
]);
918 TextPalette
->palPalEntry
[i
].peBlue
= GetBValue(ConsoleColors
[i
]);
919 TextPalette
->palPalEntry
[i
].peFlags
= 0;
922 /* Create the palettes */
923 PaletteHandle
= CreatePalette(Palette
);
924 TextPaletteHandle
= CreatePalette(TextPalette
);
926 if (PaletteHandle
!= NULL
&& TextPaletteHandle
!= NULL
)
928 /* The palettes have been created successfully */
933 /* Free the palettes */
934 if (Palette
) HeapFree(GetProcessHeap(), 0, Palette
);
935 if (TextPalette
) HeapFree(GetProcessHeap(), 0, TextPalette
);
939 /* Something failed, delete the palettes */
940 if (PaletteHandle
) DeleteObject(PaletteHandle
);
941 if (TextPaletteHandle
) DeleteObject(TextPaletteHandle
);
947 static VOID
VgaSetActiveScreenBuffer(HANDLE ScreenBuffer
)
949 /* Set the active buffer */
950 SetConsoleActiveScreenBuffer(ScreenBuffer
);
952 /* Reinitialize the VDM menu */
954 CreateVdmMenu(ScreenBuffer
);
957 static BOOL
VgaEnterGraphicsMode(PCOORD Resolution
)
960 CONSOLE_GRAPHICS_BUFFER_INFO GraphicsBufferInfo
;
961 BYTE BitmapInfoBuffer
[VGA_BITMAP_INFO_SIZE
];
962 LPBITMAPINFO BitmapInfo
= (LPBITMAPINFO
)BitmapInfoBuffer
;
963 LPWORD PaletteIndex
= (LPWORD
)(BitmapInfo
->bmiColors
);
965 LONG Width
= Resolution
->X
;
966 LONG Height
= Resolution
->Y
;
968 /* Use DoubleVision mode if the resolution is too small */
969 DoubleWidth
= (Width
< VGA_MINIMUM_WIDTH
);
970 if (DoubleWidth
) Width
*= 2;
971 DoubleHeight
= (Height
< VGA_MINIMUM_HEIGHT
);
972 if (DoubleHeight
) Height
*= 2;
974 /* Fill the bitmap info header */
975 RtlZeroMemory(&BitmapInfo
->bmiHeader
, sizeof(BitmapInfo
->bmiHeader
));
976 BitmapInfo
->bmiHeader
.biSize
= sizeof(BitmapInfo
->bmiHeader
);
977 BitmapInfo
->bmiHeader
.biWidth
= Width
;
978 BitmapInfo
->bmiHeader
.biHeight
= Height
;
979 BitmapInfo
->bmiHeader
.biBitCount
= 8;
980 BitmapInfo
->bmiHeader
.biPlanes
= 1;
981 BitmapInfo
->bmiHeader
.biCompression
= BI_RGB
;
982 BitmapInfo
->bmiHeader
.biSizeImage
= Width
* Height
/* * 1 == biBitCount / 8 */;
984 /* Fill the palette data */
985 for (i
= 0; i
< (VGA_PALETTE_SIZE
/ 3); i
++) PaletteIndex
[i
] = (WORD
)i
;
987 /* Fill the console graphics buffer info */
988 GraphicsBufferInfo
.dwBitMapInfoLength
= VGA_BITMAP_INFO_SIZE
;
989 GraphicsBufferInfo
.lpBitMapInfo
= BitmapInfo
;
990 GraphicsBufferInfo
.dwUsage
= DIB_PAL_COLORS
;
992 /* Create the buffer */
993 GraphicsConsoleBuffer
= CreateConsoleScreenBuffer(GENERIC_READ
| GENERIC_WRITE
,
994 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
996 CONSOLE_GRAPHICS_BUFFER
,
997 &GraphicsBufferInfo
);
998 if (GraphicsConsoleBuffer
== INVALID_HANDLE_VALUE
) return FALSE
;
1000 /* Save the framebuffer address and mutex */
1001 ConsoleFramebuffer
= GraphicsBufferInfo
.lpBitMap
;
1002 ConsoleMutex
= GraphicsBufferInfo
.hMutex
;
1004 /* Clear the framebuffer */
1005 RtlZeroMemory(ConsoleFramebuffer
, BitmapInfo
->bmiHeader
.biSizeImage
);
1007 /* Set the active buffer */
1008 VgaSetActiveScreenBuffer(GraphicsConsoleBuffer
);
1010 /* Set the graphics mode palette */
1011 SetConsolePalette(GraphicsConsoleBuffer
,
1013 SYSPAL_NOSTATIC256
);
1015 /* Set the screen mode flag */
1016 ScreenMode
= GRAPHICS_MODE
;
1021 static VOID
VgaLeaveGraphicsMode(VOID
)
1023 /* Release the console framebuffer mutex */
1024 ReleaseMutex(ConsoleMutex
);
1026 /* Switch back to the default console text buffer */
1027 // VgaSetActiveScreenBuffer(TextConsoleBuffer);
1029 /* Cleanup the video data */
1030 CloseHandle(ConsoleMutex
);
1031 ConsoleMutex
= NULL
;
1032 ConsoleFramebuffer
= NULL
;
1033 CloseHandle(GraphicsConsoleBuffer
);
1034 GraphicsConsoleBuffer
= NULL
;
1036 DoubleWidth
= FALSE
;
1037 DoubleHeight
= FALSE
;
1040 static BOOL
VgaEnterTextMode(PCOORD Resolution
)
1042 DPRINT1("VgaEnterTextMode\n");
1044 /* Switch to the text buffer */
1045 VgaSetActiveScreenBuffer(TextConsoleBuffer
);
1047 /* Adjust the text framebuffer if we changed the resolution */
1048 if (TextResolution
.X
!= Resolution
->X
||
1049 TextResolution
.Y
!= Resolution
->Y
)
1051 VgaDetachFromConsole(TRUE
);
1054 * VgaAttachToConsoleInternal sets TextResolution to the
1055 * new resolution and updates ConsoleInfo.
1057 if (!VgaAttachToConsoleInternal(Resolution
))
1059 DisplayMessage(L
"An unexpected error occurred!\n");
1060 EmulatorTerminate();
1066 VgaUpdateCursorPosition();
1069 /* The active framebuffer is now the text framebuffer */
1070 ConsoleFramebuffer
= TextFramebuffer
;
1073 * Set the text mode palette.
1075 * WARNING: This call should fail on Windows (and therefore
1076 * we get the default palette and our external behaviour is
1077 * just like Windows' one), but it should success on ReactOS
1078 * (so that we get console palette changes even for text-mode
1079 * screen-buffers, which is a new feature on ReactOS).
1081 SetConsolePalette(TextConsoleBuffer
,
1083 SYSPAL_NOSTATIC256
);
1085 /* Set the screen mode flag */
1086 ScreenMode
= TEXT_MODE
;
1091 static VOID
VgaLeaveTextMode(VOID
)
1093 /* Reset the active framebuffer */
1094 ConsoleFramebuffer
= NULL
;
1097 static VOID
VgaChangeMode(VOID
)
1099 COORD Resolution
= VgaGetDisplayResolution();
1101 if (ScreenMode
== GRAPHICS_MODE
)
1103 /* Leave the current graphics mode */
1104 VgaLeaveGraphicsMode();
1108 /* Leave the current text mode */
1112 /* Check if the new mode is alphanumeric */
1113 if (!(VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
))
1115 /* Enter new text mode */
1116 if (!VgaEnterTextMode(&Resolution
))
1118 DisplayMessage(L
"An unexpected VGA error occurred while switching into text mode. Error: %u", GetLastError());
1119 EmulatorTerminate();
1125 /* Enter graphics mode */
1126 if (!VgaEnterGraphicsMode(&Resolution
))
1128 DisplayMessage(L
"An unexpected VGA error occurred while switching into graphics mode. Error: %u", GetLastError());
1129 EmulatorTerminate();
1134 /* Trigger a full update of the screen */
1136 UpdateRectangle
.Left
= 0;
1137 UpdateRectangle
.Top
= 0;
1138 UpdateRectangle
.Right
= Resolution
.X
;
1139 UpdateRectangle
.Bottom
= Resolution
.Y
;
1141 /* Reset the mode change flag */
1142 ModeChanged
= FALSE
;
1145 static VOID
VgaUpdateFramebuffer(VOID
)
1148 COORD Resolution
= VgaGetDisplayResolution();
1149 DWORD AddressSize
= VgaGetAddressSize();
1150 DWORD Address
= MAKEWORD(VgaCrtcRegisters
[VGA_CRTC_START_ADDR_LOW_REG
],
1151 VgaCrtcRegisters
[VGA_CRTC_START_ADDR_HIGH_REG
]);
1152 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
1155 * If console framebuffer is NULL, that means something went wrong
1156 * earlier and this is the final display refresh.
1158 if (ConsoleFramebuffer
== NULL
) return;
1160 /* Check if this is text mode or graphics mode */
1161 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
1164 PBYTE GraphicsBuffer
= (PBYTE
)ConsoleFramebuffer
;
1165 DWORD InterlaceHighBit
= VGA_INTERLACE_HIGH_BIT
;
1168 * Synchronize access to the graphics framebuffer
1169 * with the console framebuffer mutex.
1171 WaitForSingleObject(ConsoleMutex
, INFINITE
);
1173 /* Shift the high bit right by 1 in odd/even mode */
1174 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
1176 InterlaceHighBit
>>= 1;
1179 /* Loop through the scanlines */
1180 for (i
= 0; i
< Resolution
.Y
; i
++)
1182 if ((VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_OE
) && (i
& 1))
1184 /* Odd-numbered line in interlaced mode - set the high bit */
1185 Address
|= InterlaceHighBit
;
1188 /* Loop through the pixels */
1189 for (j
= 0; j
< Resolution
.X
; j
++)
1193 /* Check the shifting mode */
1194 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFT256
)
1196 /* 4 bits shifted from each plane */
1198 /* Check if this is 16 or 256 color mode */
1199 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
1201 /* One byte per pixel */
1202 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
1203 + LOWORD((Address
+ (j
/ VGA_NUM_BANKS
))
1208 /* 4-bits per pixel */
1210 PixelData
= VgaMemory
[(j
% VGA_NUM_BANKS
) * VGA_BANK_SIZE
1211 + LOWORD((Address
+ (j
/ (VGA_NUM_BANKS
* 2)))
1214 /* Check if we should use the highest 4 bits or lowest 4 */
1215 if (((j
/ VGA_NUM_BANKS
) % 2) == 0)
1227 else if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_SHIFTREG
)
1229 /* Check if this is 16 or 256 color mode */
1230 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
1232 // TODO: NOT IMPLEMENTED
1233 DPRINT1("8-bit interleaved mode is not implemented!\n");
1238 * 2 bits shifted from plane 0 and 2 for the first 4 pixels,
1239 * then 2 bits shifted from plane 1 and 3 for the next 4
1241 DWORD BankNumber
= (j
/ 4) % 2;
1242 DWORD Offset
= Address
+ (j
/ 8);
1243 BYTE LowPlaneData
= VgaMemory
[BankNumber
* VGA_BANK_SIZE
+ LOWORD(Offset
* AddressSize
)];
1244 BYTE HighPlaneData
= VgaMemory
[(BankNumber
+ 2) * VGA_BANK_SIZE
+ LOWORD(Offset
* AddressSize
)];
1246 /* Extract the two bits from each plane */
1247 LowPlaneData
= (LowPlaneData
>> (6 - ((j
% 4) * 2))) & 3;
1248 HighPlaneData
= (HighPlaneData
>> (6 - ((j
% 4) * 2))) & 3;
1250 /* Combine them into the pixel */
1251 PixelData
= LowPlaneData
| (HighPlaneData
<< 2);
1256 /* 1 bit shifted from each plane */
1258 /* Check if this is 16 or 256 color mode */
1259 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
)
1261 /* 8 bits per pixel, 2 on each plane */
1263 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
1265 /* The data is on plane k, 4 pixels per byte */
1266 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
1267 + LOWORD((Address
+ (j
/ VGA_NUM_BANKS
))
1270 /* The mask of the first bit in the pair */
1271 BYTE BitMask
= 1 << (((3 - (j
% VGA_NUM_BANKS
)) * 2) + 1);
1273 /* Bits 0, 1, 2 and 3 come from the first bit of the pair */
1274 if (PlaneData
& BitMask
) PixelData
|= 1 << k
;
1276 /* Bits 4, 5, 6 and 7 come from the second bit of the pair */
1277 if (PlaneData
& (BitMask
>> 1)) PixelData
|= 1 << (k
+ 4);
1282 /* 4 bits per pixel, 1 on each plane */
1284 for (k
= 0; k
< VGA_NUM_BANKS
; k
++)
1286 BYTE PlaneData
= VgaMemory
[k
* VGA_BANK_SIZE
1287 + LOWORD((Address
+ (j
/ (VGA_NUM_BANKS
* 2)))
1290 /* If the bit on that plane is set, set it */
1291 if (PlaneData
& (1 << (7 - (j
% 8)))) PixelData
|= 1 << k
;
1296 if (!(VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
))
1299 * In 16 color mode, the value is an index to the AC registers
1300 * if external palette access is disabled, otherwise (in case
1301 * of palette loading) it is a blank pixel.
1303 PixelData
= (VgaAcPalDisable
? VgaAcRegisters
[PixelData
& 0x0F]
1307 /* Take into account DoubleVision mode when checking for pixel updates */
1308 if (DoubleWidth
&& DoubleHeight
)
1310 /* Now check if the resulting pixel data has changed */
1311 if (GraphicsBuffer
[(i
* 2 * Resolution
.X
* 2) + (j
* 2)] != PixelData
)
1313 /* Yes, write the new value */
1314 GraphicsBuffer
[(i
* 2 * Resolution
.X
* 2) + (j
* 2)] = PixelData
;
1315 GraphicsBuffer
[(i
* 2 * Resolution
.X
* 2) + (j
* 2 + 1)] = PixelData
;
1316 GraphicsBuffer
[((i
* 2 + 1) * Resolution
.X
* 2) + (j
* 2)] = PixelData
;
1317 GraphicsBuffer
[((i
* 2 + 1) * Resolution
.X
* 2) + (j
* 2 + 1)] = PixelData
;
1319 /* Mark the specified pixel as changed */
1320 VgaMarkForUpdate(i
, j
);
1323 else if (DoubleWidth
&& !DoubleHeight
)
1325 /* Now check if the resulting pixel data has changed */
1326 if (GraphicsBuffer
[(i
* Resolution
.X
* 2) + (j
* 2)] != PixelData
)
1328 /* Yes, write the new value */
1329 GraphicsBuffer
[(i
* Resolution
.X
* 2) + (j
* 2)] = PixelData
;
1330 GraphicsBuffer
[(i
* Resolution
.X
* 2) + (j
* 2 + 1)] = PixelData
;
1332 /* Mark the specified pixel as changed */
1333 VgaMarkForUpdate(i
, j
);
1336 else if (!DoubleWidth
&& DoubleHeight
)
1338 /* Now check if the resulting pixel data has changed */
1339 if (GraphicsBuffer
[(i
* 2 * Resolution
.X
) + j
] != PixelData
)
1341 /* Yes, write the new value */
1342 GraphicsBuffer
[(i
* 2 * Resolution
.X
) + j
] = PixelData
;
1343 GraphicsBuffer
[((i
* 2 + 1) * Resolution
.X
) + j
] = PixelData
;
1345 /* Mark the specified pixel as changed */
1346 VgaMarkForUpdate(i
, j
);
1349 else // if (!DoubleWidth && !DoubleHeight)
1351 /* Now check if the resulting pixel data has changed */
1352 if (GraphicsBuffer
[i
* Resolution
.X
+ j
] != PixelData
)
1354 /* Yes, write the new value */
1355 GraphicsBuffer
[i
* Resolution
.X
+ j
] = PixelData
;
1357 /* Mark the specified pixel as changed */
1358 VgaMarkForUpdate(i
, j
);
1363 if ((VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_OE
) && (i
& 1))
1365 /* Clear the high bit */
1366 Address
&= ~InterlaceHighBit
;
1369 if (!(VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_OE
) || (i
& 1))
1371 /* Move to the next scanline */
1372 Address
+= ScanlineSize
;
1377 * Release the console framebuffer mutex
1378 * so that we allow for repainting.
1380 ReleaseMutex(ConsoleMutex
);
1386 PCHAR_CELL CharBuffer
= (PCHAR_CELL
)ConsoleFramebuffer
;
1389 /* Loop through the scanlines */
1390 for (i
= 0; i
< Resolution
.Y
; i
++)
1392 /* Loop through the characters */
1393 for (j
= 0; j
< Resolution
.X
; j
++)
1395 CurrentAddr
= LOWORD((Address
+ j
) * AddressSize
);
1397 /* Plane 0 holds the character itself */
1398 CharInfo
.Char
= VgaMemory
[CurrentAddr
];
1400 /* Plane 1 holds the attribute */
1401 CharInfo
.Attributes
= VgaMemory
[CurrentAddr
+ VGA_BANK_SIZE
];
1403 /* Now check if the resulting character data has changed */
1404 if ((CharBuffer
[i
* Resolution
.X
+ j
].Char
!= CharInfo
.Char
) ||
1405 (CharBuffer
[i
* Resolution
.X
+ j
].Attributes
!= CharInfo
.Attributes
))
1407 /* Yes, write the new value */
1408 CharBuffer
[i
* Resolution
.X
+ j
] = CharInfo
;
1410 /* Mark the specified cell as changed */
1411 VgaMarkForUpdate(i
, j
);
1415 /* Move to the next scanline */
1416 Address
+= ScanlineSize
;
1421 static VOID
VgaUpdateTextCursor(VOID
)
1424 CONSOLE_CURSOR_INFO CursorInfo
;
1425 BYTE CursorStart
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_START_REG
] & 0x3F;
1426 BYTE CursorEnd
= VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] & 0x1F;
1427 DWORD ScanlineSize
= (DWORD
)VgaCrtcRegisters
[VGA_CRTC_OFFSET_REG
] * 2;
1428 BYTE TextSize
= 1 + (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & 0x1F);
1429 WORD Location
= MAKEWORD(VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_LOW_REG
],
1430 VgaCrtcRegisters
[VGA_CRTC_CURSOR_LOC_HIGH_REG
]);
1432 /* Just return if we are not in text mode */
1433 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
) return;
1435 if (CursorStart
< CursorEnd
)
1437 /* Visible cursor */
1438 CursorInfo
.bVisible
= TRUE
;
1439 CursorInfo
.dwSize
= (100 * (CursorEnd
- CursorStart
)) / TextSize
;
1444 CursorInfo
.bVisible
= FALSE
;
1445 CursorInfo
.dwSize
= 0;
1448 /* Add the cursor skew to the location */
1449 Location
+= (VgaCrtcRegisters
[VGA_CRTC_CURSOR_END_REG
] >> 5) & 3;
1451 /* Find the coordinates of the new position */
1452 Position
.X
= (SHORT
)(Location
% ScanlineSize
);
1453 Position
.Y
= (SHORT
)(Location
/ ScanlineSize
);
1455 DPRINT("VgaUpdateTextCursor: X = %d ; Y = %d\n", Position
.X
, Position
.Y
);
1457 /* Update the physical cursor */
1458 SetConsoleCursorInfo(TextConsoleBuffer
, &CursorInfo
);
1459 SetConsoleCursorPosition(TextConsoleBuffer
, Position
);
1461 /* Reset the cursor move flag */
1462 CursorMoved
= FALSE
;
1465 static BYTE WINAPI
VgaReadPort(USHORT Port
)
1467 DPRINT("VgaReadPort: Port 0x%X\n", Port
);
1472 return VgaMiscRegister
;
1474 case VGA_INSTAT0_READ
:
1475 return 0; // Not implemented
1477 case VGA_INSTAT1_READ_MONO
:
1478 case VGA_INSTAT1_READ_COLOR
:
1482 /* Reset the AC latch */
1485 /* Set a flag if there is a vertical or horizontal retrace */
1486 if (InVerticalRetrace
|| InHorizontalRetrace
) Result
|= VGA_STAT_DD
;
1488 /* Set an additional flag if there was a vertical retrace */
1489 if (InVerticalRetrace
) Result
|= VGA_STAT_VRETRACE
;
1491 /* Clear the flags */
1492 InHorizontalRetrace
= InVerticalRetrace
= FALSE
;
1497 case VGA_FEATURE_READ
:
1498 return VgaFeatureRegister
;
1504 return VgaAcRegisters
[VgaAcIndex
];
1510 return VgaSeqRegisters
[VgaSeqIndex
];
1515 case VGA_DAC_READ_INDEX
:
1516 /* This returns the read/write state */
1517 return (VgaDacReadWrite
? 0 : 3);
1519 case VGA_DAC_WRITE_INDEX
:
1520 return (VgaDacIndex
/ 3);
1524 /* Ignore reads in write mode */
1525 if (!VgaDacReadWrite
)
1527 BYTE Data
= VgaDacRegisters
[VgaDacIndex
++];
1528 VgaDacIndex
%= VGA_PALETTE_SIZE
;
1535 case VGA_CRTC_INDEX_MONO
:
1536 case VGA_CRTC_INDEX_COLOR
:
1537 return VgaCrtcIndex
;
1539 case VGA_CRTC_DATA_MONO
:
1540 case VGA_CRTC_DATA_COLOR
:
1541 return VgaCrtcRegisters
[VgaCrtcIndex
];
1547 return VgaGcRegisters
[VgaGcIndex
];
1550 DPRINT1("VgaReadPort: Unknown port 0x%X\n", Port
);
1557 static VOID WINAPI
VgaWritePort(USHORT Port
, BYTE Data
)
1559 DPRINT("VgaWritePort: Port 0x%X, Data 0x%02X\n", Port
, Data
);
1563 case VGA_MISC_WRITE
:
1565 VgaMiscRegister
= Data
;
1567 if (VgaMiscRegister
& 0x01)
1569 /* Color emulation */
1570 DPRINT1("Color emulation\n");
1572 /* Register the new I/O Ports */
1573 RegisterIoPort(0x3D4, VgaReadPort
, VgaWritePort
); // VGA_CRTC_INDEX_COLOR
1574 RegisterIoPort(0x3D5, VgaReadPort
, VgaWritePort
); // VGA_CRTC_DATA_COLOR
1575 RegisterIoPort(0x3DA, VgaReadPort
, VgaWritePort
); // VGA_INSTAT1_READ_COLOR, VGA_FEATURE_WRITE_COLOR
1577 /* Unregister the old ones */
1578 UnregisterIoPort(0x3B4); // VGA_CRTC_INDEX_MONO
1579 UnregisterIoPort(0x3B5); // VGA_CRTC_DATA_MONO
1580 UnregisterIoPort(0x3BA); // VGA_INSTAT1_READ_MONO, VGA_FEATURE_WRITE_MONO
1584 /* Monochrome emulation */
1585 DPRINT1("Monochrome emulation\n");
1587 /* Register the new I/O Ports */
1588 RegisterIoPort(0x3B4, VgaReadPort
, VgaWritePort
); // VGA_CRTC_INDEX_MONO
1589 RegisterIoPort(0x3B5, VgaReadPort
, VgaWritePort
); // VGA_CRTC_DATA_MONO
1590 RegisterIoPort(0x3BA, VgaReadPort
, VgaWritePort
); // VGA_INSTAT1_READ_MONO, VGA_FEATURE_WRITE_MONO
1592 /* Unregister the old ones */
1593 UnregisterIoPort(0x3D4); // VGA_CRTC_INDEX_COLOR
1594 UnregisterIoPort(0x3D5); // VGA_CRTC_DATA_COLOR
1595 UnregisterIoPort(0x3DA); // VGA_INSTAT1_READ_COLOR, VGA_FEATURE_WRITE_COLOR
1598 // if (VgaMiscRegister & 0x02) { /* Enable RAM access */ } else { /* Disable RAM access */ }
1602 case VGA_FEATURE_WRITE_MONO
:
1603 case VGA_FEATURE_WRITE_COLOR
:
1605 VgaFeatureRegister
= Data
;
1610 // case VGA_AC_WRITE:
1614 /* Change the index */
1615 BYTE Index
= Data
& 0x1F;
1616 if (Index
< VGA_AC_MAX_REG
) VgaAcIndex
= Index
;
1619 * Change palette protection by checking for
1620 * the Palette Address Source bit.
1622 VgaAcPalDisable
= (Data
& 0x20) ? TRUE
: FALSE
;
1626 /* Write the data */
1630 /* Toggle the latch */
1631 VgaAcLatch
= !VgaAcLatch
;
1637 /* Set the sequencer index register */
1638 if (Data
< VGA_SEQ_MAX_REG
) VgaSeqIndex
= Data
;
1644 /* Call the sequencer function */
1645 VgaWriteSequencer(Data
);
1655 case VGA_DAC_READ_INDEX
:
1657 VgaDacReadWrite
= FALSE
;
1658 VgaDacIndex
= Data
* 3;
1662 case VGA_DAC_WRITE_INDEX
:
1664 VgaDacReadWrite
= TRUE
;
1665 VgaDacIndex
= Data
* 3;
1671 /* Ignore writes in read mode */
1672 if (VgaDacReadWrite
) VgaWriteDac(Data
& 0x3F);
1676 case VGA_CRTC_INDEX_MONO
:
1677 case VGA_CRTC_INDEX_COLOR
:
1679 /* Set the CRTC index register */
1680 if (Data
< VGA_CRTC_MAX_REG
) VgaCrtcIndex
= Data
;
1684 case VGA_CRTC_DATA_MONO
:
1685 case VGA_CRTC_DATA_COLOR
:
1687 /* Call the CRTC function */
1694 /* Set the GC index register */
1695 if (Data
< VGA_GC_MAX_REG
) VgaGcIndex
= Data
;
1701 /* Call the GC function */
1707 DPRINT1("VgaWritePort: Unknown port 0x%X\n", Port
);
1712 /* PUBLIC FUNCTIONS ***********************************************************/
1714 DWORD
VgaGetVideoBaseAddress(VOID
)
1716 return MemoryBase
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
1719 DWORD
VgaGetVideoLimitAddress(VOID
)
1721 return MemoryLimit
[(VgaGcRegisters
[VGA_GC_MISC_REG
] >> 2) & 0x03];
1724 COORD
VgaGetDisplayResolution(VOID
)
1727 BYTE MaximumScanLine
= 1 + (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & 0x1F);
1729 /* The low 8 bits are in the display registers */
1730 Resolution
.X
= VgaCrtcRegisters
[VGA_CRTC_END_HORZ_DISP_REG
];
1731 Resolution
.Y
= VgaCrtcRegisters
[VGA_CRTC_VERT_DISP_END_REG
];
1733 /* Set the top bits from the overflow register */
1734 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE8
)
1736 Resolution
.Y
|= 1 << 8;
1738 if (VgaCrtcRegisters
[VGA_CRTC_OVERFLOW_REG
] & VGA_CRTC_OVERFLOW_VDE9
)
1740 Resolution
.Y
|= 1 << 9;
1743 /* Increase the values by 1 */
1747 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
1749 /* Multiply the horizontal resolution by the 9/8 dot mode */
1750 Resolution
.X
*= (VgaSeqRegisters
[VGA_SEQ_CLOCK_REG
] & VGA_SEQ_CLOCK_98DM
)
1753 /* The horizontal resolution is halved in 8-bit mode */
1754 if (VgaAcRegisters
[VGA_AC_CONTROL_REG
] & VGA_AC_CONTROL_8BIT
) Resolution
.X
/= 2;
1757 if (VgaCrtcRegisters
[VGA_CRTC_MAX_SCAN_LINE_REG
] & VGA_CRTC_MAXSCANLINE_DOUBLE
)
1759 /* Halve the vertical resolution */
1764 /* Divide the vertical resolution by the maximum scan line (== font size in text mode) */
1765 Resolution
.Y
/= MaximumScanLine
;
1768 /* Return the resolution */
1772 VOID
VgaRefreshDisplay(VOID
)
1774 HANDLE ConsoleBufferHandle
= NULL
;
1777 /* Set the vertical retrace flag */
1778 InVerticalRetrace
= TRUE
;
1780 /* If nothing has changed, just return */
1781 // if (!ModeChanged && !CursorMoved && !PaletteChanged && !NeedsUpdate)
1784 /* Change the display mode */
1785 if (ModeChanged
) VgaChangeMode();
1787 /* Change the text cursor location */
1788 if (CursorMoved
) VgaUpdateTextCursor();
1790 /* Retrieve the current resolution */
1791 Resolution
= VgaGetDisplayResolution();
1795 /* Trigger a full update of the screen */
1797 UpdateRectangle
.Left
= 0;
1798 UpdateRectangle
.Top
= 0;
1799 UpdateRectangle
.Right
= Resolution
.X
;
1800 UpdateRectangle
.Bottom
= Resolution
.Y
;
1802 PaletteChanged
= FALSE
;
1805 /* Update the contents of the framebuffer */
1806 VgaUpdateFramebuffer();
1808 /* Ignore if there's nothing to update */
1809 if (!NeedsUpdate
) return;
1811 DPRINT("Updating screen rectangle (%d, %d, %d, %d)\n",
1812 UpdateRectangle
.Left
,
1813 UpdateRectangle
.Top
,
1814 UpdateRectangle
.Right
,
1815 UpdateRectangle
.Bottom
);
1817 /* Check if this is text mode or graphics mode */
1818 if (VgaGcRegisters
[VGA_GC_MISC_REG
] & VGA_GC_MISC_NOALPHA
)
1821 ConsoleBufferHandle
= GraphicsConsoleBuffer
;
1823 /* In DoubleVision mode, scale the update rectangle */
1826 UpdateRectangle
.Left
*= 2;
1827 UpdateRectangle
.Right
= UpdateRectangle
.Right
* 2 + 1;
1831 UpdateRectangle
.Top
*= 2;
1832 UpdateRectangle
.Bottom
= UpdateRectangle
.Bottom
* 2 + 1;
1838 ConsoleBufferHandle
= TextConsoleBuffer
;
1841 /* Redraw the screen */
1842 __InvalidateConsoleDIBits(ConsoleBufferHandle
, &UpdateRectangle
);
1844 /* Clear the update flag */
1845 NeedsUpdate
= FALSE
;
1848 VOID
VgaHorizontalRetrace(VOID
)
1851 InHorizontalRetrace
= TRUE
;
1854 VOID
VgaReadMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
1859 DPRINT("VgaReadMemory: Address 0x%08X, Size %lu\n", Address
, Size
);
1861 /* Ignore if video RAM access is disabled */
1862 if ((VgaMiscRegister
& VGA_MISC_RAM_ENABLED
) == 0) return;
1864 /* Loop through each byte */
1865 for (i
= 0; i
< Size
; i
++)
1867 VideoAddress
= VgaTranslateReadAddress(Address
+ i
);
1869 /* Copy the value to the buffer */
1870 Buffer
[i
] = VgaMemory
[VideoAddress
];
1873 /* Load the latch registers */
1874 VgaLatchRegisters
[0] = VgaMemory
[LOWORD(VideoAddress
)];
1875 VgaLatchRegisters
[1] = VgaMemory
[VGA_BANK_SIZE
+ LOWORD(VideoAddress
)];
1876 VgaLatchRegisters
[2] = VgaMemory
[(2 * VGA_BANK_SIZE
) + LOWORD(VideoAddress
)];
1877 VgaLatchRegisters
[3] = VgaMemory
[(3 * VGA_BANK_SIZE
) + LOWORD(VideoAddress
)];
1880 VOID
VgaWriteMemory(DWORD Address
, LPBYTE Buffer
, DWORD Size
)
1885 DPRINT("VgaWriteMemory: Address 0x%08X, Size %lu\n", Address
, Size
);
1887 /* Ignore if video RAM access is disabled */
1888 if ((VgaMiscRegister
& VGA_MISC_RAM_ENABLED
) == 0) return;
1890 /* Also ignore if write access to all planes is disabled */
1891 if ((VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & 0x0F) == 0x00) return;
1893 /* Loop through each byte */
1894 for (i
= 0; i
< Size
; i
++)
1896 VideoAddress
= VgaTranslateWriteAddress(Address
+ i
);
1898 for (j
= 0; j
< VGA_NUM_BANKS
; j
++)
1900 /* Make sure the page is writeable */
1901 if (!(VgaSeqRegisters
[VGA_SEQ_MASK_REG
] & (1 << j
))) continue;
1903 /* Check if this is chain-4 mode */
1904 if (VgaSeqRegisters
[VGA_SEQ_MEM_REG
] & VGA_SEQ_MEM_C4
)
1906 if (((Address
+ i
) & 3) != j
)
1908 /* This plane will not be accessed */
1913 /* Check if this is odd-even mode */
1914 if (VgaGcRegisters
[VGA_GC_MODE_REG
] & VGA_GC_MODE_OE
)
1916 if (((Address
+ i
) & 1) != (j
& 1))
1918 /* This plane will not be accessed */
1923 /* Copy the value to the VGA memory */
1924 VgaMemory
[VideoAddress
+ j
* VGA_BANK_SIZE
] = VgaTranslateByteForWriting(Buffer
[i
], j
);
1929 VOID
VgaClearMemory(VOID
)
1931 RtlZeroMemory(VgaMemory
, sizeof(VgaMemory
));
1934 VOID
VgaResetPalette(VOID
)
1936 PALETTEENTRY Entries
[VGA_MAX_COLORS
];
1938 /* Restore the default palette */
1939 VgaRestoreDefaultPalette(Entries
, VGA_MAX_COLORS
);
1940 SetPaletteEntries(PaletteHandle
, 0, VGA_MAX_COLORS
, Entries
);
1941 PaletteChanged
= TRUE
;
1944 VOID
VgaWriteFont(UINT FontNumber
, CONST UCHAR
*FontData
, UINT Height
)
1947 PUCHAR FontMemory
= (PUCHAR
)&VgaMemory
[VGA_BANK_SIZE
* VGA_FONT_BANK
+ (FontNumber
* VGA_FONT_SIZE
)];
1949 ASSERT(Height
<= VGA_MAX_FONT_HEIGHT
);
1951 for (i
= 0 ; i
< VGA_FONT_CHARACTERS
; i
++)
1953 /* Write the character */
1954 for (j
= 0; j
< Height
; j
++)
1956 FontMemory
[i
* VGA_MAX_FONT_HEIGHT
+ j
] = FontData
[i
* Height
+ j
];
1959 /* Clear the unused part */
1960 for (j
= Height
; j
< VGA_MAX_FONT_HEIGHT
; j
++)
1962 FontMemory
[i
* VGA_MAX_FONT_HEIGHT
+ j
] = 0;
1967 VOID
ScreenEventHandler(PWINDOW_BUFFER_SIZE_RECORD ScreenEvent
)
1969 DPRINT1("Screen events not handled\n");
1972 BOOL
VgaAttachToConsole(VOID
)
1975 // FIXME: We should go back to the saved screen state
1977 if (TextResolution
.X
== 0 || TextResolution
.Y
== 0)
1978 DPRINT1("VgaAttachToConsole -- TextResolution uninitialized\n");
1980 if (TextResolution
.X
== 0) TextResolution
.X
= 80;
1981 if (TextResolution
.Y
== 0) TextResolution
.Y
= 25;
1983 return VgaAttachToConsoleInternal(&TextResolution
);
1986 VOID
VgaDetachFromConsole(BOOL ChangingMode
)
1990 COORD dummySize
= {0};
1993 // FIXME: We should save the screen state
1996 __RegisterConsoleVDM(0,
2008 TextFramebuffer
= NULL
;
2014 /* Restore the old screen buffer */
2015 VgaSetActiveScreenBuffer(TextConsoleBuffer
);
2017 /* Restore the original console size */
2020 ConRect
.Right
= ConRect
.Left
+ OrgConsoleBufferInfo
.srWindow
.Right
- OrgConsoleBufferInfo
.srWindow
.Left
;
2021 ConRect
.Bottom
= ConRect
.Top
+ OrgConsoleBufferInfo
.srWindow
.Bottom
- OrgConsoleBufferInfo
.srWindow
.Top
;
2023 * See the following trick explanation in VgaAttachToConsoleInternal.
2025 SetConsoleScreenBufferSize(TextConsoleBuffer
, OrgConsoleBufferInfo
.dwSize
);
2026 SetConsoleWindowInfo(TextConsoleBuffer
, TRUE
, &ConRect
);
2027 SetConsoleScreenBufferSize(TextConsoleBuffer
, OrgConsoleBufferInfo
.dwSize
);
2029 /* Restore the original cursor shape */
2030 SetConsoleCursorInfo(TextConsoleBuffer
, &OrgConsoleCursorInfo
);
2034 BOOLEAN
VgaInitialize(HANDLE TextHandle
)
2036 /* Save the default text-mode console output handle */
2037 if (!IsConsoleHandle(TextHandle
)) return FALSE
;
2038 TextConsoleBuffer
= TextHandle
;
2040 /* Save the original cursor and console screen buffer information */
2041 if (!GetConsoleCursorInfo(TextConsoleBuffer
, &OrgConsoleCursorInfo
) ||
2042 !GetConsoleScreenBufferInfo(TextConsoleBuffer
, &OrgConsoleBufferInfo
))
2046 ConsoleInfo
= OrgConsoleBufferInfo
;
2048 /* Initialize the VGA palette and fail if it isn't successfully created */
2049 if (!VgaInitializePalette()) return FALSE
;
2050 /***/ VgaResetPalette(); /***/
2052 /* Switch to the text buffer */
2053 VgaSetActiveScreenBuffer(TextConsoleBuffer
);
2055 /* Clear the VGA memory */
2058 /* Register the I/O Ports */
2059 RegisterIoPort(0x3CC, VgaReadPort
, NULL
); // VGA_MISC_READ
2060 RegisterIoPort(0x3C2, VgaReadPort
, VgaWritePort
); // VGA_MISC_WRITE, VGA_INSTAT0_READ
2061 RegisterIoPort(0x3CA, VgaReadPort
, NULL
); // VGA_FEATURE_READ
2062 RegisterIoPort(0x3C0, VgaReadPort
, VgaWritePort
); // VGA_AC_INDEX, VGA_AC_WRITE
2063 RegisterIoPort(0x3C1, VgaReadPort
, NULL
); // VGA_AC_READ
2064 RegisterIoPort(0x3C4, VgaReadPort
, VgaWritePort
); // VGA_SEQ_INDEX
2065 RegisterIoPort(0x3C5, VgaReadPort
, VgaWritePort
); // VGA_SEQ_DATA
2066 RegisterIoPort(0x3C6, VgaReadPort
, VgaWritePort
); // VGA_DAC_MASK
2067 RegisterIoPort(0x3C7, VgaReadPort
, VgaWritePort
); // VGA_DAC_READ_INDEX
2068 RegisterIoPort(0x3C8, VgaReadPort
, VgaWritePort
); // VGA_DAC_WRITE_INDEX
2069 RegisterIoPort(0x3C9, VgaReadPort
, VgaWritePort
); // VGA_DAC_DATA
2070 RegisterIoPort(0x3CE, VgaReadPort
, VgaWritePort
); // VGA_GC_INDEX
2071 RegisterIoPort(0x3CF, VgaReadPort
, VgaWritePort
); // VGA_GC_DATA
2073 /* Return success */
2077 VOID
VgaCleanup(VOID
)
2079 if (ScreenMode
== GRAPHICS_MODE
)
2081 /* Leave the current graphics mode */
2082 VgaLeaveGraphicsMode();
2086 /* Leave the current text mode */
2090 VgaDetachFromConsole(FALSE
);
2092 CloseHandle(AnotherEvent
);
2093 CloseHandle(EndEvent
);
2094 CloseHandle(StartEvent
);