Sync with trunk r63270.
[reactos.git] / 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 default:
116 {
117 DPRINT1("BIOS Function INT 15h, AH = 0x%02X NOT IMPLEMENTED\n",
118 getAH());
119 }
120 }
121 }
122
123 static VOID WINAPI BiosTimeService(LPWORD Stack)
124 {
125 switch (getAH())
126 {
127 case 0x00:
128 {
129 /* Set AL to 1 if midnight had passed, 0 otherwise */
130 setAL(Bda->MidnightPassed ? 0x01 : 0x00);
131
132 /* Return the tick count in CX:DX */
133 setCX(HIWORD(Bda->TickCounter));
134 setDX(LOWORD(Bda->TickCounter));
135
136 /* Reset the midnight flag */
137 Bda->MidnightPassed = FALSE;
138
139 break;
140 }
141
142 case 0x01:
143 {
144 /* Set the tick count to CX:DX */
145 Bda->TickCounter = MAKELONG(getDX(), getCX());
146
147 /* Reset the midnight flag */
148 Bda->MidnightPassed = FALSE;
149
150 break;
151 }
152
153 default:
154 {
155 DPRINT1("BIOS Function INT 1Ah, AH = 0x%02X NOT IMPLEMENTED\n",
156 getAH());
157 }
158 }
159 }
160
161 static VOID WINAPI BiosSystemTimerInterrupt(LPWORD Stack)
162 {
163 /* Increase the system tick count */
164 Bda->TickCounter++;
165 }
166
167
168 // From SeaBIOS
169 static VOID PicSetIRQMask(USHORT off, USHORT on)
170 {
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);
174 }
175
176 // From SeaBIOS
177 VOID EnableHwIRQ(UCHAR hwirq, EMULATOR_INT32_PROC func)
178 {
179 UCHAR vector;
180
181 PicSetIRQMask(1 << hwirq, 0);
182 if (hwirq < 8)
183 vector = BIOS_PIC_MASTER_INT + hwirq;
184 else
185 vector = BIOS_PIC_SLAVE_INT + hwirq - 8;
186
187 RegisterBiosInt32(vector, func);
188 }
189
190
191 VOID PicIRQComplete(LPWORD Stack)
192 {
193 /* Get the interrupt number */
194 BYTE IntNum = LOBYTE(Stack[STACK_INT_NUM]);
195
196 /*
197 * If this was a PIC IRQ, send an End-of-Interrupt to the PIC.
198 */
199
200 if (IntNum >= BIOS_PIC_MASTER_INT && IntNum < BIOS_PIC_MASTER_INT + 8)
201 {
202 /* It was an IRQ from the master PIC */
203 IOWriteB(PIC_MASTER_CMD, PIC_OCW2_EOI);
204 }
205 else if (IntNum >= BIOS_PIC_SLAVE_INT && IntNum < BIOS_PIC_SLAVE_INT + 8)
206 {
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);
210 }
211 }
212
213 static VOID WINAPI BiosHandleMasterPicIRQ(LPWORD Stack)
214 {
215 BYTE IrqNumber;
216
217 IOWriteB(PIC_MASTER_CMD, PIC_OCW3_READ_ISR /* == 0x0B */);
218 IrqNumber = IOReadB(PIC_MASTER_CMD);
219
220 DPRINT("Master - IrqNumber = 0x%x\n", IrqNumber);
221
222 PicIRQComplete(Stack);
223 }
224
225 static VOID WINAPI BiosHandleSlavePicIRQ(LPWORD Stack)
226 {
227 BYTE IrqNumber;
228
229 IOWriteB(PIC_SLAVE_CMD, PIC_OCW3_READ_ISR /* == 0x0B */);
230 IrqNumber = IOReadB(PIC_SLAVE_CMD);
231
232 DPRINT("Slave - IrqNumber = 0x%x\n", IrqNumber);
233
234 PicIRQComplete(Stack);
235 }
236
237 // Timer IRQ 0
238 static VOID WINAPI BiosTimerIrq(LPWORD Stack)
239 {
240 /*
241 * Perform the system timer interrupt.
242 *
243 * Do not call directly BiosSystemTimerInterrupt(Stack);
244 * because some programs may hook only BIOS_SYS_TIMER_INTERRUPT
245 * for their purpose...
246 */
247 /** EmulatorInterrupt(BIOS_SYS_TIMER_INTERRUPT); **/
248 Int32Call(&BiosContext, BIOS_SYS_TIMER_INTERRUPT);
249 PicIRQComplete(Stack);
250 }
251
252
253 static VOID BiosHwSetup(VOID)
254 {
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);
258
259 /*
260 * Set the interrupt vector offsets for each PIC
261 * (base IRQs: 0x08-0x0F for IRQ 0-7, 0x70-0x77 for IRQ 8-15)
262 */
263 IOWriteB(PIC_MASTER_DATA, BIOS_PIC_MASTER_INT);
264 IOWriteB(PIC_SLAVE_DATA , BIOS_PIC_SLAVE_INT );
265
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);
270
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);
274
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);
281
282
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);
287
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);
292
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);
297
298 EnableHwIRQ(0, BiosTimerIrq);
299 }
300
301 static VOID InitializeBiosInt32(VOID)
302 {
303 USHORT i;
304 // USHORT Offset = 0;
305
306 /* Initialize the callback context */
307 InitializeContext(&BiosContext, BIOS_SEGMENT, 0x0000);
308
309 /* Register the BIOS 32-bit Interrupts */
310 for (i = 0x00; i <= 0xFF; i++)
311 {
312 // Offset += RegisterInt32(MAKELONG(Offset, BIOS_SEGMENT), i, NULL, NULL);
313 BiosContext.NextOffset += RegisterInt32(MAKELONG(BiosContext.NextOffset,
314 BiosContext.Segment),
315 i, NULL, NULL);
316 }
317
318 /* Initialize the exception vector interrupts to a default Exception handler */
319 for (i = 0; i < 8; i++)
320 RegisterBiosInt32(i, BiosException);
321
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);
327
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);
334
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;
341 }
342
343 /* PUBLIC FUNCTIONS ***********************************************************/
344
345 /*
346 * The BIOS POST (Power On-Self Test)
347 */
348 BOOLEAN Bios32Initialize(VOID)
349 {
350 BOOLEAN Success;
351 UCHAR Low, High;
352
353 /* Initialize the stack */
354 // That's what says IBM... (stack at 30:00FF going downwards)
355 // setSS(0x0000);
356 // setSP(0x0400);
357 setSS(0x0050); // Stack at 50:0400, going downwards
358 setSP(0x0400);
359
360 /* Set data segment */
361 setDS(BDA_SEGMENT);
362
363 /* Initialize the BDA contents */
364 Bda->EquipmentList = BIOS_EQUIPMENT_LIST;
365
366 /*
367 * Retrieve the conventional memory size
368 * in kB from CMOS, typically 640 kB.
369 */
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);
375
376 /* Register the BIOS 32-bit Interrupts */
377 InitializeBiosInt32();
378
379 /* Initialize platform hardware (PIC/PIT chips, ...) */
380 BiosHwSetup();
381
382 /* Initialize the Keyboard and Video BIOS */
383 if (!KbdBios32Initialize() || !VidBios32Initialize()) return FALSE;
384
385 ///////////// MUST BE DONE AFTER IVT INITIALIZATION !! /////////////////////
386
387 /* Load some ROMs */
388 Success = LoadRom("boot.bin", (PVOID)0xE0000, NULL);
389 DPRINT1("Test ROM loading %s ; GetLastError() = %u\n", Success ? "succeeded" : "failed", GetLastError());
390
391 SearchAndInitRoms(&BiosContext);
392
393 /* We are done */
394 return TRUE;
395 }
396
397 VOID Bios32Cleanup(VOID)
398 {
399 VidBios32Cleanup();
400 KbdBios32Cleanup();
401 }
402
403 /* EOF */