OSDN Git Service

wwww
[proj16/16.git] / 16 / v2 / source / verge / MAPED / TIMER.C
1 /*\r
2 Copyright (C) 1998 BJ Eirich (aka vecna)\r
3 This program is free software; you can redistribute it and/or\r
4 modify it under the terms of the GNU General Public License\r
5 as published by the Free Software Foundation; either version 2\r
6 of the License, or (at your option) any later version.\r
7 This program is distributed in the hope that it will be useful,\r
8 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
10 See the GNU General Public Lic\r
11 See the GNU General Public License for more details.\r
12 You should have received a copy of the GNU General Public License\r
13 along with this program; if not, write to the Free Software\r
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
15 */\r
16 \r
17 #include <dos.h>\r
18 #include <stdio.h>\r
19 #include <conio.h>\r
20 \r
21 #include "mikmod.h"\r
22 \r
23 #include "render.h"\r
24 \r
25 #define PIT0 0x40\r
26 #define PIT1 0x41\r
27 #define PIT2 0x42\r
28 #define PITMODE 0x43\r
29 #define PITCONST 1193180L\r
30 \r
31 #define OCR1    0x20\r
32 #define IMR1    0x21\r
33 \r
34 #define OCR2    0xA0\r
35 #define IMR2    0xA1\r
36 \r
37 // ================================= Data ====================================\r
38 \r
39 void (__interrupt __far *biosTimerHandler)();\r
40 char timerinstalled=0;\r
41 extern char soundokay;\r
42 \r
43 volatile unsigned int systemtime=0, timer_count=0, tick=0;\r
44 volatile int backupct=0, idlect=0;\r
45 \r
46 // ================================= Code ====================================\r
47 \r
48 #pragma aux disable =\\r
49         "cli";\r
50 \r
51 #pragma aux enable =\\r
52         "sti";\r
53 \r
54 static void SendEOI (unsigned char irqno)\r
55 {\r
56   static unsigned char ocr;\r
57   static unsigned char eoi;\r
58 \r
59   ocr=(irqno>7) ? OCR2 : OCR1;\r
60   eoi=0x60|(irqno&7);\r
61   outp(ocr,eoi);\r
62   if (irqno>7) outp(OCR1,0x20);\r
63 }\r
64 \r
65 void __interrupt __far newhandler(void)\r
66 {\r
67         systemtime++;\r
68         timer_count++;\r
69         tick++;\r
70         backupct++;\r
71         idlect++;\r
72         CheckTileAnimation();\r
73         if (soundokay) MD_Update();\r
74         SendEOI(0);\r
75 }\r
76 \r
77 void SetHZ(unsigned int hz)\r
78 { unsigned int pit0_set, pit0_value;\r
79 \r
80   disable();\r
81 \r
82   outp(PITMODE, 0x34);\r
83   pit0_value=PITCONST / hz;\r
84   pit0_set=(pit0_value & 0x00ff);\r
85   outp(PIT0, pit0_set);\r
86   pit0_set=(pit0_value >> 8);\r
87   outp(PIT0, pit0_set);\r
88 \r
89   enable();\r
90 }\r
91 \r
92 void RestoreHZ()\r
93 {\r
94   disable();\r
95   outp(PITMODE, 0x34);\r
96   outp(PIT0, 0x00);\r
97   outp(PIT0, 0x00);\r
98   enable();\r
99 }\r
100 \r
101 void InitTimer()\r
102 {\r
103   if (timerinstalled) return;\r
104 \r
105   biosTimerHandler=_dos_getvect(0x08);\r
106   _dos_setvect(0x08, newhandler);\r
107   timerinstalled=1;\r
108   SetHZ(100);\r
109 }\r
110 \r
111 void ShutdownTimer()\r
112 {\r
113   if (!timerinstalled) return;\r
114 \r
115   _dos_setvect(0x08, biosTimerHandler);\r
116   RestoreHZ();\r
117   timerinstalled=0;\r
118 }\r