-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatDiskC64_1541.cc
More file actions
337 lines (302 loc) · 8.31 KB
/
FormatDiskC64_1541.cc
File metadata and controls
337 lines (302 loc) · 8.31 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
///////////////////////////////////////////////////////////
//
// SuperDiskIndex
//
////////////////////
//
//
//
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "Global.h"
#include "Helpers.h"
#include "CRC.h"
#include "VirtualDisk.h"
#include "DiskMap.h"
#include "DiskLayout.h"
#include "FormatDiskC64_1541.h"
#include "CBM.h"
///////////////////////////////////////////////////////////
struct _1540_sysblk
{
u8 dir0_track;
u8 dir0_sect;
u8 format;
u8 unused;
u32 bam[35];
char label[18];
u8 id1;
u8 id2;
u8 space;
u8 dosversion;
u8 dosformat;
// rest of sector is unused
};
struct _1540_direntry
{
u8 next_track; // only valid for first entry
u8 next_sect; // only valid for first entry
u8 type;
u8 data_track;
u8 data_sect;
char filename[16];
u8 rel0;
u8 rel1;
u8 rel2;
u8 unused[4];
u8 ovr_track;
u8 ovr_sect;
u16 size_in_blocks; // little endian
};
struct _1540_dirblk
{
_1540_direntry entries[8];
};
///////////////////////////////////////////////////////////
FormatDiskC64_1541::FormatDiskC64_1541() : cur_c(-1),cur_h(-1),cur_s(-1),LayoutLocked(false)
{
InitLayout();
SectSize=256;
// set layout to expected 1541 format
//DLayout->SetLayout(35,1,0,true);
//int i=0;
//while (i<17) { DLayout->SetTrackLen(i++, 21); }
//while (i<24) { DLayout->SetTrackLen(i++, 19); }
//while (i<30) { DLayout->SetTrackLen(i++, 18); }
//while (i<35) { DLayout->SetTrackLen(i++, 17); }
}
FormatDiskC64_1541::~FormatDiskC64_1541()
{
}
char const *FormatDiskC64_1541::GetName()
{
return "C64/1541";
}
u8 FormatDiskC64_1541::GetSyncWordCount()
{
return 2;
}
u32 FormatDiskC64_1541::GetSyncWord(int n)
{
switch (n)
{
case 0: return 0xfffffd49;
case 1: return 0xfffffd57;
}
return 0;
}
u32 FormatDiskC64_1541::GetSyncBlockLen(int n)
{
switch (n)
{
case 0: return 10;
case 1: return 325;
}
return 0;
}
void FormatDiskC64_1541::PreTrackInit()
{
cur_c=cur_h=cur_s=-1;
}
void FormatDiskC64_1541::HandleBlock(Buffer *buffer, int currev)
{
buffer->GCRDecode(2,6);
u8 *db = buffer->GetBuffer();
// Sector Header
if (*db==0x8)
{
CRC8_xor crc(0);
crc.Block(db+1, 5, false);
clog(2,"# Sector Header + %02d/%02d + crc %02x (%s)\n", db[3], db[2], db[1], crc.Check()?"OK":"BAD");
if (crc.Check()) {
if (DLayout->FoundSector(db[3]-1,0,db[2]))
{
cur_c=db[3]-1;
cur_h=0;
cur_s=db[2];
}
}
}
// Sector Data
if (*db==0x7)
{
CRC8_xor crc(0);
crc.Block(db+1, 257, false);
clog(2,"# Sector Data + %04x (%s)\n", db[1+256], crc.Check()?"OK":"BAD");
if ((cur_c<0)||(cur_h<0)||(cur_s<0))
{
clog(2, "# WARN: Found sector data block without valid sector header!\n");
} else {
// process
if (Disk!=NULL)
{
Disk->AddSector(cur_c, cur_h, cur_s, currev, buffer->GetBuffer()+1, minval(buffer->GetFill()-1, 256), true, crc.Check());
}
cur_c=cur_h=cur_s=-1;
}
}
}
bool FormatDiskC64_1541::Analyze()
{
char sbuf[64];
if ((DLayout->GetCylinders()<31)||(DLayout->GetCylinders()>41)) return false; // only standard c64 floppy format is supported for now.
if (DLayout->GetHeads()!=1) return false; // only standard c64 floppy format is supported for now.
if (DLayout->GetSectors()!=21) return false; // only standard c64 floppy format is supported for now.
// boot block
_1540_sysblk *sys = (_1540_sysblk *)(Disk->GetSector(17,0,0));
clog(1, "# DISK: Format = %s\n",sys->format=='A'?"1540":"Unknown");
//if (sys->format!='A') return false; // only standard c64 floppy format is supported for now.
CBM::petscii2ascii(sbuf, sys->label, 18);
clog(1, "# DISK: Label = %s\n",sbuf);
clog(1, "# DISK: ID = $%02x $%02x (%c%c)\n",sys->id1,sys->id2,sys->id1,sys->id2);
clog(1, "# DISK: DOSVersion = %c%c\n",sys->dosversion,sys->dosformat);
// prep and init dmap
{
DMap = new DiskMap(DLayout->GetTotalSectors());
DMap->SetBitsSector(DLayout->CHStoBLK(17,0,0), DMF_ROOTBLOCK);
// set crc states
for (u32 i=0; i<DLayout->GetTotalSectors(); i++)
{
u16 c,h,s;
DLayout->BLKtoCHS(i, &c,&h,&s);
if (Disk->IsSectorCRCBad(c,h,s)) DMap->SetBitsSector(i, DMF_CRC_LOWLEVEL_BAD);
if (Disk->IsSectorMissing(c,h,s)) DMap->SetBitsSector(i, DMF_MISSING);
}
// scan/mirror blockmap
for (int i=0; i<35; i++)
{
u8 freesect = sys->bam[i]&0xff;
u32 mapbits = sys->bam[i]>>8;
// count set bits
u8 setbits = 0;
for (int j=0; j<24; j++) setbits+=((mapbits>>j)&1);
if (freesect!=setbits) clog(2, "# BAM: Track %d has %d blocks marked free, but reports %d free blocks.\n", i, setbits, freesect);
for (int j=0; j<DLayout->GetTrackLen(i); j++)
{
DMap->SetBitsSector(DLayout->CHStoBLK(i,0, j), (mapbits&(0x1<<j))>0?0:DMF_BLOCK_USED);
}
}
}
// dir block
u8 dir_trk = 17;//sys->dir0_track-1;
u8 dir_sect = 1;//sys->dir0_sect;
clog(1, "#############################################################\n");
// Listing
{
int fd=-1;
char *fnout=NULL;
if (Config.gen_listing)
{
gen_output_filename(&fnout, Config.fn_out, OT_LISTING, ".lst", OutputParams("c64", 0,0,0,0));
clog(1,"# Generating file listing '%s'.\n",fnout);
fd = open(fnout, O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE);
}
//if ((Config.gen_listing)&&(fd>=0))
{
memset(sbuf, 0, sizeof(sbuf));
CBM::petscii2ascii(sbuf, sys->label, 18);
cdprintf(Config.show_listing, Config.gen_listing, fd, "0 \"%-16s\" %c%c %c%c\n", sbuf, sys->id1, sys->id2, sys->dosversion, sys->dosformat);
}
while ((dir_trk!=0)&&(dir_trk!=0xff))
{
DMap->SetBitsSector(DLayout->CHStoBLK(dir_trk,0,dir_sect), DMF_DIRECTORY);
_1540_dirblk *dir = (_1540_dirblk *)(Disk->GetSector(dir_trk,0,dir_sect));
if (dir==NULL) break;
for (int i=0; i<8; i++)
{
if (dir->entries[i].type==0) continue;
clog(2,"$%d.%d", dir->entries[i].data_track, dir->entries[i].data_sect);
char const *fstatestr = "";
int fstate=0;
if (dir->entries[i].data_track>0)
{
fstate = ScanFile(dir->entries[i].data_track-1, dir->entries[i].data_sect, dir->entries[i].size_in_blocks);
}
//if ((Config.gen_listing)&&(fd>=0))
{
CBM::petscii2ascii(sbuf, dir->entries[i].filename, 16);
char const *etype="???";
switch (dir->entries[i].type)
{
case 0x80: etype="DEL"; break;
case 0x81: etype="SEQ"; break;
case 0x82: etype="PRG"; break;
case 0x83: etype="USR"; break;
case 0x84: etype="REL"; break;
case 0: etype="NUL"; break;
default: etype="???";
}
switch (fstate)
{
case -1: fstatestr = "<BADSIZE>"; break;
case -2: fstatestr = "<BADLNKS>"; break;
case -3: fstatestr = "<BADPTR>"; break;
case -4: fstatestr = "<BADCRC>"; break;
}
cdprintf(Config.show_listing, Config.gen_listing, fd, "%-4d \"%-16s\" %s %s\n", dir->entries[i].size_in_blocks, sbuf, etype, fstatestr);
}
}
dir_trk = dir->entries[0].next_track-1;
dir_sect = dir->entries[0].next_sect;
}
if ((Config.gen_listing)&&(fd>=0))
{
close(fd);
free(fnout);
}
}
// Output DiskMaps
{
//if (Config.gen_maps)
{
char *fnout=NULL;
gen_output_filename(&fnout, Config.fn_out, OT_MAPS, ".maps", OutputParams("c64", 0,0,0,0));
if (Config.gen_maps)
{
clog(1,"# Generating block/usage/healthmaps '%s'.\n",fnout);
}
if (DMap)
{
DMap->OutputMaps(fnout);
}
free(fnout);
}
}
// Export
if (Config.gen_export)
{
char *fnout=NULL;
gen_output_filename(&fnout, Config.fn_out, OT_DISKIMAGE, ".d64", OutputParams("c64", 0,0,0,0));
clog(1,"# Generating diskimage '%s'.\n",fnout);
if (Disk)
{
Disk->ExportD64(fnout);
}
free(fnout);
}
return true;
}
char const *FormatDiskC64_1541::GetDiskTypeString()
{
return "C=64";
}
char const *FormatDiskC64_1541::GetDiskSubTypeString()
{
return "DD/1540";
}
int FormatDiskC64_1541::ScanFile(u8 trk, u8 sect, int dir_blkcount, int cur_blkcount)
{
if (DMap->GetSector(DLayout->CHStoBLK(trk,0,sect))&DMF_DATA) {
clog(2, "# File chain link loop or cross link detected. skipping further travel.\n");
return -2;
}
DMap->SetBitsSector(DLayout->CHStoBLK(trk,0,sect),DMF_DATA);
u8 *sdata = (u8 *)Disk->GetSector(trk, 0, sect);
if (sdata==NULL) { clog(2, " <T/S Pointer beyond disk limits>\n"); return -3; }
clog(2, " -> $%d.%d", sdata[0], sdata[1]);
if (sdata[0]>0) return ScanFile(sdata[0]-1, sdata[1], dir_blkcount, cur_blkcount+1);
//else
clog(2, " (chain %d blocks, dir %d blocks)\n", cur_blkcount+1, dir_blkcount);
return ((cur_blkcount+1)==dir_blkcount)?0:-1;
}