-
Notifications
You must be signed in to change notification settings - Fork 43
/
CTRLIB.PAS
439 lines (359 loc) · 9.94 KB
/
CTRLIB.PAS
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
{ Catacomb Source Code
Copyright (C) 1993-2014 Flat Rock Software
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
}
Unit CtrLib;
interface
Uses
dos;
type
dirtype= (north,east,south,west
,northeast,southeast,southwest,northwest,nodir);
Var
oldint9: pointer;
{The new int9 handler set and clears the keydown table}
KeyDown: array [0..127] of boolean;
{Joystick value boundaries}
joy_xlow,joy_xhigh,joy_ylow,joy_yhigh: word;
{Mouse minimum movements}
mou_sensitivity: word;
{Keyboard scan codes}
key_North,key_NorthEast,key_East,key_SouthEast,key_South
,key_SouthWest,key_West,key_NorthWest,key_B1,key_B2: byte;
Procedure Rd_Joy (num:integer; var x,y:integer);
Procedure Rd_Joystick1 (var dir:dirtype; var button1,button2:boolean);
Procedure Rd_Joystick2 (var dir:dirtype; var button1,button2:boolean);
Procedure Rd_Mouse (var dir:dirtype; var button1,button2:boolean);
Procedure Rd_Keyboard (var dir:dirtype; var button1,button2:boolean);
Procedure ConnectKBD;
Procedure DisconnectKBD;
{==========================================================================}
implementation
{===================================}
{ }
{ Rd_Joy }
{ Read the values of the joystick }
{ pots. Will vary with clock speed }
{ }
{===================================}
Procedure Rd_Joy (num:integer; var x,y:integer);
{ num should be 1 or 2 }
var
i,p1,p2:byte;
b1,b2: word;
Begin
if num=1 then
Begin
p1:=1;
p2:=2;
end
else
Begin
p1:=4;
p2:=8;
end;
x:=0;
y:=0;
inline ($FA); {clear ints}
port[$201]:=port[$201];
Repeat
i:=port[$201] and 3;
b1:=i and p1;
b2:=(i and p2) shr 1;
x:=x+b1;
y:=y+b2;
Until (b1+b2=0) or (x>500) or (y>500);
inline ($FB); {set ints}
end;
{===================================================}
{ }
{ Rd_Joystick1/2 }
{ Reads the joystick buttons and converts positions }
{ into a direction based on Joy_### boundaries }
{ }
{===================================================}
Procedure Rd_Joystick1 (var dir:dirtype; var button1,button2:boolean);
Var
i,joyx,joyy,xmove,ymove:integer;
Begin
{get button status}
i:=port[$201];
if (i and $10)>0 then
button1:=false
else
button1:=true;
if (i and $20)>0 then
button2:=false
else
button2:=true;
Rd_Joy (1,joyx,joyy);
{set xmove / ymove if the joyx/y is greater than boundary values}
if joyx< joy_xlow then
xmove:=-1
else
if joyx> joy_xhigh then
xmove:=1
else
xmove:=0;
if joyy< joy_ylow then
ymove:=-1
else
if joyy> joy_yhigh then
ymove:=1
else
ymove:=0;
{decide direction from xmove / ymove}
case ymove*3+xmove of
-4: dir:=NorthWest;
-3: dir:=North;
-2: dir:=NorthEast;
-1: dir:=West;
0: dir:=nodir;
1: dir:=East;
2: dir:=SouthWest;
3: dir:=South;
4: dir:=SouthEast;
end;
End;
Procedure Rd_Joystick2 (var dir:dirtype; var button1,button2:boolean);
Var
i,joyx,joyy,xmove,ymove:integer;
Begin
{get button status}
i:=port[$201];
if (i and $40)>0 then
button1:=false
else
button1:=true;
if (i and $80)>0 then
button2:=false
else
button2:=true;
Rd_Joy (2,joyx,joyy);
{set xmove / ymove if the joyx/y is greater than boundary values}
if joyx< joy_xlow then
xmove:=-1
else
if joyx> joy_xhigh then
xmove:=1
else
xmove:=0;
if joyy< joy_ylow then
ymove:=-1
else
if joyy> joy_yhigh then
ymove:=1
else
ymove:=0;
{decide direction from xmove / ymove}
case ymove*3+xmove of
-4: dir:=NorthWest;
-3: dir:=North;
-2: dir:=NorthEast;
-1: dir:=West;
0: dir:=nodir;
1: dir:=East;
2: dir:=SouthWest;
3: dir:=South;
4: dir:=SouthEast;
end;
End;
{===================================================}
{ }
{ Rd_Mouse }
{ Returns the movement direction of the mouse since }
{ the last call, and the button status. }
{ }
{===================================================}
Procedure Rd_Mouse (var dir:dirtype; var button1,button2:boolean);
var
regs: registers;
deltax,deltay,xmove,ymove:integer;
Begin
{read mouse status}
regs.ax:=3;
intr($33,regs); {mouse status}
button1:=(regs.bx and 1=1);
button2:=(regs.bx and 2=2);
regs.ax:=11;
intr($33,regs); {mouse motion counters}
deltax:= integer(regs.cx);
deltay:= integer(regs.dx);
{set xmove / ymove if the delta is greater than boundary values}
if deltax> mou_sensitivity then
xmove:=1
else
if deltax< -mou_sensitivity then
xmove:=-1
else
xmove:=0;
if deltay> mou_sensitivity then
ymove:=1
else
if deltay< -mou_sensitivity then
ymove:=-1
else
ymove:=0;
{decide direction from xmove / ymove}
case ymove*3+xmove of
-4: dir:=NorthWest;
-3: dir:=North;
-2: dir:=NorthEast;
-1: dir:=West;
0: dir:=nodir;
1: dir:=East;
2: dir:=SouthWest;
3: dir:=South;
4: dir:=SouthEast;
end;
{reset mouse to center of screen}
regs.cx:=320;
regs.dx:=100;
regs.ax:=4;
intr($33,regs); {setmouse}
End;
{=====================================================}
{ }
{ CtrlInt9 }
{ An interrupt routine that intercepts ALL scan codes }
{ }
{=====================================================}
Procedure CtrlInt9;
interrupt;
type
proc = procedure;
var
key: byte;
Begin
port[$20]:=$20; {tell the interrupt manager we got it}
key:=Port[$60]; {get the scan code that caused the interrupt}
If key>127 then
KeyDown [key-128]:=false {break code, key was released}
else
begin
keydown [key]:=true; {make code, key was just pressed}
MemW[$40:$1C]:=MemW[$40:$1A]; {clear the BIOS keyboard buffer}
end;
Proc (OldInt9); {give it to the BIOS}
End;
{==============================================}
{ }
{ ConnectKBD }
{ If the KBD handler isn't active, this clears }
{ the KeyDown table and sets INT 9 to point to }
{ CtrlInt9 }
{ }
{==============================================}
Procedure ConnectKBD;
Var
i:integer;
Begin
for i:=0 to 127 do
KeyDown [i]:=false;
If OldInt9=nil then
Begin
GetIntVec (9,OldInt9);
SetIntVec (9,@CtrlInt9);
End;
end;
{============================================}
{ }
{ DisconnectKBD }
{ If the KBD handler is active, it gets shut }
{ down and the BIOS is given control. }
{ }
{============================================}
Procedure DisconnectKBD;
Begin
If OldInt9<>nil then
Begin
SetIntVec (9,OldInt9);
oldint9:=nil;
end;
End;
{=================================================}
{ }
{ Rd_Keyboard }
{ Checks the keyflags set by CtrlInt9 and decides }
{ on direction and button states }
{ }
{=================================================}
Procedure Rd_Keyboard (var dir:dirtype; var button1,button2:boolean);
Var
xmove,ymove: integer;
Begin
Button1:=KeyDown [Key_B1];
Button2:=KeyDown [Key_B2];
xmove:=0;
ymove:=0;
If KeyDown [Key_North] then
ymove:=-1;
If KeyDown [Key_East] then
xmove:=1;
If KeyDown [Key_South] then
ymove:=1;
If KeyDown [Key_West] then
xmove:=-1;
If KeyDown [Key_NorthWest] then
Begin
xmove:=-1;
ymove:=-1;
end;
If KeyDown [Key_NorthEast] then
Begin
xmove:=1;
ymove:=-1;
end;
If KeyDown [Key_SouthWest] then
Begin
xmove:=-1;
ymove:=1;
end;
If KeyDown [Key_SouthEast] then
Begin
xmove:=1;
ymove:=1;
end;
{decide direction from xmove / ymove}
case ymove*3+xmove of
-4: dir:=NorthWest;
-3: dir:=North;
-2: dir:=NorthEast;
-1: dir:=West;
0: dir:=nodir;
1: dir:=East;
2: dir:=SouthWest;
3: dir:=South;
4: dir:=SouthEast;
end;
End;
{===========================================================================}
Begin
joy_xlow:=5;
joy_ylow:=5;
joy_xhigh:=20;
joy_yhigh:=20;
mou_sensitivity:=5;
key_north:=$48;
key_northeast:=$49;
key_east:=$4D;
key_southeast:=$51;
key_south:=$50;
key_southwest:=$4f;
key_west:=$4B;
key_northwest:=$47;
key_b1:=$1D;
key_b2:=$38;
oldint9:=nil;
end.