deepstone

annotate src/timer.c @ 27:dcfe615c4c5f

moved firstp.c to main.c
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 02:47:46 +0300
parents f04884489bad
children
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@21 20 #include <conio.h>
nuclear@0 21 #include <dos.h>
nuclear@21 22 #include <i86.h>
nuclear@0 23 #include "pit8254.h"
nuclear@0 24
nuclear@0 25 #define PIT_TIMER_INTR 8
nuclear@0 26 #define DOS_TIMER_INTR 0x1c
nuclear@0 27
nuclear@0 28 /* macro to divide and round to the nearest integer */
nuclear@0 29 #define DIV_ROUND(a, b) \
nuclear@0 30 ((a) / (b) + ((a) % (b)) / ((b) / 2))
nuclear@0 31
nuclear@0 32 static void set_timer_reload(int reload_val);
nuclear@0 33 static void cleanup(void);
nuclear@21 34 static void __interrupt __far timer_irq();
nuclear@21 35 static void __interrupt __far dos_timer_intr();
nuclear@21 36
nuclear@21 37 static void (__interrupt __far *prev_timer_intr)();
nuclear@0 38
nuclear@0 39 static unsigned long ticks;
nuclear@0 40 static unsigned long tick_interval, ticks_per_dos_intr;
nuclear@0 41 static int inum;
nuclear@0 42
nuclear@0 43 void init_timer(int res_hz)
nuclear@0 44 {
nuclear@21 45 _disable();
nuclear@0 46
nuclear@0 47 if(res_hz > 0) {
nuclear@0 48 int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz);
nuclear@0 49 set_timer_reload(reload_val);
nuclear@0 50
nuclear@0 51 tick_interval = DIV_ROUND(1000, res_hz);
nuclear@0 52 ticks_per_dos_intr = DIV_ROUND(65535L, reload_val);
nuclear@0 53
nuclear@0 54 inum = PIT_TIMER_INTR;
nuclear@21 55 prev_timer_intr = _dos_getvect(inum);
nuclear@21 56 _dos_setvect(inum, timer_irq);
nuclear@0 57 } else {
nuclear@0 58 tick_interval = 55;
nuclear@0 59
nuclear@0 60 inum = DOS_TIMER_INTR;
nuclear@21 61 prev_timer_intr = _dos_getvect(inum);
nuclear@21 62 _dos_setvect(inum, dos_timer_intr);
nuclear@0 63 }
nuclear@21 64 _enable();
nuclear@0 65
nuclear@0 66 atexit(cleanup);
nuclear@0 67 }
nuclear@0 68
nuclear@0 69 static void cleanup(void)
nuclear@0 70 {
nuclear@0 71 if(!prev_timer_intr) {
nuclear@0 72 return; /* init hasn't ran, there's nothing to cleanup */
nuclear@0 73 }
nuclear@0 74
nuclear@21 75 _disable();
nuclear@0 76 if(inum == PIT_TIMER_INTR) {
nuclear@0 77 /* restore the original timer frequency */
nuclear@0 78 set_timer_reload(65535);
nuclear@0 79 }
nuclear@0 80
nuclear@0 81 /* restore the original interrupt handler */
nuclear@21 82 _dos_setvect(inum, prev_timer_intr);
nuclear@21 83 _enable();
nuclear@0 84 }
nuclear@0 85
nuclear@0 86 void reset_timer(void)
nuclear@0 87 {
nuclear@0 88 ticks = 0;
nuclear@0 89 }
nuclear@0 90
nuclear@0 91 unsigned long get_msec(void)
nuclear@0 92 {
nuclear@0 93 return ticks * tick_interval;
nuclear@0 94 }
nuclear@0 95
nuclear@0 96 static void set_timer_reload(int reload_val)
nuclear@0 97 {
nuclear@21 98 outp(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
nuclear@21 99 outp(PORT_DATA0, reload_val & 0xff);
nuclear@21 100 outp(PORT_DATA0, (reload_val >> 8) & 0xff);
nuclear@0 101 }
nuclear@0 102
nuclear@21 103 static void __interrupt __far dos_timer_intr()
nuclear@0 104 {
nuclear@0 105 ticks++;
nuclear@21 106 _chain_intr(prev_timer_intr); /* DOES NOT RETURN */
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 /* first PIC command port */
nuclear@0 110 #define PIC1_CMD 0x20
nuclear@0 111 /* end of interrupt control word */
nuclear@0 112 #define OCW2_EOI (1 << 5)
nuclear@0 113
nuclear@21 114 static void __interrupt __far timer_irq()
nuclear@0 115 {
nuclear@0 116 static unsigned long dos_ticks;
nuclear@0 117
nuclear@0 118 ticks++;
nuclear@0 119
nuclear@0 120 if(++dos_ticks >= ticks_per_dos_intr) {
nuclear@0 121 /* I suppose the dos irq handler does the EOI so I shouldn't
nuclear@0 122 * do it if I am to call the previous function
nuclear@0 123 */
nuclear@0 124 dos_ticks = 0;
nuclear@21 125 _chain_intr(prev_timer_intr); /* XXX DOES NOT RETURN */
nuclear@21 126 return; /* just for clarity */
nuclear@0 127 }
nuclear@21 128
nuclear@21 129 /* send EOI to the PIC */
nuclear@21 130 outp(PIC1_CMD, OCW2_EOI);
nuclear@0 131 }