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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
|
/*********************************************************************
*
* formats/ti99_dsk.c
*
* TI99 and Geneve disk images
* Sector Dump and Track Dump format
*
* used by TI-99/4, TI-99/4A, TI-99/8, SGCPU ("TI-99/4P"), and Geneve
*
* The Sector Dump Format is also known as v9t9 (named after the first TI
* emulator to use this format). It is a contiguous sequence of sector contents
* without track data. The first sector of the disk is located at the start of
* the image, while the last sector is at its end.
*
* There is also a variant of the SDF which adds three sectors at the end
* containing a map of bad sectors. This was introduced by a tool to read
* real TI floppy disks on a PC. As other emulators tolerate this additional
* bad sector map, we just check whether there are 3 more sectors and ignore
* them.
*
* The Track Dump Format is also known as pc99 (again, named after the first
* TI emulator to use this format). It is a contiguous sequence of track
* contents, containing all information including address marks and CRC, but it
* does not contain clock signals. Therefore, the format requires that the
* address marks be at the same positions within each track.
*
* Both formats allow for a broad range of medium sizes. All sectors are 256
* bytes long. The most common formats are 9 sectors per track, single-sided,
* 40 tracks, which yields 90 KiB of sector data (known as SSSD), and 18
* sectors per track, double-sided, and 40 tracks, which is 360 KiB (known as
* DSDD). There are rare occurances of 8/16 sectors/track
* (prototypical TI double-density controller) and 35 track media. Newer
* controllers and ROMs allow for up to 36 sectors per track and 80 tracks on
* both sides, which is 2,88 MiB (DSHD80).
*
* Different to earlier implementations, these format implementations provide
* full track read/write capabilities. For the SDF format, the missing track
* information is recreated, and a common interleave is assumed.
*
* Note that in principle, writing tracks may change track sizes.
* As neither one of the formats contains meta-information about track lengths,
* and as the image consists of a contiguous sequence of the tracks, we
* cannot allow different track sizes currently. Accordingly, the track size
* cannot be changed after the image has been created by imgtool.
* Hence, also sector count per tracks and recording type (FM or MFM) cannot be
* changed without breaking the image.
* For example, MFM disks with 18 sectors per track can only be reformatted to
* MFM/18. Moreover, while this could be handled in a future revision,
* the number of tracks should also remain unchanged.
*
* You can use blank image files (filled with zeros or arbitrary data) and use
* the Disk Manager cartridge to properly format them. Also, this can be done
* by imgtool. The image file must match the format length (representing the
* space on the physical medium).
*
* 80 track drive handling
*
* For the TI system there may be a situation where 40 track disks are used
* in 80 track drives. In these cases, the disk driver applies double-stepping,
* that is, it advances the head twice in order to seek the next track. We
* emulate this by dividing the track number by 2, which means that two tracks
* are mapped to one (0,1 -> 0; 2,3 -> 1; 4,5 -> 2; ...; 78,79 -> 39)
*
* Technical detail: The size of the track must be given before actually
* starting write_track as the controller implementation uses buffered
* transfer.
*
* Michael Zapf, Feb 2010
*
* TODO: Link to imgtool. imgtool still uses its own format creation
*
* FIXME: If image is broken with good first track and defect higher tracks,
* tdf_guess_geometry fails to define a geometry. The guessing should
* always be done.
********************************************************************/
#include <string.h>
#include <assert.h>
#include <time.h>
#include "imageutl.h"
#include "ti99_dsk.h"
#define SECTOR_SIZE 256
#define TI99_IDAM_LENGTH 7
/*
Determines whether we are using 80 track drives. This variable is
necessary unless we get information about the dip
switch settings. It is set by the disk controller implementations
(bwg, hfdc, ti_fdc).
*/
static int use_80_track_drives = FALSE;
struct ti99dsk_geometry
{
UINT8 sides;
UINT8 tracksperside;
UINT8 secspertrack;
UINT8 density;
};
struct ti99dsk_tag
{
int heads;
int tracks;
int sectors;
int track_size; // Complete track; to be calculated
int format;
int first_idam;
int dam_offset;
};
/*
Parametrizes the format to use 80 track drives. For TI systems we
need to be able to emulate 40 track drives and 80 track drives.
This is set in the configuration, but the format has no direct access.
So as a preliminary solution we use this global function which sets the
flag. This function is called by the disk controller implementations
(bwg, hfdc, ti_fdc).
*/
void ti99_set_80_track_drives(int use80)
{
use_80_track_drives = use80;
}
#define TI99_DSK_TAG "ti99dsktag"
#define TI99DSK_BLOCKNOTFOUND -1
/*
Searches a block containing number * byte, starting at the given
position. Returns the position of the first byte of the block.
*/
static int find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number)
{
int i = start;
size_t current = number;
while (i < stop && current > 0)
{
if (buffer[i++] != byte)
{
current = number;
}
else
{
current--;
}
}
if (current==0)
{
return i - number;
}
else
return TI99DSK_BLOCKNOTFOUND;
}
/*
Queried by flopdrv. Instead of the image tracks, we return the tracks
of the floppy drive. We assume we always have two heads.
*/
static int ti99_get_heads_per_disk(floppy_image_legacy *floppy)
{
// struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
// return tag->heads;
return 2;
}
/*
Queried by flopdrv. Instead of the image tracks, we return the tracks
of the floppy drive.
*/
static int ti99_get_tracks_per_disk(floppy_image_legacy *floppy)
{
int drivetracks = 42;
// This may fail on startup; therefore we use a special function in
// in the floppy controller implementations to explicitly set the geometry.
if (use_80_track_drives)
{
drivetracks = 83;
}
return drivetracks;
}
static floperr_t ti99_get_sector_length(floppy_image_legacy *floppy, int head, int track, int sector, UINT32 *sector_length)
{
*sector_length=SECTOR_SIZE;
return FLOPPY_ERROR_SUCCESS;
}
/*
Return the track size. We do not allow different track
sizes, so we store the track size in the tag.
*/
static UINT32 ti99_get_track_size(floppy_image_legacy *floppy, int head, int track)
{
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
return tag->track_size;
}
/*
static void create_vib(UINT8 *sector0, option_resolution *params)
{
const char *name = "UNNAMED ";
int sides, tracksperside,secspertrack,totalsectors,ausize,allocbytes,i;
enum { NAME=0, TOTALSECS=10, SECSPERTRACK=11, SIG=12, PROT=16, TRACKS=17, SIDES=18, DENSITY=19, EXT=20, ALLOC=56 };
sides = option_resolution_lookup_int(params, PARAM_HEADS);
tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS);
secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS);
totalsectors = sides * tracksperside * secspertrack;
memcpy(§or0[NAME], name, 10);
sector0[TOTALSECS] = (totalsectors>>8)&0xff;
sector0[TOTALSECS+1] = (totalsectors&0xff);
sector0[SECSPERTRACK] = secspertrack;
sector0[SIG] = 'D';
sector0[SIG+1] = 'S';
sector0[SIG+2] = 'K';
sector0[PROT] = ' ';
sector0[TRACKS] = tracksperside;
sector0[SIDES] = sides;
sector0[DENSITY] = (secspertrack<10)? 0x01 : 0x02;
for (i=EXT; i < ALLOC; i++)
sector0[i] = 0;
ausize = (totalsectors / 1441) + 1;
allocbytes = totalsectors / ausize / 8;
for (i=ALLOC; i < ALLOC+allocbytes; i++)
sector0[i] = 0;
while (i++ < SECTOR_SIZE)
sector0[i] = 0xff;
}
*/
/* =========================================================================
Sector dump format
=========================================================================*/
static floperr_t ti99_sdf_read_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen);
static floperr_t ti99_sdf_write_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam);
/* -----------------------------------------------------------------------
* Guess the geometry of the sector dump disk.
*
* Sector size is always 256 bytes.
*
* Common formats:
* 90 KiB = 40 tracks / 1 side / 9 sectors = SSSD
* 180 KiB = 40 tracks / 2 side / 9 sectors = DSSD (most common)
* = 40 tracks / 1 side / 18 sectors = SSDD (rare)
* = 80 tracks / 1 side / 9 sectors = SSSD80 (rare)
* 360 KiB = 40 tracks / 2 side / 18 sectors = DSDD (most common)
* = 80 tracks / 2 side / 9 sectors = DSSD80 (rare)
* = 80 tracks / 1 side / 18 sectors = SSDD80 (rare)
* = 40 tracks / 1 side / 36 sectors = SSHD (rare)
* 720 KiB = 80 tracks / 2 side / 18 sectors = DSDD80 (most common)
* 1440 KiB= 80 tracks / 2 side / 36 sectors = DSHD80 (most common)
*
* Moreover, there may be formats with 35 tracks (ancient) and 8 sectors/track.
* We only check for the 8 sector formats.
* 160 KiB = 40 tracks / 1 side / 16 sectors = SSDD8
* 320 KiB = 40 tracks / 2 side / 16 sectors = DSDD8
*
* The Volume Information Block may contain suitable information, but not
* before the disk is formatted and initialized.
*
* returns 100 if recognized, 0 if not recognized
* ----------------------------------------------------------------------- */
static int ti99_sdf_guess_geometry(floppy_image_legacy *floppy, UINT64 size,
struct ti99dsk_geometry *geometry)
{
int totsecs;
typedef struct ti99_vib
{
char name[10]; // volume name (10 characters, pad with spaces)
UINT8 totsecsMSB; // disk length in sectors (big-endian) (usually 360, 720 or 1440)
UINT8 totsecsLSB;
UINT8 secspertrack; // sectors per track (usually 9 (FM) or 18 (MFM))
UINT8 id[3]; // String "DSK"
UINT8 protection; // 'P' if disk is protected, ' ' otherwise.
UINT8 tracksperside; // tracks per side (usually 40)
UINT8 sides; // sides (1 or 2)
UINT8 density; // 0,1 (FM) or 2,3,4 (MFM)
UINT8 res[36]; // Empty for traditional disks, or up to 3 directory pointers
UINT8 abm[200]; // allocation bitmap: a 1 for each sector in use (sector 0 is LSBit of byte 0,
// sector 7 is MSBit of byte 0, sector 8 is LSBit of byte 1, etc.)
} ti99_vib;
ti99_vib vib;
UINT32 file_size;
struct ti99dsk_geometry dummy_geometry;
if (geometry)
memset(geometry, 0, sizeof(*geometry));
else
geometry = &dummy_geometry;
if (size > 0xFFFFFFFF)
return 0;
file_size = (UINT32) size;
// Read the Volume Information Block
floppy_image_read(floppy, &vib, 0, sizeof(vib));
// If we have read the sector successfully, let us parse it
totsecs = (vib.totsecsMSB << 8) | vib.totsecsLSB;
geometry->secspertrack = vib.secspertrack;
if (geometry->secspertrack == 0)
// Some images might be like this, because the original SSSD
// TI controller always assumes 9.
geometry->secspertrack = 9;
geometry->tracksperside = vib.tracksperside;
if (geometry->tracksperside == 0)
// Some images are like this, because the original SSSD TI controller
// always assumes 40.
geometry->tracksperside = 40;
geometry->sides = vib.sides;
if (geometry->sides == 0)
// Some images are like this, because the original SSSD TI controller
// always assumes that tracks beyond 40 are on side 2. */
geometry->sides = totsecs / (geometry->secspertrack * geometry->tracksperside);
geometry->density = vib.density;
// check that the format makes sense
if (((geometry->secspertrack * geometry->tracksperside * geometry->sides) == totsecs)
&& (geometry->density <= 4) && (totsecs >= 2) && (! memcmp(vib.id, "DSK", 3))
&& (file_size == totsecs*256))
{
LOG_FORMATS("SDF/VIB consistent; tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack);
return 100;
}
LOG_FORMATS("SDF/VIB not consistent; guessing format\n");
// So that was not consistent. We guess the size from the file size
// and assume that the VIB did not contain reliable data. For the
// ambiguous case we choose the most common format.
// Adding support for another sector image format which adds 768 bytes
// as a bad sector map
if ((file_size / 256) % 10 == 3)
{
LOG_FORMATS("Stripping map of bad sectors at image end\n");
file_size -= 768;
}
switch (file_size)
{
case 1*40*9*256:
// 90kbytes: SSSD
case 0:
/*default:*/
geometry->sides = 1;
geometry->tracksperside = 40;
geometry->secspertrack = 9;
geometry->density = 1;
break;
case 2*40*9*256:
// 180kbytes: either DSSD or 18-sector-per-track SSDD.
// We assume DSSD since DSSD is more common and is supported by
// the original TI SD disk controller.
geometry->sides = 2;
geometry->tracksperside = 40;
geometry->secspertrack = 9;
geometry->density = 1;
break;
case 1*40*16*256:
// 160kbytes: 16-sector-per-track SSDD (standard format for TI
// DD disk controller prototype, and the TI hexbus disk
// controller?) */
geometry->sides = 1;
geometry->tracksperside = 40;
geometry->secspertrack = 16;
geometry->density = 2;
break;
case 2*40*16*256:
// 320kbytes: 16-sector-per-track DSDD (standard format for TI
// DD disk controller prototype, and TI hexbus disk
// controller?)
geometry->sides = 2;
geometry->tracksperside = 40;
geometry->secspertrack = 16;
geometry->density = 2;
break;
case 2*40*18*256:
// 360kbytes: 18-sector-per-track DSDD (standard format for most
// third-party DD disk controllers, but reportedly not supported by
// the original TI DD disk controller prototype)
geometry->sides = 2;
geometry->tracksperside = 40;
geometry->secspertrack = 18;
geometry->density = 2;
break;
case 2*80*18*256:
// 720kbytes: 18-sector-per-track 80-track DSDD (Myarc only)
geometry->sides = 2;
geometry->tracksperside = 80;
geometry->secspertrack = 18;
geometry->density = 2;
break;
case 2*80*36*256:
// 1.44Mbytes: DSHD (Myarc only)
geometry->sides = 2;
geometry->tracksperside = 80;
geometry->secspertrack = 36;
geometry->density = 3;
break;
default:
LOG_FORMATS("Unrecognized disk image geometry\n");
return 0;
}
LOG_FORMATS("SDF geometry guess: tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack);
return 100;
}
/*
Figure out the interleave for the sector arrangement in the track. For
SDF format this is required for the read_track function.
Interleave:
0 7 5 3 1 8 6 4 2;
start with 0, 3, 6, 0, 3, 6, ...
0 11 4 15 8 1 12 5 16 9 2 13 6 17 10 3 14 7
start with 0 for every track
Rare formats: 8 sectors
0 3 6 1 4 7 2 5 (i*3)%8
16 sectors
0 9 2 11 4 13 6 15 8 1 10 3 12 5 14 7 (i*9)%16
36 sectors
0 11 22 ... 3 14 25 (i*11)%36
*/
static void guess_interleave(int sectors, int *step, int *initial)
{
switch (sectors)
{
case 8:
*step = 3;
/* initial is the offset from track to track+1 of the starting sector number */
*initial = 0;
break;
case 9:
*step = 7;
*initial = 3;
break;
case 16:
*step = 9;
*initial = 0;
break;
case 18:
*step = 11;
*initial = 0;
break;
case 36:
*step = 11;
*initial = 0;
break;
default:
*step = 11;
*initial = 0;
break;
}
}
/*
Delivers a whole track. As the image consists only of
sector data, we must reconstruct the track data and
then insert the sector data.
The track layout is exactly the Track Dump Format, except that the
CRC values must be correctly calculated.
Total length (FM) = 247 + sectors*334; (sectors=8 or 9)
Total length (MFM) = 752 + sectors*340; (sectors=16 or 18)
*/
static floperr_t ti99_sdf_read_track(floppy_image_legacy *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
{
int sector;
int startstep;
int step;
int position=0;
unsigned short crc;
int i;
floperr_t err;
int imgtrack = track;
UINT8 *trackdata = (UINT8*)buffer;
/* Determine from the image geometry whether we have FM or MFM */
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
// Do we have a 40 track disk in an 80 track drive? In that case we
// double the "width" of the tracks. */
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
guess_interleave(tag->sectors, &step, &startstep);
if (tag->sectors < 10) /* must be FM */
{
/* Write lead-in. */
memset(trackdata, 0x00, 16);
position += 16;
for (i=0; i < tag->sectors; i++)
{
/* Create interleaving */
sector = (i*step + imgtrack*startstep) % tag->sectors;
memset(&trackdata[position], 0x00, 6);
position += 6;
/* Set IDAM */
trackdata[position++] = 0xfe;
trackdata[position++] = imgtrack;
trackdata[position++] = head;
trackdata[position++] = sector;
trackdata[position++] = 0x01;
/* Calculate CRC16. Preset to 0xffff (what is 0xcdb4?) */
crc = ccitt_crc16(0xffff, &trackdata[position-(TI99_IDAM_LENGTH-2)], TI99_IDAM_LENGTH - 2);
trackdata[position++] = (crc>>8)&0xff;
trackdata[position++] = crc & 0xff;
/* Write Gap2 */
memset(&trackdata[position], 0xff, 11);
position += 11;
memset(&trackdata[position], 0x00, 6);
position += 6;
/* Write DAM */
trackdata[position++] = 0xfb;
/* Locate the sector content in the image and load it. */
err = ti99_sdf_read_sector(floppy, head, imgtrack, sector, &trackdata[position], SECTOR_SIZE);
if (err)
return err;
position += SECTOR_SIZE;
/* Set CRC16 */
crc = ccitt_crc16(0xffff, &trackdata[position-(SECTOR_SIZE+1)], SECTOR_SIZE+1);
trackdata[position++] = (crc>>8)&0xff;
trackdata[position++] = crc & 0xff;
/* Write Gap3 */
memset(&trackdata[position], 0xff, 45);
position += 45;
}
/* Write lead-out */
memset(&trackdata[position], 0xff, 231);
position += 231;
}
else /* we have MFM */
{
/* Write lead-in. */
memset(trackdata, 0x4e, 40);
position += 40;
for (i=0; i < tag->sectors; i++)
{
sector = (i*step + imgtrack*startstep) % tag->sectors;
memset(&trackdata[position], 0x00, 10);
position += 10;
/* Write sync */
memset(&trackdata[position], 0xa1, 3);
position += 3;
/* Set IDAM */
trackdata[position++] = 0xfe;
trackdata[position++] = imgtrack;
trackdata[position++] = head;
trackdata[position++] = sector;
trackdata[position++] = 0x01;
/* Set CRC16 */
crc = ccitt_crc16(0xffff, &trackdata[position-TI99_IDAM_LENGTH+2], TI99_IDAM_LENGTH - 2);
trackdata[position++] = (crc>>8)&0xff;
trackdata[position++] = crc & 0xff;
/* Write Gap2 */
memset(&trackdata[position], 0x4e, 22);
position += 22;
memset(&trackdata[position], 0x00, 12);
position += 12;
/* Write sync */
memset(&trackdata[position], 0xa1, 3);
position += 3;
/* Write DAM */
trackdata[position++] = 0xfb;
/* Locate the sector content in the image and load it. */
err = ti99_sdf_read_sector(floppy, head, track, sector, &trackdata[position], SECTOR_SIZE);
if (err)
return err;
position += SECTOR_SIZE;
/* Set CRC16 */
crc = ccitt_crc16(0xffff, &trackdata[position-SECTOR_SIZE-1], SECTOR_SIZE+1);
trackdata[position++] = (crc>>8)&0xff;
trackdata[position++] = crc & 0xff;
/* Write Gap3 */
memset(&trackdata[position], 0x4e, 24);
position += 24;
}
/* Write lead-out */
memset(&trackdata[position], 0x4e, 712);
position += 712;
}
return FLOPPY_ERROR_SUCCESS;
}
/*
Writes a whole track. As the sector dump format does not include track
data, we have to extract the sector contents and to write them
in sequence into the image. All the rest (gaps, crc, sync bytes) are
dropped here.
Bytes written to locations beyond the image end will be droppped
silently (as if the medium is unwritable from that point).
*/
static floperr_t ti99_sdf_write_track(floppy_image_legacy *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
{
int current_pos = 0;
UINT8 *track_image;
int leadin, gap1, gap2;
int is_fm, found;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
int imgtrack = track;
// Find out if we are going to write an FM or MFM track. Note that the
// original TI controller is known to have a bug in the formatting
// routine (using a MOVB instead of a MOV instruction when setting the
// pointer to the track data), outputting false bytes at the
// beginning of the track. So we do not rely on absolute positions
// (and neither does the controller).
track_image = (UINT8*)buffer;
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
/* Only search in the first 100 bytes for the start. */
leadin = 40;
gap1 = 10;
gap2 = 12;
is_fm = FALSE;
current_pos = find_block(track_image, 0, 100, 0x4e, leadin);
// In case of defect formats, we continue as far as possible. This
// may lead to sectors not being written. */
if (current_pos==TI99DSK_BLOCKNOTFOUND)
{
/* Not MFM, possibly FM image */
/* Again, find Lead-in */
leadin = 16;
current_pos = find_block(track_image, 0, 100, 0x00, leadin);
if (current_pos==TI99DSK_BLOCKNOTFOUND)
{
/* If neither, forget about this process completely. */
LOG_FORMATS("Cannot find lead-in for track %d, head %d.\n", track, head);
return FLOPPY_ERROR_INVALIDIMAGE;
}
gap1 = 6;
gap2 = 6;
is_fm = TRUE;
}
found = FALSE;
/* Get behind lead-in */
current_pos += leadin;
while (current_pos < buflen)
{
/* We must find the address block to determine the sector. */
int new_pos = find_block(track_image, current_pos, buflen, 0x00, gap1);
if (new_pos==TI99DSK_BLOCKNOTFOUND)
{
/* Forget about the rest. */
if (found) break; /* we were already successful, so all ok */
LOG_FORMATS("Cannot find gap1 for track %d, head %d.\n", track, head);
return FLOPPY_ERROR_INVALIDIMAGE;
}
found = TRUE;
if (!is_fm)
new_pos += 3; /* skip sync bytes in MFM */
if (track_image[new_pos + gap1]==0xfe)
{
current_pos = new_pos + gap1 + 1;
/* IDAM found. */
int wtrack = track_image[current_pos];
int whead = track_image[current_pos+1];
int sector = track_image[current_pos+2];
if (wtrack == imgtrack && whead == head)
{
/* We skip the first part of gap2. */
new_pos = find_block(track_image, current_pos, buflen, 0x00, gap2);
if (current_pos==TI99DSK_BLOCKNOTFOUND)
{
LOG_FORMATS("Cannot find gap2 for track %d, head %d.\n", imgtrack, head);
return FLOPPY_ERROR_INVALIDIMAGE;
}
else
{
new_pos += gap2;
if (!is_fm)
new_pos += 3; /* skip sync */
current_pos = new_pos;
if (track_image[current_pos]==0xfb)
{
/* DAM found. We now write the sector content to the image. */
ti99_sdf_write_sector(floppy, head, track, sector, &track_image[current_pos+1], SECTOR_SIZE, 0);
current_pos += SECTOR_SIZE;
}
else
LOG_FORMATS("DAM not found. Not writing write sector %d\n", sector);
/* else not found, ignore this sector. */
}
}
else
{
// else the sector head data do not match the
// current track and head. Ignore the sector.
LOG_FORMATS("Wrong track: wtrack=%d, imgtrack=%d, whead=%d, imghead=%d\n",wtrack,imgtrack, whead,head);
}
}
else
{
/* One step forward. Retry. */
current_pos++;
}
}
return FLOPPY_ERROR_SUCCESS;
}
/*
This method creates a properly formatted image.
For a proper image, we need to format all tracks, and to create a
minimal filesystem:
name = "UNNAMED "
number of sectors
sectors/track
"DSK"
protection = 0x00
tracks/side
sides
density (0,1 = FM, 2 = MFM)
0x14 - 0x37 : 0x00
Allocation bitmap: 0xFF except for the first #sectors/AU size
AU size = 1 (360 - 1440 sectors)
= 2 (1441 - 2880 sectors)
= 4 (2881 - 5760 sectors)
= 8 (5760 - 11520 sectors)
sector 0 =
sector 1 = 256* 0x00
*/
/* static floperr_t ti99_sdf_format_track(floppy_image_legacy *floppy, int head, int track, option_resolution *params)
{
UINT8 *sector0 = (UINT8*)malloc(SECTOR_SIZE);
create_vib(sector0, params);
free(sector0);
if (true)
{
printf("not implemented\n");
return FLOPPY_ERROR_SEEKERROR;
}
return FLOPPY_ERROR_SUCCESS;
}
*/
/*
Get the offset from the start of the image file. In this format, logical
tracks on two-sided disks are arranged as follows (example for 40
tracks; accordingly for 35 or 80)
head-track
0-0
0-1
0-2
0-3
...
0-39
1-39
1-38
1-37
...
1-0
Note that we take the imgtrack, which may be half of the drive track.
*/
static floperr_t ti99_sdf_get_offset(floppy_image_legacy *floppy, int head, int imgtrack, int sector, UINT64 *offset)
{
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
if ((head < 0) || (head >= tag->heads)
|| (imgtrack < 0) || (imgtrack >= tag->tracks)
|| (sector < 0) || (sector > tag->sectors))
return FLOPPY_ERROR_SEEKERROR;
if (head == 0) /* track numbers increasing towards inner track */
{
*offset = (imgtrack * tag->sectors + sector) * SECTOR_SIZE;
}
else /* track numbers increasing towards outer track */
{
*offset = (((2*tag->tracks)-1-imgtrack) * tag->sectors + sector) * SECTOR_SIZE;
}
return FLOPPY_ERROR_SUCCESS;
}
/*
Read one sector at the specified position.
*/
static floperr_t ti99_sdf_read_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
floperr_t err;
UINT64 offset;
int imgtrack = track;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset);
if (err)
{
return err;
}
floppy_image_read(floppy, buffer, offset, SECTOR_SIZE);
return FLOPPY_ERROR_SUCCESS;
}
/*
For the SDF format, sectors are always numbered in ascending order,
starting with the lowest numbered sector in each track. In other words,
we have nothing to calculate here.
*/
static floperr_t ti99_sdf_read_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
return ti99_sdf_read_sector(floppy, head, track, sector, buffer, buflen);
}
/*
Write one sector at the specified position.
*/
static floperr_t ti99_sdf_write_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
{
UINT64 offset;
floperr_t err;
int imgtrack = track;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset);
if (err)
{
return err;
}
floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
return FLOPPY_ERROR_SUCCESS;
}
/*
See above; indexing and numbering coincide.
*/
static floperr_t ti99_sdf_write_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
{
return ti99_sdf_write_sector(floppy, head, track, sector, buffer, buflen, ddam);
}
/*
Required for ReadAddress command
*/
static floperr_t ti99_sdf_get_indexed_sector_info(floppy_image_legacy *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
{
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
if (use_80_track_drives && tag->tracks<=40)
{
track = track / 2;
}
if (sector_length)
*sector_length=SECTOR_SIZE;
if (cylinder)
*cylinder = track;
if (side)
*side = head;
if (sector)
*sector = sector_index;
if (flags)
*flags = 0;
if (sector_index >= tag->sectors)
{
return FLOPPY_ERROR_SEEKERROR;
}
return FLOPPY_ERROR_SUCCESS;
}
/*
Try to identify the image as an SDF format image.
*/
static FLOPPY_IDENTIFY(ti99_sdf_identify)
{
UINT64 size;
size = floppy_image_size(floppy);
*vote = ti99_sdf_guess_geometry(floppy, size, NULL);
LOG_FORMATS("SDF voting %d\n", *vote);
return FLOPPY_ERROR_SUCCESS;
}
/*
Create the SDF data structures and set the callbacks.
*/
static FLOPPY_CONSTRUCT(ti99_sdf_construct)
{
struct ti99dsk_geometry geometry;
struct FloppyCallbacks *callbacks;
struct ti99dsk_tag *tag;
LOG_FORMATS("Reading image as SDF\n");
if (params)
{
/* create */
memset(&geometry, 0, sizeof(geometry));
geometry.sides = option_resolution_lookup_int(params, PARAM_HEADS);
geometry.tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS);
geometry.secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS);
/* We don't have headers for geometry */
/* check for usage in imgtool - we want to be able to create useful disks */
}
else
{
/* load */
if (ti99_sdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50)
return FLOPPY_ERROR_INVALIDIMAGE;
}
assert(geometry.sides);
assert(geometry.tracksperside);
assert(geometry.secspertrack);
tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag));
if (!tag)
return FLOPPY_ERROR_OUTOFMEMORY;
tag->heads = geometry.sides;
tag->tracks = geometry.tracksperside;
tag->sectors = geometry.secspertrack;
if (tag->sectors < 10)
/* FM mode */
tag->track_size = 16 + tag->sectors*334 + 231;
else
tag->track_size = 40 + tag->sectors*340 + 712;
/* set up format callbacks */
callbacks = floppy_callbacks(floppy);
callbacks->read_sector = ti99_sdf_read_sector;
callbacks->write_sector = ti99_sdf_write_sector;
callbacks->read_indexed_sector = ti99_sdf_read_indexed_sector;
callbacks->write_indexed_sector = ti99_sdf_write_indexed_sector;
callbacks->read_track = ti99_sdf_read_track;
callbacks->write_track = ti99_sdf_write_track;
/* callbacks->format_track = ti99_sdf_format_track; */
callbacks->get_track_size = ti99_get_track_size;
/* post format unset */
callbacks->get_heads_per_disk = ti99_get_heads_per_disk;
callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk;
callbacks->get_sector_length = ti99_get_sector_length;
callbacks->get_indexed_sector_info = ti99_sdf_get_indexed_sector_info;
return FLOPPY_ERROR_SUCCESS;
}
/* -----------------------------------------------------------------------
* Track Dump Format, aka PC99 format
* This format includes all track data, including address marks and gaps.
* However, CRC is not stored; instead, a f7f7 is stored at that location.
* Two track images exist, one for FM and one for MFM format.
*
* In this implementation, however, we do calculate the CRC.
*
* All tracks have exactly the same size, so offsets are calculated easily.
*
* This format is a precise map of the data contents of the track;
* however, it does not store clock patterns.
*
* The TDF format does not have a special index part, so any sector on it
* must be searched (as done by the disk controller)
*
* Interesting detail: The original TI disk controller has a bug which has
* just recently been discovered (using MESS!): The lead-in has another 9
* bytes as a prefix; this is due to a wrongly set pointer in the format
* routine. Strictly speaking, it produces invalid track images.
* The original hardware does not care, however, so neither should we.
* To keep the emulation realistic (and to allow for a
* possible future extension with clock bits), we search for the
* beginning of the track. From there on, however, we accept no further
* deviations. This may make the resulting format unusable with the PC99
* emulator, but it only appears when formatting with the TI disk
* controller anyway. If the image is created with another disk controller or
* using imgtool, the format is correctly created.
*
* FM track image
*
* Lead-in: 16* 00
* Pre-ID gap: 6* 00 ---+
* IDAM: fe |
* Track: tt |
* Head: hh |
* Sector: ss |
* Size: 01 |
* CRC: 2* f7 +--- repeat 9 times (8 in rare cases)
* Gap2: 11* ff |
* 6* 00 |
* DAM: fb |
* Content: 256* xx |
* CRC: 2* f7 |
* Gap3: 45* ff ---+
* Lead-out: 231* ff
*
* --------------
*
* MFM track image
*
* Lead-in: 40* 4e
* Pre-ID gap: 10* 00 ---+
* Sync: 3* a1 |
* IDAM: fe |
* Track: tt |
* Head: hh |
* Sector: ss |
* Size: 01 |
* CRC: 2* f7 +--- repeat 18 times (16 in rare cases)
* Gap2: 22* 4e |
* 12* 00 |
* Sync: 3* a1 |
* DAM: fb |
* Content: 256* xx |
* CRC: 2* f7 |
* Gap3: 24* 4e ---+
* Lead-out: 712* 4e
*
* ------------------
* The tracks are located on the image as follows (different to SDF!):
*
* head-track
* 0-0
* 0-1
* 0-2
* 0-3
* ...
* 0-39
* 1-0
* 1-1
* 1-2
* ...
* 1-39
* ----------------------------------------------------------------------- */
#define TI99_FM 1
#define TI99_MFM 2
/* For the emulation of the clock pattern, we add a 9th bit. */
#define TI99_DAM 0x1fb
#define TI99_IDAM 0x1fe
#define TI99_FM_DAM_OFFSET 24
#define TI99_MFM_DAM_OFFSET 44
#define TI99_FM_TRACK_ADD 247
#define TI99_MFM_TRACK_ADD 752
#define TI99_FM_TOTAL_SECTOR 334
#define TI99_MFM_TOTAL_SECTOR 340
/*
Determine offset of the first IDAM. This is the base of the simulation
of the clock bytes. We use a heuristic to guess where the track begins
and simply check whether we can locate the first IDAM and DAM. Return
value is the offset to the first IDAM.
*/
static floperr_t determine_offset(int format, UINT8 *track, int *offset)
{
floperr_t retval;
int current_pos;
if (format==TI99_FM)
{
// printf("Trying FM\n");
// We are seeking the lead-in and pre-id gap from
// position 0 to position 50. This is just to make sure that
// if there are bad bytes at the beginning (original TI controller)
// we will skip them.
current_pos = find_block(track, 0, 50, 0x00, 22);
if (current_pos==TI99DSK_BLOCKNOTFOUND)
{
LOG_FORMATS("Lead-in not found\n");
retval = FLOPPY_ERROR_SEEKERROR;
}
else
{
current_pos += 22;
if (track[current_pos]==0xfe /* IDAM */ &&
track[current_pos+4]==0x01 /* sector length, always 256 for TI */ &&
track[current_pos+24]==0xfb) /* DAM */
{
/* We're pretty sure this is the beginning. */
*offset = current_pos;
retval = FLOPPY_ERROR_SUCCESS;
}
else
{
LOG_FORMATS("IDAM or DAM not found\n");
retval = FLOPPY_ERROR_SEEKERROR;
}
}
}
else
{
/* MFM */
// printf("Trying MFM\n");
int track_start = find_block(track, 0, 100, 0x4e, 40);
if (track_start==TI99DSK_BLOCKNOTFOUND)
{
LOG_FORMATS("Lead-in not found\n");
retval = FLOPPY_ERROR_SEEKERROR;
}
else
{
track_start += 40;
current_pos = find_block(track, track_start, track_start+10, 0x00, 10);
if (current_pos==TI99DSK_BLOCKNOTFOUND)
{
LOG_FORMATS("Pre-gap not found\n");
retval = FLOPPY_ERROR_SEEKERROR;
}
else
{
current_pos += 10;
if (track[current_pos] == 0xa1 /* First sync */ &&
track[current_pos+3] == 0xfe /* IDAM */ &&
track[current_pos+44] == 0xa1 /* First sync */ &&
track[current_pos+47] == 0xfb) /* DAM */
{
/* We're pretty sure this is the beginning. */
*offset = current_pos + 3;
retval = FLOPPY_ERROR_SUCCESS;
}
else
{
LOG_FORMATS("Sync/IDAM/DAM not found\n");
retval = FLOPPY_ERROR_SEEKERROR;
}
}
}
}
/* printf("TDF offset = 0x%x\n", *offset); */
return retval;
}
/*
Gets a byte from the track. In this format we do not have a clock
signal, so we will simulate it. Thus, this function returns a 9-bit
value, with the first bit set as a marker for an address mark.
This may be a little more complicated than necessary, but it correctly
emulates the behavior of the controller (and it allows for an
alternative format which actually supports clock and data).
*/
static int read_byte(int format, int first_idam, UINT8 *track, int position)
{
int totalseclen;
int dam_offset;
int byte;
/* Check whether we are at any IDAM or DAM in this track. */
if (format==TI99_FM)
{
totalseclen = TI99_FM_TOTAL_SECTOR;
dam_offset = TI99_FM_DAM_OFFSET;
}
else
{
totalseclen = TI99_MFM_TOTAL_SECTOR;
dam_offset = TI99_MFM_DAM_OFFSET;
}
int curpos = (position - first_idam) % totalseclen;
if (curpos==0 && track[position]==0xfe)
byte = TI99_IDAM;
else
{
if (curpos==dam_offset && track[position]==0xfb)
byte = TI99_DAM;
else
byte = track[position] & 0xff;
}
return byte;
}
/*
Determine the position of this track in the image.
*/
static floperr_t ti99_tdf_get_offset(floppy_image_legacy *floppy, int head, int imgtrack, UINT64 *offset)
{
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
if ((head < 0) || (head >= tag->heads) || (imgtrack < 0) || (imgtrack >= tag->tracks))
return FLOPPY_ERROR_SEEKERROR;
// For head 0, tracks increase from 0 to tracks-1; then for head 1,
// tracks also increase from 0 to tracks-1. */
*offset = head * (tag->tracks * tag->track_size) + imgtrack * tag->track_size;
return FLOPPY_ERROR_SUCCESS;
}
/*
Reads a track. We just copy the contents from the format and recreate
the CRC if it is the "blank" CRC as F7F7.
This method assumes that the track division be done already.
*/
static floperr_t ti99_tdf_read_track_internal(floppy_image_legacy *floppy, int head, int imgtrack, UINT64 offset, void *buffer, size_t buflen)
{
floperr_t err;
UINT64 track_offset;
int first_idam = 0;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
int i, byte, crc;
UINT8 *track_data = (UINT8*)buffer;
err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
if (err)
return err;
floppy_image_read(floppy, buffer, track_offset, buflen);
// Rebuild the CRCs. PC99 did not store the CRC but put
// F7F7 in its place.
// The first CRC is at position IDAM + 5
// The second CRC is at position IDAM + 0x119 (FM) or +0x12d (MFM)
// All this is repeated for each sector in the track
if (determine_offset(tag->format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
return FLOPPY_ERROR_SEEKERROR;
i = 0;
while (i < buflen)
{
byte = read_byte(tag->format, first_idam, track_data, i);
if (byte == TI99_IDAM)
{
// Do we have a valid CRCs already? Then this image
// already contains proper CRCs handling. Do not recreate them.
if (track_data[i+5] != 0xf7 || track_data[i+6] != 0xf7)
return FLOPPY_ERROR_SUCCESS;
crc = ccitt_crc16(0xffff, &track_data[i], 5);
track_data[i+5] = (crc>>8) & 0xff;
track_data[i+6] = (crc & 0xff);
}
else
{
if (byte == TI99_DAM)
{
crc = ccitt_crc16(0xffff, &track_data[i], SECTOR_SIZE+1);
track_data[i+SECTOR_SIZE+1] = (crc>>8) & 0xff;
track_data[i+SECTOR_SIZE+2] = (crc & 0xff);
}
}
i++;
}
// dump_contents(track_data, buflen);
return FLOPPY_ERROR_SUCCESS;
}
/*
Reads a track. We just copy the contents from the format, without
changes.
*/
static floperr_t ti99_tdf_read_track(floppy_image_legacy *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen)
{
int imgtrack = track;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
return ti99_tdf_read_track_internal(floppy, head, imgtrack, offset, buffer, buflen);
}
/*
Writes a track.
*/
static floperr_t ti99_tdf_write_track(floppy_image_legacy *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen)
{
floperr_t err;
UINT64 track_offset;
int imgtrack = track;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
if (err)
return err;
// According to the TDF format, we don't need the CRC but replace it
// with F7F7. For now we keep it and see whether PC99 can cope with
// that. (Otherwise we would have to copy the const buffer). */
floppy_image_write(floppy, buffer, offset + track_offset, buflen);
return FLOPPY_ERROR_SUCCESS;
}
/*
static floperr_t ti99_tdf_format_track(floppy_image_legacy *floppy, int head, int track, option_resolution *params)
{
int sectors;
int sector_length;
int interleave;
int first_sector_id;
sectors = option_resolution_lookup_int(params, PARAM_SECTORS);
sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH);
interleave = option_resolution_lookup_int(params, PARAM_INTERLEAVE);
first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID);
return FLOPPY_ERROR_SUCCESS;
}
*/
/*
Get a sector from a track. This function takes a complete track,
searches for the sector, and passes a pointer to the sector contents
back.
As the original TI controller creates bad bytes at the start of a track
we do *not* assume a simple layout. The strategy is to search the
lead-in of the track, and from this offset, step through the track
in search for the sector. Since we do not have index marks (we cannot
distinguish them from ordinary data, as we do not store the clock), we
do not attempt to detect IDAM or DAM.
*/
static floperr_t ti99_tdf_seek_sector_in_track(floppy_image_legacy *floppy, int head, int track, int sector, UINT8 *track_data, UINT8 **sector_data)
{
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
int byte;
int state;
enum { SEARCHIDAM, SEARCHDAM };
state = SEARCHIDAM;
if (tag->first_idam==0) /* should we check for every track? */
{
if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
{
LOG_FORMATS("could not determine offset\n");
return FLOPPY_ERROR_SEEKERROR;
}
}
for (int i=0; i < tag->track_size; i++)
{
byte = read_byte(tag->format, tag->first_idam, track_data, i);
switch (state)
{
case SEARCHIDAM:
/* search for the IDAM */
if (byte==TI99_IDAM)
{
if ((track_data[i+1]==track) && (track_data[i+2]==head) && (track_data[i+3]==sector))
state = SEARCHDAM;
else
state = SEARCHIDAM;
}
break;
case SEARCHDAM:
if (byte==TI99_DAM)
{
*sector_data = &track_data[i+1];
return FLOPPY_ERROR_SUCCESS;
}
break;
}
}
LOG_FORMATS("Sector not found.\n");
return FLOPPY_ERROR_SEEKERROR;
}
static floperr_t ti99_tdf_read_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
floperr_t err;
UINT8 *sector_data;
UINT8 *track_data;
int imgtrack = track;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
track_data = (UINT8*)malloc(tag->track_size);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
if (err)
return err;
err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, §or_data);
if (err)
return err;
/* verify CRC? */
memcpy(buffer, sector_data, SECTOR_SIZE);
free(track_data);
return FLOPPY_ERROR_SUCCESS;
}
/*
Writes a sector into the track. Note that the indexed_write method
may be used instead of this method, according to the implementation
of flopimg/flopdrv.
*/
static floperr_t ti99_tdf_write_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
{
floperr_t err;
UINT8 *sector_data;
UINT8 *track_data;
UINT64 track_offset;
int imgtrack = track;
UINT64 offset;
UINT8 crc_field[2];
int crc;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
track_data = (UINT8*)malloc(tag->track_size);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
if (err)
return err;
err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, §or_data);
if (err)
return err;
err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
if (err)
return err;
offset = track_offset + (sector_data - track_data);
floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
// Recalculate CRC. We init the CRC with bf84 (DAM fb is
// already included; would be ffff without)
crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
crc_field[0] = (crc>>8) & 0xff;
crc_field[1] = (crc & 0xff);
floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
return FLOPPY_ERROR_SUCCESS;
}
static floperr_t ti99_tdf_write_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam)
{
/* Read track, head */
int byte;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
UINT8 *track_data;
UINT8 *sector_data;
UINT64 track_offset;
UINT64 offset;
UINT8 crc_field[2];
int crc;
int imgtrack = track;
int i;
floperr_t err;
track_data = (UINT8*)malloc(tag->track_size);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
if (err)
{
free(track_data);
return err;
}
/* Search for the sector_index-th sector. */
if (tag->first_idam==0) /* should we check for every track? */
{
if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
{
free(track_data);
return FLOPPY_ERROR_SEEKERROR;
}
}
/* Search for the IDAM of the n-th sector */
i=0;
while (i < tag->track_size && sector_index>=0)
{
byte = read_byte(tag->format, tag->first_idam, track_data, i);
if (byte == TI99_IDAM)
{
sector_index--;
}
i++;
}
i--;
/* Not that many sectors in this track? */
if (sector_index>0)
{
free(track_data);
return FLOPPY_ERROR_SEEKERROR;
}
/* Find the DAM. */
while (i < tag->track_size)
{
byte = read_byte(tag->format, tag->first_idam, track_data, i);
if (byte == TI99_DAM)
break;
else
i++;
}
/* There was no DAM. Image seems to be broken. */
if (i == tag->track_size)
{
free(track_data);
return FLOPPY_ERROR_SEEKERROR;
}
/* Sector data is right after the DAM. */
sector_data = &track_data[i+1];
/* Get the position of the track in the image. */
err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset);
if (err)
return err;
offset = track_offset + (sector_data - track_data);
/* Write the sector data at that position. */
floppy_image_write(floppy, buffer, offset, SECTOR_SIZE);
// Recalculate CRC. We init the CRC with bf84 (DAM fb is
// already included; would be ffff without)
crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE);
crc_field[0] = (crc>>8) & 0xff;
crc_field[1] = (crc & 0xff);
floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2);
/* Free the temporary buffer */
free(track_data);
return FLOPPY_ERROR_SUCCESS;
}
static floperr_t ti99_tdf_find_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, void *buffer, unsigned long *flags)
{
/* Read track, head */
int byte = 0;
struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy);
UINT8 *track_data;
int imgtrack = track;
int i;
floperr_t err;
floperr_t retval;
track_data = (UINT8*)malloc(tag->track_size);
if (use_80_track_drives && tag->tracks<=40)
{
imgtrack = track / 2;
}
err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size);
if (err)
{
free(track_data);
return err;
}
/* Search for the sector_index-th sector. */
if (tag->first_idam==0) /* should we check for every track? */
{
if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR)
{
free(track_data);
return FLOPPY_ERROR_SEEKERROR;
}
}
i=0;
sector_index = sector_index+1;
while (i++ < tag->track_size && sector_index>0)
{
byte = read_byte(tag->format, tag->first_idam, track_data, i);
if (byte == TI99_IDAM)
{
sector_index--;
}
}
retval = FLOPPY_ERROR_SEEKERROR;
if (sector_index==0)
{
/* If desired, return the ID field */
if (cylinder)
{
*cylinder = track_data[i];
// printf("cylinder=%d", *cylinder);
}
if (side)
{
*side = track_data[i+1];
// printf(", side=%d", *side);
}
if (sector)
{
*sector = track_data[i+2];
// printf(", sector=%d", *sector);
}
if (sector_length)
{
*sector_length = 128 << track_data[i+3];
// printf(", sector_length=%d\n", *sector_length);
}
if (flags)
{
*flags = 0;
}
retval = FLOPPY_ERROR_SUCCESS;
/* If desired, return the sector contents. */
if (buffer)
{
while (i++ < tag->track_size)
{
byte = read_byte(tag->format, tag->first_idam, track_data, i);
if (byte == TI99_DAM)
break;
}
if (byte == TI99_DAM)
{
memcpy(buffer, &track_data[i+1], SECTOR_SIZE);
}
else
{
retval = FLOPPY_ERROR_SEEKERROR;
}
}
}
free(track_data);
return retval;
}
/*
Required for ReadAddress command. This function gets the 1st, 2nd, 3rd,
sector (and so on) from the track, not sector 1, 2, 3. That is, the
returned sector info depends on the interleave and the start sector.
*/
static floperr_t ti99_tdf_get_indexed_sector_info(floppy_image_legacy *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
{
return ti99_tdf_find_indexed_sector(floppy, head, track, sector_index, cylinder, side, sector, sector_length, NULL, flags);
}
static floperr_t ti99_tdf_read_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
return ti99_tdf_find_indexed_sector(floppy, head, track, sector, (int*)NULL, (int*)NULL, (int*)NULL, (UINT32*)NULL, buffer, (unsigned long*)NULL);
}
/*
Guess the geometry of the disk.
1. Check whether the disk is formatted, and if so, whether it is
a FM or a MFM disk
2. If this fails, guess from the file size which format it should have
SDF formats have sizes which are multiples of 10KiB.
TDF: full sector size FM (MFM) = 334 (340) bytes
additional track data FM (MFM) = 247 (752) bytes
=> check whether size is dividable by 40 (or 35) and by 2
=> get track size
=> subtract track data
=> divide by 334 or 340
=> number of sectors should be 8, 9, 16, 18, 32, or 36
SSDD is 274880, but DSSD is 260240, so they differ (unlike in the SDF format)
Still, we cannot tell apart single-sided 80 track formats from double-sided
40 tracks. We assume the latter. Moreover, we only attempt to detect
9/18/36 sectors (no 8/16) and 40 tracks (no 35).
SSSD40 = 130120
DSSD = 260240
SSDD = 274880
DSDD = 549760
DSHD = 1039360
DSDD80 = 1099520
DSHD80 = 2078720
*/
static int ti99_tdf_guess_geometry(floppy_image_legacy *floppy, UINT64 size,
struct ti99dsk_geometry *geometry)
{
int idamcnt, state, track, head, i, totalseclen = 0, format = 0, byte, tracklength, trackadd = 0;
int first_idam = 0;
UINT8 *track_data;
/*int offset;*/
struct ti99dsk_geometry dummy_geometry;
if (geometry)
memset(geometry, 0, sizeof(*geometry));
else
geometry = &dummy_geometry;
/* Allocate enough bytes to hold the longest supported track. */
track_data = (UINT8*)malloc(13000);
floppy_image_read(floppy, track_data, 0, 13000);
if (determine_offset(TI99_MFM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
{
/* MFM failed. */
if (determine_offset(TI99_FM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
{
LOG_FORMATS("IDAM not found. Unformatted disk.\n");
// FM failed as well. Disk is not formatted. We assume
// a format that fits into the space provided by the file.
// We only give a moderate vote in this case, so SDF
// make take it.
free(track_data);
if (size < 130120 || size > 2078720)
{
LOG_FORMATS("Unknown format size: %d\n", (int)size);
return 0;
}
if (size >= 130120 && size < 260240)
{
/* SSSD */
geometry->sides = 1;
geometry->tracksperside = 40;
geometry->secspertrack = 9;
geometry->density = 1;
return 50;
}
if (size >= 260240 && size < 274880)
{
/* DSSD */
geometry->sides = 2;
geometry->tracksperside = 40;
geometry->secspertrack = 9;
geometry->density = 1;
return 50;
}
if (size >= 274880 && size < 549760)
{
/* SSDD */
geometry->sides = 1;
geometry->tracksperside = 40;
geometry->secspertrack = 18;
geometry->density = 2;
return 50;
}
if (size >= 549760 && size < 1039360)
{
/* DSDD */
geometry->sides = 2;
geometry->tracksperside = 40;
geometry->secspertrack = 18;
geometry->density = 2;
return 50;
}
if (size >= 1039360 && size < 1099520)
{
/* DSHD */
geometry->sides = 2;
geometry->tracksperside = 40;
geometry->secspertrack = 36;
geometry->density = 2;
return 50;
}
if (size >= 1099520 && size < 2078720)
{
/* DSDD80 */
geometry->sides = 2;
geometry->tracksperside = 80;
geometry->secspertrack = 18;
geometry->density = 2;
return 50;
}
if (size == 2078720)
{
/* DSHD80 */
geometry->sides = 2;
geometry->tracksperside = 80;
geometry->secspertrack = 36;
geometry->density = 2;
return 50;
}
}
else
{
/* FM format */
totalseclen = TI99_FM_TOTAL_SECTOR;
trackadd = TI99_FM_TRACK_ADD; /* additional track data (gaps etc.) */
geometry->density = 1;
format = TI99_FM;
}
}
else
{
/* MFM format */
totalseclen = TI99_MFM_TOTAL_SECTOR;
trackadd = TI99_MFM_TRACK_ADD;
geometry->density = 2;
format = TI99_MFM;
}
/* Count the number of IDAMs on track 0, head 0 */
idamcnt = 0;
state = 0;
track = 0;
head = 0;
i=0;
while (i++ < 13000 && head == 0 && track == 0)
{
byte = read_byte(format, first_idam, track_data, i);
switch (state)
{
case 0:
if (byte==TI99_IDAM)
state = 1;
break;
case 1:
track = byte;
state = 2;
break;
case 2:
head = byte;
state = 3;
break;
case 3:
idamcnt++;
state = 0;
}
}
LOG_FORMATS("Determined %d sectors\n", idamcnt);
// Now calculate the geometry
// The track size, in theory, may change due to reformatting,
// and it may also vary between tracks, but the TDF does not support
// varying track lengths. So every track has the same length as track 0.
// Also, we don't yet know whether the drive and the disk have the
// same track count. For 80 track drives and 40 track disks, we need
// double-stepping; this will be known as soon as the controller is
// initialized; we need to check that in the access methods.
tracklength = idamcnt * totalseclen + trackadd;
/*offset = 0;*/
// Read the last track. We assume that all tracks are properly
// formatted; otherwise, the above calculations would already fail.
// That is, the last track in the image tells us whether it is for
// head 0 or for head 1.
geometry->sides = 1;
floppy_image_read(floppy, track_data, size-tracklength, tracklength);
if (determine_offset(format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR)
{
/* error ... what now? */
LOG_FORMATS("Error when reading last track. Image broken.\n");
free(track_data);
return 50;
}
else
{
if (track_data[first_idam+2]==0)
{
geometry->sides = 1;
}
else
{
if (track_data[first_idam+2]==1)
{
geometry->sides = 2;
}
else
{
LOG_FORMATS("Error: Last track has invalid first IDAM: head = %d.\n", track_data[first_idam+2]);
}
}
}
geometry->tracksperside = (size / tracklength) / geometry->sides;
geometry->secspertrack = idamcnt;
if (geometry->tracksperside < 35 || geometry->tracksperside > 80)
{
LOG_FORMATS("Unsupported track count: %d\n", geometry->tracksperside);
free(track_data);
return 0;
}
free(track_data);
return 100;
}
static FLOPPY_IDENTIFY(ti99_tdf_identify)
{
UINT64 size;
size = floppy_image_size(floppy);
*vote = ti99_tdf_guess_geometry(floppy, size, NULL);
LOG_FORMATS("TDF voting %d\n", *vote);
return FLOPPY_ERROR_SUCCESS;
}
static FLOPPY_CONSTRUCT(ti99_tdf_construct)
{
struct ti99dsk_geometry geometry;
struct FloppyCallbacks *callbacks;
struct ti99dsk_tag *tag;
LOG_FORMATS("Reading image as TDF\n");
if (params)
{
/* create */
memset(&geometry, 0, sizeof(geometry));
/*
geometry.sides = option_resolution_lookup_int(params, PARAM_HEADS);
geometry.tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS);
geometry.secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS);
*/
}
else
{
/* load */
if (ti99_tdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50)
return FLOPPY_ERROR_INVALIDIMAGE;
}
assert(geometry.sides);
assert(geometry.tracksperside);
assert(geometry.secspertrack);
tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag));
tag->heads = geometry.sides;
tag->tracks = geometry.tracksperside;
if (geometry.density==TI99_FM)
tag->track_size = geometry.secspertrack * TI99_FM_TOTAL_SECTOR + TI99_FM_TRACK_ADD;
else
tag->track_size = geometry.secspertrack * TI99_MFM_TOTAL_SECTOR + TI99_MFM_TRACK_ADD;
tag->format = geometry.density;
tag->first_idam = 0; /* let's find out later */
tag->dam_offset = 0;
/* set up format callbacks */
callbacks = floppy_callbacks(floppy);
callbacks->read_sector = ti99_tdf_read_sector;
callbacks->write_sector = ti99_tdf_write_sector;
callbacks->read_indexed_sector = ti99_tdf_read_indexed_sector;
callbacks->write_indexed_sector = ti99_tdf_write_indexed_sector;
callbacks->read_track = ti99_tdf_read_track;
callbacks->write_track = ti99_tdf_write_track;
/* callbacks->format_track = ti99_tdf_format_track; */
callbacks->get_track_size = ti99_get_track_size;
/* post format unset */
callbacks->get_heads_per_disk = ti99_get_heads_per_disk;
callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk;
callbacks->get_sector_length = ti99_get_sector_length;
callbacks->get_indexed_sector_info = ti99_tdf_get_indexed_sector_info;
return FLOPPY_ERROR_SUCCESS;
}
/* ----------------------------------------------------------------------- */
LEGACY_FLOPPY_OPTIONS_START( ti99 )
LEGACY_FLOPPY_OPTION( ti99_sdf, "dsk", "TI99 sector dump (v9t9)", ti99_sdf_identify, ti99_sdf_construct, NULL,
HEADS([1]-2)
TRACKS(35-[40]-80)
SECTORS(8/9/16/[18]/36)
SECTOR_LENGTH([256])
FIRST_SECTOR_ID(0))
LEGACY_FLOPPY_OPTION( ti99_tdf, "dsk,dtk", "TI99 track dump (pc99)", ti99_tdf_identify, ti99_tdf_construct, NULL,
TRACKS(35-[40]-80)
SECTORS(8/9/16/[18]/36)
SECTOR_LENGTH([256])
FIRST_SECTOR_ID(0))
LEGACY_FLOPPY_OPTIONS_END
|