summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/i82586.cpp
blob: 4e55b61b8bdbbb32ce0de1ba85c3f6e3902434f9 (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
// license:BSD-3-Clause
// copyright-holders:Patrick Mackinlay

/*
* An implementation of the Intel 82586 and 82596 Ethernet controller devices.
*
* This driver covers the following devices:
*
*   - 82586 - 16/24 data/address bus, 6/8/10 MHz
*   - 82596SX - 16/32 data/address bus, 16/20 MHz
*   - 82596DX - 32/32 data/address bus, 25/33 MHz
*   - 82596CA - 32/32 data/address bus, 16/20/25/33 MHz
*
* This implementation should cover all of the above reasonably well, but
* no testing of big endian mode in particular, and very limited testing
* of the 82596 in non-linear modes has been done so far.
*
* Some documents covering the above include:
*
*   http://bitsavers.org/pdf/intel/_dataBooks/1991_Microcommunications.pdf
*   http://bitsavers.org/pdf/intel/_dataBooks/1996_Networking.pdf
*   https://www.intel.com/assets/pdf/general/82596ca.pdf
*
* TODO
*   - testing for 82596 big endian and non-linear modes
*   - more complete statistics capturing
*   - 82596 monitor mode
*   - throttle timers and diagnostic command
*   - special case handling for different 82596 steppings in big endian mode
*/

#include "emu.h"
#include "i82586.h"
#include "hashing.h"

#define LOG_GENERAL (1U << 0)
#define LOG_FRAMES  (1U << 1)
#define LOG_FILTER  (1U << 2)
#define LOG_CONFIG  (1U << 3)

//#define VERBOSE (LOG_GENERAL | LOG_FRAMES | LOG_FILTER | LOG_CONFIG)

#include "logmacro.h"

// disable FCS insertion (on transmit) and checking (on receive) because pcap doesn't expose them
#define I82586_FCS 0

ALLOW_SAVE_TYPE(i82586_base_device::cu_state);
ALLOW_SAVE_TYPE(i82586_base_device::ru_state);

DEFINE_DEVICE_TYPE(I82586, i82586_device, "i82586", "Intel 82586 IEEE 802.3 Ethernet LAN Coprocessor")
DEFINE_DEVICE_TYPE(I82596_LE16, i82596_le16_device, "i82596sx_le", "Intel 82596 SX High-Performance 32-Bit Local Area Network Coprocessor (little)")
DEFINE_DEVICE_TYPE(I82596_BE16, i82596_be16_device, "i82596sx_be", "Intel 82596 SX High-Performance 32-Bit Local Area Network Coprocessor (big)")
DEFINE_DEVICE_TYPE(I82596_LE32, i82596_le32_device, "i82596dx_le", "Intel 82596 DX/CA High-Performance 32-Bit Local Area Network Coprocessor (little)")
DEFINE_DEVICE_TYPE(I82596_BE32, i82596_be32_device, "i82596dx_be", "Intel 82596 DX/CA High-Performance 32-Bit Local Area Network Coprocessor (big)")

// Ethernet broadcast address
static const u8 ETH_BROADCAST[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

// configure parameter default values
static const u8 CFG_DEFAULTS[] = { 0x00, 0xc8, 0x40, 0x26, 0x00, 0x60, 0x00, 0xf2, 0x00, 0x00, 0x40, 0xff, 0x00, 0x3f };

// describes parameters and default values for logging
static const struct
{
	const char *const name, *const unit;
	const u8 dflt, byte, mask, shift;
	const bool ieee8023;
}
CFG_PARAMS[] =
{
	{ "address length",             "bytes",                               6,  3, 0x07, 0, true },
	{ "a/l field location",         "located in fd",                       0,  3, 0x08, 3, false },
	{ "auto retransmit",            "auto retransmit enable",              1, 11, 0x08, 3, false },
	{ "bitstuffing/eoc",            "eoc",                                 0,  8, 0x40, 6, false },
	{ "broadcast disable",          "broadcast reception enabled",         0,  8, 0x02, 1, false },
	{ "cdbsac",                     "disabled",                            1, 11, 0x10, 4, false },
	{ "cdt filter",                 "bit times",                           0,  9, 0x70, 4, false },
	{ "cdt src",                    "external collision detection",        0,  9, 0x80, 7, false },
	{ "crc in memory",              "crc not transferred to memory",       1, 11, 0x04, 2, false },
	{ "crc-16/crc-32",              "crc-32",                              0,  8, 0x20, 5, true },
	{ "crs filter",                 "bit times",                           0,  9, 0x07, 0, false },
	{ "crs src",                    "external crs",                        0,  9, 0x08, 3, false },
	{ "disbof",                     "backoff enabled",                     0, 13, 0x80, 7, false },
	{ "ext loopback",               "disabled",                            0,  3, 0x80, 7, false },
	{ "exponential priority",       "802.3 algorithm",                     0,  4, 0x70, 4, true },
	{ "exponential backoff method", "802.3 algorithm",                     0,  4, 0x80, 7, true },
	{ "full duplex (fdx)",          "csma/cd protocol (no fdx)",           0, 12, 0x40, 6, false },
	{ "fifo threshold",             "tx: 32 bytes, rx: 64 bytes",          8,  1, 0x0f, 0, false },
	{ "int loopback",               "disabled",                            0,  3, 0x40, 6, false },
	{ "interframe spacing",         "bit times",                          96,  5, 0xff, 0, true },
	{ "linear priority",            "802.3 algorithm",                     0,  4, 0x07, 0, true },
	{ "length field",               "padding disabled",                    1, 11, 0x02, 1, false },
	{ "min frame length",           "bytes",                              64, 10, 0xff, 0, true },
	{ "mc all",                     "disabled",                            1, 11, 0x20, 5, false },
	{ "monitor",                    "disabled",                            3, 11, 0xc0, 6, false },
	{ "manchester/nrz",             "nrz",                                 0,  8, 0x04, 2, false },
	{ "multi ia",                   "disabled",                            0, 14, 0x40, 6, false },
	{ "number of retries",          "maximum number of retries",          15,  7, 0xf0, 4, true },
	{ "no crc insertion",           "crc appended to frame",               0,  8, 0x10, 4, false },
	{ "prefetch bit in rbd",        "disabled (valid only in new modes)",  0,  0, 0x80, 7, false },
	{ "preamble length",            "bytes",                               7,  3, 0x30, 4, true },
	{ "preamble until crs",         "disabled",                            1, 11, 0x01, 0, false },
	{ "promiscuous mode",           "address filter on",                   0,  8, 0x01, 0, false },
	{ "padding",                    "no padding",                          0,  8, 0x80, 7, false },
	{ "resume rd",                  "do not reread next cb on resume (82596B stepping only)",
																		   0,  2, 0x02, 1, false },
	{ "slot time (lo)",             "bit times",                           0,  6, 0xff, 0, true },
	{ "slot time (hi)",             "bit times",                           2,  7, 0x07, 0, true },
	{ "save bad frame",             "discards bad frames",                 0,  2, 0x80, 7, false },
	{ "transmit on no crs",         "disabled",                            0,  8, 0x08, 3, false },
};

i82586_base_device::i82586_base_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, endianness_t endian, u8 datawidth, u8 addrwidth)
	: device_t(mconfig, type, tag, owner, clock),
	device_memory_interface(mconfig, *this),
	device_network_interface(mconfig, *this, 10.0f),
	m_space_config("shared", endian, datawidth, addrwidth),
	m_out_irq(*this),
	m_cx(false),
	m_fr(false),
	m_cna(false),
	m_rnr(false),
	m_irq_state(false),
	m_initialised(false),
	m_cu_state(CU_IDLE),
	m_ru_state(RU_IDLE),
	m_scp_address(SCP_ADDRESS),
	m_lb_length(0)
{}

i82586_device::i82586_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i82586_base_device(mconfig, I82586, tag, owner, clock, ENDIANNESS_LITTLE, 16, 24)
{}

i82596_device::i82596_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, endianness_t endian, u8 datawidth)
	: i82586_base_device(mconfig, type, tag, owner, clock, endian, datawidth, 32)
{}

i82596_le16_device::i82596_le16_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i82596_device(mconfig, I82596_LE16, tag, owner, clock, ENDIANNESS_LITTLE, 16)
{}

i82596_be16_device::i82596_be16_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i82596_device(mconfig, I82596_BE16, tag, owner, clock, ENDIANNESS_BIG, 16)
{}

i82596_le32_device::i82596_le32_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i82596_device(mconfig, I82596_LE32, tag, owner, clock, ENDIANNESS_LITTLE, 32)
{}

i82596_be32_device::i82596_be32_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i82596_device(mconfig, I82596_BE32, tag, owner, clock, ENDIANNESS_BIG, 32)
{}

// shared implementation
void i82586_base_device::device_start()
{
	m_space = &space(0);

	m_out_irq.resolve();

	m_cu_timer = timer_alloc(CU_TIMER);
	m_cu_timer->enable(false);
	m_ru_timer = timer_alloc(RU_TIMER);
	m_ru_timer->enable(false);

	save_item(NAME(m_cx));
	save_item(NAME(m_fr));
	save_item(NAME(m_cna));
	save_item(NAME(m_rnr));
	save_item(NAME(m_irq_state));
	save_item(NAME(m_initialised));

	save_item(NAME(m_cu_state));
	save_item(NAME(m_ru_state));

	save_item(NAME(m_scp_address));
	save_item(NAME(m_scb_base));
	save_item(NAME(m_scb_address));
	save_item(NAME(m_scb_cs));
	save_item(NAME(m_cba));
	save_item(NAME(m_rfd));

	save_item(NAME(m_mac_multi));

	save_item(NAME(m_lb_length));
	save_item(NAME(m_lb_buf));
}

void i82586_base_device::device_reset()
{
	m_cu_timer->enable(false);
	m_ru_timer->enable(false);

	m_cx = false;
	m_fr = false;
	m_cna = false;
	m_rnr = false;
	m_irq_state = false;
	m_initialised = false;

	m_cu_state = CU_IDLE;
	m_ru_state = RU_IDLE;

	m_scp_address = SCP_ADDRESS;
	m_lb_length = 0;
}

void i82586_base_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (id)
	{
		case CU_TIMER:
			cu_execute();
			update_scb();
			break;

		case RU_TIMER:
			if (m_lb_length)
			{
				LOG("device_timer injecting loopback frame length %d\n", m_lb_length);

				recv_cb(m_lb_buf, m_lb_length);
			}
			m_lb_length = 0;
			break;
	}
}

device_memory_interface::space_config_vector i82586_base_device::memory_space_config() const
{
	return space_config_vector {
		std::make_pair(0, &m_space_config)
	};
}

WRITE_LINE_MEMBER(i82586_base_device::ca)
{
	LOG("channel attention %s (%s)\n", state ? "asserted" : "deasserted", machine().describe_context());

	if (state)
	{
		// on first ca after reset, initialise
		if (!m_initialised)
			initialise();
		else
			process_scb();
	}
}

void i82586_base_device::recv_cb(u8 *buf, int length)
{
	switch (m_ru_state)
	{
	case RU_IDLE:
	case RU_SUSPENDED:
		// discard all frames
		break;

	case RU_READY:
		if (address_filter(buf))
		{
			LOG("recv_cb receiving frame length %d\n", length);
			dump_bytes(buf, length);

			ru_execute(buf, length);

			update_scb();
		}
		break;

	default:
		// no resources
		// TODO: accumulate statistics
		break;
	}
}

void i82586_base_device::process_scb()
{
	// fetch current command and status
	m_scb_cs = m_space->read_dword(m_scb_address);

	static const char *const CUC_NAME[] = { "NOP", "START", "RESUME", "SUSPEND", "ABORT", "THROTTLE_D", "THROTTLE_I", "reserved" };
	static const char *const RUC_NAME[] = { "NOP", "START", "RESUME", "SUSPEND", "ABORT", "reserved", "reserved", "reserved" };
	LOG("process_scb command/status 0x%08x (cuc %s, ruc %s%s)\n", m_scb_cs,
		CUC_NAME[(m_scb_cs & CUC) >> 24],
		RUC_NAME[(m_scb_cs & RUC) >> 20],
		m_scb_cs & RESET ? ", reset" : "");

	// clear interrupt flags when acknowledged
	if (m_scb_cs & ACK_CX)
		m_cx = false;
	if (m_scb_cs & ACK_FR)
		m_fr = false;
	if (m_scb_cs & ACK_CNA)
		m_cna = false;
	if (m_scb_cs & ACK_RNR)
		m_rnr = false;

	switch (m_scb_cs & CUC)
	{
	case CUC_NOP:
		break;

	case CUC_START:
		m_cba = address(m_scb_address, 4, 4);

		LOG("process_scb cuc start command block address 0x%08x\n", m_cba);

		m_cu_state = CU_ACTIVE;
		m_cu_timer->adjust(attotime::zero);
		break;

	case CUC_RESUME:
		m_cu_state = CU_ACTIVE;
		m_cu_timer->enable(true);
		break;

	case CUC_SUSPEND:
		m_cu_state = CU_SUSPENDED;
		m_cu_timer->enable(false);
		m_cna = true;
		break;

	case CUC_ABORT:
		m_cu_state = CU_IDLE;
		m_cu_timer->reset();
		m_cna = true;
		break;

	case CUC_THROTTLE_D:
	case CUC_THROTTLE_I:
		break;
	}

	switch (m_scb_cs & RUC)
	{
	case RUC_NOP:
		break;

	case RUC_START:
		m_rfd = address(m_scb_address, 6, 8);

		LOG("process_scb ruc start receive frame descriptor address 0x%08x\n", m_rfd);

		m_ru_state = RU_READY;
		break;

	case RUC_RESUME:
		m_ru_state = RU_READY;
		break;

	case RUC_SUSPEND:
		m_ru_state = RU_SUSPENDED;
		m_rnr = true;
		break;

	case RUC_ABORT:
		m_ru_state = RU_IDLE;
		m_rnr = true;
		break;
	}

	LOG("process_scb complete\n");
	update_scb();
}

void i82586_base_device::update_scb()
{
	// write the status word and clear the command word of the scb
	// TODO: T (throttle) status flag
	m_space->write_dword(m_scb_address,
		(m_cx ? CX : 0) |
		(m_fr ? FR : 0) |
		(m_cna ? CNA : 0) |
		(m_rnr ? RNR : 0) |
		(m_cu_state << 8) |
		(m_ru_state << 4));

	// update interrupt status
	set_irq(m_cx || m_fr || m_cna || m_rnr);
}

void i82586_base_device::cu_execute()
{
	// fetch the command block command/status
	u32 cb_cs = m_space->read_dword(m_cba);

	// set busy status
	m_space->write_dword(m_cba, cb_cs | CB_B);

	static const char *const CMD_NAME[] = { "NOP", "INDIVIDUAL ADDRESS SETUP", "CONFIGURE", "MULTICAST SETUP", "TRANSMIT", "TIME DOMAIN REFLECTOMETER", "DUMP", "DIAGNOSE" };
	LOG("cu_execute command 0x%08x (%s)\n", cb_cs, CMD_NAME[(cb_cs & CB_CMD) >> 16]);

	if (m_cu_state != CU_IDLE)
	{
		// execute command logic
		switch (cb_cs & CB_CMD)
		{
		case CB_NOP:
			cb_cs |= CB_OK;
			break;

		case CB_IASETUP:
			if (cu_iasetup())
				cb_cs |= CB_OK;
			break;

		case CB_CONFIGURE:
			if (cu_configure())
				cb_cs |= CB_OK;
			break;

		case CB_MCSETUP:
			if (cu_mcsetup())
				cb_cs |= CB_OK;
			break;

		case CB_TRANSMIT:
			// always turn on the heartbeat indicator status after a successful transmission; not
			// strictly correct, but allows one InterPro 2000 diagnostic to pass
			if (cu_transmit(cb_cs))
				cb_cs |= CB_OK | CB_S6;
			break;

		case CB_TDREFLECT:
			if (cu_tdreflect())
				cb_cs |= CB_OK;
			break;

		case CB_DUMP:
			if (cu_dump())
				cb_cs |= CB_OK;
			break;

		case CB_DIAGNOSE:
			cb_cs |= CB_OK;
			break;
		}
	}
	else
		// abort status
		cb_cs |= CB_A;

	// clear busy status and set completion status
	m_space->write_dword(m_cba, cb_cs | CB_C);

	// chain to next command
	if (!(cb_cs & CB_EL))
	{
		// check for suspend or abort
		if (m_cu_state == CU_ACTIVE)
		{
			// fetch link address
			m_cba = address(m_cba, 4, 4);

			// restart timer
			m_cu_timer->adjust(attotime::zero);
		}
	}
	else
	{
		// no more commands
		m_cu_state = CU_IDLE;
		m_cna = true;
	}

	// suspend on completion
	if (cb_cs & CB_S)
	{
		m_cu_state = CU_SUSPENDED;
		m_cu_timer->enable(false);
		m_cna = true;
	}

	static const char *const CU_STATE_NAME[] = { "IDLE", "SUSPENDED", "ACTIVE" };
	LOG("cu_execute complete state %s\n", CU_STATE_NAME[m_cu_state]);

	// set command executed status
	m_cx = (cb_cs & CB_I) && (cb_cs & CB_OK);
}

bool i82586_base_device::address_filter(u8 *mac)
{
	if (cfg_address_length() != 6)
	{
		LOG("address_filter error: address length %d not supported\n", cfg_address_length());

		return false;
	}

	LOGMASKED(LOG_FILTER, "address_filter testing destination address %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);

	if (cfg_promiscuous_mode())
	{
		LOG("address_filter accepted: promiscuous mode enabled\n");

		return true;
	}

	// ethernet broadcast
	if (!cfg_broadcast_disable() && !memcmp(mac, ETH_BROADCAST, cfg_address_length()))
	{
		LOGMASKED(LOG_FILTER, "address_filter accepted: broadcast\n");

		return true;
	}

	// individual address
	if (!memcmp(mac, get_mac(), cfg_address_length()))
	{
		LOGMASKED(LOG_FILTER, "address_filter accepted: individual address match\n");

		return true;
	}

	// ethernet multicast
	if ((mac[0] & 0x1) && m_mac_multi)
		if (m_mac_multi & address_hash(mac, cfg_address_length()))
		{
			LOGMASKED(LOG_FILTER, "address_filter accepted: multicast filter match\n");

			return true;
		}

	return false;
}

// shared helpers
void i82586_base_device::set_irq(bool irq)
{
	if (m_irq_state != irq)
	{
		m_irq_state = irq;
		m_out_irq(m_irq_state ? ASSERT_LINE : CLEAR_LINE);
	}
	else if (m_irq_state && irq)
	{
		m_out_irq(CLEAR_LINE);
		m_out_irq(ASSERT_LINE);
	}
}

u32 i82586_base_device::compute_crc(u8 *buf, int length, bool crc16)
{
	// TODO: crc16 (not used by Ethernet)
	return util::crc32_creator::simple(buf, length);
}

u64 i82586_base_device::address_hash(u8 *buf, int length)
{
	// address hash is computed using bits 2-7 from crc of address
	u32 crc = compute_crc(buf, length, false);

	return 1U << ((crc >> 2) & 0x3f);
}

int i82586_base_device::fetch_bytes(u8 *buf, u32 src, int length)
{
	int offset = 0;

	switch (m_space->data_width())
	{
	case 16:
		// handle misaligned start address
		if (src & 1)
		{
			buf[offset] = m_space->read_byte(src + offset);
			offset++;
		}

		// fetch aligned words from the source
		while (offset + 1 < length)
		{
			*(u16 *)&buf[offset] = m_space->read_word(src + offset);
			offset += 2;
		}

		// handle misaligned end address
		if ((src + length) & 1)
		{
			buf[offset] = m_space->read_byte(src + offset);
			offset++;
		}
		break;

	case 32:
		// handle misaligned start address
		switch (src & 3)
		{
		case 1:
			buf[offset] = m_space->read_byte(src + offset);
			offset++;

			*(u16 *)&buf[offset] = m_space->read_word(src + offset);
			offset += 2;
			break;

		case 2:
			*(u16 *)&buf[offset] = m_space->read_word(src + offset);
			offset += 2;
			break;

		case 3:
			buf[offset] = m_space->read_byte(src + offset);
			offset++;
			break;
		}

		// fetch aligned dwords from the source
		while (offset + 3 < length)
		{
			*(u32 *)&buf[offset] = m_space->read_dword(src + offset);
			offset += 4;
		}

		// handle misaligned end address
		switch ((src + length) & 3)
		{
		case 1:
			buf[offset] = m_space->read_byte(src + offset);
			offset++;
			break;

		case 2:
			*(u16 *)&buf[offset] = m_space->read_word(src + offset);
			offset += 2;
			break;

		case 3:
			*(u16 *)&buf[offset] = m_space->read_word(src + offset);
			offset += 2;
			buf[offset] = m_space->read_byte(src + offset);
			offset++;
			break;
		}
		break;
	}

	return offset;
}

int i82586_base_device::store_bytes(u32 dst, u8 *buf, int length)
{
	int offset = 0;

	switch (m_space->data_width())
	{
	case 16:
		// handle misaligned start address
		if (dst & 1)
		{
			m_space->write_byte(dst + offset, buf[offset]);
			offset++;
		}

		// store aligned words to the destination
		while (offset + 1 < length)
		{
			m_space->write_word(dst + offset, *(u16 *)&buf[offset]);
			offset += 2;
		}

		// handle misaligned end address
		if ((dst + length) & 1)
		{
			m_space->write_byte(dst + offset, buf[offset]);
			offset++;
		}
		break;

	case 32:
		// handle misaligned start address
		switch (dst & 3)
		{
		case 1:
			m_space->write_byte(dst + offset, buf[offset]);
			offset++;
			m_space->write_word(dst + offset, *(u16 *)&buf[offset]);
			offset += 2;
			break;

		case 2:
			m_space->write_word(dst + offset, *(u16 *)&buf[offset]);
			offset += 2;
			break;

		case 3:
			m_space->write_byte(dst + offset, buf[offset]);
			offset++;
			break;
		}

		// store aligned dwords to the destination
		while (offset + 3 < length)
		{
			m_space->write_dword(dst + offset, *(u32 *)&buf[offset]);
			offset += 4;
		}

		// handle misaligned end address
		switch ((dst + length) & 3)
		{
		case 1:
			m_space->write_byte(dst + offset, buf[offset]);
			offset++;
			break;

		case 2:
			m_space->write_word(dst + offset, *(u16 *)&buf[offset]);
			offset += 2;
			break;

		case 3:
			m_space->write_word(dst + offset, *(u16 *)&buf[offset]);
			offset += 2;
			m_space->write_byte(dst + offset, buf[offset]);
			offset++;
			break;
		}
		break;
	}

	return offset;
}

void i82586_base_device::dump_bytes(u8 *buf, int length)
{
	if (VERBOSE & LOG_FRAMES)
	{
		// pad frame with zeros to 8-byte boundary
		for (int i = 0; i < 8 - (length % 8); i++)
			buf[length + i] = 0;

		// dump length / 8 (rounded up) groups of 8 bytes
		for (int i = 0; i < (length + 7) / 8; i++)
			LOGMASKED(LOG_FRAMES, "%02x %02x %02x %02x %02x %02x %02x %02x\n",
				buf[i * 8 + 0], buf[i * 8 + 1], buf[i * 8 + 2], buf[i * 8 + 3],
				buf[i * 8 + 4], buf[i * 8 + 5], buf[i * 8 + 6], buf[i * 8 + 7]);
	}
}

// 82586 implementation
void i82586_device::device_start()
{
	i82586_base_device::device_start();

	save_item(NAME(m_cfg_bytes));
}

void i82586_device::device_reset()
{
	i82586_base_device::device_reset();

	// configure parameter defaults
	memcpy(m_cfg_bytes, CFG_DEFAULTS, CFG_SIZE);
}

void i82586_device::initialise()
{
	// read iscp address from scp
	u32 iscp_address = m_space->read_dword(m_scp_address + 8);
	LOG("initialise iscp address 0x%08x\n", iscp_address);

	u16 scb_offset = m_space->read_word(iscp_address + 2);

	m_scb_base = m_space->read_dword(iscp_address + 4);
	m_scb_address = m_scb_base + scb_offset;
	LOG("initialise scb base address 0x%06x offset 0x%04x address 0x%08x\n", m_scb_base, scb_offset, m_scb_address);

	// clear iscp busy byte
	m_space->write_byte(iscp_address, 0);

	m_cx = true;
	m_cna = true;

	m_initialised = true;
	LOG("initialise complete\n");

	// update scb
	update_scb();
}

bool i82586_device::cu_iasetup()
{
	int len = cfg_address_length();
	char mac[6];
	u32 data;

	if (len != 6)
	{
		LOG("cu_iasetup unexpected individual address length %d != 6\n", len);

		return false;
	}

	data = m_space->read_dword(m_cba + 4);
	mac[0] = (data >> 16) & 0xff;
	mac[1] = (data >> 24) & 0xff;

	data = m_space->read_dword(m_cba + 8);
	mac[2] = (data >> 0) & 0xff;
	mac[3] = (data >> 8) & 0xff;
	mac[4] = (data >> 16) & 0xff;
	mac[5] = (data >> 24) & 0xff;

	LOG("cu_iasetup individual address %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
	set_mac(mac);

	return true;
}

bool i82586_device::cu_configure()
{
	int count;

	// first two bytes
	u16 data = m_space->read_word(m_cba + 6);
	cfg_set(0, (data >> 0) & 0xff);
	cfg_set(1, (data >> 8) & 0xff);

	// extract byte count (4 <= count <= 12)
	count = cfg_get(0) & 0xf;
	count = count < 4 ? 4 : (count > CFG_SIZE ? CFG_SIZE : count);

	// read remaining bytes one word at a time
	for (int i = 2; i < count; i++)
	{
		if ((i & 1) == 0)
		{
			data = m_space->read_word(m_cba + 6 + i);
			cfg_set(i, (data >> 0) & 0xff);
		}
		else
			cfg_set(i, (data >> 8) & 0xff);
	}

	if (VERBOSE & LOG_CONFIG)
	{
		LOGMASKED(LOG_CONFIG, "%-30s %3s %3s %3s %s\n", "parameter", "def", "cur", "chg", "default value interpretation");
		for (auto param : CFG_PARAMS)
		{
			if (param.byte < (CFG_SIZE - 1))
			{
				u8 value = (m_cfg_bytes[param.byte] & param.mask) >> param.shift;

				LOGMASKED(LOG_CONFIG, "%-30s %3d %3d  %c  %s%s\n",
					param.name, param.dflt, value, value == param.dflt ? ' ' : '*', param.unit,
					param.ieee8023 ? (value == param.dflt ? "" : " (current value not 802.3 compatible)") : "");
			}
		}
	}

	return true;
}

bool i82586_device::cu_mcsetup()
{
	int addr_len = cfg_address_length();
	u16 mc_count;
	u8 data[6];

	if (addr_len != 6)
	{
		LOG("cu_mcsetup unexpected address length %d != 6\n", addr_len);
		return false;
	}

	// read the address count
	mc_count = m_space->read_word(m_cba + 6, TB_COUNT);

	// reset current list
	LOG("mc_setup configuring %d addresses\n", mc_count);
	m_mac_multi = 0;

	// read and process the addresses
	for (int i = 0; i < mc_count; i++)
	{
		*(u16 *)&data[0] = m_space->read_word(m_cba + 8 + i * 6 + 0);
		*(u16 *)&data[1] = m_space->read_word(m_cba + 8 + i * 6 + 2);
		*(u16 *)&data[2] = m_space->read_word(m_cba + 8 + i * 6 + 4);

		// add a hash of this address to the table
		m_mac_multi |= address_hash(data, cfg_address_length());

		LOG("mc_setup inserting address %02x:%02x:%02x:%02x:%02x:%02x\n",
			data[0], data[1], data[2], data[3], data[4], data[5]);
	}

	return true;
}

bool i82586_device::cu_transmit(u32 command)
{
	u16 tbd_count;

	// ethernet frame buffer
	u8 buf[MAX_FRAME_SIZE];
	u16 length = 0;

	u16 tbd_offset = m_space->read_word(m_cba + 6);

	// optionally insert source, destination address and length (14 bytes)
	if (!cfg_no_src_add_ins())
	{
		const char *mac = get_mac();
		u32 data;

		// insert destination address (6 bytes)
		data = m_space->read_dword(m_cba + 8);
		buf[length++] = (data >> 0) & 0xff;
		buf[length++] = (data >> 8) & 0xff;
		buf[length++] = (data >> 16) & 0xff;
		buf[length++] = (data >> 24) & 0xff;

		data = m_space->read_dword(m_cba + 12);
		buf[length++] = (data >> 0) & 0xff;
		buf[length++] = (data >> 8) & 0xff;

		// insert source address (6 bytes)
		LOG("cu_transmit inserting source address %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
		for (int i = 0; i < 6; i++)
			buf[length++] = mac[i];

		// insert length (2 bytes)
		LOG("cu_transmit frame length 0x%04x\n", ((data >> 24) & 0xff) | ((data >> 16) & 0xff00));
		buf[length++] = (data >> 16) & 0xff;
		buf[length++] = (data >> 24) & 0xff;
	}

	// check if there is no tbd
	tbd_count = (tbd_offset == TBD_EMPTY) ? TB_EOF : 0;

	// insert payload from tbd
	while (!(tbd_count & TB_EOF))
	{
		u32 tbd_address = m_scb_base + tbd_offset;
		u32 tb_address, data;

		// get the size and address of this buffer, and address of the next descriptor
		data = m_space->read_dword(tbd_address + 0);
		tbd_count = (data >> 0) & 0xffff;
		tbd_offset = (data >> 16) & 0xffff;

		tb_address = m_space->read_dword(tbd_address + 4);

		LOG("cu_transmit inserting %d bytes from transmit buffer address 0x%08x\n", tbd_count & TB_COUNT, tb_address);
		length += fetch_bytes(&buf[length], tb_address, tbd_count & TB_COUNT);
	}

#if I82586_FCS
	// optionally compute/insert ethernet frame check sequence (4 bytes)
	if (!cfg_no_crc_insertion())
	{
		LOG("cu_transmit inserting frame check sequence\n");

		u32 crc = compute_crc(buf, length, cfg_crc16());

		// insert the fcs
		buf[length++] = (crc >> 0) & 0xff;
		buf[length++] = (crc >> 8) & 0xff;
		buf[length++] = (crc >> 16) & 0xff;
		buf[length++] = (crc >> 24) & 0xff;
	}
#endif

	if (cfg_loopback_mode() != LOOPBACK_NONE)
	{
		LOG("cu_transmit loopback frame length %d\n", length);

		if (m_lb_length == 0)
		{
			memcpy(m_lb_buf, buf, length);
			m_lb_length = length;

			m_ru_timer->adjust(attotime::zero);
		}
		else
			LOG("cu_tranmit error: loopback buffer not empty\n");

		return true;
	}
	else
	{
		LOG("cu_transmit sending frame length %d\n", length);
		dump_bytes(buf, length);

		return send(buf, length) == 0;
	}
}

bool i82586_device::cu_tdreflect()
{
	m_space->write_word(m_cba + 6, TDR_LNK_OK | TDR_TIME);

	return true;
}

bool i82586_device::cu_dump()
{
	int length = DUMP_SIZE;
	u8 buf[DUMP_SIZE];
	u32 dump_address;

	// clear dump buffer
	memset(buf, 0, length);

	// populate dump buffer
	// configure bytes
	memcpy(&buf[0x00], &m_cfg_bytes[0], CFG_SIZE);

	// individual address
	memcpy(&buf[0x0c], get_mac(), 6);

	// hash register
	*(u64 *)&buf[0x24] = m_mac_multi;

	// store dump buffer
	dump_address = m_scb_base + m_space->read_word(m_cba + 6);

	LOG("cu_dump storing %d bytes address 0x%08x\n", length, dump_address);
	store_bytes(dump_address, buf, length);

	return true;
}

bool i82586_device::address_filter(u8 *mac)
{
	if (i82586_base_device::address_filter(mac))
		return true;

	LOGMASKED(LOG_FILTER, "address_filter rejected\n");

	return false;
}

void i82586_device::ru_execute(u8 *buf, int length)
{
	// fetch receive frame descriptor command/status
	u32 rfd_cs = m_space->read_dword(m_rfd);

	// current buffer position and bytes remaining
	int position = 0, remaining = length;

	// set busy status
	m_space->write_dword(m_rfd, rfd_cs | RFD_B);

	LOG("ru_execute receiving %d bytes into rfd 0x%08x\n", length, m_rfd);

	// set short frame status
	if (length < cfg_min_frame_length())
		rfd_cs |= RFD_S_SHORT;

#if I82586_FCS
	// set crc status
	if (~compute_crc(buf, length, cfg_crc16()) != FCS_RESIDUE)
	{
		LOGMASKED(LOG_FRAMES, "ru_execute crc error computed 0x%08x stored 0x%08x\n",
			compute_crc(buf, length - 4, cfg_crc16()), *(u32 *)&buf[length - 4]);

		// increment crc error count
		m_space->write_word(m_scb_address + 8, m_space->read_word(m_scb_address + 8) + 1);

		rfd_cs |= RFD_S_CRC;
	}
#endif

	// TODO: alignment error (crc in misaligned frame), status bit 10
	// TODO: increment alignment error counter

	// fetch initial rbd offset from rfd
	u16 rbd_offset = m_space->read_word(m_rfd + 6);

	if (!cfg_no_src_add_ins())
	{
		// compute stored length (from 2 * addresses + length field)
		int actual = cfg_address_length() * 2 + 2;

		LOG("ru_execute storing %d bytes into rfd\n", actual);

		// store data in rfd
		store_bytes(m_rfd + 8, buf, actual);
		position += actual;
		remaining -= actual;
	}

	// store remaining bytes in receive buffers
	while (remaining && rbd_offset != RBD_EMPTY)
	{
		// fetch the count and address for this buffer
		u32 rb_address = m_space->read_dword(m_scb_base + rbd_offset + 4);
		u16 rbd_size = m_space->read_word(m_scb_base + rbd_offset + 8);

		// compute number of bytes to store in buffer
		int actual = remaining > (rbd_size & RB_SIZE) ? (rbd_size & RB_SIZE) : remaining;

		LOG("ru_execute storing %d bytes into receive buffer 0x%08x size %d\n", actual, rb_address, rbd_size & RB_SIZE);

		// store data in buffer
		store_bytes(rb_address, &buf[position], actual);
		position += actual;
		remaining -= actual;

		// store actual count
		m_space->write_word(m_scb_base + rbd_offset + 0, actual | RB_F | (remaining ? 0 : RB_EOF));

		// check if buffers exhausted
		if ((rbd_size & RB_EL))
		{
			rbd_offset = RBD_EMPTY;

			if (remaining)
			{
				// set buffers exhausted status
				rfd_cs |= RFD_S_BUFFER;

				m_ru_state = RU_NR;
				m_rnr = true;
			}
		}
		else
			// fetch next rbd offset
			rbd_offset = m_space->read_word(m_scb_base + rbd_offset + 2);
	}

	if (remaining == 0 || cfg_save_bad_frames())
		// set frame received status
		rfd_cs |= RFD_C;

	// frame received without errors
	if (!(rfd_cs & RFD_ERROR_82586))
	{
		LOG("ru_execute frame received without error\n");

		rfd_cs |= RFD_OK;
	}
	else
		LOG("ru_execute frame received with errors status 0x%04x\n", rfd_cs);

	// store status
	m_space->write_dword(m_rfd, rfd_cs);

	// if we received without error, or we're saving bad frames, advance to the next rfd
	if ((rfd_cs & RFD_OK) || cfg_save_bad_frames())
	{
		if (!(rfd_cs & RFD_EL))
		{
			// advance to next rfd
			m_rfd = m_scb_base + m_space->read_word(m_rfd + 4);

			// store next free rbd address into rfd
			if (rbd_offset != RBD_EMPTY)
				m_space->write_word(m_rfd + 6, rbd_offset);
		}
		else
		{
			m_ru_state = RU_NR;
			m_rnr = true;
		}

		// set frame received status
		m_fr = true;
	}

	// suspend on completion
	if (rfd_cs & RFD_S)
	{
		m_ru_state = RU_SUSPENDED;
		m_rnr = true;
	}

	static const char *const RU_STATE_NAME[] = { "IDLE", "SUSPENDED", "NO RESOURCES", nullptr, "READY" };
	LOG("ru_execute complete state %s\n", RU_STATE_NAME[m_ru_state]);
}

u32 i82586_device::address(u32 base, int offset, int address, u16 empty)
{
	u16 data = m_space->read_word(base + offset);

	return (data == empty) ? empty : m_scb_base + data;
}

// 82596 implementation
void i82596_device::device_start()
{
	i82586_base_device::device_start();

	save_item(NAME(m_cfg_bytes));

	save_item(NAME(m_sysbus));

	save_item(NAME(m_mac_multi_ia));
}

void i82596_device::device_reset()
{
	i82586_base_device::device_reset();

	// configure parameter defaults
	memcpy(m_cfg_bytes, CFG_DEFAULTS, CFG_SIZE);
}

void i82596_device::port(u32 data)
{
	switch (data & 0xf)
	{
	case 0:
		// execute a software reset
		LOG("port reset\n");
		reset();
		break;

	case 1:
		// execute a self-test
		LOG("port self-test\n");
		break;

	case 2:
		// write an alterantive system configuration pointer address
		if (!m_initialised)
		{
			m_scp_address = data & ~0xf;
			LOG("port scp address 0x%08x\n", data);
		}
		break;

	case 3:
		// write an alternative dump area pointer and perform dump
		LOG("port dump\n");
		break;
	}
}

void i82596_device::initialise()
{
	// read iscp address and sysbus from scp
	u32 iscp_address = m_space->read_dword(m_scp_address + 8);
	m_sysbus = m_space->read_byte(m_scp_address + 2);

	LOG("initialise sysbus 0x%02x mode %s, %s triggering of bus throttle timers, lock function %s, interrupt active %s, 32-bit address pointers in linear mode per %s stepping)\n",
		m_sysbus,
		mode() == MODE_82586 ? "82586" : (mode() == MODE_32SEGMENTED ? "32-bit segmented mode" : (mode() == MODE_LINEAR ? "linear" : "reserved")),
		m_sysbus & SYSBUS_TRG ? "external" : "internal",
		m_sysbus & SYSBUS_LOCK ? "disabled" : "enabled",
		m_sysbus & SYSBUS_INT ? "low" : "high",
		m_sysbus & SYSBUS_BE ? "B" : "A1");
	LOG("initialise iscp address 0x%08x\n", iscp_address);

	switch (mode())
	{
	case MODE_82586:
	case MODE_32SEGMENTED:
	{
		u16 scb_offset = m_space->read_word(iscp_address + 2);

		m_scb_base = m_space->read_dword(iscp_address + 4);
		m_scb_address = m_scb_base + scb_offset;
		LOG("initialise scb base address 0x%08x offset 0x%04x address 0x%08x\n", m_scb_base, scb_offset, m_scb_address);
	}
		break;

	case MODE_LINEAR:
		m_scb_address = m_space->read_dword(iscp_address + 4);
		LOG("initialise scb address 0x%08x\n", m_scb_address);
		break;
	}

	// clear iscp busy byte
	m_space->write_byte(iscp_address, 0);

	m_cx = true;
	m_cna = true;

	m_initialised = true;
	LOG("initialise complete\n");

	// update scb
	update_scb();
}

bool i82596_device::cu_iasetup()
{
	int len = cfg_address_length();
	u32 data;
	char mac[6];

	if (len != 6)
	{
		LOG("cu_iasetup unexpected individual address length %d != 6\n", len);

		return false;
	}

	switch (mode())
	{
	case MODE_82586:
	case MODE_32SEGMENTED:
		data = m_space->read_dword(m_cba + 4);
		mac[0] = (data >> 16) & 0xff;
		mac[1] = (data >> 24) & 0xff;

		data = m_space->read_dword(m_cba + 8);
		mac[2] = (data >> 0) & 0xff;
		mac[3] = (data >> 8) & 0xff;
		mac[4] = (data >> 16) & 0xff;
		mac[5] = (data >> 24) & 0xff;
		break;

	case MODE_LINEAR:
		data = m_space->read_dword(m_cba + 8);
		mac[0] = (data >> 0) & 0xff;
		mac[1] = (data >> 8) & 0xff;
		mac[2] = (data >> 16) & 0xff;
		mac[3] = (data >> 24) & 0xff;

		data = m_space->read_dword(m_cba + 12);
		mac[4] = (data >> 0) & 0xff;
		mac[5] = (data >> 8) & 0xff;
		break;
	}

	LOG("cu_iasetup individual address %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
	set_mac(mac);

	return true;
}

bool i82596_device::cu_configure()
{
	u32 data32 = 0;
	u16 data16;
	int count;

	switch (mode())
	{
	case MODE_82586:
		// first two bytes are word aligned
		data16 = m_space->read_word(m_cba + 6);

		cfg_set(0, (data16 >> 0) & 0xff);
		cfg_set(1, (data16 >> 8) & 0xff);

		// extract byte count (datasheet does not state minimum count)
		count = cfg_get(0) & 0xf;
		count = count < 4 ? 4 : (count > i82586_device::CFG_SIZE ? i82586_device::CFG_SIZE : count);

		// read remaining bytes one dword at a time
		for (int i = 2; i < count; i++)
		{
			switch (i & 3)
			{
			case 2:
				data32 = m_space->read_dword(m_cba + 6 + i);
				cfg_set(i, (data32 >> 0) & 0xff);
				break;
			case 3: cfg_set(i, (data32 >> 8) & 0xff); break;
			case 0: cfg_set(i, (data32 >> 16) & 0xff); break;
			case 1: cfg_set(i, (data32 >> 24) & 0xff); break;
			}
		}
	break;

	case MODE_32SEGMENTED:
		// first two bytes are word aligned
		data16 = m_space->read_word(m_cba + 6);

		cfg_set(0, (data16 >> 0) & 0xff);
		cfg_set(1, (data16 >> 8) & 0xff);

		// extract byte count (datasheet does not state minimum count)
		count = cfg_get(0) & 0xf;
		count = count < 4 ? 4 : (count > CFG_SIZE ? CFG_SIZE : count);

		// read remaining bytes one dword at a time
		for (int i = 2; i < count; i++)
		{
			switch (i & 3)
			{
			case 2:
				data32 = m_space->read_dword(m_cba + 6 + i);
				cfg_set(i, (data32 >> 0) & 0xff);
				break;
			case 3: cfg_set(i, (data32 >> 8) & 0xff); break;
			case 0: cfg_set(i, (data32 >> 16) & 0xff); break;
			case 1: cfg_set(i, (data32 >> 24) & 0xff); break;
			}
		}
		break;

	case MODE_LINEAR:
		// bytes are all dword aligned
		data32 = m_space->read_dword(m_cba + 8);

		cfg_set(0, (data32 >> 0) & 0xff);
		cfg_set(1, (data32 >> 8) & 0xff);
		cfg_set(2, (data32 >> 16) & 0xff);
		cfg_set(3, (data32 >> 24) & 0xff);

		// extract byte count (datasheet does not state minimum count)
		count = cfg_get(0) & 0xf;
		count = count < 4 ? 4 : (count > CFG_SIZE ? CFG_SIZE : count);

		// read remaining bytes one dword at a time
		for (int i = 4; i < count; i++)
		{
			switch (i & 3)
			{
			case 0:
				data32 = m_space->read_dword(m_cba + 8 + i);
				cfg_set(i, (data32 >> 0) & 0xff);
				break;

			case 1: cfg_set(i, (data32 >> 8) & 0xff); break;
			case 2: cfg_set(i, (data32 >> 16) & 0xff); break;
			case 3: cfg_set(i, (data32 >> 24) & 0xff); break;
			}
		}
		break;
	}

	if (VERBOSE & LOG_CONFIG)
	{
		LOGMASKED(LOG_CONFIG, "%-30s %3s %3s %3s %s\n", "parameter", "def", "cur", "chg", "default value interpretation");
		for (auto param : CFG_PARAMS)
		{
			u8 value = (m_cfg_bytes[param.byte] & param.mask) >> param.shift;

			LOGMASKED(LOG_CONFIG, "%-30s %3d %3d  %c  %s%s\n",
				param.name, param.dflt, value, value == param.dflt ? ' ' : '*', param.unit,
				param.ieee8023 ? (value == param.dflt ? "" : " (current value not 802.3 compatible)") : "");
		}
	}

	return true;
}

bool i82596_device::cu_mcsetup()
{
	int addr_len = cfg_address_length();
	u16 mc_count = 0;

	int offset = 0;
	u8 data[20];
	bool multi_ia;

	if (addr_len != 6)
	{
		LOG("cu_mcsetup unexpected address length %d != 6\n", addr_len);
		return false;
	}

	switch (mode())
	{
	case MODE_82586:
	case MODE_32SEGMENTED:
		mc_count = m_space->read_word(m_cba + 6, TB_COUNT);
		break;

	case MODE_LINEAR:
		mc_count = m_space->read_word(m_cba + 8, TB_COUNT);
		offset = 2;
		break;
	}

	// if count is zero, release multicast list and finish
	if (mc_count == 0)
	{
		LOG("mc_setup multicast filter disabled\n");
		m_mac_multi = 0;

		return true;
	}

	// fetch the first word
	*(u32 *)&data[0] = m_space->read_dword(m_cba + 8);

	// multi ia when configured and lsb of first address is clear
	multi_ia = cfg_multi_ia() && !BIT(data[offset], 0);

	// clear existing list
	LOG("mc_setup configuring %d %s addresses\n", mc_count, multi_ia ? "multi-ia" : "multicast");
	(multi_ia ? m_mac_multi_ia : m_mac_multi) = 0;

	for (int i = 0; i < mc_count; i++)
	{
		// compute offset of address in 18 byte buffer
		int n = (i % 3) * 6;

		// read the next dword
		*(u32 *)&data[n + 6] = m_space->read_dword(m_cba + 8 + i * 4 + 4);

		// unaligned case needs special handling
		if (n == 12 && offset == 2)
			*(u16 *)&data[18] = *(u16 *)&data[0];

		// add a hash of this address to the table
		(multi_ia ? m_mac_multi_ia : m_mac_multi) |= address_hash(&data[n + offset], cfg_address_length());

		LOG("mc_setup inserting address %02x:%02x:%02x:%02x:%02x:%02x\n",
			data[n + offset + 0], data[n + offset + 1], data[n + offset + 2], data[n + offset + 3], data[n + offset + 4], data[n + offset + 5]);
	}

	return true;
}

bool i82596_device::cu_transmit(u32 command)
{
	u32 tbd_address;
	u16 tcb_count, tbd_count;

	// ethernet frame buffer
	u8 buf[MAX_FRAME_SIZE];
	u16 length = 0;

	// need offset into tcb for linear mode
	int offset = mode() == MODE_LINEAR ? 4 : 0;

	// fetch tbd address
	if (mode() != MODE_LINEAR)
	{
		u16 tbd_offset = m_space->read_word(m_cba + 6);

		tbd_address = (tbd_offset == TBD_EMPTY) ? tbd_offset : m_scb_base + tbd_offset;
	}
	else
		tbd_address = m_space->read_dword(m_cba + 8);

	// fetch the tcb count
	tcb_count = (mode() == MODE_82586) ? 0 : m_space->read_word(m_cba + 8 + offset);

	LOG("cu_transmit %s mode, crc insertion %s, tcb count %d, %s tbd\n",
		command & CB_SF ? "flexible" : "simplified", command & CB_NC ? "disabled" : "enabled", tcb_count & TB_COUNT, (tbd_address == TBD_EMPTY) ? "no" : "valid");

	if ((command & CB_SF) && !(tcb_count & TB_EOF))
		LOG("cu_transmit error: tcb eof not set in simplified mode\n");

	// insert payload from tcb when in simplified mode, or when flexible mode and tcb_count > 0
	if ((command & CB_SF) || (!(command & CB_SF) && (tcb_count & TB_COUNT)))
	{
		// optionally insert destination, source and length (14 bytes)
		if (!cfg_no_src_add_ins())
		{
			const char *mac = get_mac();
			u32 data;

			// insert destination address (6 bytes)
			data = m_space->read_dword(m_cba + 12 + offset);
			buf[length++] = (data >> 0) & 0xff;
			buf[length++] = (data >> 8) & 0xff;
			buf[length++] = (data >> 16) & 0xff;
			buf[length++] = (data >> 24) & 0xff;

			data = m_space->read_dword(m_cba + 16 + offset);
			buf[length++] = (data >> 0) & 0xff;
			buf[length++] = (data >> 8) & 0xff;

			// insert source address (6 bytes)
			LOG("cu_transmit inserting source address %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
			for (int i = 0; i < 6; i++)
				buf[length++] = mac[i];

			// insert length from tcb (2 bytes)
			LOG("cu_transmit frame length 0x%04x\n", ((data >> 24) & 0xff) | ((data >> 16) & 0xff00));
			buf[length++] = (data >> 16) & 0xff;
			buf[length++] = (data >> 24) & 0xff;

			// insert payload from tcb
			LOG("cu_transmit inserting %d bytes from transmit command block\n", (tcb_count & TB_COUNT) - 8);
			length += fetch_bytes(&buf[length], m_cba + 20 + offset, (tcb_count & TB_COUNT) - 8);
		}
		else
		{
			// insert entire payload from tcb
			LOG("cu_transmit inserting %d bytes from transmit command block\n", tcb_count & TB_COUNT);
			length += fetch_bytes(&buf[length], m_cba + 12 + offset, tcb_count & TB_COUNT);
		}
	}
	else if (!cfg_no_src_add_ins())
		LOG("cu_transmit error: don't know how to insert source address in flexible mode without tcb payload\n");

	// check for no tbd
	tbd_count = ((tcb_count & TB_EOF) || (tbd_address == TBD_EMPTY)) ? TB_EOF : 0;

	// insert payload from tbd
	while (!(tbd_count & TB_EOF))
	{
		u32 data, tb_address = 0;

		// fetch the count and address for this buffer, and address of the next descriptor
		switch (mode())
		{
		case MODE_82586:
		case MODE_32SEGMENTED:
			data = m_space->read_dword(tbd_address + 0);
			tbd_count = data;
			tb_address = m_space->read_dword(tbd_address + 4);

			tbd_address = m_scb_base + (data >> 16);
			break;

		case MODE_LINEAR:
			tbd_count = m_space->read_word(tbd_address + 0);
			tb_address = m_space->read_dword(tbd_address + 8);

			tbd_address = m_space->read_dword(tbd_address + 4);
			break;
		}

		// fetch and insert the buffer bytes into our transmit buffer
		LOG("cu_transmit inserting %d bytes from transmit buffer address 0x%08x\n", tbd_count & TB_COUNT, tb_address);
		length += fetch_bytes(&buf[length], tb_address, tbd_count & TB_COUNT);
	}

#if I82586_FCS
	// optionally compute/insert ethernet frame check sequence (4 bytes)
	if (!cfg_no_crc_insertion() && !(command & CB_NC))
	{
		LOG("cu_transmit inserting frame check sequence\n");

		u32 crc = compute_crc(buf, length, cfg_crc16());

		// append the fcs
		buf[length++] = (crc >> 0) & 0xff;
		buf[length++] = (crc >> 8) & 0xff;
		buf[length++] = (crc >> 16) & 0xff;
		buf[length++] = (crc >> 24) & 0xff;
	}
#endif

	if (cfg_loopback_mode() != LOOPBACK_NONE)
	{
		LOG("cu_transmit loopback frame length %d\n", length);

		if (m_lb_length == 0)
		{
			dump_bytes(buf, length);

			memcpy(m_lb_buf, buf, length);
			m_lb_length = length;

			m_ru_timer->adjust(attotime::zero);
		}
		else
			LOG("cu_tranmit error: loopback buffer not empty\n");

		return true;
	}
	else
	{
		LOG("cu_transmit sending frame length %d\n", length);
		dump_bytes(buf, length);

		return send(buf, length) == 0;
	}
}

bool i82596_device::cu_tdreflect()
{
	switch (mode())
	{
	case MODE_82586:
	case MODE_32SEGMENTED:
		m_space->write_word(m_cba + 6, TDR_LNK_OK | TDR_TIME);
		break;

	case MODE_LINEAR:
		m_space->write_word(m_cba + 8, TDR_LNK_OK | TDR_TIME);
		break;
	}

	return true;
}

bool i82596_device::cu_dump()
{
	int length = mode() == MODE_82586 ? i82586_device::DUMP_SIZE : DUMP_SIZE;
	u8 buf[DUMP_SIZE];
	u32 dump_address;

	// clear dump buffer
	memset(buf, 0, length);

	// populate dump buffer
	if (mode() == MODE_82586)
	{
		// configure bytes 2-10
		memcpy(&buf[0x02], &m_cfg_bytes[2], 9);

		// individual address
		memcpy(&buf[0x0c], get_mac(), 6);

		// hash register
		*(u64 *)&buf[0x24] = m_mac_multi;
	}
	else
	{
		// configure bytes 2-13
		memcpy(&buf[0x00], &m_cfg_bytes[2], 12);

		// individual address
		memcpy(&buf[0x0e], get_mac(), 6);

		// hash register
		*(u64 *)&buf[0x26] = m_mac_multi;
	}

	// store dump buffer
	dump_address = address(m_cba, 6, 8);
	LOG("cu_dump storing %d bytes address 0x%08x\n", length, dump_address);
	store_bytes(dump_address, buf, length);

	return true;
}

bool i82596_device::address_filter(u8 *mac)
{
	if (i82586_base_device::address_filter(mac))
		return true;

	// check for accept all multicast
	if ((mac[0] & 0x1) && !cfg_mc_all())
	{
		LOGMASKED(LOG_FILTER, "address_filter accepted: multicast and configured to accept all multicast\n");

		return true;
	}

	// not ethernet multicast, check multi-ia
	if (!(mac[0] & 0x1) && cfg_multi_ia() && m_mac_multi_ia)
	{
		if (m_mac_multi_ia & address_hash(mac, cfg_address_length()))
		{
			LOGMASKED(LOG_FILTER, "address_filter accepted: multi-ia filter match");

			return true;
		}
	}

	LOGMASKED(LOG_FILTER, "address_filter rejected\n");

	return false;
}

void i82596_device::ru_execute(u8 *buf, int length)
{
	// fetch receive frame descriptor command/status
	u32 rfd_cs = m_space->read_dword(m_rfd);

	// offset into rfd/rbd for linear mode
	int linear_offset = mode() == MODE_LINEAR ? 4 : 0;

	if (!cfg_crc_in_memory())
	{
		// compute and append fcs
		u32 crc = compute_crc(buf, length, false);

		// append the fcs
		buf[length++] = (crc >> 0) & 0xff;
		buf[length++] = (crc >> 8) & 0xff;
		buf[length++] = (crc >> 16) & 0xff;
		buf[length++] = (crc >> 24) & 0xff;
	}

	// current buffer position and bytes remaining
	int position = 0, remaining = length;

	// set busy status
	m_space->write_dword(m_rfd, rfd_cs | RFD_B);

	LOG("ru_execute receiving %d bytes using %s mode into rfd 0x%08x\n", length, (mode() == MODE_82586 ? "82586" : ((rfd_cs & RFD_SF) ? "flexible" : "simplified")), m_rfd);

	// TODO: check length if configured, status bit 12

	// set short frame status
	if (length < cfg_min_frame_length())
	{
		LOGMASKED(LOG_FRAMES, "ru_execute frame length %d less than minimum %d\n", length, cfg_min_frame_length());

		// increment short frame count
		if (mode() != MODE_82586)
			m_space->write_dword(m_scb_address + 28 + linear_offset, m_space->read_dword(m_scb_address + 28 + linear_offset) + 1);

		rfd_cs |= RFD_S_SHORT;
	}

#if I82586_FCS
	// set crc status
	if (~compute_crc(buf, length, cfg_crc16()) != FCS_RESIDUE)
	{
		LOGMASKED(LOG_FRAMES, "ru_execute crc error computed 0x%08x stored 0x%08x\n",
			compute_crc(buf, length - 4, cfg_crc16()), *(u32 *)&buf[length - 4]);

		// increment crc error count
		if (mode() == MODE_82586)
			m_space->write_word(m_scb_address + 8, m_space->read_word(m_scb_address + 8) + 1);
		else
			m_space->write_dword(m_scb_address + 8 + linear_offset, m_space->read_dword(m_scb_address + 8 + linear_offset) + 1);

		rfd_cs |= RFD_S_CRC;
	}
#endif

	// TODO: alignment error (crc in misaligned frame), status bit 10
	// TODO: increment alignment error counter

	// set multicast status
	if (mode() != MODE_82586 && memcmp(buf, get_mac(), cfg_address_length()))
		rfd_cs |= RFD_S_MULTICAST;

	// fetch initial rbd address from rfd
	u32 rbd_address = address(m_rfd, 6, 8, RBD_EMPTY);

	// check for simplified mode
	if (mode() != MODE_82586 && !(rfd_cs & RFD_SF))
	{
		// fetch size word
		u16 rfd_size = m_space->read_word(m_rfd + 10 + linear_offset, RB_SIZE);

		// increment "no resources" counter
		if (rfd_size < length)
			m_space->write_dword(m_scb_address + 16 + linear_offset, m_space->read_dword(m_scb_address + 16 + linear_offset) + 1);

		// truncate/capture the frame
		if (length <= rfd_size || cfg_save_bad_frames())
		{
			// compute stored length
			int actual = (rfd_size < length) ? rfd_size : length;

			LOG("ru_execute storing %d bytes into rfd size %d\n", actual, rfd_size);

			// store data in rfd
			store_bytes(m_rfd + 12 + linear_offset, buf, actual);
			position += actual;
			remaining -= actual;

			// store actual count, f and eof
			m_space->write_word(m_rfd + 8 + linear_offset, actual | RB_F | RB_EOF);

			// set frame received and truncated frame status
			rfd_cs |= RFD_C | (actual < length ? RFD_S_TRUNCATED : 0);
		}
		else
			LOG("ru_execute discarding %d byte frame exceeding rfd size %d\n", length, rfd_size);
	}
	else
	{
		// flexible mode, store leading data into rfd
		if (mode() != MODE_82586)
		{
			// fetch size word
			u16 rfd_size = m_space->read_word(m_rfd + 10 + linear_offset, RB_SIZE);

			// compute stored length (from rfd_size)
			int actual = (rfd_size < length) ? rfd_size : length;

			LOG("ru_execute storing %d bytes into rfd size %d\n", actual, rfd_size);

			// store data in rfd
			store_bytes(m_rfd + 12 + linear_offset, buf, actual);
			position += actual;
			remaining -= actual;

			// store actual count, f and eof
			m_space->write_word(m_rfd + 8 + linear_offset, actual | RB_F | (remaining ? 0 : RB_EOF));
		}
		else if (!cfg_no_src_add_ins())
		{
			// compute stored length (from 2 * addresses + length field)
			int actual = cfg_address_length() * 2 + 2;

			LOG("ru_execute storing %d bytes into rfd\n", actual);

			// store data in rfd
			store_bytes(m_rfd + 8, buf, actual);
			position += actual;
			remaining -= actual;
		}

		// store remaining bytes in receive buffers
		while (remaining && rbd_address != RBD_EMPTY)
		{
			// fetch the count and address for this buffer
			u32 rb_address = m_space->read_dword(rbd_address + 4 + linear_offset);
			u16 rbd_size = m_space->read_word(rbd_address + 8 + linear_offset);

			// compute number of bytes to store in buffer
			int actual = remaining > (rbd_size & RB_SIZE) ? (rbd_size & RB_SIZE) : remaining;

			LOG("ru_execute storing %d bytes into receive buffer 0x%08x size %d\n", actual, rb_address, rbd_size & RB_SIZE);

			// store data in buffer
			store_bytes(rb_address, &buf[position], actual);
			position += actual;
			remaining -= actual;

			// store actual count
			m_space->write_word(rbd_address + 0, actual | RB_F | (remaining ? 0 : RB_EOF));

			// check if buffers exhausted
			if ((rbd_size & RB_EL))
			{
				rbd_address = RBD_EMPTY;

				if (remaining)
				{
					// set buffers exhausted status
					rfd_cs |= RFD_S_BUFFER;

					m_ru_state = mode() == MODE_82586 ? RU_NR : RU_NR_RBD;
					m_rnr = true;
				}
			}
			else
				// fetch next rbd address
				rbd_address = address(rbd_address, 2, 4);
		}

		if (remaining == 0 || cfg_save_bad_frames())
			// set frame received status
			rfd_cs |= RFD_C;
	}

	// frame received without errors
	if (!(rfd_cs & (mode() == MODE_82586 ? RFD_ERROR_82586 : RFD_ERROR)))
	{
		LOG("ru_execute frame received without error\n");

		rfd_cs |= RFD_OK;
	}
	else
		LOG("ru_execute frame received with errors status 0x%04x\n", rfd_cs);

	// store status
	m_space->write_dword(m_rfd, rfd_cs);

	// if we received without error, or we're saving bad frames, advance to the next rfd
	if ((rfd_cs & RFD_OK) || cfg_save_bad_frames())
	{
		if (!(rfd_cs & RFD_EL))
		{
			// advance to next rfd
			m_rfd = address(m_rfd, 4, 4);

			// store next free rbd address into rfd
			if (rbd_address != RBD_EMPTY)
			{
				if (mode() == MODE_LINEAR)
					m_space->write_dword(m_rfd + 8, rbd_address);
				else
					m_space->write_word(m_rfd + 6, rbd_address - m_scb_base);
			}
		}
		else
		{
			m_ru_state = mode() == MODE_82586 ? RU_NR : RU_NR_RFD;
			m_rnr = true;
		}

		// set frame received status
		m_fr = true;
	}

	// suspend on completion
	if (rfd_cs & RFD_S)
	{
		m_ru_state = RU_SUSPENDED;
		m_rnr = true;
	}

	static const char *const RU_STATE_NAME[] = { "IDLE", "SUSPENDED", "NO RESOURCES", nullptr, "READY", nullptr, nullptr, nullptr, nullptr, nullptr, "NO RESOURCES (RFD)", nullptr, "NO RESOURCES (RBD)" };
	LOG("ru_execute complete state %s\n", RU_STATE_NAME[m_ru_state]);
}

u32 i82596_device::address(u32 base, int offset, int address, u16 empty)
{
	if (mode() != MODE_LINEAR)
	{
		u16 data = m_space->read_word(base + offset);

		return (data == empty) ? empty : m_scb_base + data;
	}
	else
		return m_space->read_dword(base + address);
}