This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathdrive.js
1365 lines (1258 loc) · 46.9 KB
/
drive.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
/**
* @fileoverview Implements a generic disk drive controller.
* @author <a href="mailto:[email protected]">Jeff Parsons</a>
* @copyright © 2012-2020 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
if (typeof module !== "undefined") {
var Str = require("../../shared/lib/strlib");
var Web = require("../../shared/lib/weblib");
var DiskAPI = require("../../shared/lib/diskapi");
var Component = require("../../shared/lib/component");
var State = require("../../shared/lib/state");
var PDP11 = require("./defines");
var MessagesPDP11 = require("./messages");
var DiskPDP11 = require("./disk");
}
/**
* @typedef {{
* PRI: number,
* VEC: number,
* DRIVES: number
* }}
*/
var Config;
/**
* Since the Closure Compiler treats ES6 classes as @struct rather than @dict by default,
* it deters us from defining named properties on our components; eg:
*
* this['exports'] = {...}
*
* results in an error:
*
* Cannot do '[]' access on a struct
*
* So, in order to define 'exports', we must override the @struct assumption by annotating
* the class as @unrestricted (or @dict). Note that this must be done both here and in the
* Component class, because otherwise the Compiler won't allow us to *reference* the named
* property either.
*
* TODO: Consider marking ALL our classes unrestricted, because otherwise it forces us to
* define every single property the class uses in its constructor, which results in a fair
* bit of redundant initialization, since many properties aren't (and don't need to be) fully
* initialized until the appropriate init(), reset(), restore(), etc. function is called.
*
* The upside, however, may be that since the structure of the class is completely defined by
* the constructor, JavaScript engines may be able to optimize and run more efficiently.
*
* @unrestricted
*/
class DriveController extends Component {
/**
* DriveController(type, parms, bitsMessage, configDC, configDrive, configIO)
*
* The DriveController component has the following component-specific (parms) properties:
*
* autoMount: one or more JSON-encoded objects, each containing 'name' and 'path' properties
*
* @this {DriveController}
* @param {string} type
* @param {Object} parms
* @param {number} bitsMessage
* @param {Config} configDC
* @param {Array} configDrive
* @param {Object} configIO
*/
constructor(type, parms, bitsMessage, configDC, configDrive, configIO)
{
super(type, parms, bitsMessage);
/*
* We preliminarily parse and record any 'autoMount' object now, but we no longer process it
* until initBus(), because the Computer's getMachineParm() service may have an override for us.
*/
this.configMount = this.parseConfig(parms['autoMount']);
this.cAutoMount = 0;
this.configDC = configDC;
this.configDrive = configDrive;
this.configIO = configIO;
this.nDrives = configDC.DRIVES;
this.aDrives = new Array(this.nDrives);
this.fLocalDisks = (!Web.isMobile() && window && 'FileReader' in window);
this.sDiskSource = DriveController.SOURCE.NONE;
/*
* The following array keeps track of every disk image we've ever mounted. Each entry in the
* array is another array whose elements are:
*
* [0]: name of disk
* [1]: path of disk
* [2]: array of deltas, uninitialized until the disk is unmounted and/or all state is saved
*
* See functions addDiskHistory() and updateDiskHistory().
*/
this.aDiskHistory = [];
this.irq = null;
this['exports'] = {
'bootDisk': this.bootSelectedDisk,
'loadDisk': this.loadSelectedDisk,
'selectDrive': this.selectDrive,
'wait': this.waitDrives
};
}
/**
* parseConfig(config)
*
* @this {DriveController}
* @param {*} config
* @return {*}
*/
parseConfig(config)
{
if (config && typeof config == "string") {
try {
/*
* The most likely source of any exception will be right here, where we're parsing
* this JSON-encoded data.
*/
config = eval("(" + config + ")");
} catch (e) {
Component.error(this.type + " auto-mount error: " + e.message + " (" + config + ")");
config = null;
}
}
return config || {};
}
/**
* setBinding(sHTMLType, sBinding, control, sValue)
*
* @this {DriveController}
* @param {string} sHTMLType is the type of the HTML control (eg, "button", "list", "text", etc)
* @param {string} sBinding is the value of the 'binding' parameter stored in the HTML control's "data-value" attribute (eg, "listDisks")
* @param {HTMLElement} control is the HTML control DOM object (eg, HTMLButtonElement)
* @param {string} [sValue] optional data value
* @return {boolean} true if binding was successful, false if unrecognized binding request
*/
setBinding(sHTMLType, sBinding, control, sValue)
{
var dc = this;
switch (sBinding) {
case "listDisks":
this.bindings[sBinding] = control;
control.onchange = function onChangeListDisks(event) {
dc.updateSelectedDisk();
};
return true;
case "descDisk":
case "listDrives":
this.bindings[sBinding] = control;
/*
* I tried going with onclick instead of onchange, so that if you wanted to confirm what's
* loaded in a particular drive, you could click the drive control without having to change it.
* However, that doesn't seem to work for all browsers, so I've reverted to onchange.
*/
var controlSelect = /** @type {HTMLSelectElement} */ (control);
control.onchange = function onChangeListDrives(event) {
var iDrive = Str.parseInt(controlSelect.value, 10);
if (iDrive != null) dc.displayDisk(iDrive);
};
return true;
case "loadDisk":
this.bindings[sBinding] = control;
control.onclick = function onClickLoadDrive(event) {
dc.loadSelectedDisk();
};
return true;
case "bootDisk":
this.bindings[sBinding] = control;
control.onclick = function onClickBootDisk(event) {
dc.bootSelectedDisk();
};
return true;
case "saveDisk":
/*
* Yes, technically, this feature does not require "Local disk support" (which is really a reference
* to FileReader support), but since fLocalDisks is also false for all mobile devices, and since there
* is an "orthogonality" to disabling both features in tandem, let's just let it slide, OK?
*/
if (!this.fLocalDisks) {
if (DEBUG) this.log("Local disk support not available");
/*
* We could also simply hide the control; eg:
*
* control.style.display = "none";
*
* but removing the control altogether seems better.
*/
control.parentNode.removeChild(/** @type {Node} */ (control));
return false;
}
this.bindings[sBinding] = control;
control.onclick = function onClickSaveDrive(event) {
var controlDrives = dc.bindings["listDrives"];
if (controlDrives && controlDrives.options && dc.aDrives) {
var iDriveSelected = Str.parseInt(controlDrives.value, 10) || 0;
var drive = dc.aDrives[iDriveSelected];
if (drive) {
/*
* Note the similarity (and hence factoring opportunity) between this code and the HDC's "saveHD*" binding.
*/
var disk = drive.disk;
if (disk) {
if (DEBUG) dc.println("saving disk " + disk.sDiskPath + "...");
var sAlert = Web.downloadFile(disk.encodeAsBinary(), "octet-stream", true, disk.sDiskFile.replace(".json", ".img"));
Component.alertUser(sAlert);
} else {
dc.notice("No disk loaded in drive.");
}
} else {
dc.notice("No disk drive selected.");
}
}
};
return true;
case "mountDisk":
var controlInput = /** @type {Object} */ (control);
if (!this.fLocalDisks) {
if (DEBUG) this.log("Local disk support not available");
/*
* We could also simply hide the control; eg:
*
* controlInput.style.display = "none";
*
* but removing the control altogether seems better.
*/
controlInput.parentNode.removeChild(/** @type {Node} */ (controlInput));
return false;
}
this.bindings[sBinding] = controlInput;
/*
* Enable "Mount" button only if a file is actually selected
*/
controlInput.addEventListener('change', function() {
var fieldset = controlInput.children[0];
var files = fieldset.children[0].files;
var submit = fieldset.children[1];
submit.disabled = !files.length;
});
controlInput.onsubmit = function(event) {
var file = event.currentTarget[1].files[0];
if (file) {
var sDiskPath = file.name;
var sDiskName = Str.getBaseName(sDiskPath, true);
dc.loadSelectedDisk(sDiskName, sDiskPath, file);
}
/*
* Prevent reloading of web page after form submission
*/
return false;
};
return true;
default:
break;
}
return false;
}
/**
* initBus(cmp, bus, cpu, dbg)
*
* @this {DriveController}
* @param {ComputerPDP11} cmp
* @param {BusPDP11} bus
* @param {CPUStatePDP11} cpu
* @param {DebuggerPDP11} dbg
*/
initBus(cmp, bus, cpu, dbg)
{
this.cmp = cmp;
this.bus = bus;
this.cpu = cpu;
this.dbg = dbg;
var configMount = this.parseConfig(this.cmp.getMachineParm('autoMount'));
/*
* Add only drives from the machine-wide autoMount configuration that match drives managed by this component.
*/
if (configMount) {
for (var sDrive in configMount) {
if (sDrive.substr(0, 2) != this.type.substr(0, 2)) continue;
this.configMount[sDrive] = configMount[sDrive];
}
}
/*
* If we didn't need auto-mount support, we could defer controller and drive initialization until we received
* a powerUp() notification, at which point reset() would call initController(), or restore() would restore the
* controller.
*/
this.reset();
this.irq = this.cpu.addIRQ(this.configDC.VEC, this.configDC.PRI, this.bitsMessage);
bus.addIOTable(this, this.configIO);
bus.addResetHandler(this.reset.bind(this));
this.addDisk("None", DriveController.SOURCE.NONE, true);
if (this.fLocalDisks) this.addDisk("Local Disk", DriveController.SOURCE.LOCAL);
this.addDisk("Remote Disk", DriveController.SOURCE.REMOTE);
if (!this.autoMount()) this.setReady();
}
/**
* getDriveName(iDrive)
*
* Form a drive name using the two-letter controller type prefix and the drive number.
*
* @this {DriveController}
* @param {number} iDrive
* @return {string}
*/
getDriveName(iDrive)
{
var drive = this.aDrives[iDrive];
return drive.sName || "---";
}
/**
* getDriveNumber(sDrive)
*
* @this {DriveController}
* @param {string} sDrive
* @return {number} (0-3, or -1 if error)
*/
getDriveNumber(sDrive)
{
var iDrive = -1;
if (sDrive) {
iDrive = sDrive.charCodeAt(sDrive.length - 1) - 0x30;
if (iDrive < 0 || iDrive > 9) iDrive = -1;
}
return iDrive;
}
/**
* powerUp(data, fRepower)
*
* @this {DriveController}
* @param {Object|null} data
* @param {boolean} [fRepower]
* @return {boolean} true if successful, false if failure
*/
powerUp(data, fRepower)
{
if (!fRepower) {
if (!data) {
this.reset();
if (this.cmp.fReload) {
/*
* If the computer's fReload flag is set, we're required to toss all currently
* loaded disks and remount all disks specified in the auto-mount configuration.
*/
this.unloadAllDrives(true);
this.autoMount(true);
}
} else {
if (!this.restore(data)) return false;
}
/*
* Populate the HTML controls to match the actual (well, um, specified) number of floppy drives.
*/
var controlDrives;
if ((controlDrives = this.bindings['listDrives'])) {
while (controlDrives.firstChild) {
controlDrives.removeChild(controlDrives.firstChild);
}
controlDrives.value = "";
for (var iDrive = 0; iDrive < this.nDrives; iDrive++) {
var controlOption = document.createElement("option");
controlOption.value = iDrive;
controlOption.text = this.getDriveName(iDrive);
controlDrives.appendChild(controlOption);
}
if (this.nDrives > 0) {
controlDrives.value = "0";
this.displayDisk(0);
}
}
}
return true;
}
/**
* powerDown(fSave, fShutdown)
*
* @this {DriveController}
* @param {boolean} [fSave]
* @param {boolean} [fShutdown]
* @return {Object|boolean} component state if fSave; otherwise, true if successful, false if failure
*/
powerDown(fSave, fShutdown)
{
return fSave? this.save() : true;
}
/**
* reset()
*
* @this {DriveController}
*/
reset()
{
this.initController();
this.initDrives();
}
/**
* save()
*
* This implements save support for the DriveController component.
*
* @this {DriveController}
* @return {Object}
*/
save()
{
var state = new State(this);
state.set(0, this.saveController());
state.set(1, this.saveHistory());
state.set(2, this.saveDrives());
return state.data();
}
/**
* restore(data)
*
* This implements restore support for the DriveController component.
*
* @this {DriveController}
* @param {Object} data
* @return {boolean} true if successful, false if failure
*/
restore(data)
{
var fSuccess = true;
if (!this.initController(data[0])) fSuccess = false;
if (!this.initHistory(data[1])) fSuccess = false;
if (!this.initDrives(data[2])) fSuccess = false;
return fSuccess;
}
/**
* initController(aRegs)
*
* Placeholder for subclasses.
*
* @this {DriveController}
* @param {Array} [aRegs]
* @return {boolean} true if successful, false if failure
*/
initController(aRegs)
{
return true;
}
/**
* saveController()
*
* Placeholder for subclasses.
*
* @this {DriveController}
* @return {Array}
*/
saveController()
{
return [];
}
/**
* initDrive(drive, iDrive, configDrive, configDisk)
*
* @this {DriveController}
* @param {Object} drive
* @param {number} iDrive
* @param {Array} configDrive
* @param {Array} [configDisk]
* @return {boolean} true if successful, false if failure
*/
initDrive(drive, iDrive, configDrive, configDisk)
{
var i = 0;
var fSuccess = true;
drive.iDrive = iDrive;
drive.name = this.idComponent;
drive.fBusy = drive.fLocal = false;
drive.fnCallReady = null;
drive.fRemovable = true;
/*
* NOTE: We initialize the following drive properties to their MAXIMUMs; disks may have
* these or SMALLER values (subject to the limits of what the controller supports, of course).
*/
drive.sName = configDrive[i++] + iDrive;
drive.nCylinders = configDrive[i++];
drive.nHeads = configDrive[i++];
drive.nSectors = configDrive[i++];
drive.cbSector = configDrive[i++];
drive.iCylinderBoot = configDrive[i++];
drive.iHeadBoot = configDrive[i++];
drive.iSectorBoot = configDrive[i++];
drive.cbSectorBoot = configDrive[i++];
drive.status = configDrive[i];
/*
* The next group of properties are set by various controller command sequences.
*/
drive.bHead = 0;
drive.bCylinder = 0;
drive.bSector = 1;
drive.bSectorEnd = drive.nSectors; // aka EOT
drive.nBytes = drive.cbSector;
/*
* The next group of properties are managed by worker functions (eg, doRead()) to maintain state across DMA requests.
*/
drive.ibSector = 0;
drive.sector = null;
if (!drive.disk) {
drive.sDiskPath = ""; // ensure this is initialized to a default that displayDisk() can deal with
}
if (configDisk) {
var fLocal = configDisk[0];
var sDiskName = configDisk[1];
var sDiskPath = configDisk[2];
/*
* If we're restoring a local disk image, then the entire disk contents should be captured in aDiskHistory,
* so all we have to do is mount a blank disk and let disk.restore() do the rest; ie, there's nothing to
* "load" (it's a purely synchronous operation).
*
* Otherwise, we must call loadDrive(); in the common case, loadDrive() will have already "auto-mounted"
* the disk, so it will return true, and then we restore any deltas to the current image.
*
* However, if loadDrive() returns false, then it has initiated the load for a *different* disk image,
* so we must mark ourselves as "not ready" again, and add another "wait for ready" test in Computer before
* finally powering the CPU.
*/
if (fLocal) {
this.mountDrive(iDrive, sDiskName, sDiskPath);
}
else if (this.loadDrive(iDrive, sDiskName, sDiskPath, true)) {
if (drive.disk) {
if (sDiskPath) {
this.addDiskHistory(sDiskName, sDiskPath, drive.disk);
} else {
if (MAXDEBUG) Component.warning("Disk '" + (drive.disk.sDiskName || sDiskName) + "' not recorded properly in drive " + iDrive);
}
}
} else {
this.setReady(false);
}
}
return fSuccess;
}
/**
* initDrives(aConfigDisks)
*
* @this {DriveController}
* @param {Array} [aConfigDisks]
* @return {boolean} true if successful, false if failure
*/
initDrives(aConfigDisks)
{
var fSuccess = true;
for (var iDrive = 0; iDrive < this.aDrives.length; iDrive++) {
var drive = this.aDrives[iDrive];
if (drive === undefined) {
drive = this.aDrives[iDrive] = {};
}
var configDisk = aConfigDisks && aConfigDisks[iDrive];
if (!this.initDrive(drive, iDrive, this.configDrive, configDisk)) {
fSuccess = false;
}
}
return fSuccess;
}
/**
* saveDrive(drive)
*
* @this {DriveController}
* @param {Object} drive
* @return {Array}
*/
saveDrive(drive)
{
return [
drive.fLocal,
drive.sDiskName,
drive.sDiskPath
]
}
/**
* saveDrives()
*
* @this {DriveController}
* @return {Array}
*/
saveDrives()
{
var data = [];
for (var iDrive = 0; iDrive < this.aDrives.length; iDrive++) {
data.push(this.saveDrive(this.aDrives[iDrive]));
}
return data;
}
/**
* initHistory(aHistory)
*
* @this {DriveController}
* @param {Array} [aHistory]
* @return {boolean} true if successful, false if failure
*/
initHistory(aHistory)
{
/*
* Initialize the disk history (if available) before initializing the drives, so that any disk deltas can be
* applied to disk images that are already loaded.
*/
if (aHistory) this.aDiskHistory = aHistory;
return true;
}
/**
* saveHistory()
*
* This returns an array of entries, one for each disk image we've ever mounted, including any deltas; ie:
*
* [name, path, deltas]
*
* aDiskHistory contains exactly that, except that deltas may not be up-to-date for any currently mounted
* disk image(s), so we call updateHistory() for all those disks, and then aDiskHistory is ready to be saved.
*
* @this {DriveController}
* @return {Array}
*/
saveHistory()
{
for (var iDrive = 0; iDrive < this.aDrives.length; iDrive++) {
var drive = this.aDrives[iDrive];
if (drive.disk) {
this.updateDiskHistory(drive.sDiskName, drive.sDiskPath, drive.disk);
}
}
return this.aDiskHistory;
}
/**
* autoMount(fRemount)
*
* @this {DriveController}
* @param {boolean} [fRemount] is true if we're remounting all auto-mounted disks
* @return {boolean} true if one or more disk images are being auto-mounted, false if none
*/
autoMount(fRemount)
{
if (!fRemount) this.cAutoMount = 0;
for (var sDrive in this.configMount) {
var configDisk = this.configMount[sDrive];
var sDiskPath = configDisk['path'] || "";
var sDiskName = configDisk['name'] || this.findDisk(sDiskPath);
if (sDiskPath && sDiskName) {
var iDrive = this.getDriveNumber(sDrive);
if (iDrive >= 0 && iDrive < this.aDrives.length) {
if (!this.loadDrive(iDrive, sDiskName, sDiskPath, true) && fRemount) {
this.setReady(false);
}
continue;
}
}
this.notice("Incorrect auto-mount settings for drive " + sDrive + " (" + JSON.stringify(configDisk) + ")");
}
return !!this.cAutoMount;
}
/**
* loadSelectedDisk(sDiskName, sDiskPath, file)
*
* @this {DriveController}
* @param {string} [sDiskName]
* @param {string} [sDiskPath]
* @param {File} [file] is set if there's an associated File object
* @return {boolean}
*/
loadSelectedDisk(sDiskName, sDiskPath, file)
{
if (!sDiskName && !sDiskPath) {
var controlDisks = this.bindings["listDisks"];
if (controlDisks && controlDisks.options) {
sDiskName = controlDisks.options[controlDisks.selectedIndex].text;
sDiskPath = controlDisks.value;
}
}
var controlDrives = this.bindings["listDrives"];
var iDrive = controlDrives && Str.parseInt(controlDrives.value, 10);
if (iDrive === undefined || iDrive < 0 || iDrive >= this.aDrives.length) {
this.notice("Unable to load the selected drive");
return false;
}
if (!sDiskPath) {
this.unloadDrive(iDrive);
return true;
}
if (sDiskPath == DriveController.SOURCE.LOCAL) {
this.notice('Use "Choose File" and "Mount" to select and load a local disk.');
return false;
}
/*
* If the special DriveController.SOURCE.REMOTE path is selected, then we want to prompt the user for a URL.
* Oh, and make sure we pass an empty string as the 2nd parameter to prompt(), so that IE won't display
* "undefined" -- because after all, undefined and "undefined" are EXACTLY the same thing, right?
*
* TODO: This is literally all I've done to support remote disk images. There's probably more
* I should do, like dynamically updating "listDisks" to include new entries, and adding new entries
* to the save/restore data.
*/
if (sDiskPath == DriveController.SOURCE.REMOTE) {
sDiskPath = window.prompt("Enter the URL of a remote disk image.", "") || "";
if (!sDiskPath) return false;
sDiskName = Str.getBaseName(sDiskPath);
this.status('Attempting to load %s as "%s"', sDiskPath, sDiskName);
this.sDiskSource = DriveController.SOURCE.REMOTE;
}
else {
this.sDiskSource = sDiskPath;
}
this.loadDrive(iDrive, sDiskName, sDiskPath, false, file);
return true;
}
/**
* bootSelectedDisk()
*
* @this {DriveController}
* @return {boolean}
*/
bootSelectedDisk()
{
var drive;
var controlDrives = this.bindings["listDrives"];
var iDrive = controlDrives && Str.parseInt(controlDrives.value, 10);
if (iDrive == null || iDrive < 0 || iDrive >= this.aDrives.length || !(drive = this.aDrives[iDrive])) {
this.notice("Unable to boot the selected drive");
return false;
}
if (!drive.disk) {
this.notice("Load a disk into the drive first");
return false;
}
/*
* NOTE: We're calling setReset() BEFORE reading the boot code in order to eliminate any side-effects
* of the previous state of either the controller OR the CPU; for example, we don't want any previous MMU
* or UNIBUS Map registers affecting the simulated readData() call. Also, some boot code (eg, RSTS/E)
* expects the controller to be in a READY state; since setReset() triggers a call to our reset() handler,
* a READY state is assured, and the readData() call shouldn't do anything to change that.
*/
this.cpu.setReset(0, true, iDrive);
var err = this.readData(drive, drive.iCylinderBoot, drive.iHeadBoot, drive.iSectorBoot, drive.cbSectorBoot, 0x0000, 2);
if (err) {
this.notice("Unable to read the boot sector (" + err + ")");
return false;
}
return true;
}
/**
* mountDrive(iDrive, sDiskName, sDiskPath)
*
* @this {DriveController}
* @param {number} iDrive
* @param {string} sDiskName
* @param {string} sDiskPath
*/
mountDrive(iDrive, sDiskName, sDiskPath)
{
var drive = this.aDrives[iDrive];
this.unloadDrive(iDrive, true);
drive.fLocal = true;
var disk = new DiskPDP11(this, drive, DiskAPI.MODE.PRELOAD);
this.doneLoadDrive(drive, disk, sDiskName, sDiskPath, true);
}
/**
* loadDrive(iDrive, sDiskName, sDiskPath, fAutoMount, file)
*
* NOTE: If sDiskPath is already loaded, nothing needs to be done.
*
* @this {DriveController}
* @param {number} iDrive
* @param {string} sDiskName
* @param {string} sDiskPath
* @param {boolean} [fAutoMount]
* @param {File} [file] is set if there's an associated File object
* @return {number} 1 if disk loaded, 0 if queued up (or busy), -1 if already loaded
*/
loadDrive(iDrive, sDiskName, sDiskPath, fAutoMount, file)
{
var nResult = -1;
var drive = this.aDrives[iDrive];
if (drive.sDiskPath.toLowerCase() != sDiskPath.toLowerCase()) {
nResult++;
this.unloadDrive(iDrive, true);
if (drive.fBusy) {
this.notice(this.type + " busy");
}
else {
// this.status("disk queued: %s", sDiskName);
drive.fBusy = true;
if (fAutoMount) {
drive.fAutoMount = true;
this.cAutoMount++;
if (this.messageEnabled()) this.printMessage("auto-loading disk: " + sDiskName);
}
drive.fLocal = !!file;
var disk = new DiskPDP11(this, drive, DiskAPI.MODE.PRELOAD);
if (disk.load(sDiskName, sDiskPath, file, this.doneLoadDrive)) {
nResult++;
}
}
}
return nResult;
}
/**
* doneLoadDrive(drive, disk, sDiskName, sDiskPath, fAutoMount)
*
* The disk parameter is set if the disk was successfully loaded, null if not.
*
* @this {DriveController}
* @param {Object} drive
* @param {DiskPDP11} disk
* @param {string} sDiskName
* @param {string} sDiskPath
* @param {boolean} [fAutoMount]
*/
doneLoadDrive(drive, disk, sDiskName, sDiskPath, fAutoMount)
{
drive.fBusy = false;
if (disk) {
/*
* TODO: While this is a perfectly reasonable thing to do, one wonders if the Disk object shouldn't
* have done this itself, since we passed our Drive object to it (it already knows the drive's limits).
*/
if (disk.nCylinders > drive.nCylinders || disk.nHeads > drive.nHeads /* || disk.nSectors > drive.nSectors */) {
this.notice("Disk \"" + sDiskName + "\" too large for drive " + this.getDriveName(drive.iDrive));
disk = null;
}
}
if (disk) {
drive.disk = disk;
drive.sDiskName = sDiskName;
drive.sDiskPath = sDiskPath;
/*
* Inform the controller implementation (eg, RX11) of the disk change.
*/
this.notifyLoad(drive.iDrive);
/*
* Adding local disk image names to the disk list seems like a nice idea, but it's too confusing,
* because then it looks like the "Mount" button should be able to (re)load them, and that can NEVER
* happen, for security reasons; local disk images can ONLY be loaded via the "Mount" button after
* the user has selected them via the "Choose File" button.
*
* this.addDisk(sDiskName, sDiskPath);
*
* So we're going to take a different approach: when displayDisk() is asked to display the name
* of a local disk image, it will map all such disks to "Local Disk", and any attempt to "Mount" such
* a disk, will essentially result in a "Disk not found" error.
*/
this.addDiskHistory(sDiskName, sDiskPath, disk);
/*
* With the addition of notify(), users are now "alerted" whenever a disk has finished loading;
* notify() is selective about its output, using print() if a print window is open, alert() otherwise.
*/
this.notice("Loaded disk \"" + sDiskName + "\" in drive " + this.getDriveName(drive.iDrive), drive.fAutoMount || fAutoMount);
/*
* Since you usually want the Computer to have focus again after loading a new disk, let's try automatically
* updating the focus after a successful load.
*/
if (this.cmp) this.cmp.setFocus();
}
else {
drive.fLocal = false;
}
if (drive.fAutoMount) {
drive.fAutoMount = false;
if (!--this.cAutoMount) this.setReady();
}
this.displayDisk(drive.iDrive);
if (drive.fnCallReady) {
drive.fnCallReady();
drive.fnCallReady = null;
}
}
/**
* addDisk(sName, sPath, fTop)
*
* @this {DriveController}
* @param {string} sName
* @param {string} sPath
* @param {boolean} [fTop] (default is bottom)
*/
addDisk(sName, sPath, fTop)
{
var controlDisks = this.bindings["listDisks"];
if (controlDisks && controlDisks.options) {
for (var i = 0; i < controlDisks.options.length; i++) {
if (controlDisks.options[i].value == sPath) return;
}
var controlOption = document.createElement("option");
controlOption.text = sName;
controlOption.value = sPath;
if (fTop && controlDisks.childNodes[0]) {
controlDisks.insertBefore(controlOption, controlDisks.childNodes[0]);
} else {
controlDisks.appendChild(controlOption);
}
}
}
/**
* findDisk(sPath)
*
* This is used to deal with mount requests (eg, autoMount) that supply a path without a name;
* if we can find the path in the "listDisks" control, then we return the associated disk name.