forked from markusC64/nibtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnibread.c
523 lines (447 loc) · 12.3 KB
/
nibread.c
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
/*
NIBREAD - part of the NIBTOOLS package for 1541/1571 disk image nibbling
by Peter Rittwage <peter(at)rittwage(dot)com>
based on code from MNIB, by Dr. Markus Brenner
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <ctype.h>
#include "mnibarch.h"
#include "gcr.h"
#include "nibtools.h"
#include "lz.h"
int _dowildcard = 1;
char bitrate_range[4] = { 43 * 2, 31 * 2, 25 * 2, 18 * 2 };
char bitrate_value[4] = { 0x00, 0x20, 0x40, 0x60 };
char density_branch[4] = { 0xb1, 0xb5, 0xb7, 0xb9 };
BYTE compressed_buffer[(MAX_HALFTRACKS_1541 + 2) * NIB_TRACK_LENGTH];
BYTE file_buffer[(MAX_HALFTRACKS_1541 + 2) * NIB_TRACK_LENGTH];
BYTE track_buffer[(MAX_HALFTRACKS_1541 + 2) * NIB_TRACK_LENGTH];
BYTE track_density[MAX_HALFTRACKS_1541 + 2];
BYTE track_alignment[MAX_HALFTRACKS_1541 + 2];
size_t track_length[MAX_HALFTRACKS_1541 + 2];
size_t error_retries;
int file_buffer_size;
int reduce_sync, reduce_badgcr, reduce_gap;
int fix_gcr;
int start_track, end_track, track_inc;
int read_killer;
int align;
int drivetype;
int imagetype;
int mode;
int force_density;
int track_match;
int gap_match_length;
int cap_min_ignore;
int interactive_mode;
int verbose;
int extended_parallel_test;
int force_nosync;
int ihs;
int auto_capacity_adjust;
int align_disk;
int skew;
int rawmode;
int rpm_real;
int unformat_passes;
int capacity_margin;
int align_delay;
int align_report;
int increase_sync = 0;
int presync = 0;
BYTE fillbyte = 0x55;
BYTE drive = 8;
char * cbm_adapter = "";
int use_floppycode_srq = 0;
int override_srq = 0;
int extra_capacity_margin=5;
int sync_align_buffer=0;
int fattrack=0;
int old_g64=0;
BYTE density_map;
float motor_speed;
CBM_FILE fd;
FILE *fplog;
int ARCH_MAINDECL
main(int argc, char *argv[])
{
int bump, reset, i;
char filename[256], logfilename[256], *dotpos;
char argcache[256];
FILE *fp;
fprintf(stdout,
"\nnibread - Commodore 1541/1571 disk image nibbler\n"
AUTHOR VERSION "\n\n");
/* we can do nothing with no switches */
if (argc < 2)
usage();
/* clear heap buffers */
memset(compressed_buffer, 0x00, sizeof(compressed_buffer));
memset(file_buffer, 0x00, sizeof(file_buffer));
memset(track_buffer, 0x00, sizeof(track_buffer));
#ifdef DJGPP
fd = 1;
#endif
bump = 1; /* failing to bump sometimes give us wrong tracks on heavily protected disks */
reset = 1;
start_track = 1 * 2;
end_track = 41 * 2;
track_inc = 2;
reduce_sync = 4;
reduce_badgcr = 0;
reduce_gap = 0;
fix_gcr = 0;
read_killer = 1;
error_retries = 10;
force_density = 0;
track_match = 0;
interactive_mode = 0;
verbose = 1;
extended_parallel_test = 0;
force_nosync = 0;
align = ALIGN_NONE;
gap_match_length = 7;
cap_min_ignore = 0;
ihs = 0;
mode = MODE_READ_DISK;
align_report = 0;
// cache our arguments for logfile generation
strcpy(argcache, "");
for (i = 0; i < argc; i++)
{
strcat(argcache, argv[i]);
strcat(argcache," ");
}
// parse arguments
while (--argc && (*(++argv)[0] == '-'))
{
switch ((*argv)[1])
{
case '@':
cbm_adapter = &(*argv)[2];
printf("* Using OpenCBM adapter %s\n", cbm_adapter);
break;
case 'P':
printf("* Skip 1571 SRQ Support (Use parallel)\n");
override_srq = 1;
break;
case 's':
break;
case 'j':
printf("* 1541/1571 Index Hole Sensor (SC+ compatible)\n");
Use_SCPlus_IHS = 1;
use_floppycode_ihs = 1; // ihs floppy code!
break;
case 'x':
printf("* Track Alignment Report (1541/1571 SC+ compatible IHS)\n");
track_align_report = 1;
use_floppycode_ihs = 1; // ihs floppy code!
break;
case 'y':
printf("* Deep Bitrate Scan (1541/1571 SC+ compatible IHS)\n");
Deep_Bitrate_SCPlus_IHS = 1;
use_floppycode_ihs = 1; // ihs floppy code!
break;
case 'z':
printf("* Testing 1541/1571 Index Hole Sensor (SC+ compatible)\n");
Test_SCPlus_IHS = 1;
bump = 0; // Don't bump for simple IHS check
use_floppycode_ihs = 1; // ihs floppy code!
break;
case 'A':
align_report = 1;
printf("* Track Alignment Report\n");
break;
case 'h':
track_inc = 1;
end_track = 83;
printf("* Using halftracks\n");
break;
case 'i': // this is not implemented in SRQ mode //
printf("* 1571 or SuperCard-compatible index hole sensor (use only for side 1)\n");
ihs = 1;
break;
case 'V':
track_match = 1;
printf("* Enable track match (low-level verify)\n");
break;
case 'n':
force_nosync = 0;
printf("* Allowing track reads to wait for sync\n");
break;
case 't':
extended_parallel_test = atoi(&(*argv)[2]);
if(!extended_parallel_test)
extended_parallel_test = 100;
printf("* Extended port test loops = %d\n", extended_parallel_test);
break;
case 'I':
interactive_mode = 1;
printf("* Interactive mode\n");
break;
case 'd':
force_density = 1;
printf("* Forcing default density\n");
break;
case 'k':
read_killer = 0;
printf("* Disabling read of 'killer' tracks\n");
break;
case 'S':
if (!(*argv)[2]) usage();
start_track = (BYTE) (2*(atoi(&(*argv)[2])));
printf("* Start track set to %d\n", start_track/2);
break;
case 'E':
if (!(*argv)[2]) usage();
end_track = (BYTE) (2*(atoi(&(*argv)[2])));
printf("* End track set to %d\n", end_track/2);
break;
case 'D':
if (!(*argv)[2]) usage();
drive = (BYTE) (atoi(&(*argv)[2]));
printf("* Use Device %d\n", drive);
break;
case 'G':
if (!(*argv)[2]) usage();
gap_match_length = atoi(&(*argv)[2]);
printf("* Gap match length set to %d\n", gap_match_length);
break;
case 'v':
verbose++;
printf("* Verbose mode on\n");
break;
case 'e': // change read retries
if (!(*argv)[2]) usage();
error_retries = atoi(&(*argv)[2]);
printf("* Read retries set to %d\n", error_retries);
break;
case 'm':
printf("* Minimum capacity ignore on\n");
cap_min_ignore = 1;
break;
default:
usage();
break;
}
}
printf("\n");
if(argc < 1) usage();
strcpy(filename, argv[0]);
if( (fp=fopen(filename,"r")) )
{
fclose(fp);
printf("File exists - Overwrite? (y/N)");
if(getchar() != 'y') exit(0);
}
#ifdef DJGPP
calibrate();
if (!detect_ports(reset))
return 0;
#elif defined(OPENCBM_42)
/* remain compatible with OpenCBM < 0.4.99 */
if (cbm_driver_open(&fd, 0) != 0)
{
printf("Is your X-cable properly configured?\n");
exit(0);
}
#else /* assume > 0.4.99 */
if (cbm_driver_open_ex(&fd, cbm_adapter) != 0)
{
printf("Is your X-cable properly configured?\n");
exit(0);
}
#endif
/* Once the drive is accessed, we need to close out state when exiting */
atexit(handle_exit);
signal(SIGINT, handle_signals);
if(!init_floppy(fd, drive, bump))
{
printf("Floppy drive initialization failed\n");
exit(0);
}
if(extended_parallel_test)
parallel_test(extended_parallel_test);
if (Test_SCPlus_IHS) // "-z"
{
IHSresult = Check_SCPlus_IHS(fd,0); // 0=TurnIHSoffAfterwards
OutputIHSResult(TRUE,FALSE,IHSresult,NULL);
exit(0);
}
if(align_report)
TrackAlignmentReport(fd);
/* create log file */
strcpy(logfilename, filename);
dotpos = strrchr(logfilename, '.');
if (dotpos != NULL)
*dotpos = '\0';
strcat(logfilename, ".log");
if ((fplog = fopen(logfilename, "wb")) == NULL)
{
fprintf(stderr, "Couldn't create log file %s!\n", logfilename);
exit(2);
}
fprintf(fplog, "%s\n", VERSION);
fprintf(fplog, "'%s'\n", argcache);
if(strrchr(filename, '.') == NULL) strcat(filename, ".nbz");
if((compare_extension(filename, "D64")) || (compare_extension(filename, "G64")))
{
printf("\nDisk imaging only directly supports NIB, NB2, and NBZ formats.\n");
printf("Use nibconv after imaging to convert to desired file type.\n");
exit(0);
}
if (Use_SCPlus_IHS) // "-j"
{
IHSresult = Check_SCPlus_IHS(fd,1); // 1=KeepOn
OutputIHSResult(TRUE,TRUE,IHSresult,fplog);
if (IHSresult != 0)
{
// turn SCPlus IHS off
send_mnib_cmd(fd, FL_IHS_OFF, NULL, 0);
burst_read(fd);
if (fplog) fclose(fplog);
exit(0);
}
}
if (Deep_Bitrate_SCPlus_IHS) // "-y"
{
// We need some memory for the Deep Bitrate Scan
if(!(logline = malloc(0x10000)))
{
printf("Error: Could not allocate memory for Deep Bitrate Scan buffer.\n");
exit(0);
}
DeepBitrateAnalysis(fd,filename,track_buffer,logline);
}
else if (track_align_report) // "-x"
TrackAlignmentReport2(fd,track_buffer);
else
{
if(!(disk2file(fd, filename)))
printf("Operation failed!\n");
}
if (Use_SCPlus_IHS) // "-j"
{
// turn SCPlus IHS off
send_mnib_cmd(fd, FL_IHS_OFF, NULL, 0);
burst_read(fd);
}
motor_on(fd);
step_to_halftrack(fd, 18*2);
if(fplog) fclose(fplog);
exit(0);
}
void parallel_test(int iterations)
{
int i;
printf("Performing extended parallel port test\n");
for(i=0; i<iterations; i++)
{
if(!verify_floppy(fd))
{
printf("Parallel port failed extended testing. Check wiring and sheilding.\n");
exit(0);
}
printf(".");
}
printf("\nPassed advanced parallel port test\n");
exit(0);
}
int disk2file(CBM_FILE fd, char *filename)
{
int count = 0;
char newfilename[256];
char filenum[4], *dotpos;
/* read data from drive to file */
motor_on(fd);
if(compare_extension(filename, "NB2"))
{
track_inc = 1;
if(!(write_nb2(fd, filename))) return 0;
}
else if(compare_extension(filename, "NIB"))
{
if(!(read_floppy(fd, track_buffer, track_density, track_length))) return 0;
if(!(file_buffer_size = write_nib(file_buffer, track_buffer, track_density, track_length))) return 0;
if(!(save_file(filename, file_buffer, file_buffer_size))) return 0;
if(interactive_mode)
{
for(;;)
{
motor_off(fd);
printf("Swap disk and press a key for next image, or CTRL-C to quit.\n");
getchar();
motor_on(fd);
/* create new filename */
sprintf(filenum, "%d", ++count);
strcpy(newfilename, filename);
dotpos = strrchr(newfilename, '.');
if (dotpos != NULL) *dotpos = '\0';
strcat(newfilename, filenum);
strcat(newfilename, ".nib");
if(!(read_floppy(fd, track_buffer, track_density, track_length))) return 0;
if(!(file_buffer_size = write_nib(file_buffer, track_buffer, track_density, track_length))) return 0;
if(!(save_file(newfilename, file_buffer, file_buffer_size))) return 0;
}
}
}
else
{
if(!(read_floppy(fd, track_buffer, track_density, track_length))) return 0;
if(!(file_buffer_size = write_nib(file_buffer, track_buffer, track_density, track_length))) return 0;
if(!(file_buffer_size = LZ_CompressFast(file_buffer, compressed_buffer, file_buffer_size))) return 0;
if(!(save_file(filename, compressed_buffer, file_buffer_size))) return 0;
if(interactive_mode)
{
for(;;)
{
motor_off(fd);
printf("Swap disk and press a key for next image, or CTRL-C to quit.\n");
getchar();
motor_on(fd);
/* create new filename */
sprintf(filenum, "%d", ++count);
strcpy(newfilename, filename);
dotpos = strrchr(newfilename, '.');
if (dotpos != NULL) *dotpos = '\0';
strcat(newfilename, filenum);
strcat(newfilename, ".nbz");
if(!(read_floppy(fd, track_buffer, track_density, track_length))) return 0;
if(!(file_buffer_size = write_nib(file_buffer, track_buffer, track_density, track_length))) return 0;
if(!(file_buffer_size = LZ_CompressFast(file_buffer, compressed_buffer, file_buffer_size))) return 0;
if(!(save_file(newfilename, compressed_buffer, file_buffer_size))) return 0;
}
}
}
return 1;
}
void
usage(void)
{
fprintf(stderr, "usage: nibread [options] <filename>\n\n"
" -@x: Use OpenCBM device 'x' (xa1541, xum1541:0, xum1541:1, etc.)\n"
" -D[n]: Use drive #[n]\n"
" -e[n]: Retry reading tracks with errors [n] times\n"
" -S[n]: Override starting track\n"
" -E[n]: Override ending track\n"
" -G[n]: Match track gap by [n] number of bytes (advanced users only)\n"
" -s: Use SRQ transfer code instead of parallel (1571 only)\n"
" -k: Disable reading of 'killer' tracks\n"
" -d: Force default densities\n"
" -v: Enable track matching (crude read verify)\n"
" -I: Interactive imaging mode\n"
// " -m: Disable minimum capacity check\n"
" -V: Verbose (output more detailed track data)\n"
" -h: Read halftracks\n"
" -t: Extended parallel port tests\n"
" -j: Use Index Hole Sensor (1541/1571 SC+ compatible IHS)\n"
" -x: Track Alignment Report (1541/1571 SC+ compatible IHS)\n"
" -y: Deep Bitrate Analysis (1541/1571 SC+ compatible IHS)\n"
" -z: Test Index Hole Sensor (1541/1571 SC+ compatible IHS)\n"
);
exit(1);
}