1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* PC/HW routine collection v1.5 for DOS/DJGPP
*
* Copyright (C) 2002 - Daniel Borca
* Email : dborca@yahoo.com
* Web : http://www.geocities.com/dborca
*/
#include <pc.h>
#include <string.h>
#include "pc_hw.h"
#define TIMER_IRQ 0
#define MAX_TIMERS 8
#define PIT_FREQ 0x1234DD
#define ADJUST(timer, basefreq) timer.counter = PIT_FREQ * timer.freq / SQR(basefreq)
#define unvolatile(__v, __t) __extension__ ({union { volatile __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p;})
static int timer_installed;
typedef struct {
volatile unsigned int counter, clock_ticks, freq;
volatile PFUNC func;
volatile void *parm;
} TIMER;
static TIMER timer_main, timer_func[MAX_TIMERS];
/* Desc: main timer callback
*
* In : -
* Out : 0 to bypass BIOS, 1 to chain to BIOS
*
* Note: -
*/
static int
timer ()
{
int i;
for (i = 0; i < MAX_TIMERS; i++) {
TIMER *t = &timer_func[i];
if (t->func) {
t->clock_ticks += t->counter;
if (t->clock_ticks >= timer_main.counter) {
t->clock_ticks -= timer_main.counter;
t->func(unvolatile(t->parm, void *));
}
}
}
timer_main.clock_ticks += timer_main.counter;
if (timer_main.clock_ticks >= 0x10000) {
timer_main.clock_ticks -= 0x10000;
return 1;
} else {
outportb(0x20, 0x20);
return 0;
}
} ENDOFUNC(timer)
/* Desc: uninstall timer engine
*
* In : -
* Out : -
*
* Note: -
*/
void
pc_remove_timer (void)
{
if (timer_installed) {
timer_installed = FALSE;
pc_clexit(pc_remove_timer);
DISABLE();
outportb(0x43, 0x34);
outportb(0x40, 0);
outportb(0x40, 0);
ENABLE();
pc_remove_irq(TIMER_IRQ);
}
}
/* Desc: remove timerfunc
*
* In : timerfunc id
* Out : 0 if success
*
* Note: tries to relax the main timer whenever possible
*/
int
pc_remove_int (int fid)
{
int i;
unsigned int freq = 0;
/* are we installed? */
if (!timer_installed) {
return -1;
}
/* sanity check */
if ((fid < 0) || (fid >= MAX_TIMERS) || (timer_func[fid].func == NULL)) {
return -1;
}
timer_func[fid].func = NULL;
/* scan for maximum frequency */
for (i = 0; i < MAX_TIMERS; i++) {
TIMER *t = &timer_func[i];
if (t->func) {
if (freq < t->freq) {
freq = t->freq;
}
}
}
/* if there are no callbacks left, cleanup */
if (!freq) {
pc_remove_timer();
return 0;
}
/* if we just lowered the maximum frequency, try to relax the timer engine */
if (freq < timer_main.freq) {
unsigned int new_counter = PIT_FREQ / freq;
DISABLE();
for (i = 0; i < MAX_TIMERS; i++) {
if (timer_func[i].func) {
ADJUST(timer_func[i], freq);
}
}
outportb(0x43, 0x34);
outportb(0x40, (unsigned char)new_counter);
outportb(0x40, (unsigned char)(new_counter>>8));
timer_main.clock_ticks = 0;
timer_main.counter = new_counter;
timer_main.freq = freq;
ENABLE();
}
return 0;
} ENDOFUNC(pc_remove_int)
/* Desc: adjust timerfunc
*
* In : timerfunc id, new frequency (Hz)
* Out : 0 if success
*
* Note: might change the main timer frequency
*/
int
pc_adjust_int (int fid, unsigned int freq)
{
int i;
/* are we installed? */
if (!timer_installed) {
return -1;
}
/* sanity check */
if ((fid < 0) || (fid >= MAX_TIMERS) || (timer_func[fid].func == NULL)) {
return -1;
}
timer_func[fid].freq = freq;
/* scan for maximum frequency */
freq = 0;
for (i = 0; i < MAX_TIMERS; i++) {
TIMER *t = &timer_func[i];
if (t->func) {
if (freq < t->freq) {
freq = t->freq;
}
}
}
/* update main timer / sons to match highest frequency */
DISABLE();
/* using '>' is correct still (and avoids updating
* the HW timer too often), but doesn't relax the timer!
*/
if (freq != timer_main.freq) {
unsigned int new_counter = PIT_FREQ / freq;
for (i = 0; i < MAX_TIMERS; i++) {
if (timer_func[i].func) {
ADJUST(timer_func[i], freq);
}
}
outportb(0x43, 0x34);
outportb(0x40, (unsigned char)new_counter);
outportb(0x40, (unsigned char)(new_counter>>8));
timer_main.clock_ticks = 0;
timer_main.counter = new_counter;
timer_main.freq = freq;
} else {
ADJUST(timer_func[fid], timer_main.freq);
}
ENABLE();
return 0;
} ENDOFUNC(pc_adjust_int)
/* Desc: install timer engine
*
* In : -
* Out : 0 for success
*
* Note: initial frequency is 18.2 Hz
*/
static int
install_timer (void)
{
if (timer_installed || pc_install_irq(TIMER_IRQ, timer)) {
return -1;
} else {
memset(timer_func, 0, sizeof(timer_func));
LOCKDATA(timer_func);
LOCKDATA(timer_main);
LOCKFUNC(timer);
LOCKFUNC(pc_adjust_int);
LOCKFUNC(pc_remove_int);
timer_main.counter = 0x10000;
DISABLE();
outportb(0x43, 0x34);
outportb(0x40, 0);
outportb(0x40, 0);
timer_main.clock_ticks = 0;
ENABLE();
pc_atexit(pc_remove_timer);
timer_installed = TRUE;
return 0;
}
}
/* Desc: install timerfunc
*
* In : callback function, opaque pointer to be passed to callee, freq (Hz)
* Out : timerfunc id (0 .. MAX_TIMERS-1)
*
* Note: returns -1 if error
*/
int
pc_install_int (PFUNC func, void *parm, unsigned int freq)
{
int i;
TIMER *t = NULL;
/* ensure the timer engine is set up */
if (!timer_installed) {
if (install_timer()) {
return -1;
}
}
/* find an empty slot */
for (i = 0; i < MAX_TIMERS; i++) {
if (!timer_func[i].func) {
t = &timer_func[i];
break;
}
}
if (t == NULL) {
return -1;
}
DISABLE();
t->func = func;
t->parm = parm;
t->freq = freq;
t->clock_ticks = 0;
/* update main timer / sons to match highest frequency */
if (freq > timer_main.freq) {
unsigned int new_counter = PIT_FREQ / freq;
for (i = 0; i < MAX_TIMERS; i++) {
if (timer_func[i].func) {
ADJUST(timer_func[i], freq);
}
}
outportb(0x43, 0x34);
outportb(0x40, (unsigned char)new_counter);
outportb(0x40, (unsigned char)(new_counter>>8));
timer_main.clock_ticks = 0;
timer_main.counter = new_counter;
timer_main.freq = freq;
} else {
/* t == &timer_func[i] */
ADJUST(timer_func[i], timer_main.freq);
}
i = t - timer_func;
ENABLE();
return i;
}
|