-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTIMER.C
129 lines (110 loc) · 3.14 KB
/
TIMER.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Timer handler, calls a user function at the desired PIT clock frequency.
*
* Doesn't "officially" belong to JUDAS but feel free to use! This is
* blasphemy-ware too!
*/
#include <conio.h>
#include <dos.h>
#include "judasmem.h"
#ifdef __DJGPP__
#include <go32.h>
#include <dpmi.h>
#endif
int timer_init(unsigned short frequency, void (*function)());
void timer_uninit(void);
static int timer_lock(void);
static void timer_unlock(void);
unsigned short timer_get_ds(void);
#ifdef __WATCOMC__
void __interrupt __far timer_handler(void);
#else
void timer_handler(void);
#endif
#ifdef __DJGPP__
/* DJGPP version */
inline unsigned short timer_get_ds(void) {
return (unsigned short)_my_ds();
}
#else
/* WATCOM C++ version */
#pragma aux timer_get_ds = \
"mov ax, ds" \
modify [ax] \
value [ax];
#endif
static unsigned char timer_initialized = 0;
#ifdef __DJGPP__
/* DJGPP version
__dpmi_paddr structure contains (unsigned long offset32, unsigned short selector) */
extern __dpmi_paddr timer_oldvect;
static __dpmi_paddr timer_newvect;
#else
/* WATCOM C++ version */
extern void (__interrupt __far *timer_oldvect)();
static void (__interrupt __far *timer_newvect)();
#endif
extern void (*timer_function)();
extern unsigned timer_count;
extern unsigned short timer_frequency;
extern unsigned short timer_systemcount;
extern unsigned short timer_ds;
extern int timer_code_lock_start;
extern int timer_code_lock_end;
int timer_init(unsigned short frequency, void (*function)())
{
if (timer_initialized) return 1;
if (!timer_lock()) return 0;
timer_function = function;
timer_count = 0;
timer_systemcount = 0;
timer_frequency = frequency;
timer_ds = timer_get_ds();
#ifdef __DJGPP__
/* DJGPP version */
timer_newvect.offset32 = (unsigned long) &timer_handler;
timer_newvect.selector = (unsigned short) _my_cs();
__dpmi_get_protected_mode_interrupt_vector(8, &timer_oldvect);
_disable();
__dpmi_set_protected_mode_interrupt_vector(8, &timer_newvect);
#else
/* WATCOM C++ version */
timer_newvect = &timer_handler;
timer_oldvect = _dos_getvect(8);
_disable();
_dos_setvect(8, timer_newvect);
#endif
outp(0x43, 0x34);
outp(0x40, frequency);
outp(0x40, frequency >> 8);
_enable();
timer_initialized = 1;
return 1;
}
void timer_uninit(void)
{
if (!timer_initialized) return;
_disable();
#ifdef __DJGPP__
/* DJGPP version */
__dpmi_set_protected_mode_interrupt_vector(8, &timer_oldvect);
#else
/* WATCOM C++ version */
_dos_setvect(8, timer_oldvect);
#endif
outp(0x43, 0x34);
outp(0x40, 0x00);
outp(0x40, 0x00);
_enable();
timer_unlock();
timer_initialized = 0;
}
static int timer_lock(void)
{
if (!judas_memlock(&timer_code_lock_start, (int)&timer_code_lock_end - (int)&timer_code_lock_start)) return 0;
return 1;
}
static void timer_unlock(void)
{
judas_memunlock(&timer_code_lock_start, (int)&timer_code_lock_end - (int)&timer_code_lock_start);
}