-
Notifications
You must be signed in to change notification settings - Fork 4
/
fds.cpp
1251 lines (1072 loc) · 32.8 KB
/
fds.cpp
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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "device.h"
#include "fds.h"
#include "spi.h"
#include "os.h"
/*
Disk format in flash:
struct {
uint16_t filename[120]; //null terminated unicode string. filename[0]: 0xFFFF=empty, 0x0000=multi-disk image (continued from previous)
uint8_t reserved[14]; //set to 0
uint16_t lead_in; //lead-in length (#bits), 0=default
uint8_t data[0xff00]; //disk data, beginning with gap end mark (0x80) of first block
}
*/
enum {
DEFAULT_LEAD_IN=28300, //#bits (~25620 min)
GAP=976/8-1, //(~750 min)
MIN_GAP_SIZE=0x300, //bits
FDSSIZE=65500, //size of .fds disk side, excluding header
FLASHHEADERSIZE=0x100,
};
static void raw03_to_bin(uint8_t *raw, int rawSize, uint8_t **_bin, int *_binSize);
// allocate buffer and read whole file
bool loadFile(char *filename, uint8_t **buf, int *filesize) {
FILE *f=NULL;
int size;
bool result=false;
do {
if(!buf || !filesize)
break;
f=fopen(filename,"rb");
if(!f)
break;
fseek(f,0,SEEK_END);
size=ftell(f);
if(size>0x100000) //sanity check
break;
fseek(f,0,SEEK_SET);
if( !(*buf=(uint8_t*)malloc(size)) )
break;
*filesize = fread(*buf, 1, size, f);
result=true;
} while(0);
if(f)
fclose(f);
return result;
}
//don't include gap end
uint16_t calc_crc(uint8_t *buf, int size) {
uint32_t crc=0x8000;
int i;
while(size--) {
crc |= (*buf++)<<16;
for(i=0;i<8;i++) {
if(crc & 1) crc ^= 0x10810;
crc>>=1;
}
}
return crc;
}
void copy_block(uint8_t *dst, uint8_t *src, int size) {
dst[0] = 0x80;
memcpy(dst+1, src, size);
uint32_t crc = calc_crc(dst+1, size+2);
dst[size+1]=crc;
dst[size+2]=crc>>8;
}
//Adds GAP + GAP end (0x80) + CRCs to .FDS image
//Returns size (0=error)
int fds_to_bin(uint8_t *dst, uint8_t *src, int dstSize) {
int i=0, o=0;
//check *NINTENDO-HVC* header
if(src[0]!=0x01 || src[1]!=0x2a || src[2]!=0x4e) {
printf("Not an FDS file.\n");
return 0;
}
memset(dst, 0, dstSize);
//block type 1
copy_block(dst+o, src+i, 0x38);
i+=0x38;
o+=0x38+3+GAP;
//block type 2
copy_block(dst+o, src+i, 2);
i+=2;
o+=2+3+GAP;
//block type 3+4...
while(src[i]==3) {
int size = (src[i+13] | (src[i+14]<<8))+1;
if(o + 16+3 + GAP + size+3 > dstSize) { //end + block3 + crc + gap + end + block4 + crc
printf("Out of space (%d bytes short), adjust GAP size?\n",(o + 16+3 + GAP + size+3)-dstSize);
return 0;
}
copy_block(dst+o, src+i, 16);
i+=16;
o+=16+3+GAP;
copy_block(dst+o, src+i, size);
i+=size;
o+=size+3+GAP;
}
return o;
}
/*
Adds GAP + GAP end (0x80) + CRCs to Game Doctor image. Returns size (0=error)
GD format:
0x??, 0x??, 0x8N 3rd byte seems to be # of files on disk, same as block 2.
repeat to end of disk {
N bytes (block contents, same as .fds)
2 dummy CRC bytes (0x00 0x00)
}
*/
int gameDoctor_to_bin(uint8_t *dst, uint8_t *src, int dstSize) {
//check for *NINTENDO-HVC* at 0x03 and second block following CRC
if(src[3]!=0x01 || src[4]!=0x2a || src[5]!=0x4e || src[0x3d]!=0x02) {
printf("Not GD format.\n");
return 0;
}
memset(dst, 0, dstSize);
//block type 1
int i=3, o=0;
copy_block(dst+o, src+i, 0x38);
i += 0x38+2; //block + dummy crc
o += 0x38+3+GAP; //gap end + block + crc + gap
//block type 2
copy_block(dst+o, src+i, 2);
i += 2+2;
o += 2+3+GAP;
//block type 3+4...
while(src[i]==3) {
int size = (src[i+13] | (src[i+14]<<8))+1;
if(o + 16+3 + GAP + size+3 > dstSize) { //end + block3 + crc + gap + end + block4 + crc
printf("Out of space (%d bytes short), adjust GAP size?\n",(o + 16+3 + GAP + size+3)-dstSize);
return 0;
}
copy_block(dst+o, src+i, 16);
i+=16+2;
o+=16+3+GAP;
copy_block(dst+o, src+i, size);
i+=size+2;
o+=size+3+GAP;
}
return o;
}
//look for pattern of bits matching block 1
static int findFirstBlock(uint8_t *raw) {
static const uint8_t dat[]={1,0,1,0,0,0,0,0, 0,1,2,2,1,0,1,0, 0,1,1,2,1,1,1,1, 1,1,0,0,1,1,1,0};
int i,len;
for(i=0, len=0; i<0x2000*8; i++) {
if(raw[i]==dat[len]) {
if(len==sizeof(dat)-1)
return i-len;
len++;
} else {
i-=len;
len=0;
}
}
return -1;
}
bool block_decode(uint8_t *dst, uint8_t *src, int *inP, int *outP, int srcSize, int dstSize, int blockSize, char blockType) {
if(*outP+blockSize+2 > dstSize) {
printf("Out of space\n");
return false;
}
int in=*inP;
int outEnd=(*outP+blockSize+2)*8;
int out=(*outP)*8;
int start;
//scan for gap end
for(int zeros=0; src[in]!=1 || zeros<MIN_GAP_SIZE; in++) {
if(src[in]==0) {
zeros++;
} else {
zeros=0;
}
if(in>=srcSize-2)
return false;
}
start=in;
char bitval=1;
in++;
do {
if(in>=srcSize) { //not necessarily an error, probably garbage at end of disk
//printf("Disk end\n");
return false;
}
switch(src[in]|(bitval<<4)) {
case 0x11:
out++;
case 0x00:
out++;
bitval=0;
break;
case 0x12:
out++;
case 0x01:
case 0x10:
dst[out/8] |= 1<<(out&7);
out++;
bitval=1;
break;
default: //Unexpected value. Keep going, we'll probably get a CRC warning
//printf("glitch(%d) @ %X(%X.%d)\n", src[in], in, out/8, out%8);
out++;
bitval=0;
break;
}
in++;
} while(out<outEnd);
if(dst[*outP] !=blockType) {
printf("Wrong block type %X(%X)-%X(%X) (found %d, expected %d)\n", start, *outP, in, out-1, dst[*outP], blockType);
return false;
}
out=out/8-2;
//printf("Out%d %X(%X)-%X(%X)\n", blockType, start, *outP, in, out-1);
if(calc_crc(dst+*outP,blockSize+2)) {
uint16_t crc1=(dst[out+1]<<8)|dst[out];
dst[out]=0;
dst[out+1]=0;
uint16_t crc2=calc_crc(dst+*outP,blockSize+2);
printf("Bad CRC (%04X!=%04X)\n", crc1, crc2);
}
dst[out]=0; //clear CRC
dst[out+1]=0;
dst[out+2]=0; //+spare bit
*inP=in;
*outP=out;
return true;
}
__inline uint8_t raw_to_raw03_byte(uint8_t raw)
{
if (raw < 0x48)
return(3);
else if (raw < 0x70)
return(0);
else if (raw < 0xA0)
return(1);
else if (raw < 0xD0)
return(2);
return(3);
}
//Turn raw data from adapter to pulse widths (0..3)
//Input capture clock is 6MHz. At 96.4kHz (FDS bitrate), 1 bit ~= 62 clocks
//We could be clever about this and account for drive speed fluctuations, etc. but this will do for now
static void raw_to_raw03(uint8_t *raw, int rawSize) {
for(int i=0; i<rawSize; ++i) {
raw[i] = raw_to_raw03_byte(raw[i]);
}
}
//Simplified disk decoding. This assumes disk will follow standard FDS file structure
static bool raw03_to_fds(uint8_t *raw, uint8_t *fds, int rawsize) {
int in,out;
memset(fds,0,FDSSIZE);
//lead-in can vary a lot depending on drive, scan for first block to get our bearings
in=findFirstBlock(raw)-MIN_GAP_SIZE;
if(in<0)
return false;
out=0;
if(!block_decode(fds, raw, &in, &out, rawsize, FDSSIZE+2, 0x38, 1))
return false;
if(!block_decode(fds, raw, &in, &out, rawsize, FDSSIZE+2, 2, 2))
return false;
do {
if(!block_decode(fds, raw, &in, &out, rawsize, FDSSIZE+2, 16, 3))
return true;
if(!block_decode(fds, raw, &in, &out, rawsize, FDSSIZE+2, 1+(fds[out-16+13] | (fds[out-16+14]<<8)), 4))
return true;
} while(in<rawsize);
return true;
}
// TODO - only handles one side, files will need to be joined manually
bool FDS_readDisk(char *filename_raw, char *filename_bin, char *filename_fds) {
enum { READBUFSIZE=0x90000 };
FILE *f;
uint8_t *readBuf=NULL;
int result;
int bytesIn=0;
//if(!(dev_readIO()&MEDIA_SET)) {
// printf("Warning - Disk not inserted?\n");
//}
if(!dev_readStart())
return false;
readBuf=(uint8_t*)malloc(READBUFSIZE);
do {
result=dev_readDisk(readBuf+bytesIn);
bytesIn+=result;
if(!(bytesIn%((DISK_READMAX)*32)))
printf(".");
} while(result==DISK_READMAX && bytesIn<READBUFSIZE-DISK_READMAX);
printf("\n");
if(result<0) {
printf("Read error.\n");
return false;
}
if(filename_raw) {
if( (f=fopen(filename_raw,"wb")) ) {
fwrite(readBuf, 1, bytesIn, f);
fclose(f);
printf("Wrote %s\n",filename_raw);
}
}
raw_to_raw03(readBuf, bytesIn);
//decode to .fds
if(filename_fds) {
uint8_t *fds=(uint8_t*)malloc(FDSSIZE+16); //extra room for CRC junk
raw03_to_fds(readBuf, fds, bytesIn);
if( (f=fopen(filename_fds,"wb")) ) {
fwrite(fds, 1, FDSSIZE, f);
fclose(f);
printf("Wrote %s\n",filename_fds);
}
free(fds);
//decode to .bin
} else if(filename_bin) {
uint8_t *binBuf;
int binSize;
raw03_to_bin(readBuf, bytesIn, &binBuf, &binSize);
if( (f=fopen(filename_bin, "wb")) ) {
fwrite(binBuf, 1, binSize, f);
fclose(f);
printf("Wrote %s\n", filename_bin);
}
free(binBuf);
}
free(readBuf);
return true;
}
static bool writeDisk(uint8_t *bin, int binSize) {
static const uint8_t expand[]={ 0xaa, 0xa9, 0xa6, 0xa5, 0x9a, 0x99, 0x96, 0x95, 0x6a, 0x69, 0x66, 0x65, 0x5a, 0x59, 0x56, 0x55 };
int bytesOut;
bool fail=false;
//if(!(readIO()&MEDIA_SET)) {
// printf("Disk not inserted.\n");
//}
if(!dev_writeStart())
return false;
//expand to mfm for writing
uint8_t *mfm=(uint8_t*)malloc(binSize*2 + DISK_WRITEMAX);
for(int i=0; i<binSize; i++) {
mfm[i*2 + 0]=expand[bin[i]&0x0f];
mfm[i*2 + 1]=expand[(bin[i]>>4)&0x0f];
}
memset(mfm+binSize*2, 0xAA, DISK_WRITEMAX); //zero out last packet
for(bytesOut=0; bytesOut<binSize*2; bytesOut+=DISK_WRITEMAX) {
if(!dev_writeDisk(mfm+bytesOut, DISK_WRITEMAX)) {
printf("Write error (disk full?)\n");
fail=true;
break;
}
if(!(bytesOut%(DISK_WRITEMAX*16)))
printf("#");
}
if(!fail) {
//Fill remainder with empty space. Keep writing until we can't, EP0 will stall at end of disk
memset(mfm, 0xaa, DISK_WRITEMAX);
for(bytesOut=0; bytesOut<0x20000; bytesOut+=DISK_WRITEMAX) {
if(!dev_writeDisk(mfm, DISK_WRITEMAX))
break;
if(!(bytesOut%(DISK_WRITEMAX*16)))
printf(".");
}
}
free(mfm);
return !fail;
}
void hexdump(char*, void*, int);
static bool writeDisk2(uint8_t *bin, int binSize) {
// printf("writeDisk2: sending bin image to adaptor, size = %d\n", binSize);
// hexdump("bin", bin+ 3537, 256);
spi_writeSram(bin, 0, binSize);
if (!dev_writeStart())
return false;
return(true);
}
bool FDS_writeDisk(char *filename) {
enum {
LEAD_IN = DEFAULT_LEAD_IN / 8,
DISKSIZE = 0x11000, //whole disk contents including lead-in
};
uint8_t *inbuf = 0; //.FDS buffer
uint8_t *bin = 0; //.FDS with gaps/CRC
uint8_t *zero = 0;
int filesize;
int binSize;
if (!loadFile(filename, &inbuf, &filesize))
{
printf("Can't read %s\n", filename); return false;
}
bin = (uint8_t*)malloc(DISKSIZE);
zero = (uint8_t*)malloc(0x10000);
memset(zero, 0, 0x10000);
int inpos = 0, side = 0;
if (inbuf[0] == 'F')
inpos = 16; //skip fwNES header
filesize -= (filesize - inpos) % FDSSIZE; //truncate down to whole disk
char prompt;
do {
printf("Side %d\n", side + 1);
spi_writeSram(zero, 0, 0x10000);
memset(bin, 0, LEAD_IN);
binSize = fds_to_bin(bin + LEAD_IN, inbuf + inpos, DISKSIZE - LEAD_IN);
if (!binSize)
break;
if (!writeDisk2(bin, binSize + LEAD_IN))
break;
inpos += FDSSIZE;
side++;
// printf("finished write, inpos = %d, filesize = %d, inbuf[inpos] = %X", inpos, filesize, inbuf[inpos]);
//prompt for disk change
prompt = 0;
if (inpos<filesize && inbuf[inpos] == 0x01) {
printf("\nPlease wait for disk activity to stop before pressing ENTER to write the next disk side.\n");
prompt = readKb();
}
else {
printf("\nDisk image sent to SRAM on device and is currently writing.\nPlease wait for disk activity to stop before removing disk.\n");
}
} while (prompt == 0x0d);
free(bin);
free(zero);
free(inbuf);
return true;
}
/*bool FDS_writeDisk(char *filename) {
enum {
LEAD_IN = DEFAULT_LEAD_IN / 8,
DISKSIZE = 0x11000, //whole disk contents including lead-in
};
uint8_t *inbuf = 0; //.FDS buffer
uint8_t *bin = 0; //.FDS with gaps/CRC
int filesize;
int binSize;
if (!loadFile(filename, &inbuf, &filesize))
{
printf("Can't read %s\n", filename); return false;
}
bin = (uint8_t*)malloc(DISKSIZE);
int inpos = 0, side = 0;
if (inbuf[0] == 'F')
inpos = 16; //skip fwNES header
filesize -= (filesize - inpos) % FDSSIZE; //truncate down to whole disk
char prompt;
do {
printf("Side %d\n", side + 1);
memset(bin, 0, LEAD_IN);
binSize = fds_to_bin(bin + LEAD_IN, inbuf + inpos, DISKSIZE - LEAD_IN);
if (!binSize)
break;
if (!writeDisk(bin, binSize + LEAD_IN))
break;
inpos += FDSSIZE;
side++;
//prompt for disk change
prompt = 0;
if (inpos<filesize && inbuf[inpos] == 0x01) {
printf("Push ENTER for next disk side\n");
prompt = readKb();
}
} while (prompt == 0x0d);
free(bin);
free(inbuf);
return true;
}*/
uint32_t chksum_calc(uint8_t *buf, int size)
{
uint32_t ret = 0;
uint32_t *data = (uint32_t*)buf;
int i;
for (i = 0; i < size / 4; i++) {
ret ^= buf[i];
}
return(ret);
}
//slot 1..n
bool FDS_writeFlash(char *filename, int slot) {
enum { FILENAMELENGTH=120, }; //number of characters including null
uint8_t *inbuf=0;
uint8_t *outbuf=0;
int filesize;
if(!loadFile(filename, &inbuf, &filesize))
{ printf("Can't read %s\n",filename); return false; }
outbuf=(uint8_t*)malloc(SLOTSIZE);
int pos=0, side=0;
if(inbuf[0]=='F')
pos=16; //skip fwNES header
filesize -= (filesize-pos)%FDSSIZE; //truncate down to whole disks
while(pos<filesize && inbuf[pos]==0x01) {
printf("Side %d\n", side+1);
if(fds_to_bin(outbuf+FLASHHEADERSIZE, inbuf+pos, SLOTSIZE-FLASHHEADERSIZE)) {
memset(outbuf,0,FLASHHEADERSIZE);
uint32_t chksum = chksum_calc(outbuf + FLASHHEADERSIZE, SLOTSIZE - FLASHHEADERSIZE);
outbuf[240] = (uint8_t)(chksum >> 0);
outbuf[241] = (uint8_t)(chksum >> 8);
outbuf[242] = (uint8_t)(chksum >> 16);
outbuf[243] = (uint8_t)(chksum >> 24);
outbuf[244] = DEFAULT_LEAD_IN & 0xff;
outbuf[245] = DEFAULT_LEAD_IN / 256;
if(side==0) {
//strip path from filename
char *shortName=strrchr(filename,'/'); // ...dir/file.fds
#ifdef _WIN32
if(!shortName)
shortName=strrchr(filename,'\\'); // ...dir\file.fds
if(!shortName)
shortName=strchr(filename,':'); // C:file.fds
#endif
if(!shortName)
shortName=filename;
else
shortName++;
// utf8_to_utf16((uint16_t*)outbuf, shortName, FILENAMELENGTH*2);
// ((uint16_t*)outbuf)[FILENAMELENGTH-1]=0;
strncpy((char*)outbuf, shortName, 240);
}
spi_writeFlash(outbuf, (slot+side)*SLOTSIZE, SLOTSIZE);
}
pos+=FDSSIZE;
side++;
}
free(inbuf);
free(outbuf);
return true;
}
void hexdump(char *desc, void *addr, int len)
{
int i;
unsigned char buff[17];
unsigned char *pc = (unsigned char *)addr;
// Output description if given.
if (desc != NULL)
printf("%s:\r\n", desc);
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf(" %s\r\n", buff);
// Output the offset.
printf(" %04x ", i);
}
// Now the hex code for the specific character.
printf(" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf(" ");
i++;
}
// And print the final ASCII bit.
printf(" %s\r\n", buff);
}
bool FDS_list() {
uint8_t buf[256];
int side=0;
printf("\n");
for(int slot=1;slot<dev_slots;slot++) {
if(!spi_readFlash((slot)*SLOTSIZE,buf,256))
return false;
if(buf[0]==0xff) { //empty
printf("%d:\n",slot);
side=0;
} else if(buf[0]!=0) { //filename present
printf("%d: %s\n", slot, buf);
side=1;
} else if(!side) { //first side is missing
printf("%d: ?\n", slot);
} else { //next side
printf("%d: Side %d\n", slot, ++side);
}
}
return true;
}
//===============================
//check for gap at EOF
bool looks_like_file_end(uint8_t *raw, int start, int rawSize) {
enum {
MIN_GAP=976-100,
MAX_GAP=976+100,
};
int zeros=0;
int in=start;
for(; in<start+MAX_GAP && in<rawSize; in++) {
if(raw[in]==1 && zeros>MIN_GAP) {
return true;
} else if(raw[in]==0) {
zeros++;
}
if(raw[in]!=0)
zeros=0;
}
return in>=rawSize; //end of disk = end of file!
}
//detect EOF by looking for good CRC. in=start of file
//returns 0 if nothing found
int crc_detect(uint8_t *raw, int in, int rawSize) {
static uint32_t crc;
static uint8_t bitval;
static int out;
static bool match;
//local function ;)
struct { void shift(uint8_t bit) {
crc|=bit<<16;
if(crc & 1) crc ^= 0x10810;
crc>>=1;
bitval=bit;
out++;
if(crc==0 && !(out&7)) //on a byte bounary and CRC is valid
match=true;
}} f;
crc=0x8000;
bitval=1;
out=0;
do {
match=false;
switch(raw[in]|(bitval<<4)) {
case 0x11:
f.shift(0);
case 0x00:
f.shift(0);
break;
case 0x12:
f.shift(0);
case 0x01:
case 0x10:
f.shift(1);
break;
default: //garbage / bad encoding
return 0;
}
in++;
} while(in<rawSize && !(match && looks_like_file_end(raw,in,rawSize)));
return match? in: 0;
}
//gap end is known, backtrack and mark the start. !! this assumes junk data exists between EOF and gap start
static void mark_gap_start(uint8_t *raw, int gapEnd) {
int i;
for(i=gapEnd-1; i>=0 && raw[i]==0; --i)
{ }
raw[i+1]=3;
printf("mark gap %X-%X\n", i+1, gapEnd);
}
//For information only for now. This checks for standard file format
static void verify_block(uint8_t *bin, int start, int *reverse) {
enum { MAX_GAP=(976+100)/8, MIN_GAP=(976-100)/8 };
static const uint8_t next[]={0,2,3,4,3};
static int last = 0;
static int lastLen = 0;
static int blockCount = 0;
int len=0;
uint8_t type=bin[start];
printf("%d:%X", ++blockCount, type);
switch(type) {
case 1:
len=0x38;
break;
case 2:
len=2;
break;
case 3:
len=16;
break;
case 4:
len=1+(bin[last+13] | (bin[last+14]<<8));
break;
default:
printf(" bad block (%X)\n",start);
return;
}
printf(" %X-%X / %X-%X(%X)", reverse[start], reverse[start+len], start, start+len, len);
if((!last && type!=1) || (last && type!=next[bin[last]]))
printf(", wrong filetype");
if(calc_crc(bin+start, len+2)!=0)
printf(", bad CRC");
if(last && (last+lastLen+MAX_GAP)<start)
printf(", lost block?");
if(last+lastLen+MIN_GAP>start)
printf(", block overlap?");
//if(type==3 && ...) //check other fields in file header?
printf("\n");
last=start;
lastLen=len;
}
//find gap + gap end. returns bit following gap end, >=rawSize if not found.
int nextGapEnd(uint8_t *raw, int in, int rawSize) {
enum { MIN_GAP=976-100, };
int zeros=0;
for(; (raw[in]!=1 || zeros<MIN_GAP) && in<rawSize; in++) {
if(raw[in]==0) {
zeros++;
} else {
zeros=0;
}
}
return in+1;
}
/*
Try to create byte-for-byte, unadulterated representation of disk. Use hints from the disk structure, given
that it's probably a standard FDS game image but this should still make a best attempt regardless of the disk content.
_bin and _binSize are updated on exit. alloc'd buffer is returned in _bin, caller is responsible for freeing it.
*/
static void raw03_to_bin(uint8_t *raw, int rawSize, uint8_t **_bin, int *_binSize) {
enum {
BINSIZE=0xa0000,
POST_GLITCH_GARBAGE=16,
LONG_POST_GLITCH_GARBAGE=64,
LONG_GAP=900, //976 typ.
SHORT_GAP=16,
};
int in, out;
uint8_t *bin;
int *reverse;
int glitch;
int zeros;
bin=(uint8_t*)malloc(BINSIZE);
reverse=(int*)malloc(BINSIZE*sizeof(int));
memset(bin,0,BINSIZE);
//--- assume any glitch is OOB, mark a run of zeros near a glitch as a gap start.
int junk=0;
glitch=0;
zeros=0;
junk=0;
for(in=0; in<rawSize; in++) {
if(raw[in]==3) {
glitch=in;
junk=0;
} else if(raw[in]==1 || raw[in]==2) {
junk=in;
} else if(raw[in]==0) {
zeros++;
if(glitch && junk && zeros>SHORT_GAP && (junk-glitch)<POST_GLITCH_GARBAGE) {
mark_gap_start(raw,in);
glitch=0;
}
}
if(raw[in]!=0)
zeros=0;
}
//--- Walk filesystem, mark blocks where something looks like a valid file
in=findFirstBlock(raw);
if(in>0) {
printf("header at %X\n",in);
mark_gap_start(raw, in-1);
}
/*
do {
if(block_decode(..)) {
raw[head]=0xff;
raw[tail]=3;
}
next_gap(..);
} while(..);
*/
//--- Identify files by CRC. If data looks like it's surrounded by gaps and it has a valid CRC where we
// expect one to be, assume it's a file and mark its start/end.
in=findFirstBlock(raw)+1;
if(in>0) do {
out=crc_detect(raw,in,rawSize);
if(out) {
printf("crc found %X-%X\n",in,out);
raw[out]=3; //mark glitch (gap start)
//raw[in-1]=0xff; //mark gap end
}
in=nextGapEnd(raw, out? out: in, rawSize);
} while(in<rawSize);
//--- mark gap start/end using glitches to find gap start
for(glitch=0, zeros=0, in=0; in<rawSize; in++) {
if(raw[in]==3) {
glitch=in;
} else if(raw[in]==1) {
if(zeros>LONG_GAP && (in-zeros-LONG_POST_GLITCH_GARBAGE)<glitch) {
mark_gap_start(raw,in);
raw[in]=0xff;
}
} else if(raw[in]==0) {
zeros++;
}
if(raw[in]!=0)
zeros=0;
}
//--- output
/*
FILE *f=fopen("raw03.bin","wb");
fwrite(raw,1,rawSize,f);
fclose(f);
*/
char bitval=0;
int lastBlockStart=0;
for(in=0, out=0; in<rawSize; in++) {
switch(raw[in]|(bitval<<4)) {
case 0x11:
out++;
case 0x00:
out++;
bitval=0;
break;
case 0x12:
out++;
case 0x01:
case 0x10:
bin[out/8] |= 1<<(out&7);
out++;
bitval=1;
break;
case 0xff: //block end
if(lastBlockStart)
verify_block(bin, lastBlockStart, reverse);
bin[out/8] = 0x80;
out=(out|7)+1; //byte-align for readability
lastBlockStart=out/8;
bitval=1;
break;
case 0x02:
//printf("Encoding error @ %X(%X)\n",in,out/8);
default: //anything else (glitch)
out++;
bitval=0;
break;
}
reverse[out/8]=in;
}
//last block
verify_block(bin, lastBlockStart, reverse);
*_bin=bin;
*_binSize=out/8+1;
free(reverse);
}
/*
bool FDS_rawToBin(char *filename_raw, char *filename_bin) {
FILE *f;
uint8_t *rawBuf=NULL;
uint8_t *binBuf=NULL;
int rawSize=0;
int binSize=0;
if(!loadFile(filename_raw, &rawBuf, &rawSize))
return false;
raw_to_raw03(rawBuf, rawSize);
raw03_to_bin(rawBuf, rawSize, &binBuf, &binSize);
f=fopen(filename_bin, "wb");
fwrite(binBuf, 1, binSize, f);
fclose(f);
printf("Wrote %s\n", filename_bin);
free(binBuf);
free(rawBuf);
return true;
}
*/
// =========================================
//make raw0-3 from flash image (sans header)
static void bin_to_raw03(uint8_t *bin, uint8_t *raw, int binSize, int rawSize) {
int in, out;
uint8_t bit, data;
memset(raw,0xff,rawSize);
for(bit=1, out=0, in=0; in<binSize*8; in++) {
if ((in & 7) == 0) {
data = *bin;
bin++;
}
bit = (bit << 7) | (1 & (data >> (in & 7))); //LSB first
// bit = (bit<<7) | (1 & (bin[in/8]>>(in%8))); //LSB first
switch(bit) {
case 0x00: //10 10