forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk.cpp
5084 lines (4659 loc) · 131 KB
/
disk.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
/*
* UAE - The Un*x Amiga Emulator
*
* Floppy disk emulation
*
* Copyright 1995 Hannu Rummukainen
* Copyright 1995-2001 Bernd Schmidt
* Copyright 2000-2003 Toni Wilen
*
* Original High Density Drive Handling by Dr. Adil Temel (C) 2001 [[email protected]]
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
int disk_debug_logging = 0;
int disk_debug_mode = 0;
int disk_debug_track = -1;
#define REVOLUTION_DEBUG 0
#define MFM_VALIDATOR 0
#include "uae.h"
#include "options.h"
#include "memory.h"
#include "events.h"
#include "custom.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"
#ifdef FDI2RAW
#include "fdi2raw.h"
#endif
#include "catweasel.h"
#include "driveclick.h"
#ifdef CAPS
#include "caps/caps_win32.h"
#endif
#ifdef SCP
#include "scp.h"
#endif
#include "crc32.h"
#include "inputrecord.h"
#include "amax.h"
#ifdef RETROPLATFORM
#include "rp.h"
#endif
#include "fsdb.h"
#include "statusline.h"
#include "rommgr.h"
#undef CATWEASEL
int floppy_writemode = 0;
/* support HD floppies */
#define FLOPPY_DRIVE_HD
/* writable track length with normal 2us bitcell/300RPM motor, 12667 PAL, 12797 NTSC */
#define FLOPPY_WRITE_LEN_PAL 12668
#define FLOPPY_WRITE_LEN_NTSC 12798
#define FLOPPY_WRITE_LEN (currprefs.floppy_write_length > 256 ? currprefs.floppy_write_length / 2 : (currprefs.ntscmode ? (FLOPPY_WRITE_LEN_NTSC / 2) : (FLOPPY_WRITE_LEN_PAL / 2)))
#define FLOPPY_WRITE_MAXLEN 0x3800
/* This works out to 350 */
#define FLOPPY_GAP_LEN (FLOPPY_WRITE_LEN - 11 * 544)
/* (cycles/bitcell) << 8, normal = ((2us/280ns)<<8) = ~1828.5714 */
#define NORMAL_FLOPPY_SPEED (currprefs.ntscmode ? 1812 : 1829)
/* max supported floppy drives, for small memory systems */
#define MAX_FLOPPY_DRIVES 4
#ifdef FLOPPY_DRIVE_HD
#define DDHDMULT 2
#else
#define DDHDMULT 1
#endif
#define MAX_SECTORS (DDHDMULT * 11)
#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 (upper byte = disk revolutions - 1)
* L available space for track in bytes (must be even)
* L track length in bits
*/
static int side, direction, reserved_side;
static uae_u8 selected = 15, disabled, reserved;
static uae_u8 writebuffer[544 * MAX_SECTORS];
static uae_u8 writesecheadbuffer[16 * MAX_SECTORS];
#define DISK_INDEXSYNC 1
#define DISK_WORDSYNC 2
#define DISK_REVOLUTION 4 /* 8,16,32,64 */
#define DSKREADY_UP_TIME 18
#define DSKREADY_DOWN_TIME 24
#define DSKDMA_OFF 0
#define DSKDMA_INIT 1
#define DSKDMA_READ 2
#define DSKDMA_WRITE 3
static int dskdmaen, dsklength, dsklength2, dsklen;
static uae_u16 dskbytr_val;
static uae_u32 dskpt;
static bool fifo_filled;
static uae_u16 fifo[3];
static int fifo_inuse[3];
static int dma_enable, bitoffset, syncoffset;
static uae_u16 word, dsksync;
static unsigned long dsksync_cycles;
#define WORDSYNC_TIME 11
/* Always carried through to the next line. */
int disk_hpos;
static int disk_jitter;
static int indexdecay;
static uae_u8 prev_data;
static int prev_step;
static bool initial_disk_statusline;
static struct diskinfo disk_info_data = { 0 };
static bool amax_enabled;
typedef enum { TRACK_AMIGADOS, TRACK_RAW, TRACK_RAW1, TRACK_PCDOS, TRACK_DISKSPARE, TRACK_NONE } image_tracktype;
typedef struct {
uae_u16 len;
int offs, extraoffs;
int bitlen, track;
uae_u16 sync;
image_tracktype type;
int revolutions;
} trackid;
#define MAX_TRACKS (2 * 83)
/* 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_NONE = -1, ADF_NORMAL, ADF_EXT1, ADF_EXT2, ADF_FDI, ADF_IPF, ADF_SCP, ADF_CATWEASEL, ADF_PCDOS, ADF_KICK, ADF_SKICK, ADF_NORMAL_HEADER } drive_filetype;
typedef struct {
struct zfile *diskfile;
struct zfile *writediskfile;
struct zfile *pcdecodedfile;
drive_filetype filetype;
trackid trackdata[MAX_TRACKS];
trackid writetrackdata[MAX_TRACKS];
int buffered_cyl, buffered_side;
int cyl;
bool motoroff;
int motordelay; /* dskrdy needs some clock cycles before it changes after switching off motor */
bool state;
int selected_delay;
bool wrprot;
bool forcedwrprot;
uae_u16 bigmfmbuf[0x4000 * DDHDMULT];
uae_u16 tracktiming[0x4000 * DDHDMULT];
int multi_revolution;
int revolution_check;
int skipoffset;
int mfmpos;
int indexoffset;
int tracklen;
int revolutions;
int prevtracklen;
int trackspeed;
int num_tracks, write_num_tracks, num_secs, num_heads;
int hard_num_cyls;
bool dskeject;
bool dskchange;
int dskchange_time;
bool dskready;
int dskready_up_time;
int dskready_down_time;
int writtento;
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 */
TCHAR newname[256]; /* storage space for new filename during eject delay */
bool newnamewriteprotected;
uae_u32 crc32;
#ifdef FDI2RAW
FDI *fdi;
#endif
int useturbo;
int floppybitcounter; /* number of bits left */
#ifdef CATWEASEL
catweasel_drive *catweasel;
#else
int catweasel;
int amax;
int lastdataacesstrack;
int lastrev;
bool track_access_done;
#endif
} drive;
#define MIN_STEPLIMIT_CYCLE (CYCLE_UNIT * 140)
static uae_u16 bigmfmbufw[0x4000 * DDHDMULT];
static drive floppy[MAX_FLOPPY_DRIVES];
static TCHAR dfxhistory[HISTORY_MAX][MAX_PREVIOUS_IMAGES][MAX_DPATH];
static uae_u8 exeheader[]={0x00,0x00,0x03,0xf3,0x00,0x00,0x00,0x00};
static uae_u8 bootblock_ofs[]={
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
};
static uae_u8 bootblock_ffs[]={
0x44, 0x4F, 0x53, 0x01, 0xE3, 0x3D, 0x0E, 0x72, 0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x3E,
0x70, 0x25, 0x4E, 0xAE, 0xFD, 0xD8, 0x4A, 0x80, 0x67, 0x0C, 0x22, 0x40, 0x08, 0xE9, 0x00, 0x06,
0x00, 0x22, 0x4E, 0xAE, 0xFE, 0x62, 0x43, 0xFA, 0x00, 0x18, 0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80,
0x67, 0x0A, 0x20, 0x40, 0x20, 0x68, 0x00, 0x16, 0x70, 0x00, 0x4E, 0x75, 0x70, 0xFF, 0x4E, 0x75,
0x64, 0x6F, 0x73, 0x2E, 0x6C, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x00, 0x65, 0x78, 0x70, 0x61,
0x6E, 0x73, 0x69, 0x6F, 0x6E, 0x2E, 0x6C, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x00, 0x00, 0x00,
};
#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 uae_u32 disk_checksum (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;
if (c) {
c[0] = cs >> 24; c[1] = cs >> 16; c[2] = cs >> 8; c[3] = cs >> 0;
}
return cs;
}
static int dirhash (const uae_char *name)
{
unsigned long hash;
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)
{
static int pdays, pmins, pticks;
int days, mins, ticks;
struct timeval tv;
struct mytimeval mtv;
gettimeofday (&tv, NULL);
tv.tv_sec -= _timezone;
mtv.tv_sec = tv.tv_sec;
mtv.tv_usec = tv.tv_usec;
timeval_to_amiga (&mtv, &days, &mins, &ticks, 50);
if (days == pdays && mins == pmins && ticks == pticks) {
ticks++;
if (ticks >= 50 * 60) {
ticks = 0;
mins++;
if (mins >= 24 * 60)
days++;
}
}
pdays = days;
pmins = mins;
pticks = ticks;
p[0] = days >> 24; p[1] = days >> 16; p[2] = days >> 8; p[3] = days >> 0;
p[4] = mins >> 24; p[5] = mins >> 16; p[6] = mins >> 8; p[7] = mins >> 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_ofs, sizeof bootblock_ofs);
}
static void createrootblock (uae_u8 *sector, const TCHAR *disk_name)
{
char *dn = ua (disk_name);
if (strlen (dn) >= 30)
dn[30] = 0;
const char *dn2 = dn;
if (dn2[0] == 0)
dn2 = "empty";
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 (dn2);
strcpy ((char*)sector + 433, dn2);
sector[508 + 3] = 1;
disk_date (sector + 420);
memcpy (sector + 472, sector + 420, 3 * 4);
memcpy (sector + 484, sector + 420, 3 * 4);
xfree (dn);
}
static int getblock (uae_u8 *bitmap, int *prev)
{
int i = *prev;
while (bitmap[i] != 0xff) {
if (bitmap[i] == 0) {
bitmap[i] = 1;
*prev = i;
return i;
}
i++;
}
i = 0;
while (bitmap[i] != 0xff) {
if (bitmap[i] == 0) {
bitmap[i] = 1;
*prev = i;
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 *prevblock)
{
int block = getblock (bitmap, prevblock);
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, int *prevblock)
{
uae_u8 sector2[FS_FLOPPY_BLOCKSIZE];
uae_u8 sector3[FS_FLOPPY_BLOCKSIZE];
int block = getblock (bitmap, prevblock);
int datablock = getblock (bitmap, prevblock);
int datasec = 1;
int extensions;
int extensionblock, extensioncounter, headerextension = 1;
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, prevblock);
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, prevblock);
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)
{
int i, j;
memset (sector, 0, FS_FLOPPY_BLOCKSIZE);
i = 0;
for (;;) {
uae_u32 mask = 0;
for (j = 0; j < 32; j++) {
if (bitmap[2 + i * 32 + j] == 0xff)
break;
if (!bitmap[2 + i * 32 + j])
mask |= 1 << j;
}
sector[4 + i * 4 + 0] = mask >> 24;
sector[4 + i * 4 + 1] = mask >> 16;
sector[4 + i * 4 + 2] = mask >> 8;
sector[4 + i * 4 + 3] = mask >> 0;
if (bitmap[2 + i * 32 + j] == 0xff)
break;
i++;
}
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 TCHAR *fname1b = _T("runme.adf");
const char *fname2 = "startup-sequence";
const char *dirname1 = "s";
struct zfile *ss;
int prevblock;
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;
bitmap[1760] = -1;
prevblock = 880;
dblock1 = createdirheaderblock (sector2, 880, dirname1, bitmap, &prevblock);
ss = zfile_fopen_empty (src, fname1b, strlen (fname1));
zfile_fwrite (fname1, strlen(fname1), 1, ss);
fblock1 = createfileheaderblock (dst, sector1, dblock1, fname2, ss, bitmap, &prevblock);
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, &prevblock);
createrootblock (sector1, zfile_getfilename (src));
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 bool isfloppysound (drive *drv)
{
return drv->useturbo == 0;
}
static int get_floppy_speed (void)
{
int m = currprefs.floppy_speed;
if (m <= 10)
m = 100;
m = NORMAL_FLOPPY_SPEED * 100 / m;
return m;
}
static int get_floppy_speed_from_image(drive *drv)
{
int l, m;
l = drv->tracklen;
m = get_floppy_speed () * l / (2 * 8 * FLOPPY_WRITE_LEN * drv->ddhd);
// 4us track?
if (l < (FLOPPY_WRITE_LEN_PAL * 8) * 4 / 6)
m *= 2;
if (m <= 0)
m = 1;
return m;
}
static const TCHAR *drive_id_name(drive *drv)
{
switch(drv->drive_id)
{
case DRIVE_ID_35HD : return _T("3.5HD");
case DRIVE_ID_525SD: return _T("5.25SD");
case DRIVE_ID_35DD : return _T("3.5DD");
case DRIVE_ID_NONE : return _T("NONE");
}
return _T("UNKNOWN");
}
/* 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.floppyslots[drv - &floppy[0]].dfxtype;
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_ESCOM:
case DRV_35_DD:
case DRV_525_DD:
default:
drv->drive_id = DRIVE_ID_35DD;
break;
case DRV_525_SD:
drv->drive_id = DRIVE_ID_525SD;
break;
case DRV_NONE:
case DRV_PC_525_ONLY_40:
case DRV_PC_525_40_80:
case DRV_PC_35_ONLY_80:
drv->drive_id = DRIVE_ID_NONE;
break;
}
#ifdef DEBUG_DRIVE_ID
write_log (_T("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_SCP:
#ifdef SCP
scp_close (drv - floppy);
#endif
break;
case ADF_FDI:
#ifdef FDI2RAW
fdi2raw_header_free (drv->fdi);
drv->fdi = 0;
#endif
break;
}
drv->filetype = ADF_NONE;
zfile_fclose(drv->diskfile);
drv->diskfile = NULL;
zfile_fclose(drv->writediskfile);
drv->writediskfile = NULL;
zfile_fclose(drv->pcdecodedfile);
drv->pcdecodedfile = NULL;
}
static int drive_insert (drive * drv, struct uae_prefs *p, int dnum, const TCHAR *fname, bool fake, bool writeprotected);
static void reset_drive_gui (int num)
{
struct gui_info_drive *gid = &gui_data.drives[num];
gid->drive_disabled = 0;
gid->df[0] = 0;
gid->crc32 = 0;
if (currprefs.floppyslots[num].dfxtype < 0)
gid->drive_disabled = 1;
}
static void setamax (void)
{
#ifdef AMAX
amax_enabled = false;
if (is_device_rom(&currprefs, ROMTYPE_AMAX, 0) > 0) {
/* Put A-Max as last drive in drive chain */
int j;
amax_enabled = true;
for (j = 0; j < MAX_FLOPPY_DRIVES; j++)
if (floppy[j].amax)
return;
for (j = 0; j < MAX_FLOPPY_DRIVES; j++) {
if ((1 << j) & disabled) {
floppy[j].amax = 1;
write_log (_T("AMAX: drive %d\n"), j);
return;
}
}
}
#endif
}
static bool ispcbridgedrive(int num)
{
int type = currprefs.floppyslots[num].dfxtype;
return type == DRV_PC_525_ONLY_40 || type == DRV_PC_35_ONLY_80 || type == DRV_PC_525_40_80;
}
static bool drive_writeprotected(drive *drv)
{
#ifdef CATWEASEL
if (drv->catweasel)
return 1;
#endif
return currprefs.floppy_read_only || drv->wrprot || drv->forcedwrprot || drv->diskfile == NULL;
}
static void reset_drive (int num)
{
drive *drv = &floppy[num];
drv->amax = 0;
drive_image_free (drv);
drv->motoroff = 1;
drv->idbit = 0;
drv->drive_id = 0;
drv->drive_id_scnt = 0;
drv->lastdataacesstrack = -1;
disabled &= ~(1 << num);
reserved &= ~(1 << num);
if (currprefs.floppyslots[num].dfxtype < 0 || ispcbridgedrive(num))
disabled |= 1 << num;
if (ispcbridgedrive(num))
reserved |= 1 << num;
reset_drive_gui (num);
/* 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 (num == 0 && currprefs.floppyslots[num].dfxtype == 0)
drv->indexhackmode = 1;
drv->dskchange_time = 0;
drv->dskchange = false;
drv->dskready_down_time = 0;
drv->dskready_up_time = 0;
drv->buffered_cyl = -1;
drv->buffered_side = -1;
gui_led (num + LED_DF0, 0, -1);
drive_settype_id (drv);
_tcscpy (currprefs.floppyslots[num].df, changed_prefs.floppyslots[num].df);
drv->newname[0] = 0;
drv->newnamewriteprotected = false;
if (!drive_insert (drv, &currprefs, num, currprefs.floppyslots[num].df, false, false))
disk_eject (num);
}
/* code for track display */
static void update_drive_gui (int num, bool force)
{
drive *drv = floppy + num;
bool writ = dskdmaen == DSKDMA_WRITE && drv->state && !((selected | disabled) & (1 << num));
struct gui_info_drive *gid = &gui_data.drives[num];
if (!force && drv->state == gid->drive_motor
&& drv->cyl == gid->drive_track
&& side == gui_data.drive_side
&& drv->crc32 == gid->crc32
&& writ == gid->drive_writing
&& drive_writeprotected(drv) == gid->floppy_protected
&& !_tcscmp (gid->df, currprefs.floppyslots[num].df))
return;
_tcscpy (gid->df, currprefs.floppyslots[num].df);
gid->crc32 = drv->crc32;
gid->drive_motor = drv->state;
gid->drive_track = drv->cyl;
if (reserved & (1 << num))
gui_data.drive_side = reserved_side;
else
gui_data.drive_side = side;
gid->drive_writing = writ;
gid->floppy_protected = drive_writeprotected(drv);
gui_led (num + LED_DF0, (gid->drive_motor ? 1 : 0) | (gid->drive_writing ? 2 : 0), -1);
}
static void drive_fill_bigbuf (drive * drv,int);
int DISK_validate_filename (struct uae_prefs *p, const TCHAR *fname_in, TCHAR *outfname, int leave_open, bool *wrprot, uae_u32 *crc32, struct zfile **zf)
{
TCHAR outname[MAX_DPATH];
if (zf)
*zf = NULL;
if (crc32)
*crc32 = 0;
if (wrprot)
*wrprot = p->floppy_read_only ? 1 : 0;
cfgfile_resolve_path_out_load(fname_in, outname, MAX_DPATH, PATH_FLOPPY);
if (outfname)
_tcscpy(outfname, outname);
if (leave_open || !zf) {
struct zfile *f = zfile_fopen (outname, _T("r+b"), ZFD_NORMAL | ZFD_DISKHISTORY);
if (!f) {
if (wrprot)
*wrprot = 1;
f = zfile_fopen (outname, _T("rb"), ZFD_NORMAL | ZFD_DISKHISTORY);
}
if (f && crc32)
*crc32 = zfile_crc32 (f);
if (!zf)
zfile_fclose (f);
else
*zf = f;
return f ? 1 : 0;
} else {
if (zfile_exists (outname)) {
if (wrprot && !p->floppy_read_only)
*wrprot = 0;
if (crc32) {
struct zfile *f = zfile_fopen (outname, _T("rb"), ZFD_NORMAL | ZFD_DISKHISTORY);
if (f)
*crc32 = zfile_crc32 (f);
zfile_fclose (f);
}
return 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;
if (drv->mfmpos >= drv->tracklen)
drv->mfmpos = drv->tracklen - 1;
}
drv->mfmpos %= drv->tracklen;
drv->prevtracklen = drv->tracklen;
}
static void track_reset (drive *drv)
{
drv->tracklen = FLOPPY_WRITE_LEN * drv->ddhd * 2 * 8;
drv->revolutions = 1;
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 int read_header_ext2 (struct zfile *diskfile, trackid *trackdata, int *num_tracks, int *ddhd)
{
uae_u8 buffer[2 + 2 + 4 + 4];
trackid *tid;
int offs;
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 = (image_tracktype)buffer[3];
tid->revolutions = buffer[2] + 1;
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;
}
static void saveimagecutpathpart(TCHAR *name)
{
int i;
i = _tcslen (name) - 1;
while (i > 0) {
if (name[i] == '/' || name[i] == '\\') {
name[i] = 0;
break;
}
if (name[i] == '.') {
name[i] = 0;
break;
}
i--;
}
while (i > 0) {
if (name[i] == '/' || name[i] == '\\') {
name[i] = 0;
break;
}
i--;
}
}
static void saveimagecutfilepart(TCHAR *name)
{
TCHAR tmp[MAX_DPATH];
int i;
_tcscpy(tmp, name);
i = _tcslen (tmp) - 1;
while (i > 0) {
if (tmp[i] == '/' || tmp[i] == '\\') {
_tcscpy(name, tmp + i + 1);
break;
}
if (tmp[i] == '.') {
tmp[i] = 0;
break;
}
i--;
}
while (i > 0) {
if (tmp[i] == '/' || tmp[i] == '\\') {
_tcscpy(name, tmp + i + 1);
break;
}
i--;
}
}
static void saveimageaddfilename(TCHAR *dst, const TCHAR *src, int type)
{
_tcscat(dst, src);
if (type)
_tcscat(dst, _T(".save_adf"));
else
_tcscat(dst, _T("_save.adf"));
}
static TCHAR *DISK_get_default_saveimagepath (const TCHAR *name)
{
TCHAR name1[MAX_DPATH];
TCHAR path[MAX_DPATH];
_tcscpy(name1, name);
saveimagecutfilepart(name1);
fetch_saveimagepath (path, sizeof path / sizeof (TCHAR), 1);
saveimageaddfilename(path, name1, 0);
return my_strdup(path);
}
// -2 = existing, if not, use 0.
// -1 = as configured
// 0 = saveimages-dir
// 1 = image dir
TCHAR *DISK_get_saveimagepath(const TCHAR *name, int type)
{
int typev = type;
for (int i = 0; i < 2; i++) {
if (typev == 1 || (typev == -1 && saveimageoriginalpath) || (typev == -2 && (saveimageoriginalpath || i == 1))) {
TCHAR si_name[MAX_DPATH], si_path[MAX_DPATH];
_tcscpy(si_name, name);
_tcscpy(si_path, name);
saveimagecutfilepart(si_name);
saveimagecutpathpart(si_path);
_tcscat(si_path, FSDB_DIR_SEPARATOR_S);
saveimageaddfilename(si_path, si_name, 1);
if (typev != -2 || (typev == -2 && zfile_exists(si_path)))
return my_strdup(si_path);
}
if (typev == 2 || (typev == -1 && !saveimageoriginalpath) || (typev == -2 && (!saveimageoriginalpath || i == 1))) {
TCHAR *p = DISK_get_default_saveimagepath(name);
if (typev != -2 || (typev == -2 && zfile_exists(p)))
return p;
xfree(p);
}
}
return DISK_get_saveimagepath(name, -1);
}
static struct zfile *getexistingwritefile(struct uae_prefs *p, const TCHAR *name, bool *wrprot)
{
struct zfile *zf = NULL;
TCHAR *path;
TCHAR outname[MAX_DPATH];
path = DISK_get_saveimagepath(name, saveimageoriginalpath);
DISK_validate_filename (p, path, outname, 1, wrprot, NULL, &zf);
xfree(path);
if (zf)
return zf;