rayzor
view src/timer.c @ 2:c273c6f799a4
added fixcase utility to fix the ftp-ed files' case on linux
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 05 Apr 2014 18:31:00 +0300 |
parents | 2a5340a6eee4 |
children | 5380ff64e83f |
line source
1 /*
2 256-color 3D graphics hack for real-mode DOS.
3 Copyright (C) 2011 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/>.
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"
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;
41 static int inum;
43 void init_timer(int res_hz)
44 {
45 _disable();
47 if(res_hz > 0) {
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);
57 } else {
58 tick_interval = 55;
60 inum = DOS_TIMER_INTR;
61 prev_timer_intr = _dos_getvect(inum);
62 _dos_setvect(inum, dos_timer_intr);
63 }
64 _enable();
66 atexit(cleanup);
67 }
69 static void cleanup(void)
70 {
71 if(!prev_timer_intr) {
72 return; /* init hasn't ran, there's nothing to cleanup */
73 }
75 _disable();
76 if(inum == PIT_TIMER_INTR) {
77 /* restore the original timer frequency */
78 set_timer_reload(65535);
79 }
81 /* restore the original interrupt handler */
82 _dos_setvect(inum, prev_timer_intr);
83 _enable();
84 }
86 void reset_timer(void)
87 {
88 ticks = 0;
89 }
91 unsigned long get_msec(void)
92 {
93 return ticks * tick_interval;
94 }
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 }
103 static void __interrupt __far dos_timer_intr()
104 {
105 ticks++;
106 _chain_intr(prev_timer_intr); /* DOES NOT RETURN */
107 }
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()
115 {
116 static unsigned long dos_ticks;
118 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
123 */
124 dos_ticks = 0;
125 _chain_intr(prev_timer_intr); /* XXX DOES NOT RETURN */
126 return; /* just for clarity */
127 }
129 /* send EOI to the PIC */
130 outp(PIC1_CMD, OCW2_EOI);
131 }