2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: VDM 32-bit BIOS
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
21 #include "kbdbios32.h"
22 #include "vidbios32.h"
25 #include "hardware/cmos.h"
26 #include "hardware/pic.h"
27 #include "hardware/timer.h"
29 /* PRIVATE VARIABLES **********************************************************/
31 CALLBACK16 BiosContext
;
33 /* PRIVATE FUNCTIONS **********************************************************/
35 static VOID WINAPI
BiosException(LPWORD Stack
)
37 /* Get the exception number and call the emulator API */
38 BYTE ExceptionNumber
= LOBYTE(Stack
[STACK_INT_NUM
]);
39 EmulatorException(ExceptionNumber
, Stack
);
42 static VOID WINAPI
BiosMiscService(LPWORD Stack
)
50 * Interval in microseconds in CX:DX
51 * See Ralf Brown: http://www.ctyme.com/intr/rb-1525.htm
52 * for more information.
54 Sleep(MAKELONG(getDX(), getCX()));
57 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
62 /* Copy Extended Memory */
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);
73 if (Gdt
[2].Granularity
) SourceLimit
= (SourceLimit
<< 12) | 0xFFF;
74 if (Gdt
[3].Granularity
) DestLimit
= (DestLimit
<< 12) | 0xFFF;
76 if ((Count
> SourceLimit
) || (Count
> DestLimit
))
79 Stack
[STACK_FLAGS
] |= EMULATOR_FLAG_CF
;
85 RtlMoveMemory((PVOID
)((ULONG_PTR
)BaseAddress
+ DestBase
),
86 (PVOID
)((ULONG_PTR
)BaseAddress
+ SourceBase
),
90 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
94 /* Get Extended Memory Size */
100 * Return the (usable) extended memory (after 1 MB)
101 * size in kB from CMOS.
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
));
110 Stack
[STACK_FLAGS
] &= ~EMULATOR_FLAG_CF
;
117 DPRINT1("BIOS Function INT 15h, AH = 0x%02X NOT IMPLEMENTED\n",
123 static VOID WINAPI
BiosTimeService(LPWORD Stack
)
129 /* Set AL to 1 if midnight had passed, 0 otherwise */
130 setAL(Bda
->MidnightPassed
? 0x01 : 0x00);
132 /* Return the tick count in CX:DX */
133 setCX(HIWORD(Bda
->TickCounter
));
134 setDX(LOWORD(Bda
->TickCounter
));
136 /* Reset the midnight flag */
137 Bda
->MidnightPassed
= FALSE
;
144 /* Set the tick count to CX:DX */
145 Bda
->TickCounter
= MAKELONG(getDX(), getCX());
147 /* Reset the midnight flag */
148 Bda
->MidnightPassed
= FALSE
;
155 DPRINT1("BIOS Function INT 1Ah, AH = 0x%02X NOT IMPLEMENTED\n",
161 static VOID WINAPI
BiosSystemTimerInterrupt(LPWORD Stack
)
163 /* Increase the system tick count */
169 static VOID
PicSetIRQMask(USHORT off
, USHORT on
)
171 UCHAR pic1off
= off
, pic1on
= on
, pic2off
= off
>>8, pic2on
= on
>>8;
172 IOWriteB(PIC_MASTER_DATA
, (IOReadB(PIC_MASTER_DATA
) & ~pic1off
) | pic1on
);
173 IOWriteB(PIC_SLAVE_DATA
, (IOReadB(PIC_SLAVE_DATA
) & ~pic2off
) | pic2on
);
177 VOID
EnableHwIRQ(UCHAR hwirq
, EMULATOR_INT32_PROC func
)
181 PicSetIRQMask(1 << hwirq
, 0);
183 vector
= BIOS_PIC_MASTER_INT
+ hwirq
;
185 vector
= BIOS_PIC_SLAVE_INT
+ hwirq
- 8;
187 RegisterBiosInt32(vector
, func
);
191 VOID
PicIRQComplete(LPWORD Stack
)
193 /* Get the interrupt number */
194 BYTE IntNum
= LOBYTE(Stack
[STACK_INT_NUM
]);
197 * If this was a PIC IRQ, send an End-of-Interrupt to the PIC.
200 if (IntNum
>= BIOS_PIC_MASTER_INT
&& IntNum
< BIOS_PIC_MASTER_INT
+ 8)
202 /* It was an IRQ from the master PIC */
203 IOWriteB(PIC_MASTER_CMD
, PIC_OCW2_EOI
);
205 else if (IntNum
>= BIOS_PIC_SLAVE_INT
&& IntNum
< BIOS_PIC_SLAVE_INT
+ 8)
207 /* It was an IRQ from the slave PIC */
208 IOWriteB(PIC_SLAVE_CMD
, PIC_OCW2_EOI
);
209 IOWriteB(PIC_MASTER_CMD
, PIC_OCW2_EOI
);
213 static VOID WINAPI
BiosHandleMasterPicIRQ(LPWORD Stack
)
217 IOWriteB(PIC_MASTER_CMD
, PIC_OCW3_READ_ISR
/* == 0x0B */);
218 IrqNumber
= IOReadB(PIC_MASTER_CMD
);
220 DPRINT("Master - IrqNumber = 0x%x\n", IrqNumber
);
222 PicIRQComplete(Stack
);
225 static VOID WINAPI
BiosHandleSlavePicIRQ(LPWORD Stack
)
229 IOWriteB(PIC_SLAVE_CMD
, PIC_OCW3_READ_ISR
/* == 0x0B */);
230 IrqNumber
= IOReadB(PIC_SLAVE_CMD
);
232 DPRINT("Slave - IrqNumber = 0x%x\n", IrqNumber
);
234 PicIRQComplete(Stack
);
238 static VOID WINAPI
BiosTimerIrq(LPWORD Stack
)
241 * Perform the system timer interrupt.
243 * Do not call directly BiosSystemTimerInterrupt(Stack);
244 * because some programs may hook only BIOS_SYS_TIMER_INTERRUPT
245 * for their purpose...
247 /** EmulatorInterrupt(BIOS_SYS_TIMER_INTERRUPT); **/
248 Int32Call(&BiosContext
, BIOS_SYS_TIMER_INTERRUPT
);
249 PicIRQComplete(Stack
);
253 static VOID
BiosHwSetup(VOID
)
255 /* Initialize the master and the slave PICs (cascade mode) */
256 IOWriteB(PIC_MASTER_CMD
, PIC_ICW1
| PIC_ICW1_ICW4
);
257 IOWriteB(PIC_SLAVE_CMD
, PIC_ICW1
| PIC_ICW1_ICW4
);
260 * Set the interrupt vector offsets for each PIC
261 * (base IRQs: 0x08-0x0F for IRQ 0-7, 0x70-0x77 for IRQ 8-15)
263 IOWriteB(PIC_MASTER_DATA
, BIOS_PIC_MASTER_INT
);
264 IOWriteB(PIC_SLAVE_DATA
, BIOS_PIC_SLAVE_INT
);
266 /* Tell the master PIC that there is a slave PIC at IRQ 2 */
267 IOWriteB(PIC_MASTER_DATA
, 1 << 2);
268 /* Tell the slave PIC its cascade identity */
269 IOWriteB(PIC_SLAVE_DATA
, 2);
271 /* Make sure both PICs are in 8086 mode */
272 IOWriteB(PIC_MASTER_DATA
, PIC_ICW4_8086
);
273 IOWriteB(PIC_SLAVE_DATA
, PIC_ICW4_8086
);
275 /* Clear the masks for both PICs */
276 // IOWriteB(PIC_MASTER_DATA, 0x00);
277 // IOWriteB(PIC_SLAVE_DATA , 0x00);
278 /* Disable all IRQs */
279 IOWriteB(PIC_MASTER_DATA
, 0xFF);
280 IOWriteB(PIC_SLAVE_DATA
, 0xFF);
283 /* Initialize PIT Counter 0 */
284 IOWriteB(PIT_COMMAND_PORT
, 0x34);
285 IOWriteB(PIT_DATA_PORT(0), 0x00);
286 IOWriteB(PIT_DATA_PORT(0), 0x00);
288 /* Initialize PIT Counter 1 */
289 IOWriteB(PIT_COMMAND_PORT
, 0x74);
290 IOWriteB(PIT_DATA_PORT(1), 0x00);
291 IOWriteB(PIT_DATA_PORT(1), 0x00);
293 /* Initialize PIT Counter 2 */
294 IOWriteB(PIT_COMMAND_PORT
, 0xB4);
295 IOWriteB(PIT_DATA_PORT(2), 0x00);
296 IOWriteB(PIT_DATA_PORT(2), 0x00);
298 EnableHwIRQ(0, BiosTimerIrq
);
301 static VOID
InitializeBiosInt32(VOID
)
304 // USHORT Offset = 0;
306 /* Initialize the callback context */
307 InitializeContext(&BiosContext
, BIOS_SEGMENT
, 0x0000);
309 /* Register the BIOS 32-bit Interrupts */
310 for (i
= 0x00; i
<= 0xFF; i
++)
312 // Offset += RegisterInt32(MAKELONG(Offset, BIOS_SEGMENT), i, NULL, NULL);
313 BiosContext
.NextOffset
+= RegisterInt32(MAKELONG(BiosContext
.NextOffset
,
314 BiosContext
.Segment
),
318 /* Initialize the exception vector interrupts to a default Exception handler */
319 for (i
= 0; i
< 8; i
++)
320 RegisterBiosInt32(i
, BiosException
);
322 /* Initialize HW vector interrupts to a default HW handler */
323 for (i
= BIOS_PIC_MASTER_INT
; i
< BIOS_PIC_MASTER_INT
+ 8; i
++)
324 RegisterBiosInt32(i
, BiosHandleMasterPicIRQ
);
325 for (i
= BIOS_PIC_SLAVE_INT
; i
< BIOS_PIC_SLAVE_INT
+ 8; i
++)
326 RegisterBiosInt32(i
, BiosHandleSlavePicIRQ
);
328 /* Initialize software vector handlers */
329 RegisterBiosInt32(BIOS_EQUIPMENT_INTERRUPT
, BiosEquipmentService
);
330 RegisterBiosInt32(BIOS_MEMORY_SIZE
, BiosGetMemorySize
);
331 RegisterBiosInt32(BIOS_MISC_INTERRUPT
, BiosMiscService
);
332 RegisterBiosInt32(BIOS_TIME_INTERRUPT
, BiosTimeService
);
333 RegisterBiosInt32(BIOS_SYS_TIMER_INTERRUPT
, BiosSystemTimerInterrupt
);
335 /* Some interrupts are in fact addresses to tables */
336 ((PULONG
)BaseAddress
)[0x1E] = (ULONG
)NULL
;
337 ((PULONG
)BaseAddress
)[0x41] = (ULONG
)NULL
;
338 ((PULONG
)BaseAddress
)[0x46] = (ULONG
)NULL
;
339 ((PULONG
)BaseAddress
)[0x48] = (ULONG
)NULL
;
340 ((PULONG
)BaseAddress
)[0x49] = (ULONG
)NULL
;
343 /* PUBLIC FUNCTIONS ***********************************************************/
346 * The BIOS POST (Power On-Self Test)
348 BOOLEAN
Bios32Initialize(VOID
)
353 /* Initialize the stack */
354 // That's what says IBM... (stack at 30:00FF going downwards)
357 setSS(0x0050); // Stack at 50:0400, going downwards
360 /* Set data segment */
363 /* Initialize the BDA contents */
364 Bda
->EquipmentList
= BIOS_EQUIPMENT_LIST
;
367 * Retrieve the conventional memory size
368 * in kB from CMOS, typically 640 kB.
370 IOWriteB(CMOS_ADDRESS_PORT
, CMOS_REG_BASE_MEMORY_LOW
);
371 Low
= IOReadB(CMOS_DATA_PORT
);
372 IOWriteB(CMOS_ADDRESS_PORT
, CMOS_REG_BASE_MEMORY_HIGH
);
373 High
= IOReadB(CMOS_DATA_PORT
);
374 Bda
->MemorySize
= MAKEWORD(Low
, High
);
376 /* Register the BIOS 32-bit Interrupts */
377 InitializeBiosInt32();
379 /* Initialize platform hardware (PIC/PIT chips, ...) */
382 /* Initialize the Keyboard and Video BIOS */
383 if (!KbdBios32Initialize() || !VidBios32Initialize()) return FALSE
;
385 ///////////// MUST BE DONE AFTER IVT INITIALIZATION !! /////////////////////
388 Success
= LoadRom("boot.bin", (PVOID
)0xE0000, NULL
);
389 DPRINT1("Test ROM loading %s ; GetLastError() = %u\n", Success
? "succeeded" : "failed", GetLastError());
391 SearchAndInitRoms(&BiosContext
);
397 VOID
Bios32Cleanup(VOID
)