-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathi_system.c
364 lines (298 loc) · 6.83 KB
/
i_system.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
/*-----------------------------------------------------------------------------
*
*
* Copyright (C) 2023-2024 Frenkel Smeijers
*
* 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., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* DESCRIPTION:
* DOS implementation of i_system.h
*
*-----------------------------------------------------------------------------*/
#include <conio.h>
#include <dos.h>
#include <stdarg.h>
#include <time.h>
#include "doomdef.h"
#include "doomtype.h"
#include "compiler.h"
#include "a_taskmn.h"
#include "d_main.h"
#include "i_system.h"
#include "globdata.h"
void I_InitGraphicsHardwareSpecificCode(void);
void I_ShutdownGraphics(void);
static boolean isGraphicsModeSet = false;
//**************************************************************************************
//
// Screen code
//
void I_SetScreenMode(uint16_t mode)
{
union REGS regs;
regs.w.ax = mode;
int86(0x10, ®s, ®s);
}
void I_InitGraphics(void)
{
I_InitGraphicsHardwareSpecificCode();
isGraphicsModeSet = true;
}
//**************************************************************************************
//
// Keyboard code
//
#define KEYBOARDINT 9
#define KBDQUESIZE 32
static byte keyboardqueue[KBDQUESIZE];
static int16_t kbdtail, kbdhead;
static boolean isKeyboardIsrSet = false;
#if defined __DJGPP__
static _go32_dpmi_seginfo oldkeyboardisr, newkeyboardisr;
#else
static void (__interrupt *oldkeyboardisr)(void);
#endif
static void __interrupt I_KeyboardISR(void)
{
// Get the scan code
keyboardqueue[kbdhead & (KBDQUESIZE - 1)] = inp(0x60);
kbdhead++;
// Tell the XT keyboard controller to clear the key
byte temp;
outp(0x61, (temp = inp(0x61)) | 0x80);
outp(0x61, temp);
// acknowledge the interrupt
outp(0x20, 0x20);
}
void I_InitKeyboard(void)
{
replaceInterrupt(oldkeyboardisr, newkeyboardisr, KEYBOARDINT, I_KeyboardISR);
isKeyboardIsrSet = true;
}
#define SC_ESCAPE 0x01
#define SC_MINUS 0x0c
#define SC_PLUS 0x0d
#define SC_TAB 0x0f
#define SC_BRACKET_LEFT 0x1a
#define SC_BRACKET_RIGHT 0x1b
#define SC_ENTER 0x1c
#define SC_CTRL 0x1d
#define SC_LSHIFT 0x2a
#define SC_RSHIFT 0x36
#define SC_COMMA 0x33
#define SC_PERIOD 0x34
#define SC_ALT 0x38
#define SC_SPACE 0x39
#define SC_F10 0x44
#define SC_UPARROW 0x48
#define SC_DOWNARROW 0x50
#define SC_LEFTARROW 0x4b
#define SC_RIGHTARROW 0x4d
#define SC_Q 0x10
#define SC_P 0x19
#define SC_A 0x1e
#define SC_L 0x26
#define SC_Z 0x2c
#define SC_M 0x32
#if defined VIDEO_MODE_CGA
#define SC_F5 0x3f
void I_SwitchPalette(void);
#endif
void I_StartTic(void)
{
//
// process keyboard events
//
byte k;
event_t ev;
while (kbdtail < kbdhead)
{
k = keyboardqueue[kbdtail & (KBDQUESIZE - 1)];
kbdtail++;
// extended keyboard shift key bullshit
if ((k & 0x7f) == SC_LSHIFT || (k & 0x7f) == SC_RSHIFT)
{
if (keyboardqueue[(kbdtail - 2) & (KBDQUESIZE - 1)] == 0xe0)
continue;
k &= 0x80;
k |= SC_RSHIFT;
}
if (k == 0xe0)
continue; // special / pause keys
if (keyboardqueue[(kbdtail - 2) & (KBDQUESIZE - 1)] == 0xe1)
continue; // pause key bullshit
if (k == 0xc5 && keyboardqueue[(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_ESCAPE:
ev.data1 = KEYD_START;
break;
case SC_ENTER:
case SC_SPACE:
ev.data1 = KEYD_A;
break;
case SC_RSHIFT:
ev.data1 = KEYD_SPEED;
break;
case SC_UPARROW:
ev.data1 = KEYD_UP;
break;
case SC_DOWNARROW:
ev.data1 = KEYD_DOWN;
break;
case SC_LEFTARROW:
ev.data1 = KEYD_LEFT;
break;
case SC_RIGHTARROW:
ev.data1 = KEYD_RIGHT;
break;
case SC_TAB:
ev.data1 = KEYD_SELECT;
break;
case SC_CTRL:
ev.data1 = KEYD_B;
break;
case SC_ALT:
ev.data1 = KEYD_STRAFE;
break;
case SC_COMMA:
ev.data1 = KEYD_L;
break;
case SC_PERIOD:
ev.data1 = KEYD_R;
break;
case SC_MINUS:
ev.data1 = KEYD_MINUS;
break;
case SC_PLUS:
ev.data1 = KEYD_PLUS;
break;
case SC_BRACKET_LEFT:
ev.data1 = KEYD_BRACKET_LEFT;
break;
case SC_BRACKET_RIGHT:
ev.data1 = KEYD_BRACKET_RIGHT;
break;
#if defined VIDEO_MODE_CGA
case SC_F5:
if (ev.type == ev_keydown)
I_SwitchPalette();
continue;
#endif
case SC_F10:
I_Quit();
default:
if (SC_Q <= k && k <= SC_P)
{
ev.data1 = "qwertyuiop"[k - SC_Q];
break;
}
else if (SC_A <= k && k <= SC_L)
{
ev.data1 = "asdfghjkl"[k - SC_A];
break;
}
else if (SC_Z <= k && k <= SC_M)
{
ev.data1 = "zxcvbnm"[k - SC_Z];
break;
}
else
continue;
}
D_PostEvent(&ev);
}
}
//**************************************************************************************
//
// Returns time in 1/35th second tics.
//
#define TIMER_PRIORITY 0
static volatile int32_t ticcount;
static boolean isTimerSet;
static void I_TimerISR(void)
{
ticcount++;
}
int32_t I_GetTime(void)
{
return ticcount;
}
void I_InitTimer(void)
{
TS_ScheduleTask(I_TimerISR, TICRATE, TIMER_PRIORITY);
TS_Dispatch();
isTimerSet = true;
}
static void I_ShutdownTimer(void)
{
TS_Terminate(TIMER_PRIORITY);
TS_Shutdown();
}
//**************************************************************************************
//
// Exit code
//
static void I_Shutdown(void)
{
if (isGraphicsModeSet)
I_ShutdownGraphics();
I_ShutdownSound();
if (isTimerSet)
I_ShutdownTimer();
if (isKeyboardIsrSet)
{
restoreInterrupt(KEYBOARDINT, oldkeyboardisr, newkeyboardisr);
}
W_Shutdown();
Z_Shutdown();
}
segment_t I_GetTextModeVideoMemorySegment(void);
void I_Quit(void)
{
I_Shutdown();
int16_t lumpnum = W_GetNumForName("ENDOOM");
W_ReadLumpByNum(lumpnum, D_MK_FP(I_GetTextModeVideoMemorySegment(), 0 + __djgpp_conventional_base));
union REGS regs;
regs.h.ah = 2;
regs.h.bh = 0;
regs.h.dl = 0;
regs.h.dh = 23;
int86(0x10, ®s, ®s);
printf("\n");
exit(0);
}
void I_Error (const char *error, ...)
{
va_list argptr;
I_Shutdown();
va_start(argptr, error);
vprintf(error, argptr);
va_end(argptr);
printf("\n");
exit(1);
}