-
Notifications
You must be signed in to change notification settings - Fork 0
/
gb_gates.w
executable file
·1934 lines (1733 loc) · 74.6 KB
/
gb_gates.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
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
540
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
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
653
654
655
656
657
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
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
% This file is part of the Stanford GraphBase (c) Stanford University 1993
@i boilerplate.w %<< legal stuff: PLEASE READ IT BEFORE MAKING ANY CHANGES!
@i gb_types.w
\def\title{GB\_\,GATES}
\prerequisite{GB\_\,GRAPH}
@* Introduction. This GraphBase module provides six external subroutines:
$$\vbox{\hsize=.8\hsize \everypar{\hangindent3em}
\noindent|risc|, a routine that creates a directed acyclic graph based on the
logic of a simple RISC computer;\par
\noindent|prod|, a routine that creates a directed acyclic graph based on the
logic of parallel multiplication circuits;\par
\noindent|print_gates|, a routine that outputs a symbolic representation of
such directed acyclic graphs;\par
\noindent|gate_eval|, a routine that evaluates such directed acyclic graphs by
assigning boolean values to each gate;\par
\noindent|partial_gates|, a routine that extracts a subgraph by assigning
random values to some of the input gates;\par
\noindent|run_risc|, a routine that can be used to play with the output
of |risc|.}$$
Examples of the use of these routines can be found in the demo programs
{\sc TAKE\_\,RISC} and {\sc MULTIPLY}.
@(gb_gates.h@>=
#define print_gates p_gates /* abbreviation for Procrustean linkers */
extern Graph *risc(); /* make a network for a microprocessor */
extern Graph *prod(); /* make a network for high-speed multiplication */
extern void print_gates(); /* write a network to standard output file */
extern long gate_eval(); /* evaluate a network */
extern Graph *partial_gates(); /* reduce network size */
extern long run_risc(); /* simulate the microprocessor */
extern unsigned long risc_state[]; /* the output of |run_risc| */
@ The directed acyclic graphs produced by {\sc GB\_\,GATES} are GraphBase
graphs with special conventions related to logical networks. Each vertex
represents a gate of a network, and utility field |val| is a boolean
value associated with that gate. Utility field |typ| is an ASCII code
that tells what kind of gate is present:
{\advance\parindent 2em
\smallskip
\item{|'I'|} denotes an input gate, whose value is specified externally.
\smallskip
\item{|'&'|} denotes an \.{AND} gate, whose value is the logical {\sc AND} of
two or more previous gates (namely, 1 if all those gates are~1, otherwise~0).
\smallskip
\item{|'|'|} denotes an \.{OR} gate, whose value is the logical {\sc OR} of
two or more previous gates (namely, 0 if all those gates are~0, otherwise~1).
\smallskip
\item{|'^'|} denotes an \.{XOR} gate, whose value is the logical {\sc
EXCLUSIVE-OR} of two or more previous gates (namely, their sum modulo~2).
\smallskip
\item{|'~'|} denotes an inverter, whose value is the logical complement of
the value of a single previous gate.
\smallskip
\item{|'L'|} denotes a latch, whose value depends on past history; it is
the value that was assigned to a subsequent gate when the network was most
recently evaluated. Utility field |alt| points to that subsequent gate.
\smallskip}\noindent
Latches can be used to include ``state'' information in a circuit; for example,
they correspond to registers of the RISC machine constructed by |risc|.
The |prod| procedure does not use latches.
The vertices of the directed acyclic graph appear in a special ``topological''
order convenient for evaluation: All the input gates come first, followed
by all the latches; then come the other types of gates, whose values are
computed from their predecessors. The arcs of the graph run from each gate
to its arguments, and all arguments to a gate precede that gate.
If |g| points to such a graph of gates, the utility field |g->outs| points to
a list of |Arc| records, denoting ``outputs'' that might be used in
certain applications. For example, the outputs of the graphs
created by |prod| correspond to the bits of the product of the numbers
represented in the input gates.
A special convention is used so that the routines will support partial
evaluation: The |tip| fields in the output list either point to a
vertex or hold one of the constant values 0 or~1 when regarded as an
unsigned long integer.
@d val x.I /* the field containing a boolean value */
@d typ y.I /* the field containing the gate type */
@d alt z.V /* the field pointing to another related gate */
@d outs zz.A /* the field pointing to the list of output gates */
@d is_boolean(v) ((unsigned long)(v)<=1) /* is a |tip| field constant? */
@d the_boolean(v) ((long)(v)) /* if so, this is its value */
@d tip_value(v) (is_boolean(v)? the_boolean(v): (v)->val)
@d AND '&'
@d OR '|'
@d NOT '~'
@d XOR '^'
@(gb_gates.h@>=
#define val @t\quad@> x.I /* the definitions are repeated in the header file */
#define typ @t\quad@> y.I
#define alt @t\quad@> z.V
#define outs @t\quad@> zz.A
#define is_boolean(v) @t\quad@> ((unsigned long)(v)<=1)
#define the_boolean(v) @t\quad@> ((long)(v))
#define tip_value(v) @t\quad@> (is_boolean(v)? the_boolean(v): (v)->val)
#define AND @t\quad@> '&'
#define OR @t\quad@> '|'
#define NOT @t\quad@> '~'
#define XOR @t\quad@> '^'
@ Let's begin with the |gate_eval| procedure, because it is quite simple
and because it illustrates the conventions just explained. Given a gate
graph |g| and optional pointers |in_vec| and |out_vec|, the procedure
|gate_eval| will assign values to each gate of~|g|. If |in_vec| is
non-null, it should point to a string of characters, each |'0'| or~|'1'|,
that will be assigned to the first gates of the network, in order;
otherwise |gate_eval| assumes that all input gates have already received
appropriate values and it will not change them. New values are computed for
each gate after the bits of |in_vec| have been consumed.
If |out_vec| is non-null, it should point to a memory area capable of
receiving |m+1| characters, where |m| is the number of outputs of~|g|;
a string containing the respective output values will be deposited there.
If |gate_eval| encounters an unknown gate type, it terminates execution
prematurely and returns the value |-1|. Otherwise it returns~0.
@<The |gate_eval| routine@>=
long gate_eval(g,in_vec,out_vec)
Graph *g; /* graph with gates as vertices */
char *in_vec; /* string for input values, or |NULL| */
char *out_vec; /* string for output values, or |NULL| */
{@+register Vertex *v; /* the current vertex of interest */
register Arc *a; /* the current arc of interest */
register char t; /* boolean value being computed */
if (!g) return -2; /* no graph supplied! */
v=g->vertices;
if (in_vec) @<Read a sequence of input values from |in_vec|@>;
for (; v<g->vertices+g->n; v++) {
switch (v->typ) { /* branch on type of gate */
case 'I': continue; /* this input gate's value should be externally set */
case 'L': t=v->alt->val;@+break;
@t\4\4@>@<Compute the value |t| of a classical logic gate@>;
default: return -1; /* unknown gate type! */
}
v->val=t; /* assign the computed value */
}
if (out_vec) @<Store the sequence of output values in |out_vec|@>;
return 0;
}
@ @<Read a sequence...@>=
while (*in_vec && v<g->vertices+g->n)
(v++)->val = *in_vec++ - '0';
@ @<Store the sequence of output values in |out_vec|@>=
{
for (a=g->outs; a; a=a->next)
*out_vec++='0'+tip_value(a->tip);
*out_vec=0; /* terminate the string */
}
@ @<Compute the value |t| of a classical logic gate@>=
case AND: t=1;
for (a=v->arcs; a; a=a->next)
t &= a->tip->val;
break;
case OR: t=0;
for (a=v->arcs; a; a=a->next)
t |= a->tip->val;
break;
case XOR: t=0;
for (a=v->arcs; a; a=a->next)
t ^= a->tip->val;
break;
case NOT: t=1-v->arcs->tip->val;
break;
@ Here now is an outline of the entire {\sc GB\_\,GATES} module, as seen by
the \CEE/ compiler:
@p
#include "gb_flip.h"
/* we will use the {\sc GB\_\,FLIP} routines for random numbers */
#include "gb_graph.h"
/* and we will use the {\sc GB\_\,GRAPH} data structures */
@h@#
@<Private variables@>@;
@<Global variables@>@;
@<Internal subroutines@>@;
@<The |gate_eval| routine@>@;
@<The |print_gates| routine@>@;
@<The |risc| routine@>@;
@<The |run_risc| routine@>@;
@<The |prod| routine@>@;
@<The |partial_gates| routine@>@;
@* The RISC netlist. The subroutine call |risc(regs)| creates a
gate graph having |regs| registers; the value of |regs| must be
between 2 and~16, inclusive, otherwise |regs| is set to~16.
This gate graph describes the circuitry for a small RISC computer, defined
below. The total number of gates turns out to be |1400+115*regs|;
thus it lies between 1630 (when |regs=2|) and 3240 (when |regs=16|).
{\sc EXCLUSIVE-OR} gates are not used; the effect of xoring is obtained where
needed by means of {\sc AND}s, {\sc OR}s, and inverters.
If |risc| cannot do its thing, it returns |NULL| (\.{NULL})
and sets |panic_code|
to indicate the problem. Otherwise |risc| returns a pointer to the graph.
@d panic(c) @+{@+panic_code=c;@+gb_trouble_code=0;@+return NULL;@+}
@<The |risc| routine@>=
Graph *risc(regs)
unsigned long regs; /* number of registers supported */
{@+@<Local variables for |risc|@>@;
@#
@<Initialize |new_graph| to an empty graph of the appropriate size@>;
@<Add the RISC data to |new_graph|@>;
if (gb_trouble_code) {
gb_recycle(new_graph);
panic(alloc_fault); /* oops, we ran out of memory somewhere back there */
}
return new_graph;
}
@ @<Local variables for |risc|@>=
Graph *new_graph; /* the graph constructed by |risc| */
register long k,r; /* all-purpose indices */
@ This RISC machine works with 16-bit registers and 16-bit data words.
It cannot write into memory, but it assumes the existence of an
external read-only memory. The circuit has 16 outputs, representing
the 16 bits of a memory address register. It also has 17 inputs, the
last 16 of which are supposed to be set to the contents of the memory
address computed on the previous cycle. Thus we can run the machine
by accessing memory between calls of |gate_eval|. The first input
bit, called \.{RUN}, is normally set to~1; if it is~0, the other
inputs are effectively ignored and all registers and outputs will be
cleared to~0. Input bits for the memory appear in ``little-endian
order,'' that is, least significant bit first; but the output bits for
the memory address register appear in ``big-endian order,'' most
significant bit first.
Words read from memory are interpreted as instructions having the following
format:
$$\vbox{\offinterlineskip
\def\\#1&{\omit&}
\hrule
\halign{&\vrule#&\strut\sevenrm\hbox to 1.7em{\hfil#\hfil}\cr
height 5pt&\multispan7\hfill&&\multispan7\hfill&&\multispan3\hfill
&&\multispan3\hfill&&\multispan7\hfill&\cr
&\multispan7\hfill\.{DST}\hfill&&\multispan7\hfill\.{MOD}\hfill
&&\multispan3\hfill\.{OP}\hfill&&\multispan3\hfill\.{A}\hfill
&&\multispan7\hfill\.{SRC}\hfill&\cr
height 5pt&\multispan7\hfill&&\multispan7\hfill&&\multispan3\hfill
&&\multispan3\hfill&&\multispan7\hfill&\cr
\noalign{\hrule}
\\15&\\14&\\13&\\12&\\11&\\10&\\9&\\8&\\7&\\6&\\5&\\4&\\3&\\2&\\1&%
\\0&\omit\cr}}$$
The \.{SRC} and \.A fields specify a ``source'' value.
If $\.A=0$, the source is \.{SRC}, treated as a 16-bit signed
number between $-8$ and $+7$ inclusive.
If $\.A=1$, the source is the contents of register \.{DST} plus the
(signed) value of \.{SRC}. If $\.A=2$, the source is the contents of register
\.{SRC}. And if $\.A=3$, the source is the contents of the memory location
whose address is the contents of register \.{SRC}. Thus, for example,
if $\.{DST}=3$ and $\.{SRC}=10$, and if \.{r3} contains 17 while \.{r10}
contains 1009, the source value will be $-6$ if $\.A=0$,
or $17-6=11$ if $\.A=1$, or 1009 if $\.A=2$, or the contents of memory location
1009 if $\.A=3$.
The \.{DST} field specifies the number of the destination register. This
register receives a new value based on its previous value and the source
value, as prescribed by the operation defined in the \.{OP} and \.{MOD}
fields. For example, when $\.{OP}=0$, a general logical operation is
performed, as follows:
Suppose the bits of \.{MOD} are called $\mu_{11}\mu_{10}\mu_{01}
\mu_{00}$ from left to right. Then if the $k$th bit of the destination register
currently is equal to~$i$ and the $k$th bit of the source value is
equal to~$j$, the general logical operator changes the $k$th bit of
the destination register to~$\mu_{ij}$. If the \.{MOD} bits are,
for example, $1010$, the source value is simply copied to the
destination register; if $\.{MOD}=0110$, an exclusive-or is done;
if $\.{MOD}=0011$, the destination register is complemented and the
source value is effectively ignored.
The machine contains four status bits called \.S (sign), \.N (nonzero),
\.K (carry), and \.V (overflow). Every general logical operation sets
\.S equal to the sign of the new result transferred to the destination
register; this is bit~15, the most significant bit. A general logical
operation also sets \.N to~1 if any of the other 15 bits are~1, to~0
if all of the other bits are~0. Thus \.S and \.N both become zero if and
only if the new result is entirely zero. Logical operations do not change
the values of \.K and~\.V; the latter are affected only by the arithmetic
operations described below.
The status of the \.S and \.N bits can be tested by using the
conditional load operator, $\.{OP}=2$: This operation loads the source
value into the destination register if and only if \.{MOD} bit
$\mu_{ij}=1$, where $i$ and~$j$ are the current values of \.S and~\.N,
respectively. For example, if $\.{MOD}=0011$, the source value is
loaded if and only if $\.S=0$, which means that the last value
affecting \.S and~\.N was greater than or equal to zero. If
$\.{MOD}=1111$, loading is always done; this option provides a way
to move source to destination without affecting \.S or~\.N.
A second conditional load operator, $\.{OP}=3$, is similar, but
it is used for testing the status of \.K and~\.V instead of
\.S and~\.N. For example, a command having $\.{MOD}=1010$,
$\.{OP}=3$, $\.A=1$, and $\.{SRC}=1$ adds the current overflow bit to the
destination register. (Please take a moment to understand why
this is true.)
We have now described all the operations except those that
are performed when $\.{OP}=1$.
As you might expect, our machine is able to do rudimentary arithmetic.
The general addition and subtraction operators belong to this final case,
together with various shift operators, depending on the value of \.{MOD}.
Eight of the $\.{OP}=1$ operations set the destination register to a shifted
version of the source value: $\.{MOD}=0$ means ``shift left~1,''
which is equivalent to multiplying the source by~2; $\.{MOD}=1$ means
``cyclic shift left~1,'' which is the same except that it also adds the
previous sign bit to the result; $\.{MOD}=2$ means ``shift left~4,''
which is equivalent to multiplying by~16; $\.{MOD}=3$ means ``cyclic
shift left~4''; $\.{MOD}=4$ means ``shift right~1,'' which is
equivalent to dividing the source by~2 and rounding down to the
next lower integer if there was a remainder; $\.{MOD}=5$ means
``unsigned shift right~1,'' which is the same except that the
most significant bit is always set to zero instead of retaining the
previous sign; $\.{MOD}=6$ means ``shift right~4,'' which is equivalent
to dividing the source by~16 and rounding down; $\.{MOD}=7$ means
``unsigned shift right~4.'' Each of these shift operations affects
\.S and~\.N, as in the case of logical operations. They also affect
\.K and~\.V, as follows: Shifting left sets \.K to~1 if and
only if at least one of the bits shifted off the left was nonzero,
and sets \.V to~1 if and only if the corresponding multiplication
would cause overflow.
Shifting right~1 sets \.K to the value of the bit
shifted out, and sets \.V to~0;
shifting right~4 sets \.K to the value of the last
bit shifted out, and sets \.V to the logical {\sc OR} of the other three
lost bits. The same values of \.K and \.V arise from cyclic or unsigned
shifts as from ordinary shifts.
When $\.{OP}=1$ and $\.{MOD}=8$, the source value is added to the
destination register. This sets \.S, \.N, and \.V as you would expect;
and it sets \.K to the carry you would get if you were treating the operands
as 16-bit unsigned integers. Another addition operation, having
$\.{MOD}=9$, is similar, but the current value of \.K is also added to
the result; in this case, the new value of \.N will be zero if and only if
the 15 non-sign bits of the result are zero and the previous values of
\.S and~\.N were also zero. This means
that you can use the first addition operation on the lower
halves of a 32-bit number and the second operation on the upper halves,
thereby obtaining a correct 32-bit result, with appropriate sign,
nonzero, carry, and overflow bits set.
Higher precision (48 bits, 64 bits, etc.)~can be obtained in a similar way.
When $\.{OP}=1$ and $\.{MOD}=10$, the source value is subtracted
from the destination register. Again, \.S, \.N, \.K, and \.V are set;
the \.K value in this case represents the ``borrow'' bit.
An auxiliary subtraction operation, having $\.{MOD}=11$, subtracts
also the current value of \.K, thereby allowing for correct 32-bit subtraction.
The operations for $\.{OP}=1$ and $\.{MOD}=12$, 13, and~14 are
``reserved for future expansion.'' Actually they will never change, however,
since this RISC chip is purely academic. If you check out the logic
below, you will find that they simply set the destination register and
the four status bits all to zero.
A final operation, called \.{JUMP}, will be explained momentarily.
It has $\.{OP}=1$ and $\.{MOD}=15$. It does not affect \.S, \.N, \.K, or~\.V.
If the RISC is made with fewer than 16 registers, the higher-numbered ones
will effectively contain zero whenever their values are fetched.
But if you use them as destination registers, you will set
\.S, \.N, \.K, and~\.V as if actual numbers were being stored.
Register 0 is different from the other 15 registers: It is the location
of the current instruction. Therefore if you change the contents of
register~0, you are changing the control flow of the program. If you
do not change register~0, it automatically increases by~1.
Special treatment occurs when $\.A=3$ and $\.{SRC}=0$.
In such a case, the normal rules given above say that the source value
should be the contents of the memory location specified by register~0. But
that memory location holds the current instruction; so the machine
uses the {\sl following\/} location instead, as a 16-bit source
operand. If the contents of register~0 are not changed by such a
two-word instruction, register~0 will increase by~2 instead of~1.
We have now discussed everything about the machine except the operation
of the \.{JUMP} command. This command moves the source value to
register~0, thereby changing the flow of control. Furthermore, if
$\.{DST}\ne0$, it also sets register \.{DST} to the location of the
instruction following the \.{JUMP}. Assembly language programmers will
recognize this as a convenient way to jump to a subroutine.
Example programs can be found in the {\sc TAKE\_\,RISC} module, which includes
a simple subroutine for multiplication and division.
@ A few auxiliary functions will ameliorate the task of constructing
the RISC logic. First comes a routine that ``christens'' a new gate,
assigning it a name and a type. The name is constructed from a prefix
and a serial number, where the prefix indicates the current portion of
logic being created.
@<Internal...@>=
static Vertex* new_vert(t)
char t; /* the type of the new gate */
{@+register Vertex *v;
v=next_vert++;
if (count<0) v->name=gb_save_string(prefix);
else {
sprintf(name_buf,"%s%ld",prefix,count);
v->name=gb_save_string(name_buf);
count++;
}
v->typ=t;
return v;
}
@ @d start_prefix(s) strcpy(prefix,s);@+count=0
@d numeric_prefix(a,b) sprintf(prefix,"%c%ld:",a,b);@+count=0;
@<Private...@>=
static Vertex* next_vert; /* the first vertex not yet assigned a name */
static char prefix[5]; /* prefix string for vertex names */
static long count; /* serial number for vertex names */
static char name_buf[100]; /* place to form vertex names */
@ Here are some trivial routines to create gates with 2, 3, or more
arguments. The arcs from such a gate to its inputs are assigned length 100.
Other routines, defined below,
assign length~1 to the arc between an inverter and its unique
input. This convention makes the lengths of shortest paths in the resulting
network a bit more interesting than they would otherwise be.
@d DELAY 100L
@<Internal...@>=
static Vertex* make2(t,v1,v2)
char t; /* the type of the new gate */
Vertex *v1,*v2;
{@+register Vertex *v=new_vert(t);
gb_new_arc(v,v1,DELAY);
gb_new_arc(v,v2,DELAY);
return v;
}
@#
static Vertex* make3(t,v1,v2,v3)
char t; /* the type of the new gate */
Vertex *v1,*v2,*v3;
{@+register Vertex *v=new_vert(t);
gb_new_arc(v,v1,DELAY);
gb_new_arc(v,v2,DELAY);
gb_new_arc(v,v3,DELAY);
return v;
}
@#
static Vertex* make4(t,v1,v2,v3,v4)
char t; /* the type of the new gate */
Vertex *v1,*v2,*v3,*v4;
{@+register Vertex *v=new_vert(t);
gb_new_arc(v,v1,DELAY);
gb_new_arc(v,v2,DELAY);
gb_new_arc(v,v3,DELAY);
gb_new_arc(v,v4,DELAY);
return v;
}
@#
static Vertex* make5(t,v1,v2,v3,v4,v5)
char t; /* the type of the new gate */
Vertex *v1,*v2,*v3,*v4,*v5;
{@+register Vertex *v=new_vert(t);
gb_new_arc(v,v1,DELAY);
gb_new_arc(v,v2,DELAY);
gb_new_arc(v,v3,DELAY);
gb_new_arc(v,v4,DELAY);
gb_new_arc(v,v5,DELAY);
return v;
}
@ We use utility field |w.V| to store a pointer to the complement
of a gate, if that complement has been formed. This trick prevents the creation
of excessive gates that are equivalent to each other. The following subroutine
returns a pointer to the complement of a given gate.
@d bar w.V /* field pointing to complement, if known to exist */
@d even_comp(s,v) ((s)&1? v: comp(v))
@<Internal...@>=
static Vertex* comp(v)
Vertex *v;
{@+register Vertex *u;
if (v->bar) return v->bar;
u=next_vert++;
u->bar=v;@+v->bar=u;
sprintf(name_buf,"%s~",v->name);
u->name=gb_save_string(name_buf);
u->typ=NOT;
gb_new_arc(u,v,1L);
return u;
}
@ To create a gate for the {\sc EXCLUSIVE-OR} of two arguments, we can
either construct the {\sc OR} of two {\sc AND}s or the {\sc AND} of two
{\sc OR}s. We choose the former alternative:
@<Internal...@>=
static Vertex* make_xor(u,v)
Vertex *u,*v;
{@+register Vertex *t1,*t2;
t1=make2(AND,u,comp(v));
t2=make2(AND,comp(u),v);
return make2(OR,t1,t2);
}
@ OK, let's get going.
@<Initialize |new_graph|...@>=
if (regs<2 || regs>16) regs=16;
new_graph=gb_new_graph(1400+115*regs);
if (new_graph==NULL)
panic(no_room); /* out of memory before we're even started */
sprintf(new_graph->id,"risc(%lu)",regs);
strcpy(new_graph->util_types,"ZZZIIVZZZZZZZA");
next_vert=new_graph->vertices;
@ @<Add the RISC data to |new_graph|@>=
@<Create the inputs and latches@>;
@<Create gates for instruction decoding@>;
@<Create gates for fetching the source value@>;
@<Create gates for the general logic operation@>;
@<Create gates for the conditional load operations@>;
@<Create gates for the arithmetic operations@>;
@<Create gates that bring everything together properly@>;
if (next_vert!=new_graph->vertices+new_graph->n)
panic(impossible); /* oops, we miscounted; this should be impossible */
@ Internal names will make it convenient to refer to the most important
gates. Here are the names of inputs and latches.
@<Local variables for |risc|@>=
Vertex *run_bit; /* the \.{RUN} input */
Vertex *mem[16]; /* 16 bits of input from read-only memory */
Vertex *prog; /* first of 10 bits in the program register */
Vertex *sign; /* the latched value of \.S */
Vertex *nonzero; /* the latched value of \.N */
Vertex *carry; /* the latched value of \.K */
Vertex *overflow; /* the latched value of \.V */
Vertex *extra; /* latched status bit: are we doing an extra memory cycle? */
Vertex *reg[16]; /* the least-significant bit of a given register */
@ @d first_of(n,t) new_vert(t);@+for (k=1;k<n;k++)@+new_vert(t);
@<Create the inputs and latches@>=
strcpy(prefix,"RUN");@+count=-1;@+run_bit=new_vert('I');
start_prefix("M");@+for (k=0;k<16;k++)@+mem[k]=new_vert('I');
start_prefix("P");@+prog=first_of(10,'L');
strcpy(prefix,"S");@+count=-1;@+sign=new_vert('L');
strcpy(prefix,"N");@+nonzero=new_vert('L');
strcpy(prefix,"K");@+carry=new_vert('L');
strcpy(prefix,"V");@+overflow=new_vert('L');
strcpy(prefix,"X");@+extra=new_vert('L');
for (r=0;r<regs;r++) {
numeric_prefix('R',r);
reg[r]=first_of(16,'L');
}
@ The order of evaluation of function arguments is not defined in \CEE/,
so we introduce a few macros that force left-to-right order.
@d do2(result,t,v1,v2)
{@+t1=v1;@+t2=v2;
result=make2(t,t1,t2);@+}
@d do3(result,t,v1,v2,v3)
{@+t1=v1;@+t2=v2;@+t3=v3;
result=make3(t,t1,t2,t3);@+}
@d do4(result,t,v1,v2,v3,v4)
{@+t1=v1;@+t2=v2;@+t3=v3;@+t4=v4;
result=make4(t,t1,t2,t3,t4);@+}
@d do5(result,t,v1,v2,v3,v4,v5)
{@+t1=v1;@+t2=v2;@+t3=v3;@+t4=v4;@+t5=v5;
result=make5(t,t1,t2,t3,t4,t5);@+}
@<Local variables for |risc|@>=
Vertex *t1,*t2,*t3,*t4,*t5; /* temporary holds to force evaluation order */
Vertex *tmp[16]; /* additional holding places for partial results */
Vertex *imm; /* is the source value immediate (a given constant)? */
Vertex *rel; /* is the source value relative to the
current destination register? */
Vertex *dir; /* should the source value be fetched directly
from a source register? */
Vertex *ind; /* should the source value be fetched indirectly from memory? */
Vertex *op; /* least significant bit of \.{OP} */
Vertex *cond; /* most significant bit of \.{OP} */
Vertex *mod[4]; /* the \.{MOD} bits */
Vertex *dest[4]; /* the \.{DEST} bits */
@ The sixth line of the program here can be translated into the logic
equation
$$ |op|=(|extra|\land|prog|)\lor(\mskip1mu\overline{|extra|}\land|mem[6]|)\,.$$
Once you see why, you'll be able to read the rest of this curious code.
@<Create gates for instruction decoding@>=
start_prefix("D");
do3(imm,AND,comp(extra),comp(mem[4]),comp(mem[5])); /* $\.A=0$ */
do3(rel,AND,comp(extra),mem[4],comp(mem[5])); /* $\.A=1$ */
do3(dir,AND,comp(extra),comp(mem[4]),mem[5]); /* $\.A=2$ */
do3(ind,AND,comp(extra),mem[4],mem[5]); /* $\.A=3$ */
do2(op,OR,make2(AND,extra,prog),make2(AND,comp(extra),mem[6]));
do2(cond,OR,make2(AND,extra,prog+1),make2(AND,comp(extra),mem[7]));
for (k=0;k<4;k++) {
do2(mod[k],OR,make2(AND,extra,prog+2+k),make2(AND,comp(extra),mem[8+k]));
do2(dest[k],OR,make2(AND,extra,prog+6+k),make2(AND,comp(extra),mem[12+k]));
}
@ @<Create gates for fetching the source value@>=
start_prefix("F");
@<Set |old_dest| to the present value of the destination register@>;
@<Set |old_src| to the present value of the source register@>;
@<Set |inc_dest| to |old_dest| plus \.{SRC}@>;
for (k=0;k<16;k++)@/
do4(source[k],OR,
make2(AND,imm,mem[k<4?k:3]),
make2(AND,rel,inc_dest[k]),@|
make2(AND,dir,old_src[k]),
make2(AND,extra,mem[k]));
@ Here and in the immediately following section we create {\sc OR}
gates |old_dest[k]| and |old_src[k]| that might have as many as
16~inputs. (The actual number of inputs is |regs|.) All other
gates in the network will have at most five inputs.
@<Set |old_dest| to the present value of the destination register@>=
for (r=0;r<regs;r++) @/
do4(dest_match[r],AND,even_comp(r,dest[0]),even_comp(r>>1,dest[1]),@|
even_comp(r>>2,dest[2]),even_comp(r>>3,dest[3]));
for (k=0;k<16;k++) {
for (r=0;r<regs;r++)@/
tmp[r]=make2(AND,dest_match[r],reg[r]+k);
old_dest[k]=new_vert(OR);
for (r=0;r<regs;r++) gb_new_arc(old_dest[k],tmp[r],DELAY);
}
@ @<Set |old_src| to the present value of the source register@>=
for (k=0;k<16;k++) {
for (r=0;r<regs;r++)@/
do5(tmp[r],AND,reg[r]+k,even_comp(r,mem[0]),even_comp(r>>1,mem[1]),
even_comp(r>>2,mem[2]),even_comp(r>>3,mem[3]));
old_src[k]=new_vert(OR);
for (r=0;r<regs;r++) gb_new_arc(old_src[k],tmp[r],DELAY);
}
@ @<Local variables for |risc|@>=
Vertex *dest_match[16]; /* |dest_match[r]==1| iff $\.{DST}=r$ */
Vertex *old_dest[16]; /* contents of destination register before operation */
Vertex *old_src[16]; /* contents of source register before operation */
Vertex *inc_dest[16]; /* |old_dest| plus the \.{SRC} field */
Vertex *source[16]; /* source value for the operation */
Vertex *log[16]; /* result of general logic operation */
Vertex *shift[18]; /* result of shift operation, with carry and overflow */
Vertex *sum[18]; /* |old_dest| plus |source| plus optional carry */
Vertex *diff[18]; /* |old_dest| minus |source| minus optional borrow */
Vertex *next_loc[16]; /* contents of register 0, plus 1 */
Vertex *next_next_loc[16]; /* contents of register 0, plus 2 */
Vertex *result[18]; /* result of operating on |old_dest| and |source| */
@ @<Create gates for the general logic operation@>=
start_prefix("L");
for (k=0;k<16;k++)@/
do4(log[k],OR,@|
make3(AND,mod[0],comp(old_dest[k]),comp(source[k])),@|
make3(AND,mod[1],comp(old_dest[k]),source[k]),@|
make3(AND,mod[2],old_dest[k],comp(source[k])),@|
make3(AND,mod[3],old_dest[k],source[k]));
@ @<Create gates for the conditional load operations@>=
start_prefix("C");
do4(tmp[0],OR,@|
make3(AND,mod[0],comp(sign),comp(nonzero)),@|
make3(AND,mod[1],comp(sign),nonzero),@|
make3(AND,mod[2],sign,comp(nonzero)),@|
make3(AND,mod[3],sign,nonzero));
do4(tmp[1],OR,@|
make3(AND,mod[0],comp(carry),comp(overflow)),@|
make3(AND,mod[1],comp(carry),overflow),@|
make3(AND,mod[2],carry,comp(overflow)),@|
make3(AND,mod[3],carry,overflow));
do3(change,OR,comp(cond),make2(AND,tmp[0],comp(op)),make2(AND,tmp[1],op));
@ @<Local variables for |risc|@>=
Vertex *change; /* is the destination register supposed to change? */
@ Hardware is like software except that it performs all the operations
all the time and then selects only the results it needs. (If you think about
it, this is a profound observation about economics, society, and nature.
Gosh.)
@<Create gates that bring everything together properly@>=
start_prefix("Z");
@<Create gates for the |next_loc| and |next_next_loc| bits@>;
@<Create gates for the |result| bits@>;
@<Create gates for the new values of registers 1 to |regs|@>;
@<Create gates for the new values of \.S, \.N, \.K, and \.V@>;
@<Create gates for the new values of the program register and |extra|@>;
@<Create gates for the new values of register 0
and the memory address register@>;
@ @<Create gates for the |next_loc|...@>=
next_loc[0]=comp(reg[0]);@+next_next_loc[0]=reg[0];
next_loc[1]=make_xor(reg[0]+1,reg[0]);@+next_next_loc[1]=comp(reg[0]+1);
for (t5=reg[0]+1,k=2;k<16;t5=make2(AND,t5,reg[0]+k++)) {
next_loc[k]=make_xor(reg[0]+k,make2(AND,reg[0],t5));
next_next_loc[k]=make_xor(reg[0]+k,t5);
}
@ @<Create gates for the |result| bits@>=
jump=make5(AND,op,mod[0],mod[1],mod[2],mod[3]); /* assume |cond=0| */
for (k=0;k<16;k++) {
do5(result[k],OR,@|
make2(AND,comp(op),log[k]),@|
make2(AND,jump,next_loc[k]),@|
make3(AND,op,comp(mod[3]),shift[k]),@|
make5(AND,op,mod[3],comp(mod[2]),comp(mod[1]),sum[k]),@|
make5(AND,op,mod[3],comp(mod[2]),mod[1],diff[k]));
do2(result[k],OR,@|
make3(AND,cond,change,source[k]),@|
make2(AND,comp(cond),result[k]));
}
for (k=16;k<18;k++) /* carry and overflow bits of the result */
do3(result[k],OR,@|
make3(AND,op,comp(mod[3]),shift[k]),@|
make5(AND,op,mod[3],comp(mod[2]),comp(mod[1]),sum[k]),@|
make5(AND,op,mod[3],comp(mod[2]),mod[1],diff[k]));
@ The program register |prog| and the |extra| bit are needed for
the case when we must spend an extra cycle to fetch a word from memory.
On the first cycle, |ind| is true, so a ``result'' is calculated but not
actually used. On the second cycle, |extra| is true.
A slight optimization has been introduced in order to make the circuit
a bit more interesting: If a conditional load instruction occurs with
indirect addressing and a false condition, the extra cycle is not taken.
(The |next_next_loc| values were computed for this reason.)
@d latchit(u,@!latch)
(latch)->alt=make2(AND,u,run_bit) /* |u&run_bit| is new value for |latch| */
@<Create gates for the new values of the program reg...@>=
for (k=0;k<10;k++)
latchit(mem[k+6],prog+k);
do2(nextra,OR,make2(AND,ind,comp(cond)),make2(AND,ind,change));
latchit(nextra,extra);
nzs=make4(OR,mem[0],mem[1],mem[2],mem[3]);
nzd=make4(OR,dest[0],dest[1],dest[2],dest[3]);
@ @<Local variables for |risc|@>=
Vertex *jump; /* is this command a \.{JUMP}, assuming |cond| is false? */
Vertex *nextra; /* must we take an extra cycle? */
Vertex *nzs; /* is the \.{SRC} field nonzero? */
Vertex *nzd; /* is the \.{DST} field nonzero? */
@ @<Create gates for the new values of registers 1 to |regs|@>=
t5=make2(AND,change,comp(ind)); /* should destination register change? */
for (r=1;r<regs;r++) {
t4=make2(AND,t5,dest_match[r]); /* should register |r| change? */
for (k=0;k<16;k++) {
do2(t3,OR,make2(AND,t4,result[k]),make2(AND,comp(t4),reg[r]+k));
latchit(t3,reg[r]+k);
}
}
@ @<Create gates for the new values of \.S, \.N, \.K, and \.V@>=
do4(t5,OR,@|
make2(AND,sign,cond),@|
make2(AND,sign,jump),@|
make2(AND,sign,ind),@|
make4(AND,result[15],comp(cond),comp(jump),comp(ind)));
latchit(t5,sign);
do4(t5,OR,@|
make4(OR,result[0],result[1],result[2],result[3]),@|
make4(OR,result[4],result[5],result[6],result[7]),@|
make4(OR,result[8],result[9],result[10],result[11]),@|
make4(OR,result[12],result[13],result[14],@|
@t\hskip5em@>make5(AND,make2(OR,nonzero,sign),op,mod[0],comp(mod[2]),mod[3])));
do4(t5,OR,@|
make2(AND,nonzero,cond),@|
make2(AND,nonzero,jump),@|
make2(AND,nonzero,ind),@|
make4(AND,t5,comp(cond),comp(jump),comp(ind)));
latchit(t5,nonzero);
do5(t5,OR,@|
make2(AND,overflow,cond),@|
make2(AND,overflow,jump),@|
make2(AND,overflow,comp(op)),@|
make2(AND,overflow,ind),@|
make5(AND,result[17],comp(cond),comp(jump),comp(ind),op));
latchit(t5,overflow);
do5(t5,OR,@|
make2(AND,carry,cond),@|
make2(AND,carry,jump),@|
make2(AND,carry,comp(op)),@|
make2(AND,carry,ind),@|
make5(AND,result[16],comp(cond),comp(jump),comp(ind),op));
latchit(t5,carry);
@ As usual, we have left the hardest case for last, hoping that we will
have learned enough tricks to handle it when the time of reckoning
finally arrives.
The most subtle part of the logic here is perhaps the case of a
\.{JUMP} command with $\.A=3$. We want to increase register~0 by~1
during the first cycle of
such a command, if $\.{SRC}=0$, so that the |result| will be
correct on the next cycle.
@<Create gates for the new values of register 0...@>=
skip=make2(AND,cond,comp(change)); /* false conditional? */
hop=make2(AND,comp(cond),jump); /* \.{JUMP} command? */
do4(normal,OR,@|
make2(AND,skip,comp(ind)),@|
make2(AND,skip,nzs),@|
make3(AND,comp(skip),ind,comp(nzs)),@|
make3(AND,comp(skip),comp(hop),nzd));
special=make3(AND,comp(skip),ind,nzs);
for (k=0;k<16;k++) {
do4(t5,OR,@|
make2(AND,normal,next_loc[k]),@|
make4(AND,skip,ind,comp(nzs),next_next_loc[k]),@|
make3(AND,hop,comp(ind),source[k]),@|
make5(AND,comp(skip),comp(hop),comp(ind),comp(nzd),result[k]));
do2(t4,OR,@|
make2(AND,special,reg[0]+k),@|
make2(AND,comp(special),t5));
latchit(t4,reg[0]+k);
do2(t4,OR,@|
make2(AND,special,old_src[k]),@|
make2(AND,comp(special),t5));
{@+register Arc *a=gb_virgin_arc();
a->tip=make2(AND,t4,run_bit);
a->next=new_graph->outs;
new_graph->outs=a; /* pointer to memory address bit */
}
} /* arcs for output bits will appear in big-endian order */
@ @<Local variables for |risc|@>=
Vertex *skip; /* are we skipping a conditional load operation? */
Vertex *hop; /* are we doing a \.{JUMP}? */
Vertex *normal; /* is this a case where register 0 is simply incremented? */
Vertex *special; /* is this a case where register 0 and the memory address
register will not coincide? */
@* Serial addition. We haven't yet specified the parts of |risc| that
deal with addition and subtraction; somehow, those parts wanted to
be separate from the rest. To complete our mission, we will use
subroutine calls of the form `|make_adder(n,x,y,z,carry,add)|',
where |x| and |y| are |n|-bit arrays of input gates and
|z|~is an |(n+1)|-bit array of output gates. If |add!=0|, the subroutine
computes |x+y|, otherwise it computes |x-y|. If |carry!=0|, the |carry| gate
is effectively added to~|y| before the operation.
A simple |n|-stage serial scheme, which reduces the problem of |n|-bit
addition to |(n-1)|-bit addition, is adequate for our purposes here.
(A parallel adder, which gains efficiency by reducing the problem size
from |n| to~$n/\phi$, can be found in the |prod| routine below.)
The handy identity $x-y=\overline{\overline x+y}$ is used to reduce
subtraction to addition.
@<Internal...@>=
static void make_adder(n,x,y,z,carry,add)
unsigned long n; /* number of bits */
Vertex *x[],*y[]; /* input gates */
Vertex *z[]; /* output gates */
Vertex *carry; /* add this to |y|, unless it's null */
char add; /* should we add or subtract? */
{@+register long k;
Vertex *t1,*t2,*t3,*t4; /* temporary storage used by |do4| */
if (!carry) {
z[0]=make_xor(x[0],y[0]);
carry=make2(AND,even_comp(add,x[0]),y[0]);
k=1;
}@+else k=0;
for (;k<n;k++) {
comp(x[k]);@+comp(y[k]);@+comp(carry); /* generate inverse gates */
do4(z[k],OR,@|
make3(AND,x[k],comp(y[k]),comp(carry)),@|
make3(AND,comp(x[k]),y[k],comp(carry)),@|
make3(AND,comp(x[k]),comp(y[k]),carry),@|
make3(AND,x[k],y[k],carry));
do3(carry,OR,@|
make2(AND,even_comp(add,x[k]),y[k]),@|
make2(AND,even_comp(add,x[k]),carry),@|
make2(AND,y[k],carry));
}
z[n]=carry;
}
@ OK, now we can add. What good does that do us?
In the first place, we need a 4-bit adder to compute the least
significant bits of $|old_dest|+\.{SRC}$. The other 12 bits of that
sum are simpler.
@<Set |inc_dest| to |old_dest| plus \.{SRC}@>=
make_adder(4L,old_dest,mem,inc_dest,NULL,1);
up=make2(AND,inc_dest[4],comp(mem[3])); /* remaining bits must increase */
down=make2(AND,comp(inc_dest[4]),mem[3]); /* remaining bits must decrease */
for (k=4;;k++) {
comp(up);@+comp(down);
do3(inc_dest[k],OR,@|
make2(AND,comp(old_dest[k]),up),@|
make2(AND,comp(old_dest[k]),down),@|
make3(AND,old_dest[k],comp(up),comp(down)));
if (k<15) {
up=make2(AND,up,old_dest[k]);
down=make2(AND,down,comp(old_dest[k]));
}@+else break;
}
@ @<Local variables for |risc|@>=
Vertex *up,*down; /* gates used when computing |inc_dest| */
@ In the second place, we need a 16-bit adder and a 16-bit subtracter
for the four addition/subtraction commands.
@<Create gates for the arithmetic operations@>=
start_prefix("A");
@<Create gates for the shift operations@>;
make_adder(16L,old_dest,source,sum,make2(AND,carry,mod[0]),1); /* adder */
make_adder(16L,old_dest,source,diff,make2(AND,carry,mod[0]),0); /* subtracter */
do2(sum[17],OR,@|
make3(AND,old_dest[15],source[15],comp(sum[15])),@|
make3(AND,comp(old_dest[15]),comp(source[15]),sum[15])); /* overflow */
do2(diff[17],OR,@|
make3(AND,old_dest[15],comp(source[15]),comp(diff[15])),@|
make3(AND,comp(old_dest[15]),source[15],diff[15])); /* overflow */
@ @<Create gates for the shift operations@>=
for (k=0;k<16;k++)@/
do4(shift[k],OR,@|
(k==0? make4(AND,source[15],mod[0],comp(mod[1]),comp(mod[2])):@|
@t\hskip5em@>make3(AND,source[k-1],comp(mod[1]),comp(mod[2]))),@|
(k<4? make4(AND,source[k+12],mod[0],mod[1],comp(mod[2])):@|
@t\hskip5em@>make3(AND,source[k-4],mod[1],comp(mod[2]))),@|
(k==15? make4(AND,source[15],comp(mod[0]),comp(mod[1]),mod[2]):@|
@t\hskip5em@>make3(AND,source[k+1],comp(mod[1]),mod[2])),@|
(k>11? make4(AND,source[15],comp(mod[0]),mod[1],mod[2]):@|
@t\hskip5em@>make3(AND,source[k+4],mod[1],mod[2])));
do4(shift[16],OR,@|
make2(AND,comp(mod[2]),source[15]),@|
make3(AND,comp(mod[2]),mod[1],
make3(OR,source[14],source[13],source[12])),@|
make3(AND,mod[2],comp(mod[1]),source[0]),@|
make3(AND,mod[2],mod[1],source[3])); /* ``carry'' */
do3(shift[17],OR,@|
make3(AND,comp(mod[2]),comp(mod[1]),
make_xor(source[15],source[14])),@|
make4(AND,comp(mod[2]),mod[1],@|
@t\hskip5em@>make5(OR,source[15],source[14],
source[13],source[12],source[11]),@|
@t\hskip5em@>make5(OR,comp(source[15]),comp(source[14]),
comp(source[13]),@|
@t\hskip10em@>comp(source[12]),comp(source[11]))),@|
make3(AND,mod[2],mod[1],
make3(OR,source[0],source[1],source[2]))); /* ``overflow'' */
@* RISC management. The |run_risc| procedure takes a gate graph output by
|risc| and simulates its behavior, given the contents of its read-only memory.
(See the demonstration program {\sc TAKE\_\,RISC}, which appears in a module
by itself, for a typical illustration of how |run_risc| might be used.)
This procedure clears the simulated machine and begins executing the program
that starts at address~0. It stops when it gets to an address greater
than the size of read-only memory supplied. One way to stop it
is therefore to execute a command such as |0x0f00|, which will transfer
control to location |0xffff|; even better is |0x0f8f|, which transfers
to location |0xffff| without changing the status of \.S and \.N.
However, if the given read-only memory
contains a full set of $2^{16}$ words, |run_risc| will never stop.
When |run_risc| does stop, it returns 0 and puts the final contents of the
simulated registers into the global array |risc_state|.
Or, if |g| was not a decent graph, |run_risc| returns a negative value and
leaves |risc_state| untouched.
@<The |run_risc|...@>=
long run_risc(g,rom,size,trace_regs)
Graph *g; /* graph output by |risc| */
unsigned long rom[]; /* contents of read-only memory */
unsigned long size; /* length of |rom| vector */
unsigned long trace_regs; /* if nonzero, this many registers will be traced */
{@+register unsigned long l; /* memory address */
register unsigned long m; /* memory or register contents */
register Vertex *v; /* the current gate of interest */
register Arc *a; /* the current output list element of interest */