[NTVDM]
[reactos.git] / subsystems / ntvdm / bios.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: bios.c
5 * PURPOSE: VDM BIOS
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 #include "ntvdm.h"
10
11 BYTE CursorRow, CursorCol;
12 WORD ConsoleWidth, ConsoleHeight;
13
14 BOOLEAN BiosInitialize()
15 {
16 INT i;
17 WORD Offset = 0;
18 HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
19 CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
20 LPWORD IntVecTable = (LPWORD)((ULONG_PTR)BaseAddress);
21 LPBYTE BiosCode = (LPBYTE)((ULONG_PTR)BaseAddress + TO_LINEAR(BIOS_SEGMENT, 0));
22
23 /* Generate ISR stubs and fill the IVT */
24 for (i = 0; i < 256; i++)
25 {
26 IntVecTable[i * 2] = Offset;
27 IntVecTable[i * 2 + 1] = BIOS_SEGMENT;
28
29 if (i != SPECIAL_INT_NUM)
30 {
31 BiosCode[Offset++] = 0xFA; // cli
32
33 BiosCode[Offset++] = 0x6A; // push i
34 BiosCode[Offset++] = (BYTE)i;
35
36 BiosCode[Offset++] = 0xCD; // int SPECIAL_INT_NUM
37 BiosCode[Offset++] = SPECIAL_INT_NUM;
38
39 BiosCode[Offset++] = 0x83; // add sp, 2
40 BiosCode[Offset++] = 0xC4;
41 BiosCode[Offset++] = 0x02;
42 }
43
44 BiosCode[Offset++] = 0xCF; // iret
45 }
46
47 /* Get the console buffer info */
48 if (!GetConsoleScreenBufferInfo(ConsoleOutput, &ConsoleInfo))
49 {
50 return FALSE;
51 }
52
53 /* Set the initial cursor position and console size */
54 CursorCol = ConsoleInfo.dwCursorPosition.X;
55 CursorRow = ConsoleInfo.dwCursorPosition.Y;
56 ConsoleWidth = ConsoleInfo.dwSize.X;
57 ConsoleHeight = ConsoleInfo.dwSize.Y;
58
59 return TRUE;
60 }
61
62 static COORD BiosVideoAddressToCoord(ULONG Address)
63 {
64 COORD Result = {0, 0};
65 CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
66 HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
67
68 if (!GetConsoleScreenBufferInfo(ConsoleOutput, &ConsoleInfo))
69 {
70 assert(0);
71 return Result;
72 }
73
74 Result.X = ((Address - CONSOLE_VIDEO_MEM_START) >> 1) % ConsoleInfo.dwSize.X;
75 Result.Y = ((Address - CONSOLE_VIDEO_MEM_START) >> 1) / ConsoleInfo.dwSize.X;
76
77 return Result;
78 }
79
80 VOID BiosUpdateConsole(ULONG StartAddress, ULONG EndAddress)
81 {
82 ULONG i;
83 COORD Coordinates;
84 DWORD CharsWritten;
85 HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
86
87 /* Loop through all the addresses */
88 for (i = StartAddress; i < EndAddress; i++)
89 {
90 /* Get the coordinates */
91 Coordinates = BiosVideoAddressToCoord(i);
92
93 /* Check if this is a character byte or an attribute byte */
94 if ((i - CONSOLE_VIDEO_MEM_START) % 2 == 0)
95 {
96 /* This is a regular character */
97 FillConsoleOutputCharacterA(ConsoleOutput,
98 *(PCHAR)((ULONG_PTR)BaseAddress + i),
99 sizeof(CHAR),
100 Coordinates,
101 &CharsWritten);
102 }
103 else
104 {
105 /* This is an attribute */
106 FillConsoleOutputAttribute(ConsoleOutput,
107 *(PCHAR)((ULONG_PTR)BaseAddress + i),
108 sizeof(CHAR),
109 Coordinates,
110 &CharsWritten);
111 }
112 }
113 }
114
115 VOID BiosUpdateVideoMemory(ULONG StartAddress, ULONG EndAddress)
116 {
117 ULONG i;
118 COORD Coordinates;
119 WORD Attribute;
120 DWORD CharsWritten;
121 HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
122
123 /* Loop through all the addresses */
124 for (i = StartAddress; i < EndAddress; i++)
125 {
126 /* Get the coordinates */
127 Coordinates = BiosVideoAddressToCoord(i);
128
129 /* Check if this is a character byte or an attribute byte */
130 if ((i - CONSOLE_VIDEO_MEM_START) % 2 == 0)
131 {
132 /* This is a regular character */
133 ReadConsoleOutputCharacterA(ConsoleOutput,
134 (LPSTR)((ULONG_PTR)BaseAddress + i),
135 sizeof(CHAR),
136 Coordinates,
137 &CharsWritten);
138 }
139 else
140 {
141 /* This is an attribute */
142 ReadConsoleOutputAttribute(ConsoleOutput,
143 &Attribute,
144 sizeof(CHAR),
145 Coordinates,
146 &CharsWritten);
147
148 *(PCHAR)((ULONG_PTR)BaseAddress + i) = LOBYTE(Attribute);
149 }
150 }
151 }
152
153 VOID BiosVideoService()
154 {
155 HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
156 INT CursorHeight;
157 BOOLEAN Invisible = FALSE;
158 COORD CursorPosition;
159 CONSOLE_CURSOR_INFO CursorInfo;
160 DWORD Eax = EmulatorGetRegister(EMULATOR_REG_AX);
161 DWORD Ecx = EmulatorGetRegister(EMULATOR_REG_CX);
162 DWORD Edx = EmulatorGetRegister(EMULATOR_REG_DX);
163
164 switch (LOBYTE(Eax))
165 {
166 /* Set Text-Mode Cursor Shape */
167 case 0x01:
168 {
169 /* Retrieve and validate the input */
170 Invisible = ((HIBYTE(Ecx) >> 5) & 0x03) ? TRUE : FALSE;
171 CursorHeight = (HIBYTE(Ecx) & 0x1F) - (LOBYTE(Ecx) & 0x1F);
172 if (CursorHeight < 1) CursorHeight = 1;
173 if (CursorHeight > 100) CursorHeight = 100;
174
175 /* Set the cursor */
176 CursorInfo.dwSize = (CursorHeight * 100) / CONSOLE_FONT_HEIGHT;
177 CursorInfo.bVisible = !Invisible;
178 SetConsoleCursorInfo(ConsoleOutput, &CursorInfo);
179
180 break;
181 }
182
183 /* Set Cursor Position */
184 case 0x02:
185 {
186 CursorPosition.X = LOBYTE(Edx);
187 CursorPosition.Y = HIBYTE(Edx);
188
189 SetConsoleCursorPosition(ConsoleOutput, CursorPosition);
190 break;
191 }
192
193 /* Scroll Up Window */
194 case 0x06:
195 {
196 break;
197 }
198
199 /* Scroll Down Window */
200 case 0x07:
201 {
202 break;
203 }
204
205 /* Read Character And Attribute At Cursor Position */
206 case 0x08:
207 {
208 break;
209 }
210
211 /* Write Character And Attribute At Cursor Position */
212 case 0x09:
213 {
214 break;
215 }
216
217 /* Write Character Only At Cursor Position */
218 case 0x0A:
219 {
220 break;
221 }
222 }
223 }
224
225 /* EOF */