-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.c
executable file
·2986 lines (2696 loc) · 75.3 KB
/
disk.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
/*
* UAE - The Un*x Amiga Emulator
*
* Floppy disk emulation
*
* Copyright 1995 Hannu Rummukainen
* Copyright 1995-2001 Bernd Schmidt
* Copyright 2000-2003 Toni Wilen
*
* High Density Drive Handling by Dr. Adil Temel (C) 2001 [[email protected]]
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "uae.h"
#include "options.h"
#include "memory.h"
#include "events.h"
#include "custom.h"
#include "custom_private.h"
#include "ersatz.h"
#include "disk.h"
#include "gui.h"
#include "zfile.h"
#include "newcpu.h"
#include "osemu.h"
#include "execlib.h"
#include "savestate.h"
#include "cia.h"
#include "debug.h"
#include "driveclick.h"
#include "fsdb.h"
/*
#ifdef CAPS
#ifdef _WIN32
#include "caps/caps_win32.h"
#else
#include "caps.h"
#endif
#endif
*/
#include "crc32.h"
#include <ctype.h>
//ric
#include "profiling.h"
#define PROFILINGINDEX 500
//#undef PROFILING
//end ric
/* support HD floppies */
#define FLOPPY_DRIVE_HD
/* writable track length with normal 2us bitcell/300RPM motor (PAL) */
#ifdef DISK_OPT
#define FLOPPY_WRITE_LEN (12667 / 2)
#define FLOPPY_GAP_LEN (FLOPPY_WRITE_LEN - 11 * 544)
#define NORMAL_FLOPPY_SPEED 1830
#else
#define FLOPPY_WRITE_LEN (currprefs.ntscmode ? (12798 / 2) : (12668 / 2)) /* 12667 PAL, 12797 NTSC */
/* This works out to 350 */
#define FLOPPY_GAP_LEN (FLOPPY_WRITE_LEN - 11 * 544)
/* (cycles/bitcell) << 8, normal = ((2us/280ns)<<8) = ~1830 */
#define NORMAL_FLOPPY_SPEED (currprefs.ntscmode ? 1810 : 1829)
/* max supported floppy drives, for small memory systems */
#endif
//FOL
#define MAX_FLOPPY_DRIVES 4
//FOL
#ifdef FLOPPY_DRIVE_HD
#define DDHDMULT 2
#else
#define DDHDMULT 1
#endif
#undef DEBUG_DRIVE_ID
/* UAE-1ADF (ADF_EXT2)
* W reserved
* W number of tracks (default 2*80=160)
*
* W reserved
* W type, 0=normal AmigaDOS track, 1 = raw MFM
* L available space for track in bytes (must be even)
* L track length in bits
*/
static int side, direction;
static uae_u8 selected = 15, disabled;
static uae_u8 writebuffer[544 * 11 * DDHDMULT];
#define DISK_INDEXSYNC 1
#define DISK_WORDSYNC 2
#define DISK_REVOLUTION 4 /* 4,8,16,32 */
#define DSKREADY_TIME 4
#define DSKREADY_DOWN_TIME 10
#if 0
#define MAX_DISK_WORDS_PER_LINE 50 /* depends on floppy_speed */
static uae_u32 dma_tab[MAX_DISK_WORDS_PER_LINE + 1];
#endif
static int dskdmaen, dsklength, dsklen;
static uae_u16 dskbytr_val;
static uae_u32 dskpt;
static int dma_enable, bitoffset;
static uae_u16 word, dsksync;
/* Always carried through to the next line. */
static unsigned int disk_hpos;
static int disk_jitter;
typedef enum { TRACK_AMIGADOS, TRACK_RAW, TRACK_RAW1 } image_tracktype;
typedef struct {
uae_u16 len;
uae_u32 offs;
unsigned int bitlen, track;
unsigned int sync;
image_tracktype type;
} trackid;
#ifdef DISK_OPT
#define MAX_TRACKS 166
#else
#define MAX_TRACKS (2 * 83)
#endif
/* We have three kinds of Amiga floppy drives
* - internal A500/A2000 drive:
* ID is always DRIVE_ID_NONE (S.T.A.G expects this)
* - HD drive (A3000/A4000):
* ID is DRIVE_ID_35DD if DD floppy is inserted or drive is empty
* ID is DRIVE_ID_35HD if HD floppy is inserted
* - regular external drive:
* ID is always DRIVE_ID_35DD
*/
#define DRIVE_ID_NONE 0x00000000
#define DRIVE_ID_35DD 0xFFFFFFFF
#define DRIVE_ID_35HD 0xAAAAAAAA
#define DRIVE_ID_525SD 0x55555555 /* 40 track 5.25 drive , kickstart does not recognize this */
typedef enum { ADF_NORMAL, ADF_EXT1, ADF_EXT2, ADF_FDI, ADF_IPF, ADF_CATWEASEL } drive_filetype;
typedef struct {
struct zfile *diskfile;
struct zfile *writediskfile;
drive_filetype filetype;
trackid trackdata[MAX_TRACKS];
trackid writetrackdata[MAX_TRACKS];
int buffered_cyl, buffered_side;
unsigned int cyl;
int motoroff;
int state;
int wrprot;
uae_u16 bigmfmbuf[0x4000 * DDHDMULT];
uae_u16 tracktiming[0x4000 * DDHDMULT];
int multi_revolution;
int skipoffset;
unsigned int mfmpos;
unsigned int indexoffset;
unsigned int tracklen;
unsigned int prevtracklen;
int trackspeed;
int dmalen;
unsigned int num_tracks, write_num_tracks, num_secs;
unsigned int hard_num_cyls;
int dskchange;
int dskchange_time;
int dskready;
int dskready_time;
int dskready_down_time;
int steplimit;
frame_time_t steplimitcycle;
int indexhack, indexhackmode;
int ddhd; /* 1=DD 2=HD */
int drive_id_scnt; /* drive id shift counter */
int idbit;
unsigned long drive_id; /* drive id to be reported */
char newname[256]; /* storage space for new filename during eject delay */
uae_u32 crc32;
#ifdef FDI2RAW
FDI *fdi;
#endif
int useturbo;
int floppybitcounter; /* number of bits left */
#ifdef CATWEASEL
catweasel_drive *catweasel;
#else
int catweasel;
#endif
} drive;
int disk_debug_logging;
int disk_debug_mode;
int disk_debug_track = -1;
#ifdef DISK_OPT
#define MIN_STEPLIMIT_CYCLE 76800 //38400
#else
#define MIN_STEPLIMIT_CYCLE (CYCLE_UNIT * 150)
#endif
static uae_u16 bigmfmbufw[0x4000 * DDHDMULT];
#ifndef SAVE_MEMORY
static drive floppy[MAX_FLOPPY_DRIVES];
#else
static drive *floppy;
#endif
#define MAX_PREVIOUS_FLOPPIES 99
#ifndef MAX_DPATH
#define MAX_DPATH 256
#endif
static char dfxhistory[MAX_PREVIOUS_FLOPPIES][MAX_DPATH];
static uae_u8 exeheader[]={0x00,0x00,0x03,0xf3,0x00,0x00,0x00,0x00};
static uae_u8 bootblock[]={
0x44,0x4f,0x53,0x00,0xc0,0x20,0x0f,0x19,0x00,0x00,0x03,0x70,0x43,0xfa,0x00,0x18,
0x4e,0xae,0xff,0xa0,0x4a,0x80,0x67,0x0a,0x20,0x40,0x20,0x68,0x00,0x16,0x70,0x00,
0x4e,0x75,0x70,0xff,0x60,0xfa,0x64,0x6f,0x73,0x2e,0x6c,0x69,0x62,0x72,0x61,0x72,
0x79
};
#define FS_OFS_DATABLOCKSIZE 488
#define FS_FLOPPY_BLOCKSIZE 512
#define FS_EXTENSION_BLOCKS 72
#define FS_FLOPPY_TOTALBLOCKS 1760
#define FS_FLOPPY_RESERVED 2
static void writeimageblock (struct zfile *dst, uae_u8 *sector, int offset)
{
zfile_fseek (dst, offset, SEEK_SET);
zfile_fwrite (sector, FS_FLOPPY_BLOCKSIZE, 1, dst);
}
static void disk_checksum (const uae_u8 *p, uae_u8 *c)
{
uae_u32 cs = 0;
int i;
for (i = 0; i < FS_FLOPPY_BLOCKSIZE; i+= 4) cs += (p[i] << 24) | (p[i+1] << 16) | (p[i+2] << 8) | (p[i+3] << 0);
cs = -cs;
c[0] = cs >> 24; c[1] = cs >> 16; c[2] = cs >> 8; c[3] = cs >> 0;
}
static int dirhash (const char *name)
{
unsigned long hash;
unsigned int i;
hash = strlen (name);
for(i = 0; i < strlen (name); i++) {
hash = hash * 13;
hash = hash + toupper (name[i]);
hash = hash & 0x7ff;
}
hash = hash % ((FS_FLOPPY_BLOCKSIZE / 4) - 56);
return hash;
}
static void disk_date (uae_u8 *p)
{
time_t t;
struct tm *today;
int year, days, minutes, ticks;
char tmp[10];
time (&t);
today = localtime( &t );
strftime (tmp, sizeof(tmp), "%Y", today);
year = atol (tmp);
strftime (tmp, sizeof(tmp), "%j", today);
days = atol (tmp) - 1;
strftime (tmp, sizeof(tmp), "%H", today);
minutes = atol (tmp) * 60;
strftime (tmp, sizeof(tmp), "%M", today);
minutes += atol (tmp);
strftime (tmp, sizeof(tmp), "%S", today);
ticks = atol (tmp) * 50;
while (year > 1978) {
if ( !(year % 100) ? !(year % 400) : !(year % 4) ) days++;
days += 365;
year--;
}
p[0] = days >> 24; p[1] = days >> 16; p[2] = days >> 8; p[3] = days >> 0;
p[4] = minutes >> 24; p[5] = minutes >> 16; p[6] = minutes >> 8; p[7] = minutes >> 0;
p[8] = ticks >> 24; p[9] = ticks >> 16; p[10] = ticks >> 8; p[11] = ticks >> 0;
}
static void createbootblock (uae_u8 *sector, int bootable)
{
memset (sector, 0, FS_FLOPPY_BLOCKSIZE);
memcpy (sector, "DOS", 3);
if (bootable)
memcpy (sector, bootblock, sizeof(bootblock));
}
static void createrootblock (uae_u8 *sector, const char *disk_name)
{
memset (sector, 0, FS_FLOPPY_BLOCKSIZE);
sector[0+3] = 2;
sector[12+3] = 0x48;
sector[312] = sector[313] = sector[314] = sector[315] = (uae_u8)0xff;
sector[316+2] = 881 >> 8; sector[316+3] = 881 & 255;
sector[432] = strlen (disk_name);
strcpy ((char *)sector + 433, disk_name);
sector[508 + 3] = 1;
disk_date (sector + 420);
memcpy (sector + 472, sector + 420, 3 * 4);
memcpy (sector + 484, sector + 420, 3 * 4);
}
static int getblock (uae_u8 *bitmap)
{
int i = 0;
while (bitmap[i] != 0xff) {
if (bitmap[i] == 0) {
bitmap[i] = 1;
return i;
}
i++;
}
return -1;
}
static void pl (uae_u8 *sector, int offset, uae_u32 v)
{
sector[offset + 0] = v >> 24;
sector[offset + 1] = v >> 16;
sector[offset + 2] = v >> 8;
sector[offset + 3] = v >> 0;
}
static int createdirheaderblock (uae_u8 *sector, int parent, const char *filename, uae_u8 *bitmap)
{
int block = getblock (bitmap);
memset (sector, 0, FS_FLOPPY_BLOCKSIZE);
pl (sector, 0, 2);
pl (sector, 4, block);
disk_date (sector + 512 - 92);
sector[512 - 80] = strlen (filename);
strcpy ((char *)sector + 512 - 79, filename);
pl (sector, 512 - 12, parent);
pl (sector, 512 - 4, 2);
return block;
}
static int createfileheaderblock (struct zfile *z,uae_u8 *sector, int parent, const char *filename, struct zfile *src, uae_u8 *bitmap)
{
uae_u8 sector2[FS_FLOPPY_BLOCKSIZE];
uae_u8 sector3[FS_FLOPPY_BLOCKSIZE];
int block = getblock (bitmap);
int datablock = getblock (bitmap);
int datasec = 1;
int extensions;
int extensionblock, extensioncounter;
int size;
zfile_fseek (src, 0, SEEK_END);
size = zfile_ftell (src);
zfile_fseek (src, 0, SEEK_SET);
extensions = (size + FS_OFS_DATABLOCKSIZE - 1) / FS_OFS_DATABLOCKSIZE;
memset (sector, 0, FS_FLOPPY_BLOCKSIZE);
pl (sector, 0, 2);
pl (sector, 4, block);
pl (sector, 8, extensions > FS_EXTENSION_BLOCKS ? FS_EXTENSION_BLOCKS : extensions);
pl (sector, 16, datablock);
pl (sector, FS_FLOPPY_BLOCKSIZE - 188, size);
disk_date (sector + FS_FLOPPY_BLOCKSIZE - 92);
sector[FS_FLOPPY_BLOCKSIZE - 80] = strlen (filename);
strcpy ((char *)sector + FS_FLOPPY_BLOCKSIZE - 79, filename);
pl (sector, FS_FLOPPY_BLOCKSIZE - 12, parent);
pl (sector, FS_FLOPPY_BLOCKSIZE - 4, -3);
extensioncounter = 0;
extensionblock = 0;
while (size > 0) {
int datablock2 = datablock;
int extensionblock2 = extensionblock;
if (extensioncounter == FS_EXTENSION_BLOCKS) {
extensioncounter = 0;
extensionblock = getblock (bitmap);
if (datasec > FS_EXTENSION_BLOCKS + 1) {
pl (sector3, 8, FS_EXTENSION_BLOCKS);
pl (sector3, FS_FLOPPY_BLOCKSIZE - 8, extensionblock);
pl (sector3, 4, extensionblock2);
disk_checksum(sector3, sector3 + 20);
writeimageblock (z, sector3, extensionblock2 * FS_FLOPPY_BLOCKSIZE);
} else {
pl (sector, 512 - 8, extensionblock);
}
memset (sector3, 0, FS_FLOPPY_BLOCKSIZE);
pl (sector3, 0, 16);
pl (sector3, FS_FLOPPY_BLOCKSIZE - 12, block);
pl (sector3, FS_FLOPPY_BLOCKSIZE - 4, -3);
}
memset (sector2, 0, FS_FLOPPY_BLOCKSIZE);
pl (sector2, 0, 8);
pl (sector2, 4, block);
pl (sector2, 8, datasec++);
pl (sector2, 12, size > FS_OFS_DATABLOCKSIZE ? FS_OFS_DATABLOCKSIZE : size);
zfile_fread (sector2 + 24, size > FS_OFS_DATABLOCKSIZE ? FS_OFS_DATABLOCKSIZE : size, 1, src);
size -= FS_OFS_DATABLOCKSIZE;
datablock = 0;
if (size > 0) datablock = getblock (bitmap);
pl (sector2, 16, datablock);
disk_checksum(sector2, sector2 + 20);
writeimageblock (z, sector2, datablock2 * FS_FLOPPY_BLOCKSIZE);
if (datasec <= FS_EXTENSION_BLOCKS + 1)
pl (sector, 512 - 204 - extensioncounter * 4, datablock2);
else
pl (sector3, 512 - 204 - extensioncounter * 4, datablock2);
extensioncounter++;
}
if (datasec > FS_EXTENSION_BLOCKS) {
pl (sector3, 8, extensioncounter);
disk_checksum(sector3, sector3 + 20);
writeimageblock (z, sector3, extensionblock * FS_FLOPPY_BLOCKSIZE);
}
disk_checksum(sector, sector + 20);
writeimageblock (z, sector, block * FS_FLOPPY_BLOCKSIZE);
return block;
}
static void createbitmapblock (uae_u8 *sector, uae_u8 *bitmap)
{
uae_u8 mask;
int i, j;
memset (sector, 0, FS_FLOPPY_BLOCKSIZE);
for (i = FS_FLOPPY_RESERVED; i < FS_FLOPPY_TOTALBLOCKS; i += 8) {
mask = 0;
for (j = 0; j < 8; j++) {
if (bitmap[i + j]) mask |= 1 << j;
}
sector[4 + i / 8] = mask;
}
disk_checksum(sector, sector + 0);
}
static int createimagefromexe (struct zfile *src, struct zfile *dst)
{
uae_u8 sector1[FS_FLOPPY_BLOCKSIZE], sector2[FS_FLOPPY_BLOCKSIZE];
uae_u8 bitmap[FS_FLOPPY_TOTALBLOCKS + 8];
int exesize;
int blocksize = FS_OFS_DATABLOCKSIZE;
int blocks, extensionblocks;
int totalblocks;
int fblock1, dblock1;
const char *fname1 = "runme.exe";
const char *fname2 = "startup-sequence";
const char *dirname1 = "s";
struct zfile *ss;
memset (bitmap, 0, sizeof (bitmap));
zfile_fseek (src, 0, SEEK_END);
exesize = zfile_ftell (src);
blocks = (exesize + blocksize - 1) / blocksize;
extensionblocks = (blocks + FS_EXTENSION_BLOCKS - 1) / FS_EXTENSION_BLOCKS;
/* bootblock=2, root=1, bitmap=1, startup-sequence=1+1, exefileheader=1 */
totalblocks = 2 + 1 + 1 + 2 + 1 + blocks + extensionblocks;
if (totalblocks > FS_FLOPPY_TOTALBLOCKS)
return 0;
bitmap[880] = 1;
bitmap[881] = 1;
bitmap[0] = 1;
bitmap[1] = 1;
dblock1 = createdirheaderblock (sector2, 880, dirname1, bitmap);
ss = zfile_fopen_empty (fname1, strlen(fname1));
zfile_fwrite (fname1, strlen(fname1), 1, ss);
fblock1 = createfileheaderblock (dst, sector1, dblock1, fname2, ss, bitmap);
zfile_fclose (ss);
pl (sector2, 24 + dirhash (fname2) * 4, fblock1);
disk_checksum(sector2, sector2 + 20);
writeimageblock (dst, sector2, dblock1 * FS_FLOPPY_BLOCKSIZE);
fblock1 = createfileheaderblock (dst, sector1, 880, fname1, src, bitmap);
createrootblock (sector1, "empty");
pl (sector1, 24 + dirhash (fname1) * 4, fblock1);
pl (sector1, 24 + dirhash (dirname1) * 4, dblock1);
disk_checksum(sector1, sector1 + 20);
writeimageblock (dst, sector1, 880 * FS_FLOPPY_BLOCKSIZE);
createbitmapblock (sector1, bitmap);
writeimageblock (dst, sector1, 881 * FS_FLOPPY_BLOCKSIZE);
createbootblock (sector1, 1);
writeimageblock (dst, sector1, 0 * FS_FLOPPY_BLOCKSIZE);
return 1;
}
static int get_floppy_speed (void)
{
int m = currprefs.floppy_speed;
if (m <= 10)
m = 100;
m = NORMAL_FLOPPY_SPEED * 100 / m;
return m;
}
#ifdef DEBUG_DRIVE_ID
static const char *drive_id_name (drive *drv)
{
switch (drv->drive_id) {
case DRIVE_ID_35HD : return "3.5HD";
case DRIVE_ID_525SD: return "5.25SD";
case DRIVE_ID_35DD : return "3.5DD";
}
return "UNKNOWN";
}
#endif
/* Simulate exact behaviour of an A3000T 3.5 HD disk drive.
* The drive reports to be a 3.5 DD drive whenever there is no
* disk or a 3.5 DD disk is inserted. Only 3.5 HD drive id is reported
* when a real 3.5 HD disk is inserted. -Adil
*/
static void drive_settype_id (drive *drv)
{
int t = currprefs.dfxtype[drv - &floppy[0]];
switch (t) {
case DRV_35_HD:
#ifdef FLOPPY_DRIVE_HD
if (!drv->diskfile || drv->ddhd <= 1)
drv->drive_id = DRIVE_ID_35DD;
else
drv->drive_id = DRIVE_ID_35HD;
#else
drv->drive_id = DRIVE_ID_35DD;
#endif
break;
case DRV_35_DD:
default:
drv->drive_id = DRIVE_ID_35DD;
break;
case DRV_525_SD:
drv->drive_id = DRIVE_ID_525SD;
break;
}
#ifdef DEBUG_DRIVE_ID
write_log ("drive_settype_id: DF%d: set to %s\n", drv-floppy, drive_id_name (drv));
#endif
}
static void drive_image_free (drive *drv)
{
switch (drv->filetype) {
case ADF_IPF:
/*
#ifdef CAPS
caps_unloadimage (drv - floppy);
#endif
*/
break;
case ADF_FDI:
#ifdef FDI2RAW
fdi2raw_header_free (drv->fdi);
drv->fdi = 0;
#endif
break;
default:
break;
}
drv->filetype = -1;
zfile_fclose (drv->diskfile);
drv->diskfile = 0;
zfile_fclose (drv->writediskfile);
drv->writediskfile = 0;
}
static int drive_insert (drive * drv, struct uae_prefs *p, int dnum, const char *fname);
static void reset_drive (int i)
{
drive *drv = &floppy[i];
drive_image_free (drv);
drv->motoroff = 1;
disabled &= ~(1 << i);
gui_data.drive_disabled[i] = 0;
gui_data.df[i][0] = 0;
gui_data.crc32[i] = 0;
if (currprefs.dfxtype[i] < 0) {
disabled |= 1 << i;
gui_data.drive_disabled[i] = 1;
}
/* most internal Amiga floppy drives won't enable
* diskready until motor is running at full speed
* and next indexsync has been passed
*/
drv->indexhackmode = 0;
if (i == 0 && currprefs.dfxtype[i] == 0)
drv->indexhackmode = 1;
drv->dskchange_time = 0;
drv->buffered_cyl = -1;
drv->buffered_side = -1;
gui_led (i + 1, 0);
drive_settype_id (drv);
if (strlen (drv->newname) > 0)
strcpy (currprefs.df[i], drv->newname);
if (!drive_insert (drv, &currprefs, i, currprefs.df[i]))
disk_eject (i);
}
/* code for track display */
static void update_drive_gui (int num)
{
drive *drv = floppy + num;
int writ = dskdmaen == 3 && drv->state ? 1 : 0;
if (drv->state == gui_data.drive_motor[num]
&& drv->cyl == gui_data.drive_track[num]
&& side == gui_data.drive_side
&& drv->crc32 == gui_data.crc32[num]
&& writ == gui_data.drive_writing[num])
return;
strcpy (gui_data.df[num], currprefs.df[num]);
gui_data.crc32[num] = drv->crc32;
gui_data.drive_motor[num] = drv->state;
gui_data.drive_track[num] = drv->cyl;
gui_data.drive_side = side;
gui_data.drive_writing[num] = writ;
gui_ledstate &= ~(2 << num);
if (drv->state)
gui_ledstate |= 2 << num;
gui_led (num + 1, gui_data.drive_motor[num]);
}
static void drive_fill_bigbuf (drive * drv,int);
struct zfile *DISK_validate_filename (const char *fname, int leave_open, int *wrprot, uae_u32 *crc32)
{
if (crc32)
*crc32 = 0;
if (leave_open) {
struct zfile *f = zfile_fopen (fname, "r+b");
if (f) {
if (wrprot)
*wrprot = 0;
} else {
if (wrprot)
*wrprot = 1;
f = zfile_fopen (fname, "rb");
}
if (f && crc32)
*crc32 = zfile_crc32 (f);
return f;
} else {
if (zfile_exists (fname)) {
if (wrprot)
*wrprot = 0;
if (crc32) {
struct zfile *f = zfile_fopen (fname, "rb");
if (f)
*crc32 = zfile_crc32 (f);
zfile_fclose (f);
}
return (void*)1;
} else {
if (wrprot)
*wrprot = 1;
return 0;
}
}
}
static void updatemfmpos (drive *drv)
{
if (drv->prevtracklen)
drv->mfmpos = drv->mfmpos * (drv->tracklen * 1000 / drv->prevtracklen) / 1000;
drv->mfmpos %= drv->tracklen;
drv->prevtracklen = drv->tracklen;
}
static void track_reset (drive *drv)
{
drv->tracklen = FLOPPY_WRITE_LEN * drv->ddhd * 2 * 8;
drv->trackspeed = get_floppy_speed ();
drv->buffered_side = -1;
drv->skipoffset = -1;
drv->tracktiming[0] = 0;
memset (drv->bigmfmbuf, 0xaa, FLOPPY_WRITE_LEN * 2 * drv->ddhd);
updatemfmpos (drv);
}
static unsigned int read_header_ext2 (struct zfile *diskfile, trackid *trackdata, unsigned int *num_tracks, int *ddhd)
{
uae_u8 buffer[2 + 2 + 4 + 4];
trackid *tid;
unsigned int offs;
unsigned int i;
zfile_fseek (diskfile, 0, SEEK_SET);
zfile_fread (buffer, 1, 8, diskfile);
if (strncmp ((char*)buffer, "UAE-1ADF", 8))
return 0;
zfile_fread (buffer, 1, 4, diskfile);
*num_tracks = buffer[2] * 256 + buffer[3];
offs = 8 + 2 + 2 + (*num_tracks) * (2 + 2 + 4 + 4);
for (i = 0; i < (*num_tracks); i++) {
tid = trackdata + i;
zfile_fread (buffer, 2 + 2 + 4 + 4, 1, diskfile);
tid->type = buffer[2] * 256 + buffer[3];
tid->len = buffer[5] * 65536 + buffer[6] * 256 + buffer[7];
tid->bitlen = buffer[9] * 65536 + buffer[10] * 256 + buffer[11];
tid->offs = offs;
if (tid->len > 20000 && ddhd)
*ddhd = 2;
tid->track = i;
offs += tid->len;
}
return 1;
}
#ifdef SUPPORT_SAVE_IMAGES
static char *getwritefilename (const char *name)
{
static char name1[MAX_DPATH];
char name2[MAX_DPATH];
char path[MAX_DPATH];
int i;
strcpy (name2, name);
i = strlen (name2) - 1;
while (i > 0) {
if (name2[i] == '.') {
name2[i] = 0;
break;
}
i--;
}
while (i > 0) {
if (name2[i] == '/' || name2[i] == '\\'
#ifdef TARGET_AMIGAOS
|| name2[i] == ':'
#endif
) {
i++;
break;
}
i--;
}
fetch_saveimagepath (path, sizeof (path), 1);
sprintf (name1, "%s%s_save.adf", path, name2 + i);
return name1;
}
static struct zfile *getwritefile (const char *name, int *wrprot)
{
return DISK_validate_filename (DISK_get_saveimagepath (name), 1, wrprot, NULL);
}
static int iswritefileempty (const char *name)
{
struct zfile *zf;
int wrprot;
uae_u8 buffer[8];
trackid td[MAX_TRACKS];
unsigned int tracks, ddhd, i, ret;
zf = getwritefile (name, &wrprot);
if (!zf) return 1;
zfile_fread (buffer, sizeof (char), 8, zf);
if (strncmp ((char *) buffer, "UAE-1ADF", 8))
return 0;
ret = read_header_ext2 (zf, td, &tracks, &ddhd);
zfile_fclose (zf);
if (!ret)
return 1;
for (i = 0; i < tracks; i++) {
if (td[i].bitlen) return 0;
}
return 1;
}
static int openwritefile (drive *drv, int create)
{
int wrprot = 0;
drv->writediskfile = getwritefile (currprefs.df[drv - &floppy[0]], &wrprot);
if (drv->writediskfile) {
drv->wrprot = wrprot;
if (!read_header_ext2 (drv->writediskfile, drv->writetrackdata, &drv->write_num_tracks, 0)) {
zfile_fclose (drv->writediskfile);
drv->writediskfile = 0;
drv->wrprot = 1;
} else {
if (drv->write_num_tracks > drv->num_tracks)
drv->num_tracks = drv->write_num_tracks;
}
} else if (zfile_iscompressed (drv->diskfile)) {
drv->wrprot = 1;
}
return drv->writediskfile ? 1 : 0;
}
#endif
static int diskfile_iswriteprotect (const char *fname, int *needwritefile, drive_type *drvtype)
{
struct zfile *zf1, *zf2;
int wrprot1 = 0, wrprot2 = 1;
unsigned char buffer[25];
*needwritefile = 0;
*drvtype = DRV_35_DD;
zf1 = DISK_validate_filename (fname, 1, &wrprot1, NULL);
if (!zf1)
return 1;
if (zfile_iscompressed (zf1)) {
wrprot1 = 1;
*needwritefile = 1;
}
#ifdef SUPPORT_SAVE_IMAGES
zf2 = getwritefile (fname, &wrprot2);
zfile_fclose (zf2);
zfile_fread (buffer, sizeof (char), 25, zf1);
zfile_fclose (zf1);
#endif
if (strncmp ((char *) buffer, "CAPS", 4) == 0) {
*needwritefile = 1;
return wrprot2;
}
if (strncmp ((char *) buffer, "Formatted Disk Image file", 25) == 0) {
*needwritefile = 1;
return wrprot2;
}
if (strncmp ((char *) buffer, "UAE-1ADF", 8) == 0) {
if (wrprot1)
return wrprot2;
return wrprot1;
}
if (strncmp ((char *) buffer, "UAE--ADF", 8) == 0) {
*needwritefile = 1;
return wrprot2;
}
if (memcmp (exeheader, buffer, sizeof(exeheader)) == 0)
return 0;
if (wrprot1)
return wrprot2;
return wrprot1;
}
static int drive_insert (drive * drv, struct uae_prefs *p, int dnum, const char *fname)
{
unsigned char buffer[2 + 2 + 4 + 4];
trackid *tid;
drive_image_free (drv);
drv->diskfile = DISK_validate_filename (fname, 1, &drv->wrprot, &drv->crc32);
drv->ddhd = 1;
drv->num_secs = 0;
drv->hard_num_cyls = p->dfxtype[dnum] == DRV_525_SD ? 40 : 80;
drv->tracktiming[0] = 0;
drv->useturbo = 0;
drv->indexoffset = 0;
if (!drv->motoroff) {
drv->dskready_time = DSKREADY_TIME;
drv->dskready_down_time = 0;
}
if (drv->diskfile == 0 && !drv->catweasel) {
track_reset (drv);
return 0;
}
strncpy (currprefs.df[dnum], fname, 255);
currprefs.df[dnum][255] = 0;
strncpy (changed_prefs.df[dnum], fname, 255);
changed_prefs.df[dnum][255] = 0;
strcpy (drv->newname, fname);
gui_filename (dnum, fname);
memset (buffer, 0, sizeof (buffer));
if (drv->diskfile)
zfile_fread (buffer, sizeof (char), 8, drv->diskfile);
if (drv->catweasel) {
drv->wrprot = 1;
drv->filetype = ADF_CATWEASEL;
drv->num_tracks = 80;
drv->ddhd = 1;
/*
#ifdef CAPS
} else if (strncmp ((char *) buffer, "CAPS", 4) == 0) {
unsigned int num_tracks;
drv->wrprot = 1;
if (!caps_loadimage (drv->diskfile, drv - floppy, &num_tracks)) {
zfile_fclose (drv->diskfile);
drv->diskfile = 0;
return 0;
}
drv->num_tracks = num_tracks;
drv->filetype = ADF_IPF;
#endif
*/
#ifdef FDI2RAW
} else if ((drv->fdi = fdi2raw_header (drv->diskfile))) {
drv->wrprot = 1;
drv->num_tracks = fdi2raw_get_last_track (drv->fdi);
drv->num_secs = fdi2raw_get_num_sector (drv->fdi);
drv->filetype = ADF_FDI;
#endif
} else if (strncmp ((char *) buffer, "UAE-1ADF", 8) == 0) {
read_header_ext2 (drv->diskfile, drv->trackdata, &drv->num_tracks, &drv->ddhd);
drv->filetype = ADF_EXT2;
drv->num_secs = 11;
if (drv->ddhd > 1)
drv->num_secs = 22;
} else if (strncmp ((char *) buffer, "UAE--ADF", 8) == 0) {
int offs = 160 * 4 + 8;
int i;
drv->wrprot = 1;
drv->filetype = ADF_EXT1;
drv->num_tracks = 160;
drv->num_secs = 11;
for (i = 0; i < 160; i++) {
tid = &drv->trackdata[i];
zfile_fread (buffer, 4, 1, drv->diskfile);
tid->sync = buffer[0] * 256 + buffer[1];
tid->len = buffer[2] * 256 + buffer[3];
tid->offs = offs;
if (tid->sync == 0) {
tid->type = TRACK_AMIGADOS;
tid->bitlen = 0;
} else {
tid->type = TRACK_RAW1;
tid->bitlen = tid->len * 8;
}
offs += tid->len;
}
} else if (memcmp (exeheader, buffer, sizeof(exeheader)) == 0) {
unsigned int i;
struct zfile *z = zfile_fopen_empty ("", 512 * 1760);
createimagefromexe (drv->diskfile, z);
drv->filetype = ADF_NORMAL;
zfile_fclose (drv->diskfile);
drv->diskfile = z;
drv->num_tracks = 160;
drv->num_secs = 11;
for (i = 0; i < drv->num_tracks; i++) {
tid = &drv->trackdata[i];
tid->type = TRACK_AMIGADOS;
tid->len = 512 * drv->num_secs;
tid->bitlen = 0;
tid->offs = i * 512 * drv->num_secs;
}
drv->useturbo = 1;
} else {
unsigned int i;
drv->filetype = ADF_NORMAL;
zfile_fseek (drv->diskfile, 0, SEEK_END);
i = zfile_ftell (drv->diskfile);
zfile_fseek (drv->diskfile, 0, SEEK_SET);
/* High-density disk? */
if (i >= 160 * 22 * 512) {
drv->num_tracks = i / (512 * (drv->num_secs = 22));
drv->ddhd = 2;
} else
drv->num_tracks = i / (512 * (drv->num_secs = 11));
if (drv->num_tracks > MAX_TRACKS)
write_log ("Your diskfile is too big!\n");
for (i = 0; i < drv->num_tracks; i++) {
tid = &drv->trackdata[i];
tid->type = TRACK_AMIGADOS;
tid->len = 512 * drv->num_secs;
tid->bitlen = 0;
tid->offs = i * 512 * drv->num_secs;
}
}
#if SUPPORT_SAVE_IMAGES
openwritefile (drv, 0);