1 #include "ibm.h"
2 
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"
10 
11 #define TIMERS_MAX 32
12 
13 int TIMER_USEC;
14 
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];
23 
24 int timers_present = 0;
25 int timer_one = 1;
26 
27 int timer_count = 0, timer_latch = 0;
28 int timer_start = 0;
29 
timer_process()30 void timer_process()
31 {
32 	int c;
33 	int process = 0;
34 	/*Get actual elapsed time*/
35 	int diff = timer_latch - timer_count;
36 	int enable[TIMERS_MAX];
37 
38 	timer_latch = 0;
39 
40         for (c = 0; c < timers_present; c++)
41         {
42                 enable[c] = *timers[c].enable;
43                 if (*timers[c].enable)
44                 {
45                         *timers[c].count = *timers[c].count - diff;
46                         if (*timers[c].count <= 0)
47                                 process = 1;
48                 }
49         }
50 
51         if (!process)
52                 return;
53 
54         while (1)
55         {
56                 int lowest = 1, lowest_c;
57 
58                 for (c = 0; c < timers_present; c++)
59                 {
60                         if (enable[c])
61                         {
62                                 if (*timers[c].count < lowest)
63                                 {
64                                         lowest = *timers[c].count;
65                                         lowest_c = c;
66                                 }
67                         }
68                 }
69 
70                 if (lowest > 0)
71                         break;
72 
73                 timers[lowest_c].callback(timers[lowest_c].priv);
74                 enable[lowest_c] = *timers[lowest_c].enable;
75         }
76 }
77 
timer_update_outstanding()78 void timer_update_outstanding()
79 {
80 	int c;
81 	timer_latch = 0x7fffffff;
82 	for (c = 0; c < timers_present; c++)
83 	{
84 		if (*timers[c].enable && *timers[c].count < timer_latch)
85 			timer_latch = *timers[c].count;
86 	}
87 	timer_count = timer_latch = (timer_latch + ((1 << TIMER_SHIFT) - 1));
88 }
89 
timer_reset()90 void timer_reset()
91 {
92 	pclog("timer_reset\n");
93 	timers_present = 0;
94 	timer_latch = timer_count = 0;
95 //	timer_process();
96 }
97 
timer_add(void (* callback)(void * priv),int * count,int * enable,void * priv)98 int timer_add(void (*callback)(void *priv), int *count, int *enable, void *priv)
99 {
100 	if (timers_present < TIMERS_MAX)
101 	{
102 //		pclog("timer_add : adding timer %i\n", timers_present);
103 		timers[timers_present].present = 1;
104 		timers[timers_present].callback = callback;
105 		timers[timers_present].priv = priv;
106 		timers[timers_present].count = count;
107 		timers[timers_present].enable = enable;
108 		timers_present++;
109 		return timers_present - 1;
110 	}
111 	return -1;
112 }
113 
timer_set_callback(int timer,void (* callback)(void * priv))114 void timer_set_callback(int timer, void (*callback)(void *priv))
115 {
116 	timers[timer].callback = callback;
117 }
118