deepstone
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/timer.c Mon Nov 21 06:14:01 2011 +0200 1.3 @@ -0,0 +1,127 @@ 1.4 +/* 1.5 +256-color 3D graphics hack for real-mode DOS. 1.6 +Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#include <stdio.h> 1.22 +#include <stdlib.h> 1.23 +#include <dos.h> 1.24 +#include "pit8254.h" 1.25 + 1.26 +#define PIT_TIMER_INTR 8 1.27 +#define DOS_TIMER_INTR 0x1c 1.28 + 1.29 +/* macro to divide and round to the nearest integer */ 1.30 +#define DIV_ROUND(a, b) \ 1.31 + ((a) / (b) + ((a) % (b)) / ((b) / 2)) 1.32 + 1.33 +static void set_timer_reload(int reload_val); 1.34 +static void cleanup(void); 1.35 +static void interrupt dos_timer_intr(); 1.36 +static void interrupt timer_irq(); 1.37 +static void interrupt (*prev_timer_intr)(); 1.38 + 1.39 +static unsigned long ticks; 1.40 +static unsigned long tick_interval, ticks_per_dos_intr; 1.41 +static int inum; 1.42 + 1.43 +void init_timer(int res_hz) 1.44 +{ 1.45 + 1.46 + disable(); 1.47 + if(res_hz > 0) { 1.48 + int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz); 1.49 + set_timer_reload(reload_val); 1.50 + 1.51 + tick_interval = DIV_ROUND(1000, res_hz); 1.52 + ticks_per_dos_intr = DIV_ROUND(65535L, reload_val); 1.53 + 1.54 + inum = PIT_TIMER_INTR; 1.55 + prev_timer_intr = getvect(inum); 1.56 + setvect(inum, timer_irq); 1.57 + } else { 1.58 + tick_interval = 55; 1.59 + 1.60 + inum = DOS_TIMER_INTR; 1.61 + prev_timer_intr = getvect(inum); 1.62 + setvect(inum, dos_timer_intr); 1.63 + } 1.64 + enable(); 1.65 + 1.66 + atexit(cleanup); 1.67 +} 1.68 + 1.69 +static void cleanup(void) 1.70 +{ 1.71 + if(!prev_timer_intr) { 1.72 + return; /* init hasn't ran, there's nothing to cleanup */ 1.73 + } 1.74 + 1.75 + disable(); 1.76 + if(inum == PIT_TIMER_INTR) { 1.77 + /* restore the original timer frequency */ 1.78 + set_timer_reload(65535); 1.79 + } 1.80 + 1.81 + /* restore the original interrupt handler */ 1.82 + setvect(inum, prev_timer_intr); 1.83 + enable(); 1.84 +} 1.85 + 1.86 +void reset_timer(void) 1.87 +{ 1.88 + ticks = 0; 1.89 +} 1.90 + 1.91 +unsigned long get_msec(void) 1.92 +{ 1.93 + return ticks * tick_interval; 1.94 +} 1.95 + 1.96 +static void set_timer_reload(int reload_val) 1.97 +{ 1.98 + outportb(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE); 1.99 + outportb(PORT_DATA0, reload_val & 0xff); 1.100 + outportb(PORT_DATA0, (reload_val >> 8) & 0xff); 1.101 +} 1.102 + 1.103 +static void interrupt dos_timer_intr() 1.104 +{ 1.105 + ticks++; 1.106 + prev_timer_intr(); 1.107 +} 1.108 + 1.109 +/* first PIC command port */ 1.110 +#define PIC1_CMD 0x20 1.111 +/* end of interrupt control word */ 1.112 +#define OCW2_EOI (1 << 5) 1.113 + 1.114 +static void interrupt timer_irq() 1.115 +{ 1.116 + static unsigned long dos_ticks; 1.117 + 1.118 + ticks++; 1.119 + 1.120 + if(++dos_ticks >= ticks_per_dos_intr) { 1.121 + /* I suppose the dos irq handler does the EOI so I shouldn't 1.122 + * do it if I am to call the previous function 1.123 + */ 1.124 + prev_timer_intr(); 1.125 + dos_ticks = 0; 1.126 + } else { 1.127 + /* send EOI to the PIC */ 1.128 + outportb(PIC1_CMD, OCW2_EOI); 1.129 + } 1.130 +}