eobish

annotate src/dos/timer.c @ 7:6a350c554e46

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