-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathD64Image.pas
More file actions
437 lines (362 loc) · 15.8 KB
/
D64Image.pas
File metadata and controls
437 lines (362 loc) · 15.8 KB
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
namespace D64;
//
// Based on Info from http://unusedino.de/ec64/technical/formats/d64.html and http://unusedino.de/ec64/technical/formats/d71.html
//
type
DiskFormat = public abstract class
public
property FormatName: String read; abstract;
property MinNumberOfTracks: Int32 read; abstract;
property MaxNumberOfTracks: Int32 read; abstract;
property TrackOffsets: array of Int32 read; abstract;
property SectorsPerTrack: array of Byte read; abstract;
const SectorSize = $100;
const FileEntrySize = $20;
class method DiskFormatForExtension(aExtension: not nullable String): not nullable DiskFormat;
begin
case aExtension.ToLower of
".d64": result := new D64DiskFormat;
".d61": result := new D64DiskFormat;
".d71": result := new D71DiskFormat;
".d81": result := new D81DiskFormat;
else raise new Exception(String.Format("Unsupported disk format: '{0}'", aExtension));
end;
end;
method GetTrackOffset(aTrack: Int32): Int32; virtual;
begin
result := TrackOffsets[aTrack];
end;
method GetSectorsForTrack(aTrack: Int32): Byte; virtual;
begin
result := SectorsPerTrack[aTrack];
end;
method GetMinimumImageSize: Int32; virtual;
begin
var lTrack := MinNumberOfTracks;
result := GetTrackOffset(lTrack)+GetSectorsForTrack(lTrack)*SectorSize;
end;
end;
D64DiskFormat = public class(DiskFormat)
public
property FormatName: String read "D64"; override;
property MinNumberOfTracks: Int32 read 35; override;
property MaxNumberOfTracks: Int32 read 40; override;
property TrackOffsets: array of Int32 := [-1, // Tracks are one-based
$00000, $01500, $02A00, $03F00, $05400, $06900, $07E00, $09300, $0A800, $0BD00,
$0D200, $0E700, $0FC00, $11100, $12600, $13B00, $15000, $16500, $17800, $18B00,
$19E00, $1B100, $1C400, $1D700, $1EA00, $1FC00, $20E00, $22000, $23200, $24400,
$25600, $26700, $27800, $28900, $29A00, $2AB00, $2BC00, $2CD00, $2DE00, $2EF00]; override;
property SectorsPerTrack: array of Byte := [-1, // Tracks are one-based
21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 19, 19, 19,
19, 19, 19, 19, 18, 18, 18, 18, 18, 18,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17]; override;
end;
D71DiskFormat = public class(DiskFormat)
public
property FormatName: String read "D71"; override;
property MinNumberOfTracks: Int32 read 70; override;
property MaxNumberOfTracks: Int32 read 70; override;
property TrackOffsets: array of Int32 := [-1, // Tracks are one-based
$00000, $01500, $02A00, $03F00, $05400, $06900, $07E00, $09300, $0A800, $0BD00,
$0D200, $0E700, $0FC00, $11100, $12600, $13B00, $15000, $16500, $17800, $18B00,
$19E00, $1B100, $1C400, $1D700, $1EA00, $1FC00, $20E00, $22000, $23200, $24400,
$25600, $26700, $27800, $28900, $29A00,
$2AB00, $2C000, $2D500, $2EA00, $2FF00, $31400, $32900, $33E00, $35300, $36800,
$37D00, $39200, $3A700, $3BC00, $3D100, $3E600, $3FB00, $41000, $42300, $43600,
$44900, $45C00, $46F00, $48200, $49500, $4A700, $4B900, $4CB00, $4DD00, $4EF00,
$50100, $51200, $52300, $53400, $54500]; override;
property SectorsPerTrack: array of Byte := [-1, // Tracks are one-based
21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 19, 19, 19,
19, 19, 19, 19, 18, 18, 18, 18, 18, 18,
17, 17, 17, 17, 17,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 19, 19, 19,
19, 19, 19, 19, 18, 18, 18, 18, 18, 18,
17, 17, 17, 17, 17]; override;
end;
D81DiskFormat = public class(DiskFormat)
public
property FormatName: String read "D81"; override;
property MinNumberOfTracks: Int32 read 80; override;
property MaxNumberOfTracks: Int32 read 80; override;
property TrackOffsets: array of Int32 read []; override;
property SectorsPerTrack: array of Byte read []; override;
method GetTrackOffset(aTrack: Int32): Int32; override;
begin
result := (aTrack-1)*$2800;
end;
method GetSectorsForTrack(aTrack: Int32): Byte; override;
begin
result := 40;
end;
end;
DiskImage = public class
private
protected
public
constructor withFile(aFilename: not nullable String);
begin
constructor withBinary(File.ReadBinary(aFilename), DiskFormat.DiskFormatForExtension(aFilename.PathExtension));
end;
constructor withBinary(aBinary: not nullable ImmutableBinary; aFormat: DiskFormat);
begin
var lMinimumSize := aFormat.GetMinimumImageSize;
if aBinary.Length < lMinimumSize then
raise new Exception(String.Format("File too small to be a {0} image", aFormat.FormatName));
Format := aFormat;
Binary := aBinary;
Directory := new D64Directory withImage(self);
end;
method GetSector(aSector: Integer) Track(aTrack: Integer): D64Sector;
begin
result := new D64Sector withImage(self) Sector(aSector) Track(aTrack);
end;
property Format: DiskFormat read private write;
property Binary: ImmutableBinary read private write;
property Directory: D64Directory read private write;
property Files: ImmutableList<D64File> read Directory.Files;
property Name: String read Directory.Name;
property DisplayName: String read Directory.DisplayName;
end;
D64Sector = public class
public
property Image: DiskImage read private write;
property Sector: Int32 read private write;
property Track: Int32 read private write;
property SectorOffset: Int32 read private write;
property Bytes[aOffset: Byte]: Byte read get_Bytes; default;
method GetBytes(aOffset: Byte; aCount: Int32): ImmutableBinary;
begin
if Integer(aOffset)+aCount > 256 then
raise new Exception(String.Format("Invalid offset/count pair: {0},{1}", aOffset, aCount));
result := Image.Binary.Subdata(SectorOffset+aOffset, aCount);
end;
method GetBytesAsArray(aOffset: Byte; aCount: Int32): array of Byte;
begin
if Integer(aOffset)+aCount > 256 then
raise new Exception(String.Format("Invalid offset/count pair: {0},{1}", aOffset, aCount));
result := Image.Binary.Read(SectorOffset+aOffset, aCount);
end;
method GetBytes(): ImmutableBinary; inline;
begin
result := GetBytes(0, Image.Format.SectorSize);
end;
method GetBytesAsArray(): array of Byte; inline;
begin
result := GetBytesAsArray(0, Image.Format.SectorSize);
end;
method GetString(aOffset: Byte; aCount: Int32): String;
begin
var lArray := GetBytesAsArray(aOffset, aCount);
result := Encoding.ASCII.GetString(lArray).TrimEnd([#$a0]);
end;
method GetUnicodeString(aOffset: Byte; aCount: Int32): String;
begin
var lArray := GetBytesAsArray(aOffset, aCount);
result := PETSCII.GetString(lArray).TrimEnd([#$a0]);
end;
[ToString]
method ToString: String;
begin
result := Convert.ToHexString(Image.Binary.ToArray, SectorOffset, Image.Format.SectorSize);
var i := 32;
while i < length(result) do begin
result := result.Insert(i, Environment.LineBreak);
inc(i, 32+length(Environment.LineBreak));
end;
result := String.Format("Track {0} Sector {1}:", Track, Sector)+Environment.LineBreak+result;
end;
unit
constructor withImage(aImage: DiskImage) Sector(aSector: Integer) Track(aTrack: Integer);
begin
Image := aImage;
Sector := aSector;
Track := aTrack;
if (aTrack < 1) or (aTrack > Image.Format.MaxNumberOfTracks) then
raise new Exception(String.Format("Invalid track number {0}", aTrack));
var lTrackOffset := Image.Format.GetTrackOffset(aTrack);
if lTrackOffset > Image.Binary.Length then
raise new Exception(String.Format("Image too small to contain track {0}", aTrack));
if (aSector < 0) or (aSector >= Image.Format.GetSectorsForTrack(aTrack)) then
raise new Exception(String.Format("Invalid sector number {0} for track {1}", aSector, aTrack));
SectorOffset := lTrackOffset+aSector*Image.Format.SectorSize;
if SectorOffset+Image.Format.SectorSize > Image.Binary.Length then
raise new Exception(String.Format("Image too small to contain sector {0} on track {1}", aSector, aTrack));
end;
private
method get_Bytes(aOffset: Byte): Byte;
begin
var data := Image.Binary.Read(SectorOffset+aOffset, 1);
result := data[0];
end;
end;
D64Directory = public class
public
property Image: DiskImage read private write;
property BAMSector: D64Sector read private write;
property BAM: ImmutableBinary read private write;
property DiskVersion: Byte read BAMSector[2];
property DOSType: String read private write;
property Name: String read private write;
property DisplayName: String read private write;
property FreeSectors: Int32 read private write;
property TotalSectors: Int32 read private write;
property Files: ImmutableList<D64File> read private write;
[ToString]
method ToString: String;
begin
result := "";
result := result+String.Format("Name: '{0}'", Name)+Environment.LineBreak;
result := result+String.Format("Disk Version: ${0}", Convert.ToHexString(DiskVersion))+Environment.LineBreak;
result := result+String.Format("Free Sectors: {0} of {1}", FreeSectors, TotalSectors)+Environment.LineBreak;
result := result+String.Format("Free Bytes: {0} of {1}", FreeSectors*Image.Format.SectorSize, TotalSectors*Image.Format.SectorSize)+Environment.LineBreak;
for each f in Files do
result := result+String.Format("'{0}' {1}, {2}", f.Name, f.FileType, f.Size)+Environment.LineBreak;
end;
unit
constructor withImage(aImage: DiskImage);
begin
Image := aImage;
var lDirectoryTrack: Byte := 18;
var lDirectorySector: Byte := 1;
var lFree := 0;
var lTotal := 0;
if Image.Format is D81DiskFormat then begin
BAMSector := Image.GetSector(0) Track(40);
Name := BAMSector.GetString($04, $10);
DisplayName := BAMSector.GetUnicodeString($04, $10);
DOSType := BAMSector.GetString($19, $2);
lDirectoryTrack := BAMSector[$00];
lDirectorySector := BAMSector[$01];
var lBAM1 := Image.GetSector(1) Track(40).GetBytesAsArray;
var lBAM2 := Image.GetSector(2) Track(40).GetBytesAsArray;
for t: Int32 := 1 to 80 do begin
if t ≠ 40 then begin
var lTrack := if t <= 40 then t else t-40;
var lBAMData := if t <= 40 then lBAM1 else lBAM2;
var lOffset := $10+(lTrack-1)*6;
inc(lFree, lBAMData[lOffset]);
inc(lTotal, Image.Format.GetSectorsForTrack(t));
end;
end;
end
else begin
BAMSector := Image.GetSector(0) Track(18);
Name := BAMSector.GetString($90, $10);
DisplayName := BAMSector.GetUnicodeString($90, $10);
DOSType := BAMSector.GetString($a5, $2);
BAM := BAMSector.GetBytes($04, $8f-$04);
var lBAMBytes := BAM.ToArray;
for t: Byte := 1 to 35 do begin
if t ≠ 18 then begin
var lOffset := (t-1)*4;
//writeLn(String.Format("{0} of {1} Free", BAM[lOffset], Image.Format.GetSectorsForTrack(t)));
inc(lFree, lBAMBytes[lOffset]);
inc(lTotal, Image.Format.GetSectorsForTrack(t));
end;
end;
end;
FreeSectors := lFree;
TotalSectors := lTotal;
//var lBytes := BAMSector.GetBytesAsArray;
//var lNextTrack := lBytes[$00];
//var lNextSector := lBytes[$01];
var lNextTrack := lDirectoryTrack;
var lNextSector := lDirectorySector;
var lFiles := new List<D64File>();
while (lNextTrack ≠ 0) and (lNextSector ≠ $ff) do begin
var lSector := Image.GetSector(lNextSector) Track(lNextTrack);
var lBytes := lSector.GetBytesAsArray();
lNextTrack := lBytes[$00];
lNextSector := lBytes[$01];
for f: Int32 := 0 to 7 do begin
var lOffset := f*Image.Format.FileEntrySize;
var lFileType := lBytes[lOffset+$02];
if lFileType ≠ 0 then
lFiles.Add(new D64File withImage(Image) Sector(lSector) &Index(f));
end;
end;
Files := lFiles;
end;
end;
D64File = public class
public
property Image: DiskImage read private write;
property FileTypeCode: Byte read private write;
property FileType: String read private write;
property Name: String read private write;
property DisplayName: String read private write;
property Size: Int32 read private write;
property StartTrack: Byte read private write;
property StartSector: Byte read private write;
property &Locked: Boolean read private write;
property Closed: Boolean read private write;
property LoadAddress: Int32 read get_LoadAddress;
property SideTrack: Byte read private write;
property SideSector: Byte read private write;
property RecordLength: Byte read private write;
property IsDirectory: Boolean read (StartTrack = 18) and (StartSector = 0);
method GetBytes: ImmutableBinary;
begin
var lNextTrack := StartTrack;
var lNextSector := StartSector;
//var lResult := new Binary(Size); // H3 parameter 1 is "Int32" should be "array of Byte"
var lResult := new Binary();
while lNextTrack ≠ 0 do begin
var lSector := Image.GetSector(lNextSector) Track(lNextTrack);
var lBytes := lSector.GetBytesAsArray();
lNextTrack := lBytes[$00];
lNextSector := lBytes[$01];
var lStart := if lResult.Length = 0 then $04 else $02;
var lEnd := if lNextTrack ≠ 0 then $ff else lNextSector;
var lLength := lEnd-lStart+1;
if lLength > 0 then
lResult.Write(lBytes, lStart, lLength);
end;
result := lResult;
end;
unit
constructor withImage(aImage: DiskImage) Sector(aSector: D64Sector) &Index(aIndex: Byte);
begin
if (aIndex < 0) or (aIndex > 7) then
raise new Exception(String.Format("Invalid fine Index: {0}", aIndex));
Image := aImage;
var lOffset := aIndex*Image.Format.FileEntrySize;
var lBytes := aSector.GetBytesAsArray(lOffset, Image.Format.FileEntrySize);
FileTypeCode := lBytes[$02];
if FileTypeCode ≠ 0 then begin
FileType := case (FileTypeCode and $07) of
$00: "DEL";
$01: "SEQ";
$02: "PRG";
$03: "USR";
$04: "REL";
end;
&Locked := FileTypeCode and $40 > 0;
&Closed := FileTypeCode and $80 > 0;
StartTrack := lBytes[$03];
StartSector := lBytes[$04];
Name := aSector.GetString(lOffset+$05, $10);
DisplayName := aSector.GetUnicodeString(lOffset+$05, $10);
Size := Int32(lBytes[$1e])+Int32(lBytes[$1f])*$100;
SideTrack := lBytes[$15];
SideSector := lBytes[$16];
RecordLength := lBytes[$17];
//&write(Convert.ToHexString(lBytes));
//&write(" ");
//writeLn(String.Format("'{0}' {1}, {2}", Name, FileTypeCode, Size));
end;
end;
private
method get_LoadAddress: Int32;
begin
if StartTrack = 0 then
exit 0;
var lSector := Image.GetSector(StartSector) Track(StartTrack);
var lBytes := lSector.GetBytesAsArray();
result := lBytes[$02]+lBytes[$03]*$100;
end;
end;
end.