-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebox.js
1881 lines (1563 loc) · 65.9 KB
/
ebox.js
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
'use strict';
const _ = require('lodash');
const util = require('util');
const {assert} = require('chai');
const StampIt = require('@stamp/it');
const {
octal, oct6, octW, octA, octC,
maskForBit, shiftForBit,
fieldInsert, fieldExtract, fieldMask,
centeredBanner, typeofFunction,
} = require('./util');
const utilFormats = require('./util');
// Read our defines.mic and gobble definitions for fields and
// constants for CRAM and DRAM.
const {CRAMdefinitions, DRAMdefinitions} = require('./cram-defs');
module.exports.CRAMdefinitions = CRAMdefinitions;
// RH sign bit
const SIGN18 = maskForBit(18);
// LH or full word sign bit
const SIGN00 = maskForBit(0);
// All 18 bits in RH
const RHMASK = fieldMask(18, 35);
// All 36 bits in word
const NEG1 = 0o777777n;
// Size of pages in words
const PAGESIZE = 0o1000n;
// Shift to get to page number
const PAGESHIFT = 9n;
module.exports.SIGN18 = SIGN18;
module.exports.SIGN00 = SIGN00;
module.exports.RHMASK = RHMASK;
module.exports.NEG1 = NEG1;
module.exports.PAGESIZE = PAGESIZE;
module.exports.PAGESHIFT = PAGESHIFT;
const unusedCRAMFields = `U0,U21,U23,U42,U45,U48,U51,U73`.split(/,/);
module.exports.unusedCRAMFields = unusedCRAMFields;
// EBOX notes:
//
// M8539 APR module is replaced by M8545 in KL10 model B.
// M8523 VMA module is replaced by M8542 in KL10 model B.
////////////////////////////////////////////////////////////////
// Handy utility functions
// For each XXXXis(name) function, return 1n iff XXXX/${name} is true.
const SPECis = matcherFactory('SPEC');
const CONDis = matcherFactory('COND') ;
const DISPis = matcherFactory('DISP') ;
const VMAXis = matcherFactory('VMAX');
const MEMis = matcherFactory('MEM');
const BRis = matcherFactory('BR');
const BRXis = matcherFactory('BRX');
// Return 1n iff CR[fieldName] has value of the constant
// fieldValueName defined for that field.
function fieldIs(fieldName, fieldValueName) {
return BigInt(CR[fieldName].get() === CR[fieldName][fieldValueName]);
}
// Returns a matcher function to match CR field `fieldName` against
// its constant value named by the function parameter.
function matcherFactory(fieldName) {
return name => {
const toMatch = CR[fieldName][name];
const cur = CR[fieldName].get();
return cur === toMatch;
}
}
function CONDControlFactory(fieldValueName) {
return {
get() {return CONDis(fieldValueName)}
};
}
////////////////////////////////////////////////////////////////
// Wire up an EBOX block diagram.
////////////////////////////////////////////////////////////////
// Name our instances for debugging and display. Every single Named
// unit is remembered in the Named.units object.
const Named = StampIt({name: 'Named'}).statics({
units: {}, // Dictionary of all Named instances
// This is the list of property names that should be fixed up. That
// is, replace their string values with the result of evaluating
// that string at the end of the module.
mayFixup: `input,inputs,control,addr,matchValue,enableF,loBits,hiBits`.split(/,\s*/),
// This is the STAMP (not instance) method invoked at the end of
// this module to fix up all of the `mayFixup` properties on all
// stamps we know about.
fixupForwardReferences(){
Object.values(Named.units).forEach(unit => unit.fixupForwardReferences());
},
}).init(function({name}, {stamp}) {
this.name = name;
this.stamp = stamp;
this.propName = this.name.replace(/[ .,]/g, '_');
Named.units[this.propName] = this;
this.doNotWrap = {};
}).methods({
reset() { },
debugBanner(msg) {
console.log(`${this.name}: ` + centeredBanner(msg, 80 - this.name.length));
},
// This is called on every instance to fix up forward references
// found in its `mayFixup` stamp static property, which is an array
// of property names whose value is a string to be evaluated at the
// end of the module to replace its value.
fixupForwardReferences() {
const that = this;
this.stamp.mayFixup
.filter(prop => typeof that[prop] === typeof 'string')
.forEach(prop => {
const asString = that[prop];
that[prop] = eval(asString);
});
},
});
module.exports.Named = Named;
// Each Clock has a list of units it drives. Calling the `latch()` or
// `sampleInputs()` method on a Clock calls that same method each driven
// unit.
const Clock = Named.init(function({drives = []}) {
this.drives = drives;
this.wrappableMethods = `cycle,addUnit`.split(/,\s*/);
this.phase = 'INIT';
}).methods({
addUnit(unit) { this.drives.push(unit) },
cycle() {
if (EBOX.debugCLOCK) console.log(`<< ${this.name} cycle SAMPLE`);
this.phase = 'SAMPLE';
this.drives.forEach(unit => {
assert(typeof unit.sampleInputs === typeofFunction, `${unit.name} sampleInputs() must exist`);
if (unit.clockGate()) unit.sampleInputs();
});
if (EBOX.debugCLOCK) console.log(`<< ${this.name} cycle LATCH`);
this.phase = 'LATCH';
this.drives.forEach(unit => {
assert(typeof unit.latch === typeofFunction, `${unit.name} latch() must exist`);
if (unit.clockGate()) unit.latch();
});
if (EBOX.debugCLOCK) console.log(`<< ${this.name} cycle END`);
this.phase = 'END';
},
});
module.exports.Clock = Clock;
// Used for non-clocked objects like ZERO and ONES. This is never
// actually cycled.
const NOCLOCK = Clock({name: 'NOCLOCK'});
module.exports.NOCLOCK = NOCLOCK;
// The EBOX-wide clock that drives the whole kaboodle.
const EBOXClock = Clock({name: 'EBOXClock'});
module.exports.EBOXClock = EBOXClock;
// The CRAM clock which is stepped AFTER everything has settled to
// prep for the next cycle.
const CRAMClock = Clock({name: 'CRAMClock'});
module.exports.CRAMClock = CRAMClock;
////////////////////////////////////////////////////////////////
// The rules for a Unit:
//
// * Clocked units can only change their state in latch(). Calls to
// get() must be idempotent and pure.
//
// * Combinatorial units have no state. Calls to get() are idempotent
// and pure.
////////////////////////////////////////////////////////////////
//
// Derived from p. 212 Figure 3-5 Loading IR Via FM (COND/LOAD IR) and
// p. 214 Figure 3-7 NICOND Dispatch and Waiting.
//
// │[=========]<---sampleInputs() regs recursively load inputs to toLatch, output remains stable
// │[=========]<---non-regs settle inputs --> outputs
// │ │[========]<---latch() regs drive toLatch on their outputs
// __│ │ __________│ │ __________│ │ _
// \ │/ \ │/ \ │/ EBOX CLOCK
// |\__________/ │\__________/ │\_________/
// │ │ │ │ │ │
// │ __________│ │ __________│ │ _________│
// |/ │ │/ \ │/ \ EBOX SYNC
// __/ │\__________/ │\__________/ │\_
// │ │ │ │ │ │
// __│ __________│ __________│___________│ __________│__________│ _
// \/UNLATCHED \/ μinsn1 │ UNLATCHED \/ μinsn2 │UNLATCHED \/ CR
// __/\__________/\__________│___________/\__________│__________/\_
// │ │ │ │ │ │
// __│___________│___________│___________│ │ │
// │ │ │ \ │ │ NICOND DISP
// │ │ │ │\__________│__________│__
// │ │ │ │ │ │
// │ │ │ │ __________│__________│
// │ │ │ │/ │ \ CON LOAD DRAM
// __│___________│___________│___________/ │ │\_
// │ │ │ │ │ │
// │ │ │ │ __________│ │ _
// │ │ UNLATCHED │/ LATCHED \ UNLATCHED│/ DR
// __│___________│___________│___________/ │\_________/
// │ │ │ │ │ │
// │ │ │ │ __________│__________│
// │ │ UNLATCHED │/ LATCHED │ \ IR
// __│___________│___________│___________/ │ │\_
// │ │ │ │ │ │
// ^ │ ^ │ ^ │
// └───────────│───────────┴───────────│───────────┴──────────│── latch(): value = toLatch
// ^ ^ ^
// └───────────────────────┴──────────────────────┴─── sampleInputs(): toLatch = input.get()
// (reg outputs remain stable)
// (regs get non-reg state recursively)
////////////////////////////////////////////////////////////////
//
// Call Flow
//
// get() retrieves the output value of a Unit. Use this to retrieve
// the current latched value of a Clocked Unit or output of a
// Combinatorial Unit.
//
// All sampleInputs(), recursively calling unit.get().
// All latch(), registers saving value from toLatch.
////////////////////////////////////////////////////////////////
// Stamps
//
// Base Stamp for EBOX functional units. This is an abstract Stamp
// defining protocol but all should be completely overridden.
//
// The convention is that each EBOXUnit has a `getInputs()` method
// that retrieves the unit's value based on its current inputs and
// control and address and configuration.
//
// There is also a `get()` method to retrieve the current _output_ of
// the Unit.
//
// Combinatorial (not clocked) logic units simply implement `get()` as
// directly returning the value from `getInputs()`. Since these are
// not clocked, they implement a no-op for `latch()` and
// `sampleInputs()`. Registered and RAMs (Clocked) return the
// currently addressed or latched value in `get()` and use
// `getInputs()` (and `getControl()` and `getAddress()`) to determine
// what value to latch during `sampleInputs()`. When `latch()` is
// called, the output of a Clocked Unit reflects what was loaded
// during `sampleInputs()`.
//
// * `sampleInputs()` uses `getInputs()`, `getControl()` and
// `getAddress()` to save a register or RAM's value and is a no-op
// for combinatorial units.
//
// * `latch()` makes the saved value the new output of the unit.
//
// * `get()` returns current output value. For combinatorial units
// this just returns the transformed inputs.
//
const EBOXUnit = Named.compose({name: 'EBOXUnit'}).init(
function({name, bitWidth, input, inputs, addr, control, debugFormat = 'octW'}) {
this.name = name;
this.input = input; // We can use singular OR plural
this.inputs = inputs; // but not both (`input` wins over `inputs`)
this.control = control;
this.addr = addr;
assert(typeof bitWidth === 'bigint' || typeof bitWidth === 'number', `${name} needs to define bitWidth`);
this.bitWidth = BigInt(bitWidth);
this.ones = (1n << this.bitWidth) - 1n;
this.value = this.toLatch = 0n;
this.debugFormat = debugFormat;
this.wrappableMethods = `\
reset,cycle,getAddress,getControl,getInputs,get,latch,sampleInputs
`.trim().split(/,\s*/);
}).props({
value: 0n,
}).methods({
// Stringify a value of our `this.value` type.
vToString(v) { return utilFormats[this.debugFormat](v, Math.ceil(Number(this.bitWidth) / 3)) },
reset() {
this.value = 0n;
},
getAddress() {
assert(this.addr, `${this.name}.addr not null`);
return this.addr.get();
},
getControl() {
assert(this.control, `${this.name}.control not null`);
return BigInt(this.control.get());
},
getInputs() {
if (this.clock) assert(this.clock.phase === 'SAMPLE',
`${this.name}.getInputs must only be called in SAMPLE phase`);
assert(typeof this.input.get === typeofFunction,
`${this.name}.input=${this.input.name}.get must be a function`);
return this.input.get();
},
getLH(v = this.value) { return (v >> 18n) & RHMASK },
getRH(v = this.value) { return v & RHMASK },
joinHalves(lh, rh) { return (lh << 18n) | (rh & RHMASK) },
});
module.exports.EBOXUnit = EBOXUnit;
////////////////////////////////////////////////////////////////
// The EBOX.
const EBOX = StampIt.compose(Named, {
}).init(function ({serialNumber}) {
this.serialNumber = serialNumber;
this.microInstructionsExecuted = 0n;
this.executionTime = 0;
this.debugNICOND = false;
this.debugCLOCK = false;
this.debugFlags = `NICOND,CLOCK`.split(/,\s*/);
}).props({
unitArray: [], // List of all EBOX Units as an array of objects
clock: EBOXClock,
wrappableMethods: `cycle,reset`.split(/,\s*/),
memCycle: false, // Initiated by MEM/xxx, cleared by MEM/MB WAIT
fetchCycle: false, // Initiated by MEM/IFET, cleared by MEM/MB WAIT
run: false, // CPU is RUNNING
start: false, // CONTINUE button pressed
// Pager state.
userBase: 0n,
execBase: 0n,
}).methods({
// Exec/User process table accessors.
gettEPT(offset) {return MBOX.data[(this.execBase << PAGESHIFT) + offset]},
putEPT(offset, value) {MBOX.data[(this.execBase << PAGESHIFT) + offset] = value},
gettUPT(offset) {return MBOX.data[(this.userBase << PAGESHIFT) + offset]},
putUPT(offset, value) {MBOX.data[(this.userBase << PAGESHIFT) + offset] = value},
reset() {
// Substitute real object references for strings we placed in
// properties whose names are in `unit.stamp.mayFixup`.
Named.fixupForwardReferences();
this.unitArray = [...Object.values(Named.units), this];
this.resetActive = true;
this.ucodeRun = false;
this.memCycle = false;
this.fetchCycle = false;
this.run = false;
this.start = false;
this.userBase = 0n;
this.execBase = 0n;
// Reset every Unit back to initial value.
this.unitArray.filter(unit => unit !== this).forEach(unit => unit.reset());
// RESET always generates several clocks with zero CRAM and DRAM.
CRAMClock.cycle(); // Explicitly set up CRAM for this cycle.
_.range(4).forEach(k => this.cycle());
this.resetActive = false;
},
cycle() {
if (EBOX.debugCLOCK) console.log(`<< ${this.name} cycle BEGIN`);
// Microcode stops the CPU on execution of HALT instruction
if (SPECis('FLAG CTL') && fieldIs('FLAG CTL', 'HALT')) this.run = false;
this.clock.cycle(this.debugCLOCK);
CRAMClock.cycle(this.debugCLOCK); // Explicitly set up CRAM for next cycle.
if (EBOX.debugCLOCK) console.log(`>> ${this.name} cycle END`);
++this.microInstructionsExecuted;
},
// Used by doExamine to allow arbitrary expression evaluation for
// debugging.
eval(s) {
return moduleEval(s);
},
}) ({name: 'EBOX', serialNumber: 0o6543n});
module.exports.EBOX = EBOX;
function moduleEval(s) {
return eval(s);
}
module.exports.moduleEval = moduleEval;
// Use this mixin to define a Combinatorial unit that is unclocked.
const Combinatorial = EBOXUnit.compose({name: 'Combinatorial'}).init(function() {
this.doNotWrap.reset = true;
this.doNotWrap.latch = true;
this.doNotWrap.sampleInputs = true;
this.doNotWrap.cycle = true;
}).methods({
// None of these methods applies to non-clocked combinatorial logic.
sampleInputs() { console.error(`ERROR: ${this.name}.sampleInputs() called`); },
latch() { console.error(`ERROR: ${this.name}.latch() called`); },
cycle() { console.error(`ERROR: ${this.name}.cycle() called`); },
get() {
assert(typeof this.getInputs() === 'bigint', `${this.name}.getInputs returned non-BigInt`);
return this.value = this.getInputs() & this.ones;
},
});
// Use this stamp to define a Clocked unit.
const Clocked = EBOXUnit.compose({name: 'Clocked'})
.init(
function({clockGate = () => 1, clock = EBOXClock}) {
assert(!clockGate || typeof clockGate === typeofFunction,
`${this.name}.clockGate must be a function`);
this.clockGate = clockGate;
this.clock = clock;
clock.addUnit(this);
}).methods({
sampleInputs() {
assert(typeof this.getInputs === typeofFunction,
`${this.name}.getInputs (Clocked) must be a function`);
this.toLatch = this.getInputs();
},
cycle() {
if (this.clockGate()) {
this.sampleInputs();
this.latch();
}
},
latch() { this.value = this.toLatch },
get() { return this.value },
});
////////////////////////////////////////////////////////////////
// BitField definition. Define with a specific name. Convention is
// that `s` is PDP10 numbered leftmost bit and `e` is PDP10 numbered
// rightmost bit in field. So number of bits is `e-s+1`.
// Not an EBOXUnit, just a useful stamp.
const BitField = Combinatorial.compose({name: 'BitField'}).init(function({name, s, e}) {
this.name = name;
this.s = s;
this.e = e;
}).methods({
reset() {
assert(this.input, `${this.name}.input not null`);
this.wordWidth = Number(this.input.bitWidth);
assert(typeof this.input.bitWidth === 'bigint', `${this.name} must input.bitWidth`);
this.shift = shiftForBit(this.e, this.input.bitWidth);
},
getInputs() {
const v = this.input.get();
assert(typeof this.shift === 'bigint', `${this.name} must define bigint shift`);
const shifted = v >> this.shift;
return shifted & this.ones;
},
});
module.exports.BitField = BitField;
////////////////////////////////////////////////////////////////
// Use this for inputs that are always zero.
const ConstantUnit = Combinatorial.compose({name: 'ConstantUnit'}).init(function ({value = 0, bitWidth}) {
value = BigInt(value);
this.value = value >= 0n ? value : value & this.ones;
this.doNotWrap.reset = true;
this.doNotWrap.latch = true;
this.doNotWrap.get = true;
}).methods({
getInputs() { return this.value },
// Neuter to prevent this.value being replaced with 0n
reset() {},
});
module.exports.ConstantUnit = ConstantUnit;
const ZERO = ConstantUnit({name: 'ZERO', bitWidth: 36, value: 0n});
module.exports.ZERO = ZERO;
const ONES = ConstantUnit({name: 'ONES', bitWidth: 36, value: (1n << 36n) - 1n});
module.exports.ONES = ONES;
////////////////////////////////////////////////////////////////
// Take a group of inputs and concatenate them into a single wide
// field.
const BitCombiner = Combinatorial.compose({name: 'BitCombiner'}).init(function() {
}).methods({
getInputs() {
assert(this.inputs, `${this.name}.inputs not null`);
const combined = this.inputs.reduce((v, i) => (v << i.bitWidth) | i.get(), 0n);
return this.value = combined & this.ones;
},
});
module.exports.BitCombiner = BitCombiner;
////////////////////////////////////////////////////////////////
// Given an address, retrieve the stored word or if `isWrite()` write
// the value presented via `sampleInputs()`.
const RAM = Clocked.compose({name: 'RAM'}).init(function({nWords, initValue = 0n}) {
this.nWords = nWords;
this.initValue = initValue;
}).methods({
RAMreset() {
if (this.bitWidth <= 64 && this.initValue === 0n) {
this.data = new BigUint64Array(this.nWords);
} else {
this.data = _.range(Number(this.nWords)).map(x => this.initValue);
}
this.latchedAddr = 0n;
this.toLatch = this.value = this.initValue;
this.ones = (1n << this.bitWidth) - 1n;
},
reset() { this.RAMreset() },
sampleInputs() {
this.latchedIsWrite = this.isWrite();
this.latchedAddr = this.getAddress();
if (this.latchedIsWrite) { // Write
this.toLatch = this.getInputs() & this.ones;
} else { // Read
// RAMs act as Combinatorial, passing through to their outputs
// whatever is addressed during read.
this.value = this.toLatch = this.data[this.latchedAddr] & this.ones;
}
},
latch() {
// Read was already handled during sampleInputs()
if (this.latchedIsWrite) { // Write
this.value = this.data[this.latchedAddr] = this.toLatch;
}
},
// Default behavior
isWrite() {
return !!this.getControl(); // Active-high truthy WRITE control
},
});
module.exports.RAM = RAM;
////////////////////////////////////////////////////////////////
// Given a control input and a series of selectable inputs, produce
// the value of the selected input on the output.
const Mux = Combinatorial.compose({name: 'Mux'}).init(function({enableF = () => 1n}) {
this.enableF = enableF;
}).methods({
get() {
const controlValue = this.getControl();
const input = this.inputs[controlValue];
assert(this.enableF, `${this.name}.enableF not null`);
assert(input, `${this.name}.get(): input not null for controlValue=${controlValue}.`);
return this.enableF() ? input.get() & this.ones : 0n;
},
});
module.exports.Mux = Mux;
////////////////////////////////////////////////////////////////
// Latch input for later retrieval.
const Reg = Clocked.compose({name: 'Reg'});
module.exports.Reg = Reg;
////////////////////////////////////////////////////////////////
// Given a function selector and a set of inputs, compute a set of
// results.
const LogicUnit = Combinatorial.compose({name: 'LogicUnit'});
module.exports.LogicUnit = LogicUnit;
////////////////////////////////////////////////////////////////
// Given a function selector and a set of inputs, compute a set of
// results.
const ShiftMult = Combinatorial.compose({name: 'ShiftMult'})
.init(function({shift = 1, loBits = ZERO}) {
this.shift = BigInt(shift);
this.loBits = loBits;
}).methods({
get() {
const inp = this.input.get();
assert(this.loBits, `${this.name}.loBits not null`);
const loBits = this.loBits.get() >> (this.loBits.bitWidth - this.shift);
return (inp << this.shift | loBits) & this.ones;
},
});
module.exports.ShiftMult = ShiftMult;
////////////////////////////////////////////////////////////////
// Given a function selector and a set of inputs, compute a set of
// results.
const ShiftDiv = Combinatorial.compose({name: 'ShiftDiv'})
.init(function({shift = 1, hiBits = ZERO}) {
this.shift = BigInt(shift);
this.hiBits = hiBits;
}).methods({
getInputs() {
assert(this.hiBits, `${this.name}.hiBits not null`);
const hiBits = this.hiBits.get() << (this.hiBits.bitWidth - this.shift);
assert(this.input, `${this.name} must have input`);
assert(typeof this.input.get === typeofFunction, `${this.name} must have input.get()`);
return ((this.input.get() | hiBits) >> this.shift) & this.ones;
},
});
module.exports.ShiftDiv = ShiftDiv;
////////////////////////////////////////////////////////////////
// RAMs
// Control RAM: Readonly by default. This is not a RAM object because
// we explicitly clock it and manage its settled value for each EBOX
// cycle in EBOX. The CRAM value never changes during a cycle; it only
// changes in the time between cycles.
const CRAM = RAM.methods({
reset() {
this.RAMreset();
this.force1777 = false; // Set by page fault conditions
this.stack = [];
},
// Our "front end" always writes directly to CRAM.data.
isWrite() { return false },
get() { return this.value },
getAddress() {
// XXX Still remaining:
// * A7: The conditions from Muxes E1,E32 CRA2
// * A8: The conditions from Muxes E2,E16 CRA2
// * A9: The conditions from Muxes E6,E26 CRA2
// * A10: The conditions from Muxes E22,E31,E36,E39 CRA2
// * A10: CON COND ADR 10 CRA2
// * Note changes in p. 234 of EBOX 006 C.8 MODULE M8541.
if (this.force1777) {
this.force1777 = false; // Handled.
// Push return address from page fault handler.
// Push VALUE not TOLATCH.
this.stack.push(this.latchedAddr);
// Force next address to 0o1777 ORed with current MSBs.
return this.latchedAddr | 0o1777n;
}
const skip = CR.SKIP.get();
let orBits = 0n;
// Determine if LSB is forced to '1' by a skip test condition or
// If DISP/MUL finds sign(FE) == 0.
switch(skip) {
default:
break;
case CR.SKIP.RUN:
if (EBOX.run) orBits = 0o1n;
break;
case CR.SKIP.KERNEL:
orBits = 0o1n; // XXX for now we are always in KERNEL mode
break;
case CR.SKIP['-START']:
if (EBOX.start) {
EBOX.start = false; // Clear flag like it timed out in hardware
} else {
orBits |= 1n;
}
break;
case CR.SKIP.FETCH: // Soon
case CR.SKIP.USER:
case CR.SKIP.PUBLIC:
case CR.SKIP['RPW REF']:
case CR.SKIP['PI CYCLE']:
case CR.SKIP['-EBUS GRANT']:
case CR.SKIP['-EBUS XFER']:
case CR.SKIP.INTRPT:
case CR.SKIP['IO LEGAL']:
case CR.SKIP['P!S XCT']:
case CR.SKIP['-VMA SEC0']:
case CR.SKIP['AC REF']: // Soon
case CR.SKIP['-MTR REQ']:
break; // XXX these need to be implemented
}
const disp = CR.DISP.get();
switch (disp) {
// See schematics p. 344 and
// EK-EBOX-all p. 256 for explanation.
// FE0*4 + MQ34*2 + MQ35; implies MQ SHIFT, AD LONG
case CR.DISP.MUL:
if (FE.get() >= 0o1000n) orBits |= 0o04n;
orBits |= MQ.get() & 3n;
break;
case CR.DISP['DRAM J']:
orBits = DR.J.get() & 0o17n;
break;
case CR.DISP['RETURN']: // POPJ return--may not coexist with CALL
orBits = this.stack.pop();
break;
case CR.DISP['DRAM B']: // 8 WAYS ON DRAM B FIELD
// E.g., EXIT "DISP/DRAM B,MEM/B WRITE,J/ST0"
orBits = DR.B.get();
break;
case CR.DISP['EA MOD']: // (ARX0 or -LONG EN)*8 + -(LONG EN and ARX1)*4 +
// ARX13*2 + (ARX2-5) or (ARX14-17) non zero; enable
// is (ARX0 or -LONG EN) for second case. If ARX18
// is 0, clear AR left; otherwise, poke ARL select
// to set bit 2 (usually gates AD left into ARL)
// XXX this needs to be conditional on instructtion word
// format for byte pointers, etc.
if (IR.get() & X_NONZERO_MASK) orBits |= 0o01n;
if (IR.get() & INDEXED_MASK) orBits |= 0o02n;
break;
case CR.DISP['NICOND']: // NEXT INSTRUCTION CONDITION (see NEXT for detail)
if (EBOX.piCyclePending) {
if (EBOX.debugNICOND) console.log(`NICOND: CON PI CYCLE`);
} else if (!EBOX.run) {
orBits |= 0o02n; // -CON RUN (halt)
if (EBOX.debugNICOND) console.log(`NICOND: CON -RUN (halt)`);
} else if (EBOX.mtrIntReq) {
orBits |= 0o04n; // MTR INT REQ (meter interrupt)
if (EBOX.debugNICOND) console.log(`NICOND: MTR INT REQ (meter interrupt)`);
} else if (EBOX.intReq) {
orBits |= 0o06n; // INT REQ (interrupt)
if (EBOX.debugNICOND) console.log(`NICOND: INT REQ (interrupt)`);
} else if (EBOX.ucodeState05) {
orBits |= 0o10n; // STATE 05 Tracks enable
if (EBOX.trapReq) orBits |= 1n; // STATE 05 Tracks enable+TRAP
if (EBOX.debugNICOND) console.log(`NICOND: STATE 05 Tracks enable`);
} else if (!EBOX.vmaACRef) {
orBits |= 0o12n; // Normal instruction
if (EBOX.trapReq) orBits |= 1n; // normal instruction=TRAP
if (EBOX.debugNICOND) console.log(`NICOND: Normal instruction`);
} else {
orBits |= 0o16n; // AC ref and not PI cycle
if (EBOX.trapReq) orBits |= 1n; // AC ref and not PI cycle+TRAP
if (EBOX.debugNICOND) console.log(`NICOND: AC ref and not PI cycle`);
}
break;
case CR.DISP['DRAM A RD']:// IMPLIES INH CRY18
// 01 234 567 89A
const a = DR.A.get(); // Dispatch on DRAM A
// This condition matches CRA3 A .GE. 3 L
orBits |= a >= 3n ? a : (DR.J.get() & 0o1777n);
// If DR.A is IMMED-PF or READ-PF, prefetch next instruction.
switch(a) {
case DR.A['IMMED-PF']:
case DR.A['READ-PF']:
MBOX.startFetch();
break;
}
break;
case CR.DISP['BYTE']: // FPD*4 + AR12*2 + SCAD0--WRONG ON OVERFLOW FROM BIT 1!!
if (VMA_FLAGS.get() & maskForBit(4, VMA_FLAGS.bitWidth)) orBits |= 4n;
if (AR.get() & maskForBit(12, AR.bitWidth)) orBits |= 2n;
if (SCAD.get() & maskForBit(0, SCAD.bitWidth)) orBits |= 1n;
break;
case CR.DISP['DIAG']:
case CR.DISP['PG FAIL']: // PAGE FAIL TYPE DISP
case CR.DISP['SR']: // 16 WAYS ON STATE REGISTER
case CR.DISP['SH0-3']: // [337] 16 WAYS ON HIGH-ORDER BITS OF SHIFTER
case CR.DISP['DIV']: // FE0*4 + BR0*2 + AD CRY0; implies MQ SHIFT, AD LONG
case CR.DISP['SIGNS']: // ARX0*8 + AR0*4 + BR0*2 + AD0
case CR.DISP['NORM']: // See normalization for details. Implies AD LONG
break;
}
if (CR.CALL.get()) {
this.stack.push(this.latchedAddr);
}
return CR.J.get() | orBits;
},
}) ({name: 'CRAM', nWords: 2048, bitWidth: 84, clock: CRAMClock});
// This simply passes CRAM current addressed word through so we can
// extract bitfields.
const CR = Combinatorial({name: 'CR', bitWidth: 84, input: CRAM});
// Populate CR with properties representing CRAM bit fields. Ignore
// the unusued bits.
defineBitFields(CR, CRAMdefinitions, unusedCRAMFields);
// Mask for I bit in instruction word
const INDEXED_MASK = maskForBit(13);
// Mask for index register number in instruction word
const X_NONZERO_MASK = fieldMask(14, 18);
// Dispatch RAM: readonly by default
const DRAM = RAM.methods({
// Special method to use weird DRAM addressing to get DRAM word.
// We ignore `this.addr`.
//
// IR1 p. 128 shows DRAM address generation.
// DR ADR 00-02 = IR00-02.
// DR ADR 03-05 = INSTR 7XX ? (IR03-06 !== 0b1111 ? IR07-09<<1 :
// 012 345 678
getAddress() {
const ir = IR.get();
const op = fieldExtract(ir, 0, 8);
let result = op;
if ((op & 0o700n) === 0o700n) { // IO instructions are special
result = 0o700n | fieldExtract(ir, 7, 12);
if ((op & 0o074n) === 0o074n) result |= 0o070n; // A03-05
}
return this.value = result;
},
}) ({name: 'DRAM', nWords: 512, bitWidth: 24,
input: `ONES`, control: `ZERO`, addr: `CR.J`});
const DR = Reg.methods({
// Special override to handle special cases for DRAM read/dispatch
// logic.
get() {
const op = Number(IR.OP);
const ac = Number(IR.AC);
const indirect = Number(IR.INDIRECT);
const x = Number(IR.X);
let dw = DRAM.get();
if (op === 0o254) { // JRST
// JRST case clears J4,J7-10 and then substitutes AC number from
// IR for J7-10.
dw = dw & ~0o37 | ac;
}
return dw;
},
}) ({name: 'DR', bitWidth: 24, input: `DRAM`});
defineBitFields(DR, DRAMdefinitions);
// XXX this should probably be eliminated and replaced with explicit
// loading of CURRENT_BLOCK as a side effect of some other operation.
const LOAD_AC_BLOCKS = Clock({name: 'LOAD_AC_BLOCKS'});
const CURRENT_BLOCK = Reg({name: 'CURRENT_BLOCK', bitWidth: 3,
clock: LOAD_AC_BLOCKS, input: `EBUS`});
// Instruction register. This flows from CACHE (eventually) and AD.
// This is ECL 10173 with flow through input to output while !HOLD
// (HOLD DRAM B) whose rising edge latches input.
//
// MCL VMA FETCH & CON5 MEM CYCLE ==> CON5 FETCH CYCLE H (p.162).
//
// It looks to be clearer and simpler to just latch IR.value from the
// various sources (MBOX, COND/LOAD IR) while also driving the full
// instruction into ARX. We let IR_CLOCK handle the COND/LOAD IR
// condition normally while forcing the IR.value from the external
// sources when appropriate.
const COND_LOAD_IR = CR.COND['LOAD IR'];
const IR = Reg.init(function() {
const fields = {
op: {s: 0, e: 8},
ac: {s: 9, e: 12},
indirect: {s: 13, e: 13},
x: {s: 14, e: 17},
y: {s: 18, e: 35},
};
const that = this; // Create closure variable
// NOTE The fields we define here have getter/setter so you can
// directly reference them as IR members without using
// fieldName.get().
Object.entries(fields).forEach(([name, field]) => {
const {s, e} = field;
Object.defineProperty(that, name, {
configurable: true,
enumerable: true,
get() {return fieldExtract(that.value, s, e)},
set(v) {that.value = fieldInsert(that.value, v, s, e, that.bitWidth)},
});
});
}) ({name: 'IR', bitWidth: 36, input: `MBOX`, clockGate: () => CONDis('LOAD IR')});
// AC subfield of IR.
const IRAC = BitField({name: 'IRAC', s: 9, e: 12, bitWidth: 4, input: `IR`});
// State Register (internal machine state during instructions)
// This is CON3 E35 p. 150.
// This is also used for PXCT bits (see PXCT/=<75:77>).
// See SR_xxx macros for more info.
const SR = Reg({name: 'SR', bitWidth: 4, input: `CR['#']`,
clockGate: () => CONDis('SR_#')});
// EBOX STATE (UCODE STATE) Register (internal machine state during instructions)
// This is CON4 E50 p. 161.
// * MTR4 E54 input CON UCODE STATE 01 is an OR condition for MTR PERF CNT CLOCK
// * MTR2 E40 input CON UCODE STATE 03 controls MTR CACHE CNT EN
const EBOX_STATE = Reg({name: 'EBOX_STATE', bitWidth: 4, input: `CR['#']`,
clockGate: () => CONDis('EBOX STATE')});
const ALU10181 = StampIt.init(function({bitWidth = 36}) {
this.bitWidth = BigInt(bitWidth);
this.ones = (1n << this.bitWidth) - 1n;
}).props({
name: 'ALU10181',
INH_CRY18: 0o001, // specialFlags value to inhibit CRY18 for EA calc
}).methods({
add(a, b, cin = 0n, specialFlags = 0) {
if (specialFlags & this.INH_CRY18) { // Inhibit carry for EA calc
const sum18 = (a & RHMASK) + (b & RHMASK) + cin;
let sum = a + b + cin;
// Prevent carry out of bit 18
if (sum18 > RHMASK) sum -= RHMASK + 1n;
const cout = sum > this.ones ? 1n : 0n;
return {value: sum & this.ones, cout};
} else { // Normal case
const sum = a + b + cin;
const cout = sum > this.ones ? 1n : 0n;
return {value: sum & this.ones, cout};
}
},
sub(a, b, cin = 0n) {
const diff = a - b + cin;
const cout = diff > this.ones ? 1n : 0n; // XXX Need to support diff < 0