mmixlib.ch 57.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Types and preprocessor macros go into libtype.h

@x
@* Basics. To get started, we define a type that provides semantic sugar.
@y
@ @(libtype.h@>=
@h
@<Preprocessor macros@>@;
@<Type declarations@>@;

@* Basics. To get started, we define a type that provides semantic sugar.
@z

@x
@ @<Sub...@>=
void print_hex @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@ @(libprint.c@>=
#include <stdio.h>
#include "libconfig.h"
22
23
24
25
26
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"

27
28
29
30
31
32
33
#include "libarith.h"

void print_hex @,@,@[ARGS((octa))@];@+@t}\6{@>
@z


The external definitions of mmix-arith
34
should also go into a header file.
35
36
37
38
39
40
41
42
43
44
45
46
47
48

@x
@<Sub...@>=
extern octa zero_octa; /* |zero_octa.h=zero_octa.l=0| */
@y
@(libarith.h@>=
extern octa zero_octa; /* |zero_octa.h=zero_octa.l=0| */
@z

Fatal errors need to be handled but panic moves to libconfig.h

@x
@d panic(m) {@+fprintf(stderr,"Panic: %s!\n",m);@+exit(-2);@+}
@y
49
@d panic(m) {@+MMIX_ERROR("Panic: %s!\n",m);@+longjmp(mmix_exit,-2);@+}
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@z

@x
@<Sub...@>=
void print_int @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@(libprint.c@>=
void print_int @,@,@[ARGS((octa))@];@+@t}\6{@>
@z

The tet field of the mem_tetra may be eliminated.

@x
  tetra tet; /* the tetrabyte of simulated memory */
@y
65
  MMIX_MEM_TET
66
67
68
69
70
71
72
73
74
75
@z

@x
@<Sub...@>=
mem_node* new_mem @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libmem.c@>=
#include <stdlib.h>
#include <stdio.h>
#include "libconfig.h"
76
77
78
79
80
81
82
83
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"


#include <setjmp.h>
extern jmp_buf mmix_exit;
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

mem_node* new_mem @,@,@[ARGS((void))@];@+@t}\6{@>
@z

@x
@<Initialize...@>=
mem_root=new_mem();
mem_root->loc.h=0x40000000;
last_mem=mem_root;
@y
@<Set up persistent data@>=
mem_root=new_mem();
mem_root->loc.h=0x40000000;
last_mem=mem_root;
@z

@x
@<Sub...@>=
mem_tetra* mem_find @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@(libmem.c@>=
mem_tetra* mem_find @,@,@[ARGS((octa))@];@+@t}\6{@>
@z

Defining the macro mm conflicts with other macros when using MS Visual C

@x
@d mm 0x98 /* the escape code of \.{mmo} format */
@y
@d mmo_esc 0x98 /* the escape code of \.{mmo} format */
@z

@x
@<Initialize everything@>=
mmo_file=fopen(mmo_file_name,"rb");
@y
@<Load object file@>=
mmo_file=fopen(mmo_file_name,"rb");
@z

124
125
This is simpler and uses the predefined panic subroutine.

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
@x
    fprintf(stderr,"Can't open the object file %s or %s!\n",
@.Can't open...@>
               mmo_file_name,alt_name);
    exit(-3);
@y
   panic("Can't open the object file");
@z


@x
@d mmo_err {
     fprintf(stderr,"Bad object file! (Try running MMOtype.)\n");
@.Bad object file@>
     exit(-4);
   }
@y   
@d mmo_err {panic("Bad object file! (Try running MMOtype.)");}
@z

@x
@<Sub...@>=
void read_tet @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libload.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libconfig.h"
155
156
157
158
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
159
#include "libarith.h"
160
161
162
163
164
#include "libname.h"

#include <setjmp.h>
extern jmp_buf mmix_exit;

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

@<Loading subroutines@>@;

@ @<Loading subroutines@>=
void read_tet @,@,@[ARGS((void))@];@+@t}\6{@>
@z

@x
@ @<Sub...@>=
byte read_byte @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@ @<Loading subroutines@>=
byte read_byte @,@,@[ARGS((void))@];@+@t}\6{@>
@z

@x
if (buf[0]!=mm || buf[1]!=lop_pre) mmo_err;
@y
if (buf[0]!=mmo_esc || buf[1]!=lop_pre) mmo_err;
@z

@x
 loop:@+if (buf[0]==mm) switch (buf[1]) {
@y
 loop:@+if (buf[0]==mmo_esc) switch (buf[1]) {
@z

@x
@d mmo_load(loc,val) ll=mem_find(loc), ll->tet^=val
@y
@d mmo_load(loc,val) ll=load_mem_tetra(loc,val)

@(libload.c@>=
static mem_tetra *load_mem_tetra(octa loc, tetra val)  
{ octa x; 
  mem_tetra *ll=mem_find(loc);           
  load_tetra(&x,loc);     
  x.l = x.l^val;             
  if (!store_tetra(x,loc))  
  panic("Unable to store mmo file to RAM");
  return ll;
}

@ This function is used here:

@z

before we increment the line number,
we reset the frequency.

@x
    cur_line++;
@y
    ll->freq=0;
    cur_line++;
@z

The next lines of code complete loading the object file.

@x
@ @<Initialize...@>=
cur_loc.h=cur_loc.l=0;
@y
@ @<Load object file@>=
cur_loc.h=cur_loc.l=0;
@z

we have to map the file number stored in
the mmo file as the ybyte to the 
filenumbers used inside the library.

First the case of known files.

@x
case lop_file:@+if (file_info[ybyte].name) {
   if (zbyte) mmo_err;
   cur_file=ybyte;
@y
case lop_file:
   if (ybyte2file_no[ybyte]>=0) {
   if (zbyte) mmo_err;
   cur_file=ybyte2file_no[ybyte];
@z

Now we handle new files.

@x
 }@+else {
   if (!zbyte) mmo_err;
   file_info[ybyte].name=(char*)calloc(4*zbyte+1,1);
   if (!file_info[ybyte].name) {
     fprintf(stderr,"No room to store the file name!\n");@+exit(-5);
@.No room...@>
   }
   cur_file=ybyte;
   for (j=zbyte,p=file_info[ybyte].name; j>0; j--,p+=4) {
     read_tet();
     *p=buf[0];@+*(p+1)=buf[1];@+*(p+2)=buf[2];@+*(p+3)=buf[3];
   }
 }
@y
 }@+else {
   char *name;
   if (!zbyte) mmo_err;
   name=(char*)calloc(4*zbyte+1,1);
   if (!name) {
     panic("No room to store the file name!\n");
@.No room...@>
   }   
   for (j=zbyte,p=name; j>0; j--,p+=4) {
     read_tet();
     *p=buf[0];@+*(p+1)=buf[1];@+*(p+2)=buf[2];@+*(p+3)=buf[3];
   }
   cur_file=filename2file(name,ybyte);
   ybyte2file_no[ybyte]=cur_file;
   file_info[cur_file].name=name;
 }
@z


@x
   if (buf[0]==mm) {
@y
   if (buf[0]==mmo_esc) {
@z

291
We load the postamble into the beginning
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
of segment~3, also known as \.{Stack\_Segment}).
The stack segment is set up to be used with an unsave instruction.
On the stack, we have, the local registers (argc and argv) and the value of rL, then the global 
registers and the special registers rB, rD, rE, rH, rJ, rM, rR, rP, rW, rX, rY, and rZ,
followed by rG and rA packed into eight byte.

@x
@<Load the postamble@>=
aux.h=0x60000000;@+ aux.l=0x18;
ll=mem_find(aux);
(ll-1)->tet=2; /* this will ultimately set |rL=2| */
(ll-5)->tet=argc; /* and $\$0=|argc|$ */
(ll-4)->tet=0x40000000;
(ll-3)->tet=0x8; /* and $\$1=\.{Pool\_Segment}+8$ */
G=zbyte;@+ L=0;@+ O=0;
for (j=G+G;j<256+256;j++,ll++,aux.l+=4) read_tet(), ll->tet=tet;
inst_ptr.h=(ll-2)->tet, inst_ptr.l=(ll-1)->tet; /* \.{Main} */
(ll+2*12)->tet=G<<24;
g[255]=incr(aux,12*8); /* we will \.{UNSAVE} from here, to get going */
@y
@<Load the postamble@>=
{ octa x;
  aux.h=0x60000000;
  x.h=0;@+x.l=0;@+aux.l=0x00;
  if (!store_octa(x,aux)) /* $\$0=|argc|$ */
     panic("Unable to store mmo file to RAM");
  x.h=0x40000000;@+x.l=0x8;@+aux.l=0x08;
  if (!store_octa(x,aux)) /* and $\$1=\.{Pool\_Segment}+8$ */
     panic("Unable to store mmo file to RAM");
  x.h=0;@+x.l=2;@+aux.l=0x10;
  if (!store_octa(x,aux)) /* this will ultimately set |rL=2| */
     panic("Unable to store mmo file to RAM");
  G=zbyte;@+ L=0;@+ O=0;
  aux.l=0x18;
  for (j=G;j<256;j++,aux.l+=8) 
  { read_tet(); x.h=tet;
    read_tet(), x.l=tet;
    if (!store_octa(x,aux))
       panic("Unable to store mmo file to RAM");
  }
332
333
  aux=incr(aux,12*8); /* we can |UNSAVE| from here, to get going */
#ifdef MMIX_BOOT
334
  g[rWW] = x;  /* last octa stored is address of \.{Main} */
335
336
337
338
339
340
341
342
  g[rBB] = aux;
  g[rXX].h = 0; g[rXX].l = ((tetra)UNSAVE<<24)+255; /* \.{UNSAVE} \$255 */
  rzz = 1;
#else
  inst_ptr = x;
  g[255] = aux;
  rzz = 0;  /* pretend \.{RESUME} 0 */
#endif
343
344
345
346
347
348
349
350
//  if (interacting) set_break(x,exec_bit);
  x.h=G<<24; x.l=0 /* rA */; 
  if (!store_octa(x,aux))
     panic("Unable to store mmo file to RAM");
  G=g[rG].l; /* restore G to rG because it was changed above */
}
@z

351
Because mmo files have their own notion of
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
file numbers, we have to map mmo file numbers
to file numbers used in mem and in file_nodes.

@x
file_node file_info[256]; /* data about each source file */
@y
file_node file_info[256]; /* data about each source file */
int ybyte2file_no[256]; /* mapping internal to external files */
@z


@x
@<Initialize...@>=
if (buf_size<72) buf_size=72;
buffer=(Char*)calloc(buf_size+1,sizeof(Char));
if (!buffer) panic("Can't allocate source line buffer");
@.Can't allocate...@>
@y
@<Set up persistent data@>=
if (buf_size<72) buf_size=72;
buffer=(Char*)calloc(buf_size+1,sizeof(Char));
if (!buffer) return -1;
@z

@x
@<Sub...@>=
void make_map @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libshowline.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "libconfig.h"
387
388
389
390
391
392
393
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"

#include <setjmp.h>
extern jmp_buf mmix_exit;
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

void make_map @,@,@[ARGS((void))@];@+@t}\6{@>
@z

these include files are needed only in libshowline.c

@x
@<Preprocessor macros@>=
@y
@<Showline macros@>=
@z

@x
@<Sub...@>=
void print_line @,@,@[ARGS((int))@];@+@t}\6{@>
void print_line(k)
  int k;
@y
@(libshowline.c@>=
void print_line(int k)
@z


@x
@ @<Preprocessor macros@>=
@y
@ @<Showline macros@>=
@z

@x
@<Sub...@>=
void show_line @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libshowline.c@>=
428
void show_line @,@,@[ARGS((void))@];@+@t}\6{@>
429
430
431
432
433
434
435
436
437
@z

@x
@<Sub...@>=
void print_freqs @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@y
@(libprofile.c@>=
#include <stdio.h>
#include "libconfig.h"
438
439
440
441
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
442
443
444
445
446
#include "libarith.h"

void print_freqs @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@z

447
448
We need ll as a local variable.

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
@x
  octa cur_loc;
@y
  octa cur_loc;
  mem_tetra *ll;
@z

@x
 loc_implied: printf("%10d. %08x%08x: %08x (%s)\n",
      p->dat[j].freq, cur_loc.h, cur_loc.l, p->dat[j].tet,
      info[p->dat[j].tet>>24].name);
@y
 loc_implied:
 { tetra inst; 
   load_instruction(&inst,cur_loc);
   printf("%10d. %08x%08x: %08x (%s)\n",
      p->dat[j].freq, cur_loc.h, cur_loc.l, inst,
      info[inst>>24].name);
 }
@z

For the mmixlib, we split performing the
instruction in three parts: 
resuming,fetching, and executing.

@x
@<Perform one instruction@>=
{
  if (resuming) loc=incr(inst_ptr,-4), inst=g[rX].l;
  else @<Fetch the next instruction@>;
@y
@<Perform one instruction@>=
{
  if (resuming)
  { loc=incr(inst_ptr,-4), inst=g[rzz?rXX:rX].l;
    if (rzz==0) /* RESUME 0 */
    { if ((loc.h&sign_bit) != (inst_ptr.h&sign_bit))
      { resuming = false;
        goto protection_violation;
      }
      @<Check for security violation@>
    }
  }
@z

This restriction is no longer necessary.

@x
  if (loc.h>=0x20000000) goto privileged_inst;
@y
@z

the next two lines are not a propper part of
performing an instruction and move to a separate 
function respectively to the main loop.
But we check for traps.
@x
  @<Trace the current instruction, if requested@>;
  if (resuming && op!=RESUME) resuming=false;
@y
@z

@x
int rop; /* ropcode of a resumed instruction */
@y
int rop; /* ropcode of a resumed instruction */
int rzz; /* Z field of a resumed instruction */
@z

@x
bool interacting; /* are we in interactive mode? */
@y
bool interacting; /* are we in interactive mode? */
bool show_operating_system = false; /* do we show negative addresses */
bool trace_once=false;
bool rw_break=false;
octa rOlimit={-1,-1}; /* tracing and break only if g[rO]<=rOlimit */
bool interact_after_resume = false;
@z

We make some more variables global.

@x
@ @<Local...@>=
register mmix_opcode op; /* operation code of the current instruction */
register int xx,yy,zz,yz; /* operand fields of the current instruction */
register tetra f; /* properties of the current |op| */
@y
@ @<Glob...@>=
mmix_opcode op; /* operation code of the current instruction */
int xx,yy,zz,yz; /* operand fields of the current instruction */
540
tetra f; /* properties of the current |op| */
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

@ @<Local...@>=
@z

and p is no longer needed in performing an instruction.

@x
register char *p; /* current place in a string */
@y
@z


@x
@ @<Fetch the next instruction@>=
{
  loc=inst_ptr;
  ll=mem_find(loc);
  inst=ll->tet;
  cur_file=ll->file_no;
  cur_line=ll->line_no;
  ll->freq++;
  if (ll->bkpt&exec_bit) breakpoint=true;
  tracing=breakpoint||(ll->bkpt&trace_bit)||(ll->freq<=trace_threshold);
  inst_ptr=incr(inst_ptr,4);
}
@y
@ @<Fetch the next instruction@>=
{ loc=inst_ptr;
  ll=mem_find(loc);
570
  inst=SWYM<<24; /* default SWYM */
571
572
573
574
575
576
577
578
579
580
581
582
583
584
  cur_file=ll->file_no;
  cur_line=ll->line_no;
  ll->freq++;
  if (ll->bkpt&exec_bit) breakpoint=true;
  tracing=breakpoint||(ll->bkpt&trace_bit)||(ll->freq<=trace_threshold);
  @<Check for security violation@>
  if(!load_instruction(&inst,loc)) 
    goto page_fault;
  inst_ptr=incr(inst_ptr,4);
  if ((inst_ptr.h&sign_bit) && !(loc.h&sign_bit))
    goto protection_violation;
}
@z

585
We change how to display certain instructions.
586
587
588

@x
{"RESUME",0x00,0,0,5,"{%#b} -> %#z"},@|
589
590
591
592
593
@y
{"RESUME",0x00,0,0,5,"{%#b}, $255 = %x, -> %#z"},@|
@z

@x
594
595
596
597
598
599
600
{"SYNC",0x01,0,0,1,""},@|
{"SWYM",0x00,0,0,1,""},@|
@y
{"SYNC",0x01,0,0,1,"%z"},@|
{"SWYM",0x01,0,0,1,"%r"},@|
@z

601
L,G, and O are made global.
602
603
604
605
606
607
608
609
610

@x
@ @<Local...@>=
register int G,L,O; /* accessible copies of key registers */
@y
@ @<Glob...@>=
int G=255,L=0,O=0; /* accessible copies of key registers */
@z

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
Initialization needs to be done at each reboot.

@x
@<Initialize...@>=
g[rK]=neg_one;
g[rN].h=(VERSION<<24)+(SUBVERSION<<16)+(SUBSUBVERSION<<8);
g[rN].l=ABSTIME; /* see comment and warning above */
g[rT].h=0x80000005;
g[rTT].h=0x80000006;
g[rV].h=0x369c2004;
if (lring_size<256) lring_size=256;
lring_mask=lring_size-1;
if (lring_size&lring_mask)
  panic("The number of local registers must be a power of 2");
@.The number of local...@>
l=(octa*)calloc(lring_size,sizeof(octa));
if (!l) panic("No room for the local registers");
@.No room...@>
cur_round=ROUND_NEAR;
@y
@<Set up persistent data@>=
if (lring_size<256) lring_size=256;
lring_mask=lring_size-1;
if (lring_size&lring_mask) return -1;
l=(octa*)calloc(lring_size,sizeof(octa));
if (!l) return -1;

@ @<Initialize...@>=
sclock.l=sclock.h=0;
profile_started=false;
halted=false;
stdin_buf_start=stdin_buf_end=NULL;
good_guesses=bad_guesses=0;
profiling=false;
interrupt=false;

@ @<Boot the machine@>=
memset(l,0,lring_size*sizeof(octa));
memset(g,0,sizeof(g));
L=O=S=0;
G=g[rG].l=255;
653
654
655
656
657
#if MMIX_BOOT
g[rK] = zero_octa;
#else
g[rK] = neg_one;
#endif
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
g[rN].h=(VERSION<<24)+(SUBVERSION<<16)+(SUBSUBVERSION<<8);
g[rN].l=ABSTIME; /* see comment and warning above */
g[rT].h=0x80000000;g[rT].l=0x00000000;
g[rTT].h=0x80000000;g[rTT].l=0x00000000;
g[rV].h=0x12340D00;
g[rV].l=0x00002000;
cur_round=ROUND_NEAR;
@z

@x
  if (((S-O-L)&lring_mask)==0) stack_store();
@y
  if (((S-O-L)&lring_mask)==0) stack_store(l[S&lring_mask]);
@z

@x
@d test_store_bkpt(ll) if ((ll)->bkpt&write_bit) breakpoint=tracing=true
@y
@d test_store_bkpt(ll) if ((ll)->bkpt&write_bit) rw_break=breakpoint=tracing=true
@z


stack_store must implement the rC register.

@x
@<Sub...@>=
void stack_store @,@,@[ARGS((void))@];@+@t}\6{@>
void stack_store()
{
  register mem_tetra *ll=mem_find(g[rS]);
  register int k=S&lring_mask;
  ll->tet=l[k].h;@+test_store_bkpt(ll);
  (ll+1)->tet=l[k].l;@+test_store_bkpt(ll+1);
  if (stack_tracing) {
    tracing=true;
    if (cur_line) show_line();
    printf("             M8[#%08x%08x]=l[%d]=#%08x%08x, rS+=8\n",
              g[rS].h,g[rS].l,k,l[k].h,l[k].l);
  }
  g[rS]=incr(g[rS],8),  S++;
}
@y
@<Stack store@>=
void stack_store @,@,@[ARGS((octa))@];@+@t}\6{@>
void stack_store(x)
  octa x;
{ unsigned int pw_bit, new_pw_bit;
  mem_tetra *ll;
  pw_bit=g[rQ].h&PW_BIT;
  new_pw_bit=new_Q.h&PW_BIT;
  if(!store_octa(x,g[rS])) /* implementing the rC register */
  {   /* set CP_BIT */
      g[rQ].l |= CP_BIT;
      new_Q.l |= CP_BIT;
      if (g[rC].l&0x02)  /* Write bit */
      {  int s;
         octa address, base, offset,mask;
         mask.h=mask.l=0xFFFFFFFF;
         g[rQ].h &=~PW_BIT;  /* restore PW_BIT */
         new_Q.h &=~PW_BIT;
         g[rQ].h |= pw_bit;
         new_Q.h |= new_pw_bit;
         s    = (g[rV].h>>8)&0xFF;  /* extract the page size from rV */
         mask = shift_left(mask,s);
         offset.h = g[rS].h&~mask.h,offset.l = g[rS].l&~mask.l;
         mask.h &= 0x0000FFFF;     /* reduce mask to 48 bits */
         base.h = g[rC].h&mask.h,base.l = g[rC].l&mask.l;
         address.h=base.h|offset.h,address.l=base.l|offset.l;
         store_octa(x,address);
      }
  }
  ll=mem_find(g[rS]);
  test_store_bkpt(ll);
  test_store_bkpt(ll+1);
  if (stack_tracing) {
    tracing=true;
    printf("             M8[#%08x%08x]=#%08x%08x, rS+=8\n",
              g[rS].h,g[rS].l,x.h,x.l);
  }
  g[rS]=incr(g[rS],8),  S++;
}

@ @<Sub...@>=
@<Stack store@>@;
@z

@x
@d test_load_bkpt(ll) if ((ll)->bkpt&read_bit) breakpoint=tracing=true
@y
@d test_load_bkpt(ll) if ((ll)->bkpt&read_bit) rw_break=breakpoint=tracing=true
@z

Same with stack load.
@x
@<Sub...@>=
void stack_load @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@<Sub...@>=
@<Stack load@>@;

@ @<Stack load@>=
void stack_load @,@,@[ARGS((void))@];@+@t}\6{@>
@z

@x
  l[k].h=ll->tet;@+test_load_bkpt(ll);
  l[k].l=(ll+1)->tet;@+test_load_bkpt(ll+1);
@y
  test_load_bkpt(ll);@+test_load_bkpt(ll+1);
  load_octa(&l[k],g[rS]);
@z

@x
    if (cur_line) show_line();
@y
@z

@x
@<Sub...@>=
int register_truth @,@,@[ARGS((octa,mmix_opcode))@];@+@t}\6{@>
@y
@<Sub...@>=
@<Register truth@>@;

@ @<Register truth@>=
int register_truth @,@,@[ARGS((octa,mmix_opcode))@];@+@t}\6{@>
@z

@x
   inst_ptr=z;
@y
   if ((z.h&sign_bit) && !(loc.h&sign_bit))
   goto protection_violation;
   inst_ptr=z;
@z


@x
796
797
798
799
800
801
802
case LDB: case LDBI: case LDBU: case LDBUI:@/
 i=56;@+j=(w.l&0x3)<<3; goto fin_ld;
case LDW: case LDWI: case LDWU: case LDWUI:@/
 i=48;@+j=(w.l&0x2)<<3; goto fin_ld;
case LDT: case LDTI: case LDTU: case LDTUI:@/
 i=32;@+j=0;@+ goto fin_ld;
case LDHT: case LDHTI: i=j=0;
803
804
805
806
807
fin_ld: ll=mem_find(w);@+test_load_bkpt(ll);
 x.h=ll->tet;
 x=shift_right(shift_left(x,j),i,op&0x2);
check_ld:@+if (w.h&sign_bit) goto privileged_inst; 
@y
808
809
810
811
812
813
814
815
case LDB: case LDBI: case LDBU: case LDBUI:@/
 i=56;@+j=56;@+if(!load_byte(&x,w)) goto page_fault; goto fin_ld;
case LDW: case LDWI: case LDWU: case LDWUI:@/
 i=48;@+j=48;@+if(!load_wyde(&x,w)) goto page_fault; goto fin_ld;
case LDT: case LDTI: case LDTU: case LDTUI:@/
 i=32;@+j=32;@+if(!load_tetra(&x,w)) goto page_fault; goto fin_ld;
case LDHT: case LDHTI: i=j=0;@+if(!load_tetra(&x,w)) goto page_fault;
x.h=x.l;x.l=0;
816
fin_ld: ll=mem_find(w);@+test_load_bkpt(ll);
817
 if (op&0x2) x=shift_right(shift_left(x,i),i,op&0x2);
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
check_ld:@+ if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
 goto store_x;
page_fault:
 if ((g[rK].h & g[rQ].h) != 0 || (g[rK].l & g[rQ].l) != 0) 
 { x.h=0, x.l=inst;
   y = w;
   z = zero_octa;
   @<Initiate a trap interrupt@>
   inst_ptr=y=g[rTT];
 }
 break;
@z

@x
case LDO: case LDOI: case LDOU: case LDOUI: case LDUNC: case LDUNCI:
 w.l&=-8;@+ ll=mem_find(w);
 test_load_bkpt(ll);@+test_load_bkpt(ll+1);
 x.h=ll->tet;@+ x.l=(ll+1)->tet;
 goto check_ld;
case LDSF: case LDSFI: ll=mem_find(w);@+test_load_bkpt(ll);
 x=load_sf(ll->tet);@+ goto check_ld;
@y
case LDO: case LDOI: case LDOU: case LDOUI: 
 w.l&=-8;@+ ll=mem_find(w);
 test_load_bkpt(ll);@+test_load_bkpt(ll+1);
 if (!load_octa(&x,w)) goto page_fault;
 goto check_ld;
case LDUNC: case LDUNCI:
 w.l&=-8;@+ ll=mem_find(w);
 test_load_bkpt(ll);@+test_load_bkpt(ll+1);
 if (!load_octa_uncached(&x,w)) goto page_fault;
 goto check_ld;
case LDSF: case LDSFI: ll=mem_find(w);@+test_load_bkpt(ll);
 if (!load_tetra(&x,w)) goto page_fault;
 x=load_sf(x.l);@+ goto check_ld;
@z


@x
case STB: case STBI: case STBU: case STBUI:@/
 i=56;@+j=(w.l&0x3)<<3; goto fin_pst;
859
860
861
862
863
864
865
866
867
868
869
870
871
@y
case STB: case STBI: case STBU: case STBUI:@/
 if ((op&0x2)==0) {
   a=shift_right(shift_left(b,56),56,0);
   if (a.h!=b.h || a.l!=b.l) exc|=V_BIT;
 }
 if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
 if (!store_byte(b,w)) goto page_fault;
 ll=mem_find(w); test_store_bkpt(ll);
 break;
@z

@x
872
873
case STW: case STWI: case STWU: case STWUI:@/
 i=48;@+j=(w.l&0x2)<<3; goto fin_pst;
874
875
876
877
878
879
880
881
882
883
884
885
886
@y
case STW: case STWI: case STWU: case STWUI:@/
 if ((op&0x2)==0) {
   a=shift_right(shift_left(b,48),48,0);
   if (a.h!=b.h || a.l!=b.l) exc|=V_BIT;
 }
 if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
 if (!store_wyde(b,w)) goto page_fault;
 ll=mem_find(w); test_store_bkpt(ll);
 break;
@z

@x
887
888
889
890
891
892
893
894
895
case STT: case STTI: case STTU: case STTUI:@/
 i=32;@+j=0;
fin_pst: ll=mem_find(w);
 if ((op&0x2)==0) {
   a=shift_right(shift_left(b,i),i,0);
   if (a.h!=b.h || a.l!=b.l) exc|=V_BIT;
 }
 ll->tet^=(ll->tet^(b.l<<(i-32-j))) & ((((tetra)-1)<<(i-32))>>j);
 goto fin_st;
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
@y
case STT: case STTI: case STTU: case STTUI:@/
 if ((op&0x2)==0) {
   a=shift_right(shift_left(b,32),32,0);
   if (a.h!=b.h || a.l!=b.l) exc|=V_BIT;
 }
fin_pst:
 if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
 if (!store_tetra(b,w)) goto page_fault;
 ll=mem_find(w); test_store_bkpt(ll);
 break;
@z


@x
911
912
913
case STSF: case STSFI: ll=mem_find(w);
 ll->tet=store_sf(b);@+exc=exceptions;
 goto fin_st;
914
915
916
917
918
919
920
921
@y
case STSF: case STSFI: 
 a.l=b.l= store_sf(b);@+exc=exceptions;
 a.h=b.h=0;
 goto fin_pst;
@z

@x
922
923
924
925
926
case STHT: case STHTI: ll=mem_find(w);@+ ll->tet=b.h;
fin_st: test_store_bkpt(ll);
 w.l&=-8;@+ll=mem_find(w);
 a.h=ll->tet;@+ a.l=(ll+1)->tet; /* for trace output */
 goto check_st; 
927
928
929
930
931
932
933
934
@y
case STHT: case STHTI: 
  a.l=b.l=b.h;
  b.h=a.h=0;
  goto fin_pst;
@z

@x
935
936
937
938
939
940
941
942
case STCO: case STCOI: b.l=xx;
case STO: case STOI: case STOU: case STOUI: case STUNC: case STUNCI:
 w.l&=-8;@+ll=mem_find(w);
 test_store_bkpt(ll);@+ test_store_bkpt(ll+1);
 ll->tet=b.h;@+ (ll+1)->tet=b.l;
check_st:@+if (w.h&sign_bit) goto privileged_inst;
 break;
@y
943
944
945
946
947
948
case STCO: case STCOI: b.l=xx;
case STO: case STOI: case STOU: case STOUI:
 w.l&=-8;
 if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
 if (!store_octa(b,w)) goto page_fault;
 ll=mem_find(w); test_store_bkpt(ll);test_store_bkpt(ll+1);
949
950
 break;
case STUNC: case STUNCI:
951
 w.l&=-8;
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
 if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto translation_bypassed_inst;
 if (!store_octa_uncached(b,w)) goto page_fault;
 ll=mem_find(w); test_store_bkpt(ll);test_store_bkpt(ll+1);
 break;
@z

@x
case CSWAP: case CSWAPI: w.l&=-8;@+ll=mem_find(w);
 test_load_bkpt(ll);@+test_load_bkpt(ll+1);
 a=g[rP];
 if (ll->tet==a.h && (ll+1)->tet==a.l) {
   x.h=0, x.l=1;
   test_store_bkpt(ll);@+test_store_bkpt(ll+1);
   ll->tet=b.h, (ll+1)->tet=b.l;
   strcpy(rhs,"M8[%#w]=%#b");
 }@+else {
   b.h=ll->tet, b.l=(ll+1)->tet;
   g[rP]=b;
   strcpy(rhs,"rP=%#b");
 }
@y
case CSWAP: case CSWAPI: w.l&=-8;@+ll=mem_find(w);
 test_load_bkpt(ll);@+test_load_bkpt(ll+1);
 if ((w.h&sign_bit) && !(loc.h&sign_bit)) goto  translation_bypassed_inst;
 if (!load_octa(&a,w)) goto page_fault;
 if (g[rP].h==a.h && g[rP].l==a.l) {
   x.h=0, x.l=1;
   test_store_bkpt(ll);@+test_store_bkpt(ll+1);
   if (!store_octa(b,w)) goto page_fault;
   strcpy(rhs,"M8[%#w]=%#b");
 }@+else {
   b=a;
   a = g[rP];
   g[rP]=b;
   x.h=0, x.l=0;
   strcpy(rhs,"rP=%#b");
 }
@z


@x
case GET:@+if (yy!=0 || zz>=32) goto illegal_inst;
  x=g[zz];
  goto store_x;
case PUT: case PUTI:@+ if (yy!=0 || xx>=32) goto illegal_inst;
  strcpy(rhs,"%z = %#z");
  if (xx>=8) {
    if (xx<=11 && xx!=8) goto illegal_inst; /* can't change rN, rO, rS */
    if (xx<=18) goto privileged_inst;
    if (xx==rA) @<Get ready to update rA@>@;
    else if (xx==rL) @<Set $L=z=\min(z,L)$@>@;
    else if (xx==rG) @<Get ready to update rG@>;
  }
  g[xx]=z;@+zz=xx;@+break;
@y
case GET:@+if (yy!=0 || zz>=32) goto illegal_inst;
  x=g[zz];
  if (zz==rQ) { 
      new_Q.h = new_Q.l = 0;
  }
  goto store_x;
case PUT: case PUTI:@+ if (yy!=0 || xx>=32) goto illegal_inst;
  strcpy(rhs,"%z = %#z");
  if (xx>=8) {
    if (xx==9) goto illegal_inst; /* can't change rN */
    if (xx<=18 && !(loc.h&sign_bit)) goto privileged_inst;
    if (xx==rA) @<Get ready to update rA@>@;
    else if (xx==rL) @<Set $L=z=\min(z,L)$@>@;
    else if (xx==rG) @<Get ready to update rG@>@;
    else if (xx==rQ)
    { new_Q.h |= z.h &~ g[rQ].h;@+
      new_Q.l |= z.l &~ g[rQ].l;
      z.l |= new_Q.l;@+
      z.h |= new_Q.h;@+
    }
  }
  g[xx]=z;@+zz=xx;@+break;
@z

@x
case PUSHGO: case PUSHGOI: inst_ptr=w;@+goto push;
case PUSHJ: case PUSHJB: inst_ptr=z;
push:@+if (xx>=G) {
   xx=L++;
   if (((S-O-L)&lring_mask)==0) stack_store();
 }
@y
case PUSHGO: case PUSHGOI: 
if ((w.h&sign_bit) && !(loc.h&sign_bit))
goto protection_violation;
inst_ptr=w;@+goto push;
case PUSHJ: case PUSHJB: 
if ((z.h&sign_bit) && !(loc.h&sign_bit))
goto protection_violation;  
inst_ptr=z;
push:@+if (xx>=G) {
   xx=L++;
   if (((S-O-L)&lring_mask)==0) stack_store(l[S&lring_mask]);
 }
@z

@x
 y=g[rJ];@+ z.l=yz<<2;@+ inst_ptr=oplus(y,z);
@y
 y=g[rJ];@+ z.l=yz<<2;
 { octa tmp;
   tmp=oplus(y,z);
   if ((tmp.h&sign_bit) && !(loc.h&sign_bit))
   goto protection_violation;  
   inst_ptr = tmp;
}
@z

@x
case SAVE:@+if (xx<G || yy!=0 || zz!=0) goto illegal_inst;
 l[(O+L)&lring_mask].l=L, L++;
 if (((S-O-L)&lring_mask)==0) stack_store();
@y
case SAVE:@+if (xx<G || yy!=0 || zz!=0) goto illegal_inst;
 l[(O+L)&lring_mask].l=L, L++;
 if (((S-O-L)&lring_mask)==0) stack_store(l[S&lring_mask]);
@z

@x
 while (g[rO].l!=g[rS].l) stack_store();
@y
 while (g[rO].l!=g[rS].l) stack_store(l[S&lring_mask]);
@z

@x
@<Store |g[k]| in the register stack...@>=
ll=mem_find(g[rS]);
if (k==rZ+1) x.h=G<<24, x.l=g[rA].l;
else x=g[k];
ll->tet=x.h;@+test_store_bkpt(ll);
(ll+1)->tet=x.l;@+test_store_bkpt(ll+1);
if (stack_tracing) {
  tracing=true;
  if (cur_line) show_line();
  if (k>=32) printf("             M8[#%08x%08x]=g[%d]=#%08x%08x, rS+=8\n",
            g[rS].h,g[rS].l,k,x.h,x.l);
  else printf("             M8[#%08x%08x]=%s=#%08x%08x, rS+=8\n",
            g[rS].h,g[rS].l,k==rZ+1? "(rG,rA)": special_name[k],x.h,x.l);
}
S++, g[rS]=incr(g[rS],8);
@y
@<Store |g[k]| in the register stack...@>=
if (k==rZ+1) x.h=G<<24, x.l=g[rA].l;
else x=g[k];
stack_store(x);
@z

@x
@ @<Load |g[k]| from the register stack@>=
g[rS]=incr(g[rS],-8);
ll=mem_find(g[rS]);
test_load_bkpt(ll);@+test_load_bkpt(ll+1);
if (k==rZ+1) {
  x.l=G=g[rG].l=ll->tet>>24, a.l=g[rA].l=(ll+1)->tet&0x3ffff;
  if (G<32) x.l=G=g[rG].l=32;
}@+else g[k].h=ll->tet, g[k].l=(ll+1)->tet;
if (stack_tracing) {
  tracing=true;
  if (cur_line) show_line();
  if (k>=32) printf("             rS-=8, g[%d]=M8[#%08x%08x]=#%08x%08x\n",
            k,g[rS].h,g[rS].l,ll->tet,(ll+1)->tet);
  else if (k==rZ+1) printf("             (rG,rA)=M8[#%08x%08x]=#%08x%08x\n",
            g[rS].h,g[rS].l,ll->tet,(ll+1)->tet);
  else printf("             rS-=8, %s=M8[#%08x%08x]=#%08x%08x\n",
            special_name[k],g[rS].h,g[rS].l,ll->tet,(ll+1)->tet);
}
@y
@ @<Load |g[k]| from the register stack@>=
g[rS]=incr(g[rS],-8);
ll=mem_find(g[rS]);
test_load_bkpt(ll);@+test_load_bkpt(ll+1);
if (k==rZ+1) 
{ if (!load_octa(&a,g[rS])) { w=g[rS]; goto page_fault; }
  x.l=G=g[rG].l=a.h>>24;
  a.l=g[rA].l=a.l&0x3ffff;
}
else
 if (!load_octa(&(g[k]),g[rS]))  { w=g[rS]; goto page_fault; }
if (stack_tracing) {
  tracing=true;
  if (k>=32) printf("             rS-=8, g[%d]=M8[#%08x%08x]=#%08x%08x\n",
            k,g[rS].h,g[rS].l,g[k].h,g[k].l);
  else if (k==rZ+1) printf("             (rG,rA)=M8[#%08x%08x]=#%08x%08x\n",
            g[rS].h,g[rS].l,g[k].h,g[k].l);
  else printf("             rS-=8, %s=M8[#%08x%08x]=#%08x%08x\n",
            special_name[k],g[rS].h,g[rS].l,g[k].h,g[k].l);
}
@z

@x
@ The cache maintenance instructions don't affect this simulation,
because there are no caches. But if the user has invoked them, we do
provide a bit of information when tracing, indicating the scope of the
instruction.

@<Cases for ind...@>=
case SYNCID: case SYNCIDI: case PREST: case PRESTI:
case SYNCD: case SYNCDI: case PREGO: case PREGOI:
case PRELD: case PRELDI: x=incr(w,xx);@+break;
@y
@ The cache maintenance instructions do affect this simulation.

@<Cases for ind...@>=
case SYNCID: case SYNCIDI:
 delete_instruction_cache(w,xx+1);
 if (loc.h&sign_bit)
   delete_data_cache(w,xx+1);
 else
   store_data_cache(w,xx+1);
 break;
case PREST: case PRESTI: x=incr(w,xx);@+break;
case SYNCD: case SYNCDI:  
 store_data_cache(w,xx+1);
 if (loc.h&sign_bit)
   delete_data_cache(w,xx+1);
 break;
case PREGO: case PREGOI:
 prego_instruction(w,xx+1);
 break;
case PRELD: case PRELDI:
 preload_data_cache(w,xx+1);
 x=incr(w,xx);@+break;
@z

@x
case GO: case GOI: x=inst_ptr;@+inst_ptr=w;@+goto store_x;
case JMP: case JMPB: inst_ptr=z;
case SWYM: break;
case SYNC:@+if (xx!=0 || yy!=0 || zz>7) goto illegal_inst;
 if (zz<=3) break;
case LDVTS: case LDVTSI: privileged_inst: strcpy(lhs,"!privileged");
 goto break_inst;
illegal_inst: strcpy(lhs,"!illegal");
break_inst: breakpoint=tracing=true;
 if (!interacting && !interact_after_break) halted=true;
 break;
@y
case GO: case GOI: 
   if ((w.h&sign_bit) && !(loc.h&sign_bit))
   goto protection_violation;  
   x=inst_ptr;@+inst_ptr=w;@+goto store_x;
case JMP: case JMPB: 
   if ((z.h&sign_bit) && !(loc.h&sign_bit))
   goto protection_violation;  
   inst_ptr=z;@+break;
case SYNC:@+if (xx!=0 || yy!=0 || zz>7) goto illegal_inst;
/* should give a privileged instruction interrupt in case zz  >3 */
 else if (zz==4) /* power save mode */
 {  const unsigned int cycle_speed = 1000000; /* cycles per ms */
    const unsigned int max_wait = 100;
    int d, ms;
    if (g[rI].h!=0 || g[rI].l>max_wait*cycle_speed) /* large rI values */
      ms = max_wait;
    else
      ms = g[rI].l/cycle_speed;
    if (ms>0)
    {  MMIX_DELAY(ms,d); 
       g[rI]=incr(g[rI],-(ms-d)*cycle_speed);
     }
     else if (g[rI].l>1000)
        g[rI].l = g[rI].l-1000;
     else if (g[rI].l>100)
        g[rI].l = g[rI].l-100;
     else if (g[rI].l>10)
        g[rI].l = g[rI].l-10;
 }
 else if (zz==5) /* empty write buffer */
   write_all_data_cache();
 else if (zz==6) /* clear VAT cache */
 { clear_all_data_vtc();
   clear_all_instruction_vtc();
 }
 else if (zz==7) /* clear instruction and data cache */
 { clear_all_data_cache();
   clear_all_instruction_cache();
 }
 break;
case LDVTS: case LDVTSI:   
{ if (!(loc.h&sign_bit)) goto privileged_inst;
  if (w.h&sign_bit) goto illegal_inst;
  x = update_vtc(w);
  goto store_x;
}
break;
case SWYM:
 if ((inst&0xFFFFFF)!=0) 
 {   char buf[256+1];
     int n;
     strcpy(rhs,"$%x,%z");
     z.h=0, z.l=yz;
     x.h=0, x.l=xx;
     tracing=interacting;
     breakpoint=true;
     interrupt=false;
     @<Set |b| from register X@>;
     n=mmgetchars((unsigned char *)buf,256,b,0);
     buf[n]=0;
     if (n>6 && strncmp(buf,"DEBUG ",6)==0) 
     { fprintf(stdout,"\n%s!\n",buf+6);
       sprintf(rhs,"rF=#%08X%08X\n",g[rF].h, g[rF].l);
       tracing= true;
     }
 }
 else
   strcpy(rhs,"");
break;
translation_bypassed_inst: strcpy(lhs,"!absolute address");
g[rQ].h |= N_BIT; new_Q.h |= N_BIT; /* set the n bit */
 goto break_inst;
privileged_inst: strcpy(lhs,"!kernel only");
g[rQ].h |= K_BIT; new_Q.h |= K_BIT; /* set the k bit */
 goto break_inst;
illegal_inst: strcpy(lhs,"!broken");
g[rQ].h |= B_BIT; new_Q.h |= B_BIT; /* set the b bit */
 goto break_inst;
protection_violation: strcpy(lhs,"!protected");
g[rQ].h |= P_BIT; new_Q.h |= P_BIT; /* set the p bit */
 goto break_inst;
security_inst: strcpy(lhs,"!insecure");
break_inst: breakpoint=tracing=true;
 if (!interacting && !interact_after_break) halted=true;
break;
@z

@x
case TRAP:@+if (xx!=0 || yy>max_sys_call) goto privileged_inst;
@y
1284
1285
1286
#ifndef MMIX_TRAP
case TRAP:@+if (xx!=0 || yy>max_sys_call) goto privileged_inst;
@z
1287

1288
1289
1290
1291
1292
@x
 x=g[255]=g[rBB];@+break;
@y
 x=g[255]=g[rBB];@+break;
#else
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
case TRAP:@+if (xx==0 && yy<=max_sys_call) 
      { strcpy(rhs,trap_format[yy]);
        a=incr(b,8);
        @<Prepare memory arguments $|ma|={\rm M}[a]$ and $|mb|={\rm M}[b]$ if needed@>;
      }
     else strcpy(rhs, "%#x -> %#y");
 if (tracing && !show_operating_system) interact_after_resume = true;    
 x.h=sign_bit, x.l=inst;
 @<Initiate a trap interrupt@>
 inst_ptr=y=g[rT];
 break;
1304
#endif
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
@z


@x
"$255 = Fopen(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fclose(%!z) = %x",
"$255 = Fread(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fgets(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fgetws(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fwrite(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fputs(%!z,%#b) = %x",
"$255 = Fputws(%!z,%#b) = %x",
"$255 = Fseek(%!z,%b) = %x",
"$255 = Ftell(%!z) = %x"};
@y
1320
#ifdef MMIX_TRAP
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
"$255 = Fopen(%!z,M8[%#b]=%#q,M8[%#a]=%p) -> %#y",
"$255 = Fclose(%!z) -> %#y",
"$255 = Fread(%!z,M8[%#b]=%#q,M8[%#a]=%p) -> %#y",
"$255 = Fgets(%!z,M8[%#b]=%#q,M8[%#a]=%p) -> %#y",
"$255 = Fgetws(%!z,M8[%#b]=%#q,M8[%#a]=%p) -> %#y",
"$255 = Fwrite(%!z,M8[%#b]=%#q,M8[%#a]=%p) -> %#y",
"$255 = Fputs(%!z,%#b) -> %#y",
"$255 = Fputws(%!z,%#b) -> %#y",
"$255 = Fseek(%!z,%b) -> %#y",
"$255 = Ftell(%!z) -> %#y"};
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
#else
"$255 = Fopen(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fclose(%!z) = %x",
"$255 = Fread(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fgets(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fgetws(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fwrite(%!z,M8[%#b]=%#q,M8[%#a]=%p) = %x",
"$255 = Fputs(%!z,%#b) = %x",
"$255 = Fputws(%!z,%#b) = %x",
"$255 = Fseek(%!z,%b) = %x",
"$255 = Ftell(%!z) = %x"};
#endif
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
@z



@x
@ @<Prepare memory arguments...@>=
if (arg_count[yy]==3) {
  ll=mem_find(b);@+test_load_bkpt(ll);@+test_load_bkpt(ll+1);
  mb.h=ll->tet, mb.l=(ll+1)->tet;
  ll=mem_find(a);@+test_load_bkpt(ll);@+test_load_bkpt(ll+1);
  ma.h=ll->tet, ma.l=(ll+1)->tet;
}
@y
@ @<Prepare memory arguments...@>=
if (arg_count[yy]==3) {
   load_octa(&mb,b);
   load_octa(&ma,a);
}
@z

@x
@ The input/output operations invoked by \.{TRAP}s are
done by subroutines in an auxiliary program module called {\mc MMIX-IO}.
Here we need only declare those subroutines, and write three primitive
interfaces on which they depend.

@ @<Glob...@>=
@y
1371
@ @(mmix-io.h@>=
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
@z

@x
@<Sub...@>=
int mmgetchars @,@,@[ARGS((char*,int,octa,int))@];@+@t}\6{@>
int mmgetchars(buf,size,addr,stop)
  char *buf;
  int size;
  octa addr;
  int stop;
{
  register char *p;
  register int m;
  register mem_tetra *ll;
  register tetra x;
  octa a;
  for (p=buf,m=0,a=addr; m<size;) {
    ll=mem_find(a);@+test_load_bkpt(ll);
    x=ll->tet;
    if ((a.l&0x3) || m>size-4) @<Read and store one byte; |return| if done@>@;
    else @<Read and store up to four bytes; |return| if done@>@;
  }
  return size;
}
@y
@(libmmget.c@>=
#include <stdio.h>
#include "libconfig.h"
1400
1401
1402
1403
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
#include "libarith.h"

int mmgetchars(buf,size,addr,stop)
  unsigned char *buf;
  int size;
  octa addr;
  int stop;
{
  register unsigned char *p;
  register int m;
  octa x;
  octa a;
  mem_tetra *ll;
  for (p=buf,m=0,a=addr; m<size;) {
    if ((a.l&0x7) || m+8>size) @<Read and store one byte; |return| if done@>@;
    else @<Read and store eight bytes; |return| if done@>@;
  }
  return size;
}
@z

@x
@ @<Read and store one byte...@>=
{
  *p=(x>>(8*((~a.l)&0x3)))&0xff;
  if (!*p && stop>=0) {
    if (stop==0) return m;
    if ((a.l&0x1) && *(p-1)=='\0') return m-1;
  }
  p++,m++,a=incr(a,1);
}

@ @<Read and store up to four bytes...@>=
{
  *p=x>>24;
  if (!*p && (stop==0 || (stop>0 && x<0x10000))) return m;
  *(p+1)=(x>>16)&0xff;
  if (!*(p+1) && stop==0) return m+1;
  *(p+2)=(x>>8)&0xff;
  if (!*(p+2) && (stop==0 || (stop>0 && (x&0xffff)==0))) return m+2;
  *(p+3)=x&0xff;
  if (!*(p+3) && stop==0) return m+3;
  p+=4,m+=4,a=incr(a,4);
}
@y
@ @<Read and store one byte...@>=
{ load_byte(&x,a);
    *p=x.l&0xff;
  if (!*p && stop>=0) {
    if (stop==0) return m;
    if ((a.l&0x1) && *(p-1)=='\0') return m-1;
  }
  p++,m++,a=incr(a,1);
}

@ @<Read and store eight bytes...@>=
{ load_octa(&x,a);
  *p=x.h>>24;
  if (!*p && (stop==0 || (stop>0 && x.h<0x10000))) return m;
  *(p+1)=(x.h>>16)&0xff;
  if (!*(p+1) && stop==0) return m+1;
  *(p+2)=(x.h>>8)&0xff;
  if (!*(p+2) && (stop==0 || (stop>0 && (x.h&0xffff)==0))) return m+2;
  *(p+3)=x.h&0xff;
  if (!*(p+3) && stop==0) return m+3;
  p+=4,m+=4,a=incr(a,4);
  *p=x.l>>24;
  if (!*p && (stop==0 || (stop>0 && x.l<0x10000))) return m;
  *(p+1)=(x.l>>16)&0xff;
  if (!*(p+1) && stop==0) return m+1;
  *(p+2)=(x.l>>8)&0xff;
  if (!*(p+2) && (stop==0 || (stop>0 && (x.l&0xffff)==0))) return m+2;
  *(p+3)=x.l&0xff;
  if (!*(p+3) && stop==0) return m+3;
  p+=4,m+=4,a=incr(a,4);
}
@z

@x      
@ The subroutine |mmputchars(buf,size,addr)| puts |size| characters
into the simulated memory starting at address |addr|.

@<Sub...@>=
void mmputchars @,@,@[ARGS((unsigned char*,int,octa))@];@+@t}\6{@>
void mmputchars(buf,size,addr)
  unsigned char *buf;
  int size;
  octa addr;
{
  register unsigned char *p;
  register int m;
  register mem_tetra *ll;
  octa a;
  for (p=buf,m=0,a=addr; m<size;) {
    ll=mem_find(a);@+test_store_bkpt(ll);
    if ((a.l&0x3) || m>size-4) @<Load and write one byte@>@;
    else @<Load and write four bytes@>;
  }
}
@y
@ The subroutine |mmputchars(buf,size,addr)| puts |size| characters
into the simulated memory starting at address |addr|.

@(libmmput.c@>=
#include <stdio.h>
#include "libconfig.h"
1510
1511
1512
1513
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
#include "libarith.h"

void mmputchars(unsigned char *buf,int size,octa addr)
{
  register unsigned char *p;
  register int m;
  octa x;
  octa a;
  mem_tetra *ll; /* current place in the simulated memory */
  for (p=buf,m=0,a=addr; m<size;) {
    if ((a.l&0x7) || m+8>size) @<Load and write one byte@>@;
    else @<Load and write eight bytes@>;
  }
}
@z

@x
@ @<Load and write one byte@>=
{
  register int s=8*((~a.l)&0x3);
  ll->tet^=(((ll->tet>>s)^*p)&0xff)<<s;
  p++,m++,a=incr(a,1);
}

@ @<Load and write four bytes@>=
{
  ll->tet=(*p<<24)+(*(p+1)<<16)+(*(p+2)<<8)+*(p+3);
  p+=4,m+=4,a=incr(a,4);
}
@y
@ @<Load and write one byte@>=
{
  x.l=*p;
  x.h=0;
  store_byte(x,a);
  p++,m++,a=incr(a,1);
}

@ @<Load and write eight bytes@>=
{ x.h=(*p<<24)+(*(p+1)<<16)+(*(p+2)<<8)+*(p+3);
  p+=4;
  x.l=(*p<<24)+(*(p+1)<<16)+(*(p+2)<<8)+*(p+3);
  p+=4;
  store_octa(x,a);
  m+=8,a=incr(a,8);
}
@z

1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
The next function is used for mmixware ftraps

@x
@<Sub...@>=
char stdin_chr @,@,@[ARGS((void))@];@+@t}\6{@>
@y
@(libstdin.c@>=
#include <stdlib.h>
#include <stdio.h>
#include "libconfig.h"
1572
1573
1574
1575
1576
1577
1578
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"

#include <setjmp.h>
extern jmp_buf mmix_exit;
1579
1580
1581
1582
1583
1584
1585

char stdin_chr @,@,@[ARGS((void))@];@+@t}\6{@>
@z




1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
@x
@ We are finally ready for the last case.
@y
@ We do similar things for a trap interrupt.

Interrupt bits in rQ might be lost if they are set between a \.{GET}
and a~\.{PUT}. Therefore we don't allow \.{PUT} to zero out bits that
have become~1 since the most recently committed \.{GET}.

@<Glob...@>=
octa new_Q; /* when rQ increases in any bit position, so should this */

@ Now we can implement external interrupts.

@<Check for trap interrupt@>=
if (!resuming)
{ 
  MMIX_GET_INTERRUPT;
  if ((g[rK].h & g[rQ].h) != 0 || (g[rK].l & g[rQ].l) != 0) 
  { /*this is a dynamic trap */
    x.h=sign_bit, x.l=inst;
    if (tracing)
      printf("Dynamic TRAP: rQ=%08x%08x rK=%08x%08x\n",
            g[rQ].h, g[rQ].l, g[rK].h, g[rK].l);
    @<Initiate a trap interrupt@>
    inst_ptr=y=g[rTT];
  }
}

@ An instruction will not be executed if it violates the basic
security rule of \MMIX: An instruction in a nonnegative location
should not be performed unless all eight of the internal interrupts
have been enabled in the interrupt mask register~rK.
Conversely, an instruction in a negative location should not be performed
if the |P_BIT| is enabled in~rK.

The nonnegative-location case turns on the |S_BIT| of both rK and~rQ\null,
leading to an immediate interrupt.

@<Check for security violation@>=
{
  if (inst_ptr.h&sign_bit)
  { if (g[rK].h&P_BIT) 
    { g[rQ].h |= P_BIT;
      new_Q.h |= P_BIT;
      goto security_inst;
    }
  }
  else
  { if ((g[rK].h&0xff)!=0xff)
    { g[rQ].h |= S_BIT;
      new_Q.h |= S_BIT;
      g[rK].h |= S_BIT;
      goto security_inst;
    }
  }
}


@ Here are the bit codes that affect traps. The first eight
cases apply to the upper half of~rQ the next eight to the lower half.

@d P_BIT (1<<0) /* instruction in privileged location */
@d S_BIT (1<<1) /* security violation */
@d B_BIT (1<<2) /* instruction breaks the rules */
@d K_BIT (1<<3) /* instruction for kernel only */
@d N_BIT (1<<4) /* virtual translation bypassed */
@d PX_BIT (1<<5) /* permission lacking to execute from page */
@d PW_BIT (1<<6) /* permission lacking to write on page */
@d PR_BIT (1<<7) /* permission lacking to read from page */

@d PF_BIT (1<<0) /* power fail */
@d MP_BIT (1<<1) /* memory parity error */
@d NM_BIT (1<<2) /* non existent memory */
@d YY_BIT (1<<3) /* unassigned */
@d RE_BIT (1<<4) /* rebooting */
@d CP_BIT (1<<5) /* page fault */
@d PT_BIT (1<<6) /* page table error */
@d IN_BIT (1<<7) /* interval counter rI reaches zero */

@ We need this:
    
@ @<Initiate a trap interrupt@>=
 g[rWW]=inst_ptr;
 g[rXX]=x;
 g[rYY]=y;
 g[rZZ]=z;
 z.h=0, z.l=zz;
 g[rK].h = g[rK].l = 0;
 g[rBB]=g[255];
 g[255]=g[rJ];

@ We are finally ready for the last case.
@z

@x
case RESUME:@+if (xx || yy || zz) goto illegal_inst;
inst_ptr=z=g[rW];
b=g[rX];
if (!(b.h&sign_bit)) @<Prepare to perform a ropcode@>;
break;
@y
case RESUME:@+if (xx || yy) goto illegal_inst;
rzz=zz;
if ( rzz == 0)
{ if (!(loc.h&sign_bit) && (g[rW].h&sign_bit)) 
  goto protection_violation;
  inst_ptr=z=g[rW];
  b=g[rX];
}
else if ( rzz == 1)
{ 
  if (!(loc.h&sign_bit)) goto privileged_inst;
  inst_ptr=z=g[rWW];
  b=g[rXX];
  g[rK]=g[255];
  x=g[255]=g[rBB];
  @<Check for security violation@>
  if (interact_after_resume)
  { breakpoint = true;
    interact_after_resume = false;
  }
}
else goto illegal_inst;
if (!(b.h&sign_bit)) @<Prepare to perform a ropcode@>
break;
@z


@x
@d RESUME_AGAIN 0 /* repeat the command in rX as if in location $\rm rW-4$ */
@d RESUME_CONT 1 /* same, but substitute rY and rZ for operands */
@d RESUME_SET 2 /* set register \$X to rZ */
@y
@d RESUME_AGAIN 0 /* repeat the command in rX as if in location $\rm rW-4$ */
@d RESUME_CONT 1 /* same, but substitute rY and rZ for operands */
@d RESUME_SET 2 /* set register \$X to rZ */
@d RESUME_TRANS 3 /* install $\rm(rY,rZ)$ into IT-cache or DT-cache,
        then |RESUME_AGAIN| */
@z

@x
@<Prepare to perform a ropcode@>=
{
  rop=b.h>>24; /* the ropcode is the leading byte of rX */
  switch (rop) {
 case RESUME_CONT:@+if ((1<<(b.l>>28))&0x8f30) goto illegal_inst;
 case RESUME_SET: k=(b.l>>16)&0xff;
   if (k>=L && k<G) goto illegal_inst;
 case RESUME_AGAIN:@+if ((b.l>>24)==RESUME) goto illegal_inst;
   break;
 default: goto illegal_inst;
  }
  resuming=true;
}

@ @<Install special operands when resuming an interrupted operation@>=
if (rop==RESUME_SET) {
    op=ORI;
    y=g[rZ];
    z=zero_octa;
    exc=g[rX].h&0xff00;
    f=X_is_dest_bit;
}@+else { /* |RESUME_CONT| */
  y=g[rY];
  z=g[rZ];
}
@y
@<Prepare to perform a ropcode@>=
{
  rop=b.h>>24; /* the ropcode is the leading byte of rX */
  switch (rop) {
 case RESUME_CONT:@+if ((1<<(b.l>>28))&0x8f30) goto illegal_inst;
 case RESUME_SET: k=(b.l>>16)&0xff;
   if (k>=L && k<G) goto illegal_inst;
 case RESUME_AGAIN:@+if ((b.l>>24)==RESUME) goto illegal_inst;
   break;
 case RESUME_TRANS:@+if (rzz==0) goto illegal_inst;
   break;
 default: goto illegal_inst;
  }
  resuming=true;
}

@ @<Install special operands when resuming an interrupted operation@>=
if (rzz == 0)
{ if (rop==RESUME_SET) {
    op=ORI;
    y=g[rZ];
    z=zero_octa;
    exc=g[rX].h&0xff00;
    f=X_is_dest_bit;
  }@+else if (rop == RESUME_CONT) {
  y=g[rY];
  z=g[rZ];
  }
}
else
{ if (rop==RESUME_SET) {
    op=ORI;
    y=g[rZZ];
    z=zero_octa;
    exc=g[rXX].h&0xff00;
    f=X_is_dest_bit;
  } else if (rop==RESUME_TRANS)
  {  if ((b.l>>24)==SWYM) 
       store_exec_translation(&g[rYY], &g[rZZ]);
     else
       store_data_translation(&g[rYY], &g[rZZ]);
  }@+else if (rop == RESUME_CONT) {
  y=g[rYY];
  z=g[rZZ];
  }
}
@z


@x
  if (g[rI].l<=info[op].oops && g[rI].l && g[rI].h==0) tracing=breakpoint=true;
@y
  if (g[rI].l<=info[op].oops && g[rI].l && g[rI].h==0) g[rQ].l |= IN_BIT, new_Q.l |= IN_BIT; 
@z


@x
if (tracing) {
@y
if (trace_once|| (tracing && (!(loc.h&sign_bit) || show_operating_system)&&
   (g[rO].h<rOlimit.h || (g[rO].h==rOlimit.h&&g[rO].l<=rOlimit.l)))) {
   trace_once=false;
@z


@x
@<Print a stream-of-consciousness description of the instruction@>=
if (lhs[0]=='!') printf("%s instruction!\n",lhs+1); /* privileged or illegal */
@y
@<Print a stream-of-consciousness description of the instruction@>=
if (lhs[0]=='!') { printf("%s instruction!\n",lhs+1); /* privileged or illegal */
  lhs[0]='\0';
}
@z

@x
@ @<Sub...@>=
fmt_style style;
char *stream_name[]={"StdIn","StdOut","StdErr"};
@.StdIn@>
@.StdOut@>
@.StdErr@>
@#
void trace_print @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@ @(libtrace.c@>=
#include <stdio.h>
#include "libconfig.h"
1842
1843
1844
1845
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
#include "libarith.h"

static fmt_style style;
static char *stream_name[]={"StdIn","StdOut","StdErr"};
@.StdIn@>
@.StdOut@>
@.StdErr@>
@#
void trace_print @,@,@[ARGS((octa))@];@+@t}\6{@>
@z
  
  

@x
char switchable_string[48]; /* holds |rhs|; position 0 is ignored */
 /* |switchable_string| must be able to hold any |trap_format| */
@y
char switchable_string[300] ={0}; /* holds |rhs|; position 0 is ignored */
 /* |switchable_string| must be able to hold any debug message */
@z

@x
@ @<Sub...@>=
void show_stats @,@,@[ARGS((bool))@];@+@t}\6{@>
@y
@ @(libstats.c@>=
#include <stdio.h>
#include "libconfig.h"
1874
1875
1876
1877
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
#include "libarith.h"

void show_stats @,@,@[ARGS((bool))@];@+@t}\6{@>
@z


@x
@* Running the program. Now we are ready to fit the pieces together into a
working simulator.

@c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include "abstime.h"
@<Preprocessor macros@>@;
@<Type declarations@>@;
@<Global variables@>@;
@<Subroutines@>@;
@#
int main(argc,argv)
  int argc;
  char *argv[];
{
  @<Local registers@>;
  mmix_io_init();
  @<Process the command line@>;
  @<Initialize everything@>;
  @<Load the command line arguments@>;
  @<Get ready to \.{UNSAVE} the initial context@>;
  while (1) {
    if (interrupt && !breakpoint) breakpoint=interacting=true, interrupt=false;
    else {
      breakpoint=false;
      if (interacting) @<Interact with the user@>;
    }
    if (halted) break;
    do @<Perform one instruction@>@;
    while ((!interrupt && !breakpoint) || resuming);
    if (interact_after_break) interacting=true, interact_after_break=false;
  }
 end_simulation:@+if (profiling) @<Print all the frequency counts@>;
  if (interacting || profiling || showing_stats) show_stats(true);
  return g[255].l; /* provide rudimentary feedback for non-interactive runs */
}
@y
@* Making the library. Now we are ready to write the different pieces of
a working simulator to separate files of a library.

@(liblibinit.c@>=
#include <stdlib.h>
#include <stdio.h>
#include "libconfig.h"
1933
1934
1935
1936
1937
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"

1938
1939
1940
#ifndef MMIX_TRAP
#include "mmix-io.h"
#endif
1941
1942
1943
1944

int mmix_lib_initialize(void)
{
   @<Set up persistent data@>;
1945
1946
1947
#ifndef MMIX_TRAP
   mmix_io_init();
#endif
1948
1949
1950
1951
1952
1953
1954
1955
   return 0;
}

@ @(libinit.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include "libconfig.h"
1956
1957
1958
1959
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
#include "libarith.h"

#ifdef WIN32
#include <windows.h>

BOOL CtrlHandler(DWORD fdwCtrlType);
#else
void catchint(int n);
#endif

1970
1971
1972
#include <setjmp.h>
extern jmp_buf mmix_exit;

1973
int mmix_initialize(void)
1974
1975
{ 
  @<Initialize everything@>;
1976
1977
1978
1979
1980
  cur_seg.h=cur_seg.l=0; /* the Text segment is current */
  return 0;
}

@ @(libboot.c@>=
1981
#include <stdlib.h>
1982
1983
#include <stdio.h>
#include <string.h>
1984
#include <setjmp.h>
1985
1986
#include "abstime.h"
#include "libconfig.h"
1987
1988
1989
1990
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
1991
1992
#include "libarith.h"

1993
extern jmp_buf mmix_exit;
1994
1995
1996
extern void dump(mem_node *p);
extern void dump_tet(tetra t);

1997
void mmix_boot(void)
1998
1999
{ 
  MMIX_PREBOOT 
2000
  @<Boot the machine@>;
2001
2002
2003
2004
2005
2006
2007
2008
#ifdef MMIX_BOOT
  loc.h=inst_ptr.h=0x80000000;
  loc.l=inst_ptr.l=0x00000000;
  g[rJ].h=g[rJ].l =0xFFFFFFFF;
  resuming=false;
#else
  @<Get ready to \.{UNSAVE} the initial context@>
#endif
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
}


@ @(libload.c@>=
int mmix_load_file(char *mmo_file_name)
{ int j; /* miscellaneous indices */
  mem_tetra *ll; /* current place in the simulated memory */
  char *p; /* current place in a string */
  free_file_info();
  if (mmo_file_name!=NULL && mmo_file_name[0]!=0)
  { 
   @<Load object file@>;
   write_all_data_cache();
   clear_all_instruction_cache();
  }
  return 0;
}

@ @(libcommand.c@>=
#include <stdio.h>
#include <string.h>
#include "libconfig.h"
2031
2032
2033
2034
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
#include "libarith.h"

int mmix_commandline(int argc, char *argv[])
{ int k;
  mem_tetra *ll; /* current place in the simulated memory */
  @<Load the command line arguments@>;
  g[rQ].h=g[rQ].l=new_Q.h=new_Q.l=0; /*hide problems with loading the command line*/
  return 0;
}

@ @(libinteract.c@>=
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "libconfig.h"
2050
2051
2052
2053
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
#include "libarith.h"

static octa scan_hex(char *s, octa offset);

int	mmix_interact(void)      
/* return zero to end the simulation */
{ int j,k; /* miscellaneous indices */
  mem_tetra *ll; /* current place in the simulated memory */
  char *p; /* current place in a string */
  @<Interact with the user@>;
  return 1;
end_simulation:
  return 0;
}

@ @(libfetch.c@>=
#include <stdio.h>
#include <string.h>
#include "libconfig.h"
2073
2074
2075
2076
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
#include "libarith.h"

int mmix_fetch_instruction(void)
/* return zero if no instruction was loaded */
{ mem_tetra *ll; /* current place in the simulated memory */
  @<Fetch the next instruction@>;
  return 1;

protection_violation: 
  strcpy(lhs,"!protected");
  g[rQ].h |= P_BIT; new_Q.h |= P_BIT; /* set the p bit */
  return 0;

security_inst: 
  strcpy(lhs,"!insecure");
  return 0;
  
page_fault: 
  strcpy(lhs,"!not fetched");
  return 0;
}

@ @(libperform.c@>=
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "libconfig.h"
2108
2109
2110
2111
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2112
#include "libarith.h"
2113
#include "mmix-io.h"
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138

static bool interact_after_resume= false;

@<Stack store@>@;
@<Stack load@>@;
@<Register truth@>@;

int mmix_perform_instruction(void)  
{ @<Local registers@>;
  @<Perform one instruction@>@;
  return 1;
}

@ @(libtrace.c@>=

void mmix_trace(void)
{ mem_tetra *ll; /* current place in the simulated memory */
  char *p; /* current place in a string */

  @<Trace the current instruction, if requested@>;
} 

@ @(libdtrap.c@>=
#include <stdio.h>
#include "libconfig.h"
2139
2140
2141
2142
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161


void mmix_dynamic_trap(void)
{  
   @<Check for trap interrupt@>;
}

@ @(libprofile.c@>=


void mmix_profile(void)
{
  @<Print all the frequency counts@>;
}


@ @(libfinal.c@>=
#include <stdio.h>
#include "libconfig.h"
2162
2163
2164
2165
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2166
2167
2168
2169
2170
2171
2172
2173
2174

int mmix_finalize(void)
{ free_file_info();
  return 0;
}

@ @(liblibfinal.c@>=
#include <stdio.h>
#include "libconfig.h"
2175
2176
2177
2178
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2179
2180
2181
2182
2183

int mmix_lib_finalize(void)
{ return 0;
}

2184
2185
2186
@ @(libglobals.c@>=
#include <stdio.h>
#include "libconfig.h"
2187
2188
2189
2190
2191
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"

2192
2193
@<Global variables@>@;

2194
2195
2196
2197
2198
2199
2200
2201
2202
@ @c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include "abstime.h"
#include "libconfig.h"
2203
2204
2205
2206
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2207

2208
jmp_buf mmix_exit;
2209
2210
2211
2212
2213
2214
2215
2216
2217

int main(argc,argv)
  int argc;
  char *argv[];
{
  char **boot_cur_arg;
  int boot_argc;
  mmix_lib_initialize();
  g[255].h=0;
2218
  g[255].l=setjmp(mmix_exit);
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
  if (g[255].l!=0)
   goto end_simulation;
  @<Process the command line@>;
  
  MMIX_AFTER_COMMANDLINE

  mmix_initialize();

  boot_cur_arg = cur_arg;
  boot_argc = argc;
boot:
  argc = boot_argc;
  cur_arg = boot_cur_arg;
  
  mmix_boot();
  
  mmix_load_file(*cur_arg);
  mmix_commandline(argc, argv);
  while (true) {
    if (interrupt && !breakpoint) breakpoint=interacting=true, interrupt=false;
    else if (!(inst_ptr.h&sign_bit) || show_operating_system || 
          (inst_ptr.h==0x80000000 && inst_ptr.l==0))
    { breakpoint=false;
      if (interacting) { 
		if (!mmix_interact()) goto end_simulation;
      }
    }
    if (MMIX_END) break;
    do   
    { if (!resuming)
        mmix_fetch_instruction();       
      mmix_perform_instruction();
      mmix_trace();
      mmix_dynamic_trap();
      if (resuming && op!=RESUME) resuming=false; 
      if (!resuming && MMIX_BREAK_LOOP) break; 
    } while (resuming || (!interrupt && !breakpoint));
    if (interact_after_break) 
       interacting=true, interact_after_break=false;
2258
    if (MMIX_REBOOT)
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
    { breakpoint=true; 
      goto boot;
    }
  }
  end_simulation:@+if (profiling) mmix_profile();
  if (interacting || profiling || showing_stats) show_stats(true);
  mmix_finalize();
  return g[255].l; /* provide rudimentary feedback for non-interactive runs */
}  
@z

mmo_file_name may become a variable not an alias.

@x
@d mmo_file_name *cur_arg
@y
@z

@x
if (!*cur_arg) scan_option("?",true); /* exit with usage note */
argc -= cur_arg-argv; /* this is the |argc| of the user program */
@y
MMIX_USAGE;
argc -= (int)(cur_arg-argv); /* this is the |argc| of the user program */
@z

@x
@<Subr...@>=
void scan_option @,@,@[ARGS((char*,bool))@];@+@t}\6{@>
@y
@(libsoption.c@>=
2290
#include <stdlib.h>
2291
2292
#include <stdio.h>
#include <string.h>
2293
#include <setjmp.h>
2294
#include "libconfig.h"
2295
2296
2297
2298
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2299
2300
#include "mmix-io.h"

2301
2302
extern jmp_buf mmix_exit;

2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
void scan_option @,@,@[ARGS((char*,bool))@];@+@t}\6{@>
@z

@x
 case 'b':@+if (sscanf(arg+1,"%d",&buf_size)!=1) buf_size=0;@+return;
@y
 case 'b':@+if (sscanf(arg+1,"%d",&buf_size)!=1) buf_size=0;@+return;
 MMIX_OPTIONS

@z

we need to replace all exits.

@x
    exit(-1);
@y
2319
    longjmp(mmix_exit,-1);
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
@z    

@x
bool interrupt; /* has the user interrupted the simulation recently? */
bool profiling; /* should we print the profile at the end? */
@y
bool interrupt=0; /* has the user interrupted the simulation recently? */
bool profiling=0; /* should we print the profile at the end? */
@z

@x
"-s    show statistics after each traced instruction\n",@|
@y
MMIX_OPTION_STRING
"-s    show statistics after each traced instruction\n",@|
@z



@x
"T         set current segment to Text_Segment\n",@|
@y
MMIX_INTERACT_STRING
"T         set current segment to Text_Segment\n",@|

@z

@x
@ @<Initialize...@>=
signal(SIGINT,catchint); /* now |catchint| will catch the first interrupt */

@ @<Subr...@>=
void catchint @,@,@[ARGS((int))@];@+@t}\6{@>
void catchint(n)
  int n;
{ if (n!=SIGINT) return;
  interrupt=true;
  signal(SIGINT,catchint); /* now |catchint| will catch the next interrupt */
}
@y
@ @<Initialize...@>=
#ifdef WIN32
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );
#else
signal(SIGINT,catchint); /* now |catchint| will catch the first interrupt */
#endif

@ @(libinit.c@>=
#ifdef WIN32
BOOL CtrlHandler( DWORD fdwCtrlType ) 
{ MMIX_CTRL_HANDLER  
  interrupt=true;
  if (fdwCtrlType==CTRL_C_EVENT || fdwCtrlType==CTRL_BREAK_EVENT )
  { 
    printf("Ctrl-C received\n");
  }
  else
  { printf("Closing MMIX\n");
    halted=1;
  }
  return TRUE;
}
#else
void catchint @,@,@[ARGS((int))@];@+@t}\6{@>
void catchint(n)
  int n;
{ MMIX_CTRL_HANDLER  
  interrupt=true;
  printf("Ctrl-C received\n");
  signal(SIGINT,catchint); /* now |catchint| will catch the next interrupt */
}
#endif
@z

@x
 interact: @<Put a new command in |command_buf|@>;
@y
 interact: @<Put a new command in |command_buf|@>;
 MMIX_GET_INTERRUPT  
@z

@x
@ @<Subr...@>=
octa scan_hex @,@,@[ARGS((char*,octa))@];@+@t}\6{@>
@y
@ @(libinteract.c@>=
octa scan_hex @,@,@[ARGS((char*,octa))@];@+@t}\6{@>
@z

@x
 case 'M':@+if (!(cur_disp_addr.h&sign_bit)) {
    ll=mem_find(cur_disp_addr);
    ll->tet=val.h;@+ (ll+1)->tet=val.l;
  }@+break;
@y
 case 'M':
  store_octa(val,cur_disp_addr);
  @+break;
@z

@x
 case 'M':@+if (cur_disp_addr.h&sign_bit) aux=zero_octa;
  else {
    ll=mem_find(cur_disp_addr);
    aux.h=ll->tet;@+ aux.l=(ll+1)->tet;
  }
@y
 case 'M':
    load_octa(&aux,cur_disp_addr);
@z


@x
@ @<Subr...@>=
void print_string @,@,@[ARGS((octa))@];@+@t}\6{@>
@y
@ @(libprint.c@>=
void print_string @,@,@[ARGS((octa))@];@+@t}\6{@>
@z


We allow tracing in the Textsegement and in the negative Segment.

@x
 if (val.h<0x20000000) {
@y
 if (val.h<0x20000000|| val.h>=0x80000000) {
@z

We allow breakpoints at all addresses.

@x
 if (!(val.h&sign_bit)) {
@y
 {
@z

@x
case 'B': show_breaks(mem_root);
@y
case 'B': show_breaks(mem_root);@+goto passit;
MMIX_INTERACT_ACTION   
@z

@x
@ @<Sub...@>=
void show_breaks @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@y
@ @(libshowbreaks.c@>=
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libconfig.h"
2473
2474
2475
2476
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
#include "libarith.h"

void show_breaks @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@z


@x
@<Load the command line arguments@>=
x.h=0x40000000, x.l=0x8;
loc=incr(x,8*(argc+1));
for (k=0; k<argc; k++,cur_arg++) {
  ll=mem_find(x);
  ll->tet=loc.h, (ll+1)->tet=loc.l;
  ll=mem_find(loc);
  mmputchars((unsigned char *)*cur_arg,strlen(*cur_arg),loc);
  x.l+=8, loc.l+=8+(strlen(*cur_arg)&-8);
}
x.l=0;@+ll=mem_find(x);@+ll->tet=loc.h, (ll+1)->tet=loc.l;
@y
@<Load the command line arguments@>=
x.h=0x60000000, x.l=0x00;
aux.h=0, aux.l=argc;
store_octa(aux,x); /* and $\$0=|argc|$ */
x.h=0x40000000, x.l=0x8;
aux=incr(x,8*(argc+1));
for (k=0; k<argc && *argv!=NULL; k++,argv++) {
  store_octa(aux,x);
  mmputchars((unsigned char *)*argv,(int)strlen(*argv),aux);
  x.l+=8, aux.l+=8+(tetra)(strlen(*argv)&-8);
}
if (argc>0) 
{ x.l=0;
  store_octa(aux,x);
}
@z

2513
2514
we do not support user libraries at #f0

2515
2516
2517
2518
2519
2520
2521
@x
x.h=0, x.l=0xf0;
ll=mem_find(x);
if (ll->tet) inst_ptr=x;
@^subroutine library initialization@>
@^initialization of a user program@>
@y
2522
2523
@z

2524

2525
2526
2527
@x
  exit(0);
@y
2528
  longjmp(mmix_exit,0);
2529
2530
@z

2531

2532
2533
2534
2535
2536
2537
2538
@x
@<Sub...@>=
void dump @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@y
@(libdump.c@>=
#include <stdio.h>
#include "libconfig.h"
2539
2540
2541
2542
#include <time.h>
#include "libtype.h"
#include "libglobals.h"
#include "mmixlib.h"
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
#include "libarith.h"

void dump @,@,@[ARGS((mem_node*))@];@+@t}\6{@>
@z

@x
@ @<Sub...@>=
void dump_tet(t)
@y
@ @(libdump.c@>=
void dump_tet(t)
2554
@z