-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdp10.js
2072 lines (1977 loc) · 89.8 KB
/
pdp10.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
// Javascript emulation code for main CPU in a DECsystem-10 (PDP-10 KI10)
// See https://skn.noip.me/pdp10/pdp10.html for more information
// Quotation of the day: "36 bits is a royal pain when you only have 32 bits for bit operations!" - Me, Now!
const
BIT35 = 0o1,
BIT34 = 0o2,
BIT33 = 0o4,
BIT32 = 0o10,
BIT31 = 0o20,
BIT30 = 0o40,
BIT29 = 0o100,
BIT28 = 0o200,
BIT27 = 0o400,
BIT26 = 0o1000,
BIT25 = 0o2000,
BIT24 = 0o4000,
BIT23 = 0o10000,
BIT22 = 0o20000,
BIT21 = 0o40000,
BIT20 = 0o100000,
BIT19 = 0o200000,
BIT18 = 0o400000,
BIT17 = 0o1000000,
BIT16 = 0o2000000,
BIT15 = 0o4000000,
BIT14 = 0o10000000,
BIT13 = 0o20000000,
BIT12 = 0o40000000,
BIT11 = 0o100000000,
BIT10 = 0o200000000,
BIT9 = 0o400000000,
BIT8 = 0o1000000000,
BIT7 = 0o2000000000,
BIT6 = 0o4000000000,
BIT5 = 0o10000000000,
BIT4 = 0o20000000000,
BIT3 = 0o40000000000,
BIT2 = 0o100000000000,
BIT1 = 0o200000000000,
BIT0 = 0o400000000000, // 2^35
wordBase = 0x1000000000, // 2^36
wordMask = wordBase - 1,
wordSign = BIT0,
halfBase = BIT17,
halfMask = halfBase - 1,
halfSign = BIT18;
// Flags: AOV C0 C1 FOV FPD USR IOT PUB AFI TR2 TR1 FXU DCX 0 0 0 0 0
// 0 1 2 3 4 5 6 7 8 9 10 11 12
const
flagAOV = 0o400000, // - arithmetic overflow from ASH MUL FIX etc
flagC0 = 0o200000, // - carry in add/sub from bit 0
flagC1 = 0o100000, // - carry in add/sub from bit 1
flagFOV = 0o40000, // - floating overflow (exponent > 127) / underflow
flagFPD = 0o20000, // - first part done
flagUSR = 0o10000, // - processor in user mode 010000
flagIOT = 0o4000, // - processor in In Out mode 04000
flagPUB = 0o2000, // - public or exec supervisor mode
flagAFI = 0o1000, // - address failure inhibit
flagTR2 = 0o400, // - pushdown overflow
flagTR1 = 0o200, // - arithmetic overflow
flagFXU = 0o100, // - floating exponent underflow
flagDCX = 0o40, // - divide check , div by 0 etc
flagTR3 = flagTR1 | flagTR2; // both trap flags
const // accessType bit mask values
accessReadPXCT = 1, // Matches PXCT read modify bit
accessWritePXCT = 2, // Matches PXCT write modify bit
accessPXCT = accessReadPXCT | accessWritePXCT, // Any PXCT bits
accessRead = 16, // Read access requested
accessWrite = 32, // Write access
accessExecute = 64, // Execute (instruction fetch)
accessModify = accessRead | accessWrite;
var CPU = {
PC: 0, // instruction PC
PXCTflags: 0, // privilege XCT flags (these change register set and addressing so big impact)
accumulator: null, // current accumulator set (points into fastMemory array)
checkInterruptFlag: 1,
dataLights: 0,
execTable: 0, // base address of exec map
fastMemory: [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // Four blocks of 16 accumulators
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
],
flags: 0, // CPU flags
halt: 0, // 1 not running, 2 halt switch on
instructionLights: 0,
interruptMode: 0, // flag whether instruction is executing in interrupt mode - will change during some instructions
interruptQueue: [],
interruptSkip: 0,
lastMode: 0,
lastVirtualAddress: 0,
loopBase: 59, // number of instructions executed per 8ms
loopCount: 0, // instructions left in current 8ms batch
loopPause: 0, // slow instruction rate until disk I/O completes
memory: new Array(0o20000000).fill(0), // Main CPU.memory
miscLights: 3, // RUN & POWER
modifyAddress: -1, // last memory read address for memory rewrite cycle
modifyType: -1, // last memory read type (accumulator, physical memory etc) in case rewrite cycle required
pageEnable: 0, // paging on or off
pageFail: 0,
pageFault: 1,
pendingLights: 0,
resultAC: -1, // instruction result type (accumulator, virtual memory etc)
resultAddress: -1, // instruction result address (accumulator number, virtual address, etc)
smallUser: 0, // user mode small addressing on or off
userMode: 0, // current processor mode (may differ from flags during an interrupt)
userRegisterSet: 0, // Which accumulator set for usermode
userTable: 0 // base address of user map
};
function resetCPU() {
"use strict";
pi.coniStatus = 0; // Nuke status completely
CPU.interruptQueue = [];
CPU.checkInterruptFlag = 1;
writeFlags(0);
}
setUserMode(0);
// Instruction logging stuff - useful ONLY in browser debugging mode!!
var log = {
limit: 0, // Size of instruction log (0 is off)
debugPC: 0, // PC for debugging - used for setting browser breakpoint when the PC reaches this value
ring: [] // Data for instruction logging (for debugging)
};
// Debug routine to print the contents of the instruction log
function log_print() {
"use strict";
var i;
function toOct(n, l) {
if (typeof n === "undefined") return "undefined";
var o = n.toString(8);
if (o.length < l) o = "0".repeat(l - o.length) + o;
return o;
}
console.log("PC ACC EA flags Instruction Name AC");
for (i = 0; i < log.ring.length; i++) {
console.log(toOct(log.ring[i][0], 6) + " " + toOct(log.ring[i][1], 12) + " " + toOct(log.ring[i][2], 6) + " " + toOct(log.ring[i][3], 6) + " " + toOct(log.ring[i][4], 12) + " " + log.ring[i][5] + " " + log.ring[i][6].toString(8) + "," + right(log.ring[i][4]).toString(8));
}
return "";
}
// Add an intruction log debug entry
function LOG_INSTRUCTION(instruction, AC, effectiveAddress, name) {
"use strict";
if (log.limit) {
log.ring.push([CPU.PC, CPU.accumulator[AC], effectiveAddress, CPU.flags, instruction, name, AC]);
while (log.ring.length > log.limit) {
log.ring.shift();
}
if (CPU.PC - 1 == log.debugPC) {
console.log("DEBUG PC: " + (CPU.PC - 1).toString(8) + " " + CPU.accumulator[AC].toString(8) + " " + effectiveAddress.toString(8) + " " + CPU.flags.toString(8) + " " + instruction.toString(8) + " " + name + " " + AC.toString(8) + "," + right(instruction).toString(8));
log.debug = 2;
}
}
}
// Debugging check that a memory word is in range and has the correct type
function checkData(data) {
"use strict";
if (typeof data == "undefined" || !Number.isInteger(data) || data < 0 || data >= wordBase) {
console.log("Bad data: " + data + " @" + CPU.PC.toString(8));
panic(); //debug
}
}
// Debugging check that a memory address is in range and has the correct type
function checkVirtual(address) {
"use strict";
if (typeof address == "undefined" || !Number.isInteger(address) || address < 0 || address >= halfBase) {
panic(); //debug
}
}
// Generate a power of 2
function power2(power) { // Math.pow(2, power) is so slow....
"use strict";
var result = 1;
if (power >= 0) {
if (power < 31) {
return 1 << power;
}
while (power > 30) {
result *= 1 << 30;
power -= 30;
}
return result * (1 << power);
} else {
while (power < -30) {
result /= 1 << 30;
power += 30;
}
return result / (1 << -power);
}
}
function left(operand) { // extract left 18 bit halfword from full 36 bit word
"use strict";
return Math.trunc(operand / halfBase);
}
function right(operand) { // extract right 18 bit halfword from full 36 bit word
"use strict";
return operand & halfMask;
}
function combine(left, right) { // assemble 36 bit word from two 18 bit half words
"use strict";
return ((left & halfMask) * halfBase) + (right & halfMask);
}
function nextAC(AC) {
"use strict";
return (AC + 1) & 0xf;
}
function incrAddress(address) {
"use strict";
return (address + 1) & halfMask;
}
function setUserMode(userMode) {
"use strict";
if (userMode) {
CPU.userMode = 1;
CPU.accumulator = CPU.fastMemory[CPU.userRegisterSet];
} else {
CPU.userMode = 0;
CPU.accumulator = CPU.fastMemory[0];
}
}
function writeFlags(flags) {
"use strict";
if ((CPU.flags ^ flags) & flagUSR) { // If CPU mode is changing...
setUserMode(flags & flagUSR);
}
CPU.flags = flags;
}
function setFlags(mask) {
"use strict";
if (CPU.interruptMode) {
mask &= ~(flagAOV | flagTR1 | flagTR2); // Interrupt instructions don't set Overflow or either trap
}
if (mask & flagUSR) {
setUserMode(1);
}
CPU.flags |= mask;
}
function clearFlags(mask) {
"use strict";
if (mask & flagUSR) {
setUserMode(0);
}
CPU.flags &= ~mask;
}
function ifPrivilege() {
"use strict";
return (!(CPU.flags & flagPUB) && (!CPU.userMode || (CPU.flags & flagIOT)));
}
function inPublicMode() {
"use strict";
return (CPU.flags & flagPUB);
}
function getPCword() {
"use strict";
var saveFlags = CPU.flags;
if (!(CPU.flags & flagUSR)) {
saveFlags &= ~flagAOV; // flagAOV has special meaning in exec mode - don't save it
}
return combine(saveFlags, CPU.PC);
}
const operationName = ["", "I", "M", "S"];
const conditionName = ["", "L", "E", "LE", "A", "GE", "N", "G"];
function incrementWord(dst) {
"use strict";
dst++; // Increment
if (dst >= wordBase) {
dst -= wordBase;
if (dst == wordSign) {
setFlags(flagAOV | flagC1); // Set AOV, C1
} else {
setFlags(flagC0 | flagC1); // Set C0, C1
}
}
return dst;
}
function decrementWord(dst) {
"use strict";
dst += wordMask; // Decrement
if (dst >= wordBase) {
dst -= wordBase;
if (dst == wordSign) {
setFlags(flagAOV | flagC1); // Set AOV, C1
} else {
setFlags(flagC0 | flagC1); // Set C0, C1
}
}
return dst;
}
function compareWord(dst, operand) {
"use strict";
var result;
if (operand == dst) {
result = 0; // E
} else {
if (fromInteger(dst) >= fromInteger(operand)) {
result = 1; // G
} else {
result = wordSign; // L
}
}
return result;
}
function evaluateCondition(opCode, operand) {
"use strict";
switch (opCode & 0x7) {
case 1: // L
if (operand >= wordSign) {
return 1;
}
break;
case 2: // E
if (!operand) {
return 1;
}
break;
case 3: // LE
if (!operand || operand >= wordSign) {
return 1;
}
break;
case 4: // A
return 1;
case 5: // GE
if (operand < wordSign) {
return 1;
}
break;
case 6: // N
if (operand) {
return 1;
}
break;
case 7: // G
if (operand && operand < wordSign) {
return 1;
}
break;
}
return 0;
}
function jump(destination) {
"use strict";
CPU.PC = destination & halfMask;
}
function skip() {
"use strict";
jump(CPU.PC + 1);
}
function branchTest(opCode, operand, effectiveAddress) {
"use strict";
if (evaluateCondition(opCode, operand)) {
jump(effectiveAddress);
}
}
function skipTest(opCode, operand) { // For normal cases
"use strict";
if (evaluateCondition(opCode, operand)) {
skip();
}
}
function skipSpecial(condition) { // Skip special case for AOSX, SKIPX, SOSX, CONSX, BLKX for interrupt handling
"use strict";
if (condition) {
if (!CPU.interruptMode) { // Non-interrupt skip...
skip();
}
} else {
CPU.interruptSkip = 0; // AOSX, SKIPX, SOSX, CONSX, BLKX interrupt skip condition
}
}
function skipTestSpecial(opCode, operand) { // For AOSX, SKIPX, SOSX for interrupt cases
"use strict";
skipSpecial(evaluateCondition(opCode, operand));
}
function fromInteger(operand) { // Javascript number from PDP-10 integer word
"use strict";
if (operand >= wordSign) { // operand -ve
operand -= wordBase; // convert to real -ve integer
}
return operand;
}
function toInteger(operand) { // Javascript integer to PDP-10 word
"use strict";
if (operand >= 0) {
if (operand >= wordSign) {
setFlags(flagAOV | flagC1); // Set AOV, C1
operand %= wordSign;
}
} else {
operand = -operand;
if (operand > wordSign) {
setFlags(flagAOV | flagC0); // Set AOV, C0
operand %= wordSign;
if (!operand) {
operand = wordSign;
}
}
operand = wordBase - operand;
}
return operand;
}
function readWordByPhysical(physicalAddress) {
"use strict";
var data;
if (physicalAddress < 0 || physicalAddress >= BIT13) panic();
data = CPU.memory[physicalAddress];
//checkData(data);
return data;
}
function writeWordByPhysical(physicalAddress, data) {
"use strict";
if (physicalAddress < 0 || physicalAddress >= BIT13) panic();
//checkData(data);
CPU.memory[physicalAddress] = data;
return 0;
}
function readWordFromExecTable(tableAddress) {
"use strict";
if (CPU.pageEnable) {
tableAddress = (CPU.execTable + tableAddress) & halfMask;
}
return readWordByPhysical(tableAddress);
}
function writeWordToExecTable(tableAddress, data) {
"use strict";
if (CPU.pageEnable) {
tableAddress = (CPU.execTable + tableAddress) & halfMask;
}
return writeWordByPhysical(tableAddress, data);
}
function readWordFromUserTable(tableAddress) {
"use strict";
if (CPU.pageEnable) {
tableAddress = (CPU.userTable + tableAddress) & halfMask;
}
return readWordByPhysical(tableAddress);
}
function writeWordToUserTable(tableAddress, data) {
"use strict";
if (CPU.pageEnable) {
tableAddress = (CPU.userTable + tableAddress) & halfMask;
}
return writeWordByPhysical(tableAddress, data);
}
function readWordFromCurrentTable(tableAddress) {
"use strict";
if (CPU.userMode) {
return readWordFromUserTable(tableAddress);
} else {
return readWordFromExecTable(tableAddress);
}
}
// Source map: A P W S X 13 bit address
// Map output: F P W S N 13 bit address
// Map Fail --- U VirtPage --- O A W S T
// There are page tables for exec and user mode located using a base address for each.
// Exec addresses under 112K (0x1c000) are not paged, between 112K and 128K
// are paged using an extension to the USER map (!), and above 128K using the exec map.
// User addresses are all from the user map with enforcement for small
// mode addressing (addresses limited to 0-40000 and 400000-440000)
// Also need to homour public and concealed pages...
// Bit values for accessType: accessRead | accessWrite | accessExecute
// Return Fail P W S NotPaged Page (23-35) as per MAP instruction (pg 2-114)
function getVirtualMap(virtualAddress, accessType) {
"use strict";
var page, userMode, publicMode, pageData, pageFail;
if (virtualAddress < 0 || virtualAddress > halfMask) panic();
page = (virtualAddress >>> 9) & 0x1ff;
if (!CPU.pageEnable) {
return BIT22 | page; // NotPaged
}
userMode = CPU.userMode;
publicMode = CPU.flags & flagPUB;
if (accessType & accessPXCT) { // If any PXCT bits are set everything changes...
if (accessType & accessRead) { // read or read/write access
if (accessType & accessReadPXCT) { // PXCT read flag
if (CPU.flags & flagIOT) { // context user
userMode = 1;
}
if (CPU.flags & flagAOV) { // context public
publicMode = 1;
}
}
} else { // write only access
if (accessType & accessWritePXCT) { // PXCT read flag
if (CPU.flags & flagIOT) { // context user
userMode = 1;
}
if (CPU.flags & flagAOV) { // context public
publicMode = 1;
}
}
}
}
if (!userMode) { // Exec space...
if (page < 0o340) { // Low exec space is unmapped
if (publicMode) { // Check if public access allowed
if (!(accessType & accessExecute) || (left(CPU.memory[virtualAddress]) & 0o777040) != 0o0254040) {
CPU.pageFail = combine((userMode << 9) | page, 0o21);
return BIT18 | BIT22 | page; // Fail NotPaged 021 Proprietary violation
}
}
return BIT22 | page; // NotPaged
}
if (page < 0o400) { // Exec pages 340 - 400 are mapped by user map
pageData = readWordFromUserTable((page >>> 1) + 0o220); // User Process Table locations 400-417
} else { // Exec pages above 400 use exec map
pageData = readWordFromExecTable(page >>> 1); // Exec Process Table locations 200-377
}
} else { // All user pages from user map!!
if (CPU.smallUser && (page & 0o340)) { // Small user limited to pages 0-37 and 400-437
CPU.pageFail = combine((userMode << 9) | page, 0o20); // 020 Small user violation
return BIT18 | page; // Not sure if map data is required? no
}
pageData = readWordFromUserTable(page >>> 1); // User Process Table locations 0-377
}
if (!(page & 1)) {
pageData = left(pageData);
} else {
pageData = right(pageData);
}
// Check for access error
if (!(pageData & BIT18) || (!(pageData & BIT20) && (accessType & accessWrite))) {
pageFail = (pageData >> 13) & 0o6; // get W & S bits
if (pageData & BIT18) { // A bit
pageFail |= 0o10;
}
if (accessType & accessWrite) { // T bit
pageFail |= BIT35;
}
CPU.pageFail = combine((userMode << 9) | page, pageFail);
if (pageData & BIT18) {
return BIT18 | (pageData & 0o357777);
} else {
return 0o437777;
}
}
// Check if not public page not from public mode...
if (!(pageData & BIT19) && publicMode) { // Check if public access allowed
if (!(accessType & accessExecute) || (left(CPU.memory[((pageData & 0x1ff) << 9) | (virtualAddress & 0x1ff)]) & 0o777040) != 0o0254040) { // JRST 1 portal acceess
CPU.pageFail = combine((userMode << 9) | page, 0o21);
return BIT18 | (pageData & 0o357777); // Fail Paged 021 Proprietary violation
}
}
// Set public flag if executing from public page...
if ((pageData & BIT19) && (accessType & accessExecute)) {
setFlags(flagPUB);
}
CPU.lastVirtualAddress = virtualAddress;
CPU.lastMode = 0;
return pageData & 0o357777; // Success with page data
}
// Page Failures are simpler than page faults seen in other computer systems
// They prevent the current instruction from updating the accumulators, PC, etc
// and then if enabled cause an optional trap.
// This routine uses getVirtualMap() to figure out all the mapping rules
// mapVirtualToPhysical() Convert an 18 bit virtual address into a 22 bit physical address
function mapVirtualToPhysical(virtualAddress, accessType) {
"use strict";
var physicalAddress, pageData;
//checkVirtual(virtualAddress);
if (!CPU.pageEnable) { // Virtual if paging off
physicalAddress = virtualAddress;
} else {
pageData = getVirtualMap(virtualAddress, accessType);
if (pageData & BIT18) { // Page Failure
console.log("Pagefail " + virtualAddress.toString(8) + " " + accessType.toString(8) + " " + pageData.toString(8) + " " + CPU.savePC.toString(8) + " " + CPU.flags.toString(8) + " @"+CPU.PC.toString(8));
// if interrupt active then cause device interrupt on device error channel (pg 2-109)
// if (CPU.flags & flagAFI) { // then what?? depends on instruction fetch or operand Address Failure Inhibit flag
//if (!(accessType & accessExecute)) {
// CPU.PC = decrAddress(CPU.PC); //CPU.PC = CPU.savePC; // Reset PC - ?????
//}
if (right(CPU.pageFail) != 0o21 || (accessType & accessWrite)) {
CPU.PC = CPU.savePC; // Restore PC to except for proprietary read violation
} // Alternate would be defer proprietary violation handling to next instruction cycle?
if (!CPU.userMode) {
writeWordToUserTable(0o426, CPU.pageFail); // Yes - exec page fail writes to user table! :-(
} else {
writeWordToUserTable(0o427, CPU.pageFail);
}
if (!CPU.interruptMode) { // scope of this exception?
XCT(readWordFromCurrentTable(0o420), 1); // Execute 420 from current table (pagefail)
}
CPU.pageFault = 1;
physicalAddress = -1;
} else {
if (pageData & BIT22) { // Not mapped
physicalAddress = virtualAddress;
} else { // Mapped
physicalAddress = ((pageData & 0x1fff) << 9) | (virtualAddress & 0x1ff);
}
}
}
return physicalAddress;
}
function getWordByVirtual(virtualAddress, accessType) {
"use strict";
var addressType, data;
if (virtualAddress < 16) { // Accumulator virtualAddress
if (accessType & accessReadPXCT) { // PXCT read flag
if (CPU.flags & flagIOT) { // flagIOT is also Previous Context User flag
if (CPU.userRegisterSet) { // Selected user accumulator set
addressType = 2; // Alternate accumulator set
data = CPU.fastMemory[CPU.userRegisterSet][virtualAddress];
} else { // User shadow area
if ((virtualAddress = mapVirtualToPhysical(virtualAddress, accessType)) < 0) { // Convert physical address to virtual
return virtualAddress;
}
addressType = 1; // Memory virtualAddress
data = CPU.memory[virtualAddress];
}
} else { // AC stack
addressType = 3; // Executive stack in user table
data = readWordFromUserTable(pag.executiveStack + virtualAddress);
}
} else { // Normal accumulator
addressType = 0; // Normal accumulator
data = CPU.accumulator[virtualAddress];
}
} else { // Normal memory access
if ((virtualAddress = mapVirtualToPhysical(virtualAddress, accessType)) < 0) { // Convert physical virtualAddress to virtual
return virtualAddress;
}
addressType = 1; // Memory virtualAddress
data = CPU.memory[virtualAddress];
CPU.dataLights = data; // Not really but I dunno what it should display yet
}
//checkData(data);
if (accessType & accessWrite) {
CPU.modifyType = addressType;
CPU.modifyAddress = virtualAddress;
} else {
CPU.modifyType = -1; // DEBUG
}
return data;
}
function readWordByVirtual(virtualAddress) { // Normal case read word which honours PXCT flags
"use strict";
return getWordByVirtual(virtualAddress, accessRead | CPU.PXCTflags);
}
function writeWordByVirtual(virtualAddress, data) {
"use strict";
var physicalAddress;
//checkData(data);
if (virtualAddress < 16) { // Accumulator address
if (CPU.PXCTflags & accessWritePXCT) { // PXCT write flag - all writes honour PXCT flags
if (CPU.flags & flagIOT) { // flagIOT is Previous context user
if (CPU.userRegisterSet) { // Selected user block
CPU.fastMemory[CPU.userRegisterSet][virtualAddress] = data;
} else { // User shadow area
if ((physicalAddress = mapVirtualToPhysical(virtualAddress, accessWrite | CPU.PXCTflags)) < 0) {
return physicalAddress;
}
CPU.memory[physicalAddress] = data;
}
} else { // AC stack
return writeWordToUserTable(pag.executiveStack + virtualAddress, data);
}
} else { // Normal accumulator
CPU.accumulator[virtualAddress] = data;
}
} else { // Normal memory access
if ((physicalAddress = mapVirtualToPhysical(virtualAddress, accessWrite | CPU.PXCTflags)) < 0) {
return physicalAddress;
}
CPU.memory[physicalAddress] = data;
}
CPU.modifyType = -1; // DEBUG
return data;
}
// modifyWord() is used after a getWordByVirtual() call to rewrite or modify the content that was
// just read. CPU.modifyType remembers whether the source was an accumulator, memory, a user register
// or the executive stack, while CPU.modifyAddress remembers the appropriate address.
function modifyWord(data) {
"use strict";
//checkData(data);
switch (CPU.modifyType) { // Set by getWordByVirtual()
case 0: // Current accumulator set
CPU.accumulator[CPU.modifyAddress] = data;
break;
case 1: // Memory access
CPU.memory[CPU.modifyAddress] = data;
break;
case 2: // Alternate accumulator set
CPU.fastMemory[CPU.userRegisterSet][CPU.modifyAddress] = data;
break;
case 3: // Executive stack in user table
return writeWordToUserTable(pag.executiveStack + CPU.modifyAddress, data);
default:
panic();
break;
}
CPU.modifyType = -1; // DEBUG
return 0;
}
// writeWordByOperand() will write a word to an accumulator or virtual address
// or both depending on the opCode
// Four types of write instructions: 0:M 1:I 2:M 3:S
// 0: C(AC)
// 1: C(AC)
// 2: C(E)
// 3: C(E) & C(AC)
function writeWordByOperand(opCode, data, AC, effectiveAddress) {
"use strict";
var result = 0;
opCode &= 0x3;
if (opCode >= 2) {
if ((result = writeWordByVirtual(effectiveAddress, data)) < 0) {
return result;
}
}
if (opCode != 2) {
CPU.accumulator[AC] = data;
}
return 0;
}
// readWordForOperand() reads a word for a general instruction and sets up
// CPU.resultAC and CPU.resultAddress so that a subsequent call to writeResult()
// will write to the correct location(s).
// Four types of general instruction operations:
// 0 C(AC) <- C(AC) . C(E)
// 1 I C(AC) <- C(AC) . 0,,E
// 2 M C(E) <- C(AC) . C(E)
// 3 B C(AC) <- C(AC) . C(E); C(E) <- C(AC)
function readWordForOperand(opCode, AC, effectiveAddress) {
"use strict";
CPU.resultAC = AC; // Default is use accumulator for writeResult()
CPU.resultAddress = -1; // -1 for no memory result (-2 is modify)
switch (opCode & 0x3) {
case 0: // C(AC) <- C(AC) . C(E)
return readWordByVirtual(effectiveAddress);
case 1: // C(AC) <- C(AC) . 0,,E
return effectiveAddress;
case 2: // C(E) <- C(AC) . C(E)
CPU.resultAC = -1; // No accumulator result
CPU.resultAddress = -2; // -2 for memory modify cycle
return getWordByVirtual(effectiveAddress, accessModify | CPU.PXCTflags);
case 3: // C(AC) <- C(AC) . C(E); C(E) <- C(AC)
CPU.resultAddress = -2; // -2 for memory modify cycle
return getWordByVirtual(effectiveAddress, accessModify | CPU.PXCTflags);
}
}
// readWordForMove() reads a word for a move type instruction and sets up
// CPU.resultAC and CPU.resultAddress so that a subsequent call to writeResult()
// will write to the correct location(s).
// Four types of move instructions:-
// C(AC) <- C(E)
// C(AC) <- 0,,E
// C(E) <- C(AC)
// C(E) <- C(E); if AC#0 then C(AC) <- C(E)
function readWordForMove(opCode, AC, effectiveAddress) {
"use strict";
CPU.resultAC = AC; // Default is use accumulator for writeResult()
CPU.resultAddress = -1; // -1 is don't write a result (-2 is modify)
switch (opCode & 0x3) {
case 0: // C(AC) <- C(E)
return readWordByVirtual(effectiveAddress);
case 1: // C(AC) <- 0,,E
return effectiveAddress;
case 2: // C(E) <- C(AC)
CPU.resultAC = -1; // No accumulator result
CPU.resultAddress = effectiveAddress;
return CPU.accumulator[AC];
case 3: // C(E) <- C(E); if AC#0 then C(AC) <- C(E)
if (!AC) {
CPU.resultAC = -1;
}
CPU.resultAddress = -2; // -2 a modify cycle
return getWordByVirtual(effectiveAddress, accessModify | CPU.PXCTflags);
}
}
// writeResult() writes an operand to the location(s) set up by prior
// calls to the readWordForOperand() and readWordForMove() functions.
// The location(s) for the write type are remembered in two variables:
// CPU.resultAC which is the accumulator to write to (or -1 if none)
// CPU.resultAddress which is the virtual address to write to (or -1 if none, or -2 for a modify)
function writeResult(data) {
"use strict";
var result;
if (CPU.resultAddress == -1 && CPU.resultAC < 0) panic();
if (CPU.resultAddress >= 0) {
if ((result = writeWordByVirtual(CPU.resultAddress, data)) < 0) {
return result;
}
} else {
if (CPU.resultAddress === -2) { // Modify the word read previously
if ((result = modifyWord(data)) < 0) {
return result;
}
}
}
if (CPU.resultAC >= 0) {
CPU.accumulator[CPU.resultAC] = data;
}
CPU.resultAC = -1; // DEBUG clear address fields
CPU.resultAddress = -1;
return 0;
}
const halfWordNames = [
"HLL", "XHLLI", "HLLM", "HLLS", "HRL", "HRLI", "HRLM", "HRLS",
"HLLZ", "HLLZI", "HLLZM", "HLLZS", "HRLZ", "HRLZI", "HRLZM", "HRLZS",
"HLLO", "HLLOI", "HLLOM", "HLLOS", "HRLO", "HRLOI", "HRLOM", "HRLOS",
"HLLE", "HLLEI", "HLLEM", "HLLES", "HRLE", "HRLEI", "HRLEM", "HRLES",
"HRR", "HRRI", "HRRM", "HRRS", "HLR", "HLRI", "HLRM", "HLRS",
"HRRZ", "HRRZI", "HRRZM", "HRRZS", "HLRZ", "HLRZI", "HLRZM", "HLRZS",
"HRRO", "HRROI", "HRROM", "HRROS", "HLRO", "HLROI", "HLROM", "HLROS",
"HRRE", "HRREI", "HRREM", "HRRES", "HLRE", "HLREI", "HLREM", "HLRES"
];
// Halfword |R right to |R right | no modification of other half | from memory to AC
// |L left |L left |Z zero other half |I Immediate
// |O set other half to ones |M from AC to memory
// |E sign extend source to other half |S to self. If AC#0, then move to AC also.
// Example of a halfWord op:
// HRL CL(AC) <- CR(E)
// HRLI CL(AC) <- E
// HRLM CL(E) <- CR(AC)
// HRLS CL(E) <- CR(E); if AC#0 then CL(AC) <- CR(E)
function halfWord(opCode, AC, effectiveAddress) {
"use strict";
var operand, dst;
if ((dst = readWordForMove(opCode, AC, effectiveAddress)) >= 0) {
if ((opCode ^ (opCode >>> 3)) & 0x4) { // source half depends on which block of opCodes
operand = right(dst);
} else {
operand = left(dst);
}
switch ((opCode >> 3) & 0x3) { // four types of fill: insert, zero, ones, sign extend
case 0: // insert into destination (so need destination)
switch (opCode & 0x3) {
case 2: // Shame this is needed - doesn't fit in well at all
if ((dst = readWordByVirtual(effectiveAddress)) < 0) {
return dst;
}
break;
case 3:
break; // do nothing as dst preloaded above
default: // preload from acc
dst = CPU.accumulator[AC];
break;
}
break;
case 1: // zero fill
dst = 0;
break;
case 2: // 1 fill
dst = wordMask;
break;
case 3: // extend
if (operand & halfSign) {
dst = wordMask;
} else {
dst = 0;
}
break;
}
if (opCode & BIT30) {
dst = combine(left(dst), operand);
} else {
dst = combine(operand, right(dst));
}
writeResult(dst);
}
}
const bitTestNames = [
"TRN", "TLN", "TRNE", "TLNE", "TRNA", "TLNA", "TRNN", "TLNN",
"TDN", "TSN", "TDNE", "TSNE", "TDNA", "TSNA", "TDNN", "TSNN",
"TRZ", "TLZ", "TRZE", "TLZE", "TRZA", "TLZA", "TRZN", "TLZN",
"TDZ", "TSZ", "TDZE", "TSZE", "TDZA", "TSZA", "TDZN", "TSZN",
"TRC", "TLC", "TRCE", "TLCE", "TRCA", "TLCA", "TRCN", "TLCN",
"TDC", "TSC", "TDCE", "TSCE", "TDCA", "TSCA", "TDCN", "TSCN",
"TRO", "TLO", "TROE", "TLOE", "TROA", "TLOA", "TRON", "TLON",
"TDO", "TSO", "TDOE", "TSOE", "TDOA", "TSOA", "TDON", "TSON"
];
// bit Test |R right half immediate |N no modification | never skip
// |L left half immediate |Z zero selected bits |N skip unless all selected bits are zero
// |D direct mask |O set selected bits to One |E skip if all selected bits are zero
// |S swapped mask |C complement selected bits |A skip always
// Example of a bit Test op:
// TSC Test Swapped, Complement, but Do Not Skip
// TSCE Test Swapped, Complement, and Skip if All Masked Bits Equal zero
// TSCA Test Swapped, Complement, but Always Skip
// TSCN Test Swapped, Complement, and Skip if Not All Masked Bits Equal zero
function bitTest(opCode, AC, effectiveAddress) {
"use strict";
var operand, dst;
switch (opCode & 0x9) { // Note: non-consecutive bits in mask
case 0: // R right half immediate
operand = effectiveAddress;
break;
case 1: // L left half immediate
operand = combine(effectiveAddress, 0);
break;
case 8: // D direct mask
case 9: // S swapped mask
if ((operand = readWordByVirtual(effectiveAddress)) < 0) { // get C(E)
return;
}
if (opCode & 1) { // S swapped mask
operand = combine(right(operand), left(operand));
}
break;
}
dst = CPU.accumulator[AC]; // get C(AC)
switch ((opCode >> 1) & 0x3) { // Do skip stuff
case 0: // Never skip
break;
case 1: // E skip if all selected bits are zero
if (!(left(operand) & left(dst)) && !(right(operand) & right(dst))) {
skip();
}
break;
case 2: // A skip always
skip();
break;
case 3: // N skip unless all selected bits are zero
if ((left(operand) & left(dst)) || (right(operand) & right(dst))) {
skip();
}
break;
}
switch ((opCode >> 4) & 0x3) { // Do AC modification
case 0: // N no modification
break;
case 1: // Z zero selected bits
dst = combine(left(dst) & ~left(operand), right(dst) & ~right(operand));
break;
case 2: // C complement selected bits
dst = combine(left(dst) ^ left(operand), right(dst) ^ right(operand));