This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
i_ibm.c
1488 lines (1266 loc) · 29.2 KB
/
i_ibm.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (C) 1993-1996 Id Software, Inc.
// Copyright (C) 1993-2008 Raven Software
// Copyright (C) 2016-2017 Alexey Khokholov (Nuke.YKT)
//
// 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.
//
// DESCRIPTION:
// IBM DOS VGA graphics and key/mouse/joystick.
//
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <graph.h>
#include "d_main.h"
#include "doomstat.h"
#include "doomdef.h"
#include "r_local.h"
#include "sounds.h"
#include "i_system.h"
#include "i_sound.h"
#include "g_game.h"
#include "m_misc.h"
#include "v_video.h"
#include "w_wad.h"
#include "z_zone.h"
#include "dpmiapi.h"
//
// Macros
//
#define DPMI_INT 0x31
//#define NOKBD
//#define NOTIMER
//
// Code
//
void I_StartupNet(void);
void I_ShutdownNet(void);
void I_ReadExternDriver(void);
typedef struct
{
unsigned edi, esi, ebp, reserved, ebx, edx, ecx, eax;
unsigned short flags, es, ds, fs, gs, ip, cs, sp, ss;
} dpmiregs_t;
extern dpmiregs_t dpmiregs;
void I_ReadMouse(void);
void I_InitDiskFlash(void);
extern int usemouse, usejoystick;
//
// Constants
//
#define SC_INDEX 0x3C4
#define SC_RESET 0
#define SC_CLOCK 1
#define SC_MAPMASK 2
#define SC_CHARMAP 3
#define SC_MEMMODE 4
#define CRTC_INDEX 0x3D4
#define CRTC_H_TOTAL 0
#define CRTC_H_DISPEND 1
#define CRTC_H_BLANK 2
#define CRTC_H_ENDBLANK 3
#define CRTC_H_RETRACE 4
#define CRTC_H_ENDRETRACE 5
#define CRTC_V_TOTAL 6
#define CRTC_OVERFLOW 7
#define CRTC_ROWSCAN 8
#define CRTC_MAXSCANLINE 9
#define CRTC_CURSORSTART 10
#define CRTC_CURSOREND 11
#define CRTC_STARTHIGH 12
#define CRTC_STARTLOW 13
#define CRTC_CURSORHIGH 14
#define CRTC_CURSORLOW 15
#define CRTC_V_RETRACE 16
#define CRTC_V_ENDRETRACE 17
#define CRTC_V_DISPEND 18
#define CRTC_OFFSET 19
#define CRTC_UNDERLINE 20
#define CRTC_V_BLANK 21
#define CRTC_V_ENDBLANK 22
#define CRTC_MODE 23
#define CRTC_LINECOMPARE 24
#define GC_INDEX 0x3CE
#define GC_SETRESET 0
#define GC_ENABLESETRESET 1
#define GC_COLORCOMPARE 2
#define GC_DATAROTATE 3
#define GC_READMAP 4
#define GC_MODE 5
#define GC_MISCELLANEOUS 6
#define GC_COLORDONTCARE 7
#define GC_BITMASK 8
#define ATR_INDEX 0x3c0
#define ATR_MODE 16
#define ATR_OVERSCAN 17
#define ATR_COLORPLANEENABLE 18
#define ATR_PELPAN 19
#define ATR_COLORSELECT 20
#define STATUS_REGISTER_1 0x3da
#define PEL_WRITE_ADR 0x3c8
#define PEL_READ_ADR 0x3c7
#define PEL_DATA 0x3c9
#define PEL_MASK 0x3c6
boolean grmode;
//
// joystick vars
//
boolean joystickpresent;
extern unsigned joystickx, joysticky;
boolean I_ReadJoystick(void); // returns false if not connected
#define VBLCOUNTER 34000 // hardware tics to a frame
#define TIMERINT 8
#define KEYBOARDINT 9
#define CRTCOFF (_inbyte(STATUS_REGISTER_1)&1)
#define CLI _disable()
#define STI _enable()
#define _outbyte(x,y) (outp(x,y))
#define _outhword(x,y) (outpw(x,y))
#define _inbyte(x) (inp(x))
#define _inhword(x) (inpw(x))
#define MOUSEB1 1
#define MOUSEB2 2
#define MOUSEB3 4
boolean mousepresent;
int ticcount;
// REGS stuff used for int calls
union REGS regs;
struct SREGS segregs;
boolean novideo; // if true, stay in text mode for debugging
#define KBDQUESIZE 32
byte keyboardque[KBDQUESIZE];
int kbdtail, kbdhead;
#define KEY_LSHIFT 0xfe
#define KEY_INS (0x80+0x52)
#define KEY_DEL (0x80+0x53)
#define KEY_PGUP (0x80+0x49)
#define KEY_PGDN (0x80+0x51)
#define KEY_HOME (0x80+0x47)
#define KEY_END (0x80+0x4f)
#define SC_RSHIFT 0x36
#define SC_LSHIFT 0x2a
void DPMIInt(int i);
void I_WaitVBL(int vbls);
void I_StartupCyberMan(void);
void I_StartupSound(void);
void I_ShutdownSound(void);
void I_ShutdownTimer(void);
byte scantokey[128] =
{
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0 , 27, '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', KEY_BACKSPACE, 9, // 0
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
'o', 'p', '[', ']', 13 , KEY_RCTRL,'a', 's', // 1
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
39 , '`', KEY_LSHIFT,92, 'z', 'x', 'c', 'v', // 2
'b', 'n', 'm', ',', '.', '/', KEY_RSHIFT,'*',
KEY_RALT,' ', 0 , KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, // 3
KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10,0 , 0 , KEY_HOME,
KEY_UPARROW,KEY_PGUP,'-',KEY_LEFTARROW,'5',KEY_RIGHTARROW,'+',KEY_END, //4
KEY_DOWNARROW,KEY_PGDN,KEY_INS,KEY_DEL,0,0, 0, KEY_F11,
KEY_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
};
typedef struct
{
int irq;
ticcmd_t cmd;
} extapi_t;
extapi_t *extcontrol;
ticcmd_t emptycmd;
ticcmd_t* I_BaseTiccmd(void)
{
if (!extcontrol)
{
return &emptycmd;
}
DPMIInt(extcontrol->irq);
return &extcontrol->cmd;
}
//
// I_GetTime
// Returns time in 1/35th second tics.
//
int I_GetTime(void)
{
return ticcount;
}
//
// I_ColorBorder
//
void I_ColorBorder(void)
{
int i;
I_WaitVBL(1);
_outbyte(PEL_WRITE_ADR, 0);
for (i = 0; i < 3; i++)
{
_outbyte(PEL_DATA, 63);
}
}
//
// I_UnColorBorder
//
void I_UnColorBorder(void)
{
int i;
I_WaitVBL(1);
_outbyte(PEL_WRITE_ADR, 0);
for (i = 0; i < 3; i++)
{
_outbyte(PEL_DATA, 0);
}
}
//
// User input
//
//
// I_WaitVBL
//
void I_WaitVBL(int vbls)
{
int stat;
if (novideo)
{
return;
}
while (vbls--)
{
do
{
stat = inp(STATUS_REGISTER_1);
if (stat & 8)
{
break;
}
} while (1);
do
{
stat = inp(STATUS_REGISTER_1);
if ((stat & 8) == 0)
{
break;
}
} while (1);
}
}
//
// I_SetPalette
// Palette source must use 8 bit RGB elements.
//
void I_SetPalette(byte *palette)
{
int i;
if(novideo)
{
return;
}
I_WaitVBL(1);
_outbyte(PEL_WRITE_ADR, 0);
for(i = 0; i < 768; i++)
{
_outbyte(PEL_DATA, (gammatable[usegamma][*palette++])>>2);
}
}
//
// Graphics mode
//
byte *pcscreen, *currentscreen, *destscreen, *destview;
//
// I_UpdateBox
//
void I_UpdateBox(int x, int y, int w, int h)
{
int i, j, k, count;
int sp_x1, sp_x2;
int poffset;
int offset;
int pstep;
int step;
byte *dest, *source;
if (x < 0 || y < 0 || w <= 0 || h <= 0
|| x + w > SCREENWIDTH || y + h > SCREENHEIGHT)
{
I_Error("Bad I_UpdateBox (%i, %i, %i, %i)", x, y, w, h);
}
sp_x1 = x / 8;
sp_x2 = (x + w) / 8;
count = sp_x2 - sp_x1 + 1;
offset = y * SCREENWIDTH + sp_x1 * 8;
step = SCREENWIDTH - count * 8;
poffset = offset / 4;
pstep = step / 4;
outp(SC_INDEX, SC_MAPMASK);
for (i = 0; i < 4; i++)
{
outp(SC_INDEX + 1, 1 << i);
source = &screens[0][offset + i];
dest = destscreen + poffset;
for (j = 0; j < h; j++)
{
k = count;
while (k--)
{
*(unsigned short *)dest = (unsigned short)(((*(source + 4)) << 8) + (*source));
dest += 2;
source += 8;
}
source += step;
dest += pstep;
}
}
}
//
// I_UpdateNoBlit
//
int olddb[2][4];
void I_UpdateNoBlit(void)
{
int realdr[4];
int x, y, w, h;
// Set current screen
currentscreen = destscreen;
// Update dirtybox size
realdr[BOXTOP] = dirtybox[BOXTOP];
if (realdr[BOXTOP] < olddb[0][BOXTOP])
{
realdr[BOXTOP] = olddb[0][BOXTOP];
}
if (realdr[BOXTOP] < olddb[1][BOXTOP])
{
realdr[BOXTOP] = olddb[1][BOXTOP];
}
realdr[BOXRIGHT] = dirtybox[BOXRIGHT];
if (realdr[BOXRIGHT] < olddb[0][BOXRIGHT])
{
realdr[BOXRIGHT] = olddb[0][BOXRIGHT];
}
if (realdr[BOXRIGHT] < olddb[1][BOXRIGHT])
{
realdr[BOXRIGHT] = olddb[1][BOXRIGHT];
}
realdr[BOXBOTTOM] = dirtybox[BOXBOTTOM];
if (realdr[BOXBOTTOM] > olddb[0][BOXBOTTOM])
{
realdr[BOXBOTTOM] = olddb[0][BOXBOTTOM];
}
if (realdr[BOXBOTTOM] > olddb[1][BOXBOTTOM])
{
realdr[BOXBOTTOM] = olddb[1][BOXBOTTOM];
}
realdr[BOXLEFT] = dirtybox[BOXLEFT];
if (realdr[BOXLEFT] > olddb[0][BOXLEFT])
{
realdr[BOXLEFT] = olddb[0][BOXLEFT];
}
if (realdr[BOXLEFT] > olddb[1][BOXLEFT])
{
realdr[BOXLEFT] = olddb[1][BOXLEFT];
}
// Leave current box for next update
memcpy(olddb[0], olddb[1], 16);
memcpy(olddb[1], dirtybox, 16);
// Update screen
if (realdr[BOXBOTTOM] <= realdr[BOXTOP])
{
x = realdr[BOXLEFT];
y = realdr[BOXBOTTOM];
w = realdr[BOXRIGHT] - realdr[BOXLEFT] + 1;
h = realdr[BOXTOP] - realdr[BOXBOTTOM] + 1;
I_UpdateBox(x, y, w, h);
}
// Clear box
M_ClearBox(dirtybox);
}
//
// I_FinishUpdate
//
void I_FinishUpdate(void)
{
static int lasttic;
int tics;
int i;
if (devparm)
{
i = ticcount;
tics = i - lasttic;
lasttic = i;
if (tics > 20) tics = 20;
outpw(SC_INDEX, 0x102);
for (i = 0; i < tics; i++)
{
destscreen[(SCREENHEIGHT - 1)*SCREENWIDTH / 4 + i] = 0xff;
}
for (; i < 20; i++)
{
destscreen[(SCREENHEIGHT - 1)*SCREENWIDTH / 4 + i] = 0x0;
}
}
outpw(CRTC_INDEX, ((int)destscreen & 0xff00) + 0xc);
//Next plane
destscreen += 0x4000;
if (destscreen == (byte*)0xac000)
{
destscreen = (byte*)0xa0000;
}
}
//
// I_InitGraphics
//
void I_InitGraphics(void)
{
if (novideo)
{
return;
}
grmode = true;
regs.w.ax = 0x13;
int386(0x10, (union REGS *)®s, ®s);
pcscreen = currentscreen = (byte *)0xa0000;
destscreen = (byte *)0xa4000;
outp(SC_INDEX, SC_MEMMODE);
outp(SC_INDEX + 1, (inp(SC_INDEX + 1)&~8) | 4);
outp(GC_INDEX, GC_MODE);
outp(GC_INDEX + 1, inp(GC_INDEX + 1)&~0x13);
outp(GC_INDEX, GC_MISCELLANEOUS);
outp(GC_INDEX + 1, inp(GC_INDEX + 1)&~2);
outpw(SC_INDEX, 0xf02);
memset(pcscreen, 0, 0x10000);
outp(CRTC_INDEX, CRTC_UNDERLINE);
outp(CRTC_INDEX + 1, inp(CRTC_INDEX + 1)&~0x40);
outp(CRTC_INDEX, CRTC_MODE);
outp(CRTC_INDEX + 1, inp(CRTC_INDEX + 1) | 0x40);
outp(GC_INDEX, GC_READMAP);
I_SetPalette(W_CacheLumpName("PLAYPAL", PU_CACHE));
I_InitDiskFlash();
}
//
// I_ShutdownGraphics
//
void I_ShutdownGraphics(void)
{
if (*(byte *)0x449 == 0x13) // don't reset mode if it didn't get set
{
regs.w.ax = 3;
int386(0x10, ®s, ®s); // back to text mode
}
}
//
// I_ReadScreen
// Reads the screen currently displayed into a linear buffer.
//
void I_ReadScreen(byte *scr)
{
int i;
int j;
outp(GC_INDEX, GC_READMAP);
for (i = 0; i < 4; i++)
{
outp(GC_INDEX+1, i);
for (j = 0; j < SCREENWIDTH*SCREENHEIGHT/4; j++)
{
scr[i+j*4] = currentscreen[j];
}
}
}
//
// I_StartTic
//
// called by D_DoomLoop
// called before processing each tic in a frame
// can call D_PostEvent
// asyncronous interrupt functions should maintain private ques that are
// read by the syncronous functions to be converted into events
//
#define SC_UPARROW 0x48
#define SC_DOWNARROW 0x50
#define SC_LEFTARROW 0x4b
#define SC_RIGHTARROW 0x4d
void I_StartTic(void)
{
int k;
event_t ev;
I_ReadMouse();
//
// keyboard events
//
while (kbdtail < kbdhead)
{
k = keyboardque[kbdtail&(KBDQUESIZE - 1)];
kbdtail++;
// extended keyboard shift key bullshit
if ((k & 0x7f) == SC_LSHIFT || (k & 0x7f) == SC_RSHIFT)
{
if (keyboardque[(kbdtail - 2)&(KBDQUESIZE - 1)] == 0xe0)
{
continue;
}
k &= 0x80;
k |= SC_RSHIFT;
}
if (k == 0xe0)
{
continue; // special / pause keys
}
if (keyboardque[(kbdtail - 2)&(KBDQUESIZE - 1)] == 0xe1)
{
continue; // pause key bullshit
}
if (k == 0xc5 && keyboardque[(kbdtail - 2)&(KBDQUESIZE - 1)] == 0x9d)
{
ev.type = ev_keydown;
ev.data1 = KEY_PAUSE;
D_PostEvent(&ev);
continue;
}
if (k & 0x80)
ev.type = ev_keyup;
else
ev.type = ev_keydown;
k &= 0x7f;
switch (k)
{
case SC_UPARROW:
ev.data1 = KEY_UPARROW;
break;
case SC_DOWNARROW:
ev.data1 = KEY_DOWNARROW;
break;
case SC_LEFTARROW:
ev.data1 = KEY_LEFTARROW;
break;
case SC_RIGHTARROW:
ev.data1 = KEY_RIGHTARROW;
break;
default:
ev.data1 = scantokey[k];
break;
}
D_PostEvent(&ev);
}
}
void I_ReadKeys(void)
{
int k;
while (1)
{
while (kbdtail < kbdhead)
{
k = keyboardque[kbdtail&(KBDQUESIZE - 1)];
kbdtail++;
printf("0x%x\n", k);
if (k == 1)
I_Quit();
}
}
}
void I_ColorBlack(int r, int g, int b)
{
_outbyte(PEL_WRITE_ADR, 0);
_outbyte(PEL_DATA, r);
_outbyte(PEL_DATA, g);
_outbyte(PEL_DATA, b);
}
//
// Timer interrupt
//
//
// I_TimerISR
//
int I_TimerISR(void)
{
ticcount++;
return 0;
}
//
// Keyboard
//
void (__interrupt __far *oldkeyboardisr) () = NULL;
int lastpress;
//
// I_KeyboardISR
//
void __interrupt I_KeyboardISR(void)
{
// Get the scan code
keyboardque[kbdhead&(KBDQUESIZE - 1)] = lastpress = _inbyte(0x60);
kbdhead++;
// acknowledge the interrupt
_outbyte(0x20, 0x20);
}
//
// I_StartupKeyboard
//
void I_StartupKeyboard(void)
{
oldkeyboardisr = _dos_getvect(KEYBOARDINT);
_dos_setvect (0x8000 | KEYBOARDINT, I_KeyboardISR);
//I_ReadKeys ();
}
void I_ShutdownKeyboard(void)
{
if (oldkeyboardisr)
_dos_setvect (KEYBOARDINT, oldkeyboardisr);
*(short *)0x41c = *(short *)0x41a; // clear bios key buffer
}
//
// Mouse
//
int I_ResetMouse(void)
{
regs.w.ax = 0; // reset
int386 (0x33, ®s, ®s);
return regs.w.ax;
}
//
// StartupMouse
//
void I_StartupMouse(void)
{
//
// General mouse detection
//
mousepresent = 0;
if (M_CheckParm("-nomouse") || !usemouse)
{
return;
}
if (I_ResetMouse() != 0xffff)
{
printf("Mouse: not present\n", 0);
return;
}
printf("Mouse: detected\n", 0);
mousepresent = 1;
I_StartupCyberMan();
}
//
// ShutdownMouse
//
void I_ShutdownMouse(void)
{
if (!mousepresent)
{
return;
}
I_ResetMouse();
}
//
// I_ReadMouse
//
void I_ReadMouse(void)
{
event_t ev;
//
// mouse events
//
if (!mousepresent)
{
return;
}
ev.type = ev_mouse;
memset(&dpmiregs, 0, sizeof(dpmiregs));
dpmiregs.eax = 3; // read buttons / position
DPMIInt(0x33);
ev.data1 = dpmiregs.ebx;
dpmiregs.eax = 11; // read counters
DPMIInt(0x33);
ev.data2 = (short)dpmiregs.ecx;
ev.data3 = -(short)dpmiregs.edx;
D_PostEvent(&ev);
}
//
// Joystick
//
int joyxl, joyxh, joyyl, joyyh;
boolean WaitJoyButton(void)
{
int oldbuttons, buttons;
oldbuttons = 0;
do
{
I_WaitVBL(1);
buttons = ((inp(0x201) >> 4) & 1) ^ 1;
if (buttons != oldbuttons)
{
oldbuttons = buttons;
continue;
}
if ((lastpress & 0x7f) == 1)
{
joystickpresent = false;
return false;
}
} while (!buttons);
do
{
I_WaitVBL(1);
buttons = ((inp(0x201) >> 4) & 1) ^ 1;
if (buttons != oldbuttons)
{
oldbuttons = buttons;
continue;
}
if ((lastpress & 0x7f) == 1)
{
joystickpresent = false;
return false;
}
} while (buttons);
return true;
}
//
// I_StartupJoystick
//
int basejoyx, basejoyy;
void I_StartupJoystick(void)
{
int buttons;
int count;
int centerx, centery;
joystickpresent = 0;
if (M_CheckParm("-nojoy") || !usejoystick)
{
return;
}
if (!I_ReadJoystick())
{
joystickpresent = false;
printf("joystick not found\n");
return;
}
printf("joystick found\n");
joystickpresent = true;
printf("CENTER the joystick and press button 1:");
if (!WaitJoyButton())
{
return;
}
I_ReadJoystick();
centerx = joystickx;
centery = joysticky;
printf("\nPush the joystick to the UPPER LEFT corner and press button 1:");
if (!WaitJoyButton())
{
return;
}
I_ReadJoystick();
joyxl = (centerx + joystickx) / 2;
joyyl = (centerx + joysticky) / 2;
printf("\nPush the joystick to the LOWER RIGHT corner and press button 1:");
if (!WaitJoyButton())
{
return;
}
I_ReadJoystick();
joyxh = (centerx + joystickx) / 2;
joyyh = (centery + joysticky) / 2;
printf("\n");
}
//
// I_StartFrame
//
void I_StartFrame(void)
{
event_t ev;
//
// joystick events
//
if (!joystickpresent)
{
return;
}
I_ReadJoystick();
ev.type = ev_joystick;
ev.data1 = ((inp(0x201) >> 4) & 15) ^ 15;
if (joystickx < joyxl)
{
ev.data2 = -1;
}
else if (joystickx > joyxh)
{
ev.data2 = 1;
}
else
{
ev.data2 = 0;
}
if (joysticky < joyyl)
{
ev.data3 = -1;
}
else if (joysticky > joyyh)
{
ev.data3 = 1;
}
else
{
ev.data3 = 0;
}
D_PostEvent(&ev);
}
//
// DPMI stuff
//
#define REALSTACKSIZE 1024
dpmiregs_t dpmiregs;
unsigned realstackseg;
void I_DivException(void);
int I_SetDivException(void);
//
// DPMIFarCall
//
void DPMIFarCall(void)
{
segread(&segregs);
regs.w.ax = 0x301;
regs.w.bx = 0;
regs.w.cx = 0;
regs.x.edi = (unsigned)&dpmiregs;
segregs.es = segregs.ds;
int386x(DPMI_INT, ®s, ®s, &segregs);
}
//
// I_StartupDPMI
//
byte *I_AllocLow(int length);
void I_StartupDPMI(void)
{
extern char __begtext;
extern char ___Argc;
//
// allocate a decent stack for real mode ISRs