initial commit
[metatoy] / src / kern / timer.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018-2023 John Tsiombikas <nuclear@member.fsf.org>
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 3 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, see <https://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include "intr.h"
20 #include "asmops.h"
21 #include "timer.h"
22 #include "panic.h"
23 #include "config.h"
24
25 /* frequency of the oscillator driving the 8254 timer */
26 #define OSC_FREQ_HZ             1193182
27
28 /* macro to divide and round to the nearest integer */
29 #define DIV_ROUND(a, b) ((a) / (b) + ((a) % (b)) / ((b) / 2))
30
31 /* I/O ports connected to the 8254 */
32 #define PORT_DATA0      0x40
33 #define PORT_DATA1      0x41
34 #define PORT_DATA2      0x42
35 #define PORT_CMD        0x43
36
37 /* command bits */
38 #define CMD_CHAN0                       0
39 #define CMD_CHAN1                       (1 << 6)
40 #define CMD_CHAN2                       (2 << 6)
41 #define CMD_RDBACK                      (3 << 6)
42
43 #define CMD_LATCH                       0
44 #define CMD_ACCESS_LOW          (1 << 4)
45 #define CMD_ACCESS_HIGH         (2 << 4)
46 #define CMD_ACCESS_BOTH         (3 << 4)
47
48 #define CMD_OP_INT_TERM         0
49 #define CMD_OP_ONESHOT          (1 << 1)
50 #define CMD_OP_RATE                     (2 << 1)
51 #define CMD_OP_SQWAVE           (3 << 1)
52 #define CMD_OP_SOFT_STROBE      (4 << 1)
53 #define CMD_OP_HW_STROBE        (5 << 1)
54
55 #define CMD_MODE_BIN            0
56 #define CMD_MODE_BCD            1
57
58 volatile unsigned long nticks;
59
60 struct timer_event {
61         int dt; /* remaining ticks delta from the previous event */
62         void (*func)(void);
63         struct timer_event *next;
64 };
65
66 static void timer_handler(int inum);
67
68 static struct timer_event *evlist;
69
70
71 void init_timer(void)
72 {
73         /* calculate the reload count: round(osc / freq) */
74         int reload_count = DIV_ROUND(OSC_FREQ_HZ, TICK_FREQ_HZ);
75
76         /* set the mode to square wave for channel 0, both low
77          * and high reload count bytes will follow...
78          */
79         outp(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
80
81         /* write the low and high bytes of the reload count to the
82          * port for channel 0
83          */
84         outp(PORT_DATA0, reload_count & 0xff);
85         outp(PORT_DATA0, (reload_count >> 8) & 0xff);
86
87         /* set the timer interrupt handler */
88         interrupt(IRQ_TO_INTR(0), timer_handler);
89         unmask_irq(0);
90 }
91
92 void cleanup_timer(void)
93 {
94         /* return the timer to the original rate */
95         outp(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
96         outp(PORT_DATA0, 0);
97         outp(PORT_DATA0, 0);
98 }
99
100 void set_alarm(unsigned long msec, void (*func)(void))
101 {
102         int ticks, tsum, iflag;
103         struct timer_event *ev, *node;
104
105         if((ticks = MSEC_TO_TICKS(msec)) <= 0) {
106                 return;
107         }
108
109         if(!(ev = malloc(sizeof *ev))) {
110                 panic("failed to allocate timer event");
111                 return;
112         }
113         ev->func = func;
114
115         iflag = get_intr_flag();
116         disable_intr();
117
118         if(!evlist || ticks < evlist->dt) {
119                 /* insert at the begining */
120                 ev->next = evlist;
121                 evlist = ev;
122
123                 ev->dt = ticks;
124                 if(ev->next) {
125                         ev->next->dt -= ticks;
126                 }
127         } else {
128                 tsum = evlist->dt;
129                 node = evlist;
130
131                 while(node->next && ticks > tsum + node->next->dt) {
132                         tsum += node->next->dt;
133                         node = node->next;
134                 }
135
136                 ev->next = node->next;
137                 node->next = ev;
138
139                 /* fix the relative times */
140                 ev->dt = ticks - tsum;
141                 if(ev->next) {
142                         ev->next->dt -= ev->dt;
143                 }
144         }
145
146         set_intr_flag(iflag);
147 }
148
149 void cancel_alarm(void (*func)(void))
150 {
151         int iflag;
152         struct timer_event *ev, *node;
153         struct timer_event dummy;
154
155         iflag = get_intr_flag();
156         disable_intr();
157
158         dummy.next = evlist;
159         node = &dummy;
160         while(node->next) {
161                 ev = node->next;
162                 if(ev->func == func) {
163                         /* found it */
164                         if(ev->next) {
165                                 ev->next->dt += ev->dt;
166                         }
167                         node->next = ev->next;
168                         free(ev);
169                         break;
170                 }
171                 node = node->next;
172         }
173
174         set_intr_flag(iflag);
175 }
176
177 static void timer_handler(int inum)
178 {
179         nticks++;
180
181         if(evlist) {
182                 evlist->dt--;
183
184                 while(evlist && evlist->dt <= 0) {
185                         struct timer_event *ev = evlist;
186                         evlist = evlist->next;
187
188                         ev->func();
189                         free(ev);
190                 }
191         }
192 }