[NTVDM]
[reactos.git] / reactos / subsystems / ntvdm / bios / bios32 / bios32.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: bios32.c
5 * PURPOSE: VDM 32-bit BIOS
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "emulator.h"
14 #include "callback.h"
15 #include "bop.h"
16
17 #include "../bios.h"
18 #include "../rom.h"
19 #include "bios32.h"
20 #include "bios32p.h"
21 #include "kbdbios32.h"
22 #include "vidbios32.h"
23
24 #include "io.h"
25 #include "hardware/cmos.h"
26 #include "hardware/pic.h"
27 #include "hardware/timer.h"
28
29 /* PRIVATE VARIABLES **********************************************************/
30
31 CALLBACK16 BiosContext;
32
33 /* PRIVATE FUNCTIONS **********************************************************/
34
35 static VOID WINAPI BiosException(LPWORD Stack)
36 {
37 /* Get the exception number and call the emulator API */
38 BYTE ExceptionNumber = LOBYTE(Stack[STACK_INT_NUM]);
39 EmulatorException(ExceptionNumber, Stack);
40 }
41
42 static VOID WINAPI BiosMiscService(LPWORD Stack)
43 {
44 switch (getAH())
45 {
46 /* Wait */
47 case 0x86:
48 {
49 /*
50 * Interval in microseconds in CX:DX
51 * See Ralf Brown: http://www.ctyme.com/intr/rb-1525.htm
52 * for more information.
53 */
54 Sleep(MAKELONG(getDX(), getCX()));
55
56 /* Clear CF */
57 Stack[STACK_FLAGS] &= ~EMULATOR_FLAG_CF;
58
59 break;
60 }
61
62 /* Copy Extended Memory */
63 case 0x87:
64 {
65 DWORD Count = (DWORD)getCX() * 2;
66 PFAST486_GDT_ENTRY Gdt = (PFAST486_GDT_ENTRY)SEG_OFF_TO_PTR(getES(), getSI());
67 DWORD SourceBase = Gdt[2].Base + (Gdt[2].BaseMid << 16) + (Gdt[2].BaseHigh << 24);
68 DWORD SourceLimit = Gdt[2].Limit + (Gdt[2].LimitHigh << 16);
69 DWORD DestBase = Gdt[3].Base + (Gdt[3].BaseMid << 16) + (Gdt[3].BaseHigh << 24);
70 DWORD DestLimit = Gdt[3].Limit + (Gdt[3].LimitHigh << 16);
71
72 /* Check for flags */
73 if (Gdt[2].Granularity) SourceLimit = (SourceLimit << 12) | 0xFFF;
74 if (Gdt[3].Granularity) DestLimit = (DestLimit << 12) | 0xFFF;
75
76 if ((Count > SourceLimit) || (Count > DestLimit))
77 {
78 setAX(0x80);
79 Stack[STACK_FLAGS] |= EMULATOR_FLAG_CF;
80
81 break;
82 }
83
84 /* Copy */
85 RtlMoveMemory((PVOID)((ULONG_PTR)BaseAddress + DestBase),
86 (PVOID)((ULONG_PTR)BaseAddress + SourceBase),
87 Count);
88
89 setAX(ERROR_SUCCESS);
90 Stack[STACK_FLAGS] &= ~EMULATOR_FLAG_CF;
91 break;
92 }
93
94 /* Get Extended Memory Size */
95 case 0x88:
96 {
97 UCHAR Low, High;
98
99 /*
100 * Return the (usable) extended memory (after 1 MB)
101 * size in kB from CMOS.
102 */
103 IOWriteB(CMOS_ADDRESS_PORT, CMOS_REG_ACTUAL_EXT_MEMORY_LOW);
104 Low = IOReadB(CMOS_DATA_PORT);
105 IOWriteB(CMOS_ADDRESS_PORT, CMOS_REG_ACTUAL_EXT_MEMORY_HIGH);
106 High = IOReadB(CMOS_DATA_PORT);
107 setAX(MAKEWORD(Low, High));
108
109 /* Clear CF */
110 Stack[STACK_FLAGS] &= ~EMULATOR_FLAG_CF;
111
112 break;
113 }
114
115 /* Get Configuration */
116 case 0xC0:
117 {
118 /* Return the BIOS ROM Configuration Table address in ES:BX */
119 setES(HIWORD(Bct));
120 setBX(LOWORD(Bct));
121
122 /* Call successful; clear CF */
123 setAH(0x00);
124 Stack[STACK_FLAGS] &= ~EMULATOR_FLAG_CF;
125
126 break;
127 }
128
129 default:
130 {
131 DPRINT1("BIOS Function INT 15h, AH = 0x%02X NOT IMPLEMENTED\n",
132 getAH());
133 }
134 }
135 }
136
137 static VOID WINAPI BiosTimeService(LPWORD Stack)
138 {
139 switch (getAH())
140 {
141 case 0x00:
142 {
143 /* Set AL to 1 if midnight had passed, 0 otherwise */
144 setAL(Bda->MidnightPassed ? 0x01 : 0x00);
145
146 /* Return the tick count in CX:DX */
147 setCX(HIWORD(Bda->TickCounter));
148 setDX(LOWORD(Bda->TickCounter));
149
150 /* Reset the midnight flag */
151 Bda->MidnightPassed = FALSE;
152
153 break;
154 }
155
156 case 0x01:
157 {
158 /* Set the tick count to CX:DX */
159 Bda->TickCounter = MAKELONG(getDX(), getCX());
160
161 /* Reset the midnight flag */
162 Bda->MidnightPassed = FALSE;
163
164 break;
165 }
166
167 default:
168 {
169 DPRINT1("BIOS Function INT 1Ah, AH = 0x%02X NOT IMPLEMENTED\n",
170 getAH());
171 }
172 }
173 }
174
175 static VOID WINAPI BiosSystemTimerInterrupt(LPWORD Stack)
176 {
177 /* Increase the system tick count */
178 Bda->TickCounter++;
179 }
180
181
182 // From SeaBIOS
183 static VOID PicSetIRQMask(USHORT off, USHORT on)
184 {
185 UCHAR pic1off = off, pic1on = on, pic2off = off>>8, pic2on = on>>8;
186 IOWriteB(PIC_MASTER_DATA, (IOReadB(PIC_MASTER_DATA) & ~pic1off) | pic1on);
187 IOWriteB(PIC_SLAVE_DATA , (IOReadB(PIC_SLAVE_DATA ) & ~pic2off) | pic2on);
188 }
189
190 // From SeaBIOS
191 VOID EnableHwIRQ(UCHAR hwirq, EMULATOR_INT32_PROC func)
192 {
193 UCHAR vector;
194
195 PicSetIRQMask(1 << hwirq, 0);
196 if (hwirq < 8)
197 vector = BIOS_PIC_MASTER_INT + hwirq;
198 else
199 vector = BIOS_PIC_SLAVE_INT + hwirq - 8;
200
201 RegisterBiosInt32(vector, func);
202 }
203
204
205 VOID PicIRQComplete(LPWORD Stack)
206 {
207 /* Get the interrupt number */
208 BYTE IntNum = LOBYTE(Stack[STACK_INT_NUM]);
209
210 /*
211 * If this was a PIC IRQ, send an End-of-Interrupt to the PIC.
212 */
213
214 if (IntNum >= BIOS_PIC_MASTER_INT && IntNum < BIOS_PIC_MASTER_INT + 8)
215 {
216 /* It was an IRQ from the master PIC */
217 IOWriteB(PIC_MASTER_CMD, PIC_OCW2_EOI);
218 }
219 else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
220 {
221 /* It was an IRQ from the slave PIC */
222 IOWriteB(PIC_SLAVE_CMD , PIC_OCW2_EOI);
223 IOWriteB(PIC_MASTER_CMD, PIC_OCW2_EOI);
224 }
225 }
226
227 static VOID WINAPI BiosHandleMasterPicIRQ(LPWORD Stack)
228 {
229 BYTE IrqNumber;
230
231 IOWriteB(PIC_MASTER_CMD, PIC_OCW3_READ_ISR /* == 0x0B */);
232 IrqNumber = IOReadB(PIC_MASTER_CMD);
233
234 DPRINT("Master - IrqNumber = 0x%x\n", IrqNumber);
235
236 PicIRQComplete(Stack);
237 }
238
239 static VOID WINAPI BiosHandleSlavePicIRQ(LPWORD Stack)
240 {
241 BYTE IrqNumber;
242
243 IOWriteB(PIC_SLAVE_CMD, PIC_OCW3_READ_ISR /* == 0x0B */);
244 IrqNumber = IOReadB(PIC_SLAVE_CMD);
245
246 DPRINT("Slave - IrqNumber = 0x%x\n", IrqNumber);
247
248 PicIRQComplete(Stack);
249 }
250
251 // Timer IRQ 0
252 static VOID WINAPI BiosTimerIrq(LPWORD Stack)
253 {
254 /*
255 * Perform the system timer interrupt.
256 *
257 * Do not call directly BiosSystemTimerInterrupt(Stack);
258 * because some programs may hook only BIOS_SYS_TIMER_INTERRUPT
259 * for their purpose...
260 */
261 /** EmulatorInterrupt(BIOS_SYS_TIMER_INTERRUPT); **/
262 Int32Call(&BiosContext, BIOS_SYS_TIMER_INTERRUPT);
263 PicIRQComplete(Stack);
264 }
265
266
267 static VOID BiosHwSetup(VOID)
268 {
269 /* Initialize the master and the slave PICs (cascade mode) */
270 IOWriteB(PIC_MASTER_CMD, PIC_ICW1 | PIC_ICW1_ICW4);
271 IOWriteB(PIC_SLAVE_CMD , PIC_ICW1 | PIC_ICW1_ICW4);
272
273 /*
274 * Set the interrupt vector offsets for each PIC
275 * (base IRQs: 0x08-0x0F for IRQ 0-7, 0x70-0x77 for IRQ 8-15)
276 */
277 IOWriteB(PIC_MASTER_DATA, BIOS_PIC_MASTER_INT);
278 IOWriteB(PIC_SLAVE_DATA , BIOS_PIC_SLAVE_INT );
279
280 /* Tell the master PIC that there is a slave PIC at IRQ 2 */
281 IOWriteB(PIC_MASTER_DATA, 1 << 2);
282 /* Tell the slave PIC its cascade identity */
283 IOWriteB(PIC_SLAVE_DATA , 2);
284
285 /* Make sure both PICs are in 8086 mode */
286 IOWriteB(PIC_MASTER_DATA, PIC_ICW4_8086);
287 IOWriteB(PIC_SLAVE_DATA , PIC_ICW4_8086);
288
289 /* Clear the masks for both PICs */
290 // IOWriteB(PIC_MASTER_DATA, 0x00);
291 // IOWriteB(PIC_SLAVE_DATA , 0x00);
292 /* Disable all IRQs */
293 IOWriteB(PIC_MASTER_DATA, 0xFF);
294 IOWriteB(PIC_SLAVE_DATA , 0xFF);
295
296
297 /* Initialize PIT Counter 0 */
298 IOWriteB(PIT_COMMAND_PORT, 0x34);
299 IOWriteB(PIT_DATA_PORT(0), 0x00);
300 IOWriteB(PIT_DATA_PORT(0), 0x00);
301
302 /* Initialize PIT Counter 1 */
303 IOWriteB(PIT_COMMAND_PORT, 0x74);
304 IOWriteB(PIT_DATA_PORT(1), 0x00);
305 IOWriteB(PIT_DATA_PORT(1), 0x00);
306
307 /* Initialize PIT Counter 2 */
308 IOWriteB(PIT_COMMAND_PORT, 0xB4);
309 IOWriteB(PIT_DATA_PORT(2), 0x00);
310 IOWriteB(PIT_DATA_PORT(2), 0x00);
311
312 EnableHwIRQ(0, BiosTimerIrq);
313 }
314
315 static VOID InitializeBiosInt32(VOID)
316 {
317 USHORT i;
318
319 /* Initialize the callback context */
320 InitializeContext(&BiosContext, BIOS_SEGMENT, 0x0000);
321
322 /* Register the default BIOS 32-bit Interrupts */
323 for (i = 0x00; i <= 0xFF; i++)
324 {
325 RegisterBiosInt32(i, NULL);
326 }
327
328 /* Initialize the exception vector interrupts to a default Exception handler */
329 for (i = 0; i < 8; i++)
330 RegisterBiosInt32(i, BiosException);
331
332 /* Initialize HW vector interrupts to a default HW handler */
333 for (i = BIOS_PIC_MASTER_INT; i < BIOS_PIC_MASTER_INT + 8; i++)
334 RegisterBiosInt32(i, BiosHandleMasterPicIRQ);
335 for (i = BIOS_PIC_SLAVE_INT ; i < BIOS_PIC_SLAVE_INT + 8; i++)
336 RegisterBiosInt32(i, BiosHandleSlavePicIRQ);
337
338 /* Initialize software vector handlers */
339 RegisterBiosInt32(BIOS_EQUIPMENT_INTERRUPT, BiosEquipmentService );
340 RegisterBiosInt32(BIOS_MEMORY_SIZE , BiosGetMemorySize );
341 RegisterBiosInt32(BIOS_MISC_INTERRUPT , BiosMiscService );
342 RegisterBiosInt32(BIOS_TIME_INTERRUPT , BiosTimeService );
343 RegisterBiosInt32(BIOS_SYS_TIMER_INTERRUPT, BiosSystemTimerInterrupt);
344
345 /* Some interrupts are in fact addresses to tables */
346 ((PULONG)BaseAddress)[0x1E] = (ULONG)NULL;
347 ((PULONG)BaseAddress)[0x41] = (ULONG)NULL;
348 ((PULONG)BaseAddress)[0x46] = (ULONG)NULL;
349 ((PULONG)BaseAddress)[0x48] = (ULONG)NULL;
350 ((PULONG)BaseAddress)[0x49] = (ULONG)NULL;
351 }
352
353 static VOID InitializeBiosInfo(VOID)
354 {
355 Bct->Length = sizeof(*Bct);
356 Bct->Model = 0xFC; // PC-AT; see http://www.ctyme.com/intr/rb-1594.htm#Table515
357 Bct->SubModel = 0x00;
358 Bct->BiosRevision = 0x01;
359 Bct->BiosFeature[0] = 0x64; // At the moment we don't support "INT 15/AH=4Fh called upon INT 09h" nor "wait for external event (INT 15/AH=41h) supported"; see http://www.ctyme.com/intr/rb-1594.htm#Table510
360 Bct->BiosFeature[1] = 0x00; // We don't support anything from here; see http://www.ctyme.com/intr/rb-1594.htm#Table511
361 Bct->BiosFeature[2] = 0x00;
362 Bct->BiosFeature[3] = 0x00;
363 Bct->BiosFeature[4] = 0x00;
364 }
365
366 static VOID InitializeBiosData(VOID)
367 {
368 UCHAR Low, High;
369
370 /* Initialize the BDA contents */
371 Bda->EquipmentList = BIOS_EQUIPMENT_LIST;
372
373 /*
374 * Retrieve the conventional memory size
375 * in kB from CMOS, typically 640 kB.
376 */
377 IOWriteB(CMOS_ADDRESS_PORT, CMOS_REG_BASE_MEMORY_LOW);
378 Low = IOReadB(CMOS_DATA_PORT);
379 IOWriteB(CMOS_ADDRESS_PORT, CMOS_REG_BASE_MEMORY_HIGH);
380 High = IOReadB(CMOS_DATA_PORT);
381 Bda->MemorySize = MAKEWORD(Low, High);
382 }
383
384 /* PUBLIC FUNCTIONS ***********************************************************/
385
386 /*
387 * The BIOS POST (Power On-Self Test)
388 */
389 BOOLEAN Bios32Initialize(VOID)
390 {
391 BOOLEAN Success;
392
393 /* Initialize the stack */
394 // That's what says IBM... (stack at 30:00FF going downwards)
395 // setSS(0x0000);
396 // setSP(0x0400);
397 setSS(0x0050); // Stack at 50:0400, going downwards
398 setSP(0x0400);
399
400 /* Set data segment */
401 setDS(BDA_SEGMENT);
402
403 /* Initialize the BDA and the BIOS ROM Information */
404 InitializeBiosData();
405 InitializeBiosInfo();
406
407 /* Register the BIOS 32-bit Interrupts */
408 InitializeBiosInt32();
409
410 /* Initialize platform hardware (PIC/PIT chips, ...) */
411 BiosHwSetup();
412
413 /* Initialize the Keyboard and Video BIOS */
414 if (!KbdBios32Initialize() || !VidBios32Initialize()) return FALSE;
415
416 ///////////// MUST BE DONE AFTER IVT INITIALIZATION !! /////////////////////
417
418 /* Load some ROMs */
419 Success = LoadRom("boot.bin", (PVOID)0xE0000, NULL);
420 DPRINT1("Test ROM loading %s ; GetLastError() = %u\n", Success ? "succeeded" : "failed", GetLastError());
421
422 SearchAndInitRoms(&BiosContext);
423
424 /* We are done */
425 return TRUE;
426 }
427
428 VOID Bios32Cleanup(VOID)
429 {
430 VidBios32Cleanup();
431 KbdBios32Cleanup();
432 }
433
434 /* EOF */