OSDN Git Service

seriously i want sound!!! bakapee!
[proj16/16.git] / 16 / ADT2PLAY / timerint.pas
1 unit TimerInt;
2 interface
3
4 const
5   _debug_str_: String = '';
6
7 procedure TimerSetup(Hz: Longint);
8 procedure TimerDone;
9 procedure TimerInstallHandler(handler: Pointer);
10 procedure TimerRemoveHandler;
11
12 implementation
13
14 uses DOS;
15
16 var
17   oldint08: {$IFDEF __TMT__} FarPointer
18             {$ELSE} Pointer
19             {$ENDIF};
20 var
21   newint08: Pointer;
22   counter,
23   clock_ticks,clock_flag: Word;
24   ticks: Longint;
25
26 const
27   timer_handler: Pointer = NIL;
28
29 procedure int08; interrupt;
30           assembler;
31 asm
32 {$IFNDEF _32BIT}
33         cmp     word ptr timer_handler,0
34         jnz     @@1
35         cmp     word ptr timer_handler+2,0
36         jz      @@2
37 @@1:    push    ds
38         call    [timer_handler]
39         pop     ds
40 @@2:    mov     ax,word ptr ticks
41         mov     bx,word ptr ticks+2
42         add     ax,1
43         adc     bx,0
44         mov     word ptr ticks,ax
45         mov     word ptr ticks+2,bx
46         inc     clock_ticks
47         mov     ax,clock_ticks
48         cmp     ax,clock_flag
49         jb      @@3
50         mov     clock_ticks,0
51         pushf
52         call    [oldint08]
53         jmp     @@ret
54 @@3:    mov     al,60h
55         out     20h,al
56 {$ELSE}
57         cmp     timer_handler,0
58         jz      @@1
59         push    ds
60         push    es
61         call    [timer_handler]
62         pop     es
63         pop     ds
64 @@1:    inc     ticks
65         inc     clock_ticks
66         mov     ax,clock_ticks
67         cmp     ax,clock_flag
68         jnz     @@2
69         mov     clock_ticks,0
70         pushfd
71         call    [oldint08]
72         jmp     @@ret
73 @@2:    mov     al,60h
74         out     20h,al
75 {$ENDIF}
76 @@ret:
77 end;
78
79 procedure DisableTimerIRQ; assembler;
80 asm
81         in      al,21h
82         or      al,1
83         out     21h,al
84 end;
85
86 procedure EnableTimerIRQ; assembler;
87 asm
88         in      al,21h
89         and     al,0feh
90         out     21h,al
91 end;
92
93 procedure TimerSetup(Hz: Longint);
94 begin
95   _debug_str_ := 'TIMERINT.PAS:TimerSetup';
96   If (Hz < 19) then Hz := 19;
97   If (Hz > 1193180) then Hz := 1193180;
98
99   counter := 1193180 DIV Hz;
100   clock_flag := Hz*1000 DIV 18206;
101   newint08 := @int08;
102   ticks := 0;
103   clock_ticks := 0;
104
105   DisableTimerIRQ;
106   asm
107         mov     al,36h
108         out     43h,al
109         mov     bx,counter
110         mov     al,bl
111         out     40h,al
112         mov     al,bh
113         out     40h,al
114   end;
115
116   SetIntVec($08,newint08);
117   EnableTimerIRQ;
118 end;
119
120 procedure TimerDone;
121 begin
122   _debug_str_ := 'TIMERINT.PAS:TimerDone';
123   DisableTimerIRQ;
124   asm
125         mov     al,36h
126         out     43h,al
127         xor     ax,ax
128         out     40h,al
129         out     40h,al
130   end;
131
132   SetIntVec($08,oldint08);
133   EnableTimerIRQ;
134 end;
135
136 procedure TimerInstallHandler(handler: Pointer);
137 begin
138   _debug_str_ := 'TIMERINT.PAS:TimerInstallHandler';
139   DisableTimerIRQ;
140   timer_handler := handler;
141   EnableTimerIRQ;
142 end;
143
144 procedure TimerRemoveHandler;
145 begin
146   _debug_str_ := 'TIMERINT.PAS:TimerRemoveHandler';
147   DisableTimerIRQ;
148   timer_handler := NIL;
149   EnableTimerIRQ;
150 end;
151
152 begin
153   GetIntVec($08,oldint08);
154 end.