rayzor

annotate src/timer.c @ 9:70e332156d02

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