dos3d

annotate src/timer.c @ 14:be61704c4cc8

added initial firstp
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 29 Nov 2011 07:22:49 +0200
parents
children 00d84ab1ef26
rev   line source
nuclear@0 1 /*
nuclear@0 2 256-color 3D graphics hack for real-mode DOS.
nuclear@0 3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@0 4
nuclear@0 5 This program is free software: you can redistribute it and/or modify
nuclear@0 6 it under the terms of the GNU General Public License as published by
nuclear@0 7 the Free Software Foundation, either version 3 of the License, or
nuclear@0 8 (at your option) any later version.
nuclear@0 9
nuclear@0 10 This program is distributed in the hope that it will be useful,
nuclear@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 13 GNU General Public License for more details.
nuclear@0 14
nuclear@0 15 You should have received a copy of the GNU General Public License
nuclear@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@0 17 */
nuclear@0 18 #include <stdio.h>
nuclear@0 19 #include <stdlib.h>
nuclear@0 20 #include <dos.h>
nuclear@0 21 #include "pit8254.h"
nuclear@0 22
nuclear@0 23 #define PIT_TIMER_INTR 8
nuclear@0 24 #define DOS_TIMER_INTR 0x1c
nuclear@0 25
nuclear@0 26 /* macro to divide and round to the nearest integer */
nuclear@0 27 #define DIV_ROUND(a, b) \
nuclear@0 28 ((a) / (b) + ((a) % (b)) / ((b) / 2))
nuclear@0 29
nuclear@0 30 static void set_timer_reload(int reload_val);
nuclear@0 31 static void cleanup(void);
nuclear@0 32 static void interrupt dos_timer_intr();
nuclear@0 33 static void interrupt timer_irq();
nuclear@0 34 static void interrupt (*prev_timer_intr)();
nuclear@0 35
nuclear@0 36 static unsigned long ticks;
nuclear@0 37 static unsigned long tick_interval, ticks_per_dos_intr;
nuclear@0 38 static int inum;
nuclear@0 39
nuclear@0 40 void init_timer(int res_hz)
nuclear@0 41 {
nuclear@0 42
nuclear@0 43 disable();
nuclear@0 44 if(res_hz > 0) {
nuclear@0 45 int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz);
nuclear@0 46 set_timer_reload(reload_val);
nuclear@0 47
nuclear@0 48 tick_interval = DIV_ROUND(1000, res_hz);
nuclear@0 49 ticks_per_dos_intr = DIV_ROUND(65535L, reload_val);
nuclear@0 50
nuclear@0 51 inum = PIT_TIMER_INTR;
nuclear@0 52 prev_timer_intr = getvect(inum);
nuclear@0 53 setvect(inum, timer_irq);
nuclear@0 54 } else {
nuclear@0 55 tick_interval = 55;
nuclear@0 56
nuclear@0 57 inum = DOS_TIMER_INTR;
nuclear@0 58 prev_timer_intr = getvect(inum);
nuclear@0 59 setvect(inum, dos_timer_intr);
nuclear@0 60 }
nuclear@0 61 enable();
nuclear@0 62
nuclear@0 63 atexit(cleanup);
nuclear@0 64 }
nuclear@0 65
nuclear@0 66 static void cleanup(void)
nuclear@0 67 {
nuclear@0 68 if(!prev_timer_intr) {
nuclear@0 69 return; /* init hasn't ran, there's nothing to cleanup */
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 disable();
nuclear@0 73 if(inum == PIT_TIMER_INTR) {
nuclear@0 74 /* restore the original timer frequency */
nuclear@0 75 set_timer_reload(65535);
nuclear@0 76 }
nuclear@0 77
nuclear@0 78 /* restore the original interrupt handler */
nuclear@0 79 setvect(inum, prev_timer_intr);
nuclear@0 80 enable();
nuclear@0 81 }
nuclear@0 82
nuclear@0 83 void reset_timer(void)
nuclear@0 84 {
nuclear@0 85 ticks = 0;
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 unsigned long get_msec(void)
nuclear@0 89 {
nuclear@0 90 return ticks * tick_interval;
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 static void set_timer_reload(int reload_val)
nuclear@0 94 {
nuclear@0 95 outportb(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
nuclear@0 96 outportb(PORT_DATA0, reload_val & 0xff);
nuclear@0 97 outportb(PORT_DATA0, (reload_val >> 8) & 0xff);
nuclear@0 98 }
nuclear@0 99
nuclear@0 100 static void interrupt dos_timer_intr()
nuclear@0 101 {
nuclear@0 102 ticks++;
nuclear@0 103 prev_timer_intr();
nuclear@0 104 }
nuclear@0 105
nuclear@0 106 /* first PIC command port */
nuclear@0 107 #define PIC1_CMD 0x20
nuclear@0 108 /* end of interrupt control word */
nuclear@0 109 #define OCW2_EOI (1 << 5)
nuclear@0 110
nuclear@0 111 static void interrupt timer_irq()
nuclear@0 112 {
nuclear@0 113 static unsigned long dos_ticks;
nuclear@0 114
nuclear@0 115 ticks++;
nuclear@0 116
nuclear@0 117 if(++dos_ticks >= ticks_per_dos_intr) {
nuclear@0 118 /* I suppose the dos irq handler does the EOI so I shouldn't
nuclear@0 119 * do it if I am to call the previous function
nuclear@0 120 */
nuclear@0 121 prev_timer_intr();
nuclear@0 122 dos_ticks = 0;
nuclear@0 123 } else {
nuclear@0 124 /* send EOI to the PIC */
nuclear@0 125 outportb(PIC1_CMD, OCW2_EOI);
nuclear@0 126 }
nuclear@0 127 }