census logo
[bootcensus] / src / timer.c
1 /*
2 256boss - bootable launcher for 256b intros
3 Copyright (C) 2018-2019  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
59 struct timer_event {
60         int dt; /* remaining ticks delta from the previous event */
61         void (*func)(void);
62         struct timer_event *next;
63 };
64
65 static void timer_handler(int inum);
66
67 static struct timer_event *evlist;
68
69
70 void init_timer(void)
71 {
72         /* calculate the reload count: round(osc / freq) */
73         int reload_count = DIV_ROUND(OSC_FREQ_HZ, TICK_FREQ_HZ);
74
75         /* set the mode to square wave for channel 0, both low
76          * and high reload count bytes will follow...
77          */
78         outb(CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE, PORT_CMD);
79
80         /* write the low and high bytes of the reload count to the
81          * port for channel 0
82          */
83         outb(reload_count & 0xff, PORT_DATA0);
84         outb((reload_count >> 8) & 0xff, PORT_DATA0);
85
86         /* set the timer interrupt handler */
87         interrupt(IRQ_TO_INTR(0), timer_handler);
88 }
89
90 void set_alarm(unsigned long msec, void (*func)(void))
91 {
92         int ticks, tsum, iflag;
93         struct timer_event *ev, *node;
94
95         if((ticks = MSEC_TO_TICKS(msec)) <= 0) {
96                 return;
97         }
98
99         if(!(ev = malloc(sizeof *ev))) {
100                 panic("failed to allocate timer event");
101                 return;
102         }
103         ev->func = func;
104
105         iflag = get_intr_flag();
106         disable_intr();
107
108         if(!evlist || ticks < evlist->dt) {
109                 /* insert at the begining */
110                 ev->next = evlist;
111                 evlist = ev;
112
113                 ev->dt = ticks;
114                 if(ev->next) {
115                         ev->next->dt -= ticks;
116                 }
117         } else {
118                 tsum = evlist->dt;
119                 node = evlist;
120
121                 while(node->next && ticks > tsum + node->next->dt) {
122                         tsum += node->next->dt;
123                         node = node->next;
124                 }
125
126                 ev->next = node->next;
127                 node->next = ev;
128
129                 /* fix the relative times */
130                 ev->dt = ticks - tsum;
131                 if(ev->next) {
132                         ev->next->dt -= ev->dt;
133                 }
134         }
135
136         set_intr_flag(iflag);
137 }
138
139 void cancel_alarm(void (*func)(void))
140 {
141         int iflag;
142         struct timer_event *ev, *node;
143         struct timer_event dummy;
144
145         iflag = get_intr_flag();
146         disable_intr();
147
148         dummy.next = evlist;
149         node = &dummy;
150         while(node->next) {
151                 ev = node->next;
152                 if(ev->func == func) {
153                         /* found it */
154                         if(ev->next) {
155                                 ev->next->dt += ev->dt;
156                         }
157                         node->next = ev->next;
158                         free(ev);
159                         break;
160                 }
161                 node = node->next;
162         }
163
164         set_intr_flag(iflag);
165 }
166
167 static void timer_handler(int inum)
168 {
169         nticks++;
170
171         if(evlist) {
172                 evlist->dt--;
173
174                 while(evlist && evlist->dt <= 0) {
175                         struct timer_event *ev = evlist;
176                         evlist = evlist->next;
177
178                         ev->func();
179                         free(ev);
180                 }
181         }
182 }