initial commit
[dosdemo] / src / dos / timer.c
1 /*
2 colcycle - color cycling image viewer
3 Copyright (C) 2016  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 <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <conio.h>
21 #include <dos.h>
22 #include <i86.h>
23 #include "pit8254.h"
24
25 #define PIT_TIMER_INTR  8
26 #define DOS_TIMER_INTR  0x1c
27
28 /* macro to divide and round to the nearest integer */
29 #define DIV_ROUND(a, b) \
30         ((a) / (b) + ((a) % (b)) / ((b) / 2))
31
32 static void set_timer_reload(int reload_val);
33 static void cleanup(void);
34 static void __interrupt __far timer_irq();
35 static void __interrupt __far dos_timer_intr();
36
37 static void (__interrupt __far *prev_timer_intr)();
38
39 static unsigned long ticks;
40 static unsigned long tick_interval, ticks_per_dos_intr;
41 static int inum;
42
43 void init_timer(int res_hz)
44 {
45         _disable();
46
47         if(res_hz > 0) {
48                 int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz);
49                 set_timer_reload(reload_val);
50
51                 tick_interval = DIV_ROUND(1000, res_hz);
52                 ticks_per_dos_intr = DIV_ROUND(65535L, reload_val);
53
54                 inum = PIT_TIMER_INTR;
55                 prev_timer_intr = _dos_getvect(inum);
56                 _dos_setvect(inum, timer_irq);
57         } else {
58                 tick_interval = 55;
59
60                 inum = DOS_TIMER_INTR;
61                 prev_timer_intr = _dos_getvect(inum);
62                 _dos_setvect(inum, dos_timer_intr);
63         }
64         _enable();
65
66         atexit(cleanup);
67 }
68
69 static void cleanup(void)
70 {
71         if(!prev_timer_intr) {
72                 return; /* init hasn't ran, there's nothing to cleanup */
73         }
74
75         _disable();
76         if(inum == PIT_TIMER_INTR) {
77                 /* restore the original timer frequency */
78                 set_timer_reload(65535);
79         }
80
81         /* restore the original interrupt handler */
82         _dos_setvect(inum, prev_timer_intr);
83         _enable();
84 }
85
86 void reset_timer(void)
87 {
88         ticks = 0;
89 }
90
91 unsigned long get_msec(void)
92 {
93         return ticks * tick_interval;
94 }
95
96 static void set_timer_reload(int reload_val)
97 {
98         outp(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
99         outp(PORT_DATA0, reload_val & 0xff);
100         outp(PORT_DATA0, (reload_val >> 8) & 0xff);
101 }
102
103 static void __interrupt __far dos_timer_intr()
104 {
105         ticks++;
106         _chain_intr(prev_timer_intr);   /* DOES NOT RETURN */
107 }
108
109 /* first PIC command port */
110 #define PIC1_CMD        0x20
111 /* end of interrupt control word */
112 #define OCW2_EOI        (1 << 5)
113
114 static void __interrupt __far timer_irq()
115 {
116         static unsigned long dos_ticks;
117
118         ticks++;
119
120         if(++dos_ticks >= ticks_per_dos_intr) {
121                 /* I suppose the dos irq handler does the EOI so I shouldn't
122                  * do it if I am to call the previous function
123                  */
124                 dos_ticks = 0;
125                 _chain_intr(prev_timer_intr);   /* XXX DOES NOT RETURN */
126                 return; /* just for clarity */
127         }
128
129         /* send EOI to the PIC */
130         outp(PIC1_CMD, OCW2_EOI);
131 }