2 colcycle - color cycling image viewer
3 Copyright (C) 2016 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>.
25 #define PIT_TIMER_INTR 8
26 #define DOS_TIMER_INTR 0x1c
28 /* macro to divide and round to the nearest integer */
29 #define DIV_ROUND(a, b) \
30 ((a) / (b) + ((a) % (b)) / ((b) / 2))
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();
37 static void (__interrupt __far *prev_timer_intr)();
39 static unsigned long ticks;
40 static unsigned long tick_interval, ticks_per_dos_intr;
43 void init_timer(int res_hz)
48 int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz);
49 set_timer_reload(reload_val);
51 tick_interval = DIV_ROUND(1000, res_hz);
52 ticks_per_dos_intr = DIV_ROUND(65535L, reload_val);
54 inum = PIT_TIMER_INTR;
55 prev_timer_intr = _dos_getvect(inum);
56 _dos_setvect(inum, timer_irq);
60 inum = DOS_TIMER_INTR;
61 prev_timer_intr = _dos_getvect(inum);
62 _dos_setvect(inum, dos_timer_intr);
69 static void cleanup(void)
71 if(!prev_timer_intr) {
72 return; /* init hasn't ran, there's nothing to cleanup */
76 if(inum == PIT_TIMER_INTR) {
77 /* restore the original timer frequency */
78 set_timer_reload(65535);
81 /* restore the original interrupt handler */
82 _dos_setvect(inum, prev_timer_intr);
86 void reset_timer(void)
91 unsigned long get_msec(void)
93 return ticks * tick_interval;
96 static void set_timer_reload(int reload_val)
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);
103 static void __interrupt __far dos_timer_intr()
106 _chain_intr(prev_timer_intr); /* DOES NOT RETURN */
109 /* first PIC command port */
110 #define PIC1_CMD 0x20
111 /* end of interrupt control word */
112 #define OCW2_EOI (1 << 5)
114 static void __interrupt __far timer_irq()
116 static unsigned long dos_ticks;
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
125 _chain_intr(prev_timer_intr); /* XXX DOES NOT RETURN */
126 return; /* just for clarity */
129 /* send EOI to the PIC */
130 outp(PIC1_CMD, OCW2_EOI);