forked from M3wP/D64Explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC64D64Image.pas
2197 lines (1851 loc) · 59.4 KB
/
C64D64Image.pas
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
//------------------------------------------------------------------------------
//C64D64Image
//===========
//Load, creates, manipulates and saves Commodore 1541, 1571 and 1581 (.D64,
//.D71, and .D81) disk image files.
//
//Please note:
//------------
//Presently, only FPC/Lazarus is supported. Delphi support is incomplete.
//Needs a class helper for TMemoryStream to include ReadByte (which I'm a fan
//of) or ReadByte changed to ReadBuffer.
//
//Copyright (C) 2016, Daniel England.
//All Rights Reserved. Released under the GPL.
//
//This program 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.
//
//This program 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
//this program. If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------
unit C64D64Image;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$H+}
interface
uses
Classes, SysUtils;
const
// Size of a sector on a disk
VAL_SIZ_D64SECTRSIZE = 256;
VAL_SIZ_D64BLOCKSIZE = 254;
LIT_LBL_D64BLNKFLN = #$A0#$A0#$A0#$A0#$A0#$A0#$A0#$A0 +
#$A0#$A0#$A0#$A0#$A0#$A0#$A0#$A0;
VAL_TYP_D64FTYPE_DEL = 0;
VAL_TYP_D64FTYPE_SEQ = 1;
VAL_TYP_D64FTYPE_PRG = 2;
VAL_TYP_D64FTYPE_USR = 3;
VAL_TYP_D64FTYPE_REL = 4;
VAL_TYP_D64FTYPE_CBM = 5;
type
// Supported disk types (d71 images may be single or double sided. d81 images
// currently do not support directory partitions).
TD64DiskType = (ddt1541, ddt1571, ddt1581);
// Range of track numbers on a disk
TD64TrackNum = 1..80;
// Range of sector numbers on a track
TD64SectorNum = 0..39;
// Range of entry numbers in a sector
TD64EntryNum = 0..7;
// Data for a file entry on the disk
TD64EntryData = array[0..31] of Byte;
TD64FileType = 0..7;
TD64FileState = (dfsReadOnly, dfsReplacing, dfsClosed);
TD64FileStates = set of TD64FileState;
{ TD64DirEntry }
// Helper record for a directory file entry
TD64DirEntry = record
Track: TD64TrackNum;
Sector: TD64SectorNum;
EntryNum: TD64EntryNum;
FileType: Byte;
DataTrack: TD64TrackNum;
DataSector: TD64SectorNum;
FileName: AnsiString;
FileSize: Word;
EntryData: TD64EntryData;
//dengland These routines are primarily for manipulating the EntryData.
// EntryNum must still be logicially handled (etc).
// procedure SetTrack(const AValue: Byte);
// procedure SetSector(const AValue: Byte);
procedure SetFileType(const AValue: Byte);
procedure SetDataTS(const ATrack, ASector: Byte);
procedure SetFileName(const AName: string);
procedure SetRelTS(const ATrack, ASector: Byte);
procedure SetRelRecSize(const AValue: Byte);
procedure SetFileSize(const AValue: Word);
procedure SetGEOSInfoTS(const ATrack, ASector: Byte); //SetRelTS
procedure SetGEOSStructure(const AValue: Byte); //SetRelRecSize
procedure SetGEOSFileType(const AValue: Byte);
procedure SetGEOSYear(const AYear: Word); //The actual year
procedure SetGEOSMonth(const AValue: Byte);
procedure SetGEOSDay(const AValue: Byte);
procedure SetGEOSHour(const AValue: Byte);
procedure SetGEOSMinute(const AValue: Byte);
procedure SetGEOSDateTime(const ADateTime: TDateTime);
end;
TD64DirEntries = array of TD64DirEntry;
// Important directory partition info
TD64DirPartitionInfo = record
Track: TD64TrackNum;
PartSize: Word;
end;
// Directory partition information helper record
TD64DirPartition = record
Info: TD64DirPartitionInfo;
PartFileName,
PartDiskName,
PartDiskID,
PartDOSType: AnsiString;
HasChildren: Boolean;
Depth: Integer;
Parent: Integer;
end;
TD64DirPartitions = array of TD64DirPartition;
// BAM info for a disk track
TD64TrackBAM = record
FreeSectors: Byte;
Bitmap: array of Byte;
end;
// BAM info for the disk
TD64DiskBAM = array of TD64TrackBAM;
type
{ TD64Image }
// Class for loading, creating and manipulating a .d64, .d71 or .d81 disk image
TD64Image = class(TObject)
private
FData: TMemoryStream;
FDiskType: TD64DiskType;
FSingleSide: Boolean;
FTrkCount: TD64TrackNum;
FMaxSectors: Byte;
FValidVersion: Boolean;
FDOSVersion: Byte;
FDiskName: AnsiString;
FDiskID: AnsiString;
FDOSType: AnsiString;
FGEOSDisk: Boolean;
FGEOSVerMajor: Byte;
FGEOSVerMinor: Byte;
FCurrPartTrk: Byte;
FCurrPartSiz: Word;
procedure DoLoadFromFile(const AFile: string);
procedure DoSaveToFile(const AFile: string);
function DoValidateTrackSector(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum): Word;
procedure DoGetSector(const ANum: Word; ADest: TStream);
procedure DoGetFileEntries(const AStream: TMemoryStream;
const ATrack: TD64TrackNum; const ASector: TD64SectorNum;
var AEntries: TD64DirEntries; const AFullList: Boolean = False);
function DoGet1581BAM(const AStream: TMemoryStream;
const ATrack: TD64TrackNum; const ATrkCnt: Byte;
var ABAM: TD64DiskBAM): Integer;
procedure DoWriteTrackSectorNextPointer(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum; const ANxtTrack: Byte;
const ANxtSector: Byte; out AData: PByte);
procedure DoAnalyseDOSDetails;
procedure DoAllocateNewSector(var ABAM: TD64DiskBAM;
out ATrack: TD64TrackNum; out ASector: TD64SectorNum;
const AAllocate: Boolean = True);
procedure DoDeallocateSectorChain(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum; var ABAM: TD64DiskBAM);
procedure DoWriteSector(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum; const ANxtTrack: Byte;
const ANxtSector: Byte; const AStream: TStream);
procedure DoAllocateDirEntry(const AEntryData: TD64EntryData);
procedure DoWriteDiskBAM(const ABAM: TD64DiskBAM);
public
constructor Create; overload;
//dengland Using this constructor when the file is share locked causes the
// LCL runtime to abort and the application to abnormally terminate. :(
constructor Create(const AFile: string); overload;
destructor Destroy; override;
// Load an image from a file. The DiskType will be detected by the filename.
procedure LoadFromFile(const AFile: string);
// Save the current image to a file.
procedure SaveToFile(const AFile: string);
// Format a new disk image. The disk name, id and type must be specified.
// Optionally, you can create a GEOS compatible disk image.
procedure FormatImage(const ADiskName: AnsiString; const ADiskID: AnsiString;
const ADiskType: TD64DiskType; const ASingleSide: Boolean = False;
const AGEOSDisk: Boolean = False);
// Return whether or not an entry refers to a partition and its data position
function IsDirectoryPartition(const AEntry: TD64DirEntry;
out ADataPos: Cardinal): Boolean; inline;
// Return the number of sectors on the given track.
function GetSectorsForTrack(const ATrack: TD64TrackNum): Byte; inline;
// Get the whole of the sector data for a given track and sector.
procedure GetRawSector(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum; ADest: TStream);
// Read the data for a chain of tracks and sectors, starting at the given
// track and sector. The track and sector link data is not returned
// but the whole data of the last sector is. The returned actual size
// should be checked for manipulating the returned file data.
procedure GetDataChain(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum; ADest: TStream;
out ActualSize: Cardinal);
// Get the file entries on the disk directory.
procedure GetFileEntries(var AEntries: TD64DirEntries;
const AFullList: Boolean = False);
// Get the BAM information for the disk.
function GetDiskBAM(var ADiskBAM: TD64DiskBAM): Integer;
// Get the total number of blocks on the disk
function GetDiskBlockCount: Integer;
// Allocate disk sectors for the data in the given stream. Allocates as
// many sectors as required to store the whole of the stream from its
// current position. The start track and sector is returned as well
// as the total number of sectors used (blocks).
procedure AllocateDiskSectors(const AStream: TStream;
out AStartTrk: TD64TrackNum;
out AStartSec: TD64SectorNum; out ABlocksUsed: Word);
// Allocates a new file entry for the given file entry data. Does not
// attempt to replace existing files with the same name. Will allocate
// new director sectors as required.
procedure AllocateFileEntry(const AEntryData: TD64EntryData);
// Deletes the given file from the disk, including freeing all of the
// sectors used by the file in the BAM. Automatically detects GEOS
// files and handles them appropriately.
procedure ScratchFileEntry(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum; const AEntry: TD64EntryNum);
// Replace a given file entry with given data
procedure ReplaceFileEntry(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum; const AEntry: TD64EntryNum;
const AEntryData: TD64EntryData);
// Get a array of directory compatible partitions.
procedure GetDirPartitions(var AParts: TD64DirPartitions);
// Set the current directory partition. Use GetDirPartitions to get the
// required information. Returns the previous partition info.
procedure SetCurrentPartition(const AInfo: TD64DirPartitionInfo); overload;
// Set the current directory partition. Use GetDirPartitions to get the
// required information.
procedure SetCurrentPartition(const AInfo: TD64DirPartitionInfo;
out APrev: TD64DirPartitionInfo); overload;
// Get the file entries for the current directory partition.
procedure GetPartitionFiles(var AEntries: TD64DirEntries);
// Get the BAM information for the current directory partition.
procedure GetPartitionBAM(var ADiskBAM: TD64DiskBAM);
// The current disk type
property DiskType: TD64DiskType read FDiskType;
// If a d71 image, whether the image is a single or double sided one.
property SingleSide: Boolean read FSingleSide;
// The total number of tracks in the current image.
property TrackCount: TD64TrackNum read FTrkCount;
// The maximum number of sectors on a track.
property MaxSectors: Byte read FMaxSectors;
// Whether the disk has valid DOS information. Uses only very minimal checks.
property ValidVersion: Boolean read FValidVersion;
// The disk DOS version.
property DOSVersion: Byte read FDOSVersion;
// The disk name.
property DiskName: AnsiString read FDiskName;
// The disk ID.
property DiskID: AnsiString read FDiskID;
// The DOS type.
property DOSType: AnsiString read FDOSType;
// Whether the disk is a GEOS disk.
property GEOSDisk: Boolean read FGEOSDisk;
// The major version of the GEOS format if a GEOS disk.
property GEOSVerMajor: Byte read FGEOSVerMajor;
// The minor version of the GEOS format if a GEOS disk.
property GEOSVerMinor: Byte read FGEOSVerMinor;
end;
// Base class for D64Image exceptions (for filtering)
D64ImageException = class(Exception);
// An exception raised when the disk file does not exist.
ED64FileDoesNotExist = class(D64ImageException);
// An exception raised when the disk file format cannot be determined.
ED64UnknownFormat = class(D64ImageException);
// An exception raised when an invalid sector is selected.
ED64InvalidSector = class(D64ImageException);
// An exception raised when the disk file cannot be opened.
ED64UnableToOpen = class(D64ImageException);
// An exception raised when the disk is full.
ED64DiskFull = class(D64ImageException);
//Convert the given DOS file type byte to a AnsiString (as per BASIC, with or
// without splat and read-only markers)
function D64FileTypeToStr(const AType: Byte;
const AStateFlags: Boolean = True): AnsiString;
//Convert the given GEOS file structure byte into a AnsiString.
function D64GEOSStructToStr(const AStruct: Byte): AnsiString;
//Convert the given GEOS file type into a AnsiString.
function D64GEOSFileTypeToStr(const AType: Byte): AnsiString;
procedure D64DecodeFileType(const AType: Byte; out AFileType: TD64FileType;
out AFileStates: TD64FileStates);
function D64EncodeFileType(const AFileType: TD64FileType;
const AFileStates: TD64FileStates): Byte;
implementation
uses
C64D64ImageStrs;
const
ARR_VAL_D64TRKSECTRS: array[1..40] of Byte = (
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, //1-17
19, 19, 19, 19, 19, 19, 19, //18-24
18, 18, 18, 18, 18, 18, //25-30
17, 17, 17, 17, 17, 17, 17, 17, 17, 17); //31-40
ARR_VAL_D71TRKSECTRS: array[1..70] of Byte = (
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, //1-17
19, 19, 19, 19, 19, 19, 19, //18-24
18, 18, 18, 18, 18, 18, //25-30
17, 17, 17, 17, 17, //31-35
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, //36-52
19, 19, 19, 19, 19, 19, 19, //53-59
18, 18, 18, 18, 18, 18, //60-65
17, 17, 17, 17, 17); //66-70
LIT_TOK_D64GEOSFMT = 'GEOS format';
LIT_TOK_D64FTYPDEL = 'DEL';
LIT_TOK_D64FTYPSEQ = 'SEQ';
LIT_TOK_D64FTYPPRG = 'PRG';
LIT_TOK_D64FTYPUSR = 'USR';
LIT_TOK_D64FTYPREL = 'REL';
LIT_TOK_D64FTYPCBM = 'CBM';
LIT_TOK_D64FTYPUNK = '???';
LIT_TOK_D64GEOSSEQ = 'Sequential';
LIT_TOK_D64GEOSVLR = 'GEOS VLIR';
LIT_TOK_D64GEOSUNK = 'Unknown';
LIT_TOK_D64GEOSNGS = 'Not a GEOS file';
LIT_TOK_D64GEOSBAS = 'BASIC';
LIT_TOK_D64GEOSASM = 'Assembler';
LIT_TOK_D64GEOSDAT = 'Data file';
LIT_TOK_D64GEOSSYS = 'System File';
LIT_TOK_D64GEOSDSA = 'Desk Accessory';
LIT_TOK_D64GEOSAPP = 'Application';
LIT_TOK_D64GEOSAPD = 'Application Data';
LIT_TOK_D64GEOSFNT = 'Font File';
LIT_TOK_D64GEOSPRN = 'Printer Driver';
LIT_TOK_D64GEOSINP = 'Input Driver';
LIT_TOK_D64GEOSDSK = 'Disk Driver/Device';
LIT_TOK_D64GEOSSBT = 'System Boot File';
LIT_TOK_D64GEOSTMP = 'Temporary';
LIT_TOK_D64GEOSAEX = 'Auto-Execute File';
procedure D64DecodeFileType(const AType: Byte; out AFileType: TD64FileType;
out AFileStates: TD64FileStates);
begin
AFileType:= AType and $07;
AFileStates:= [];
if (AType and $20) <> 0 then
Include(AFileStates, dfsReplacing);
if (AType and $40) <> 0 then
Include(AFileStates, dfsReadOnly);
if (AType and $80) <> 0 then
Include(AFileStates, dfsClosed);
end;
function D64EncodeFileType(const AFileType: TD64FileType;
const AFileStates: TD64FileStates): Byte;
begin
Result:= AFileType;
if dfsReplacing in AFileStates then
Result:= Result or $20;
if dfsReadOnly in AFileStates then
Result:= Result or $40;
if dfsClosed in AFileStates then
Result:= Result or $80;
end;
function D64FileTypeToStr(const AType: Byte; const AStateFlags: Boolean): AnsiString;
begin
case AType and $0F of
0:
Result:= LIT_TOK_D64FTYPDEL;
1:
Result:= LIT_TOK_D64FTYPSEQ;
2:
Result:= LIT_TOK_D64FTYPPRG;
3:
Result:= LIT_TOK_D64FTYPUSR;
4:
Result:= LIT_TOK_D64FTYPREL;
5:
Result:= LIT_TOK_D64FTYPCBM;
else
Result:= LIT_TOK_D64FTYPUNK;
end;
if AStateFlags then
begin
if (AType and $40) <> 0 then
Result:= Result + '<'
else
Result:= Result + ' ';
if (AType and $80) = 0 then
Result:= '*' + Result
else
Result:= ' ' + Result;
end;
end;
function D64GEOSStructToStr(const AStruct: Byte): AnsiString;
begin
case AStruct of
$00:
Result:= LIT_TOK_D64GEOSSEQ;
$01:
Result:= LIT_TOK_D64GEOSVLR;
else
Result:= LIT_TOK_D64GEOSUNK + Format(' ($%2.2x)', [AStruct]);
end;
end;
function D64GEOSFileTypeToStr(const AType: Byte): AnsiString;
begin
case AType of
$00:
Result:= LIT_TOK_D64GEOSNGS;
$01:
Result:= LIT_TOK_D64GEOSBAS;
$02:
Result:= LIT_TOK_D64GEOSASM;
$03:
Result:= LIT_TOK_D64GEOSDAT;
$04:
Result:= LIT_TOK_D64GEOSSYS;
$05:
Result:= LIT_TOK_D64GEOSDSA;
$06:
Result:= LIT_TOK_D64GEOSAPP;
$07:
Result:= LIT_TOK_D64GEOSAPD;
$08:
Result:= LIT_TOK_D64GEOSFNT;
$09:
Result:= LIT_TOK_D64GEOSPRN;
$0A:
Result:= LIT_TOK_D64GEOSINP;
$0B:
Result:= LIT_TOK_D64GEOSDSK;
$0C:
Result:= LIT_TOK_D64GEOSSBT;
$0D:
Result:= LIT_TOK_D64GEOSTMP;
$0E:
Result:= LIT_TOK_D64GEOSAEX;
else
Result:= LIT_TOK_D64GEOSUNK + Format(' ($%2.2x)', [AType]);
end;
end;
{ TD64DirEntry }
//procedure TD64DirEntry.SetTrack(const AValue: Byte);
// begin
// Track:= TD64TrackNum(AValue);
// EntryData[0]:= AValue;
// end;
//procedure TD64DirEntry.SetSector(const AValue: Byte);
// begin
// Sector:= AValue;
// EntryData[1]:= AValue
// end;
procedure TD64DirEntry.SetFileType(const AValue: Byte);
begin
FileType:= AValue;
EntryData[2]:= AValue;
end;
procedure TD64DirEntry.SetDataTS(const ATrack, ASector: Byte);
begin
EntryData[3]:= ATrack;
EntryData[4]:= ASector;
end;
procedure TD64DirEntry.SetFileName(const AName: AnsiString);
var
i: Integer;
begin
FileName:= Copy(AName + LIT_LBL_D64BLNKFLN, 1, 16);
for i:= $05 to $14 do
EntryData[i]:= Byte(AnsiChar(FileName[i - $04]));
end;
procedure TD64DirEntry.SetRelTS(const ATrack, ASector: Byte);
begin
EntryData[$15]:= ATrack;
EntryData[$16]:= ASector;
end;
procedure TD64DirEntry.SetRelRecSize(const AValue: Byte);
begin
EntryData[$17]:= AValue;
end;
procedure TD64DirEntry.SetFileSize(const AValue: Word);
begin
EntryData[$1E]:= Byte(AValue and $FF);
EntryData[$1F]:= Byte((AValue and $FF00) shr 8);
end;
procedure TD64DirEntry.SetGEOSInfoTS(const ATrack, ASector: Byte);
begin
SetRelTS(ATrack, ASector);
end;
procedure TD64DirEntry.SetGEOSStructure(const AValue: Byte);
begin
SetRelRecSize(AValue);
end;
procedure TD64DirEntry.SetGEOSFileType(const AValue: Byte);
begin
EntryData[$18]:= AValue;
end;
procedure TD64DirEntry.SetGEOSYear(const AYear: Word);
begin
EntryData[$19]:= Byte(AYear - 1900);
end;
procedure TD64DirEntry.SetGEOSMonth(const AValue: Byte);
begin
EntryData[$1A]:= AValue;
end;
procedure TD64DirEntry.SetGEOSDay(const AValue: Byte);
begin
EntryData[$1B]:= AValue;
end;
procedure TD64DirEntry.SetGEOSHour(const AValue: Byte);
begin
EntryData[$1C]:= AValue;
end;
procedure TD64DirEntry.SetGEOSMinute(const AValue: Byte);
begin
EntryData[$1D]:= AValue;
end;
procedure TD64DirEntry.SetGEOSDateTime(const ADateTime: TDateTime);
var
yr,
mo,
dy,
hr,
mn,
sc,
ms: Word;
begin
DecodeDate(ADateTime, yr, mo, dy);
DecodeTime(ADateTime, hr, mn, sc, ms);
SetGEOSYear(yr);
SetGEOSMonth(mo);
SetGEOSDay(dy);
SetGEOSHour(hr);
SetGEOSMinute(mn);
end;
{ TD64Image }
procedure TD64Image.DoLoadFromFile(const AFile: string);
var
f: TFileStream;
begin
if FileExists(AFile) then
try
f:= TFileStream.Create(AFile, fmShareCompat or fmOpenRead);
try
if CompareText(ExtractFileExt(AFile), '.D64') = 0 then
begin
// 35 track, no errors 174848
// 35 track, 683 error bytes 175531
// 40 track, no errors 196608
// 40 track, 768 error bytes 197376
if (f.Size = 174848)
or (f.Size = 175531) then
FTrkCount:= 35
else if (f.Size = 196608)
or (f.Size = 197376) then
FTrkCount:= 40
else
raise ED64UnknownFormat.Create(
Format(STR_FMT_D64FORMATUNK, [AFile]));
FDiskType:= ddt1541;
FSingleSide:= True;
end
else if CompareText(ExtractFileExt(AFile), '.D71') = 0 then
begin
// 35 track, no errors 174848
// 35 track, 683 error bytes 175531
// 70 tracks, 349696 bytes.
// 70 tracks, 351062, with error bytes.
if (f.Size = 174848)
or (f.Size = 175531) then
begin
FTrkCount:= 35;
FSingleSide:= True;
end
else if (f.Size = 349696)
or (f.Size = 351062) then
begin
FTrkCount:= 70;
FSingleSide:= False;
end
else
raise ED64UnknownFormat.Create(
Format(STR_FMT_D64FORMATUNK, [AFile]));
FDiskType:= ddt1571;
end
else if CompareText(ExtractFileExt(AFile), '.D81') = 0 then
begin
// 819200 bytes,
// 822400 bytes with errors.
if (f.Size = 819200)
or (f.Size = 822400) then
begin
FTrkCount:= 80;
FSingleSide:= False;
end
else
raise ED64UnknownFormat.Create(
Format(STR_FMT_D64FORMATUNK, [AFile]));
FDiskType:= ddt1581;
end
else
raise ED64UnknownFormat.Create(Format(STR_FMT_D64EXTNSNUNK,
[AFile]));
FData.Clear;
FData.CopyFrom(f, f.Size);
finally
f.Free;
end
except
raise ED64UnableToOpen.Create(Format(STR_FMT_D64FILENOTOP, [AFile]));
end
else
raise ED64FileDoesNotExist.Create(Format(STR_FMT_D64FILENOTEX, [AFile]));
if FDiskType in [ddt1541, ddt1571] then
FMaxSectors:= 21
else
FMaxSectors:= 40;
DoAnalyseDOSDetails;
end;
procedure TD64Image.DoSaveToFile(const AFile: string);
begin
//dengland Should do some sanity checking on file extension and DiskType
//fixme dengland Sanity check DoSaveToFile
FData.Position:= 0;
FData.SaveToFile(AFile);
end;
function TD64Image.DoValidateTrackSector(const ATrack: TD64TrackNum;
const ASector: TD64SectorNum): Word;
var
i: TD64TrackNum;
begin
if FTrkCount >= ATrack then
if (GetSectorsForTrack(ATrack) - 1) >= ASector then
begin
Result:= 0;
i:= 1;
while i < ATrack do
begin
Inc(Result, GetSectorsForTrack(i));
Inc(i);
end;
Inc(Result, Ord(ASector));
end
else
raise ED64InvalidSector.Create(
Format(STR_FMT_D64SECOUTRNG, [Ord(ATrack), Ord(ASector)]))
else
raise ED64InvalidSector.Create(
Format(STR_FMT_D64TRKOUTRNG, [Ord(ATrack)]));
end;
procedure TD64Image.DoGetSector(const ANum: Word; ADest: TStream);
begin
FData.Position:= ANum * VAL_SIZ_D64SECTRSIZE;
ADest.CopyFrom(FData, VAL_SIZ_D64SECTRSIZE);
end;
procedure TD64Image.DoGetFileEntries(const AStream: TMemoryStream;
const ATrack: TD64TrackNum; const ASector: TD64SectorNum;
var AEntries: TD64DirEntries; const AFullList: Boolean);
var
b,
nt,
ns: Byte;
fs: Word;
fn: AnsiString;
x,
i: Integer;
e: Byte;
fe: TD64DirEntry;
z: Integer;
begin
nt:= ATrack;
ns:= ASector;
x:= 0;
repeat
// SetLength(AEntries, Length(AEntries) + 8);
AStream.Clear;
GetRawSector(nt, ns, AStream);
AStream.Position:= 0;
for e:= 0 to 7 do
begin
fe.Track:= nt;
fe.Sector:= ns;
fe.EntryNum:= e;
AStream.Position:= e * $20;
for i:= 0 to $1F do
fe.EntryData[i]:= AStream.ReadByte;
AStream.Position:= e * $20 + 2;
fe.FileType:= AStream.ReadByte;
b:= AStream.ReadByte;
if (not AFullList)
and (b = 0) then
Continue;
fe.DataTrack:= b;
fe.DataSector:= AStream.ReadByte;
// AStream.Position:= AStream.Position + 2;
fn:= '';
for i:= 0 to 15 do
begin
b:= AStream.ReadByte;
// if b in [$20..$7E] then
fn:= fn + AnsiChar(b);
// else
// fn:= fn + ' ';
end;
fe.FileName:= fn;
AStream.Position:= AStream.Position + 9;
b:= AStream.ReadByte;
fs:= b;
b:= AStream.ReadByte;
fs:= fs + b shl 8;
fe.FileSize:= fs;
z:= SizeOf(TD64DirEntry);
SetLength(AEntries, Length(AEntries) + 1);
AEntries[x]:= fe;
Inc(x);
end;
AStream.Position:= 0;
nt:= AStream.ReadByte;
ns:= AStream.ReadByte;
until nt = 0;
end;
function TD64Image.DoGet1581BAM(const AStream: TMemoryStream;
const ATrack: TD64TrackNum; const ATrkCnt: Byte;
var ABAM: TD64DiskBAM): Integer;
var
x,
s,
o,
i,
j: Integer;
begin
Result:= 0;
x:= 0;
for s:= 0 to 1 do
begin
o:= s * 40;
AStream.Clear;
GetRawSector(ATrack, s + 1, AStream);
AStream.Position:= $10;
for i:= 0 to 39 do
begin
ABAM[o + i].FreeSectors:= AStream.ReadByte;
Inc(Result, ABAM[o + i].FreeSectors);
for j:= 0 to 4 do
ABAM[o + i].Bitmap[j]:= AStream.ReadByte;
Inc(x);
//dengland This is just as nasty as a darn goto.
if x = ATrkCnt then
Exit;
end;
end;
end;
procedure TD64Image.DoAnalyseDOSDetails;
var
p: Cardinal;
b: Byte;
i: Byte;
s: AnsiString;
begin
if FDiskType in [ddt1541, ddt1571] then
begin
FCurrPartTrk:= 0;
FCurrPartSiz:= 0;
end
else
begin
FCurrPartTrk:= 40;
FCurrPartSiz:= 3200;
end;
FDiskName:= EmptyStr;
FDiskID:= EmptyStr;
FDOSType:= EmptyStr;
if FDiskType in [ddt1541, ddt1571] then
p:= DoValidateTrackSector(18, 0) * VAL_SIZ_D64SECTRSIZE
else
p:= DoValidateTrackSector(40, 0) * VAL_SIZ_D64SECTRSIZE;
FData.Position:= p + 2;
FDOSVersion:= FData.ReadByte;
FValidVersion:=
((FDiskType in [ddt1541, ddt1571]) and (FDOSVersion = $41)) or
((FDiskType = ddt1581) and (FDOSVersion = $44));
if FDiskType = ddt1571 then
begin
FData.Position:= p + 3;
b:= FData.ReadByte;
if ((not FSingleSide)
and (b = $00))
or (FSingleSide
and (b = $80)) then
FValidVersion:= False;
end;
if FValidVersion then
begin
if FDiskType = ddt1581 then
FData.Position:= p + $04
else
FData.Position:= p + $90;
for i:= 0 to 15 do
begin
b:= FData.ReadByte;
// if b in [$20..$7E] then
FDiskName:= FDiskName + AnsiChar(b);
// else
// FDiskName:= FDiskName + ' ';
end;
if FDiskType = ddt1581 then
FData.Position:= p + $16
else
FData.Position:= p + $A2;
for i:= 0 to 1 do
begin
b:= FData.ReadByte;
// if b in [$20..$7E] then
FDiskID:= FDiskID + AnsiChar(b);
// else
// FDiskID:= FDiskID + ' ';
end;
if FDiskType = ddt1581 then
FData.Position:= p + $19
else
FData.Position:= p + $A5;
for i:= 0 to 1 do
begin
b:= FData.ReadByte;
// if b in [$20..$7E] then
FDOSType:= FDOSType + AnsiChar(b);
// else
// FDOSType:= FDOSType + ' ';
end;
FData.Position:= p + $AD;
s:= '';
for i:= 0 to 10 do
begin
b:= FData.ReadByte;
// if b in [$20..$7E] then
s:= s + AnsiChar(b);
// else
// s:= s + ' ';
end;
FGEOSDisk:= CompareStr(s, LIT_TOK_D64GEOSFMT) = 0;
if FGEOSDisk then
begin
FData.ReadByte;
FData.ReadByte;
FGEOSVerMajor:= StrToInt(AnsiString(AnsiChar(FData.ReadByte)));
FData.ReadByte;
FGEOSVerMinor:= StrToInt(AnsiString(AnsiChar(FData.ReadByte)));
end;
end;
end;
procedure TD64Image.DoAllocateNewSector(var ABAM: TD64DiskBAM;
out ATrack: TD64TrackNum; out ASector: TD64SectorNum;
const AAllocate: Boolean);
var
i,
j: Integer;
d: Byte;
begin
//dengland Not going to try to do disk type interleave at this point.
for i:= 0 to FTrkCount - 1 do
begin
//dengland Don't allocate in directory tracks
if (FDiskType in [ddt1541, ddt1571])
and (i = 17) then
Continue
else if (FDiskType = ddt1581)
and (i = 39) then
Continue;
if ABAM[i].FreeSectors > 0 then
begin
ATrack:= TD64TrackNum(i + 1);
if AAllocate then
Dec(ABAM[i].FreeSectors);