summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/smc92x4.c
blob: 516c6eef0193ec42b284926d0776397a349f7f90 (plain) (blame)
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
/*
    HDC9224 and HDC9234 Hard and Floppy Disk Controller

    This controller handles MFM and FM encoded floppy disks and hard disks.
    The SMC9224 is used in some DEC systems.  The HDC9234 is used in the
    Myarc HFDC card for the TI99/4a.  The main difference between the two
    chips is the way the ECC bytes are computed; there are differences in
    the way seek times are computed, too.

    References:
    * SMC HDC9234 preliminary data book (1988)

    Michael Zapf, April 2010

    First version by Raphael Nabet, 2003
*/

#include "emu.h"
#include "smc92x4.h"

/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

/*
    Definition of bits in the status register
*/
#define ST_INTPEND  0x80        /* interrupt pending */
#define ST_DMAREQ   0x40        /* DMA request */
#define ST_DONE     0x20        /* command done */
#define ST_TERMCOD  0x18        /* termination code (see below) */
#define ST_RDYCHNG  0x04        /* ready change */
#define ST_OVRUN    0x02        /* overrun/underrun */
#define ST_BADSECT  0x01        /* bad sector */

/*
    Definition of the termination codes (INT_STATUS)
*/
#define ST_TC_SUCCESS   0x00    /* Successful completion */
#define ST_TC_RDIDERR   0x08    /* Error in READ-ID sequence */
#define ST_TC_SEEKERR   0x10    /* Error in SEEK sequence */
#define ST_TC_DATAERR   0x18    /* Error in DATA-TRANSFER seq. */


/*
    Definition of bits in the Termination-Conditions register
*/
#define TC_CRCPRE   0x80        /* CRC register preset, must be 1 */
#define TC_UNUSED   0x40        /* bit 6 is not used and must be 0 */
#define TC_INTDONE  0x20        /* interrupt on done */
#define TC_TDELDAT  0x10        /* terminate on deleted data */
#define TC_TDSTAT3  0x08        /* terminate on drive status 3 change */
#define TC_TWPROT   0x04        /* terminate on write-protect (FDD only) */
#define TC_INTRDCH  0x02        /* interrupt on ready change (FDD only) */
#define TC_TWRFLT   0x01        /* interrupt on write-fault (HDD only) */

/*
    Definition of bits in the Chip-Status register
*/
#define CS_RETREQ   0x80        /* retry required */
#define CS_ECCATT   0x40        /* ECC correction attempted */
#define CS_CRCERR   0x20        /* ECC/CRC error */
#define CS_DELDATA  0x10        /* deleted data mark */
#define CS_SYNCERR  0x08        /* synchronization error */
#define CS_COMPERR  0x04        /* compare error */
#define CS_PRESDRV  0x03        /* present drive selected */

/*
    Definition of bits in the Mode register
*/
#define MO_TYPE     0x80        /* Hard disk (1) or floppy (0) */
#define MO_CRCECC   0x60        /* Values for CRC/ECC handling */
#define MO_DENSITY  0x10        /* FM = 1; MFM = 0 */
#define MO_UNUSED   0x08        /* Unused, 0 */
#define MO_STEPRATE 0x07        /* Step rates */

/*
    hfdc state structure

    status
    ab7     ecc error
    ab6     index pulse
    ab5     seek complete
    ab4     track 0
    ab3     user-defined
    ab2     write-protected
    ab1     drive ready
    ab0     write fault

    output1
    ab7     drive select 3
    ab6     drive select 2
    ab5     drive select 1
    ab4     drive select 0
    ab3     programmable outputs
    ab2     programmable outputs
    ab1     programmable outputs
    ab0     programmable outputs

    output2
    ab7     drive select 3* (active low, used for tape operations)
    ab6     reduce write current
    ab5     step direction
    ab4     step pulse
    ab3     desired head 3
    ab2     desired head 2
    ab1     desired head 1
    ab0     desired head 0
*/

#define OUT1_DRVSEL3    0x80
#define OUT1_DRVSEL2    0x40
#define OUT1_DRVSEL1    0x20
#define OUT1_DRVSEL0    0x10
#define OUT2_DRVSEL3_   0x80
#define OUT2_REDWRT 0x40
#define OUT2_STEPDIR    0x20
#define OUT2_STEPPULSE  0x10
#define OUT2_HEADSEL3   0x08
#define OUT2_HEADSEL2   0x04
#define OUT2_HEADSEL1   0x02
#define OUT2_HEADSEL0   0x01

#define DRIVE_TYPE  0x03
#define TYPE_AT     0x00
#define TYPE_FLOPPY     0x02  /* for testing on any floppy type */
#define TYPE_FLOPPY8    0x02
#define TYPE_FLOPPY5    0x03

#define MAX_SECTOR_LEN 256

#define FORMAT_LONG false

#define ERROR   0
#define DONE    1
#define AGAIN   2
#define UNDEF   3

enum
{
	DATA_TIMER,
	READ_TIMER,
	WRITE_TIMER,
	SEEK_TIMER,
	TRACK_TIMER
};

/*
    Step rates in microseconds for MFM. This is set in the mode register,
    bits 0-2. Single density doubles all values.
*/
static const int step_hd[]  = { 22, 50, 100, 200, 400, 800, 1600, 3200 };
static const int step_flop8[]   = { 218, 500, 1000, 2000, 4000, 8000, 16000, 32000 };
static const int step_flop5[]   = { 436, 1000, 2000, 4000, 8000, 16000, 32000, 64000 };

/*
    0.2 seconds for a revolution (300 rpm), +50% average waiting for index
    hole. Should be properly done with the index hole detection; we're
    simulating this with a timer for now.
*/
#define TRACKTIME_FLOPPY 300000
#define TRACKTIME_HD 1000

/*
    Register names of the HDC. The left part is the set of write registers,
    while the right part are the read registers.
*/
enum
{
	DMA7_0=0,
	DMA15_8=1,
	DMA23_16=2,
	DESIRED_SECTOR=3,
	DESIRED_HEAD=4,     CURRENT_HEAD=4,
	DESIRED_CYLINDER=5,     CURRENT_CYLINDER=5,
	SECTOR_COUNT=6,     CURRENT_IDENT=6,
	RETRY_COUNT=7,      TEMP_STORAGE2=7,
	MODE=8,         CHIP_STATUS=8,
	INT_COMM_TERM=9,    DRIVE_STATUS=9,
	DATA_DELAY=10,      DATA=10,
	COMMAND=11,         INT_STATUS=11
};

#define TRKSIZE_DD      6144
#define TRKSIZE_SD      3172

#define VERBOSE 1
#define LOG logerror

smc92x4_device::smc92x4_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
: device_t(mconfig, SMC92X4, "SMC 9224/9234 Hard/Floppy Disk Controller", tag, owner, clock, "smc92x4", __FILE__),
  m_out_intrq(*this),
  m_out_dip(*this),
  m_out_auxbus(*this),
  m_in_auxbus(*this),
  m_in_dma(*this),
  m_out_dma(*this)
{
}

int smc92x4_device::image_is_single_density()
{
	floppy_image_legacy *image = m_drive->flopimg_get_image();
	return (floppy_get_track_size(image, 0, 0)<4000);
}

bool smc92x4_device::in_single_density_mode()
{
	return ((m_register_w[MODE]&MO_DENSITY)!=0);
}

void smc92x4_device::copyid(chrn_id id1, chrn_id_hd *id2)
{
	id2->C = id1.C & 0xff;
	id2->H = id1.H;
	id2->R = id1.R;
	id2->N = id1.N;
	id2->data_id = id1.data_id;
	id2->flags = id1.flags;
}

/*
    Set IRQ
*/
void smc92x4_device::set_interrupt()
{
	if ((m_register_r[INT_STATUS] & ST_INTPEND) == 0)
	{
		m_register_r[INT_STATUS] |= ST_INTPEND;
		m_out_intrq(ASSERT_LINE);
	}
}

/*
    Assert Command Done status bit, triggering interrupts as needed
*/
void smc92x4_device::set_command_done(int flags)
{
	//assert(! (m_status & ST_DONE))
	if (VERBOSE>7) LOG("smc92x4 command %02x done, flags=%02x\n", m_command, flags);

	m_register_r[INT_STATUS] |= ST_DONE;
	m_register_r[INT_STATUS] &= ~ST_TERMCOD; /* clear the previously set flags */
	m_register_r[INT_STATUS] |= flags;

	/* sm92x4 spec, p. 6 */
	if (m_register_w[INT_COMM_TERM] & TC_INTDONE)
		set_interrupt();
}

/*
    Preserve previously set termination code
*/
void smc92x4_device::set_command_done()
{
	//assert(! (m_status & ST_DONE))
	if (VERBOSE>7) LOG("smc92x4 command %02x done\n", m_command);

	m_register_r[INT_STATUS] |= ST_DONE;

	/* sm92x4 spec, p. 6 */
	if (m_register_w[INT_COMM_TERM] & TC_INTDONE)
		set_interrupt();
}

/*
    Clear IRQ
*/
void smc92x4_device::clear_interrupt()
{
	if ((m_register_r[INT_STATUS] & ST_INTPEND) != 0)
		m_out_intrq(CLEAR_LINE);
}

/*
    Sets the DMA address on the external counter. This counter is attached
    to the auxiliary bus on the PCB.
*/
void smc92x4_device::set_dma_address(int pos2316, int pos1508, int pos0700)
{
	m_out_auxbus((offs_t)OUTPUT_DMA_ADDR, m_register_r[pos2316]);
	m_out_auxbus((offs_t)OUTPUT_DMA_ADDR, m_register_r[pos1508]);
	m_out_auxbus((offs_t)OUTPUT_DMA_ADDR, m_register_r[pos0700]);
}

void smc92x4_device::dma_add_offset(int offset)
{
	int dma_address = (m_register_w[DMA23_16]<<16) + (m_register_w[DMA15_8]<<8) + m_register_w[DMA7_0];
	dma_address += offset;

	m_register_w[DMA23_16] = m_register_r[DMA23_16] = (dma_address & 0xff0000)>>16;
	m_register_w[DMA15_8]  = m_register_r[DMA15_8]  = (dma_address & 0x00ff00)>>8;
	m_register_w[DMA7_0]   = m_register_r[DMA7_0]   = (dma_address & 0x0000ff);
}

/*
    Get the state from outside and latch it in the register.
    There should be a bus driver on the PCB which provides the signals from
    both the hard and floppy drives during S0=S1=0 and STB*=0 times via the
    auxiliary bus.
*/
void smc92x4_device::sync_status_in()
{
	UINT8 prev;
	prev = m_register_r[DRIVE_STATUS];
	m_register_r[DRIVE_STATUS] = m_in_auxbus(0);

	/* Raise interrupt if ready changes. TODO: Check this more closely. */
	if (((m_register_r[DRIVE_STATUS] & DS_READY) != (prev & DS_READY))
		& (m_register_r[INT_STATUS] & ST_RDYCHNG))
	{
		set_interrupt();
	}
}

/*
    Push the output registers over the auxiliary bus. It is expected that
    the PCB contains latches to store the values.
*/
void smc92x4_device::sync_latches_out()
{
	m_output1 = (m_output1 & 0xf0) | (m_register_w[RETRY_COUNT]&0x0f);
	m_out_auxbus((offs_t)OUTPUT_OUTPUT1, m_output1);
	m_out_auxbus((offs_t)OUTPUT_OUTPUT2, m_output2);
}

/*************************************************************
    Timed requests and callbacks
*************************************************************/
#if 0
/* setup a timed data request - data request will be triggered in a few usecs time */
void smc92x4_device::timed_data_request()
{
	int time = in_single_density_mode()? 128 : 32;

	if (!m_use_real_timing)
		time = 1;

	/* set new timer */
	m_timer_data->adjust(attotime::from_usec(time));
}
#endif

/* setup a timed read sector - read sector will be triggered in a few usecs time */
void smc92x4_device::timed_sector_read_request()
{
	int time=0;

	// set new timer
	// Average time from sector to sector.
	if (m_selected_drive_type & TYPE_FLOPPY)
		time = (in_single_density_mode())? 30000 : 15000;
	else
		time = 1000;

	if (!m_use_real_timing)
		time = 1;

	m_timer_rs->adjust(attotime::from_usec(time));
	m_to_be_continued = true;
}

/* setup a timed write sector - write sector will be triggered in a few usecs time */
void smc92x4_device::timed_sector_write_request()
{
	int time=0;

	/* Average time from sector to sector. */
	if (m_selected_drive_type & TYPE_FLOPPY)
		time = (in_single_density_mode())? 30000 : 15000;
	else
		time = 1000;

	if (!m_use_real_timing)
		time = 1;

	m_timer_ws->adjust(attotime::from_usec(time));
	m_to_be_continued = true;
}

/*
    Set up a timed track read/write
*/
void smc92x4_device::timed_track_request()
{
	int time = 0;

	if (m_selected_drive_type & TYPE_FLOPPY)
		time = TRACKTIME_FLOPPY;
	else
		time = TRACKTIME_HD;

	if (!m_use_real_timing)
		time = 1;

	m_timer_track->adjust(attotime::from_usec(time));

	m_to_be_continued = true;
}

/*
    Set up a timed track seek
*/
void smc92x4_device::timed_seek_request()
{
	int time = 0;

	int index = m_register_w[MODE] & MO_STEPRATE;
	int fm = in_single_density_mode();

	/* Get seek time. */
	if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_FLOPPY8)
		time = step_flop8[index];

	else if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_FLOPPY5)
		time = step_flop5[index];
	else
		time = step_hd[index];

	if (fm)
		time = time * 2;

	if (!m_use_real_timing)
	{
		if (VERBOSE>5) LOG("smc92x4 info: Disk access without delays\n");
		time = 1;
	}
	m_timer_seek->adjust(attotime::from_usec(time));
	m_to_be_continued = true;
}

void smc92x4_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	int transfer_enabled;
	int deldata;
	int redcur;
	int precomp;
	int write_long;

	switch (id)
	{
	case DATA_TIMER:
		// Not implemented yet
		break;
	case READ_TIMER:
		transfer_enabled = m_command & 0x01;
		// Now read the sector.
		data_transfer_read(m_recent_id, transfer_enabled);
		sync_status_in();
		break;
	case WRITE_TIMER:
		// The specification is contraditory here :-(
		// For formatting, bit 0x10 = 1 means normal, 0 means deleted
		// while for writing sectors it is the opposite?!
		// We believe the existing drivers which all set the bit for normal writing.
		deldata = 0x10 - (m_command & 0x10);
		redcur =  m_command & 0x08;
		precomp = m_command & 0x07;
		write_long = ((m_register_w[MODE]& MO_CRCECC)==0x40);

		if (deldata)
			if (VERBOSE>0) LOG("smc92x4 warn: Write deleted data mark not supported. Writing normal mark.\n");

		// Now write the sector.
		data_transfer_write(m_recent_id, deldata, redcur, precomp, write_long);
		sync_status_in();
		break;
	case SEEK_TIMER:
		// Callback for seek request.
		/*  int buffered = ((m_register_w[MODE] & MO_STEPRATE)==0); */
		/*  int buffered = (m_command & 0x01); */

		if (m_selected_drive_type & TYPE_FLOPPY)
		{
			if (m_drive==NULL)
			{
				if (VERBOSE>0) LOG("smc92x4 error: seek callback: no floppy\n");
				m_register_r[INT_STATUS] |= ST_TC_SEEKERR;
			}
			else
			{
				if (VERBOSE>5) LOG("smc92x4 step %s direction %d\n", m_drive->tag(), m_step_direction);
				m_drive->floppy_drive_seek(m_step_direction);
			}
		}
		else
		{
			if (VERBOSE>6) LOG("smc92x4 step harddisk direction %d\n", m_step_direction);
			m_harddisk->seek(m_step_direction);
		}
		sync_status_in();
		break;
	case TRACK_TIMER:
		// Callback for track access. Nothing interesting here, just used for
		// delaying.
		break;
	}
	process_after_callback();
}


/*********************************************************************
    Common functions
*********************************************************************/

/*
    Calculate the ident byte from the cylinder. The specification does not
    define idents beyond cylinder 1023, but formatting programs seem to
    continue with 0xfd for cylinders between 1024 and 2047.
*/
UINT8 smc92x4_device::cylinder_to_ident(int cylinder)
{
	if (cylinder < 256) return 0xfe;
	if (cylinder < 512) return 0xff;
	if (cylinder < 768) return 0xfc;
	if (cylinder < 1024) return 0xfd;
	return 0xfd;
}

/*
    Calculate the offset from the ident. This is only useful for AT drives;
    these drives cannot have more than 1024 cylinders.
*/
int smc92x4_device::ident_to_cylinder(UINT8 ident)
{
	switch (ident)
	{
	case 0xfe: return 0;
	case 0xff: return 256;
	case 0xfc: return 512;
	case 0xfd: return 768;
	default: return -1;
	}
}

/*
    Common function to set the read registers from the recently read id.
*/
void smc92x4_device::update_id_regs(chrn_id_hd id)
{
	// Flags for current head register. Note that the sizes are not in
	// sequence (128, 256, 512, 1024). This is only interesting for AT
	// mode.
	static const UINT8 sizeflag[] = { 0x60, 0x00, 0x20, 0x40 };

	m_register_r[CURRENT_CYLINDER] = id.C & 0xff;
	m_register_r[CURRENT_HEAD] = id.H & 0x0f;

	if (id.flags & BAD_SECTOR)
		m_register_r[CURRENT_HEAD] |= 0x80;

	if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_AT)
		m_register_r[CURRENT_HEAD] |= sizeflag[id.N];
	else
		m_register_r[CURRENT_HEAD] |= ((id.C & 0x700)>>4);

	m_register_r[CURRENT_IDENT] = cylinder_to_ident(id.C);
}

/*
    Common procedure: read_id_field (as described in the specification)
*/
void smc92x4_device::read_id_field(int *steps, int *direction, chrn_id_hd *id)
{
	int des_cylinder, cur_cylinder;
	bool found = false;

	sync_latches_out();
	sync_status_in();

	// Set command termination code. The error code is set first, and
	// on success, it is cleared.
	m_register_r[INT_STATUS] |= ST_TC_RDIDERR;

	/* Try to find an ID field. */
	if (m_selected_drive_type & TYPE_FLOPPY)
	{
		chrn_id idflop;
		if (m_drive->flopimg_get_image() == NULL)
		{
			if (VERBOSE>2) LOG("smc92x4 warn: No disk in drive\n");
			m_register_r[CHIP_STATUS] |= CS_SYNCERR;
			return;
		}

		/* Check whether image and controller are set to the same density. */
		if ((image_is_single_density() && in_single_density_mode())
			|| (!image_is_single_density() && !in_single_density_mode()))
		{
			found = m_drive->floppy_drive_get_next_id(m_register_w[DESIRED_HEAD]&0x0f, &idflop);
			copyid(idflop, id); /* Need to use bigger values for HD, but we don't use separate variables here */
		}
		else
		{
			if (VERBOSE>2) LOG("smc92x4 warn: Controller and medium density do not match.\n");
		}
		sync_status_in();

		if (!found)
		{
			if (VERBOSE>1) LOG("smc92x4 error: read_id_field (floppy): sync error\n");
			m_register_r[CHIP_STATUS] |= CS_SYNCERR;
			return;
		}
	}
	else
	{
		m_harddisk->get_next_id(m_register_w[DESIRED_HEAD]&0x0f, id);
		sync_status_in();
		if (!(m_register_r[DRIVE_STATUS]& DS_READY))
		{
			if (VERBOSE>1) LOG("smc92x4 error: read_id_field (harddisk): sync error\n");
			m_register_r[CHIP_STATUS] |= CS_SYNCERR;
			return;
		}
	}

	m_register_r[CHIP_STATUS] &= ~CS_SYNCERR;

	/* Update the registers. */
	update_id_regs(*id);

	if (id->flags & BAD_CRC)
	{
		m_register_r[CHIP_STATUS] |= CS_CRCERR;
		return;
	}

	/* Calculate the steps. */
	if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_AT)
	{
		/* Note: the spec says CURRENT_REGISTER, but that seems wrong. */
		des_cylinder = ((m_register_r[DATA] & 0x03)<<8) | m_register_w[DESIRED_CYLINDER];
		cur_cylinder = ident_to_cylinder(m_register_r[CURRENT_IDENT]) + m_register_r[CURRENT_CYLINDER];
	}
	else
	{
		des_cylinder = ((m_register_w[DESIRED_HEAD] & 0x70)<<4) | m_register_w[DESIRED_CYLINDER];
		cur_cylinder = ((m_register_r[CURRENT_HEAD] & 0x70)<<4) | m_register_r[CURRENT_CYLINDER];
	}

	if (des_cylinder >= cur_cylinder)
	{
		*steps = des_cylinder - cur_cylinder;
		*direction = +1;
	}
	else
	{
		*steps = cur_cylinder - des_cylinder;
		*direction = -1;
	}

	if (VERBOSE>6) LOG("smc92x4 seek required: %d steps\n", *steps);

	m_register_r[INT_STATUS] &= ~ST_TC_RDIDERR;
}

/*
    Common procedure: verify (as described in the specification)
*/
int smc92x4_device::verify(chrn_id_hd *id, bool check_sector)
{
	int maxtry = 132;  /* approx. 33792/(256*32) */
	int pass = 0;
	int found = false;
	int foundsect = false;
	int des_cylinder = 0;

	// Set command termination code. The error code is set first, and
	// on success, it is cleared.
	m_register_r[INT_STATUS] |= ST_TC_SEEKERR;

	while (pass < maxtry && !foundsect)
	{
		pass++;
		/* Try to find an ID field. */
		if (m_selected_drive_type & TYPE_FLOPPY)
		{
			chrn_id idflop;
			found = m_drive->floppy_drive_get_next_id(m_register_w[DESIRED_HEAD]&0x0f, &idflop);
			copyid(idflop, id);
			if (/* pass==1 && */!found)
			{
				m_register_r[CHIP_STATUS] |= CS_SYNCERR;
				if (VERBOSE>1) LOG("smc92x4 error: verify (floppy): sync error\n");
				return ERROR;
			}
		}
		else
		{
			m_harddisk->get_next_id(m_register_w[DESIRED_HEAD]&0x0f, id);
			sync_status_in();
			if (!(m_register_r[DRIVE_STATUS]& DS_READY))
			{
				if (VERBOSE>1) LOG("smc92x4 error: verify (harddisk): sync error\n");
				m_register_r[CHIP_STATUS] |= CS_SYNCERR;
				return ERROR;
			}
		}

		m_register_r[CHIP_STATUS] &= ~CS_SYNCERR;

		/* Compare with the desired sector ID. */
		if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_AT)
		{
			/* Note: the spec says CURRENT_CYLINDER, but that seems wrong. */
			des_cylinder = ((m_register_r[DATA] & 0x03)<<8) | m_register_w[DESIRED_CYLINDER];
		}
		else
		{
			des_cylinder = ((m_register_w[DESIRED_HEAD] & 0x70)<<4) | m_register_w[DESIRED_CYLINDER];
		}
		if (VERBOSE>6) LOG("smc92x4 check id: current = (%d,%d,%d), required = (%d,%d,%d)\n", id->C, id->H, id->R, des_cylinder, m_register_w[DESIRED_HEAD] & 0x0f, m_register_w[DESIRED_SECTOR]);
		if ((des_cylinder == id->C)
			&& ((m_register_w[DESIRED_HEAD] & 0x0f) == id->H))
		{
			if (!check_sector ||  (m_register_w[DESIRED_SECTOR] == id->R))
				foundsect = true;
		}
	}
	if (!foundsect)
	{
		m_register_r[CHIP_STATUS] |= CS_COMPERR;
		if (VERBOSE>1) LOG("smc92x4 error: verify: sector not found, seek error (desired cyl=%d/sec=%d, current cyl=%d/sec=%d)\n", des_cylinder, m_register_w[DESIRED_SECTOR], id->C, id->R);
		return ERROR;
	}

	m_register_r[INT_STATUS] &= ~ST_TC_SEEKERR;
	return DONE;
}

/*
    Common procedure: data_transfer(read) (as described in the specification)
*/
void smc92x4_device::data_transfer_read(chrn_id_hd id, int transfer_enable)
{
	int i, retry, sector_len;

	int sector_data_id;
	dynamic_buffer buf;

	sync_latches_out();
	sync_status_in();

	// Set command termination code. The error code is set first, and
	// on success, it is cleared.
	m_register_r[INT_STATUS] |= ST_TC_DATAERR;

	// Save the value. Note that the retry count is only in the first four
	// bits, and it is stored in one's complement. In this implementation
	// we don't work with it.
	retry = m_register_w[RETRY_COUNT];

	/* We are already at the correct sector (found by the verify sequence) */

	/* Find the data sync mark. Easy, we assume it has been found. */

	if (id.flags & ID_FLAG_DELETED_DATA)
		m_register_r[CHIP_STATUS] |= CS_DELDATA;
	else
		m_register_r[CHIP_STATUS] &= ~CS_DELDATA;

	/* Found. Now update the current cylinder/head registers. */
	update_id_regs(id);

	/* Initiate the DMA. We assume the DMARQ is positive. */
	m_register_r[INT_STATUS] &= ~ST_OVRUN;

	sector_len = 1 << (id.N+7);
	sector_data_id = id.data_id;
	buf.resize(sector_len);

	if (m_selected_drive_type & TYPE_FLOPPY)
	{
		m_drive->floppy_drive_read_sector_data(id.H, sector_data_id, buf, sector_len);
	}
	else
	{
		// TODO: Should we get the sector length from the harddisk?
		m_harddisk->read_sector(id.C, id.H, id.R, buf);
	}
	sync_status_in();

	if (transfer_enable)
	{
		/* Copy via DMA into controller RAM. */
		set_dma_address(DMA23_16, DMA15_8, DMA7_0);

		m_out_dip(ASSERT_LINE);
		for (i=0; i < sector_len; i++)
		{
			m_out_dma((offs_t)0, buf[i]);
		}
		m_out_dip(CLEAR_LINE);
	}

	/* Check CRC. We assume everything is OK, no retry required. */
	m_register_r[CHIP_STATUS] &= ~CS_RETREQ;
	m_register_r[CHIP_STATUS] &= ~CS_CRCERR;
	m_register_r[CHIP_STATUS] &= ~CS_ECCATT;

	/* Update the DMA registers. */
	if (transfer_enable) dma_add_offset(sector_len);

	/* Decrement sector count. */
	m_register_w[SECTOR_COUNT] = (m_register_w[SECTOR_COUNT]-1)&0xff;

	/* Clear the error bits. */
	m_register_r[INT_STATUS] &= ~ST_TC_DATAERR;

	if (m_register_w[SECTOR_COUNT] == 0)
		return;

	/* Else this is a multi-sector operation. */
	m_register_w[DESIRED_SECTOR] =
	m_register_r[DESIRED_SECTOR] =
		(m_register_w[DESIRED_SECTOR]+1) & 0xff;

	/* Reinitialize retry count. */
	m_register_w[RETRY_COUNT] = retry;
}

/*
    Common procedure: data_transfer(write) (as described in the specification)
*/
void smc92x4_device::data_transfer_write(chrn_id_hd id, int deldata, int redcur, int precomp, bool write_long)
{
	int retry, i, sector_len;
	dynamic_buffer buf;
	int sector_data_id;
	sync_latches_out();
	sync_status_in();

	// Set command termination code. The error code is set first, and
	// on success, it is cleared.
	m_register_r[INT_STATUS] |= ST_TC_DATAERR;

	// Save the value. Note that the retry count is only in the first four
	// bits, and it is stored in one's complement. In this implementation
	// we don't work with it.
	retry = m_register_w[RETRY_COUNT];

	/* Initiate the DMA. We assume the DMARQ is positive. */
	m_register_r[INT_STATUS] &= ~ST_OVRUN;

	sector_len = 1 << (id.N+7);
	sector_data_id = id.data_id;

	buf.resize(sector_len);

	/* Copy via DMA from controller RAM. */
	set_dma_address(DMA23_16, DMA15_8, DMA7_0);

	m_out_dip(ASSERT_LINE);
	for (i=0; i<sector_len; i++)
	{
		buf[i] = m_in_dma(0);
	}
	m_out_dip(CLEAR_LINE);

	if (write_long)
	{
		if (VERBOSE>1) LOG("smc92x4 warn: write sector: Write_long not supported. Performing a normal write.\n");
	}

	if (m_selected_drive_type & TYPE_FLOPPY)
	{
		if (VERBOSE>4) LOG("smc92x4 info: write sector CHS=(%d,%d,%d)\n", id.C, id.H, id.R);
		m_drive->floppy_drive_write_sector_data(id.H, sector_data_id, buf, sector_len, false);
	}
	else
	{
		m_harddisk->write_sector(id.C, id.H, id.R, buf);
	}
	sync_status_in();

	m_register_r[CHIP_STATUS] &= ~CS_RETREQ;
	m_register_r[CHIP_STATUS] &= ~CS_CRCERR;
	m_register_r[CHIP_STATUS] &= ~CS_ECCATT;

	/* Update the DMA registers. */
	dma_add_offset(sector_len);

	/* Decrement sector count. */
	m_register_w[SECTOR_COUNT] = (m_register_w[SECTOR_COUNT]-1)&0xff;

	/* Clear the error bits. */
	m_register_r[INT_STATUS] &= ~ST_TC_DATAERR;

	if (m_register_w[SECTOR_COUNT] == 0) return;

	/* Else this is a multi-sector operation. */
	m_register_w[DESIRED_SECTOR] =  m_register_r[DESIRED_SECTOR] = (m_register_w[DESIRED_SECTOR]+1) & 0xff;

	/* Reinitialize retry count. */
	m_register_w[RETRY_COUNT] = retry;
}

/*
    Read sectors physical / logical. Physical means that the first, the
    second, the third sector appearing under the head will be read. These
    sectors are usually not in logical sequence. The ordering depends on
    the interleave pattern.
*/
void smc92x4_device::read_write_sectors()
{
	int logical = m_command & 0x04;
	int implied_seek_disabled = m_command & 0x02; /* for read */
	int write = (m_command & 0x80);

	m_after_seek = false;

	m_to_be_continued = false;

	if (write) /* write sectors */
	{
		logical = m_command & 0x20;
		implied_seek_disabled = m_command & 0x40;
	}

	if (!logical)
		implied_seek_disabled = false;

	if (!m_found_id)
	{
		/* First start. */
		read_id_field(&m_seek_count, &m_step_direction, &m_recent_id);
	}

	if ((m_register_r[INT_STATUS] & ST_TC_DATAERR) == ST_TC_SUCCESS)
	{
		m_found_id = true;
		/* Perform the seek for the cylinder. */
		if (!implied_seek_disabled && m_seek_count > 0)
		{
			timed_seek_request();
		}
		else
		{
			if (write) write_sectors_continue();
			else read_sectors_continue();
		}
	}
	else
	{
		set_command_done(ST_TC_RDIDERR);
	}
}


void smc92x4_device::read_sectors_continue()
{
	bool check_sector = true; /* always check the first sector */
	int state = AGAIN;
	int logical = m_command & 0x04;

	/* Needed for the two ways of re-entry: during the seek process, and during sector read */
	if (!m_after_seek)
	{
		if (VERBOSE>7) LOG("smc92x4 continue with sector read\n");
		m_seek_count--;
		if (m_seek_count > 0)
		{
			read_write_sectors();
			return;
		}
		m_after_seek = true;
	}
	else
	{
		/* we are here after the sector read. */
		m_to_be_continued = false;
		if (m_register_r[INT_STATUS] & ST_TERMCOD)
		{
			if (VERBOSE>1) LOG("smc92x4 error: data error during sector read: INTSTATUS=%02x\n", m_register_r[INT_STATUS]);
			set_command_done(ST_TC_DATAERR);
			return;
		}
	}

	m_to_be_continued = false;

	/* Wait for SEEK_COMPLETE. We assume the signal has appeared. */

	if (m_register_w[SECTOR_COUNT] > 0 /* && !(m_register_r[DRIVE_STATUS] & DS_INDEX) */)
	{
		/* Call the verify sequence. */
		state = verify(&m_recent_id, check_sector);
		if (state==ERROR)
		{
			// TODO: set command done?
			if (VERBOSE>0) LOG("smc92x4 error: verify error during sector read\n");
			return;
		}

		/* For read physical, only verify the first sector. */
		if (!logical)
			check_sector = false;

		if (m_recent_id.flags & BAD_SECTOR)
		{
			if (VERBOSE>0) LOG("smc92x4 error: Bad sector, seek error\n");
			set_command_done(ST_TC_SEEKERR);
		}
		else
		{
			timed_sector_read_request();
		}
	}
	else
	{
		set_command_done(ST_TC_SUCCESS);
		if (VERBOSE>7) LOG("smc92x4 read sector command done\n");
	}
}

void smc92x4_device::write_sectors_continue()
{
	bool check_sector = true; /* always check the first sector */
	int state = AGAIN;
	int logical = m_command & 0x20;

	/* Needed for the two ways of re-entry: during the seek process, and during sector write */
	if (!m_after_seek)
	{
		m_seek_count--;
		if (m_seek_count > 0)
		{
			read_write_sectors();
			return;
		}
		m_after_seek = true;
	}
	else
	{
		/* we are here after the sector write. */
		m_to_be_continued = false;
		if (m_register_r[INT_STATUS] & ST_TERMCOD)
		{
			if (VERBOSE>0) LOG("smc92x4 error: data error during sector write\n");
			set_command_done(ST_TC_DATAERR);
			return;
		}
	}

	m_to_be_continued = false;

	if ((m_register_w[RETRY_COUNT] & 0xf0)!= 0xf0)
		if (VERBOSE>1) LOG("smc92x4 warn: RETRY_COUNT in write sector should be set to 0. Ignored.\n");

	/* Wait for SEEK_COMPLETE. We assume the signal has appeared. */
	if (m_register_w[SECTOR_COUNT] > 0)
	{
		/* Call the verify sequence. */
		state = verify(&m_recent_id, check_sector);
		if (state==ERROR)
		{
			if (VERBOSE>0) LOG("smc92x4 error: verify error during sector write\n");
			return;
		}

		/* For write physical, only verify the first sector. */
		if (!logical)
			check_sector = false;

/*      printf("smc92x4 write sectors CYL=%02x, HEA=%02x, SEC=%02x, MOD=%02x, CNT=%02x, TRY=%02x\n",
            m_register_w[DESIRED_CYLINDER],
            m_register_w[DESIRED_HEAD],
            m_register_w[DESIRED_SECTOR],
            m_register_w[MODE],
            m_register_w[SECTOR_COUNT],
            m_register_w[RETRY_COUNT]); */
		timed_sector_write_request();
	}
	else
	{
		set_command_done(ST_TC_SUCCESS);
		if (VERBOSE>7) LOG("smc92x4 write sector command done\n");
	}
}

/*********************************************************************
    Command implementations
*********************************************************************/

/*
    Handle the restore command
*/
void smc92x4_device::restore_drive()
{
	/* TODO: int_after_seek_complete required for buffered seeks */
	sync_status_in();

	if (m_seek_count>=4096 || !(m_register_r[DRIVE_STATUS] & DS_READY))
	{
		if (VERBOSE>0) LOG("smc92x4 error: seek error in restore\n");
		m_register_r[INT_STATUS] |= ST_TC_SEEKERR;
		return;
	}

	if (m_register_r[DRIVE_STATUS] & DS_TRK00)
	{
		m_register_r[INT_STATUS] |= ST_TC_SUCCESS;
		/* Issue interrupt */
		set_interrupt();
		return;
	}

	m_step_direction = -1;
	timed_seek_request();
}

void smc92x4_device::restore_continue()
{
	m_seek_count++;

	/* Next iteration */
	restore_drive();

	m_to_be_continued = false;
}

/*
    Handle the step command. Note that the CURRENT_CYLINDER register is not
    updated (this would break the format procedure).
*/
void smc92x4_device::step_in_out()
{
	int direction = (m_command & 0x02)? -1 : +1;
	// bit 0: 0 -> command ends after last seek pulse, 1 -> command
	// ends when the drive asserts the seek complete pin
	int buffered = m_command & 0x01;

	m_step_direction = direction;
	m_buffered = (buffered!=0);

	timed_seek_request();
	if (VERBOSE>6) LOG("smc92x4 waiting for drive step\n");
}

void smc92x4_device::step_continue()
{
	if (VERBOSE>7) LOG("smc92x4 step continue\n");
	m_to_be_continued = false;
	set_command_done(ST_TC_SUCCESS);
}

/*
    Poll drives
    This command is used to find out which drive has complete a buffered
    seek (RESTORE, SEEK IN/OUT with BUFFERED set to one)
*/
void smc92x4_device::poll_drives()
{
	int mask = 0x08;
	int i;
	int flags = m_command & 0x0f;

/* Spec is unclear: Do we continue to poll the drives after we have checked each
one for the first time? We are not interested in locking up the emulator, so
we decide to poll only once. */
	for (i=3; i>=0; i--)
	{
		if (flags & mask)
		{
			/* Poll drive */
			drive_select(i|((m_types[i])<<2));
			if (m_register_r[DRIVE_STATUS] & DS_SKCOM) return;
		}
		mask = mask>>1;
	}
}

void smc92x4_device::drive_select(int driveparm)
{
	m_output1 = (0x10 << (driveparm & 0x03)) | (m_register_w[RETRY_COUNT]&0x0f);
	m_selected_drive_type = (driveparm>>2) & 0x03;
	m_head_load_delay_enable = (driveparm>>4)&0x01;

	// We need to store the type of the drive for the poll_drives command
	// to be able to correctly select the device (floppy or hard disk).
	m_types[driveparm&0x03] = m_selected_drive_type;

	// Copy the DMA registers to registers CURRENT_HEAD, CURRENT_CYLINDER,
	// and CURRENT_IDENT. This is required during formatting (p. 14) as the
	// format command reuses the registers for formatting parameters.
	m_register_r[CURRENT_HEAD] = m_register_r[DMA7_0];
	m_register_r[CURRENT_CYLINDER] = m_register_r[DMA15_8];
	m_register_r[CURRENT_IDENT] = m_register_r[DMA23_16];

	sync_latches_out();
	sync_status_in();
}

/*
    Command SEEK/READID
*/
void smc92x4_device::seek_read_id()
{
	int step_enable = (m_command & 0x04);
	int verify_id = (m_command & 0x01);
	int wait = (m_command & 0x02);

	m_to_be_continued = false;

	if (!m_found_id)
	{
		/* First start. */
		read_id_field(&m_seek_count, &m_step_direction, &m_recent_id);
		m_found_id = true;
	}

	if ((m_register_r[INT_STATUS] & ST_TC_DATAERR) == ST_TC_SUCCESS)
	{
		/* Perform the seek for the cylinder. */
		if (step_enable && m_seek_count > 0)
		{
			timed_seek_request();
		}
		else
		{
			if (wait)
				if (VERBOSE>1) LOG("smc92x4 warn: seed_read_id: Waiting for seek_complete not implemented.\n");

			if (verify_id)
			{
				verify(&m_recent_id, true);
			}
		}
	}
	else
	{
		set_command_done(ST_TC_RDIDERR);
	}
}

void smc92x4_device::seek_read_id_continue()
{
	m_seek_count--;
	seek_read_id();
	m_to_be_continued = false;
}

/*
    Formats a track starting from the detection of an index mark until the
    detection of another index mark.
    The formatting is done exclusively by the controller; user programs may
    set parameters for gaps and interleaving.

    1. Before starting the command, the user program must have set up a
    sector sequence table in the controller RAM (located on the PCB):
    (ident, cylinder, head, sector1, size)  (5 bytes)
    (ident, cylinder, head, sector2, size)
    (ident, cylinder, head, sector3, size)
    ...
    ident is not required for floppy FM operation. size is not required
    for IBM AT-compatible hard disks.

    2. The DMA registers must point to the beginning of the table

    3. DRIVE_SELECT must be executed (which moves DMA regs to CUR_HEAD ...)

    4. DESIRED_HEAD register must be loaded

    5. The following setup must be done:

    GAP 0 size      DMA7_0          (2s comp)
    GAP 1 size      DMA15_8         (2s comp)
    GAP 2 size      DMA23_16        (2s comp)
    GAP 3 size      DESIRED_SECTOR      (2s comp)
    Sync size       DESIRED_CYLINDER    (1s comp)
    Sector count        SECTOR_COUNT        (1s comp)
    Sector size multiple    RETRY_COUNT         (1s comp)

    GAP4 is variable and fills the rest of the track until the next
    index hole. Usually we have 247 bytes for FM and 598 for MFM.

    6. The step rate and density must be loaded into the MODE register

    7. The drive must be stepped to the desired track.

    8. Now this command may be started.

    All data bytes of a sector are filled with 0xe5. The gaps will be filled
    with 0x4e (MFM) or 0xff (FM).

    To format another track, the sector id table must be updated, and steps
    7 and 8 must be repeated. If the DESIRED_HEAD register must be updated,
    the complete setup process must be done.

    Options: Command = 011m rppp
    m = set data mark (0 = set deleted)
    r = current (1 = reduced)
    ppp = precompensation (for sector size = (2^ppp) * 128; ppp<4 for floppy )

    ===============

    One deviation from the specification: The TI-99 family uses the SDF
    and TDF formats. The TDF formats complies with the formats produced
    by the standard TI disk controller. It does not use index address marks.
    So we could integrate a translation in ti99_dsk when writing to disk,
    but when reading, how can ti99_dsk know whether to blow up the format
    to full length for this controller or keep it as is for the other
    controller? Unless someone comes up with a better idea, we implement
    an "undocumented" option in this controller, allowing to create a
    different track layout.

    So there are two layouts for the floppy track:

    - full format, including the index address mark, according to the
      specification

    - short format without index AM which matches the PC99 format used
      for the TI-99 family.

    The formats are determined by setting the flag in the smc92x4
    interface structure.
*/
void smc92x4_device::format_floppy_track(int flags)
{
	floppy_image_legacy *floppy;
	int i,index,j, exp_size;
	int gap0, gap1, gap2, gap3, gap4, sync1, sync2, count, size, fm;
	int gap_byte, pre_gap, crc, mark, inam;
	UINT8 curr_cyl, curr_head, curr_sect, curr_size;

	int normal_data_mark = flags & 0x10;

	dynamic_buffer buffer;

	/* Determine the track size. We cannot allow different sizes in this design. */
	int data_count = 0;

	sync_status_in();

	floppy = m_drive->flopimg_get_image();

	if (floppy != NULL)
		data_count = floppy_get_track_size(floppy, 0, 0);

	if (data_count==0)
	{
		if (in_single_density_mode())
			data_count = TRKSIZE_SD;
		else
			data_count = TRKSIZE_DD;
	}

	/* Build buffer */
	buffer.resize(data_count);

	fm = in_single_density_mode();

	sync2 = (~m_register_w[DESIRED_CYLINDER])&0xff;
	gap2 = (-m_register_w[DMA23_16])&0xff;
	count = (~m_register_w[SECTOR_COUNT])&0xff;
	size = (~m_register_w[RETRY_COUNT])&0xff;
	gap_byte = (fm)? 0xff : 0x4e;

	if (m_full_track_layout)
	{
		/* Including the index AM. */
		gap0 = (-m_register_w[DMA7_0])&0xff;
		gap1 = (-m_register_w[DMA15_8])&0xff;
		gap3 = (-m_register_w[DESIRED_SECTOR])&0xff;
		gap4 = (fm)? 247 : 598;
		pre_gap = gap_byte;
		sync1 = sync2;
		inam = sync2 + ((fm)? 1 : 4);
	}
	else
	{
		/* Specific overrides for this format. We do not have the index mark. */
		gap0 = (fm)? 16 : 40;
		gap1 = 0;
		gap3 = (fm)? 45 : 24;
		gap4 = (fm)? 231 : 712;
		pre_gap = (fm)? 0x00 : 0x4e;
		sync1 = (fm)? 6 : 10;
		inam = 0;
	}

	index = 0;

	mark = (fm)? 10 : 16;  /* ID/DAM + A1 + CRC */

	exp_size = gap0 + inam + gap1 + count*(sync1 + mark + gap2 + sync2 + size*128 + gap3) + gap4;

	if (exp_size != data_count)
		if (VERBOSE>0) LOG("smc92x4 warn: The current track length in the image (%d) does not match the new track length (%d). Keeping the old length. This will break the image (sorry).\n", data_count, exp_size);

	/* use the backup registers set up during drive_select */
	set_dma_address(CURRENT_IDENT, CURRENT_CYLINDER, CURRENT_HEAD);

	memset(&buffer[index], pre_gap, gap0);
	index += gap0;

	if (m_full_track_layout)
	{
		/* Create the Index AM */
		memset(&buffer[index], 0x00, sync1);
		index += sync1;
		if (!fm)
		{
			memset(&buffer[index], 0xc2, 3);
			index += 3;
		}
		memset(&buffer[index], gap_byte, gap1);
		index += gap1;
	}

	/* for each sector */
	for (j=0; j < count; j++)
	{
		memset(&buffer[index], 0x00, sync1);
		index += sync1;

		if (!fm)
		{
			memset(&buffer[index], 0xa1, 3);
			index += 3;
		}

		buffer[index++] = 0xfe;

		m_out_dip(ASSERT_LINE);
//      if (!fm) curr_ident = m_in_dma();
		if (!fm) m_in_dma(0);
		curr_cyl = m_in_dma(0);
		curr_head = m_in_dma(0);
		curr_sect = m_in_dma(0);
		curr_size = m_in_dma(0);
		m_out_dip(CLEAR_LINE);

		buffer[index++] = curr_cyl;
		buffer[index++] = curr_head;
		buffer[index++] = curr_sect;
		buffer[index++] = curr_size;

		if (j==0)
			if (VERBOSE>6) LOG("current_floppy=%s, format track %d, head %d\n",  m_drive->tag(), curr_cyl, curr_head);

		/* Calculate CRC16 (5 bytes for ID) */
		crc = ccitt_crc16(0xffff, &buffer[index-5], 5);
		buffer[index++] = (crc>>8)&0xff;
		buffer[index++] = crc & 0xff;

		memset(&buffer[index], gap_byte, gap2);
		index += gap2;

		memset(&buffer[index], 0x00, sync2);
		index += sync2;

		if (!fm)
		{
			memset(&buffer[index], 0xa1, 3);
			index += 3;
		}

		if (normal_data_mark) buffer[index++] = 0xfb;
		else buffer[index++] = 0xf8;

		/* Sector data */
		for (i=0; i < 128*size; i++) buffer[index++] = 0xe5;

		/* Calculate CRC16 (128*size+1 bytes for sector) */
		crc = ccitt_crc16(0xffff, &buffer[index-128*size-1], 128*size+1);
		buffer[index++] = (crc>>8)&0xff;
		buffer[index++] = crc & 0xff;

		memset(&buffer[index], gap_byte, gap3);
		index += gap3;
	}

	memset(&buffer[index], gap_byte, gap4);
	index += gap4;

	m_drive->floppy_drive_write_track_data_info_buffer(m_register_w[DESIRED_HEAD]&0x0f, buffer, &data_count);
	sync_status_in();
}

/*
    Create the track layout of a MFM hard disk. Like floppy disks,
    MFM hard disks are interfaced on a track base, that is, we have to
    create a complete track layout.

    For more explanations see the comments to format_floppy_track.
*/
void smc92x4_device::format_harddisk_track(int flags)
{
	int i,index,j;
	int gap1, gap2, gap3, gap4, sync, count, size, gap_byte, crc;
	UINT8 curr_ident, curr_cyl, curr_head, curr_sect, curr_size;

	int normal_data_mark = flags & 0x10;
	int data_count=0;

	dynamic_buffer buffer;

	sync_status_in();

	/* Build buffer */
//  gap0 = (-m_register_w[DMA7_0])&0xff;
	gap1 = (-m_register_w[DMA15_8])&0xff;
	gap2 = (-m_register_w[DMA23_16])&0xff;
	gap3 = (-m_register_w[DESIRED_SECTOR])&0xff;
	gap4 = 340;

	sync = (~m_register_w[DESIRED_CYLINDER])&0xff;
	count = (~m_register_w[SECTOR_COUNT])&0xff;
	size = (~m_register_w[RETRY_COUNT])&0xff;

	data_count = gap1 + count*(sync+12+gap2+sync+size*128+gap3)+gap4;

	buffer.resize(data_count);

	index = 0;
	gap_byte = 0x4e;

	/* use the backup registers set up during drive_select */
	set_dma_address(CURRENT_IDENT, CURRENT_CYLINDER, CURRENT_HEAD);

	for (i=0; i < gap1; i++) buffer[index++] = gap_byte;

	/* Now write the sectors. */
	for (j=0; j < count; j++)
	{
		for (i=0; i < sync; i++) buffer[index++] = 0x00;
		buffer[index++] = 0xa1;

		m_out_dip(ASSERT_LINE);
		curr_ident = m_in_dma(0);
		curr_cyl = m_in_dma(0);
		curr_head = m_in_dma(0);
		curr_sect = m_in_dma(0);
		curr_size = m_in_dma(0);
		m_out_dip(CLEAR_LINE);

		buffer[index++] = curr_ident;
		buffer[index++] = curr_cyl;
		buffer[index++] = curr_head;
		buffer[index++] = curr_sect;
		buffer[index++] = curr_size;

		/* Calculate CRC16 (5 bytes for ID) */
		crc = ccitt_crc16(0xffff, &buffer[index-5], 5);
		buffer[index++] = (crc>>8)&0xff;
		buffer[index++] = crc & 0xff;

		/* GAP 2 */
		for (i=0; i < gap2; i++) buffer[index++] = gap_byte;
		for (i=0; i < sync; i++) buffer[index++] = 0x00;

		buffer[index++] = 0xa1;

		if (normal_data_mark) buffer[index++] = 0xfb;
		else buffer[index++] = 0xf8;

		/* Sector data */
		for (i=0; i < 128*size; i++) buffer[index++] = 0xe5;

		/* Calculate CRC16 (128*size+1 bytes for sector) */
		crc = ccitt_crc16(0xffff, &buffer[index-128*size-1], 128*size+1);
		buffer[index++] = (crc>>8)&0xff;
		buffer[index++] = crc & 0xff;

		/* GAP 3 */
		for (i=0; i < 3; i++) buffer[index++] = 0;  /* check that, unclear in spec */
		for (i=0; i < gap3 - 3; i++) buffer[index++] = gap_byte;
	}
	/* GAP 4 */
	for (i=0; i < gap4; i++) buffer[index++] = gap_byte;

	// Now write the whole track
	m_harddisk->write_track(m_register_w[DESIRED_HEAD]&0x0f, buffer, data_count);

	sync_status_in();
}

/*
    Read a floppy track.
    A complete track is read at the position of the head. It reads the
    track from one index pulse to the next index pulse. (Note that the
    spec talks about "index mark" and "signal from the drive" which is
    a bit confusing, since the index AM is behind Gap0, the index hole
    is before Gap0. We should check with a real device. Also, it does not
    speak about the head, so we assume the head is set in the DESIRED_HEAD
    register.)

    TODO: The TDF format does not support index marks. Need to define TDF
    in a more flexible way. Also consider format variations.
    (Hint: Do a check for the standard "IBM" format. Should probably do
    a translation from IBM to PC99 and back. Requires to parse the track
    image before. Need to decide whether we generally translate to the IBM
    format or generally to the PC99 format between controller and format.
    Format is the image is always PC99.)
*/
void smc92x4_device::read_floppy_track(bool transfer_only_ids)
{
	floppy_image_legacy *floppy;
	/* Determine the track size. We cannot allow different sizes in this design. */
	int data_count = 0;
	int i;
	UINT8 *buffer;

	sync_latches_out();

	floppy = m_drive->flopimg_get_image();

	/* Determine the track size. We cannot allow different sizes in this design. */
	if (floppy != NULL)
		data_count = floppy_get_track_size(floppy, 0, 0);

	if (data_count==0)
	{
		if (in_single_density_mode())
			data_count = TRKSIZE_SD;
		else
			data_count = TRKSIZE_DD;
	}

	buffer = global_alloc_array(UINT8, data_count);

	m_drive->floppy_drive_read_track_data_info_buffer(m_register_w[DESIRED_HEAD]&0x0f, (char *)buffer, &data_count);
	sync_status_in();

	// Transfer the buffer to the external memory. We assume the memory
	// pointer has been set appropriately in the registers.
	set_dma_address(DMA23_16, DMA15_8, DMA7_0);

	if (transfer_only_ids)
	{
		if (VERBOSE>1) LOG("smc92x4 warn: read track: Ignoring transfer-only-ids. Reading complete track.\n");
	}

	m_out_dip(ASSERT_LINE);
	for (i=0; i < data_count; i++)
	{
		m_out_dma((offs_t)0, buffer[i]);
	}
	m_out_dip(CLEAR_LINE);

	global_free_array(buffer);
}

void smc92x4_device::read_harddisk_track(bool transfer_only_ids)
{
	/* Determine the track size. We cannot allow different sizes in this design. */
	int i;
	UINT8 *buffer;
	int data_count=0;
	sync_latches_out();

	data_count = m_harddisk->get_track_length();
	buffer = global_alloc_array(UINT8, data_count);

	/* buffer and data_count are allocated and set by the function. */
	m_harddisk->read_track(m_register_w[DESIRED_HEAD]&0x0f, buffer);
	sync_status_in();

	if (!(m_register_r[DRIVE_STATUS] & DS_READY))
	{
		if (VERBOSE>0) LOG("smc92x4 error: read harddisk track failed.\n");
	}

	// Transfer the buffer to the external memory. We assume the memory
	// pointer has been set appropriately in the registers.
	set_dma_address(DMA23_16, DMA15_8, DMA7_0);

	if (transfer_only_ids)
	{
		if (VERBOSE>1) LOG("smc92x4 warn: read track: Ignoring transfer-only-ids. Reading complete track.\n");
	}

	m_out_dip(ASSERT_LINE);
	for (i=0; i < data_count; i++)
	{
		m_out_dma((offs_t)0, buffer[i]);
	}
	m_out_dip(CLEAR_LINE);

	global_free_array(buffer);
}


void smc92x4_device::read_track()
{
	int transfer_only_ids = m_command & 0x01;

	if (m_selected_drive_type & TYPE_FLOPPY)
	{
		read_floppy_track(transfer_only_ids);
	}
	else
	{
		read_harddisk_track(transfer_only_ids);
	}
	timed_track_request();
}

void smc92x4_device::read_track_continue()
{
	m_to_be_continued = false;
	set_command_done(ST_TC_SUCCESS);
}

void smc92x4_device::format_track()
{
	int flags = m_command & 0x1f;

	if (m_selected_drive_type & TYPE_FLOPPY)
		format_floppy_track(flags);
	else
		format_harddisk_track(flags);

	timed_track_request();
}

void smc92x4_device::format_track_continue()
{
	m_to_be_continued = false;
	set_command_done(ST_TC_SUCCESS);
}

/*
    Continue to process after callback
*/
void smc92x4_device::process_after_callback()
{
	UINT8 opcode = m_command;
	if (opcode >= 0x02 && opcode <= 0x03)
	{
		restore_continue();
	}
	else if (opcode >= 0x04 && opcode <= 0x07)
	{
		step_continue();
	}
	else if (opcode >= 0x50 && opcode <= 0x57)
	{
		seek_read_id_continue();
	}
	else if ((opcode >= 0x58 && opcode <= 0x59)||(opcode >= 0x5C && opcode <= 0x5f))
	{
		read_sectors_continue();
	}
	else if (opcode >= 0x5a && opcode <= 0x5b)
	{
		read_track_continue();
	}
	else if (opcode >= 0x60 && opcode <= 0x7f)
	{
		format_track_continue();
	}
	else if (opcode >= 0x80)
	{
		write_sectors_continue();
	}
	else
	{
		if (VERBOSE>1) LOG("smc92x4 warn: Invalid command %x or command changed while waiting for callback\n", opcode);
	}
}

/*
    Process a command
*/
void smc92x4_device::process_command(UINT8 opcode)
{
	if (VERBOSE>6) LOG("smc92x4 process command %02x\n", opcode);
	if (m_to_be_continued)
	{
		if (VERBOSE>1) LOG("smc92x4 warn: previous command %02x not complete\n", m_command);
	}

	/* Reset DONE and BAD_SECTOR. */
	m_register_r[INT_STATUS] &= ~(ST_DONE | ST_BADSECT);

	// Reset interrupt line (not explicitly mentioned in spec, but seems reasonable
	clear_interrupt();
	m_register_r[INT_STATUS] &= ~(ST_INTPEND | ST_RDYCHNG);

	m_command = opcode;
	m_found_id = false;
	m_seek_count = 0;

	if (opcode == 0x00)
	{
		/* RESET */
		/* same effect as the RST* pin being active */
		if (VERBOSE>0) LOG("smc92x4 info: reset command\n");
		reset();
	}
	else if (opcode == 0x01)
	{
		/* DESELECT DRIVE */
		/* done when no drive is in use */
		if (VERBOSE>2) LOG("smc92x4 info: drdeselect command\n");
		m_output1 &= ~(OUT1_DRVSEL3|OUT1_DRVSEL2|OUT1_DRVSEL1|OUT1_DRVSEL0);
		m_output2 |= OUT2_DRVSEL3_;
		/* sync the latches on the PCB */
		sync_latches_out();
		sync_status_in();
	}
	else if (opcode >= 0x02 && opcode <= 0x03)
	{
		/* RESTORE DRIVE */
		// bit 0: 0 -> command ends after last seek pulse, 1 -> command
		// ends when the drive asserts the seek complete pin
		if (VERBOSE>2) LOG("smc92x4 info: restore command %X\n", opcode);
		restore_drive();
	}
	else if (opcode >= 0x04 && opcode <= 0x07)
	{
		/* STEP IN/OUT ONE CYLINDER */
		if (VERBOSE>2) LOG("smc92x4 info: step in/out command %X\n", opcode);
		step_in_out();
	}
	else if (opcode >= 0x08 && opcode <= 0x0f)
	{
		/* TAPE BACKUP (08-0f)*/
		if (VERBOSE>0) LOG("smc92x4 error: tape backup command %X not implemented\n", opcode);
	}
	else if (opcode >= 0x10 && opcode <= 0x1f)
	{
		/* POLLDRIVE */
		if (VERBOSE>2) LOG("smc92x4 info: polldrive command %X\n", opcode);
		poll_drives();
	}
	else if (opcode >= 0x20 && opcode <= 0x3f)
	{
		/* DRIVE SELECT */
		if (VERBOSE>2) LOG("smc92x4 info: drselect command %X\n", opcode);
		drive_select(opcode&0x1f);
	}
	else if (opcode >= 0x40 && opcode <= 0x4f)
	{
		/* SETREGPTR */
		if (VERBOSE>2) LOG("smc92x4 info: setregptr command %X\n", opcode);
		m_register_pointer = opcode & 0xf;
		// Spec does not say anything about the effect of setting an
		// invalid value (only "care should be taken")
		if (m_register_pointer > 10)
		{
			if (VERBOSE>1) LOG("smc92x4 error: set register pointer: Invalid register number: %d. Setting to 10.\n", m_register_pointer);
			m_register_pointer = 10;
		}
	}
	else if (opcode >= 0x50 && opcode <= 0x57)
	{
		/* SEEK/READ ID */
		if (VERBOSE>2) LOG("smc92x4 seekreadid command %X\n", opcode);
		seek_read_id();
	}
	else if ((opcode >= 0x58 && opcode <= 0x59)
		|| (opcode >= 0x5C && opcode <= 0x5f)
		|| (opcode >= 0x80))
	{
		/* READ/WRITE SECTORS PHYSICAL/LOGICAL */
		if (VERBOSE>2) LOG("smc92x4 info: read/write sector command %X\n", opcode);
		read_write_sectors();
	}
	else if (opcode >= 0x5A && opcode <= 0x5b)
	{
		/* READ TRACK */
		if (VERBOSE>2) LOG("smc92x4 info: read track command %X\n", opcode);
		read_track();
	}
	else if (opcode >= 0x60 && opcode <= 0x7f)
	{
		/* FORMAT TRACK */
		if (VERBOSE>2) LOG("smc92x4 info: format track command %X\n", opcode);
		format_track();
	}
	else
	{
		if (VERBOSE>0) LOG("smc92x4 error: Invalid command %x, ignored\n", opcode);
	}

	if (!m_to_be_continued)
		set_command_done();
}

/***************************************************************************
    Memory accessors
****************************************************************************/

/*
    Read a byte of data from a smc92x4 controller
    The address (offset) encodes the C/D* line (command and /data)
*/
READ8_MEMBER( smc92x4_device::read )
{
	UINT8 reply = 0;

	if ((offset & 1) == 0)
	{
		/* data register */
		reply = m_register_r[m_register_pointer];
		if (VERBOSE>6) LOG("smc92x4 register_r[%d] -> %02x\n", m_register_pointer, reply);
		/* Autoincrement until DATA is reached. */
		if (m_register_pointer < DATA)
			m_register_pointer++;
	}
	else
	{
		/* status register */
		reply = m_register_r[INT_STATUS];
		// Spec (p.3) : The interrupt pin is reset to its inactive state
		// when the UDC interrupt status register is read.
		if (VERBOSE>6) LOG("smc92x4 interrupt status read = %02x\n", reply);
		clear_interrupt();
		/* Clear the bits due to int status register read. */
		m_register_r[INT_STATUS] &= ~(ST_INTPEND | ST_RDYCHNG);
	}
	return reply;
}

/*
    Write a byte to a smc99x4 controller
    The address (offset) encodes the C/D* line (command and /data)
*/
WRITE8_MEMBER( smc92x4_device::write )
{
	data &= 0xff;

	if ((offset & 1) == 0)
	{
		/* data register */
		if (VERBOSE>6) LOG("smc92x4 register_w[%d] <- %X\n", m_register_pointer, data);
		m_register_w[m_register_pointer] = data;

		// The DMA registers and the sector register for read and
		// write are identical.
		if (m_register_pointer < DESIRED_HEAD)
			m_register_r[m_register_pointer] = data;

		/* Autoincrement until DATA is reached. */
		if (m_register_pointer < DATA)
			m_register_pointer++;
	}
	else
		process_command(data);  // command register
}



/***************************************************************************
    DEVICE FUNCTIONS
***************************************************************************/

void smc92x4_device::device_start()
{
	const smc92x4_interface *intf = reinterpret_cast<const smc92x4_interface *>(static_config());

	m_out_intrq.resolve_safe();
	m_out_dip.resolve_safe();
	m_out_auxbus.resolve_safe();
	m_in_auxbus.resolve_safe(0);
	m_out_dma.resolve_safe();
	m_in_dma.resolve_safe(0);

	m_full_track_layout = intf->full_track_layout;

	// allocate timers
	// m_timer_data = timer_alloc(DATA_TIMER);
	m_timer_rs = timer_alloc(READ_TIMER);
	m_timer_ws = timer_alloc(WRITE_TIMER);
	m_timer_track = timer_alloc(TRACK_TIMER);
	m_timer_seek = timer_alloc(SEEK_TIMER);

	m_use_real_timing = true;
}

void smc92x4_device::device_reset()
{
	clear_interrupt();
	m_out_dip(CLEAR_LINE);

	for (int i=0; i<=11; i++)
		m_register_r[i] = m_register_w[i] = 0;

	m_to_be_continued = false;
}

void smc92x4_device::set_timing(bool realistic)
{
	m_use_real_timing = realistic;
	if (VERBOSE>0) LOG("smc92x4: use realistic timing: %02x\n", realistic);
}

void smc92x4_device::connect_floppy_drive(legacy_floppy_image_device *drive)
{
	m_drive = drive;
	if (VERBOSE>3)
	{
		if (drive==NULL) LOG("smc92x4: Unselect all drives\n");
		else LOG("smc92x4: Connect drive %s\n", drive->tag());
	}
}
void smc92x4_device::connect_hard_drive(mfm_harddisk_device *drive)
{
	m_harddisk = drive;
	if (VERBOSE>3)
	{
		if (drive==NULL) LOG("smc92x4: Unselect all drives\n");
		else LOG("smc92x4: Connect drive %s\n", drive->tag());
	}
}

void smc92x4_device::reset()
{
	device_reset();
}

const device_type SMC92X4 = &device_creator<smc92x4_device>;