forked from OpenCBM/nibtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.c
1624 lines (1371 loc) · 40.6 KB
/
fileio.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
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
/*
fileio.c - (C) Pete Rittwage
---
contains routines used by nibtools to read/write files on the host
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>
#include "mnibarch.h"
#include "gcr.h"
#include "nibtools.h"
#include "prot.h"
#include "crc.h"
#include "md5.h"
//#include "bitshifter.c"
void parseargs(char *argv[])
{
int count;
double st, et;
// parse arguments
switch ((*argv)[1])
{
case '@':
cbm_adapter = &(*argv)[2];
printf("* Using OpenCBM adapter %s\n", cbm_adapter);
break;
case '$':
sync_align_buffer = 1;
printf("* Force sync align tracks\n");
if((*argv)[2]=='$')
{
printf("* Don't force re-alignment\n");
sync_align_buffer = 2;
}
break;
case 'B':
backwards = 1;
printf("* Write tracks backwards\n");
break;
case 'P':
printf("* Skip 1571 SRQ Support (Use parallel always)\n");
override_srq = 1;
break;
case 'h':
if(track_inc == 1) track_inc = 2;
else track_inc = 1;
printf("* Toggle halftracks (increment=%d)\n",track_inc);
break;
case 'I':
increase_sync = atoi(&(*argv)[2]);
if(!increase_sync) increase_sync=1;
printf("* Fix/increase short syncs by %d\n", increase_sync);
break;
case 'S':
if (!(*argv)[2]) usage();
st = atof(&(*argv)[2])*2;
start_track = (int)st;
printf("* Start track set to %.1f (%d)\n", st/2, start_track);
break;
case 'E':
if (!(*argv)[2]) usage();
et = atof(&(*argv)[2])*2;
end_track = (int)et;
if((et/2)>41) printf("WARNING: Most drives won't reach past 41 tracks and your head carriage can physically JAM!\n");
if((et/2)>MAX_TRACKS_1541)
{
printf("WARNING: MAX tracks is %d\n",MAX_TRACKS_1541);
end_track=(MAX_TRACKS_1541*2);
}
printf("* End track set to %.1f (%d)\n", et/2, end_track);
break;
case 'z':
nb2cycle = atoi(&(*argv)[2]);
printf("* NB2 cycle chosen = %d\n", nb2cycle);
break;
case 'u':
case 'w':
mode = MODE_UNFORMAT_DISK;
unformat_passes = atoi(&(*argv)[2]);
if(!unformat_passes) unformat_passes = 1;
printf("* Unformat passes = %d\n", unformat_passes);
track_inc = 1;
break;
case 'R':
// hidden secret raw track file writing mode
printf("* Raw track dump write mode\n");
mode = MODE_WRITE_RAW;
break;
case 'A':
printf("* Speed adjustment mode\n");
mode = MODE_SPEED_ADJUST;
break;
case 'p':
// custom protection handling
printf("* Custom copy protection handler: ");
switch((*argv)[2])
{
case 'x':
printf("V-MAX!\n");
memset(align_map, ALIGN_VMAX, MAX_TRACKS_1541+1);
fix_gcr = 0;
presync = 1;
break;
case 'c':
printf("V-MAX! (CINEMAWARE)\n");
memset(align_map, ALIGN_VMAX_CW, MAX_TRACKS_1541+1);
fix_gcr = 0;
presync = 1;
break;
case 'g':
case 'm':
printf("SecuriSpeed/Early Rainbow Arts\n"); /* turn off reduction for track > 36 */
for(count = 36; count <= MAX_TRACKS_1541+1; count ++)
{
reduce_map[count] = REDUCE_NONE;
align_map[count] = ALIGN_AUTOGAP;
}
fix_gcr = 0;
break;
case 'v':
printf("VORPAL (NEWER)\n");
memset(align_map, ALIGN_AUTOGAP, MAX_TRACKS_1541+1);
align_map[18] = ALIGN_NONE;
fillbyte=0x55;
break;
case'r':
printf("RAPIDLOK\n"); /* don't reduce sync, but everything else */
//for(count = 1; count <= MAX_TRACKS_1541; count ++)
// reduce_map[count] = REDUCE_BAD | REDUCE_GAP;
memset(align_map, ALIGN_RAPIDLOK, MAX_TRACKS_1541+1);
break;
case'p':
printf("Pirateslayer\n");
align_map[2] = ALIGN_PSLAYER;
align_map[36] = ALIGN_PSLAYER;
align_map[37] = ALIGN_PSLAYER;
break;
default:
printf("Unknown protection handler\n");
break;
}
break;
case 'a':
// custom alignment handling
printf("* Custom alignment: ");
if ((*argv)[2] == '0')
{
printf("sector 0\n");
memset(align_map, ALIGN_SEC0, MAX_TRACKS_1541+1);
}
else if ((*argv)[2] == 'g')
{
printf("gap\n");
memset(align_map, ALIGN_GAP, MAX_TRACKS_1541+1);
}
else if ((*argv)[2] == 'w')
{
printf("longest bad GCR run\n");
memset(align_map, ALIGN_BADGCR, MAX_TRACKS_1541+1);
}
else if ((*argv)[2] == 's')
{
printf("longest sync\n");
memset(align_map, ALIGN_LONGSYNC, MAX_TRACKS_1541+1);
}
else if ((*argv)[2] == 'a')
{
printf("autogap\n");
memset(align_map, ALIGN_AUTOGAP, MAX_TRACKS_1541+1);
}
else if ((*argv)[2] == 'n')
{
printf("raw (no alignment, use NIB start)\n");
memset(align_map, ALIGN_RAW, MAX_TRACKS_1541+1);
}
else
printf("Unknown alignment parameter\n");
break;
case 'r':
reduce_sync = atoi(&(*argv)[2]);
if(reduce_sync)
{
printf("* Reduce sync to %d bytes\n", reduce_sync);
}
else
{
printf("* Disabled sync reduction\n");
for(count = 1; count <= MAX_TRACKS_1541; count ++)
reduce_map[count] &= ~REDUCE_SYNC;
}
break;
case '0':
printf("* Reduce bad GCR enabled\n");
for(count = 1; count <= MAX_TRACKS_1541; count ++)
reduce_map[count] |= REDUCE_BAD;
break;
case 'g':
printf("* Reduce gaps enabled\n");
for(count = 1; count <= MAX_TRACKS_1541; count ++)
reduce_map[count] |= REDUCE_GAP;
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 'f':
if (!(*argv)[2])
fix_gcr = 0;
else
fix_gcr = atoi(&(*argv)[2]);
printf("* Enabled level %d 'bad' GCR reproduction.\n", fix_gcr);
break;
case 'v':
verbose++;
printf("* Verbosity on level %d\n", verbose);
break;
case 'V':
track_match = 1;
printf("* Enable track match verify\n");
break;
case 'c':
auto_capacity_adjust = 0;
printf("* Disabled automatic capacity adjustment\n");
break;
case 'm':
if (!(*argv)[2])
extra_capacity_margin = 0;
else
extra_capacity_margin = atoi(&(*argv)[2]);
printf("* Changed extra capacity margin to %d\n", extra_capacity_margin);
break;
case 'M':
printf("* Minimum capacity ignore on\n");
cap_min_ignore = 1;
break;
case 'o':
printf("* Use old hard-coded G64 format\n");
old_g64 = 1;
break;
case 'T':
if (!(*argv)[2]) usage();
skew = (atoi(&(*argv)[2]));
if((skew > 200) || (skew < 0))
{
printf("Skew must be between 1 and 200ms\n");
skew = 0;
}
printf("* Skew set to %dms\n",skew);
case 't':
if(!ihs) align_disk = 1;
printf("* Attempt timer-based track alignment\n");
break;
case 'i':
printf("* 1571 or SuperCard-compatible index hole sensor (use only for side 1)\n");
align_disk = 0;
ihs = 1;
break;
case 'C':
rpm_real = atoi(&(*argv)[2]);
printf("* Simulate track capacity: %dRPM\n",rpm_real);
break;
case 'F':
/* override FAT track detection */
fattrack = atoi(&(*argv)[2]);
if(!fattrack)
{
printf("* FAT tracks disabled\n");
fattrack=99;
}
else
{
printf("* Insert FAT track on %d/%.2f/%d\n",fattrack,fattrack+0.5,fattrack+1);
fattrack*=2;
}
break;
case 'x':
presync = atoi(&(*argv)[2]);
if(!presync) presync=1;
printf("* Add short sync bytes to start of each track:%d\n",presync);
break;
case 'b':
// custom fillbyte
printf("* Custom fillbyte: ");
//if ((*argv)[2] == '0')
//{
// printf("$00\n");
// fillbyte = 0x00;
//}
//if ((*argv)[2] == '5')
//{
// printf("$55\n");
// fillbyte = 0x55;
//}
//if ((*argv)[2] == 'f')
//{
// printf("$ff\n");
// fillbyte = 0xff;
//}
if ((*argv)[2] == 'a')
{
/* adaptive */
fillbyte = 0xfe;
}
else
{
fillbyte = (unsigned char)strtol(&(*argv)[2], NULL, 16);
}
printf("%x\n",fillbyte);
break;
/* this is only used in reading or unformat */
case 'k':
read_killer = 0;
printf("* Ignore 'killer' tracks\n");
break;
default:
usage();
break;
}
}
void switchusage(void)
{
printf(
" -a[x]: Force alternative track alignments (advanced users only)\n"
" -p[x]: Custom protection handlers (advanced users only)\n"
" -f[n]: Enable level 'n' aggressive bad GCR simulation\n"
" -G[n]: Alternate gap match length\n"
" -C[n]: Simulate 'n' RPM track capacity\n"
" -T[n]: Track skew simulation (in ms, max 200ms)\n"
" -g: Enable gap reduction\n"
" -0: Enable bad GCR run reduction\n"
" -r: Disable automatic sync reduction\n"
" -f: Disable automatic bad GCR simulation\n"
//" -b: Custom fillbyte (0,5,f,a=adaptive)\n"
" -v: Verbose (output more detailed info)\n");
}
int load_file(char *filename, BYTE *file_buffer)
{
int size;
FILE *fpin;
printf("Loading \"%s\"...\n",filename);
if ((fpin = fopen(filename, "rb")) == NULL)
{
printf("Couldn't open input file %s!\n", filename);
return 0;
}
fseek(fpin, 0, SEEK_END);
size = ftell(fpin);
rewind(fpin);
if (fread(file_buffer, size, 1, fpin) != 1) {
printf("unable to read file\n");
return 0;
}
printf("Successfully loaded %d bytes\n", size);
fclose(fpin);
return size;
}
int read_nib(BYTE *file_buffer, int file_buffer_size, BYTE *track_buffer, BYTE *track_density, size_t *track_length)
{
int track, t_index=0, h_index=0;
printf("Parsing NIB data...\n");
if (memcmp(file_buffer, "MNIB-1541-RAW", 13) != 0)
{
printf("Not valid NIB data!\n");
return 0;
}
else
printf("NIB file version %d\n", file_buffer[13]);
while(file_buffer[0x10+h_index])
{
track = file_buffer[0x10+h_index];
track_density[track] = (BYTE)(file_buffer[0x10 + h_index + 1]);
track_density[track] %= BM_MATCH; /* discard unused BM_MATCH mark */
memcpy(track_buffer + (track * NIB_TRACK_LENGTH),
file_buffer + (t_index * NIB_TRACK_LENGTH) + 0x100,
NIB_TRACK_LENGTH);
h_index+=2;
t_index++;
}
printf("Successfully parsed NIB data for %d tracks\n", t_index);
return 1;
}
int read_nb2(char *filename, BYTE *track_buffer, BYTE *track_density, size_t *track_length, size_t cycle)
{
int track, pass_density, nibsize, temp_track_inc, numtracks;
int header_entry = 0;
size_t pass;
char header[0x100];
BYTE nibdata[0x2000];
BYTE tmpdata[0x2000];
BYTE diskid[2], dummy;
FILE *fpin;
size_t errors, best_err, best_pass;
size_t length, best_len;
char errorstring[0x1000];
char testfilename[16];
FILE *trkout;
printf("Reading NB2 file...\n");
temp_track_inc = 1; /* all nb2 files contain halftracks */
if ((fpin = fopen(filename, "rb")) == NULL)
{
printf("Couldn't open input file %s!\n", filename);
return 0;
}
if (fread(header, sizeof(header), 1, fpin) != 1)
{
printf("unable to read NIB header\n");
return 0;
}
if (memcmp(header, "MNIB-1541-RAW", 13) != 0)
{
printf("input file %s isn't an NB2 data file!\n", filename);
return 0;
}
/* Determine number of tracks in image (estimated by filesize) */
fseek(fpin, 0, SEEK_END);
nibsize = ftell(fpin);
numtracks = (nibsize - NIB_HEADER_SIZE) / (NIB_TRACK_LENGTH * 16);
temp_track_inc = 1;
printf("\n%d track image (filesize = %d bytes)\n", numtracks, nibsize);
/* get disk id */
rewind(fpin);
fseek(fpin, sizeof(header) + (17 * 2 * NIB_TRACK_LENGTH * 16) + (8 * NIB_TRACK_LENGTH), SEEK_SET);
fread(tmpdata, NIB_TRACK_LENGTH, 1, fpin);
if (!extract_id(tmpdata, diskid))
{
printf("Cannot find directory sector.\n");
return 0;
}
if(verbose) printf("\ndiskid: %c%c\n", diskid[0], diskid[1]);
rewind(fpin);
if (fread(header, sizeof(header), 1, fpin) != 1) {
printf("unable to read NB2 header\n");
return 0;
}
for (track = 2; track <= end_track; track += temp_track_inc)
{
/* get density from header or use default */
track_density[track] = (BYTE)(header[0x10 + (header_entry * 2) + 1]);
header_entry++;
best_pass = 0;
best_err = 0;
best_len = 0; /* unused for now */
if(verbose) printf("\n%4.1f:",(float) track / 2);
/* contains 16 passes of track, four for each density */
for(pass_density = 0; pass_density < 4; pass_density ++)
{
if(verbose) printf(" (%d)", pass_density);
for(pass = 0; pass <= 3; pass ++)
{
/* get track from file */
fread(nibdata, NIB_TRACK_LENGTH, 1, fpin);
if(pass>(cycle-1)) continue;
length = extract_GCR_track(tmpdata, nibdata,
&dummy,
track/2,
capacity_min[track_density[track]&3],
capacity_max[track_density[track]&3]);
errors = check_errors(tmpdata, length, track, diskid, errorstring);
//printf("D:%d,L:%d,E:%d",track_density[track]&3,length,errors);
if(pass_density == (track_density[track]&3))
{
if( (pass==(cycle-1)) || (errors < best_err) )
{
//track_length[track] = 0x2000;
memcpy(track_buffer + (track * NIB_TRACK_LENGTH), nibdata, NIB_TRACK_LENGTH);
best_pass = pass;
best_err = errors;
}
}
sprintf(testfilename, "raw/tr%.1fd%d", (float) track/2, pass_density);
if(NULL != (trkout = fopen(testfilename, "w")))
{
fwrite(nibdata, NIB_TRACK_LENGTH, 1, trkout);
fclose(trkout);
}
}
}
/* output some specs */
if(verbose)
{
printf(" (");
if(track_density[track] & BM_NO_SYNC) printf("NOSYNC!");
if(track_density[track] & BM_FF_TRACK) printf("KILLER!");
printf("%d:%d) (pass %d, %d errors) %.1d%%", track_density[track]&3, track_length[track],
best_pass+1, best_err,
((track_length[track] / capacity[track_density[track]&3]) * 100));
}
}
fclose(fpin);
//printf("Successfully loaded NB2 file\n");
return 1;
}
int read_g64(char *filename, BYTE *track_buffer, BYTE *track_density, size_t *track_length)
{
int track, g64maxtrack, g64tracks, headersize;
int pointer=0;
BYTE header[0x7f0];
BYTE length_record[2];
FILE *fpin;
printf("Reading G64 file...\n");
if ((fpin = fopen(filename, "rb")) == NULL)
{
printf("Couldn't open input file %s!\n", filename);
return 0;
}
if (fread(header, sizeof(header), 1, fpin) != 1)
{
printf("unable to read G64 header\n");
return 0;
}
if (memcmp(header, "GCR-1541", 8) != 0)
{
printf("input file %s isn't a G64 data file!\n", filename);
return 0;
}
if (memcmp(header+0x2ac, "EXT", 3) == 0)
{
printf("\nExtended SPS G64 detected\n");
headersize=0x7f0;
if(!sync_align_buffer) sync_align_buffer=1;
else
{
sync_align_buffer=0;
printf("SPS file, but sync align was disabled by switch\n");
}
}
else
headersize=0x2ac;
g64tracks = (char)header[0x9];
g64maxtrack = (BYTE)header[0xb] << 8 | (BYTE)header[0xa];
if(verbose) printf("\nTracks:%d\nSize:%d\n", g64tracks, g64maxtrack);
rewind(fpin);
if (fread(header, headersize, 1, fpin) != 1)
{
printf("unable to read G64 header\n");
return 0;
}
if(g64maxtrack>NIB_TRACK_LENGTH)
{
printf("\nContains too large track!\nLikely corrupt G64 file\nWill attempt to skip bad tracks\n");
//return 0;
}
for (track = 2; track <= g64tracks; track++, pointer += 4)
{
int pointer2 = *(int*)(header+0xc+pointer);
int tmpLength;
/* check to see if track exists in file, else skip it */
if(!pointer2)
{
track_length[track]=0;
continue;
}
/* get density from header */
track_density[track] = header[0x15c + pointer];
fseek(fpin, pointer2, SEEK_SET);
//printf("\nDEBUG: filepointer=%d\n",pointer2);
/* get length */
fread(length_record, 2, 1, fpin);
tmpLength = length_record[1] << 8 | length_record[0];
if(tmpLength>NIB_TRACK_LENGTH)
{
tmpLength = NIB_TRACK_LENGTH;
//printf(" skipping extra data");
}
track_length[track] = tmpLength;
/* get track from file */
fread(track_buffer + (track * NIB_TRACK_LENGTH), tmpLength, 1, fpin);
/* output some specs */
if(verbose)
{
printf("%4.1f: ",(float) track/2);
if(track_density[track] & BM_NO_SYNC) printf("NOSYNC!");
if(track_density[track] & BM_FF_TRACK) printf("KILLER!");
printf("%d (density:%d)\n", track_length[track], track_density[track]);
}
}
fclose(fpin);
//printf("Successfully loaded G64 file\n");
return 1;
}
int read_d64(char *filename, BYTE *track_buffer, BYTE *track_density, size_t *track_length)
{
int track, sector, sector_ref;
BYTE buffer[256];
BYTE gcrdata[NIB_TRACK_LENGTH];
BYTE errorinfo[MAXBLOCKSONDISK];
BYTE id[3] = { 0, 0, 0 };
int error, d64size, last_track, cur_sector=0;
char errorstring[0x1000], tmpstr[8];
FILE *fpin;
printf("Reading D64 file...\n");
if ((fpin = fopen(filename, "rb")) == NULL)
{
printf("Couldn't open input file %s!\n", filename);
return 0;
}
/* here we get to rebuild tracks from scratch */
memset(errorinfo, SECTOR_OK, sizeof(errorinfo));
/* determine d64 image size */
fseek(fpin, 0, SEEK_END);
d64size = ftell(fpin);
switch (d64size)
{
case (BLOCKSONDISK * 257): /* 35 track image with errorinfo */
fseek(fpin, BLOCKSONDISK * 256, SEEK_SET);
fread(errorinfo, BLOCKSONDISK, 1, fpin); // @@@SRT: check success
/* FALLTHROUGH */
case (BLOCKSONDISK * 256): /* 35 track image w/o errorinfo */
last_track = 35;
break;
case (MAXBLOCKSONDISK * 257): /* 40 track image with errorinfo */
fseek(fpin, MAXBLOCKSONDISK * 256, SEEK_SET);
fread(errorinfo, MAXBLOCKSONDISK, 1, fpin); // @@@SRT: check success
/* FALLTHROUGH */
case (MAXBLOCKSONDISK * 256): /* 40 track image w/o errorinfo */
last_track = 40;
break;
default: // non-standard images, attempt to load anyway
//rewind(fpin);
//printf("Bad d64 image size.\n");
//return 0;
printf("\nNon-standard D64 image... attempting to load as 40-track anyway\n");
printf("%d sectors in file\n", d64size/256);
last_track = 40;
break;
}
// determine disk id from track 18 (offsets $165A2, $165A3)
fseek(fpin, 0x165a2, SEEK_SET);
fread(id, 2, 1, fpin); // @@@SRT: check success
rewind(fpin);
sector_ref = 0;
for (track = 1; track <= last_track; track++)
{
// clear buffers
memset(gcrdata, 0x55, sizeof(gcrdata));
errorstring[0] = '\0';
for (sector = 0; sector < sector_map[track]; sector++)
{
// get error and increase reference pointer in errorinfo
error = errorinfo[sector_ref++];
if (error != SECTOR_OK)
{
sprintf(tmpstr, " E%dS%d", error, sector);
strcat(errorstring, tmpstr);
}
// read sector from file
if(d64size/256 > cur_sector)
fread(buffer, 256, 1, fpin); // @@@SRT: check success
else
memset(buffer, fillbyte, sizeof(buffer));
// convert to gcr
convert_sector_to_GCR(buffer, gcrdata + (sector * (SECTOR_SIZE + sector_gap_length[track])), track, sector, id, error);
cur_sector++;
}
// calculate track length
track_length[track*2] = sector_map[track] * (SECTOR_SIZE + sector_gap_length[track]);
// no half tracks in D64, so clear them
track_length[(track*2)+1] = 0;
// use default densities for D64
track_density[track*2] = speed_map[track];
// write track
memcpy(track_buffer + (track * 2 * NIB_TRACK_LENGTH), gcrdata, track_length[track*2]);
//printf("%s", errorstring);
}
// "unformat" last 5 tracks on 35 track disk
if (last_track == 35)
{
for (track = 36 * 2; track <= end_track; track += 2)
{
memset(track_buffer + (track * NIB_TRACK_LENGTH), 0, NIB_TRACK_LENGTH);
track_density[track] = (2 | BM_NO_SYNC);
track_length[track] = 0;
}
}
fclose(fpin);
//printf("Successfully loaded D64 file\n");
return 1;
}
int save_file(char *filename, BYTE *file_buffer, int length)
{
FILE *fpout;
/* create output file */
if ((fpout = fopen(filename, "wb")) == NULL)
{
printf("Couldn't create output file %s!\n", filename);
return 0;
}
if(!(fwrite(file_buffer, length, 1, fpout)))
{
printf("Couldn't write to output file %s!\n", filename);
return 0;
}
fclose(fpout);
printf("Successfully saved file %s\n", filename);
return 1;
}
int write_nib(BYTE*file_buffer, BYTE *track_buffer, BYTE *track_density, size_t *track_length)
{
/* writes contents of buffers into NIB file, with header and density information
it does not process the track
*/
int track;
char header[0x100];
int header_entry = 0;
printf("Converting to NIB format...\n");
/* clear header */
memset(header, 0, sizeof(header));
/* header now contains whether halftracks were read */
if(track_inc == 1)
sprintf(header, "MNIB-1541-RAW%c%c%c", 3, 0, 1);
else
sprintf(header, "MNIB-1541-RAW%c%c%c", 3, 0, 0);
for (track = start_track; track <= end_track; track += track_inc)
{
header[0x10 + (header_entry * 2)] = (BYTE)track;
header[0x10 + (header_entry * 2) + 1] = track_density[track];
/* process and save track to disk */
memcpy(file_buffer + sizeof(header) + (NIB_TRACK_LENGTH * header_entry),
track_buffer + (NIB_TRACK_LENGTH * track), NIB_TRACK_LENGTH);
header_entry++;
}
memcpy(file_buffer, header, sizeof(header));
//printf("Successfully parsed data to NIB format\n");
return (sizeof(header) + (header_entry * NIB_TRACK_LENGTH));
}
int write_d64(char *filename, BYTE *track_buffer, BYTE *track_density, size_t *track_length)
{
/* writes contents of buffers into D64 file, with errorblock information (if detected) */
FILE *fpout;
int track, sector;
int errors = 0;
int hi_errors = 0;
int save_errorinfo = 0;
int save_40_errors = 0;
int save_40_tracks = 0;
int blockindex = 0;
int offset = 0;
BYTE *cycle_start; /* start position of cycle */
BYTE *cycle_stop; /* stop position of cycle +1 */
BYTE id[4];
BYTE rawdata[260];
BYTE d64data[MAXBLOCKSONDISK * 256], *d64ptr;
BYTE errorinfo[MAXBLOCKSONDISK], errorcode;
int blocks_to_save;
printf("Writing D64 file...\n");
memset(errorinfo, 0,sizeof(errorinfo));
memset(rawdata, 0,sizeof(rawdata));
memset(d64data, 0,sizeof(d64data));
/* create output file */
if ((fpout = fopen(filename, "wb")) == NULL)
{
printf("Couldn't create output file %s!\n", filename);
return 0;
}
/* get disk id */
if (!extract_id(track_buffer + (18*2*NIB_TRACK_LENGTH), id))
{
int track = id[0];
//printf("debug: dir track really=%d\n",track);
offset = 18 - track;
if (!offset || !extract_id(track_buffer + ((18+offset)*2*NIB_TRACK_LENGTH), id))
{
printf("Cannot find directory sector.\n");
return 0;
}
else
{
printf("Track offset found in image: %d\n",offset);
//offset++; // the rest of the routines for D64 only operate on every other track
}
}
//printf("debug: diskid=%s\n",id);
d64ptr = d64data;
for (track = start_track; track <= 40*2; track += 2)
{
cycle_start = track_buffer + ((track+(offset*2)) * NIB_TRACK_LENGTH);
cycle_stop = track_buffer + ((track+(offset*2)) * NIB_TRACK_LENGTH) + track_length[track+(offset*2)];
//printf("debug: start=%d, stop=%d\n",cycle_start,cycle_stop);
if(verbose) printf("%.2d (%d):" ,track/2, capacity[speed_map[track/2]]);
if (track < 2 || track > 80)
{
for (sector = 0; sector < sector_map[track/2]; sector++)
errorinfo[blockindex] = SYNC_NOT_FOUND;
}
else
for (sector = 0; sector < sector_map[track/2]; sector++)
{
if(verbose) printf("%d", sector);
memset(rawdata, 0,sizeof(rawdata));
errorcode = convert_GCR_sector(cycle_start, cycle_stop, rawdata, track/2, sector, id);
errorinfo[blockindex] = errorcode; /* OK by default */
if (errorcode != SECTOR_OK)
{
if (track/2 <= 35)
{
save_errorinfo = 1;
errors++;
}
else
{
save_40_errors = 1;
hi_errors++;
}
}
if((track/2 > 35) &&
(errorcode != SYNC_NOT_FOUND) &&
(errorcode != HEADER_NOT_FOUND))
{
save_40_tracks = 1;
}
/* screen information */
if (errorcode == SECTOR_OK)
{
if(verbose) printf(" ");
}
else
{
if(verbose)
printf("%.1x", errorcode);
else
if(track/2<=35)
printf("Error %.1d on Track %d, Sector %d\n", errorcode, track/2, sector);
}
/* dump to buffer */
memcpy(d64ptr, rawdata+1 , 256);
d64ptr += 256;
blockindex++;
}
if(verbose) printf("\n");
}
if(verbose) printf("\n");
blocks_to_save = (save_40_tracks) ? MAXBLOCKSONDISK : BLOCKSONDISK;
if (fwrite(d64data, blocks_to_save * 256, 1, fpout) != 1)
{
printf("Cannot write d64 data.\n");
return 0;
}
if (save_errorinfo == 1)
{
assert(sizeof(errorinfo) >= (size_t)blocks_to_save);
if (fwrite(errorinfo, blocks_to_save, 1, fpout) != 1)
{
printf("Cannot write sector data.\n");
return 0;