Fix a couple of problems with FreeLDR portability.
[reactos.git] / reactos / boot / freeldr / freeldr / arch / i386 / i386rtl.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21
22 void PcBeep(void)
23 {
24 sound(700);
25 delay(200);
26 sound(0);
27 }
28
29 void delay(unsigned msec)
30 {
31 REGS Regs;
32 unsigned usec;
33 unsigned msec_this;
34
35 // Int 15h AH=86h
36 // BIOS - WAIT (AT,PS)
37 //
38 // AH = 86h
39 // CX:DX = interval in microseconds
40 // Return:
41 // CF clear if successful (wait interval elapsed)
42 // CF set on error or AH=83h wait already in progress
43 // AH = status (see #00496)
44
45 // Note: The resolution of the wait period is 977 microseconds on
46 // many systems because many BIOSes use the 1/1024 second fast
47 // interrupt from the AT real-time clock chip which is available on INT 70;
48 // because newer BIOSes may have much more precise timers available, it is
49 // not possible to use this function accurately for very short delays unless
50 // the precise behavior of the BIOS is known (or found through testing)
51
52 while (msec)
53 {
54 msec_this = msec;
55
56 if (msec_this > 4000)
57 {
58 msec_this = 4000;
59 }
60
61 usec = msec_this * 1000;
62
63 Regs.b.ah = 0x86;
64 Regs.w.cx = usec >> 16;
65 Regs.w.dx = usec & 0xffff;
66 Int386(0x15, &Regs, &Regs);
67
68 msec -= msec_this;
69 }
70 }
71
72 void sound(int freq)
73 {
74 int scale;
75
76 if (freq == 0)
77 {
78 WRITE_PORT_UCHAR((PUCHAR)0x61, READ_PORT_UCHAR((PUCHAR)0x61) & ~3);
79 return;
80 }
81
82 scale = 1193046 / freq;
83 WRITE_PORT_UCHAR((PUCHAR)0x43, 0xb6);
84 WRITE_PORT_UCHAR((PUCHAR)0x42, scale & 0xff);
85 WRITE_PORT_UCHAR((PUCHAR)0x42, scale >> 8);
86 WRITE_PORT_UCHAR((PUCHAR)0x61, READ_PORT_UCHAR((PUCHAR)0x61) | 3);
87 }