PCem
view src/timer.c @ 111:912e602b3406
Moved PIT to common timer API.
| author | TomW |
|---|---|
| date | Thu Jun 19 21:02:59 2014 +0100 |
| parents | 9c201151bb4b |
| children |
line source
1 #include "ibm.h"
3 /*#include "sound_opl.h"
4 #include "adlibgold.h"
5 #include "sound_pas16.h"
6 #include "sound_sb.h"
7 #include "sound_sb_dsp.h"
8 #include "sound_wss.h"*/
9 #include "timer.h"
11 #define TIMERS_MAX 32
13 int TIMER_USEC;
15 static struct
16 {
17 int present;
18 void (*callback)(void *priv);
19 void *priv;
20 int *enable;
21 int *count;
22 } timers[TIMERS_MAX];
24 int timers_present = 0;
25 int timer_one = 1;
27 int timer_count = 0, timer_latch = 0;
28 int timer_start = 0;
30 void timer_process()
31 {
32 int c;
33 int retry;
34 /*Get actual elapsed time*/
35 int diff = timer_latch - timer_count;
38 timer_latch = 0;
40 do
41 {
42 retry = 0;
43 for (c = 0; c < timers_present; c++)
44 {
45 if (*timers[c].enable)
46 {
47 *timers[c].count = *timers[c].count - (diff << TIMER_SHIFT);
48 if (*timers[c].count <= 0)
49 timers[c].callback(timers[c].priv);
50 if (*timers[c].count <= 0)
51 retry = 1;
52 }
53 }
54 diff = 0;
55 }
56 while (retry);
57 }
59 void timer_update_outstanding()
60 {
61 int c;
62 timer_latch = 0x7fffffff;
63 for (c = 0; c < timers_present; c++)
64 {
65 if (*timers[c].enable && *timers[c].count < timer_latch)
66 timer_latch = *timers[c].count;
67 }
68 timer_count = timer_latch = (timer_latch + ((1 << TIMER_SHIFT) - 1)) >> TIMER_SHIFT;
69 }
71 void timer_reset()
72 {
73 pclog("timer_reset\n");
74 timers_present = 0;
75 timer_latch = timer_count = 0;
76 // timer_process();
77 }
79 int timer_add(void (*callback)(void *priv), int *count, int *enable, void *priv)
80 {
81 if (timers_present < TIMERS_MAX)
82 {
83 // pclog("timer_add : adding timer %i\n", timers_present);
84 timers[timers_present].present = 1;
85 timers[timers_present].callback = callback;
86 timers[timers_present].priv = priv;
87 timers[timers_present].count = count;
88 timers[timers_present].enable = enable;
89 timers_present++;
90 return timers_present - 1;
91 }
92 return -1;
93 }
95 void timer_set_callback(int timer, void (*callback)(void *priv))
96 {
97 timers[timer].callback = callback;
98 }
