-
Notifications
You must be signed in to change notification settings - Fork 68
/
disc.js
1159 lines (1036 loc) · 38.7 KB
/
disc.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
// Translated from beebjit by Chris Evans.
// https://github.com/scarybeasts/beebjit
import * as utils from "./utils.js";
/*
* TODO: use in fingerprinting
class Crc32Builder {
constructor() {
this._crc = 0xffffffff;
}
add(data) {
for (let i = 0; i < data.length; ++i) {
const byte = data[i];
this._crc ^= byte;
for (let j = 0; j < 8; ++j) {
const doEor = this._crc & 1;
this._crc = this._crc >>> 1;
if (doEor) this._crc ^= 0xedb88320;
}
}
}
get crc() {
return ~this._crc;
}
}
*/
class TrackBuilder {
/**
* @param {Track} track
*/
constructor(track) {
this._track = track;
this._track.length = IbmDiscFormat.bytesPerTrack;
this._index = 0;
this._pulsesIndex = 0;
this._lastMfmBit = 0;
this._crc = 0;
}
get track() {
return this._track;
}
setTrackLength() {
if (this._index > IbmDiscFormat.bytesPerTrack) throw new Error("Overflowed disc size");
if (this._index !== 0) this._track.length = this._index;
return this;
}
resetCrc() {
this._crc = IbmDiscFormat.crcInit(false);
return this;
}
appendFmDataAndClocks(data, clocks) {
if (this._index >= IbmDiscFormat.bytesPerTrack) throw new Error("Overflow in disc buliding");
this._track.pulses2Us[this._index++] = IbmDiscFormat.fmTo2usPulses(clocks, data);
this._crc = IbmDiscFormat.crcAddByte(this._crc, data);
return this;
}
appendFmByte(data) {
this.appendFmDataAndClocks(data, 0xff);
return this;
}
appendRepeatFmByte(data, count) {
for (let i = 0; i < count; ++i) this.appendFmByte(data);
return this;
}
fillFmByte(data) {
if (this._index >= IbmDiscFormat.bytesPerTrack) throw new Error("Overflowed disc size");
this.appendRepeatFmByte(data, IbmDiscFormat.bytesPerTrack - this._index);
return this;
}
appendRepeatFmByteWithClocks(data, clocks, count) {
for (let i = 0; i < count; ++i) this.appendFmDataAndClocks(data, clocks);
return this;
}
appendFmChunk(bytes) {
for (const byte of bytes) this.appendFmByte(byte);
return this;
}
appendCrc(isMfm) {
// TODO consider remembering isMfM if nothing else needs to know/
// could then break this into MFM and FM builder
const firstByte = (this._crc >>> 8) & 0xff;
const secondByte = this._crc & 0xff;
if (isMfm) {
this.appendMfmByte(firstByte);
this.appendMfmByte(secondByte);
} else {
this.appendFmByte(firstByte);
this.appendFmByte(secondByte);
}
return this;
}
appendMfmPulses(pulses) {
if (this._index >= IbmDiscFormat.bytesPerTrack) throw new Error("Overflowed disc size");
const existingPulses = this._track.pulses2Us[this._index];
const mask = 0xffff << this._pulsesIndex;
this._pulsesIndex = (this._pulsesIndex + 16) & 31;
this._track.pulses2Us[this._index] = (existingPulses & mask) | (pulses << this._pulsesIndex);
if (this._pulsesIndex === 0) this._index++;
return this;
}
appendMfmByte(data) {
const { lastBit, pulses } = IbmDiscFormat.mfmTo2usPulses(this._lastMfmBit, data);
this._lastMfmBit = lastBit;
this.appendMfmPulses(pulses);
this._crc = IbmDiscFormat.crcAddByte(this._crc, data);
return this;
}
appendRepeatMfmByte(data, count) {
for (let i = 0; i < count; ++i) this.appendMfmByte(data);
return this;
}
appendMfm3xA1Sync() {
for (let i = 0; i < 3; ++i) {
this.appendMfmPulses(IbmDiscFormat.mfmA1Sync);
this._crc = IbmDiscFormat.crcAddByte(this._crc, 0xa1);
}
return this;
}
appendMfmChunk(bytes) {
for (const byte of bytes) this.appendMfmByte(byte);
return this;
}
fillMfmByte(data) {
if (this._index >= IbmDiscFormat.bytesPerTrack) throw new Error("Overflowed disc size");
while (this._index < IbmDiscFormat.bytesPerTrack) this.appendMfmByte(data);
return this;
}
/**
* @param {number[]} pulseDeltas array of lengths between pulses
* @param {boolean} isMfm whether this is an MFM track
*/
buildFromPulses(pulseDeltas, isMfm) {
let hasWarned = false;
for (const pulse of pulseDeltas) {
if (!IbmDiscFormat.checkPulse(pulse, isMfm)) {
console.log(`Found a bad pulse for ${this.track.description}`);
}
if (!this.appendPulseDelta(pulse, isMfm) && !hasWarned) {
console.log(`Truncated disc data for ${this.track.description}, ignoring the rest`);
hasWarned = true;
}
}
this.setTrackLength();
}
appendPulseDelta(deltaUs, quantizeMfm) {
let num2UsUnits = quantizeMfm ? Math.round(deltaUs / 2) : 2 * Math.round(deltaUs / 4);
while (num2UsUnits--) {
if (this._index === IbmDiscFormat.bytesPerTrack) return false;
if (num2UsUnits === 0) {
this._track.pulses2Us[this._index] |= 0x80000000 >>> this._pulsesIndex;
}
this._pulsesIndex++;
if (this._pulsesIndex === 32) {
this._pulsesIndex = 0;
this._index++;
}
}
return true;
}
}
class RawDiscReader {
/**
* @param {Track} track
* @param {Number} bitOffset
*/
constructor(track, bitOffset) {
this._track = track;
this._pos = bitOffset;
}
readPulses() {
let pulsesPos = this._pos >>> 5;
const bitPos = this._pos & 0x1f;
let sourcePulses = this._track.pulses2Us[pulsesPos];
let pulses = (sourcePulses << bitPos) & 0xfffffffff;
if (pulsesPos === this._track.length) {
pulsesPos = 0;
this._pos = bitPos;
} else {
pulsesPos++;
this._pos += 32;
}
if (bitPos > 0) {
sourcePulses = this._track.pulses2Us[pulsesPos];
pulses |= sourcePulses >>> (32 - bitPos);
}
return pulses;
}
}
class MfmReader {
/**
* @param {RawDiscReader} rawReader
*/
constructor(rawReader) {
this._rawReader = rawReader;
}
read(numBytes) {
const data = new Uint8Array(numBytes);
let pulses = 0;
for (let offset = 0; offset < numBytes; ++offset) {
if ((offset & 1) === 0) {
pulses = this._rawReader.readPulses();
} else {
pulses = (pulses << 16) & 0xffffffff;
}
data[offset] = IbmDiscFormat._2usPulsesToMfm(pulses >>> 16);
}
return { data, clocks: null, iffyPulses: false };
}
get initialCrc() {
let crc = IbmDiscFormat.crcInit(false);
crc = IbmDiscFormat.crcAddByte(crc, 0xa1);
crc = IbmDiscFormat.crcAddByte(crc, 0xa1);
crc = IbmDiscFormat.crcAddByte(crc, 0xa1);
return crc;
}
}
class FmReader {
/**
* @param {RawDiscReader} rawReader
*/
constructor(rawReader) {
this._rawReader = rawReader;
}
read(numBytes) {
const data = new Uint8Array(numBytes);
const clocks = new Uint8Array(numBytes);
let iffyPulses = false;
for (let offset = 0; offset < numBytes; ++offset) {
const pulses = this._rawReader.readPulses();
const { data: dataByte, clock: clockByte, iffyPulses: iffy } = IbmDiscFormat._2usPulsesToFm(pulses);
data[offset] = dataByte;
clocks[offset] = clockByte;
iffyPulses |= iffy;
}
return { data, clocks, iffyPulses };
}
get initialCrc() {
return IbmDiscFormat.crcInit(false);
}
}
class Sector {
/**
* @param {Track} track
* @param {boolean} isMfm
* @param {Number} idPosBitOffset
*/
constructor(track, isMfm, idPosBitOffset) {
this.track = track;
this.isMfm = isMfm;
this.idPosBitOffset = idPosBitOffset;
this.dataPosBitOffset = null;
this.isDeleted = false;
this.sectorData = null;
this.hasDataCrcError = false;
this.byteLength = null;
const idReader = this._readerAt(this.idPosBitOffset);
const { data: headerData, iffyPulses } = idReader.read(6);
if (iffyPulses) {
console.log(`Iffy pulse in sector header ${this.description}`);
}
this.header = headerData;
let crc = idReader.initialCrc;
crc = IbmDiscFormat.crcAddByte(crc, IbmDiscFormat.idMarkDataPattern);
crc = IbmDiscFormat.crcAddBytes(crc, this.header.slice(0, 4));
const discCrc = (this.header[4] << 8) | this.header[5];
this.hasHeaderCrcError = crc !== discCrc;
}
_readerAt(bitOffset) {
const rawReader = new RawDiscReader(this.track, bitOffset);
return this.isMfm ? new MfmReader(rawReader) : new FmReader(rawReader);
}
get trackNumber() {
return this.header ? this.header[0] : undefined;
}
get sectorNumber() {
return this.header ? this.header[2] : undefined;
}
get description() {
return `${this.track.description} idpos ${this.idPosBitOffset} idtrack ${this.trackNumber} idsector ${this.sectorNumber} datapos ${this.dataPosBitOffset}`;
}
/**
* @param {Sector|undefined} nextSector
*/
read(nextSector) {
const pulsesPerByte = this.isMfm ? 16 : 32; // todo put in reader
if (this.dataPosBitOffset === null) {
console.log(`"Sector header without data ${this.description}"`);
return;
}
const dataMarker = this.isDeleted
? IbmDiscFormat.deletedDataMarkDataPattern
: IbmDiscFormat.dataMarkDataPattern;
const sectorStartByte = (this.dataPosBitOffset / pulsesPerByte) | 0;
const sectorEndByte =
(nextSector ? nextSector.idPosBitOffset / pulsesPerByte : (this.track.length * 32) / pulsesPerByte) | 0;
// Account for CRC and sync bytes.
let sectorSize = Sector.toSectorSize(sectorEndByte - sectorStartByte - 5);
this.hasDataCrcError = true;
let seenIffyData = false;
do {
const { crcOk, sectorData, iffyPulses } = this._tryLoadSectorData(dataMarker, sectorSize);
seenIffyData = iffyPulses;
if (crcOk) {
this.byteLength = sectorSize;
this.hasDataCrcError = false;
this.sectorData = sectorData;
break;
}
sectorSize = sectorSize >>> 1;
} while (sectorSize >= 128);
if (seenIffyData) {
console.log(`"Iffy pulse in sector data ${this.description}"`);
}
}
_tryLoadSectorData(dataMarker, sectorSize) {
const dataReader = this._readerAt(this.dataPosBitOffset);
let crc = IbmDiscFormat.crcAddByte(dataReader.initialCrc, dataMarker);
const { data: sectorData, iffyPulses } = dataReader.read(sectorSize + 2);
crc = IbmDiscFormat.crcAddBytes(crc, sectorData.slice(0, sectorSize));
const dataCrc = (sectorData[sectorSize] << 8) | sectorData[sectorSize + 1];
return { crcOk: dataCrc === crc, sectorData, iffyPulses };
}
static toSectorSize(size) {
if (size < 256) return 128;
if (size < 512) return 256;
if (size < 1024) return 512;
if (size < 2048) return 1024;
return 2048;
}
}
class Track {
constructor(upper, trackNum, initialByte) {
this.length = IbmDiscFormat.bytesPerTrack;
this.upper = upper;
this.trackNum = trackNum;
this.pulses2Us = new Uint32Array(256 * 13);
this.pulses2Us.fill(initialByte | (initialByte << 8) | (initialByte << 16) | (initialByte << 24));
}
get description() {
return `Track ${this.trackNum} ${this.upper ? "upper" : "lower"}`;
}
/**
* Debug functionality to try and interpret the track.
* @returns {Sector[]}
*/
findSectors() {
const sectors = this.findSectorIds();
for (let sectorIndex = 0; sectorIndex !== sectors.length; ++sectorIndex) {
const nextSector = sectors[sectorIndex + 1]; // Will be unset for last
sectors[sectorIndex].read(nextSector);
}
return sectors;
}
/**
* @returns {Sector[]}
*/
findSectorIds() {
const sectors = [];
// Pass 1: walk the track and find header and data markers.
const bitLength = this.length * 32;
let shiftRegister = 0;
let numShifts = 0;
let doMfmMarkerByte = false;
let isMfm = false;
let pulses = 0;
let markDetector = 0n;
let markDetectorPrev = 0n;
const all64b = 0xffffffffffffffffn;
const top32of64b = 0xffffffff00000000n;
const fmMarker = 0x8888888800000000n;
const mfmMarker = 0xaaaa448944894489n;
let dataByte = 0;
let sector = null;
for (let pulseIndex = 0; pulseIndex < bitLength; ++pulseIndex) {
if ((pulseIndex & 31) === 0) pulses = this.pulses2Us[pulseIndex >>> 5];
markDetectorPrev = (markDetectorPrev << 1n) & all64b;
markDetectorPrev |= markDetector >> 63n;
markDetector = (markDetector << 1n) & all64b;
shiftRegister = (shiftRegister << 1) & 0xffffffff;
numShifts++;
if (pulses & 0x80000000) {
markDetector |= 1n;
shiftRegister |= 1;
}
pulses = (pulses << 1) & 0xffffffff;
if ((markDetector & top32of64b) === fmMarker) {
const { clocks, data, iffyPulses } = IbmDiscFormat._2usPulsesToFm(Number(markDetector & 0xffffffffn));
if (iffyPulses || clocks !== IbmDiscFormat.markClockPattern) continue;
isMfm = false;
doMfmMarkerByte = false;
let num0s = 8;
for (let bits = markDetectorPrev; (bits & 0xfn) === 0x8n; bits >>= 4n) {
num0s++;
}
if (num0s <= 16) {
console.log(`Short zeros sync ${this.description}`);
}
dataByte = data;
} else if (markDetector === mfmMarker) {
// Next byte is MFM marker.
isMfm = true;
doMfmMarkerByte = true;
shiftRegister = 0;
numShifts = 0;
continue;
} else if (doMfmMarkerByte && numShifts === 16) {
dataByte = IbmDiscFormat._2usPulsesToMfm(shiftRegister);
doMfmMarkerByte = false;
} else {
continue;
}
switch (dataByte) {
case IbmDiscFormat.idMarkDataPattern: {
sector = new Sector(this, isMfm, pulseIndex + 1);
sectors.push(sector);
shiftRegister = 0;
numShifts = 0;
break;
}
case IbmDiscFormat.dataMarkDataPattern:
case IbmDiscFormat.deletedDataMarkDataPattern:
if (!sector || sector.dataPosBitOffset) {
console.log(
`Sector data without header ${this.description}; mark bitpos ${pulseIndex}; previous good sector ${sector ? sector.description : "none"}`,
);
} else {
sector.dataPosBitOffset = pulseIndex + 1;
if (dataByte === IbmDiscFormat.deletedDataMarkDataPattern) {
sector.isDeleted = true;
}
shiftRegister = 0;
numShifts = 0;
}
break;
default:
console.log(`Unknown marker byte ${utils.hexbyte(dataByte)} ${this.description}`);
}
}
return sectors;
}
}
class Side {
constructor(upper, initialByte) {
this.tracks = [];
for (let i = 0; i < IbmDiscFormat.tracksPerDisc; ++i) this.tracks[i] = new Track(upper, i, initialByte);
}
}
export class DiscConfig {
constructor() {
// TODO is this even useful?
this.logProtection = false;
this.logIffyPulses = false;
this.expandTo80 = false;
this.isQuantizeFm = false;
this.isSkipOddTracks = false;
this.isSkipUpperSide = false;
this.rev = 0;
this.revSpec = "";
}
}
class SsdFormat {
static get sectorSize() {
return 256;
}
static get sectorsPerTrack() {
return 10;
}
static get tracksPerDisc() {
return 80;
}
}
/**
* @param {Disc} disc
* @param {Uint8Array} data
* @param {boolean} isDsd
* @param {*} onChange
*/
export function loadSsd(disc, data, isDsd, onChange) {
const blankSector = new Uint8Array(SsdFormat.sectorSize);
const numSides = isDsd ? 2 : 1;
if (data.length % SsdFormat.sectorSize !== 0) {
throw new Error("SSD file size is not a multiple of sector size");
}
const maxSize = SsdFormat.sectorSize * SsdFormat.sectorsPerTrack * SsdFormat.tracksPerDisc * numSides;
if (data.length > maxSize) {
throw new Error("SSD file is too large");
}
let offset = 0;
for (let track = 0; track < SsdFormat.tracksPerDisc; ++track) {
for (let side = 0; side < numSides; ++side) {
const trackBuilder = disc.buildTrack(side === 1, track);
// Sync pattern at start of track, as the index pulse starts, aka GAP 5.
trackBuilder
.appendRepeatFmByte(0xff, IbmDiscFormat.stdGap1FFs)
.appendRepeatFmByte(0x00, IbmDiscFormat.stdSync00s);
for (let sector = 0; sector < SsdFormat.sectorsPerTrack; ++sector) {
// Sector header, aka ID.
trackBuilder
.resetCrc()
.appendFmDataAndClocks(IbmDiscFormat.idMarkDataPattern, IbmDiscFormat.markClockPattern)
.appendFmByte(track)
.appendFmByte(0)
.appendFmByte(sector)
.appendFmByte(1)
.appendCrc(false);
// Sync pattern between sector header and sector data, aka GAP 2.
trackBuilder
.appendRepeatFmByte(0xff, IbmDiscFormat.stdGap2FFs)
.appendRepeatFmByte(0x00, IbmDiscFormat.stdSync00s);
// Sector data.
const sectorData =
offset < data.length ? data.subarray(offset, offset + SsdFormat.sectorSize) : blankSector;
offset += SsdFormat.sectorSize;
trackBuilder
.resetCrc()
.appendFmDataAndClocks(IbmDiscFormat.dataMarkDataPattern, IbmDiscFormat.markClockPattern)
.appendFmChunk(sectorData)
.appendCrc(false);
if (sector !== SsdFormat.sectorsPerTrack - 1) {
// Sync pattern between sectors, aka GAP 3.
trackBuilder
.appendRepeatFmByte(0xff, IbmDiscFormat.std10SectorGap3FFs)
.appendRepeatFmByte(0x00, IbmDiscFormat.stdSync00s);
}
}
trackBuilder.fillFmByte(0xff);
}
}
if (onChange) {
// TODO, maybe construct the disc directly with this stuff?
// TODO maybe change this entirely and make it lazy; and have the onChange "pull" the disc as the format it wants
// instead of doing this here. Most stuff doesn't care about changes and only needs the image on save.
// Create a dataCopy large enough for all the sectors and tracks.
const dataCopy = new Uint8Array(maxSize);
dataCopy.set(data);
disc.setWriteTrackCallback(
/** @param {Track} trackObj */
(side, trackNum, trackObj) => {
const trackOffset =
SsdFormat.sectorSize * SsdFormat.sectorsPerTrack * (trackNum * numSides + (side ? 1 : 0));
for (const sector of trackObj.findSectors()) {
const sectorOffset = sector.sectorNumber * SsdFormat.sectorSize;
for (let x = 0; x < SsdFormat.sectorSize; ++x)
dataCopy[trackOffset + sectorOffset + x] = sector.sectorData[x];
}
onChange(dataCopy);
},
);
}
return disc;
}
class AdfFormat {
static get sectorSize() {
return 256;
}
static get sectorsPerTrack() {
return 16;
}
static get tracksPerDisc() {
return 80;
}
}
/**
* @param {Disc} disc
* @param {Uint8Array} data
* @param {boolean} isDsd
*/
export function loadAdf(disc, data, isDsd) {
const blankSector = new Uint8Array(AdfFormat.sectorSize);
const numSides = isDsd ? 2 : 1;
if (data.length % AdfFormat.sectorSize !== 0) {
throw new Error("ADF file size is not a multiple of sector size");
}
const maxSize = AdfFormat.sectorSize * AdfFormat.sectorsPerTrack * AdfFormat.tracksPerDisc * numSides;
if (data.length > maxSize) {
throw new Error("ADF file is too large");
}
let offset = 0;
for (let track = 0; track < AdfFormat.tracksPerDisc; ++track) {
if (offset >= data.length) break;
for (let side = 0; side < numSides; ++side) {
// Using recommended values from the 177x datasheet.
const trackBuilder = disc.buildTrack(side === 1, track);
trackBuilder.appendRepeatMfmByte(0x4e, 60);
for (let sector = 0; sector < AdfFormat.sectorsPerTrack; ++sector) {
trackBuilder
.appendRepeatMfmByte(0x00, 12)
.resetCrc()
.appendMfm3xA1Sync()
.appendMfmByte(IbmDiscFormat.idMarkDataPattern)
.appendMfmByte(track)
.appendMfmByte(0)
.appendMfmByte(sector)
.appendMfmByte(1)
.appendCrc(true);
// Sync pattern between sector header and sector data, aka GAP 2.
trackBuilder.appendRepeatMfmByte(0x4e, 22).appendRepeatMfmByte(0x00, 12);
// Sector data.
const sectorData =
offset < data.length ? data.subarray(offset, offset + AdfFormat.sectorSize) : blankSector;
offset += AdfFormat.sectorSize;
trackBuilder
.resetCrc()
.appendMfm3xA1Sync()
.appendMfmByte(IbmDiscFormat.dataMarkDataPattern)
.appendMfmChunk(sectorData)
.appendCrc(true);
// Sync pattern between sectors, aka GAP 3.
trackBuilder.appendRepeatMfmByte(0x4e, 24);
}
trackBuilder.fillMfmByte(0x4e);
}
}
// TODO writeback
return disc;
}
/**
* @returns {Uint8Array}
* @param {Disc} disc
*/
export function toSsdOrDsd(disc) {
const numSides = disc.isDoubleSided ? 2 : 1;
const result = new Uint8Array(
numSides * SsdFormat.tracksPerDisc * SsdFormat.sectorsPerTrack * SsdFormat.sectorSize,
);
let offset = 0;
for (let trackNum = 0; trackNum < disc.tracksUsed; ++trackNum) {
for (let side = 0; side < numSides; ++side) {
const trackObj = disc.getTrack(side === 1, trackNum);
for (const sector of trackObj.findSectors()) {
const sectorOffset = offset + sector.sectorNumber * SsdFormat.sectorSize;
if (sector.hasDataCrcError || sector.hasHeaderCrcError) {
console.log(`Skipping sector ${sector.description} with bad CRC`);
continue;
}
for (let x = 0; x < SsdFormat.sectorSize; ++x) result[sectorOffset + x] = sector.sectorData[x];
}
offset += SsdFormat.sectorsPerTrack * SsdFormat.sectorSize;
}
}
return result.slice(0, offset);
}
const HfeHeaderV1 = "HXCPICFE";
const HfeHeaderV3 = "HXCHFEV3";
const HfeV3OpcodeMask = 0xf0;
const HfeV3OpcodeNop = 0xf0;
const HfeV3OpcodeSetIndex = 0xf1;
const HfeV3OpcodeSetBitrate = 0xf2;
const HfeV3OpcodeSkipBits = 0xf3;
const HfeV3OpcodeRand = 0xf4;
function hfeByteFlip(val) {
let ret = 0;
if (val & 0x80) ret |= 0x01;
if (val & 0x40) ret |= 0x02;
if (val & 0x20) ret |= 0x04;
if (val & 0x10) ret |= 0x08;
if (val & 0x08) ret |= 0x10;
if (val & 0x04) ret |= 0x20;
if (val & 0x02) ret |= 0x40;
if (val & 0x01) ret |= 0x80;
return ret;
}
/**
* @param {Uint8Array} metadata
* @param {Number} track
*/
function hfeGetTrackOffsetAndLength(metadata, track) {
const index = track << 2;
const offset = 512 * (metadata[index] + (metadata[index + 1] << 8));
const length = metadata[index + 2] + (metadata[index + 3] << 8);
return { offset, length };
}
/**
* @param {Disc} disc
* @param {Uint8Array} data
*/
export function loadHfe(disc, data) {
if (data.length < 512) throw new Error("HFE file missing header");
const header = new TextDecoder("ascii").decode(data.slice(0, 8));
let isV3 = false;
let hfeVersion = 1; // TODO maybe in metadata?
if (header === HfeHeaderV1) {
// ... note in metadata?
} else if (header === HfeHeaderV3) {
hfeVersion = 3;
isV3 = true;
} else {
throw new Error(`HFE file bad header '${header}'`);
}
if (data[8] !== 0) throw new Error("HFE file revision not 0");
if (data[11] !== 2 && data[11] !== 0) {
if (data[11] === 0xff) {
console.log(`Unknown HFE encoding ${data[11]}, trying anyway`);
} else {
throw new Error(`HFE encoding not ISOIBM_(M)FM_ENCODING: ${data[11]}`);
}
}
const numSides = data[10];
if (numSides < 1 || numSides > 2) throw new Error(`Invalid number of sides: ${numSides}`);
const numTracks = data[9];
if (numTracks > IbmDiscFormat.tracksPerDisc) throw new Error(`Too many tracks: ${numTracks}`);
let expandShift = 0;
if (disc.config.expandTo80 && numTracks * 2 <= IbmDiscFormat.tracksPerDisc) {
expandShift = 1;
console.log("Expanding 40 tracks to 80");
}
console.log(`HFE v${hfeVersion} loading ${numSides} sides, ${numTracks} tracks`);
const lutOffset = 512 * (data[18] + (data[19] << 8));
if (lutOffset + 512 > data.length) throw new Error("HFE LUT doesn't fit");
const metadata = data.slice(lutOffset, lutOffset + 512);
for (let trackNum = 0; trackNum < numTracks; ++trackNum) {
let actualTrackNum = trackNum;
if (disc.config.isSkipOddTracks) {
if (trackNum & 1) continue;
actualTrackNum = trackNum >>> 1;
}
actualTrackNum = actualTrackNum << expandShift;
const { offset, length } = hfeGetTrackOffsetAndLength(metadata, trackNum);
if (offset + length > data.length)
throw new Error(
`HFE track ${trackNum} doesn't fit (length ${length} offset ${offset} file length ${data.length})`,
);
const trackData = data.slice(offset, offset + length);
for (let sideNum = 0; sideNum < numSides; ++sideNum) {
const bufLen = length >> 1;
let bytesWritten = 0;
if (disc.config.isSkipUpperSide && sideNum === 1) continue;
let isSetBitRate = false;
let isSkipBits = false;
let skipBitsLength = 0;
let pulses = 0;
let shiftCounter = 0;
const trackObj = disc.getTrack(sideNum === 1, actualTrackNum);
disc.setTrackUsed(sideNum === 1, actualTrackNum);
const rawPulses = trackObj.pulses2Us;
for (let byteIndex = 0; byteIndex < bufLen; ++byteIndex) {
if (bytesWritten === rawPulses.length) {
console.log(`HFE track ${trackNum} truncated`);
break;
}
const index = ((byteIndex >>> 8) << 9) + (sideNum << 8) + (byteIndex & 0xff);
let byte = hfeByteFlip(trackData[index]);
let numBits = 8;
if (isSetBitRate) {
isSetBitRate = false;
if (byte < 64 || byte > 80) {
console.log(`HFE v3 SETBITRATE wild (72=250kbit) track: ${trackNum} ${byte}`);
}
continue;
} else if (isSkipBits) {
isSkipBits = false;
if (byte === 0 || byte >= 8) {
throw new Error(`HFE v3 invalid skipbits ${byte}`);
}
skipBitsLength = byte;
continue;
} else if (skipBitsLength) {
byte = (byte << (8 - skipBitsLength)) & 0xff;
numBits = skipBitsLength;
skipBitsLength = 0;
} else if (isV3 && (byte & HfeV3OpcodeMask) === HfeV3OpcodeMask) {
switch (byte) {
case HfeV3OpcodeNop:
continue; // NB continue
case HfeV3OpcodeSetIndex:
if (bytesWritten !== 0)
console.log(`HFEv3 SETINDEX not at byte 0, track ${trackNum}: ${bytesWritten}`);
continue; // NB continue
case HfeV3OpcodeSetBitrate:
isSetBitRate = true;
continue; // NB continue
case HfeV3OpcodeSkipBits:
isSkipBits = true;
continue; // NB continue
case HfeV3OpcodeRand:
// internally we represent weak bits on disc as a no flux area.
byte = 0;
break; // NB a break
default:
throw new Error(`Unknown HFE v3 opcode ${byte}`);
}
}
for (let bitIndex = 0; bitIndex < numBits; ++bitIndex) {
pulses = ((pulses << 1) & 0xffffffff) | (byte & 0x80 ? 1 : 0);
byte = (byte << 1) & 0xff;
if (++shiftCounter === 32) {
rawPulses[bytesWritten] = pulses;
bytesWritten++;
pulses = 0;
shiftCounter = 0;
}
}
}
trackObj.length = bytesWritten;
}
}
// TODO consider writeback here
return disc;
}
export class Disc {
/**
* @returns {Disc} a new blank disc
*/
static createBlank() {
return new Disc(true, new DiscConfig());
}
/**
* @param {boolean} isWriteable
* @param {DiscConfig} config
* @param {string} name
*/
constructor(isWriteable, config, name) {
this.config = config;
this.name = name;
this.isDirty = false;
this.dirtySide = -1;
this.dirtyTrack = -1;
this.tracksUsed = 0;
this.isDoubleSided = false;
this.writeTrackCallback = undefined;
this.isWriteable = isWriteable;
this.initSurface(0);
}
setWriteTrackCallback(callback) {
this.writeTrackCallback = callback;
}
get writeProtected() {
return !this.isWriteable;
}
/**
* @param {boolean} isSideUpper
* @param {Number} trackNum
* @returns {Track} */
getTrack(isSideUpper, trackNum) {
return isSideUpper ? this.upperSide.tracks[trackNum] : this.lowerSide.tracks[trackNum];
}
buildTrack(isSideUpper, trackNum) {
this.setTrackUsed(isSideUpper, trackNum);
return new TrackBuilder(this.getTrack(isSideUpper, trackNum));
}
setTrackUsed(isSideUpper, trackNum) {
if (isSideUpper) this.isDoubleSided = true;
this.tracksUsed = Math.max(this.tracksUsed, trackNum + 1);
}
initSurface(initialByte) {
this.lowerSide = new Side(false, initialByte);
this.upperSide = new Side(true, initialByte);
this.tracksUsed = 0;
this.isDoubleSided = false;
}
readPulses(isSideUpper, track, position) {
return this.getTrack(isSideUpper, track).pulses2Us[position];
}
/**
* @param {boolean} isSideUpper
* @param {Number} track
* @param {Number} position
* @param {Number} pulses
*/
writePulses(isSideUpper, track, position, pulses) {
const trackObj = this.getTrack(isSideUpper, track);
if (position >= trackObj.length)
throw new Error(`Attempt to write off end of track ${position} > ${track.length}`);
if (this.isDirty) {
if (isSideUpper !== this.dirtySide || track !== this.dirtyTrack)
throw new Error("Switched dirty track or side");
}
this.isDirty = true;
this.dirtySide = isSideUpper;
this.dirtyTrack = track;
trackObj.pulses2Us[position] = pulses;
// TODO a debug log flag for this
// console.log(`wrote to ${track}:${position * 32}`);
}
flushWrites() {
if (!this.isDirty) {
if (this.dirtySide !== -1 || this.dirtyTrack !== -1) throw new Error("Bad state in disc dirty tracking");
return;
}
const dirtySide = this.dirtySide;
const dirtyTrack = this.dirtyTrack;
this.isDirty = false;
this.dirtySide = -1;
this.dirtyTrack = -1;
if (!this.writeTrackCallback) return;
const trackObj = this.getTrack(dirtySide, dirtyTrack);
this.writeTrackCallback(dirtySide, dirtyTrack, trackObj);
this.setTrackUsed(dirtySide, dirtyTrack);
}
logSummary() {
const maxTrack = this.tracksUsed;
const numSides = this.isDoubleSided ? 2 : 1;
for (let side = 0; side < numSides; ++side) {
for (let trackNum = 0; trackNum < maxTrack; ++trackNum) {
const track = this.getTrack(side === 1, trackNum);
const sectors = track.findSectors();
if (sectors.length) {
if (track.length >= IbmDiscFormat.bytesPerTrack * 1.015) {