-
Notifications
You must be signed in to change notification settings - Fork 17
/
trs_disk.c
3619 lines (3396 loc) · 104 KB
/
trs_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
/*
* Copyright (c) 1996-2020, Timothy P. Mann
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Emulate Model I or III/4 disk controller
*/
/*
* Debug flags. Update help_message in debug.c if these change.
*/
#define DISKDEBUG_FDCREG (1<<0) /* FDC register reads and writes */
#define DISKDEBUG_FDCCMD (1<<1) /* FDC commands */
#define DISKDEBUG_VTOS3 (1<<2) /* VTOS 3.0 JV3 kludges */
#define DISKDEBUG_GAPS (1<<3) /* Gaps and real_writetrk */
#define DISKDEBUG_REALSIZE (1<<4) /* REAL sector size detection */
#define DISKDEBUG_READADR (1<<5) /* Read Address timing */
#define DISKDEBUG_DMK (1<<6) /* DMK support */
#define DISKDEBUG_REALERR (1<<7) /* ioctl errors accessing real disks */
#define TSTATEREV 1 /* Index holes timed by T-states, not real time */
#define SIZERETRY 1 /* Retry in different sizes on real_read */
#define DMK_MARK_IAM 0 /* Mark IAMs in track header; poor idea */
#define _XOPEN_SOURCE 500 /* signal.h: SA_RESTART */
#include "z80.h"
#include "trs.h"
#include "trs_disk.h"
#include "trs_hard.h"
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "crc.c"
#if __linux
#include <sys/types.h>
#include <fcntl.h>
#include <linux/fd.h>
#include <linux/fdreg.h>
#include <sys/ioctl.h>
#endif
#define NDRIVES 8
int trs_disk_nocontroller = 0;
int trs_disk_doubler = TRSDISK_BOTH;
char *trs_disk_dir = DISKDIR;
static int trs_disk_needchange = 0;
float trs_disk_holewidth = 0.01;
int trs_disk_truedam = 0;
int trs_disk_debug_flags = 0;
char *trs_disk_name[NDRIVES];
static int trs_disk_change(int drive);
typedef struct {
/* Registers */
unsigned char status;
unsigned char track;
unsigned char sector;
unsigned char data;
/* Other state */
unsigned char currcommand;
int lastdirection;
int bytecount; /* bytes left to transfer this command */
int format; /* write track state machine */
int format_bytecount; /* bytes left in this part of write track */
int format_sec; /* current sector number or id_index */
int format_gapcnt; /* measure requested gaps */
int format_gap[5];
unsigned short crc;
int curdrive;
int curside;
int density; /* sden=0, dden=1 */
unsigned char controller; /* TRSDISK_P1771 or TRSDISK_P1791 */
int last_readadr; /* id index found by last readadr */
tstate_t motor_timeout; /* 0 if stopped, else time when it stops */
} FDCState;
FDCState state, other_state;
/* Format states - what is expected next? */
#define FMT_GAP0 0
#define FMT_IAM 1
#define FMT_GAP1 2
#define FMT_IDAM 3
#define FMT_TRACKID 4
#define FMT_HEADID 5
#define FMT_SECID 6
#define FMT_SIZEID 7
#define FMT_IDCRC 8
#define FMT_GAP2 9
#define FMT_DAM 10
#define FMT_DATA 11
#define FMT_DCRC 12
#define FMT_GAP3 13
#define FMT_GAP4 14
#define FMT_DONE 15
#define FMT_PREAM 16 /* DDEN DMK only -- just saw preamble to an AM */
#define FMT_IPREAM 17 /* DDEN DMK only -- just saw preamble to an IAM */
/* Gap 0+1 and gap 4 angular size, used in Read Address timing emulation.
Units: fraction of a complete circle. */
#define GAP1ANGLE 0.020
#define GAP4ANGLE 0.050
/* How long does emulated motor stay on after drive selected? (us of
emulated time) */
#define MOTOR_USEC 2000000
/* Heuristic: how often are we willing to check whether real drive
has a disk in it? (seconds of real time) */
#define EMPTY_TIMEOUT 3
/*
* The following rather quirky data structure is designed to be
* compatible with what Jeff Vavasour's Model III/4 emulator uses to
* represent disk formatting information. My interpretation is based
* on reading his documentation, looking at some disk images, and
* experimenting with his emulator to generate odd cases, so the
* compatibility should be excellent.
*
* I have compatibly extended the format to allow for more sectors, so
* that 8" DSDD drives can be supported, by adding a second block of
* ids after the block of data sectors that is described by the first
* block. JV himself says that sounds like a good idea. Matthew
* Reed's emulators now support this extension.
*
* I've further extended the format to add a flag bit for non-IBM
* sectors. Only a subset of the non-IBM functionality is supported,
* rigged to make the VTOS 3.0 copy-protection system work. Non-IBM
* sectors were a feature of the 1771 only. Using this feature, you
* could have a sector of any length from 16 to 4096 bytes in
* multiples of 16. xtrs supports only 16-byte non-IBM sectors (with
* their data stored in a 256-byte field), and has some special kludges
* to detect and support VTOS's trick of formatting these sectors with
* inadequate gaps between so that writing to one would smash the
* index block of the next one.
*
* Finally, I've extended the format to support (IBM) sector lengths
* other than 256 bytes. The standard lengths 128, 256, 512, and 1024
* are supported, using the last two available bits in the header
* flags byte. The data area of a floppy image thus becomes an array
* of *variable length* sectors, making it more complicated to find
* the sector data corresponding to a given header and to manage freed
* sectors.
*
* NB: JV's model I emulator uses no auxiliary data structure for disk
* format. It simply assumes that all disks are single density, 256
* bytes/sector, 10 sectors/track, single sided, with nonstandard FA
* data address mark on all sectors on track 17. */
/* Values for flags below */
#define JV3_DENSITY 0x80 /* 1=dden, 0=sden */
#define JV3_DAM 0x60 /* data address mark; values follow */
#define JV3_DAMSDFB 0x00
#define JV3_DAMSDFA 0x20
#define JV3_DAMSDF9 0x40
#define JV3_DAMSDF8 0x60
#define JV3_DAMDDFB 0x00
#define JV3_DAMDDF8 0x20
#define JV3_SIDE 0x10 /* 0=side 0, 1=side 1 */
#define JV3_ERROR 0x08 /* 0=ok, 1=CRC error */
#define JV3_NONIBM 0x04 /* 0=normal, 1=short (for VTOS 3.0, xtrs only) */
#define JV3_SIZE 0x03 /* in used sectors: 0=256,1=128,2=1024,3=512
in free sectors: 0=512,1=1024,2=128,3=256 */
#define JV3_FREE 0xff /* in track/sector fields */
#define JV3_FREEF 0xfc /* in flags field, or'd with size code */
typedef struct {
unsigned char track;
unsigned char sector;
unsigned char flags;
} SectorId;
#define MAXTRACKS 255
#define JV1_SECSIZE 256
#define MAXSECSIZE 1024
/* Max bytes per unformatted track. */
/* Select codes 1, 2, 4, 8 are emulated 5" drives, disk?-0 to disk?-3 */
#define TRKSIZE_SD 3125 /* 250kHz / 5 Hz [300rpm] / (2 * 8) */
/* or 300kHz / 6 Hz [360rpm] / (2 * 8) */
#define TRKSIZE_DD 6250 /* 250kHz / 5 Hz [300rpm] / 8 */
/* or 300kHz / 6 Hz [360rpm] / 8 */
/* Select codes 3, 5, 6, 7 are emulated 8" drives, disk?-4 to disk?-7 */
#define TRKSIZE_8SD 5208 /* 500kHz / 6 Hz [360rpm] / (2 * 8) */
#define TRKSIZE_8DD 10416 /* 500kHz / 6 Hz [360rpm] / 8 */
/* TRS-80 software has no concept of HD, so these constants are unused,
* but expanded JV3 would be big enough even for 3.5" HD. */
#define TRKSIZE_5HD 10416 /* 500kHz / 6 Hz [360rpm] / 8 */
#define TRKSIZE_3HD 12500 /* 500kHz / 5 Hz [300rpm] / 8 */
#define JV3_SIDES 2
#define JV3_IDSTART 0
#define JV3_SECSTART (34*256) /* start of sectors within file */
#define JV3_SECSPERBLK ((int)(JV3_SECSTART/3))
#define JV3_SECSMAX (2*JV3_SECSPERBLK)
#define JV1_SECPERTRK 10
typedef struct {
int free_id[4]; /* first free id, if any, of each size */
int last_used_id; /* last used index */
int nblocks; /* number of blocks of ids, 1 or 2 */
int sorted_valid; /* sorted_id array valid */
SectorId id[JV3_SECSMAX + 1]; /* extra one is a loop sentinel */
int offset[JV3_SECSMAX + 1]; /* offset into file for each id */
short sorted_id[JV3_SECSMAX + 1];
short track_start[MAXTRACKS][JV3_SIDES];
} JV3State;
typedef struct {
int rps; /* phys rotations/sec; emutype REAL only */
int size_code; /* most recent sector size; REAL only */
int empty; /* 1=emulate empty drive */
time_t empty_timeout; /* real_empty valid until this time */
int fmt_nbytes; /* number of PC format command bytes */
int fmt_fill; /* fill byte for data sectors */
unsigned char buf[MAXSECSIZE];
} RealState;
/* Some constants for DMK format */
#define DMK_WRITEPROT 0
#define DMK_NTRACKS 1
#define DMK_TRACKLEN 2
#define DMK_TRACKLEN_SIZE 2
#define DMK_OPTIONS 4
#define DMK_FORMAT 0x0c
#define DMK_FORMAT_SIZE 4
#define DMK_HDR_SIZE 0x10
#define DMK_TKHDR_SIZE 0x80 /* Space reserved for IDAM pointers */
#define DMK_TRACKLEN_MAX 0x4000
/* Bit assignments in options */
#define DMK_SSIDE_OPT 0x10
#define DMK_SDEN_OPT 0x40
#define DMK_IGNDEN_OPT 0x80
/* Bit assignments in IDAM pointers */
#define DMK_DDEN_FLAG 0x8000
#define DMK_EXTRA_FLAG 0x4000 /* unused */
#define DMK_IDAMP_BITS 0x3fff
#define dmk_incr(d) \
(((d)->u.dmk.ignden || (d)->u.dmk.sden || state.density) ? 1 : 2)
typedef struct {
int ntracks; /* max number of tracks formatted */
int tracklen; /* bytes reserved per track in file */
int nsides; /* 1 or 2 (single-sided flag in header) */
int sden; /* single-density-only flag in header */
int ignden; /* ignore-density flag in header */
int curtrack, curside; /* track/side in track buffer, or -1/-1 */
int curbyte; /* index in buf for current op */
int nextidam; /* index in buf to put next idam */
unsigned char buf[DMK_TRACKLEN_MAX];
} DMKState;
typedef struct {
int writeprot; /* emulated write protect tab */
int phytrack; /* where are we really? */
int emutype;
int inches; /* 5 or 8, as seen by TRS-80 */
int real_step; /* 1=normal, 2=double-step if REAL */
char *name;
FILE* file;
union {
JV3State jv3; /* valid if emutype = JV3 */
RealState real; /* valid if emutype = REAL */
DMKState dmk; /* valid if emutype = DMK */
} u;
} DiskState;
DiskState disk[NDRIVES];
/* Emulate interleave in JV1 mode */
unsigned char jv1_interleave[10] = {0, 5, 1, 6, 2, 7, 3, 8, 4, 9};
/* Forward */
void real_verify(void);
void real_restore(int curdrive);
void real_seek(void);
void real_read(void);
void real_write(void);
void real_readadr(void);
void real_readtrk(void);
void real_writetrk(void);
int real_check_empty(DiskState *d);
/* Entry point for the zbx debugger */
void
trs_disk_debug(void)
{
int i;
printf("Floppy disk controller state:\n");
printf(" status 0x%02x, track %d (0x%02x), sector %d (0x%02x), "
"data 0x%02x\n", state.status, state.track, state.track,
state.sector, state.sector, state.data);
printf(" currcommand 0x%02x, bytecount left %d, last step direction %d\n",
state.currcommand, state.bytecount, state.lastdirection);
printf(" curdrive %d, curside %d, density %d, controller %s\n",
state.curdrive, state.curside, state.density,
state.controller == TRSDISK_P1771 ? "WD1771" : "WD1791/93");
printf(" crc state 0x%04x, last_readadr %d, motor timeout %ld\n",
state.crc, state.last_readadr,
(long) (state.motor_timeout - z80_state.t_count));
printf(" last (non-DMK) format gaps %d %d %d %d %d\n",
state.format_gap[0], state.format_gap[1], state.format_gap[2],
state.format_gap[3], state.format_gap[4]);
printf(" debug flags: %#x\n", trs_disk_debug_flags);
for (i=0; i<NDRIVES; i++) {
DiskState *d = &disk[i];
printf("Drive %d state: "
"writeprot %d, phytrack %d (0x%02x), inches %d, step %d, type ",
i, d->writeprot, d->phytrack, d->phytrack, d->inches, d->real_step);
if (d->file == NULL) {
printf("EMPTY\n");
} else {
switch (d->emutype) {
case JV1:
printf("JV1\n");
break;
case JV3:
printf("JV3\n");
printf(" last used id %d, id blocks %d\n",
d->u.jv3.last_used_id, d->u.jv3.nblocks);
break;
case DMK:
printf("DMK\n");
printf(" ntracks %d (0x%02x), tracklen 0x%04x, nsides %d, sden %d, "
"ignden %d\n", d->u.dmk.ntracks, d->u.dmk.ntracks,
d->u.dmk.tracklen, d->u.dmk.nsides, d->u.dmk.sden,
d->u.dmk.ignden);
printf(" buffered track %d, side %d, curbyte %d, nextidam %d\n",
d->u.dmk.curtrack, d->u.dmk.curside, d->u.dmk.curbyte,
d->u.dmk.nextidam);
break;
case REAL:
printf("REAL\n");
printf(" rpm %d, empty %d, last size code %d, last fmt fill 0x%02x\n",
d->u.real.rps * 60, d->u.real.empty, d->u.real.size_code,
d->u.real.fmt_fill);
break;
default:
printf("UNKNOWN\n");
break;
}
}
}
}
void
trs_disk_setsize(int unit, int value)
{
if (unit < 0 || unit > 7) return;
disk[unit].inches = (value == 8) ? 8 : 5;
}
void
trs_disk_setstep(int unit, int value)
{
if (unit < 0 || unit > 7) return;
disk[unit].real_step = (value == 2) ? 2 : 1;
}
int
trs_disk_getsize(int unit)
{
if (unit < 0 || unit > 7) return 0;
return disk[unit].inches;
}
int
trs_disk_getstep(int unit)
{
if (unit < 0 || unit > 7) return 0;
return disk[unit].real_step;
}
void
trs_sigusr1(int signo)
{
trs_disk_needchange = 1;
}
void
trs_disk_init(void)
{
int i;
struct sigaction sa;
for (i=0; i<NDRIVES; i++) {
disk[i].phytrack = 0;
disk[i].emutype = NONE;
disk[i].name = (char *) malloc(strlen(trs_disk_dir) + 10);
if (trs_model == 5) {
sprintf(disk[i].name, "%s/disk4p-%d", trs_disk_dir, i);
} else {
sprintf(disk[i].name, "%s/disk%d-%d", trs_disk_dir, trs_model, i);
}
}
sa.sa_handler = trs_sigusr1;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGUSR1);
sa.sa_flags = SA_RESTART;
sigaction(SIGUSR1, &sa, NULL);
}
/* Reset floppy controller hardware */
void
trs_disk_reset(void)
{
state.status = TRSDISK_NOTRDY|TRSDISK_TRKZERO;
state.track = 0;
state.sector = 0;
state.data = 0;
state.currcommand = TRSDISK_RESTORE;
state.lastdirection = 1;
state.bytecount = 0;
state.format = FMT_DONE;
state.format_bytecount = 0;
state.format_sec = 0;
state.curdrive = state.curside = 0;
state.density = 0;
state.controller = (trs_model == 1) ? TRSDISK_P1771 : TRSDISK_P1791;
state.last_readadr = -1;
state.motor_timeout = 0;
trs_cancel_event();
/*
* Emulate no controller if there is no disk in drive 0 at reset time,
* except for model 4.
*/
trs_disk_nocontroller = (trs_model < 5 &&
trs_disk_change(0) != 0);
}
/* trs_event_func used for delayed command completion. Clears BUSY,
sets any additional bits specified, and generates a command
completion interrupt */
static void
trs_disk_done(int bits)
{
state.status &= ~TRSDISK_BUSY;
state.status |= bits;
trs_disk_intrq_interrupt(1);
}
/* trs_event_func to abort the last command with LOSTDATA if it is
still in progress */
static void
trs_disk_lostdata(int cmd)
{
if (state.currcommand == cmd) {
state.status &= ~TRSDISK_BUSY;
state.status |= TRSDISK_LOSTDATA;
state.bytecount = 0;
trs_disk_intrq_interrupt(1);
}
}
/* trs_event_func used as a delayed command start. Sets DRQ,
generates a DRQ interrupt, sets any additional bits specified, and
schedules a trs_disk_lostdata event. */
static void
trs_disk_firstdrq(int bits)
{
state.status |= TRSDISK_DRQ | bits;
trs_disk_drq_interrupt(1);
trs_schedule_event(trs_disk_lostdata, state.currcommand,
500000 * z80_state.clockMHz);
}
static void
trs_disk_unimpl(unsigned char cmd, char* more)
{
state.status = TRSDISK_NOTRDY|TRSDISK_WRITEFLT|TRSDISK_NOTFOUND;
state.bytecount = state.format_bytecount = 0;
state.format = FMT_DONE;
trs_disk_drq_interrupt(0);
trs_schedule_event(trs_disk_done, 0, 0);
error("trs_disk_command(0x%02x) not implemented - %s", cmd, more);
}
/* Sort first by track, second by side, third by position in emulated-disk
sector array (i.e., physical sector order on track). */
static int
jv3_id_compare(const void* p1, const void* p2)
{
DiskState *d = &disk[state.curdrive];
int i1 = *(short*)p1;
int i2 = *(short*)p2;
int r = d->u.jv3.id[i1].track - d->u.jv3.id[i2].track;
if (r != 0) return r;
r = (d->u.jv3.id[i1].flags & JV3_SIDE) - (d->u.jv3.id[i2].flags & JV3_SIDE);
if (r != 0) return r;
return i1 - i2;
}
/* (Re-)create the sorted_id data structure for the given drive */
void
jv3_sort_ids(int drive)
{
DiskState *d = &disk[drive];
int olddrive = state.curdrive;
int i, track, side;
for (i=0; i<=JV3_SECSMAX; i++) {
d->u.jv3.sorted_id[i] = i;
}
state.curdrive = drive;
qsort((void*) d->u.jv3.sorted_id, JV3_SECSMAX, sizeof(short),
jv3_id_compare);
state.curdrive = olddrive;
for (track=0; track<MAXTRACKS; track++) {
d->u.jv3.track_start[track][0] = -1;
d->u.jv3.track_start[track][1] = -1;
}
track = side = -1;
for (i=0; i<JV3_SECSMAX; i++) {
SectorId *sid = &d->u.jv3.id[d->u.jv3.sorted_id[i]];
if (sid->track != track ||
(sid->flags & JV3_SIDE ? 1 : 0) != side) {
track = sid->track;
if (track == JV3_FREE) break;
side = sid->flags & JV3_SIDE ? 1 : 0;
d->u.jv3.track_start[track][side] = i;
}
}
d->u.jv3.sorted_valid = 1;
}
/* JV3 only */
int
id_index_to_size_code(DiskState *d, int id_index)
{
return (d->u.jv3.id[id_index].flags & JV3_SIZE) ^
((d->u.jv3.id[id_index].track == JV3_FREE) ? 2 : 1);
}
/* IBM formats only */
int
size_code_to_size(int code)
{
return 128 << code;
}
/* JV3 only */
int
id_index_to_size(DiskState *d, int id_index)
{
return 128 << id_index_to_size_code(d, id_index);
}
/* Return the offset of the data block for the id_index'th sector
in an emulated-disk file. Not used for DMK. */
static off_t
offset(DiskState *d, int id_index)
{
if (d->emutype == JV1) {
return id_index * JV1_SECSIZE;
} else if (d->emutype == JV3) {
return d->u.jv3.offset[id_index];
} else {
trs_disk_unimpl(state.currcommand, "DMK offset (internal error)");
return 0;
}
}
/* Return the offset of the id block for the id_index'th sector
in an emulated-disk file. Initialize a new block if needed. JV3 only. */
static off_t
idoffset(DiskState *d, int id_index)
{
if (d->emutype == JV1 || d->emutype == DMK) {
trs_disk_unimpl(state.currcommand, "JV1 or DMK idoffset (internal error)");
return -1;
} else {
if (id_index < JV3_SECSPERBLK) {
return JV3_IDSTART + id_index * sizeof(SectorId);
} else {
int idstart2 = d->u.jv3.offset[JV3_SECSPERBLK-1] +
id_index_to_size(d, JV3_SECSPERBLK-1);
if (d->u.jv3.nblocks == 1) {
/* Initialize new block of ids */
int c;
fseek(d->file, idstart2, 0);
c = fwrite((void*)&d->u.jv3.id[JV3_SECSPERBLK], JV3_SECSTART, 1, d->file);
if (c == EOF) state.status |= TRSDISK_WRITEFLT;
c = fflush(d->file);
if (c == EOF) state.status |= TRSDISK_WRITEFLT;
d->u.jv3.nblocks = 2;
}
return idstart2 + (id_index - JV3_SECSPERBLK) * sizeof(SectorId);
}
}
}
int
jv3_alloc_sector(DiskState *d, int size_code)
{
int maybe = d->u.jv3.free_id[size_code];
d->u.jv3.sorted_valid = 0;
while (maybe <= d->u.jv3.last_used_id) {
if (d->u.jv3.id[maybe].track == JV3_FREE &&
id_index_to_size_code(d, maybe) == size_code) {
d->u.jv3.free_id[size_code] = maybe + 1;
return maybe;
}
maybe++;
}
d->u.jv3.free_id[size_code] = JV3_SECSMAX; /* none are free */
if (d->u.jv3.last_used_id >= JV3_SECSMAX-1) {
return -1;
}
d->u.jv3.last_used_id++;
d->u.jv3.offset[d->u.jv3.last_used_id + 1] =
d->u.jv3.offset[d->u.jv3.last_used_id] + size_code_to_size(size_code);
if (d->u.jv3.last_used_id + 1 == JV3_SECSPERBLK) {
d->u.jv3.offset[d->u.jv3.last_used_id + 1] += JV3_SECSTART;
}
return d->u.jv3.last_used_id;
}
void
jv3_free_sector(DiskState *d, int id_index)
{
int c;
int size_code = (d->u.jv3.id[id_index].flags & JV3_SIZE) ^ 1;
if (d->u.jv3.free_id[size_code] > id_index) {
d->u.jv3.free_id[size_code] = id_index;
}
d->u.jv3.sorted_valid = 0;
d->u.jv3.id[id_index].track = JV3_FREE;
d->u.jv3.id[id_index].sector = JV3_FREE;
d->u.jv3.id[id_index].flags =
(d->u.jv3.id[id_index].flags | JV3_FREEF) ^ JV3_SIZE;
fseek(d->file, idoffset(d, id_index), 0);
c = fwrite(&d->u.jv3.id[id_index], sizeof(SectorId), 1, d->file);
if (c == EOF) state.status |= TRSDISK_WRITEFLT;
if (id_index == d->u.jv3.last_used_id) {
int newlen;
while (d->u.jv3.id[d->u.jv3.last_used_id].track == JV3_FREE) {
d->u.jv3.last_used_id--;
}
c = fflush(d->file);
if (c == EOF) state.status |= TRSDISK_WRITEFLT;
rewind(d->file);
if (d->u.jv3.last_used_id >= 0) {
newlen = offset(d, d->u.jv3.last_used_id) +
id_index_to_size(d, d->u.jv3.last_used_id);
} else {
newlen = offset(d, 0);
}
c = ftruncate(fileno(d->file), newlen);
if (c == EOF) state.status |= TRSDISK_WRITEFLT;
}
}
/* Heuristic to decide what file format we have */
/* Also decodes write-protect state */
void
trs_disk_emutype(DiskState *d)
{
int c;
char fmt[4];
int count;
fseek(d->file, 0, 0);
c = getc(d->file);
if (c == -1) {
d->emutype = JV1;
return;
}
if (c == 0 || c == 0xff) {
fseek(d->file, DMK_FORMAT, 0);
count = fread(fmt, 1, DMK_FORMAT_SIZE, d->file);
if (count != DMK_FORMAT_SIZE) {
d->emutype = JV1;
return;
}
if (fmt[0] == 0 && fmt[1] == 0 && fmt[2] == 0 && fmt[3] == 0) {
fseek(d->file, DMK_TRACKLEN, 0);
count = (unsigned char) getc(d->file);
count += (unsigned char) getc(d->file) << 8;
if (count >= 16 && count <= DMK_TRACKLEN_MAX) {
d->emutype = DMK;
d->writeprot = d->writeprot || (c == 0xff);
return;
}
}
if (fmt[0] == 0x78 && fmt[1] == 0x56 && fmt[2] == 0x34 && fmt[3] == 0x12) {
error("Real disk specifier file from DMK emulator not supported");
d->emutype = NONE;
fclose(d->file);
d->file = NULL;
return;
}
}
if (c == 0) {
fseek(d->file, 1, 0);
if (getc(d->file) == 0xfe) {
d->emutype = JV1;
return;
}
}
fseek(d->file, JV3_SECSPERBLK*sizeof(SectorId), 0);
c = getc(d->file);
if (c == 0 || c == 0xff) {
d->emutype = JV3;
d->writeprot = d->writeprot || (c == 0);
return;
}
d->emutype = JV1;
}
/* Returns 0 if OK, -1 if invalid header, errno value otherwise. */
static int
trs_disk_change(int drive)
{
DiskState *d = &disk[drive];
struct stat st;
int c, res;
if (d->file != NULL) {
c = fclose(d->file);
if (c == EOF) state.status |= TRSDISK_WRITEFLT;
d->file = NULL;
}
if (d->name == NULL) {
return 0;
}
res = stat(d->name, &st);
if (res < 0) {
return errno;
}
#if __linux
if (S_ISBLK(st.st_mode)) {
/* Real floppy drive */
int fd;
int reset_now = 0;
struct floppy_drive_params fdp;
fd = open(d->name, O_ACCMODE|O_NDELAY);
if (fd < 0) {
d->emutype = JV3;
return errno;
}
d->file = fdopen(fd, "r+");
if (d->file == NULL) {
d->emutype = JV3;
return errno;
}
d->writeprot = 0;
ioctl(fileno(d->file), FDRESET, &reset_now);
ioctl(fileno(d->file), FDGETDRVPRM, &fdp);
d->u.real.rps = fdp.rps;
d->u.real.size_code = 1; /* initial guess: 256 bytes */
d->u.real.empty_timeout = 0;
if (d->emutype != REAL) {
d->emutype = REAL;
d->phytrack = 0;
real_restore(drive);
}
} else
#endif
{
d->file = fopen(d->name, "r+");
if (d->file == NULL) {
if (errno == EACCES || errno == EROFS) {
d->file = fopen(d->name, "r");
}
if (d->file == NULL) return errno;
d->writeprot = 1;
} else {
d->writeprot = 0;
}
trs_disk_emutype(d);
}
if (d->emutype == JV3) {
int id_index, n;
int ofst;
memset((void*)d->u.jv3.id, JV3_FREE, sizeof(d->u.jv3.id));
/* Read first block of ids */
fseek(d->file, JV3_IDSTART, 0);
n = fread((void*)&d->u.jv3.id[0], 3, JV3_SECSPERBLK, d->file);
/* Scan to find their offsets */
ofst = JV3_SECSTART;
for (id_index=0; id_index<JV3_SECSPERBLK; id_index++) {
d->u.jv3.offset[id_index] = ofst;
ofst += id_index_to_size(d, id_index);
}
/* Read second block of ids, if any */
fseek(d->file, ofst, 0);
n = fread((void*)&d->u.jv3.id[JV3_SECSPERBLK], 3, JV3_SECSPERBLK, d->file);
d->u.jv3.nblocks = n > 0 ? 2 : 1;
/* Scan to find their offsets */
ofst += JV3_SECSTART;
for (id_index=JV3_SECSPERBLK; id_index<JV3_SECSPERBLK*2; id_index++) {
d->u.jv3.offset[id_index] = ofst;
ofst += id_index_to_size(d, id_index);
}
/* Find u.jv3.last_used_id value and u.jv3.free_id hints */
for (n=0; n<4; n++) {
d->u.jv3.free_id[n] = JV3_SECSMAX;
}
d->u.jv3.last_used_id = -1;
for (id_index=0; id_index<JV3_SECSMAX; id_index++) {
if (d->u.jv3.id[id_index].track == JV3_FREE) {
int size_code = id_index_to_size_code(d, id_index);
if (d->u.jv3.free_id[size_code] == JV3_SECSMAX) {
d->u.jv3.free_id[size_code] = id_index;
}
} else {
d->u.jv3.last_used_id = id_index;
}
}
jv3_sort_ids(drive);
} else if (d->emutype == DMK) {
fseek(d->file, DMK_NTRACKS, 0);
d->u.dmk.ntracks = (unsigned char) getc(d->file);
d->u.dmk.tracklen = (unsigned char) getc(d->file);
d->u.dmk.tracklen += ((unsigned char) getc(d->file)) << 8;
c = getc(d->file);
d->u.dmk.nsides = (c & DMK_SSIDE_OPT) ? 1 : 2;
d->u.dmk.sden = (c & DMK_SDEN_OPT) != 0;
d->u.dmk.ignden = (c & DMK_IGNDEN_OPT) != 0;
d->u.dmk.curtrack = d->u.dmk.curside = -1;
if (trs_disk_debug_flags & DISKDEBUG_DMK) {
debug("DMK drv=%d wp=%d #tk=%d tklen=0x%x nsides=%d sden=%d ignden=%d\n",
drive, d->writeprot, d->u.dmk.ntracks, d->u.dmk.tracklen,
d->u.dmk.nsides, d->u.dmk.sden, d->u.dmk.ignden);
}
} else if (d->emutype == NONE) {
return -1;
}
return 0;
}
void
trs_disk_change_all(void)
{
int i;
for (i=0; i<NDRIVES; i++) {
trs_disk_change(i);
}
}
const char *
trs_disk_get_name(int drive)
{
return disk[drive].name;
}
/* Returns 0 if OK, -1 if invalid header, errno value otherwise. */
int
trs_disk_set_name(int drive, const char *name)
{
DiskState *d = &disk[drive];
if (d->name) free(d->name);
d->name = name ? strdup(name) : NULL;
return trs_disk_change(drive);
}
/* Returns 0 if OK, errno value otherwise. */
int
trs_disk_create(const char *name)
{
FILE *f;
int ires;
f = fopen(name, "w");
if (f == NULL) {
return errno;
}
/*
* Default parameters
*/
ires = trs_disk_init_with(f, -1, -1, -1, -1, -1);
if (ires < 0) return errno;
ires = fclose(f);
if (ires < 0) return errno;
return 0;
}
static int
cmd_type(unsigned char cmd)
{
switch (cmd & TRSDISK_CMDMASK) {
case TRSDISK_RESTORE:
case TRSDISK_SEEK:
case TRSDISK_STEP:
case TRSDISK_STEPU:
case TRSDISK_STEPIN:
case TRSDISK_STEPINU:
case TRSDISK_STEPOUT:
case TRSDISK_STEPOUTU:
return 1;
case TRSDISK_READ:
case TRSDISK_READM:
case TRSDISK_WRITE:
case TRSDISK_WRITEM:
return 2;
case TRSDISK_READADR:
case TRSDISK_READTRK:
case TRSDISK_WRITETRK:
return 3;
case TRSDISK_FORCEINT:
return 4;
}
return -1; /* not reached */
}
/* Called by the interrupt code to determine whether a motoroff NMI is
required. Called even if this NMI is masked, so we also use it here
to set NOTRDY and LOSTDATA. */
int
trs_disk_motoroff(void)
{
int stopped;
int cmdtype;
stopped = (state.motor_timeout - z80_state.t_count > TSTATE_T_MID);
if (stopped) {
state.status |= TRSDISK_NOTRDY;
cmdtype = cmd_type(state.currcommand);
if ((cmdtype == 2 || cmdtype == 3) && (state.status & TRSDISK_DRQ)) {
/* Also end the command and set Lost Data for good measure */
state.status = (state.status | TRSDISK_LOSTDATA) &
~(TRSDISK_BUSY | TRSDISK_DRQ);
state.bytecount = 0;
}
}
return stopped;
}
/* Get the on-disk track data from the current track/side into the buffer */
void
dmk_get_track(DiskState* d)
{
int res;
if (d->phytrack == d->u.dmk.curtrack &&
state.curside == d->u.dmk.curside) return;
d->u.dmk.curtrack = d->phytrack;
d->u.dmk.curside = state.curside;
if (d->u.dmk.curtrack >= d->u.dmk.ntracks ||
(d->u.dmk.curside && d->u.dmk.nsides == 1)) {
memset(d->u.dmk.buf, 0, sizeof(d->u.dmk.buf));
return;
}
fseek(d->file, (DMK_HDR_SIZE +
(d->u.dmk.curtrack * d->u.dmk.nsides + d->u.dmk.curside)
* d->u.dmk.tracklen), 0);