-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi_elks.c
402 lines (327 loc) · 7.36 KB
/
i_elks.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
/*-----------------------------------------------------------------------------
*
*
* Copyright (C) 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:
* ELKS implementation of i_system.h
*
*-----------------------------------------------------------------------------*/
#include <fcntl.h>
#include <stdarg.h>
#include <termios.h>
#include <unistd.h>
#include <linuxmt/mem.h>
#include <sys/ioctl.h>
#include "doomdef.h"
#include "doomtype.h"
#include "compiler.h"
#include "d_main.h"
#include "i_system.h"
#include "globdata.h"
void I_InitGraphicsHardwareSpecificCode(void);
static boolean isGraphicsModeSet = false;
//**************************************************************************************
//
// Functions that are available in DOS, but not in ELKS
//
int16_t abs(int16_t v)
{
return v < 0 ? -v : v;
}
int32_t labs(int32_t v)
{
return v < 0 ? -v : v;
}
void __far* _fmemchr(const void __far* str, int c, size_t n)
{
uint8_t __far* s = (uint8_t __far*)str;
uint8_t b = c;
for (size_t i = 0; i < n; i++)
{
if (*s == b)
{
return s;
}
s++;
}
return NULL;
}
void __far* _fmemcpy(void __far* destination, const void __far* source, size_t num)
{
uint8_t __far* s = (uint8_t __far*)source;
uint8_t __far* d = (uint8_t __far*)destination;
for (size_t i = 0; i < num; i++)
*d++ = *s++;
return NULL;
}
void __far* _fmemset(void __far* str, int c, size_t n)
{
uint8_t __far* d = (uint8_t __far*)str;
uint8_t b = c;
for (size_t i = 0; i < n; i++)
*d++ = b;
return NULL;
}
char __far* _fstrcpy(char __far* destination, const char __far* source)
{
char __far* s = (char __far*)source;
while (*s)
*destination++ = *s++;
*destination = '\0';
return NULL;
}
size_t _fstrlen(const char __far* str)
{
size_t l = 0;
char __far* s = (char __far*)str;
while (*s)
{
l++;
s++;
}
return l;
}
//**************************************************************************************
//
// Screen code
//
void I_InitGraphics(void)
{
I_InitGraphicsHardwareSpecificCode();
isGraphicsModeSet = true;
}
//**************************************************************************************
//
// Keyboard code
//
static struct termios oldt;
static boolean isKeyboardIsrSet = false;
static void I_cfmakeraw(struct termios *t)
{
t->c_cc[VMIN] = 0;
t->c_cc[VTIME] = 0;
t->c_cflag &= ~(CSIZE | PARENB | CSTOPB);
t->c_cflag |= (CS8 | CREAD);
t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INPCK | ISTRIP);
t->c_iflag &= ~(INLCR | IGNCR | ICRNL | IXON | IXOFF);
t->c_iflag |= (BRKINT | IGNPAR);
t->c_oflag &= ~(OPOST | OLCUC | OCRNL | ONOCR | ONLRET | OFILL | OFDEL);
t->c_oflag &= ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
t->c_oflag |= (ONLCR | NL0 | CR0 | TAB3 | BS0 | VT0 | FF0);
t->c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL);
t->c_lflag &= ~(NOFLSH | XCASE);
t->c_lflag &= ~(ECHOPRT | ECHOCTL | ECHOKE);
}
void I_InitKeyboard(void)
{
struct termios newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
I_cfmakeraw(&newt);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
isKeyboardIsrSet = true;
}
static void I_ShutdownKeyboard(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
void I_StartTic(void)
{
//
// process keyboard events
//
event_t ev;
ev.type = ev_keydown;
uint8_t k;
ssize_t r = read(STDIN_FILENO, &k, 1);
while (r > 0)
{
ev.data1 = -1;
if (k == 0x1b)
{
r = read(STDIN_FILENO, &k, 1);
if (r <= 0)
{
// Escape
ev.data1 = KEYD_START;
}
else
{
if (0x61 <= k && k <= 0x6c)
{
// F1 - F12 not supported
}
else if (k == 0x5b)
{
r = read(STDIN_FILENO, &k, 1);
if (r <= 0)
{
// [
// Escape followed by [ is not supported
}
else
{
if (k == 0x32 || k == 0x35 || k == 0x36)
{
// Insert, Page Up, Page Down not supported
uint8_t temp;
read(STDIN_FILENO, &temp, 1);
}
else
{
switch (k)
{
case 0x41: ev.data1 = KEYD_UP; break;
case 0x42: ev.data1 = KEYD_DOWN; break;
case 0x43: ev.data1 = KEYD_RIGHT; break;
case 0x44: ev.data1 = KEYD_LEFT; break;
default: break; //not supported
}
}
}
}
else
{
// Escape followed by an unknown key not supported
}
}
}
else if (k == 0x11) // Ctrl + Q
{
I_Quit();
}
else if ('a' <= k && k <= 'z')
{
ev.data1 = k;
}
else if ('A' <= k && k <= 'Z')
{
ev.data1 = k | 0x20; // tolower(k);
}
else
{
switch (k)
{
case 0x0d: // Enter
case ' ': // Spacebar
ev.data1 = KEYD_A;
break;
//case : // Shift
// ev.data1 = KEYD_SPEED;
// break;
case '8':
ev.data1 = KEYD_UP;
break;
case '2':
ev.data1 = KEYD_DOWN;
break;
case '4':
ev.data1 = KEYD_LEFT;
break;
case '6':
ev.data1 = KEYD_RIGHT;
break;
case 0x09: // Tab
ev.data1 = KEYD_SELECT;
break;
case '/':
ev.data1 = KEYD_B;
break;
//case : // Alt
// ev.data1 = KEYD_STRAFE;
// break;
case ',':
ev.data1 = KEYD_L;
break;
case '.':
ev.data1 = KEYD_R;
break;
case '-':
ev.data1 = KEYD_MINUS;
break;
case '=':
ev.data1 = KEYD_PLUS;
break;
case '[':
ev.data1 = KEYD_BRACKET_LEFT;
break;
case ']':
ev.data1 = KEYD_BRACKET_RIGHT;
break;
}
}
if (ev.data1 != -1)
D_PostEvent(&ev);
r = read(STDIN_FILENO, &k, 1);
}
}
//**************************************************************************************
//
// Returns time in 1/35th second tics.
//
static uint32_t __far* pjiffies;
int32_t I_GetTime(void)
{
uint32_t tick10ms = *pjiffies; /* read kernel 10ms timer directly */
//return tick10ms * TICRATE / 100;
return (tick10ms * TICRATE * 10) / 1024;
}
void I_InitTimer(void)
{
int16_t fd = open("/dev/kmem", O_RDONLY);
if (fd < 0) {
I_Error("No kmem");
}
uint16_t kernel_ds, offset;
if (ioctl(fd, MEM_GETDS, &kernel_ds) < 0
|| ioctl(fd, MEM_GETJIFFADDR, &offset ) < 0) {
I_Error("No mem ioctl");
}
close(fd);
pjiffies = D_MK_FP(kernel_ds, offset);
}
//**************************************************************************************
//
// Exit code
//
static void I_Shutdown(void)
{
if (isGraphicsModeSet)
I_SetScreenMode(3);
I_ShutdownSound();
if (isKeyboardIsrSet)
I_ShutdownKeyboard();
}
void I_Quit(void)
{
I_Shutdown();
int16_t lumpnum = W_GetNumForName("ENDOOM");
W_ReadLumpByNum(lumpnum, D_MK_FP(0xb800, __djgpp_conventional_base));
exit(0);
}
void I_Error (const char *error, ...)
{
va_list argptr;
I_Shutdown();
va_start(argptr, error);
vfprintf(stdout, error, argptr);
va_end(argptr);
printf("\n");
exit(1);
}