deepstone

view src/timer.c @ 0:f04884489bad

dos3d initial import
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 06:14:01 +0200
parents
children 00d84ab1ef26
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 <dos.h>
21 #include "pit8254.h"
23 #define PIT_TIMER_INTR 8
24 #define DOS_TIMER_INTR 0x1c
26 /* macro to divide and round to the nearest integer */
27 #define DIV_ROUND(a, b) \
28 ((a) / (b) + ((a) % (b)) / ((b) / 2))
30 static void set_timer_reload(int reload_val);
31 static void cleanup(void);
32 static void interrupt dos_timer_intr();
33 static void interrupt timer_irq();
34 static void interrupt (*prev_timer_intr)();
36 static unsigned long ticks;
37 static unsigned long tick_interval, ticks_per_dos_intr;
38 static int inum;
40 void init_timer(int res_hz)
41 {
43 disable();
44 if(res_hz > 0) {
45 int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz);
46 set_timer_reload(reload_val);
48 tick_interval = DIV_ROUND(1000, res_hz);
49 ticks_per_dos_intr = DIV_ROUND(65535L, reload_val);
51 inum = PIT_TIMER_INTR;
52 prev_timer_intr = getvect(inum);
53 setvect(inum, timer_irq);
54 } else {
55 tick_interval = 55;
57 inum = DOS_TIMER_INTR;
58 prev_timer_intr = getvect(inum);
59 setvect(inum, dos_timer_intr);
60 }
61 enable();
63 atexit(cleanup);
64 }
66 static void cleanup(void)
67 {
68 if(!prev_timer_intr) {
69 return; /* init hasn't ran, there's nothing to cleanup */
70 }
72 disable();
73 if(inum == PIT_TIMER_INTR) {
74 /* restore the original timer frequency */
75 set_timer_reload(65535);
76 }
78 /* restore the original interrupt handler */
79 setvect(inum, prev_timer_intr);
80 enable();
81 }
83 void reset_timer(void)
84 {
85 ticks = 0;
86 }
88 unsigned long get_msec(void)
89 {
90 return ticks * tick_interval;
91 }
93 static void set_timer_reload(int reload_val)
94 {
95 outportb(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
96 outportb(PORT_DATA0, reload_val & 0xff);
97 outportb(PORT_DATA0, (reload_val >> 8) & 0xff);
98 }
100 static void interrupt dos_timer_intr()
101 {
102 ticks++;
103 prev_timer_intr();
104 }
106 /* first PIC command port */
107 #define PIC1_CMD 0x20
108 /* end of interrupt control word */
109 #define OCW2_EOI (1 << 5)
111 static void interrupt timer_irq()
112 {
113 static unsigned long dos_ticks;
115 ticks++;
117 if(++dos_ticks >= ticks_per_dos_intr) {
118 /* I suppose the dos irq handler does the EOI so I shouldn't
119 * do it if I am to call the previous function
120 */
121 prev_timer_intr();
122 dos_ticks = 0;
123 } else {
124 /* send EOI to the PIC */
125 outportb(PIC1_CMD, OCW2_EOI);
126 }
127 }