-
Notifications
You must be signed in to change notification settings - Fork 9
/
pdp11.js
executable file
·2176 lines (2097 loc) · 114 KB
/
pdp11.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 PDP 11/70 Emulator v1.8
// written by Paul Nankervis
// Please send suggestions, fixes and feedback to [email protected]
// I'm particularly interested in hearing from anyone with real experience on a PDP 11/70 front panel
//
// This code may be used freely provided the original author name is acknowledged in any modified source code
//
// http://skn.noip.me/pdp11/pdp11.html
//
//
//
//
const IOBASE_VIRT = 0160000,
IOBASE_18BIT = 0760000,
IOBASE_UNIBUS = 017000000,
IOBASE_22BIT = 017760000,
MAX_MEMORY = IOBASE_UNIBUS - 16384, // Maximum memory address (need less memory for BSD 2.9 boot)
MAX_ADDRESS = 020000000, // Special register addresses are above 22 bit addressing
BYTE_MODE = 1, // accessMode length of 1 and flag for byte addressing
READ_MODE = 16,
WRITE_MODE = 32,
READ_WORD = READ_MODE | 2,
READ_BYTE = READ_MODE | BYTE_MODE,
WRITE_WORD = WRITE_MODE | 2,
WRITE_BYTE = WRITE_MODE | BYTE_MODE,
MODIFY_WORD = READ_MODE | WRITE_MODE | 2,
MODIFY_BYTE = READ_MODE | WRITE_MODE | BYTE_MODE;
var STATE = {
RUN: 0,
RESET: 1,
WAIT: 2,
HALT: 3
}; // CPU.runState is either STATE.RUN, STATE.WAIT or STATE.HALT
var CPU = {
controlReg: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // various control registers we don't really care about
CPU_Error: 0,
cpuType: 70,
displayAddress: 0, // Address display for console operations
displayPhysical: 0, // Physical address display for console operations
displayRegister: 0, // Console display lights register
flagC: 0x10000, // PSW C bit
flagN: 0x8000, // PSW N bit
flagV: 0x8000, // PSW V bit
flagZ: 0xffff, // ~ PSW Z bit
memory: [], // Main memory (in words)
MMR0: 0, // MMU control registers
MMR1: 0,
MMR2: 0,
MMR3: 0,
MMR3Mask: [7, 7, 7, 7], // I&D page mask by mode from KSU bits in MMR3
mmuEnable: 0, // MMU enable mask for READ_MODE or WRITE_MODE
mmuLastPage: 0, // last used MMU page for MMR0 - 2 bits of mode and 4 bits of I/D page - used as an index into PAR/PDR
mmuMode: 0, // current memory management mode (0=kernel,1=super,2=undefined,3=user)
mmuPAR: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0 kernel
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //1 super
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //2 illegal
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //3 user
], // memory management PAR registers by mode
mmuPDR: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0 kernel
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //1 super
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 2 illegal
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 //3 user
], // memory management PDR registers by mode
PIR: 0, // Programmable interrupt register
priorityReview: 1, // flag to mark if we need to check priority change
PSW: 0xf, // PSW less flags C, N, V & Z
registerAlt: [0, 0, 0, 0, 0, 0], // Alternate registers R0 - R5
registerVal: [0, 0, 0, 0, 0, 0, 0, 0], // Current registers R0 - R7
stackLimit: 0xff, // stack overflow limit
stackPointer: [0, 0, 0, 0], // Alternate R6 (kernel, super, illegal, user)
switchRegister: 0, // console switch register
trapMask: 0, // Mask of traps to be taken at the end of the current instruction
trapPSW: -1, // PSW when first trap invoked - for tacking double traps
unibusMap: [
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
], // 32 unibus map registers
runState: -1, // current machine state defined in STATE
interruptQueue: [], // List of interrupts pending
};
var log = {
depth: 64, // 256,
extras: 0,
history: []
};
function LOG_INSTRUCTION(instruction, format, name) {
if (log.depth > 0) {
if (log.history.length > log.depth + 4) {
////LOG_PRINT();
//log.history = [];
log.history.splice(0, 5);
}
log.history.push([readPSW(), CPU.registerVal[7], instruction, format, name, -1, -1]);
}
}
function LOG_SOURCE(source) {
if (log.depth > 0) {
if (log.history.length > 0) {
if (log.history[log.history.length - 1][5] < 0) {
log.history[log.history.length - 1][5] = source;
} else {
log.history[log.history.length - 1][6] = source;
}
}
}
}
function LOG_ADDRESS(address) {
if (log.depth > 0) {
if (log.history.length > 0) {
log.history[log.history.length - 1].push(address);
}
}
}
function LOG_OPERAND(operand, history, index) {
var result = "R" + (operand & 7).toString();
switch ((operand >> 3) & 7) {
case 1:
result = "(" + result + ")";
break;
case 2:
if ((operand & 7) == 7 && history[5 + index] >= 0) {
result = "#" + history[5 + index].toString(8);
} else {
result = "(" + result + ")+";
}
break;
case 3:
if ((operand & 7) == 7) {
result = "@#" + history[log.extras++].toString(8);
} else {
result = "@" + result;
}
break;
case 4:
result = "-(" + result + ")";
break;
case 5:
result = "@-(" + result + ")";
break;
case 6:
if ((operand & 7) == 7) {
result = history[log.extras++].toString(8);
} else {
result = history[log.extras++].toString(8) + "(" + result + ")";
}
break;
case 7:
if ((operand & 7) == 7) {
result = "@" + history[log.extras++].toString(8);
} else {
result = "@" + history[log.extras++].toString(8) + "(" + result + ")";
}
break;
}
return result;
}
function LOG_OCTAL(n) {
n = n.toString(8);
return "000000".substr(n.length) + n;
}
function LOG_PRINT() {
var i, result, psw, pc, instruction, name, historyRec;
for (i = 0; i < log.history.length; i++) {
log.extras = 7;
historyRec = log.history[i];
psw = historyRec[0];
pc = (historyRec[1] - 2) & 0xffff;
instruction = historyRec[2];
name = historyRec[4];
result = LOG_OCTAL(psw) + " " + LOG_OCTAL(pc) + " " + name;
switch (historyRec[3]) {
case 0: // No operands - just print name
break;
case 1: // One operand instructions
result += " " + LOG_OPERAND(instruction, historyRec, 0);
break;
case 2: // Two operand instructions
result += " " + LOG_OPERAND(instruction >> 6, historyRec, 0) + "," + LOG_OPERAND(instruction, historyRec, 1);
break;
case 3: // Instruction with a register and a normal operand
result += " " + LOG_OPERAND((instruction >> 6) & 7, [], 0) + "," + LOG_OPERAND(instruction, historyRec, 0);
break;
case 4: // Branch instructions
result += " " + branch(historyRec[1], instruction).toString(8);
break;
case 5: // SOB instruction
result += " " + LOG_OPERAND((instruction >> 6) & 7, [], 0) + "," + (historyRec[1] - (((instruction & 0x3f) << 1)) & 0xffff).toString(8);
break;
case 6: // Single register instruction (RTS)
result += " " + LOG_OPERAND(instruction & 7, [], 0);
break;
case 7: // Eight bit number instruction (EMT & TRAP)
result += " " + (instruction & 0xff).toString(8);
break;
case 8: // Six bit number instruction (MARK)
result += " " + (instruction & 0x3f).toString(8);
break;
case 9: // Three bit number instruction (SPL)
result += " " + (instruction & 0x7).toString(8);
break;
case 10: // Set and clear CC Instructions
break;
case 11: // Specials - not actually instructions
result += " " + instruction.toString(8);
break;
default:
result = "bugger";
break;
}
if (historyRec[5] >= 0) {
result += " ; " + historyRec[5].toString(8);
if (historyRec[6] >= 0) {
result += " " + historyRec[6].toString(8);
}
}
console.log(result);
}
log.history = [];
}
// Interrupts are stored in a queue in delay order with the delay expressed as
// a difference. For example if the delays were 0, 1, 0 then the first entry
// is active and both the second and third are waiting for one more instruction
// cycle to become active.
// If the current runState is WAIT then skip any delay and go to RUN.
function interrupt(cleanFlag, delay, priority, vector, callback, callarg) {
"use strict";
var i = CPU.interruptQueue.length;
if (typeof callback == "undefined") {
callback = null;
}
if (cleanFlag) {
while (i-- > 0) {
if (CPU.interruptQueue[i].vector == vector) {
if (i > 0) {
CPU.interruptQueue[i - 1].delay += CPU.interruptQueue[i].delay;
}
CPU.interruptQueue.splice(i, 1);
break;
}
}
}
if (delay >= 0) { // delay below 0 is just used to remove vector from queue
if (CPU.runState == STATE.WAIT) { // if currently in wait then resume
delay = 0;
CPU.runState = STATE.RUN;
if (!panel.halt) {
setTimeout(emulate, 0);
}
}
i = CPU.interruptQueue.length; // queue in delay 'difference' order
while (i-- > 0) {
if (CPU.interruptQueue[i].delay > delay) {
CPU.interruptQueue[i].delay -= delay;
break;
}
delay -= CPU.interruptQueue[i].delay;
}
CPU.interruptQueue.splice(i + 1, 0, {
"delay": delay,
"priority": priority & 0xe0,
"vector": vector,
"callback": callback,
"callarg": callarg
});
if (delay > 0 || (priority & 0xe0) > (CPU.PSW & 0xe0)) {
CPU.priorityReview = 1; // Schedule an interrupt priority review if required
}
}
}
// When a wait instruction is executed do a search through the interrupt list
// to see if we can run something (anything!) which has been delayed. If there is
// something then we don't actually need to enter WAIT state.
function interruptWaitRelease() {
"use strict";
var savePSW, i;
savePSW = CPU.PSW & 0xe0;
i = CPU.interruptQueue.length;
while (i-- > 0) {
CPU.interruptQueue[i].delay = 0;
if (CPU.interruptQueue[i].priority > (CPU.PSW & 0xe0)) {
CPU.priorityReview = 1;
return 1; // Found something that can run
}
}
return 0; // No candidates found for WAIT release
}
// When the PSW, PIR or interrupt queue state have changed then it is time to review
// the list of pending interrupts to see if we can invoke one. If nothing changes
// then more reviews are not required until something does change.
// Review controlled by the flag CPU.priorityReview
function interruptReview() {
"use strict";
var highPriority, high, i;
CPU.priorityReview = 0;
high = -1;
highPriority = CPU.PIR & 0xe0;
if ((i = CPU.interruptQueue.length) > 0) {
while (i-- > 0) {
if (CPU.interruptQueue[i].delay > 0) {
CPU.interruptQueue[i].delay--;
CPU.priorityReview = 1;
break; // Decrement only one delay 'difference' per cycle
}
if (CPU.interruptQueue[i].callback) {
if (!CPU.interruptQueue[i].callback(CPU.interruptQueue[i].callarg)) {
CPU.interruptQueue.splice(i, 1);
high--;
continue;
}
CPU.interruptQueue[i].callback = null;
}
if (CPU.interruptQueue[i].priority > highPriority) {
highPriority = CPU.interruptQueue[i].priority & 0xe0;
high = i;
}
}
}
if (highPriority > (CPU.PSW & 0xe0)) { // check if we found an interrupt
if (high < 0) {
trap(0xa0, 42); // PIR trap 240
} else {
trap(CPU.interruptQueue[high].vector, 44); // BR trap
CPU.interruptQueue.splice(high, 1);
}
}
}
// writePSW() is used to update the CPU Processor Status Word. The PSW should generally
// be written through this routine so that changes can be tracked properly, for example
// the correct register set, the current memory management mode, etc. An exception is
// SPL which writes the priority directly. Note that that N, Z, V, and C flags are
// actually stored separately to the PSW (CPU.PSW) for performance reasons.
//
// CPU.PSW 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
// CM | PM |RS| |PRIORITY| T| N| Z| V| C
// mode 0 kernel 1 super 2 illegal 3 user
function writePSW(newPSW) {
"use strict";
var i, temp;
CPU.flagN = newPSW << 12;
CPU.flagZ = (~newPSW) & 4;
CPU.flagV = newPSW << 14;
CPU.flagC = newPSW << 16;
if ((newPSW ^ CPU.PSW) & 0x800) {
for (i = 0; i < 6; i++) {
temp = CPU.registerVal[i];
CPU.registerVal[i] = CPU.registerAlt[i];
CPU.registerAlt[i] = temp; // swap the active register sets
}
}
CPU.mmuMode = (newPSW >> 14) & 3;
temp = (CPU.PSW >> 14) & 3;
if (CPU.mmuMode != temp) {
CPU.stackPointer[temp] = CPU.registerVal[6];
CPU.registerVal[6] = CPU.stackPointer[CPU.mmuMode]; // swap to new mode SP
}
if ((newPSW & 0xe0) < (CPU.PSW & 0xe0)) {
CPU.priorityReview = 1; // trigger check of priority levels
}
CPU.PSW = newPSW;
}
// readPSW() reassembles the N, Z, V, and C flags back into the PSW (CPU.PSW)
function readPSW() {
"use strict";
CPU.PSW = (CPU.PSW & 0xf8f0) | ((CPU.flagN >> 12) & 8) | ((CPU.flagV >> 14) & 2) | ((CPU.flagC >> 16) & 1);
if (!(CPU.flagZ & 0xffff)) {
CPU.PSW |= 4;
}
return CPU.PSW;
}
// trap() handles all the trap/abort functions. It reads the trap vector from kernel
// D space, changes mode to reflect the new PSW and PC, and then pushes the old PSW and
// PC onto the new mode stack. trap() returns a -1 which is passed up through function
// calls to indicate that a trap/abort has occurred (suspend instruction processing)
// CPU.trapPSW records the first PSW for double trap handling. The special value of -2
// allows console mode to propagate an abort without trapping to the new vector.
function trap(vector, reason) {
"use strict";
var newPC, newPSW, doubleTrap = 0;
if (CPU.trapPSW > -2) {
if (CPU.trapPSW < 0) {
CPU.trapMask = 0; // No other traps unless we cause one here
CPU.trapPSW = readPSW(); // Remember original PSW
} else {
if (!CPU.mmuMode) {
vector = 4;
doubleTrap = 1;
}
}
//LOG_INSTRUCTION(vector, 11, "-trap-");
if (!(CPU.MMR0 & 0xe000)) {
CPU.MMR1 = 0xf6f6;
CPU.MMR2 = vector;
}
CPU.mmuMode = 0; // read from kernel D space
if ((newPC = readWordByVirtual(vector | 0x10000)) >= 0) {
if ((newPSW = readWordByVirtual(((vector + 2) & 0xffff) | 0x10000)) >= 0) {
writePSW((newPSW & 0xcfff) | ((CPU.trapPSW >> 2) & 0x3000)); // set new CPU.PSW with previous mode
if (doubleTrap) {
CPU.CPU_Error |= 4;
CPU.registerVal[6] = 4;
}
if (pushWord(CPU.trapPSW, doubleTrap) >= 0 && pushWord(CPU.registerVal[7], doubleTrap) >= 0) {
CPU.registerVal[7] = newPC;
}
}
}
CPU.trapPSW = -1; // reset flag that we have a trap within a trap
}
return -1; // signal that a trap has occurred
}
// mapVirtualToPhysical() does memory management. It converts a 17 bit I/D
// virtual address to a 22 bit physical address (Note: the eight pseudo addresses
// for handling registers are NOT known at this level - those exist only for
// higher level functions). A real PDP 11/70 memory management unit can be enabled separately
// for read and write for diagnostic purposes. This is handled here by having by having
// an enable mask (CPU.mmuEnable) which is tested against the operation access mask
// (accessMask). If there is no match then the virtual address is simply mapped
// as a 16 bit physical address with the upper page going to the IO address space.
// Access bit mask values are READ_MODE and WRITE_MODE with the lower 4 bits giving
// the operand length; for autoincrement calculation and to indicate byte mode access.
//
// As an aside it turns out that it is the memory management unit that does odd address
// and non-existant memory trapping: who knew? :-) I thought these would have been
// handled at access time.
//
// When doing mapping CPU.mmuMode is used to decide what address space is to be
// used. 0 = kernel, 1 = supervisor, 2 = illegal, 3 = user. Normally CPU.mmuMode is
// set by the writePSW() function but there are execptions for instructions which
// move data between address spaces (MFPD, MFPI, MTPD, and MTPI) and trap(). These will
// modify CPU.mmuMode outside of writePSW() and then restore it again if all worked. If
// however something happens to cause a trap then no restore is done as writePSW()
// will have been invoked as part of the trap, which will resynchronise CPU.mmuMode
//
// A PDP 11/70 is different to other PDP 11's in that the highest 18 bit space (017000000
// & above) map directly to unibus space - including low memory. This doesn't appear to
// be particularly useful as it restricts maximum system memory - although it does
// allow software testing of the unibus map. This feature also appears to confuse some
// OSes which test consequetive memory locations to find maximum memory - and on a full
// memory system find themselves accessing low memory again at high addresses.
//
// 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 MMR0
//nonr leng read trap unus unus ena mnt cmp -mode- i/d --page-- enable
//
// Map a 17 bit I/D virtual address to a 22 bit physical address
function mapVirtualToPhysical(virtualAddress, accessMask) {
"use strict";
var page, pdr, pdrUpdate, physicalAddress, errorMask;
//var CPU = window.CPU;
//if (virtualAddress & ~0x1ffff) panic(89); // check VA range
//if (!accessMask) panic(93); // Must have READ_MODE or WRITE_MODE
CPU.displayAddress = virtualAddress; // Remember the virtual address
if (!(accessMask & CPU.mmuEnable)) { // This access does not require the MMU
physicalAddress = virtualAddress & 0xffff; // virtual address without MMU is 16 bit (no I&D)
if (physicalAddress >= IOBASE_VIRT) {
physicalAddress |= IOBASE_22BIT;
} else { // no max_memory check in 16 bit mode
if ((physicalAddress & 1) && !(accessMask & BYTE_MODE)) {
CPU.CPU_Error |= 0x40;
return trap(4, 22);
}
}
return physicalAddress;
} else { // This access is mapped by the MMU
page = ((virtualAddress >> 13) & CPU.MMR3Mask[CPU.mmuMode]) | (CPU.mmuMode << 4); // Determine PDR/PAR page index using mode and I&D
physicalAddress = ((CPU.mmuPAR[page] << 6) + (virtualAddress & 0x1fff)) & 0x3fffff;
if (!(CPU.MMR3 & 0x10)) { // 18 bit mapping needs extra trimming
physicalAddress &= 0x3ffff;
if (physicalAddress >= IOBASE_18BIT) {
physicalAddress |= IOBASE_22BIT;
}
}
if (physicalAddress < MAX_MEMORY) { // Ordinary memory space only needs an odd address check
if ((physicalAddress & 1) && !(accessMask & BYTE_MODE)) {
CPU.CPU_Error |= 0x40;
return trap(4, 26);
}
CPU.mmuLastPage = page;
} else { // Higher addresses may require unibus mapping and a check if non-existant
if (physicalAddress < IOBASE_22BIT) {
if (physicalAddress >= IOBASE_UNIBUS) {
physicalAddress = mapUnibus(physicalAddress & 0x3ffff); // 18bit unibus space
if (physicalAddress >= MAX_MEMORY && physicalAddress < IOBASE_22BIT) {
CPU.CPU_Error |= 0x10; // Unibus timeout
return trap(4, 24); // KB11-EM does this after ABORT handling - KB11-CM before
}
} else {
CPU.CPU_Error |= 0x20; // Non-existant main memory
return trap(4, 24); //
}
}
if ((physicalAddress != 0x3fff7a) || CPU.mmuMode) { // MMR0 is 017777572 and doesn't affect MMR0 bits
CPU.mmuLastPage = page;
}
}
errorMask = 0;
pdr = CPU.mmuPDR[page];
switch (pdr & 0x7) { // Check the Access Control Field (ACF) - really a page type
case 1: // read-only with trap
errorMask = 0x1000; // MMU trap - then fall thru
case 2: // read-only
pdrUpdate = pdr | 0x80; // Set A bit
if (accessMask & WRITE_MODE) {
errorMask = 0x2000; // read-only abort
}
break;
case 4: // read-write with read-write trap
errorMask = 0x1000; // MMU trap - then fall thru
case 5: // read-write with write trap
if (accessMask & WRITE_MODE) {
errorMask = 0x1000; // MMU trap - then fall thru
}
case 6: // read-write
pdrUpdate = pdr | ((accessMask & WRITE_MODE) ? 0xc0 : 0x80); // Set A & W bits
break;
default:
pdrUpdate = pdr;
errorMask = 0x8000; // non-resident abort
break;
}
if (pdrUpdate != pdr) {
CPU.mmuPDR[page] = pdrUpdate;
}
if ((pdr & 0x7f08) != 0x7f00) { // Skip page length check for most common case (hopefully)
if (pdr & 0x8) { // Page expands downwards
if (pdr & 0x7f00) {
if ((virtualAddress & 0x1fc0) < ((pdr >> 2) & 0x1fc0)) {
errorMask |= 0x4000; // page length error abort
}
}
} else { // Page expand upwards
if ((virtualAddress & 0x1fc0) > ((pdr >> 2) & 0x1fc0)) {
errorMask |= 0x4000; // page length error abort
}
}
}
// aborts and traps: log FIRST trap and MOST RECENT abort
if (errorMask) {
if (errorMask & 0xe000) {
if (CPU.trapPSW >= 0) {
errorMask |= 0x80; // Instruction complete
}
if (!(CPU.MMR0 & 0xe000)) {
CPU.MMR0 |= errorMask | (CPU.mmuLastPage << 1);
}
return trap(0xa8, 28); // 0250
}
if (!(CPU.MMR0 & 0xf000)) {
if (physicalAddress < 0x3ff480 || physicalAddress > 0x3fffbf) { // 017772200 - 017777677
CPU.MMR0 |= 0x1000; // MMU trap flag
if (CPU.MMR0 & 0x0200) {
CPU.trapMask |= 2; // MMU trap
}
}
}
}
return (CPU.displayPhysical = physicalAddress);
}
}
function readWordByAddr(physicalAddress) {
"use strict";
if (physicalAddress >= MAX_ADDRESS) {
return CPU.registerVal[physicalAddress - MAX_ADDRESS];
} else {
if (physicalAddress >= IOBASE_UNIBUS) {
return access_iopage(physicalAddress, -1, 0);
} else {
if (physicalAddress >= 0) {
return CPU.memory[physicalAddress >> 1];
}
}
}
return physicalAddress;
}
function writeWordByAddr(physicalAddress, data) {
"use strict";
data &= 0xffff;
if (physicalAddress >= MAX_ADDRESS) {
return (CPU.registerVal[physicalAddress - MAX_ADDRESS] = data);
} else {
if (physicalAddress >= IOBASE_UNIBUS) {
return access_iopage(physicalAddress, data, 0);
} else {
if (physicalAddress >= 0) {
return (CPU.memory[physicalAddress >> 1] = data);
}
}
}
return physicalAddress;
}
function readByteByAddr(physicalAddress) {
"use strict";
var result;
if (physicalAddress >= MAX_ADDRESS) {
return (CPU.registerVal[physicalAddress - MAX_ADDRESS] & 0xff);
} else {
if (physicalAddress >= IOBASE_UNIBUS) {
return access_iopage(physicalAddress, -1, 1);
} else {
if (physicalAddress >= 0) {
result = CPU.memory[physicalAddress >> 1];
if (physicalAddress & 1) {
result = result >> 8;
}
return (result & 0xff);
}
}
}
return physicalAddress;
}
function writeByteByAddr(physicalAddress, data) {
"use strict";
data &= 0xff;
if (physicalAddress >= MAX_ADDRESS) {
return (CPU.registerVal[physicalAddress - MAX_ADDRESS] = (CPU.registerVal[physicalAddress - MAX_ADDRESS] & 0xff00) | data);
} else {
if (physicalAddress >= IOBASE_UNIBUS) {
return access_iopage(physicalAddress, data, 1);
} else {
if (physicalAddress >= 0) {
if (physicalAddress & 1) {
return (CPU.memory[physicalAddress >> 1] = (data << 8) | (CPU.memory[physicalAddress >> 1] & 0xff));
} else {
return (CPU.memory[physicalAddress >> 1] = (CPU.memory[physicalAddress >> 1] & 0xff00) | data);
}
}
}
}
return physicalAddress;
}
function readWordByVirtual(virtualAddress) { // input address is 17 bit (I&D)
"use strict";
return readWordByAddr(mapVirtualToPhysical(virtualAddress, READ_MODE));
}
function popWord() {
"use strict";
var result;
if ((result = readWordByVirtual(CPU.registerVal[6] | 0x10000)) >= 0) {
CPU.registerVal[6] = (CPU.registerVal[6] + 2) & 0xffff;
}
return result;
}
// Stack limit checks only occur for Kernel mode and are either a yellow warning trap
// after instruction completion, or a red abort which stops the current instruction.
function stackCheck(virtualAddress) {
"use strict";
if (!CPU.mmuMode) { // Kernel mode 0 checking only
if (virtualAddress <= CPU.stackLimit || virtualAddress >= 0xfffe) {
if (virtualAddress + 32 <= CPU.stackLimit || virtualAddress >= 0xfffe) {
CPU.CPU_Error |= 4; // Red stack
CPU.registerVal[6] = 4;
return trap(4, 38);
}
CPU.CPU_Error |= 8; // Yellow
CPU.trapMask |= 4;
}
}
return virtualAddress;
}
function pushWord(data, skipLimitCheck) {
"use strict";
var physicalAddress, virtualAddress;
CPU.registerVal[6] = virtualAddress = (CPU.registerVal[6] - 2) & 0xffff; // BSD meeds SP updated before any fault :-(
if (!(CPU.MMR0 & 0xe000)) {
CPU.MMR1 = (CPU.MMR1 << 8) | 0xf6;
}
if (!skipLimitCheck) {
if ((virtualAddress = stackCheck(virtualAddress)) < 0) {
return virtualAddress;
}
}
if ((physicalAddress = mapVirtualToPhysical(virtualAddress | 0x10000, WRITE_MODE)) >= 0) {
return writeWordByAddr(physicalAddress, data);
}
return physicalAddress;
}
// getVirtualByMode() maps a six bit operand to a 17 bit I/D virtual address space.
// Instruction operands are six bits in length - three bits for the mode and three
// for the register. The 17th I/D bit in the resulting virtual address represents
// whether the reference is to Instruction space or Data space - which depends on
// combination of the mode and whether the register is the Program Counter (register 7).
//
// The eight addressing modes are:-
// 0 R no valid virtual address
// 1 (R) operand from I/D depending if R = 7
// 2 (R)+ operand from I/D depending if R = 7
// 3 @(R)+ address from I/D depending if R = 7 and operand from D space
// 4 -(R) operand from I/D depending if R = 7
// 5 @-(R) address from I/D depending if R = 7 and operand from D space
// 6 x(R) x from I space but operand from D space
// 7 @x(R) x from I space but address and operand from D space
//
// Kernel mode stack limit checks are implemented for addressing modes 1, 2, 4 & 6 (!)
//
// The accessMode field specifies two bit flags for read or write, or both for a modify.
// Mask values for these are constants READ_MODE and WRITE_MODE
// In addition the lower four bits specify the operand length. This is 1 for a byte
// or 2 for a word - however the FPP processor may also use 4 or 8. Thus if autoincrement
// is used on a double FPP word the register will autoincrement by 8. This should always
// be set to get the autoincrement correct.
//
// Just to keep us on our toes the mode (PC)+ (immediate mode, octal 27) ALWAYS increments
// by 2 no matter what type of operand is used.
//
// Also need to keep CPU.MMR1 updated as this stores which registers have been
// incremented and decremented so that the OS can reset and restart an instruction
// if a page fault occurs.
//
// Convert a six bit instruction operand to a 17 bit I/D virtual address
function getVirtualByMode(addressMode, accessMode) {
"use strict";
var virtualAddress, autoIncrement, reg = addressMode & 7;
if (!(accessMode & 0xf)) panic(75);
switch ((addressMode >> 3) & 7) {
case 0: // Mode 0: Registers don't have a virtual address so trap!
return trap(4, 34); // trap for invalid virtual address
case 1: // Mode 1: (R)
virtualAddress = CPU.registerVal[reg];
if (reg < 6) {
virtualAddress |= 0x10000;
} else {
if (reg == 6) {
if (accessMode & WRITE_MODE) {
if ((virtualAddress = stackCheck(virtualAddress)) < 0) {
return virtualAddress;
}
}
virtualAddress |= 0x10000;
}
}
return virtualAddress;
case 2: // Mode 2: (R)+
virtualAddress = CPU.registerVal[reg];
if (reg == 7) {
autoIncrement = 2; // immediate mode (PC)+ always autoIncrements by 2
} else {
autoIncrement = accessMode & 0xf;
if (reg == 6) {
if (accessMode & BYTE_MODE) {
autoIncrement = 2; // R6 doesn't autoIncrement by 1
}
if (accessMode & WRITE_MODE) {
if ((virtualAddress = stackCheck(virtualAddress)) < 0) {
return virtualAddress;
}
}
}
virtualAddress |= 0x10000;
}
break;
case 3: // Mode 3: @(R)+
autoIncrement = 2;
virtualAddress = CPU.registerVal[reg];
if ((virtualAddress = readWordByVirtual(reg == 7 ? virtualAddress : virtualAddress | 0x10000)) < 0) {
return virtualAddress;
}
if (reg == 7) {
//LOG_ADDRESS(virtualAddress); // @#n not operational
}
virtualAddress |= 0x10000;
break;
case 4: // Mode 4: -(R)
autoIncrement = -(accessMode & 0xf);
if (reg < 6) {
virtualAddress = ((CPU.registerVal[reg] + autoIncrement) & 0xffff) | 0x10000;
} else {
if (accessMode & BYTE_MODE) {
autoIncrement = -2; // R6 & R7 don't decrement by 1
}
virtualAddress = (CPU.registerVal[reg] + autoIncrement) & 0xffff;
if (reg == 6) {
if (accessMode & WRITE_MODE) {
if ((virtualAddress = stackCheck(virtualAddress)) < 0) {
return virtualAddress;
}
}
virtualAddress |= 0x10000;
}
}
break;
case 5: // Mode 5: @-(R)
autoIncrement = -2;
virtualAddress = (CPU.registerVal[reg] - 2) & 0xffff;
if ((virtualAddress = readWordByVirtual(reg == 7 ? virtualAddress : virtualAddress | 0x10000)) < 0) {
return virtualAddress;
}
virtualAddress |= 0x10000;
break;
case 6: // Mode 6: d(R)
if ((virtualAddress = readWordByVirtual(CPU.registerVal[7])) < 0) {
return virtualAddress;
}
CPU.registerVal[7] = (CPU.registerVal[7] + 2) & 0xffff;
if (reg < 7) {
//LOG_ADDRESS(virtualAddress);
virtualAddress = (virtualAddress + CPU.registerVal[reg]) & 0xffff;
} else {
virtualAddress = (virtualAddress + CPU.registerVal[reg]) & 0xffff;
//LOG_ADDRESS(virtualAddress);
}
if (reg == 6 && (accessMode & WRITE_MODE)) {
if ((virtualAddress = stackCheck(virtualAddress)) < 0) {
return virtualAddress;
}
}
return virtualAddress | 0x10000;
case 7: // Mode 7: @d(R)
if ((virtualAddress = readWordByVirtual(CPU.registerVal[7])) < 0) {
return virtualAddress;
}
CPU.registerVal[7] = (CPU.registerVal[7] + 2) & 0xffff;
if (reg < 7) {
//LOG_ADDRESS(virtualAddress);
virtualAddress = (virtualAddress + CPU.registerVal[reg]) & 0xffff;
} else {
virtualAddress = (virtualAddress + CPU.registerVal[reg]) & 0xffff;
//LOG_ADDRESS(virtualAddress);
}
if ((virtualAddress = readWordByVirtual(virtualAddress | 0x10000)) < 0) {
return virtualAddress;
}
return virtualAddress | 0x10000; // @x
}
CPU.registerVal[reg] = (CPU.registerVal[reg] + autoIncrement) & 0xffff;
if (!(CPU.MMR0 & 0xe000)) {
CPU.MMR1 = (CPU.MMR1 << 8) | ((autoIncrement << 3) & 0xf8) | reg;
}
return virtualAddress;
}
function getAddrByMode(addressMode, accessMode) {
"use strict";
var result;
if (!(addressMode & 0x38)) {
return MAX_ADDRESS + (addressMode & 7); // Registers have special addresses above maximum address
} else {
if ((result = getVirtualByMode(addressMode, accessMode)) >= 0) {
result = mapVirtualToPhysical(result, accessMode);
}
return result;
}
}
function readWordByMode(addressMode) {
"use strict";
var result;
if (!(addressMode & 0x38)) {
result = CPU.registerVal[addressMode & 7];
//LOG_SOURCE(result);
} else {
if ((result = getAddrByMode(addressMode, READ_WORD)) >= 0) {
if ((result = readWordByAddr(result)) >= 0) {
//LOG_SOURCE(result);
}
}
}
return result;
}
function readByteByMode(addressMode) {
"use strict";
var result;
if (!(addressMode & 0x38)) {
result = CPU.registerVal[addressMode & 7] & 0xff;
//LOG_SOURCE(result);
} else {
if ((result = getAddrByMode(addressMode, READ_BYTE)) >= 0) {
if ((result = readByteByAddr(result)) >= 0) {
//LOG_SOURCE(result);
}
}
}
return result;
}
// branch() calculates the branch to PC from a branch instruction offset
function branch(PC, instruction) {
"use strict";
return (PC + ((instruction & 0x80 ? instruction | 0xff00 : instruction & 0xff) << 1)) & 0xffff;
}
// Most instruction read operations use a 6 bit instruction operand via
// readWordByMode(instruction operand). If the result is negative then
// something failed and generally a trap or abort has already been invoked.
// The code for this would generally look like:
// if ((src = readWordByMode(instruction >> 6)) >= 0) {
// success - do operation with src
//
// For each Word function there are usually corresponding Byte functions, eg readByteByMode()
//
// Write only operations are just a special case of read/write and there are
// no special functions to support them, mainly because they would only be used by MOV
// and CLR instructions. Maybe they should have been added for consistency?
//
// Read/Write operations are harder than read because we want to do memory mapping
// only once. So the strategy is to get a physical address, use it to read
// the operand, and if nothing went wrong use it to do a write.
// The instruction code for this would generally look like:
// if ((dst = readWordByAddr(dstAddr = getAddrByMode(instruction, MODIFY_WORD))) >= 0) {
// result = something and dst
// if (writeWordByAddr(dstAddr, result) >= 0) {
// finish instruction, set CC codes etc
//
// Note that the getAddrByMode() function requires that we specify a mode bit mask
// to tell it if we are doing a read or write (or modify for both), and the length
// of the operand for autoincrement and to determine if addressing is byte mode.
// This information is also passed to memory management for it's use.
//
// For performance reasons many instructions have a special case optimization for register
// read/write (operand mode 0). Although the code would work without this (because
// getAddrByMode() will return a special pseudo physical address for registers),
// it is much quicker to bypass address generation and directly access the register.
// The code for this would generally look like:
// if (!(operand & 0x38)) {
// CPU.registerVal[operand & 7] = something
// } else {
// normal non-register code
//
// Some instructions (eg JMP, JSR, MTPx..) require the address of an operand.
// The code for this would generally look like:
// if ((virtualAddress = getVirtualByMode(instruction, 2)) >= 0) {
//
// Note the access mode does not need to specify READ_MODE or WRITE_MODE however
// the operand length must still be specified for correct address autoincrement.
//
// Most of these functions work in layers. For example:
// readWordByMode() fetches an operand after converting an instruction operand to
// a physical address, which is generated using getAddrByMode()
// The physical addresses is then accessed using readWordByAddr()
// getAddrByMode() first converts an instruction operand to a 17 bit I/D virtual address
// using getVirtualByMode() - which it then converts to a physical
// address using mapVirtualToPhysical() to represent a standard 22 bit PDP 11/70 memory
// and unibus address, or to one of eight special pseudo addresses for registers.
// readWordByAddr() / writeWordByAddr() either access registers directly, memory as