-
Notifications
You must be signed in to change notification settings - Fork 0
/
monmisc.c
377 lines (335 loc) · 9.35 KB
/
monmisc.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
#include <stdio.h>
#include <trmdef.h>
#include <ssdef.h>
#include <descrip.h>
#include <iodef.h>
#include <string.h>
#include <stdlib.h>
#include <libdef.h>
#include <libdtdef.h>
#include <ctype.h>
#include <unixlib.h>
#include <math.h>
#include <dvidef.h>
#include <time.h>
#include "pascal.h"
#define short_wait 0.10
#define med_wait 0.20
extern int checkevents(), allevents();
extern int lib$signal(), lib$disable_ctrl(), lib$enable_ctrl();
extern int lib$init_timer(), sys$assign(), sys$qiow(), sys$setpri();
extern int lib$wait(), rand(), lib$cvtf_from_internal_time();
extern int lib$spawn(), lib$stat_timer(), lib$get_ef();
extern int sys$clref(), sys$qio(), sys$setimr(), sys$bintim();
extern int lib$cvtf_to_internal_time(), sys$waitfr();
extern int sys$setef(), sys$crembx(), lib$getdvi();
/* ---------------------------------------------------------------------- */
struct iosb_type {
unsigned short status, length;
unsigned short other[2];
};
struct itemlistcell {
unsigned short buffer_length, item_code;
unsigned buffer_addr, return_addr;
};
typedef unsigned long uquad[2];
/* ---------------------------------------------------------------------- */
unsigned long timercontext;
unsigned in_chan = 0, out_chan = 0, mbx_chan = 0;
unsigned long input_ef = 0, wait_ef = 0, dummy_ef = 0;
uquad delta_timer, wait_timer;
struct itemlistcell list[6];
/* ---------------------------------------------------------------------- */
static unsigned status;
#define CheckStatus(_msg) if (!(status & 1)) { printf("%s",_msg); \
lib$signal(status); }
/* ---------------------------------------------------------------------- */
struct dsc$descriptor_s dummy_d = { 0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0 };
#define fill_descrip(_d, _s) {_d.dsc$a_pointer = _s; \
_d.dsc$w_length = strlen(_s); }
/* ---------------------------------------------------------------------- */
void spawn(STRING *comm)
{
term_string(comm);
fill_descrip(dummy_d, comm->string)
status = lib$spawn(&dummy_d, 0, 0, 0, 0, 0, 0);
CheckStatus("lib$spawn");
}
int getticks(void)
{
long int code = 1;
uquad time;
float seconds;
status = lib$stat_timer(&code, &time, &timercontext);
CheckStatus("lib$stat_timer");
code = LIB$K_DELTA_SECONDS_F;
status = lib$cvtf_from_internal_time(&code, &seconds, &time);
CheckStatus("lib$cvtf_from_internal_time");
return (int)(seconds * 10.0);
}
int rnd(int val)
{
if (!val) return 0;
return (rand()%val) + 1;
}
void wait(float seconds)
{
int wait_timer[2];
wait_timer[0] = -10000000 * seconds;
wait_timer[1] = -1;
status = sys$setimr(wait_ef, wait_timer, 0, 142, 0);
CheckStatus("sys$setimr");
status = sys$waitfr(wait_ef);
CheckStatus("sys$waitfr");
}
static int get_pid(int parent)
{
if (parent) return getppid();
return getpid();
}
static void boostpriority(void)
{
int pid;
pid = get_pid(0);
status = sys$setpri(&pid, 0, 6, 0, 0, 0);
pid = get_pid(1);
status = sys$setpri(&pid, 0, 6, 0, 0, 0);
}
void freeze(float secs, AllStats *ams)
{
int end, didcheck = 0;
end = (int)(10.0 * secs) + getticks();
while (getticks() < end)
{
/* printf("%d %d\n", getticks(), ams->tick.tkallevent); */
if (getticks() > ams->tick.tkallevent) allevents(&1, &didcheck, ams);
wait(short_wait);
}
if (!didcheck) checkevents(&FALSE, &TRUE, &FALSE, ams);
}
#define putchars(_str) printf("%s", _str)
int pending = 0;
struct iosb_type iosb, mbx_iosb;
unsigned char mbxread[214];
unsigned char inp[6];
unsigned char pic = 0;
extern void launch_qio();
void mbx_read_done()
{
pending += mbx_iosb.length;
launch_qio();
}
void launch_qio()
{
status = sys$qio(input_ef, mbx_chan, IO$_READVBLK, &mbx_iosb,
mbx_read_done, 0, mbxread, sizeof(mbxread), 0, 0, 0, 0);
CheckStatus("sys$qio mbxread");
}
unsigned char inp_str[255];
unsigned char *inp_curr;
int nchars_left = 0;
static unsigned char keyget(void)
{
list[5].buffer_length = 1;
list[5].item_code = TRM$_PICSTRNG;
list[5].buffer_addr = (int)(&pic);
list[5].return_addr = 0;
if (!nchars_left)
{
if (pending <= 0) return 0;
/* printf("attempting the qiow\n"); */
status = sys$qiow(input_ef, in_chan,
IO$_READVBLK+IO$M_NOECHO+IO$M_NOFILTR+IO$M_TIMED,
&iosb, 0, 0, inp_str, sizeof(inp_str), 0, 0, 0, 0);
CheckStatus("sys$qiow in loop");
pending -= iosb.length;
inp_curr = inp_str;
nchars_left = iosb.length;
/*
printf("iosb: status %d length %d o1 %d o2 %d\n", iosb.status,
iosb.length, iosb.other[0], iosb.other[1]);
*/
if (iosb.other[0]) inp_curr[nchars_left++] = (unsigned char)iosb.other[0];
if (!nchars_left) return 0;
}
nchars_left--;
inp_curr++;
return inp_curr[-1];
}
char *prompt = "", *line = "", gecho = 1;
#define resetline printf("\015\033[K%s%s", prompt, line)
#define fixprompt printf("\012%s%s", prompt, gecho?line:"");
static int nextkey(AllStats *ams)
{
int ticks, keycode;
char dummy;
do {
if (ams->stats.ingame)
{
ticks = getticks();
/*
printf("ticks is : %d (event: %d) (all: %d) \n", ticks,
ams->tick.tkevent, ams->tick.tkallevent);
*/
if (ticks > ams->tick.tkevent) checkevents(&FALSE, &TRUE, &FALSE, ams);
if (ticks > ams->tick.tkallevent) allevents(&0, &dummy, ams);
if (ams->stats.printed) fixprompt;
ams->stats.printed = FALSE;
}
keycode = keyget();
if (!keycode) wait(short_wait);
} while (!keycode);
return keycode;
}
void grabline(STRING *_prompt, STRING *_s, AllStats *ams,
char echo, int maxlen)
{
int keycode, len = 0, pos = 0, loop;
char *curs;
gecho = echo;
prompt = _prompt->string;
term_string(_prompt);
line = _s->string;
*line = 0;
fixprompt;
while ((keycode = nextkey(ams)) != 13)
{
switch (keycode)
{
case 8:
case 127:
if (pos != 0)
{
for (loop = pos +1; loop < len; loop++) line[loop-1] = line[loop];
len--;
pos--;
line[len] = 0;
printf("\010 \010");
}
break;
case 21:
len = pos = *line = 0;
resetline;
break;
case 23:
resetline;
break;
case 2:
boostpriority;
printf("Boosting priority\n\n");
resetline;
break;
default:
if ((keycode>=32) && (keycode<=126))
{
if (len < maxlen)
{
pos++;
for (loop=len+1;loop>pos;loop++) line[loop] = line[loop-1];
line[pos-1] = keycode;
len++;
line[len] = 0;
if (echo) printf("%c", keycode);
}
else printf("Exceeded length (%d)\n", keycode);
}
else printf("Read in a %d(%c)\n", keycode, keycode);
break;
}
}
printf("\015");
set_length(_s, len);
}
$DESCRIPTOR(out_chan_d, "SYS$OUTPUT");
$DESCRIPTOR(in_chan_d, "SYS$INPUT");
static char init = 0;
static createkeyboard(void)
{
memset(list, 0, sizeof(list));
list[0].item_code = TRM$_EDITMODE;
list[0].buffer_addr = TRM$K_EM_RDVERIFY;
list[1].item_code = TRM$_ESCTRMOVR;
list[1].buffer_addr = 4;
list[2].item_code = TRM$_INIOFFSET;
list[3].item_code = TRM$_MODIFIERS;
list[3].buffer_addr = TRM$M_TM_NOEDIT + TRM$M_TM_NOFILTR +
TRM$M_TM_ESCAPE;
/*
list[6].item_code = TRM$_TIMEOUT;
list[6].item_code = 0;
*/
list[4].buffer_length = 1;
list[4].item_code = TRM$_INISTRNG;
list[4].buffer_addr = (int)&init;
}
unsigned long save_dcl_ctrl;
$DESCRIPTOR(delta_timer_d, "0000 00:00:00.01");
$DESCRIPTOR(wait_timer_d, "0000 00:00:00.30");
char mbxname[40];
$DESCRIPTOR(mbxname_d, mbxname);
void setup_guts(void)
{
unsigned long mask;
srand(time(0));
status = sys$crembx(0, &mbx_chan, 0, 0, 0, 0, 0, 0);
CheckStatus("sys$crembx");
status = lib$getdvi(&DVI$_DEVNAM, &mbx_chan, 0, 0, &mbxname_d,
&mbxname_d.dsc$w_length);
CheckStatus("lib$getdvi");
status = sys$assign(&in_chan_d, &in_chan, 0, &mbxname_d, 0);
CheckStatus("sys$assign in_chan");
launch_qio();
status = sys$assign(&out_chan_d, &out_chan, 0, 0, 0);
CheckStatus("sys$assign");
status = lib$get_ef(&input_ef);
CheckStatus("lib$get_ef");
status = sys$bintim(&delta_timer_d, delta_timer);
CheckStatus("sys$bintim");
status = lib$get_ef(&wait_ef);
CheckStatus("lib$get_ef wait_ef");
status = lib$get_ef(&dummy_ef);
CheckStatus("lib$get_ef dummy_ef");
createkeyboard();
lib$init_timer(&timercontext);
mask = 0x2000000;
status = lib$disable_ctrl(&mask, &save_dcl_ctrl);
CheckStatus("lib$disable_ctrl");
}
void finish_guts(void)
{
status = lib$enable_ctrl(&save_dcl_ctrl);
CheckStatus("lib$enable_ctrl");
}
void grab_num(STRING *prompt, int *ret, int min, int max, int def, AllStats *ams)
{
int tmp = 0, neg = 0;
char *str;
STRING temp;
str = temp.string;
grabline(prompt, &temp, ams, TRUE, 80);
term_string((&temp));
if (*str == '-') { neg = 1; str++; }
if (!isdigit(*str)) {
*ret = def;
return;
}
while (isdigit(*str)) tmp = tmp * 10 + *str++ - '0';
*ret = neg?-tmp:tmp;
}
int grabyes(STRING *prompt, AllStats *ams)
{
STRING s;
grabline(prompt, &s, ams, TRUE, 1);
term_string((&s));
if (s.string[0] && (tolower(s.string[0]) == 'y')) return TRUE;
return FALSE;
}
int readyes(STRING *prompt)
{
char string[80];
term_string(prompt);
printf("%s", prompt);
gets(string);
if (*string && (tolower(string[0]) == 'y')) return TRUE;
return FALSE;
}