forked from JamesHagerman/nankervis-pdp11-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iopage.js
executable file
·1983 lines (1924 loc) · 84 KB
/
iopage.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 v4.0
// written by Paul Nankervis
// Please send suggestions, fixes and feedback to paulnank@hotmail.com
//
// This code may be used freely provided the original author name is acknowledged in any modified source code
//
//
// This code emulates the function of the PDP 11/70 i/o page
//
// iopage handles access to the unibus i/o page. devices register their unibus
// addresses and functions to handle access, interrupt polling and reset here.
// unibus access is handled in 4 word (8 byte) chunks so some devices may require
// multiple entries in the deviceAccess table. two different devices with addresses
// in the same chunk would require a combined access handler - but none seen yet.
//
var iopage = (function() {
"use strict";
var deviceList = [], // list of devices on the i/o page (used for reset)
devicePoll = [], // list of device poll functions which do interrupt checks
deviceAccess = new Array(0o17777 >>> 3); // device access functions by unibus address
return {
// function to handle all unibus iopage access requests
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
let access = deviceAccess[(physicalAddress & 0o17777) >>> 3];
if (access !== undefined) {
result = access(physicalAddress, data, byteFlag);
} else {
result = trap(0o4, 0x10); // Trap 4 - 0x10 Unibus time-out
}
if (result >= 0) {
if (byteFlag) {
if ((physicalAddress & 1)) {
result = result >>> 8;
} else {
result &= 0xff;
}
}
} else {
if (physicalAddress !== 0o17777776) { // Special case for PSW
console.log("IOPAGE nxm failure " + physicalAddress.toString(8) + " " + data.toString(8) + " @" + CPU.registerVal[7].toString(8));
}
}
return result;
},
// function to poll all devices and trigger an interrupt trap if required
poll: function() {
"use strict";
var highPriority = CPU.PIR & 0xe0,
highIndex = -1;
CPU.interruptRequested = 0;
for (let index = devicePoll.length - 1; index >= 0; index--) {
let devicePriority = devicePoll[index](0); // get level device wants to interrupt at
if (devicePriority > highPriority) {
highIndex = index;
highPriority = devicePriority;
}
}
if (highPriority > (CPU.PSW & 0xe0)) { // Check if we found an interrupt to service
if (highIndex < 0) {
trap(0o240, 0x00); // Trap 240 - 0x00 PIR trap
} else {
trap(devicePoll[highIndex](1), 0x00); // Device trap - 0x00 BR trap and mark interrupt request complete
}
return 1;
}
return 0;
},
// function to register a device on the i/o page
register: function(address, count, device) {
"use strict";
deviceList.push(device);
if (device.poll !== undefined) {
devicePoll.push(device.poll);
}
if ((address & 0o17760000) !== 0o17760000) {
console.log("iopage.register invalid address:" + address.toString(8));
}
for (let index = (address & 0o17777) >>> 3; count > 0; count -= 4, index++) {
if (deviceAccess[index] !== undefined) {
console.log("iopage.register address inuse:" + address.toString(8));
}
deviceAccess[index] = device.access;
}
},
// function to reset all devices which contain a reset function
reset: function() {
"use strict";
for (let index = 0; index < deviceList.length; index++) {
let device = deviceList[index];
if (device !== undefined && device.reset !== undefined) {
device.reset();
}
}
}
};
})();
// helper function to merge a new word or byte with an existing word
function insertData(original, physicalAddress, data, byteFlag) {
"use strict";
if (byteFlag) {
if (data < 0) {
return original;
} else {
if (physicalAddress & 1) {
return ((data << 8) & 0xff00) | (original & 0xff);
} else {
return (original & 0xff00) | (data & 0xff);
}
}
} else {
if (physicalAddress & 1) {
return trap(0o4, 0x40); // Trap 4 - 0x40 Odd address error
} else {
if (data < 0) {
return original;
} else {
return data;
}
}
}
}
// request main loop to do an interrupt status check
function requestInterrupt() {
"use strict";
CPU.interruptRequested = 1;
if (CPU.runState === STATE_WAIT) { // if currently in wait then resume
CPU.runState = STATE_RUN;
}
}
// register micro break, pir, stack limit and psw on the i/o page
iopage.register(0o17777770, 4, (function() {
var microBreak = 0; // 17777770 Microprogram break
function init() {
"use strict";
CPU.PIR = 0;
CPU.stackLimit = 0xff;
CPU.CPU_Error = 0;
CPU.MMR0 = CPU.MMR3 = CPU.mmuEnable = 0;
setMMUmode(0);
CPU.mmuLastPage = 0;
}
init();
return {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o17777776) {
case 0o17777770: // Microprogram break
result = insertData(microBreak, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
microBreak = result & 0xff;
}
break;
case 0o17777772: // 17777772 pir
result = insertData(CPU.PIR, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
result &= 0xfe00;
if (result) { // Need to calculate priority level from priority mask
let index = result >>> 9;
do {
result += 0x22;
} while (index >>= 1);
}
CPU.PIR = result;
if ((result & 0xe0) > (CPU.PSW & 0xe0)) {
requestInterrupt(); // request an interrupt if priority higher than current
}
}
break;
case 0o17777774: // 17777774 stack limit
result = insertData(CPU.stackLimit, physicalAddress, data, byteFlag);
if (result >= 0) {
if (data >= 0) {
CPU.stackLimit = result | 0xff; // stack limit has lower byte bits set
}
result &= 0xff00;
}
break;
case 0o17777776: // 17777776 PSW
result = insertData(readPSW(), physicalAddress, data, byteFlag);
if (data >= 0 && result >= 0) {
writePSW(result);
return -1; // Kludge - signals no further processing to prevent changes to PSW
}
break;
}
return result;
},
reset: init
};
})());
// register miscellaneous 11/70 registers on i/o page
if (CPU_TYPE === 70) { // 11/45 doesn't have these
iopage.register(0o17777760, 4, {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o17777776) {
case 0o17777760: // Lower size
result = insertData((MAX_MEMORY >>> 6) - 1, physicalAddress, data, byteFlag);
break;
case 0o17777762: // Upper size
result = insertData(0, physicalAddress, data, byteFlag);
break;
case 0o17777764: // System I/D
result = insertData(1, physicalAddress, data, byteFlag);
break;
case 0o17777766: // CPU error
result = insertData(CPU.CPU_Error, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
CPU.CPU_Error = 0; // always writes as zero
}
break;
}
return result;
}
});
}
// register 11/70 maintenance registers on i/o page
if (CPU_TYPE === 70) { // 11/45 doesn't have these
iopage.register(0o17777750, 4, {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o17777776) {
case 0o17777750: // Maintenance
case 0o17777752: // Hit/miss
case 0o17777754: //
case 0o17777756: //
result = insertData(0, physicalAddress, data, byteFlag);
break;
}
return result;
}
});
}
// register 11/70 memory control registers on i/o page
if (CPU_TYPE === 70) { // 11/45 doesn't have these
iopage.register(0o17777740, 4, {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o17777776) {
case 0o17777740: // Low error address
result = insertData(0o177740, physicalAddress, data, byteFlag);
break;
case 0o17777742: // High error address
result = insertData(0o3, physicalAddress, data, byteFlag);
break;
case 0o17777744: // Memory system error
result = insertData(0, physicalAddress, data, byteFlag);
break;
case 0o17777746: // Cache control
result = insertData(0o17, physicalAddress, data, byteFlag);
break;
}
return result;
}
});
}
// put register set 0 onto the i/o page
iopage.register(0o17777700, 4, {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result, index;
index = physicalAddress & 7;
switch (index) { // no byte stuff here!
default: // register set 0 (R0 - R5)
if (CPU.PSW & 0x800) { // where is register set 0 now?
if (data >= 0) CPU.registerAlt[index] = data;
result = CPU.registerAlt[index];
} else {
if (data >= 0) CPU.registerVal[index] = data;
result = CPU.registerVal[index];
}
break;
case 0o6: // 17777706 kernel SP
if (CPU.mmuMode === 0) { // if in kernel Mode...
if (data >= 0) CPU.registerVal[6] = data;
result = CPU.registerVal[6];
} else {
if (data >= 0) CPU.stackPointer[0] = data;
result = CPU.stackPointer[0];
}
break;
case 0o7: // 17777707 kernel PC
if (data >= 0) CPU.registerVal[7] = data;
result = CPU.registerVal[7];
break;
}
return result; // this block is special as it doesn't do byte handling
}
});
// put register set 1 onto the i/o page
iopage.register(0o17777710, 4, {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result, index;
index = physicalAddress & 7;
switch (index) { // no byte stuff here!
default: // register set 1 (R0 - R5)
if (CPU.PSW & 0x800) { // where is register set 1 now?
if (data >= 0) CPU.registerVal[index] = data;
result = CPU.registerVal[index];
} else {
if (data >= 0) CPU.registerAlt[index] = data;
result = CPU.registerAlt[index];
}
break;
case 0o6: // 17777716 super SP
if (CPU.mmuMode === 1) { // if in super mode...
if (data >= 0) CPU.registerVal[6] = data;
result = CPU.registerVal[6];
} else {
if (data >= 0) CPU.stackPointer[1] = data;
result = CPU.stackPointer[1];
}
break;
case 0o7: // 17777717 user SP
if (CPU.mmuMode === 3) { // if in user mode...
if (data >= 0) CPU.registerVal[6] = data;
result = CPU.registerVal[6];
} else {
if (data >= 0) CPU.stackPointer[3] = data;
result = CPU.stackPointer[3];
}
break;
}
return result; // this block is special as it doesn't do byte handling
}
});
// register console switches and lights, mmr0, mmr1 and mmr2 on i/o page
iopage.register(0o17777570, 4, {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o17777776) {
case 0o17777570: // 17777570 console panel display/switch;
result = insertData(CPU.switchRegister & 0xffff, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
CPU.displayRegister = result;
}
break;
case 0o17777572: // 17777572 MMR0
if (!(CPU.MMR0 & 0xe000)) {
CPU.MMR0 = (CPU.MMR0 & 0xf381) | (CPU.mmuLastPage << 1);
}
result = insertData(CPU.MMR0, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
CPU.MMR0 = result &= 0xf381;
CPU.mmuLastPage = (result >>> 1) & 0x3f;
if (result & 0x101) {
if (result & 0x1) {
CPU.mmuEnable = MMU_READ | MMU_WRITE;
} else {
CPU.mmuEnable = MMU_WRITE;
}
} else {
CPU.mmuEnable = 0;
CPU.mmuLastPage = 0; // Data light off
}
}
break;
case 0o17777574: // 17777574 MMR1
result = CPU.MMR1;
if (result & 0xff00) {
result = ((result << 8) | (result >>> 8)) & 0xffff;
}
break;
case 0o17777576: // 17777576 MMR2
result = insertData(CPU.MMR2, physicalAddress, data, byteFlag);
if (result >= 0) {
CPU.MMR2 = result;
}
break;
}
return result;
}
});
// register mmr3 on i/o page (all by its lonesome self!)
iopage.register(0o17772510, 1, {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o17777776) {
case 0o17772516: // 17772516 MMR3 - UB 22 x K S U
result = insertData(CPU.MMR3, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
if (CPU_TYPE !== 70) {
result &= ~0x30; // don't allow 11/45 to do 22 bit or use unibus map
}
CPU.MMR3 = result;
setMMUmode(CPU.mmuMode);
}
break;
default:
return trap(0o4, 0x10); // Trap 4 - 0x10 Unibus time-out
}
return result;
}
});
// access function for memory management PDR registers
var mmuRegisterPDR = {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result, index; // PDR is an array of 16 descriptors for kernel, followed by 16 for super, 16 unused and 16 for user mode
index = (((physicalAddress & 0o0600) >>> 3) ^ ((physicalAddress & 0o0100) >>> 2)) | ((physicalAddress >>> 1) & 0o17);
result = insertData(CPU.mmuPDR[index], physicalAddress, data, byteFlag);
if (result >= 0) {
CPU.mmuPDR[index] = result & 0xff0f;
}
return result;
}
};
// access function for memory management PAR registers
var mmuRegisterPAR = {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result, index; // Index becomes 0-15 for kernel mode, 16-31 for super, and 48-63 for user mode
index = (((physicalAddress & 0o0600) >>> 3) ^ ((physicalAddress & 0o0100) >>> 2)) | ((physicalAddress >>> 1) & 0o17);
result = insertData(CPU.mmuPAR[index], physicalAddress, data, byteFlag);
if (result >= 0) {
CPU.mmuPAR[index] = result;
CPU.mmuPDR[index] &= 0xff0f; // access impacts PDR as well
}
return result;
}
};
// register kernel, super and user PDR and PAR memory management registers on i/o page
iopage.register(0o17772300, 16, mmuRegisterPDR); // Kernel 17772300 - 17772337 MMU kernel mode (0) PDR Map
iopage.register(0o17772340, 16, mmuRegisterPAR); // Kernel 17772340 - 17772377 MMU kernel mode (0) PAR Map
iopage.register(0o17772200, 16, mmuRegisterPDR); // Super 17772200 - 17772237 MMU super mode (1) PDR Map
iopage.register(0o17772240, 16, mmuRegisterPAR); // Super 17772240 - 17772277 MMU super mode (1) PAR Map
iopage.register(0o17777600, 16, mmuRegisterPDR); // User 17777600 - 17777637 MMU user mode (3) PDR Map
iopage.register(0o17777640, 16, mmuRegisterPAR); // User 17777640 - 17777677 MMU user mode (3) PAR Map
// register the 11/70 unibus mapping registers on i/o page
if (CPU_TYPE === 70) { // 11/45 doesn't have a unibus map
iopage.register(0o17770200, 64, { // 17770200 - 17770277 Unibus Map (32 double words!)
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result, index = (physicalAddress >>> 2) & 0x1f; // 32 double words
if (physicalAddress & 0o2) { // high word
result = insertData(CPU.unibusMap[index] >>> 16, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
CPU.unibusMap[index] = ((result & 0x803f) << 16) | (CPU.unibusMap[index] & 0xffff);
}
} else { // low word
result = insertData(CPU.unibusMap[index] & 0xffff, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
CPU.unibusMap[index] = (CPU.unibusMap[index] & 0x803f0000) | (result & 0xfffe);
}
}
return result;
}
});
}
// function to map an 18 bit unibus address to a 22 bit memory address via the unibus map (if active)
function mapUnibus(unibusAddress) {
"use strict";
var index = (unibusAddress >>> 13) & 0x1f; // 5 top bits select mapping register
if (index < 31) {
if (CPU.MMR3 & 0x20) {
unibusAddress = (CPU.unibusMap[index] + (unibusAddress & 0x1fff)) & 0x3fffff; // 13 low bits become offset
}
} else {
unibusAddress |= IOBASE_22BIT; // top page always maps to unibus i/o page - apparently.
}
return unibusAddress;
}
// register KW11L line time clock on the i/o page
iopage.register(0o17777546, 1, (function() {
"use strict";
var csr, // 17777546 kw11 csr
iMask; // interrupt mask
function init() {
"use strict";
csr = 0x80; // monitor set, ie clear
iMask = 0;
}
init();
setInterval(function() {
"use strict";
csr |= 0x80; // set monitor
if (csr & 0x40) { // interrupt if ie
iMask = 1;
requestInterrupt();
}
}, 20); // 50 Hz repeating (Australian power!)
return {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o6) {
case 0o6: // 17777546 kw11 csr
result = insertData(csr, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
if ((result ^ csr) & 0x40) { // ie change?
if (result & 0x40) { // setting...
iMask = 1;
requestInterrupt();
} else { // clearing...
iMask = 0;
}
}
csr = result & 0x40; // NOTE: monitor cleared on write (DATO)
}
return result;
default:
return trap(0o4, 0x10); // Trap 4 - 0x10 Unibus time-out
}
},
poll: function(takeInterrupt) {
"use strict";
if (iMask) {
if (takeInterrupt) {
iMask = 0;
return 0o100; // KW11 vector
} else {
if (csr & 0x40) { // interrupts still enabled?
return 6 << 5; // KW11 priority
}
iMask = 0;
}
}
return 0;
},
reset: init
};
})());
// function to register a dl11 terminal (such as the console) on the i/o page
function dl11(vt52Unit, deviceVector) {
"use strict";
var rcsr, // 17777560 tty rcsr receive control register 7 DONE 6 IE
rbuf, // 17777562 tty rbuf receive buffer
xcsr, // 17777564 tty xcsr transmit control register 7 DONE 6 IE
xbuf, // 17777566 tty xbuf transmit buffer
xdelay, // transmit delay (to pass diagnostics)
iMask; // interrupt mask
var unit = vt52Unit,
vector = deviceVector,
divElement;
function init() {
"use strict";
rcsr = 0; // no character received
rbuf = 0;
xcsr = 0x80; // ready to transmit
xbuf = 0;
xdelay = 0;
iMask = 0;
}
function dlInput(unit, ch) { // called by vt52 code when a character has been typed
"use strict";
if (rcsr & 0x80) { // done still set
return 0; // so reject new character
}
rbuf = ch;
rcsr |= 0x80; // set receive done
if (rcsr & 0x40) {
iMask |= 1; // request receive interrupt
requestInterrupt();
}
return 1; // consume character
}
init();
if (unit === 0) { // unit 0 has a pre-prepared html element waiting
divElement = document.getElementById(unit);
vt52Initialize(unit, divElement, dlInput);
}
return {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o6) {
case 0o0: // 17777560 tty rcsr
result = insertData(rcsr, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
if ((result ^ rcsr) & 0x40) { // ie change?
if (result & 0x40) { // setting...
rcsr |= 0x40;
if (rcsr & 0x80) { // if done also set
iMask |= 1; // request receive interrupt
requestInterrupt();
}
} else { // clearing...
rcsr &= ~0x40;
iMask &= ~1; // clear any receive interrupts
}
}
}
break;
case 0o2: // 17777562 tty rbuf
result = insertData(rbuf, physicalAddress, data, byteFlag);
if (result >= 0) {
rcsr &= ~0x80; // receive clear on read/write access
}
break;
case 0o4: // 17777564 tty xcsr
result = insertData(xcsr, physicalAddress, data, byteFlag);
if (result >= 0) {
if (data >= 0) { // xcsr write
if ((result ^ xcsr) & 0x40) { // ie change?
if (result & 0x40) { // setting..
if (xcsr & 0x80) { // and done bit...
xcsr = 0xc0; // set ie
iMask |= 2; // request transmit interrupt
requestInterrupt();
} else {
xcsr = 0x40; // setting ie but done clear - must be a diagnostic!
setTimeout(function() {
xcsr |= 0x80;
if ((xcsr & 0x40)) {
iMask |= 2; // request transmit interrupt
requestInterrupt();
}
}, 1);
}
} else { // clearing...
xcsr = 0x80; // clear ie and ensure done set
iMask &= ~2; // clear transmit interrupt
}
}
} else { // xcsr read - if delay wait a couple of cycles to say done
if (xdelay > 0) {
if (--xdelay <= 0) {
xcsr |= 0x80;
}
}
}
}
break;
case 0o6: // 17777566 tty xbuf
result = insertData(xbuf, physicalAddress, data, byteFlag);
if (data >= 0 && result >= 0) {
if (divElement === undefined) { // make a html element on first character output
divElement = document.createElement('div');
divElement.innerHTML = '<p>tty' + unit + '<br /><textarea id=' + unit + ' cols=132 rows=24 style="font-family:' + "'Courier New'" + ',Courier,' + "'Lucida Console'" + ',Monaco,monospace;" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"></textarea><br /></p>';
document.getElementById('dl11').appendChild(divElement);
vt52Initialize(unit, document.getElementById(unit), dlInput);
}
xbuf = result & 0x7f; // 7 bit ascii
if (xbuf >= 8 && xbuf < 127) {
vt52Put(unit, xbuf);
}
if ((xcsr & 0x40)) { // if ie
iMask |= 2; // request transmit interrupt
requestInterrupt();
} else {
xcsr &= ~0x80; // clear ready
xdelay = 3; // wait for a couple of status reads before reset
}
}
break;
}
return result;
},
poll: function(takeInterrupt) {
"use strict";
if (iMask) {
if (takeInterrupt) {
if (iMask & 1) {
iMask &= ~1;
return vector; // receive vector
} else {
iMask = 0;
return vector + 4; // transmit vector
}
} else {
return 4 << 5; // DL11 priority
}
}
return 0;
},
reset: init
};
}
// register the console terminal
iopage.register(0o17777560, 4, dl11(0, 0o60));
// and another two for fun...
iopage.register(0o17776500, 4, dl11(1, 0o310));
iopage.register(0o17776510, 4, dl11(2, 0o320));
//iopage.register(0o17776520, 4, dl11(3, 0o330));
//iopage.register(0o17776530, 4, dl11(3, 0o340));
// register an LP11 line printer on the i/o page
iopage.register(0o17777510, 2, (function() {
var lpcs, // 17777514 line printer control status
lpdb, // 17777516 line printer data buffer
iMask; // interrupt mask
var lp11Element;
function init() {
"use strict";
lpcs = 0x80; // done set, ie clear
lpdb = 0;
iMask = 0;
}
init();
return {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o6) {
case 0o4: // 17777514 lpcs line printer control register
result = insertData(lpcs, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
if ((result ^ lpcs) & 0x40) { // ie change?
if (result & 0x40) { // setting...
iMask = 1;
requestInterrupt();
} else { // clearing...
iMask = 0;
}
}
lpcs = (lpcs & 0x80) | (result & 0x40); // (we don't ever actually clear done)
}
break;
case 0o6: // 17777516 lpdb line printer buffer
result = insertData(lpdb, physicalAddress, data, byteFlag);
if (data >= 0 && result >= 0) {
if (lp11Element === undefined) {
document.getElementById("lp11").innerHTML = '<p>printer<br /><textarea id=lp11_id cols=132 rows=24 spellcheck=false style="font-family:Liberation Mono,Monaco,Courier New,Lucida Console,Consolas,DejaVu Sans Mono,Bitstream Vera Sans Mono,monospace"></textarea><br /><button onclick="document.getElementById(' + "'lp11_id'" + ').value=' + "''" + ';">Clear</button></p>';
lp11Element = document.getElementById("lp11_id");
}
lpdb = result & 0x7f; // 7 bit ascii
if (lpdb >= 0o12 && lpdb !== 0o15) {
lp11Element.value += String.fromCharCode(lpdb);
if (lpdb == 0o12) {
lp11Element.scrollTop = lp11Element.scrollHeight;
}
}
if (lpcs & 0x40) {
iMask = 1;
requestInterrupt();
}
}
break;
default:
return trap(0o4, 0x10); // Trap 4 - 0x10 Unibus time-out
}
return result;
},
poll: function(takeInterrupt) {
"use strict";
if (iMask) {
if (takeInterrupt) {
iMask = 0;
return 0o200; // LP11 vector
} else {
if (lpcs & 0x40) { // interrupts still enabled?
return 4 << 5; // LP11 priority
}
iMask = 0;
}
}
return 0;
},
reset: init
};
})());
// =========== Disk I/O support routines ===========
const
IO_BLOCKSIZE = 1024 * 1024; // 1 Mb request size. Larger reduces number of requests but increases count
// extractXHR() copies the XMLHttpRequest response to disk cache returning
// 0 on success or -1 on error
function extractXHR(xhr, cache, block) {
"use strict";
var dataView, dataLength, dataIndex, blockIndex;
switch (xhr.status) {
case 416: // Out of range - make empty cache block
dataLength = 0;
break;
case 200: // Whole file response - fill cache from beginning
//block = 0; // Note case fall thru!
case 0: // Local response - have to assume we got appropriate response
case 206: // Partial response - use what is there
dataView = new Uint8Array(xhr.response);
dataLength = dataView.length;
break;
default: // Error - signal and exit
return -1; // Return error
}
dataIndex = 0; // Start copy to cache at the beginning
do {
if (cache[block] === undefined) {
cache[block] = new Uint8Array(IO_BLOCKSIZE); // Creates zero filled cache block
for (blockIndex = 0; blockIndex < IO_BLOCKSIZE && dataIndex < dataLength;) {
cache[block][blockIndex++] = dataView[dataIndex++] & 0xff;
}
} else {
dataIndex += IO_BLOCKSIZE; // Skip any existing cache blocks
}
block++;
} while (dataIndex < dataLength);
return 0; // Return success
}
// getData() is called at the completion of an XMLHttpRequest request to GET disk data.
// It extracts the received data and stores it in the appropriate disk cache, then resumes
// the pending i/o (which may trigger additional transfers).
function getData(controlBlock, operation, position, address, count) {
"use strict";
if (extractXHR(controlBlock.xhr, controlBlock.cache, ~~(position / IO_BLOCKSIZE)) < 0) {
controlBlock.callback(controlBlock, 1, position, address, count); // NXD - error callback
} else {
diskIO(controlBlock, operation, position, address, count); // Resume I/O
}
}
// devices call diskIO() to do read and write functions. Reads must initially come from
// server files but re-read and write functions use local cache.
function diskIO(controlBlock, operation, position, address, count) {
"use strict";
var block, offset, data;
block = ~~(position / IO_BLOCKSIZE); // Disk cache block
if (controlBlock.cache[block] !== undefined) {
offset = position % IO_BLOCKSIZE;
while (count > 0) {
switch (operation) {
case 1: // Write: write from memory to cache
case 3: // Check: compare memory with disk cache
data = readWordByPhysical((controlBlock.mapped ? mapUnibus(address) : address));
if (data < 0) {
controlBlock.callback(controlBlock, 2, block * IO_BLOCKSIZE + offset, address, count); // NXM
return;
}
if (operation === 1) { // write: put data into disk cache
controlBlock.cache[block][offset] = data & 0xff;
controlBlock.cache[block][offset + 1] = (data >>> 8) & 0xff;
} else { // check: compare memory with disk cache
if (data !== ((controlBlock.cache[block][offset + 1] << 8) | controlBlock.cache[block][offset])) {
controlBlock.callback(controlBlock, 3, block * IO_BLOCKSIZE + offset, address, count); // mismatch
return;
}
}
address += 2;
count -= 2; // bytes to go.... (currently all write operations are whole offsets)
offset += 2;
break;
case 2: // Read: read to memory from cache
data = (controlBlock.cache[block][offset + 1] << 8) | controlBlock.cache[block][offset];
if (count > 1) { // tape can read odd number of bytes - of course it can. :-(
if (writeWordByPhysical((controlBlock.mapped ? mapUnibus(address) : address), data) < 0) {
controlBlock.callback(controlBlock, 2, block * IO_BLOCKSIZE + offset, address, count); // NXM
return;
}
address += 2;
count -= 2; // bytes to go....
} else {
if (writeByteByPhysical((controlBlock.mapped ? mapUnibus(address) : address), data & 0xff) < 0) {
controlBlock.callback(controlBlock, 2, block * IO_BLOCKSIZE + offset, address, count); // NXM
return;
}
address += 1;
--count; // bytes to go....
}
offset += 2;
break;
case 4: // accumulate a record count into the address field for tape operations
data = (controlBlock.cache[block][offset + 1] << 8) | controlBlock.cache[block][offset];
address = (data << 16) | (address >>> 16);
count -= 2; // bytes to go....
offset += 2;
break;
case 5: // read one lousy byte (for PTR) - result also into address field!!!!
address = controlBlock.cache[block][offset++];
count = 0; // force end!
break;
default:
panic(); // invalid operation - how did we get here?
}
if (offset >= IO_BLOCKSIZE) {
offset = 0;
block++;
if (controlBlock.cache[block] === undefined) {
break;
}
}
}
position = block * IO_BLOCKSIZE + offset;
}
if (count > 0) { // I/O not complete so we need to get some data
if (controlBlock.xhr === undefined) {
controlBlock.xhr = new XMLHttpRequest();
}
controlBlock.xhr.open("GET", controlBlock.url, true);
controlBlock.xhr.responseType = "arraybuffer";
controlBlock.xhr.onreadystatechange = function() {
if (controlBlock.xhr.readyState === controlBlock.xhr.DONE) {
getData(controlBlock, operation, position, address, count);
}
};
block = ~~(position / IO_BLOCKSIZE);
controlBlock.xhr.setRequestHeader("Range", "bytes=" + (block * IO_BLOCKSIZE) + "-" + ((block + 1) * IO_BLOCKSIZE - 1));
controlBlock.xhr.send(null);
return;
}
controlBlock.callback(controlBlock, 0, position, address, count); // success callback
}
// register the paper tape reader on the i/o page
iopage.register(0o17777550, 2, (function() {
var ptrcs, // 17777550 // paper tape reader control register 15 ERR 11 BUSY 7 DONE 6 IE 0 GO
ptrdb, // 17777552 // paper tape reader data buffer
ptrName, // paper tape file name
iMask; // interrupt mask
var ptControlblock;
function init() {
"use strict";
ptrcs = 0;
ptrdb = 0;
iMask = 0;
document.getElementById("ptr").onchange = selectPaperTape;
}
function selectPaperTape() {
ptrName = document.getElementById("ptr").value;
ptrcs = 0; // clear status when tape selected
if (ptControlblock !== undefined) {
ptControlblock = undefined; // Forget any existing details
}
}
function ptCallback(controlBlock, code, position, address, count) {
"use strict";
controlBlock.position = position;
ptrdb = address & 0xff; // diskIO function 5 stores a byte in address
if (code !== 0) {
ptrcs |= 0x8000; // set ERROR
}
if (ptrcs & 0x40) { // if ie...
iMask = 1;
requestInterrupt();
}
ptrcs = (ptrcs | 0x80) & ~0x800; // set DONE clear BUSY
}
init();
return {
access: function(physicalAddress, data, byteFlag) {
"use strict";
var result;
switch (physicalAddress & 0o6) {
case 0o0: // 17777550 ptrcs paper tape reader control register
result = insertData(ptrcs, physicalAddress, data, byteFlag);
if (result >= 0 && data >= 0) {
if ((result ^ ptrcs) & 0x40) { // ie change?
if (result & 0x40) { // setting...
iMask = 1;
requestInterrupt();
} else { // clearing...
iMask = 0;
}
}
ptrcs = (ptrcs & ~0x41) | (result & 0x41); // only update ie and go
if ((ptrcs & 0x8801) == 0x1) { // if not ERROR or BUSY and GO set...
if (ptrName === "") {