-
Notifications
You must be signed in to change notification settings - Fork 6
/
mmix-io.w
403 lines (366 loc) · 10.5 KB
/
mmix-io.w
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
% This file is part of the MMIXware package (c) Donald E Knuth 1999
@i boilerplate.w %<< legal stuff: PLEASE READ IT BEFORE MAKING ANY CHANGES!
\def\title{MMIX-IO}
\def\MMIX{\.{MMIX}}
\def\Hex#1{\hbox{$^{\scriptscriptstyle\#}$\tt#1}} % experimental hex constant
@*Introduction. This program module contains brute-force implementations
of the ten input/output primitives defined at the beginning of {\mc MMIX-SIM}.
The subroutines are grouped here as a separate package, because they
are intended to be loaded with the pipeline simulator as well as with the
simple simulator.
@^I/O@>
@^input/output@>
@c
@<Preprocessor macros@>@;
@<Type definitions@>@;
@<External subroutines@>@;
@<Global variables@>@;
@<Subroutines@>@;
@ Of course we include standard \CEE/ library routines, and we set things
up to accommodate older versions of \CEE/.
@<Preproc...@>=
#include <stdio.h>
#include <stdlib.h>
#ifdef __STDC__
#define ARGS(list) list
#else
#define ARGS(list) ()
#endif
#ifndef FILENAME_MAX
#define FILENAME_MAX 256
#endif
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
@ The unsigned 32-bit type \&{tetra} must agree with its definition
in the simulators.
@<Type...@>=
typedef unsigned int tetra;
typedef struct {tetra h,l;} octa; /* two tetrabytes make one octabyte */
@ Three basic subroutines are used to get strings from the simulated
memory and to put strings into that memory. These subroutines are
defined appropriately in each simulator. We also use a few subroutines
and constants defined in {\mc MMIX-ARITH}.
@<External...@>=
extern char stdin_chr @,@,@[ARGS((void))@];
extern int mmgetchars @,@,@[ARGS((char* buf,int size,octa addr,int stop))@];
extern void mmputchars @,@,@[ARGS((unsigned char* buf,int size,octa addr))@];
extern octa oplus @,@,@[ARGS((octa,octa))@];
extern octa ominus @,@,@[ARGS((octa,octa))@];
extern octa incr @,@,@[ARGS((octa,int))@];
extern octa zero_octa; /* |zero_octa.h=zero_octa.l=0| */
extern octa neg_one; /* |neg_one.h=neg_one.l=-1| */
@ Each possible handle has a file pointer and a current mode.
@<Type...@>=
typedef struct {
FILE *fp; /* file pointer */
int mode; /* [read OK] + 2[write OK] + 4[binary] + 8[readwrite] */
} sim_file_info;
@ @<Glob...@>=
sim_file_info sfile[256];
@ The first three handles are initially open.
@<Sub...@>=
void mmix_io_init @,@,@[ARGS((void))@];@+@t}\6{@>
void mmix_io_init()
{
sfile[0].fp=stdin, sfile[0].mode=1;
sfile[1].fp=stdout, sfile[1].mode=2;
sfile[2].fp=stderr, sfile[2].mode=2;
}
@ The only tricky thing about these routines is that we want to
protect the standard input, output, and error streams from being
preempted.
@<Sub...@>=
octa mmix_fopen @,@,@[ARGS((unsigned char,octa,octa))@];@+@t}\6{@>
octa mmix_fopen(handle,name,mode)
unsigned char handle;
octa name,mode;
{
char name_buf[FILENAME_MAX];
if (mode.h || mode.l>4) goto abort;
if (mmgetchars(name_buf,FILENAME_MAX,name,0)==FILENAME_MAX) goto abort;
if (sfile[handle].mode!=0 && handle>2) fclose(sfile[handle].fp);
sfile[handle].fp=fopen(name_buf,mode_string[mode.l]);
if (!sfile[handle].fp) goto abort;
sfile[handle].mode=mode_code[mode.l];
return zero_octa; /* success */
abort: sfile[handle].mode=0;
return neg_one; /* failure */
}
@ @<Glob...@>=
char *mode_string[]={"r","w","rb","wb","w+b"};
int mode_code[]={0x1,0x2,0x5,0x6,0xf};
@ If the simulator is being used interactively, we can avoid competition
for |stdin| by substituting another file.
@<Sub...@>=
void mmix_fake_stdin @,@,@[ARGS((FILE*))@];@+@t}\6{@>
void mmix_fake_stdin(f)
FILE *f;
{
sfile[0].fp=f; /* |f| should be open in mode \.{"r"} */
}
@ @<Sub...@>=
octa mmix_fclose @,@,@[ARGS((unsigned char))@];@+@t}\6{@>
octa mmix_fclose(handle)
unsigned char handle;
{
if (sfile[handle].mode==0) return neg_one;
if (handle>2 && fclose(sfile[handle].fp)!=0) return neg_one;
sfile[handle].mode=0;
return zero_octa; /* success */
}
@ @<Sub...@>=
octa mmix_fread @,@,@[ARGS((unsigned char,octa,octa))@];@+@t}\6{@>
octa mmix_fread(handle,buffer,size)
unsigned char handle;
octa buffer,size;
{
register unsigned char *buf;
register int n;
octa o;
o=neg_one;
if (!(sfile[handle].mode&0x1)) goto done;
if (sfile[handle].mode&0x8) sfile[handle].mode &=~ 0x2;
if (size.h) goto done;
buf=(unsigned char*)calloc(size.l,sizeof(char));
if (!buf) goto done;
@<Read |n<=size.l| characters into |buf|@>;
mmputchars(buf,n,buffer);
free(buf);
o.h=0, o.l=n;
done: return ominus(o,size);
}
@ @<Read |n<=size.l| characters into |buf|@>=
if (sfile[handle].fp==stdin) {
register unsigned char *p;
for (p=buf,n=size.l; p<buf+n; p++) *p=stdin_chr();
} else {
clearerr(sfile[handle].fp);
n=fread(buf,1,size.l,sfile[handle].fp);
if (ferror(sfile[handle].fp)) {
free(buf);
goto done;
}
}
@ @<Sub...@>=
octa mmix_fgets @,@,@[ARGS((unsigned char,octa,octa))@];@+@t}\6{@>
octa mmix_fgets(handle,buffer,size)
unsigned char handle;
octa buffer,size;
{
char buf[256];
register int n,s;
register char *p;
octa o;
int eof=0;
if (!(sfile[handle].mode&0x1)) return neg_one;
if (!size.l && !size.h) return neg_one;
if (sfile[handle].mode&0x8) sfile[handle].mode &=~ 0x2;
size=incr(size,-1);
o=zero_octa;
while (1) {
@<Read |n<256| characters into |buf|@>;
mmputchars((unsigned char*)buf,n+1,buffer);
o=incr(o,n);
size=incr(size,-n);
if ((n&&buf[n-1]=='\n') || (!size.l&&!size.h) || eof) return o;
buffer=incr(buffer,n);
}
}
@ @<Read |n<256| characters into |buf|@>=
s=255;
if (size.l<s && !size.h) s=size.l;
if (sfile[handle].fp==stdin)
for (p=buf,n=0;n<s;) {
*p=stdin_chr();
n++;
if (*p++=='\n') break;
}
else {
if (!fgets(buf,s+1,sfile[handle].fp)) return neg_one;
eof=feof(sfile[handle].fp);
for (p=buf,n=0;n<s;) {
if (!*p && eof) break;
n++;
if (*p++=='\n') break;
}
}
*p='\0';
@ The routines that deal with wyde characters might need to be
changed on a system that is little-endian; the author wishes
good luck to whoever has to do this.
\MMIX\ is always big-endian, but external files
prepared on random operating systems might be backwards.
@^little-endian versus big-endian@>
@^big-endian versus little-endian@>
@^system dependencies@>
@<Sub...@>=
octa mmix_fgetws @,@,@[ARGS((unsigned char,octa,octa))@];@+@t}\6{@>
octa mmix_fgetws(handle,buffer,size)
unsigned char handle;
octa buffer,size;
{
char buf[256];
register int n,s;
register char *p;
octa o;
int eof=0;
if (!(sfile[handle].mode&0x1)) return neg_one;
if (!size.l && !size.h) return neg_one;
if (sfile[handle].mode&0x8) sfile[handle].mode &=~ 0x2;
buffer.l&=-2;
size=incr(size,-1);
o=zero_octa;
while (1) {
@<Read |n<128| wyde characters into |buf|@>;
mmputchars((unsigned char*)buf,2*n+2,buffer);
o=incr(o,n);
size=incr(size,-n);
if ((n&&buf[2*n-1]=='\n'&&buf[2*n-2]==0) || (!size.l&&!size.h) || eof)
return o;
buffer=incr(buffer,2*n);
}
}
@ @<Read |n<128| wyde characters into |buf|@>=
s=127;
if (size.l<s && !size.h) s=size.l;
if (sfile[handle].fp==stdin)
for (p=buf,n=0;n<s;) {
*p++=stdin_chr();@+*p++=stdin_chr();
n++;
if (*(p-1)=='\n' && *(p-2)==0) break;
}
else for (p=buf,n=0;n<s;) {
if (fread(p,1,2,sfile[handle].fp)!=2) {
eof=feof(sfile[handle].fp);
if (!eof) return neg_one;
break;
}
n++,p+=2;
if (*(p-1)=='\n' && *(p-2)==0) break;
}
*p=*(p+1)='\0';
@ @<Sub...@>=
octa mmix_fwrite @,@,@[ARGS((unsigned char,octa,octa))@];@+@t}\6{@>
octa mmix_fwrite(handle,buffer,size)
unsigned char handle;
octa buffer,size;
{
char buf[256];
register int n;
if (!(sfile[handle].mode&0x2)) return ominus(zero_octa,size);
if (sfile[handle].mode&0x8) sfile[handle].mode &=~ 0x1;
while (1) {
if (size.h || size.l>=256) n=mmgetchars(buf,256,buffer,-1);
else n=mmgetchars(buf,size.l,buffer,-1);
size=incr(size,-n);
if (fwrite(buf,1,n,sfile[handle].fp)!=n) return ominus(zero_octa,size);
fflush(sfile[handle].fp);
if (!size.l && !size.h) return zero_octa;
buffer=incr(buffer,n);
}
}
@ @<Sub...@>=
octa mmix_fputs @,@,@[ARGS((unsigned char,octa))@];@+@t}\6{@>
octa mmix_fputs(handle,string)
unsigned char handle;
octa string;
{
char buf[256];
register int n;
octa o;
o=zero_octa;
if (!(sfile[handle].mode&0x2)) return neg_one;
if (sfile[handle].mode&0x8) sfile[handle].mode &=~ 0x1;
while (1) {
n=mmgetchars(buf,256,string,0);
if (fwrite(buf,1,n,sfile[handle].fp)!=n) return neg_one;
o=incr(o,n);
if (n<256) {
fflush(sfile[handle].fp);
return o;
}
string=incr(string,n);
}
}
@ @<Sub...@>=
octa mmix_fputws @,@,@[ARGS((unsigned char,octa))@];@+@t}\6{@>
octa mmix_fputws(handle,string)
unsigned char handle;
octa string;
{
char buf[256];
register int n;
octa o;
o=zero_octa;
if (!(sfile[handle].mode&0x2)) return neg_one;
if (sfile[handle].mode&0x8) sfile[handle].mode &=~ 0x1;
while (1) {
n=mmgetchars(buf,256,string,1);
if (fwrite(buf,1,n,sfile[handle].fp)!=n) return neg_one;
o=incr(o,n>>1);
if (n<256) {
fflush(sfile[handle].fp);
return o;
}
string=incr(string,n);
}
}
@ @d sign_bit ((unsigned)0x80000000)
@<Sub...@>=
octa mmix_fseek @,@,@[ARGS((unsigned char,octa))@];@+@t}\6{@>
octa mmix_fseek(handle,offset)
unsigned char handle;
octa offset;
{
if (!(sfile[handle].mode&0x4)) return neg_one;
if (sfile[handle].mode&0x8) sfile[handle].mode = 0xf;
if (offset.h&sign_bit) {
if (offset.h!=0xffffffff || !(offset.l&sign_bit)) return neg_one;
if (fseek(sfile[handle].fp,(int)offset.l+1,SEEK_END)!=0) return neg_one;
}@+else {
if (offset.h || (offset.l&sign_bit)) return neg_one;
if (fseek(sfile[handle].fp,(int)offset.l,SEEK_SET)!=0) return neg_one;
}
return zero_octa;
}
@ @<Sub...@>=
octa mmix_ftell @,@,@[ARGS((unsigned char))@];@+@t}\6{@>
octa mmix_ftell(handle)
unsigned char handle;
{
register long x;
octa o;
if (!(sfile[handle].mode&0x4)) return neg_one;
x=ftell(sfile[handle].fp);
if (x<0) return neg_one;
o.h=0, o.l=x;
return o;
}
@ One last subroutine belongs here, just in case the user has
modified the standard error handle.
@<Sub...@>=
void print_trip_warning @,@,@[ARGS((int,octa))@];@+@t}\6{@>
void print_trip_warning(n,loc)
int n;
octa loc;
{
if (sfile[2].mode&0x2)
fprintf(sfile[2].fp,"Warning: %s at location %08x%08x\n",
trip_warning[n],loc.h,loc.l);
}
@ @<Glob...@>=
char *trip_warning[]={
"TRIP",
"integer divide check",
"integer overflow",
"float-to-fix overflow",
"invalid floating point operation",
"floating point overflow",
"floating point underflow",
"floating point division by zero",
"floating point inexact"};
@* Index.