summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/audio/nl_zac1b11142.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mame/audio/nl_zac1b11142.cpp')
-rw-r--r--src/mame/audio/nl_zac1b11142.cpp338
1 files changed, 338 insertions, 0 deletions
diff --git a/src/mame/audio/nl_zac1b11142.cpp b/src/mame/audio/nl_zac1b11142.cpp
new file mode 100644
index 00000000000..a828e6b2aa5
--- /dev/null
+++ b/src/mame/audio/nl_zac1b11142.cpp
@@ -0,0 +1,338 @@
+// license:BSD-3-Clause
+// copyright-holders:Vas Crabb
+
+#include "audio/nl_zac1b11142.h"
+#include "netlist/devices/net_lib.h"
+
+
+#ifndef __PLIB_PREPROCESSOR__
+
+#endif
+
+
+NETLIST_START(zac1b11142_schematics)
+
+ // Shared chips
+
+ TTL_7474_DIP(U3A) // FIXME: need 74LS family model (higher input impedance, half the sink capability)
+ CD4016_DIP(U5D)
+
+ NET_C(VCC, /*U3A.14,*/ U5D.14) // 7474 model doesn't have Vcc pin
+ NET_C(GND, /*U3A.7,*/ U5D.7) // 7474 model doesn't have GND pin
+
+ NET_C(VCC, U3A.10, U3A.11, U3A.12, U3A.13) // only half of this chip is used in this audio section - tie up the other inputs
+
+
+ // ANAL1/IOA3/IOA4 -> RULLANTE/CASSA
+
+ CAP(C61, CAP_U(10))
+ CAP(C62, CAP_P(1000))
+ CAP(C63, CAP_U(0.01))
+ CAP(C68, CAP_U(0.1))
+ RES(R84, RES_K(1.5))
+ RES(R102, RES_K(10))
+ RES(R103, RES_K(10))
+ RES(R104, RES_K(120))
+ RES(R105, RES_K(56))
+ RES(R122, RES_K(33))
+ RES(R123, RES_K(1))
+ RES(R125, RES_K(560))
+ RES(R126, RES_K(470))
+ RES(R127, RES_K(100))
+ RES(R128A, RES_K(56)) // schematic has two resistors marked as R128, need PCB photo or parts list to resolve
+ RES(R128B, 680)
+ RES(R129, RES_K(1))
+ RES(R120, RES_K(47))
+ RES(R124, RES_K(39))
+ RES(R130, RES_K(33))
+ RES(R131, RES_K(150))
+ RES(R132, RES_K(1))
+ RES(R133, RES_K(1))
+ LM3900(U5C1)
+ LM3900(U5C2)
+ LM3900(U5C3)
+
+ NET_C(ANAL1, R133.1, C63.1, U5D.9, U5D.10)
+ NET_C(IOA3, U5D.6)
+ NET_C(IOA4, U5D.12)
+ NET_C(U5D.11, R132.1, R131.1)
+ NET_C(R131.2, R130.1, U5C1.MINUS)
+ NET_C(R130.2, R124.1, U5C1.OUT)
+ NET_C(R120.1, U5C1.PLUS)
+ NET_C(U5D.8, R129.1, R128A.1)
+ NET_C(R128A.2, R126.2, R127.1, R125.1, C62.1, U5C2.MINUS)
+ NET_C(R125.2, C62.2, U5C2.OUT, R123.1)
+ NET_C(R128B.1, U5C2.PLUS)
+ NET_C(R123.2, C61.1, C68.1)
+ NET_C(C68.2, R122.1)
+ NET_C(R122.2, R104.1, U5C3.MINUS)
+ NET_C(R104.2, U5C3.OUT, R105.1)
+ NET_C(R102.1, U5C3.PLUS)
+ NET_C(R103.2, R102.2, R84.1)
+ NET_C(VCC, R126.1, R103.1, U5C1.VP, U5C2.VP, U5C3.VP)
+ NET_C(GND, R133.2, C63.2, R132.2, R120.2, R129.2, R127.2, R128B.2, C61.2, R84.2, U5C1.VM, U5C2.VM, U5C3.VM)
+ ALIAS(RULLANTE, R124.2)
+ ALIAS(CASSA, R105.2)
+
+
+ // ANAL2 -> BASSO
+
+ CAP(C29, CAP_U(0.1))
+ CAP(C45, CAP_P(1000))
+ CAP(C52, CAP_U(0.022)) // schematic says "0,02µ" but that isn't a standard e-series value
+ CAP(C53, CAP_U(0.022)) // schematic says "0,02µ" but that isn't a standard e-series value
+ CAP(C54, CAP_U(2.2))
+ RES(R46, RES_K(1))
+ RES(R69, RES_K(2.2))
+ RES(R83, RES_K(2.2))
+ RES(R85, RES_K(120))
+ RES(R86, RES_K(100))
+ RES(R87, RES_K(33))
+ RES(R88, RES_K(15)) // schematic just says "15" but 15Ω would be way too low
+ RES(R98, RES_K(180))
+ RES(R99, RES_K(47))
+ RES(R100, RES_K(1))
+ RES(R101, RES_K(4.7))
+ RES(R106, RES_K(68))
+ RES(R118, RES_K(33))
+ LM3900(U5B4)
+ LM3900(U5C4)
+
+ NET_C(ANAL2, R46.1, C29.1)
+ NET_C(C29.2, R69.1)
+ NET_C(R69.2, R98.1, C52.1, U5B4.MINUS)
+ NET_C(R98.2, C52.2, U5B4.OUT, C53.1)
+ NET_C(R99.1, U5B4.PLUS)
+ NET_C(R101.2, R99.2, R100.1)
+ NET_C(C53.2, R118.1)
+ NET_C(R118.2, C54.1, R83.1)
+ NET_C(R83.2, C45.1, R85.1, U5C4.MINUS)
+ NET_C(C45.2, R85.2, U5C4.OUT, R106.1)
+ NET_C(R86.1, U5C4.PLUS)
+ NET_C(R87.2, R86.2, R88.1)
+ NET_C(VCC, R101.1, R87.1, U5B4.VP, U5C4.VP)
+ NET_C(GND, R46.2, R100.2, C54.2, R88.2, U5B4.VM, U5C4.VM)
+ ALIAS(BASSO, R106.2)
+
+
+ // ANAL3/SW1
+
+ CAP(C44, CAP_U(0.1))
+ CAP(C56, CAP_U(0.01))
+ RES(R70, RES_K(10))
+ RES(R71, RES_K(1))
+ RES(R72, RES_K(10))
+ RES(R82, RES_K(10))
+
+ NET_C(ANAL3, R71.1, R72.1)
+ NET_C(R72.2, U5D.4, R70.1)
+ NET_C(SW1, U5D.5) // this connection is not shown on the schematic
+ NET_C(U5D.3, C56.1)
+ NET_C(R70.2, C44.1)
+ NET_C(C44.2, R82.1)
+ NET_C(GND, R71.2, C56.2)
+
+
+ // ANAL6
+ // schematic shows an open jumper between R80 and the mix, so this channel may have been disconnected in practice
+
+ CAP(C42, CAP_U(0.1))
+ CAP(C43, CAP_U(0.01))
+ RES(R47, RES_K(1))
+ RES(R48, RES_K(10))
+ RES(R80, RES_K(10))
+ RES(R81, RES_K(10))
+
+ NET_C(ANAL6, R47.1, R48.1)
+ NET_C(R48.2, C42.1)
+ NET_C(C42.2, R81.1)
+ NET_C(R81.2, C43.1, R80.1)
+ NET_C(GND, R47.2, C43.2)
+
+
+ // ANAL4 -> PIANO
+
+ CAP(C41, CAP_U(0.1))
+ CAP(C49, CAP_U(0.01))
+ RES(R78, RES_K(1))
+ RES(R79, RES_K(47))
+ RES(R90, RES_K(68))
+ RES(R91, RES_K(12))
+ RES(R92, RES_K(33))
+ RES(R93, RES_K(100))
+ RES(R107, RES_K(100))
+ LM3900(U5B2)
+
+ NET_C(ANAL4, R78.1, C41.1)
+ NET_C(C41.2, R79.1)
+ NET_C(R79.2, C49.1, R107.1, U5B2.MINUS)
+ NET_C(C49.2, R107.2, U5B2.OUT, R90.1)
+ NET_C(R93.1, U5B2.PLUS)
+ NET_C(R92.2, R93.2, R91.1)
+ NET_C(VCC, R92.1, U5B2.VP)
+ NET_C(GND, R78.2, R91.2, U5B2.VM)
+ ALIAS(PIANO, R90.2)
+
+
+ // ANAL5 -> TROMBA
+
+ CAP(C28, CAP_P(1000))
+ CAP(C37, CAP_U(1))
+ CAP(C50, CAP_P(1000))
+ QBJT_EB(T6, "BC548C") // schematic says "BC548"
+ RES(R39, 220)
+ RES(R40, RES_K(100))
+ RES(R64, RES_K(4.7))
+ RES(R65, RES_K(4.7))
+ RES(R66, RES_K(1))
+ RES(R67, RES_K(1))
+ RES(R94, RES_K(10))
+ RES(R95, RES_K(100))
+ RES(R96, RES_K(4.7))
+ RES(R108, RES_K(10))
+ RES(R109, RES_K(10))
+ RES(R110, RES_K(10))
+ RES(R111, RES_K(8.2))
+ RES(R112, RES_K(100))
+ RES(R113, RES_M(1))
+ LM3900(U5B1)
+
+ NET_C(ANAL5, R66.1, R67.1)
+ NET_C(R67.2, C28.1, R40.1, T7.B)
+ NET_C(R64.2, T6.C, U3A.3)
+ NET_C(R65.2, U3A.2, U3A.4, U3A.1) // FIXME: pin 1 actually comes from a Schmitt trigger in U4A
+ NET_C(U3A.5, R39.1, U5D.13)
+ NET_C(R39.2, C37.1) // FIXME: this junction feeds a Schmitt trigger in U4A
+ NET_C(R109.1, R94.2, R108.1, R95.1)
+ NET_C(R109.2, LEVELT)
+ NET_C(R110.2, R112.1, R111.1)
+ NET_C(R112.2, U5D.1, C50.1, R113.1, U5B1.MINUS)
+ NET_C(R95.2, U5B1.PLUS)
+ NET_C(U5D.2, C50.2, R113.2, U5B1.OUT, R96.1)
+ NET_C(VCC, R64.1, R65.1, R94.1, R110.1, U5B1.VP)
+ NET_C(GND, R66.2, C28.2, R40.2, T6.E, C37.2, R108.2, R111.2, U5B1.VM)
+ ALIAS(TROMBA, R96.2)
+
+
+ // Mixdown
+
+ CAP(C40, CAP_U(0.1))
+ POT(P1, RES_K(10))
+ QBJT_EB(T7, "BC548C") // schematic says "BC548"
+ RES(R41, RES_K(8.2))
+ RES(R42, RES_K(5.6))
+ RES(R43, RES_K(3.3))
+ RES(R44, RES_K(1.5))
+ RES(R45, RES_K(10))
+ RES(R68, RES_K(10))
+ RES(R73, 820)
+ RES(R74, 390)
+ RES(R75, 150)
+ RES(R76, 47)
+ RES(R77, RES_K(10))
+ RES(R97, RES_K(150))
+ RES(R114, RES_K(4.7))
+ RES(R115, RES_K(82))
+ RES(R116, RES_K(47))
+ RES(R117, RES_K(1))
+ RES(R119, RES_K(4.7))
+ TTL_74156_DIP(U4B) // FIXME: should be a 74LS156 (lower sink capability)
+ LM3900(U5B3)
+
+#if 0
+ NET_C(RULLANTE, CASSA, BASSO, R82.2, R80.2, PIANO, C40.1, R77.1)
+#else
+ // cassa swamps the other instruments if it's connected - just ground it for now
+ NET_C(GND, CASSA)
+ NET_C(RULLANTE, BASSO, R82.2, R80.2, PIANO, C40.1, R77.1)
+#endif
+ NET_C(C40.2, R97.1)
+ NET_C(TROMBA, R97.2, R41.2, R42.2, R43.2, R44.2, R73.2, R74.2, R75.2, R76.2)
+ NET_C(IOA2, U4B.13)
+ NET_C(IOA1, U4B.3)
+ NET_C(IOA0, U4B.1, U4B.15)
+ NET_C(U4B.9, R41.1)
+ NET_C(U4B.10, R42.1)
+ NET_C(U4B.11, R43.1)
+ NET_C(U4B.12, R44.1)
+ NET_C(U4B.7, R73.1)
+ NET_C(U4B.6, R74.1)
+ NET_C(U4B.5, R75.1)
+ NET_C(U4B.4, R76.1)
+ NET_C(R77.2, U5B3.PLUS)
+ NET_C(R119.2, R117.2, R116.1)
+ NET_C(R116.2, R115.1, U5B3.MINUS)
+ NET_C(R115.2, U5B3.OUT, R114.1)
+ NET_C(R114.2, R45.1, P1.1)
+ NET_C(R45.2, T7.C)
+ NET_C(T7.B, R68.1)
+ NET_C(R68.2, LEVEL)
+ NET_C(VCC, R119.1, U4B.16, U5B3.VP)
+ NET_C(GND, P1.3, T7.E, R117.1, U4B.2, U4B.8, U4B.14)
+ NET_C(GND, U5B3.VM)
+
+NETLIST_END()
+
+
+NETLIST_START(zac1b11142)
+
+ SOLVER(Solver, 18000)
+ PARAM(Solver.ACCURACY, 1e-8)
+ PARAM(Solver.NR_LOOPS, 300)
+ PARAM(Solver.GS_LOOPS, 1)
+ //PARAM(Solver.METHOD, "SOR")
+ PARAM(Solver.METHOD, "MAT_CR")
+ //PARAM(Solver.METHOD, "GMRES")
+ PARAM(Solver.PARALLEL, 0)
+ PARAM(Solver.SOR_FACTOR, 1.00)
+ PARAM(Solver.DYNAMIC_TS, 0)
+ PARAM(Solver.DYNAMIC_LTE, 5e-4)
+ PARAM(Solver.DYNAMIC_MIN_TIMESTEP, 20e-6)
+
+ LOCAL_SOURCE(zac1b11142_schematics)
+
+ ANALOG_INPUT(I_P12, 11.3) // +12V dropped with a 1N4004
+ ANALOG_INPUT(I_P5, 5)
+ //ANALOG_INPUT(I_V0, 0)
+ ANALOG_INPUT(I_M5, -5)
+ ALIAS(VCC, I_P5.Q)
+ ALIAS(I_V0.Q, GND)
+
+ NET_MODEL("AY8910PORT FAMILY(OVL=0.05 OVH=4.95 ORL=100.0 ORH=0.5k)")
+
+ // AY-3-8910 4G/4H digital outputs
+ LOGIC_INPUT(I_IOA0, 1, "AY8910PORT")
+ LOGIC_INPUT(I_IOA1, 1, "AY8910PORT")
+ LOGIC_INPUT(I_IOA2, 1, "AY8910PORT")
+ LOGIC_INPUT(I_IOA3, 1, "AY8910PORT")
+ LOGIC_INPUT(I_IOA4, 1, "AY8910PORT")
+ LOGIC_INPUT(I_LEVEL, 1, "AY8910PORT")
+ LOGIC_INPUT(I_LEVELT, 1, "AY8910PORT")
+ LOGIC_INPUT(I_SW1, 1, "AY8910PORT")
+ ALIAS(IOA0, I_IOA0.Q)
+ ALIAS(IOA1, I_IOA1.Q)
+ ALIAS(IOA2, I_IOA2.Q)
+ ALIAS(IOA3, I_IOA3.Q)
+ ALIAS(IOA4, I_IOA4.Q)
+ ALIAS(LEVEL, I_LEVEL.Q)
+ ALIAS(LEVELT, I_LEVELT.Q)
+ ALIAS(SW1, I_SW1.Q)
+
+ // AY-3-8910 4G/4H analog outputs
+ RES(R_AY4G_A, 1000)
+ RES(R_AY4G_B, 1000)
+ RES(R_AY4G_C, 1000)
+ RES(R_AY4H_A, 1000)
+ RES(R_AY4H_B, 1000)
+ RES(R_AY4H_C, 1000)
+ NET_C(I_P5, R_AY4G_A.1, R_AY4G_B.1, R_AY4G_C.1, R_AY4H_A.1, R_AY4H_B.1, R_AY4H_C.1)
+ ALIAS(ANAL1, R_AY4G_A.2)
+ ALIAS(ANAL2, R_AY4G_B.2)
+ ALIAS(ANAL3, R_AY4G_C.2)
+ ALIAS(ANAL4, R_AY4H_A.2)
+ ALIAS(ANAL5, R_AY4H_B.2)
+ ALIAS(ANAL6, R_AY4H_C.2)
+
+ INCLUDE(zac1b11142_schematics)
+
+NETLIST_END()
5 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
/*************************************************************************

    laserdsc.c

    Generic laserdisc support.

    Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team.
    Visit http://mamedev.org for licensing and usage restrictions.

*************************************************************************/

#include "driver.h"
#include "laserdsc.h"
#include "avcomp.h"
#include "profiler.h"
#include "streams.h"
#include "sound/custom.h"



/***************************************************************************
    DEBUGGING
***************************************************************************/

#define PRINTF_COMMANDS				1

#if PRINTF_COMMANDS
#define CMDPRINTF(x)				mame_printf_debug x
#else
#define CMDPRINTF(x)
#endif



/***************************************************************************
    CONSTANTS
***************************************************************************/

/* fractional track handling */
#define FRACBITS					12
#define FRAC_ONE					(1 << FRACBITS)
#define INT_TO_FRAC(x)				((x) << FRACBITS)
#define FRAC_TO_INT(x)				((x) >> FRACBITS)

/* general laserdisc states */
enum _laserdisc_state
{
	LASERDISC_EJECTED,						/* no disc present */
	LASERDISC_EJECTING,						/* in the process of ejecting */
	LASERDISC_LOADED,						/* disc just loaded */
	LASERDISC_LOADING,						/* in the process of loading */
	LASERDISC_PARKED,						/* disc loaded, not playing, head on parked position */
	LASERDISC_SPINUP,						/* disc loaded, spinning up to speed */

	LASERDISC_SEARCHING_FRAME,				/* searching (seeking) to a new frame */
	LASERDISC_SEARCH_FINISHED,				/* finished searching; same as stopped */

	LASERDISC_STOPPED,						/* stopped (paused) with video visible and no sound */
	LASERDISC_AUTOSTOPPED,					/* autostopped; same as stopped */

	LASERDISC_PLAYING_FORWARD,				/* playing at 1x in the forward direction */
	LASERDISC_PLAYING_REVERSE,				/* playing at 1x in the reverse direction */
	LASERDISC_PLAYING_SLOW_FORWARD,			/* playing forward slowly */
	LASERDISC_PLAYING_SLOW_REVERSE,			/* playing backward slowly */
	LASERDISC_PLAYING_FAST_FORWARD,			/* playing fast forward */
	LASERDISC_PLAYING_FAST_REVERSE,			/* playing fast backward */
	LASERDISC_STEPPING_FORWARD,				/* stepping forward */
	LASERDISC_STEPPING_REVERSE,				/* stepping backward */
	LASERDISC_SCANNING_FORWARD,				/* scanning forward at the scan rate */
	LASERDISC_SCANNING_REVERSE				/* scanning backward at the scan rate */
};
typedef enum _laserdisc_state laserdisc_state;

/* generic states and configuration */
#define FRAMEFLAG_PREV_SAME_FRAME	0x01
#define FRAMEFLAG_NEXT_SAME_FRAME	0x02

#define AUDIO_CH1_ENABLE			0x01
#define AUDIO_CH2_ENABLE			0x02
#define AUDIO_EXPLICIT_MUTE			0x04
#define AUDIO_IMPLICIT_MUTE			0x08

#define VIDEO_ENABLE				0x01
#define VIDEO_EXPLICIT_MUTE			0x02
#define VIDEO_IMPLICIT_MUTE			0x04

#define DISPLAY_ENABLE				0x01

#define NULL_TARGET_FRAME			0					/* frame 0 indicates no target */
#define ONE_TRACK					INT_TO_FRAC(1)		/* a single track, or track #1 */
#define STOP_SPEED					INT_TO_FRAC(0)		/* no movement */
#define PLAY_SPEED					INT_TO_FRAC(1)		/* regular playback speed */

#define GENERIC_SPINUP_TIME			(attotime_make(3, 0))
#define GENERIC_LOAD_TIME			(attotime_make(10, 0))
#define GENERIC_RESET_SPEED			INT_TO_FRAC(5000)

/* Pioneer PR-7820 specific states */
#define PR7820_MODE_MANUAL			0
#define PR7820_MODE_AUTOMATIC		1
#define PR7820_MODE_PROGRAM			2

#define PR7820_SEARCH_SPEED			INT_TO_FRAC(5000)

/* Pioneer PR-8210/LD-V1100 specific states */
#define PR8210_MODE_GET_1ST			0
#define PR8210_MODE_GET_2ND			1
#define PR8210_MODE_GET_3RD			2

#define PR8210_SCAN_SPEED			(INT_TO_FRAC(2000) / 30)
#define PR8210_FAST_SPEED			(PLAY_SPEED * 3)
#define PR8210_SLOW_SPEED			(PLAY_SPEED / 5)
#define PR8210_STEP_SPEED			(PLAY_SPEED / 7)
#define PR8210_SEARCH_SPEED			INT_TO_FRAC(5000)

/* Pioneer LD-V1000 specific states */
#define LDV1000_MODE_STATUS			0
#define LDV1000_MODE_GET_FRAME		1
#define LDV1000_MODE_GET_1ST		2
#define LDV1000_MODE_GET_2ND		3
#define LDV1000_MODE_GET_RAM		4

#define LDV1000_SCAN_SPEED			(INT_TO_FRAC(2000) / 30)
#define LDV1000_SEARCH_SPEED		INT_TO_FRAC(5000)

/* Sony LDP-1450 specific states */
#define LDP1450_SCAN_SPEED			(INT_TO_FRAC(2000) / 30)
#define LDP1450_FAST_SPEED			(PLAY_SPEED * 3)
#define LDP1450_SLOW_SPEED			(PLAY_SPEED / 5)
#define LDP1450_STEP_SPEED			(PLAY_SPEED / 7)
#define LDP1450_SEARCH_SPEED		INT_TO_FRAC(5000)

/* Philips 22VP932 specific states */
#define VP932_SCAN_SPEED			(INT_TO_FRAC(2000) / 30)
#define VP932_FAST_SPEED			(PLAY_SPEED * 3)
#define VP932_SLOW_SPEED			(PLAY_SPEED / 5)
#define VP932_STEP_SPEED			(PLAY_SPEED / 7)
#define VP932_SEARCH_SPEED			INT_TO_FRAC(5000)



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

/* PR-7820-specific data */
typedef struct _pr7820_info pr7820_info;
struct _pr7820_info
{
	UINT8			mode;					/* current mode */
	UINT32			curpc;					/* current PC for automatic execution */
	INT32			configspeed;			/* configured speed */
	UINT32			activereg;				/* active register index */
	UINT8			ram[1024];				/* RAM */
};


/* PR8210-specific data */
typedef struct _pr8210_info pr8210_info;
struct _pr8210_info
{
	UINT8			mode;					/* current mode */
	UINT16			commandtriplet[3];		/* current command triplet */
	attotime		commandtime;			/* command time */
	UINT8			commandbits;			/* command bit count */
	UINT8			seekstate;				/* state of the seek command */
};


/* LD-V1000-specific data */
typedef struct _ldv1000_info ldv1000_info;
struct _ldv1000_info
{
	UINT8			mode;					/* current mode */
	UINT32			activereg;				/* active register index */
	UINT8			ram[1024];				/* RAM */
	UINT32			readpos;				/* current read position */
	UINT32			readtotal;				/* current read position */
	UINT8			readbuf[256];			/* temporary read buffer */
	UINT8			statusmask;				/* status bits mask */
};


/* LDP-1450-specific data */
typedef struct _ldp1450_info ldp1450_info;
struct _ldp1450_info
{
	UINT32			readpos;				/* current read position */
	UINT32			readtotal;				/* current read position */
	UINT8			readbuf[256];			/* temporary read buffer */
};


/* 22VP932-specific data */
typedef struct _vp932_info vp932_info;
struct _vp932_info
{
	UINT8			incount;				/* number of pending bytes accumulated */
	UINT8			inbuffer[8];			/* input data */
	UINT8			outcount;				/* number of pending bytes to send */
	UINT8			outbuffer[8];			/* output data */
};


/* per-field metadata */
typedef struct _field_metadata field_metadata;
struct _field_metadata
{
	UINT8			version;				/* version of the data */
	UINT8			frameflags;				/* per-frame flags */
	UINT8			whiteflag;				/* white flag */
	UINT32			line16;					/* line 16 Philips code */
	UINT32			line17;					/* line 17 Philips code */
	UINT32			line18;					/* line 18 Philips code */
};


/* generic data */
struct _laserdisc_info
{
	/* disc parameters */
	chd_file *		disc;					/* handle to the disc itself */
	av_codec_decompress_config avconfig;	/* decompression configuration */
	UINT8			readpending;			/* true if a read is pending */
	UINT8 *			framebuffer;			/* buffer to hold one frame */
	UINT32			maxfractrack;			/* maximum track number */
	UINT32			fieldnum;				/* field number (0 or 1) */

	/* video data */
	mame_bitmap *	videoframe[2];			/* currently cached frames */
	UINT8			videofields[2];			/* number of fields in each frame */
	UINT32			videoframenum[2];		/* frame number contained in each frame */
	UINT8			videoindex;				/* index of the current video buffer */
	mame_bitmap *	emptyframe;				/* blank frame */

	/* audio data */
	INT16 *			audiobuffer[2];			/* buffer for audio samples */
	UINT32			audiobufsize;			/* size of buffer */
	UINT32			audiobufin;				/* input index */
	UINT32			audiobufout;			/* output index */
	int				audiocustom;			/* custom sound index */

	/* metadata */
	field_metadata	metadata[2];			/* metadata parsed from the stream, for each field */
	int				last_frame;				/* last seen frame number */
	int				last_chapter;			/* last seen chapter number */

	/* core states */
	UINT8			type;					/* laserdisc type */
	laserdisc_state	state;					/* current player state */
	UINT8			video;					/* video state: bit 0 = on/off */
	UINT8			audio;					/* audio state: bit 0 = audio 1, bit 1 = audio 2 */
	UINT8			display;				/* display state: bit 0 = on/off */
	attotime		lastvsynctime;			/* time of the last vsync */

	/* deferred states */
	attotime		holdfinished;			/* time when current state will advance */
	UINT8			postholdstate;			/* state to switch into after holding */
	INT32			postholdfracspeed;		/* speed after the hold */

	/* input data */
	UINT8			datain;					/* current input data value */
	UINT8			linein[LASERDISC_INPUT_LINES]; /* current input line state */

	/* output data */
	UINT8			dataout;				/* current output data value */
	UINT8			lineout[LASERDISC_OUTPUT_LINES]; /* current output line state */

	/* command and parameter buffering */
	UINT8			command;				/* current command */
	INT32			parameter;				/* command parameter */

	/* playback/search/scan speeds */
	INT32			curfracspeed;			/* current speed the head is moving */
	INT32			curfractrack;			/* current track */
	INT32			targetframe;			/* target frame (0 means no target) */

	/* debugging */
	char			text[100];				/* buffer for the state */

	/* filled in by player-specific init */
	void 			(*writedata)(laserdisc_info *info, UINT8 prev, UINT8 new); /* write callback */
	void 			(*writeline[LASERDISC_INPUT_LINES])(laserdisc_info *info, UINT8 new); /* write line callback */
	UINT8			(*readdata)(laserdisc_info *info);	/* status callback */
	UINT8			(*readline[LASERDISC_OUTPUT_LINES])(laserdisc_info *info); /* read line callback */
	void			(*statechanged)(laserdisc_info *info, UINT8 oldstate); /* state changed callback */

	/* some player-specific data */
	union
	{
		pr7820_info	pr7820;					/* PR-7820-specific info */
		pr8210_info	pr8210;					/* PR-8210-specific info */
		ldv1000_info ldv1000;				/* LD-V1000-specific info */
		ldp1450_info ldp1450;				/* LDP-1450-specific info */
		vp932_info vp932;					/* 22VP932-specific info */
	} u;
};


/* sound callback info */
typedef struct _sound_token sound_token;
struct _sound_token
{
	sound_stream *	stream;
	laserdisc_info *info;
};



/***************************************************************************
    FUNCTION PROTOTYPES
***************************************************************************/

/* generic helper functions */
static int update_position(laserdisc_info *info);
static void read_track_data(laserdisc_info *info);
static void process_track_data(laserdisc_info *info);
static int parse_metadata(const UINT8 *rawdata, UINT32 track, UINT8 which, field_metadata *metadata);
static void *custom_start(int clock, const struct CustomSound_interface *config);
static void custom_stream_callback(void *param, stream_sample_t **inputs, stream_sample_t **outputs, int samples);

/* Pioneer PR-7820 implementation */
static void pr7820_init(laserdisc_info *info);
static void pr7820_soft_reset(laserdisc_info *info);
static void pr7820_enter_w(laserdisc_info *info, UINT8 data);
static UINT8 pr7820_ready_r(laserdisc_info *info);
static UINT8 pr7820_status_r(laserdisc_info *info);

/* Pioneer PR-8210 implementation */
static void pr8210_init(laserdisc_info *info);
static void pr8210_soft_reset(laserdisc_info *info);
static void pr8210_command(laserdisc_info *info);
static void pr8210_control_w(laserdisc_info *info, UINT8 data);

/* Pioneer LDV-1000 implementation */
static void ldv1000_init(laserdisc_info *info);
static void ldv1000_soft_reset(laserdisc_info *info);
static void ldv1000_data_w(laserdisc_info *info, UINT8 prev, UINT8 data);
static UINT8 ldv1000_status_strobe_r(laserdisc_info *info);
static UINT8 ldv1000_command_strobe_r(laserdisc_info *info);
static UINT8 ldv1000_status_r(laserdisc_info *info);

/* Sony LDP-1450 implementation */
static void ldp1450_init(laserdisc_info *info);
static void ldp1450_soft_reset(laserdisc_info *info);
static void ldp1450_compute_status(laserdisc_info *info);
static void ldp1450_data_w(laserdisc_info *info, UINT8 prev, UINT8 data);
static UINT8 ldp1450_data_avail_r(laserdisc_info *info);
static UINT8 ldp1450_data_r(laserdisc_info *info);
static void ldp1450_state_changed(laserdisc_info *info, UINT8 oldstate);

/* Philips 22VP932 implementation */
static void vp932_init(laserdisc_info *info);
static void vp932_soft_reset(laserdisc_info *info);
static void vp932_data_w(laserdisc_info *info, UINT8 prev, UINT8 data);
static UINT8 vp932_data_avail_r(laserdisc_info *info);
static UINT8 vp932_data_r(laserdisc_info *info);
static void vp932_state_changed(laserdisc_info *info, UINT8 oldstate);



/***************************************************************************
    GLOBAL VARIABLES
***************************************************************************/

struct CustomSound_interface laserdisc_custom_interface =
{
	custom_start
};



/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

/*-------------------------------------------------
    audio_channel_active - return TRUE if the
    given audio channel should be output
-------------------------------------------------*/

INLINE int audio_channel_active(laserdisc_info *info, int channel)
{
	int result = (info->audio >> channel) & 1;

	/* apply muting */
	if (info->audio & (AUDIO_EXPLICIT_MUTE | AUDIO_IMPLICIT_MUTE))
		result = 0;

	/* implicitly muted during some states */
	switch (info->state)
	{
		case LASERDISC_EJECTED:
		case LASERDISC_EJECTING:
		case LASERDISC_LOADING:
		case LASERDISC_SPINUP:
		case LASERDISC_PARKED:
		case LASERDISC_LOADED:
		case LASERDISC_SEARCHING_FRAME:
		case LASERDISC_SEARCH_FINISHED:
		case LASERDISC_STOPPED:
		case LASERDISC_AUTOSTOPPED:
		case LASERDISC_PLAYING_SLOW_FORWARD:
		case LASERDISC_PLAYING_SLOW_REVERSE:
		case LASERDISC_PLAYING_FAST_FORWARD:
		case LASERDISC_PLAYING_FAST_REVERSE:
		case LASERDISC_SCANNING_FORWARD:
		case LASERDISC_SCANNING_REVERSE:
		case LASERDISC_STEPPING_FORWARD:
		case LASERDISC_STEPPING_REVERSE:
			result = 0;
			break;

		default:
			break;
	}
	return result;
}


/*-------------------------------------------------
    video_active - return TRUE if the video should
    be output
-------------------------------------------------*/

INLINE int video_active(laserdisc_info *info)
{
	int result = info->video & VIDEO_ENABLE;

	/* apply muting */
	if (info->video & (VIDEO_EXPLICIT_MUTE | VIDEO_IMPLICIT_MUTE))
		result = 0;

	/* implicitly muted during some states */
	switch (info->state)
	{
		case LASERDISC_EJECTED:
		case LASERDISC_EJECTING:
		case LASERDISC_LOADING:
		case LASERDISC_SPINUP:
		case LASERDISC_PARKED:
		case LASERDISC_LOADED:
		case LASERDISC_SEARCHING_FRAME:
			result = 0;
			break;

		default:
			break;
	}
	return result;
}


/*-------------------------------------------------
    laserdisc_ready - return TRUE if the
    disc is not in a transient state
-------------------------------------------------*/

INLINE int laserdisc_ready(laserdisc_info *info)
{
	switch (info->state)
	{
		case LASERDISC_EJECTED:
		case LASERDISC_EJECTING:
		case LASERDISC_LOADING:
		case LASERDISC_SPINUP:
		case LASERDISC_PARKED:
			return FALSE;

		case LASERDISC_LOADED:
		case LASERDISC_SEARCHING_FRAME:
		case LASERDISC_SEARCH_FINISHED:
		case LASERDISC_STOPPED:
		case LASERDISC_AUTOSTOPPED:
		case LASERDISC_PLAYING_FORWARD:
		case LASERDISC_PLAYING_REVERSE:
		case LASERDISC_PLAYING_SLOW_FORWARD:
		case LASERDISC_PLAYING_SLOW_REVERSE:
		case LASERDISC_PLAYING_FAST_FORWARD:
		case LASERDISC_PLAYING_FAST_REVERSE:
		case LASERDISC_STEPPING_FORWARD:
		case LASERDISC_STEPPING_REVERSE:
		case LASERDISC_SCANNING_FORWARD:
		case LASERDISC_SCANNING_REVERSE:
			return TRUE;
	}
	fatalerror("Unexpected state in laserdisc_ready\n");
}


/*-------------------------------------------------
    laserdisc_active - return TRUE if the
    disc is in a playing/spinning state
-------------------------------------------------*/

INLINE int laserdisc_active(laserdisc_info *info)
{
	switch (info->state)
	{
		case LASERDISC_EJECTED:
		case LASERDISC_EJECTING:
		case LASERDISC_LOADED:
		case LASERDISC_LOADING:
		case LASERDISC_SPINUP:
		case LASERDISC_PARKED:
			return FALSE;

		case LASERDISC_SEARCHING_FRAME:
		case LASERDISC_SEARCH_FINISHED:
		case LASERDISC_STOPPED:
		case LASERDISC_AUTOSTOPPED:
		case LASERDISC_PLAYING_FORWARD:
		case LASERDISC_PLAYING_REVERSE:
		case LASERDISC_PLAYING_SLOW_FORWARD:
		case LASERDISC_PLAYING_SLOW_REVERSE:
		case LASERDISC_PLAYING_FAST_FORWARD:
		case LASERDISC_PLAYING_FAST_REVERSE:
		case LASERDISC_STEPPING_FORWARD:
		case LASERDISC_STEPPING_REVERSE:
		case LASERDISC_SCANNING_FORWARD:
		case LASERDISC_SCANNING_REVERSE:
			return TRUE;
	}
	fatalerror("Unexpected state in laserdisc_ready\n");
}


/*-------------------------------------------------
    set_state - configure the current playback
    state
-------------------------------------------------*/

INLINE void set_state(laserdisc_info *info, laserdisc_state state, INT32 fracspeed, INT32 targetframe)
{
	info->holdfinished.seconds = 0;
	info->holdfinished.attoseconds = 0;
	info->state = state;
	info->curfracspeed = fracspeed;
	info->targetframe = targetframe;
}


/*-------------------------------------------------
    set_hold_state - configure a hold time and
    a playback state to follow
-------------------------------------------------*/

INLINE void set_hold_state(laserdisc_info *info, attotime holdtime, laserdisc_state state, INT32 fracspeed)
{
	info->holdfinished = attotime_add(timer_get_time(), holdtime);
	info->postholdstate = state;
	info->postholdfracspeed = fracspeed;
}


/*-------------------------------------------------
    add_to_current_track - add a value to the
    current track, stopping if we hit the min or
    max
-------------------------------------------------*/

INLINE int add_to_current_track(laserdisc_info *info, INT32 delta)
{
	info->curfractrack += delta;
	if (info->curfractrack < ONE_TRACK)
	{
		info->curfractrack = ONE_TRACK;
		return TRUE;
	}
	else if (info->curfractrack >= info->maxfractrack - ONE_TRACK)
	{
		info->curfractrack = info->maxfractrack - ONE_TRACK;
		return TRUE;
	}
	return FALSE;
}


/*-------------------------------------------------
    frame_from_metadata - return the frame number
    encoded in the metadata, if present, or -1
-------------------------------------------------*/

INLINE int frame_from_metadata(const field_metadata *metadata)
{
	UINT32 data;

	if ((metadata->line17 & 0xf80000) == 0xf80000)
		data = metadata->line17 & 0x7ffff;
	else if ((metadata->line18 & 0xf80000) == 0xf80000)
		data = metadata->line18 & 0x7ffff;
	else
		return -1;

	return (((data >> 16) & 0x0f) * 10000) + (((data >> 12) & 0x0f) * 1000) + (((data >> 8) & 0x0f) * 100) + (((data >> 4) & 0x0f) * 10) + (data & 0x0f);
}


/*-------------------------------------------------
    chapter_from_metadata - return the chapter
    number encoded in the metadata, if present,
    or -1
-------------------------------------------------*/

INLINE int chapter_from_metadata(const field_metadata *metadata)
{
	UINT32 data;

	if ((metadata->line17 & 0xf00fff) == 0x800ddd)
		data = metadata->line17 & 0x7f000;
	else if ((metadata->line18 & 0xf00fff) == 0x800ddd)
		data = metadata->line18 & 0x7f000;
	else
		return -1;

	return (((data >> 16) & 0x0f) * 10) + ((data >> 12) & 0x0f);
}


/*-------------------------------------------------
    read_16bits_from_ram_be - read 16 bits from
    player RAM in big-endian format
-------------------------------------------------*/

INLINE UINT16 read_16bits_from_ram_be(UINT8 *ram, UINT32 offset)
{
	return (ram[offset + 0] << 8) | ram[offset + 1];
}


/*-------------------------------------------------
    write_16bits_to_ram_be - write 16 bits to
    player RAM in big endian format
-------------------------------------------------*/

INLINE void write_16bits_to_ram_be(UINT8 *ram, UINT32 offset, UINT16 data)
{
	ram[offset + 0] = data >> 8;
	ram[offset + 1] = data >> 0;
}


/*-------------------------------------------------
    fillbitmap_yuy16 - fill a YUY16 bitmap with a
    given color pattern
-------------------------------------------------*/

INLINE void fillbitmap_yuy16(mame_bitmap *bitmap, UINT8 yval, UINT8 cr, UINT8 cb)
{
	UINT16 color0 = (yval << 8) | cb;
	UINT16 color1 = (yval << 8) | cr;
	int x, y;

	/* write 32 bits of color (2 pixels at a time) */
	for (y = 0; y < bitmap->height; y++)
	{
		UINT16 *dest = (UINT16 *)bitmap->base + y * bitmap->rowpixels;
		for (x = 0; x < bitmap->width / 2; x++)
		{
			*dest++ = color0;
			*dest++ = color1;
		}
	}
}



/***************************************************************************
    GENERIC IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    laserdisc_init - initialize state for
    laserdisc playback
-------------------------------------------------*/

laserdisc_info *laserdisc_init(int type, chd_file *chd, int custom_index)
{
	int fps = 30, fpsfrac = 0, width = 720, height = 240, interlaced = 1, channels = 2, rate = 44100, metabytes = 0;
	UINT32 fps_times_1million, max_samples_per_track;
	laserdisc_info *info;
	char metadata[256];
	chd_error err;

	assert_always(mame_get_phase(Machine) == MAME_PHASE_INIT, "Can only call laserdisc_init at init time!");

	/* initialize the info */
	info = auto_malloc(sizeof(*info));
	memset(info, 0, sizeof(*info));
	info->audiocustom = custom_index;

	/* get the disc metadata and extract the info */
	info->disc = chd;
	if (info->disc != NULL)
	{
		/* require the A/V codec */
		if (chd_get_header(info->disc)->compression != CHDCOMPRESSION_AV)
			fatalerror("Laserdisc video must be compressed with the A/V codec!");

		/* read and extract the metadata */
		err = chd_get_metadata(info->disc, AV_METADATA_TAG, 0, metadata, sizeof(metadata), NULL, NULL);
		if (err != CHDERR_NONE)
			fatalerror("Non-A/V CHD file specified");
		if (sscanf(metadata, AV_METADATA_FORMAT, &fps, &fpsfrac, &width, &height, &interlaced, &channels, &rate, &metabytes) != 8)
			fatalerror("Invalid metadata in CHD file");

		/* require interlaced video */
		if (!interlaced)
			fatalerror("Laserdisc video must be interlaced!");

		/* determine the maximum track and allocate a frame buffer */
		info->maxfractrack = INT_TO_FRAC(chd_get_header(info->disc)->totalhunks / (interlaced + 1));
		info->framebuffer = auto_malloc(chd_get_header(info->disc)->hunkbytes);
	}
	else
		info->maxfractrack = INT_TO_FRAC(54000);

	/* allocate video frames */
	info->videoframe[0] = auto_bitmap_alloc(width, height * 2, BITMAP_FORMAT_YUY16);
	fillbitmap_yuy16(info->videoframe[0], 40, 109, 240);
	info->videoframe[1] = auto_bitmap_alloc(width, height * 2, BITMAP_FORMAT_YUY16);
	fillbitmap_yuy16(info->videoframe[1], 40, 109, 240);
	info->emptyframe = auto_bitmap_alloc(width, height * 2, BITMAP_FORMAT_YUY16);
	fillbitmap_yuy16(info->emptyframe, 0, 128, 128);

	/* allocate audio buffers */
	fps_times_1million = fps * 1000000 + fpsfrac;
	max_samples_per_track = ((UINT64)rate * 1000000 + fps_times_1million - 1) / fps_times_1million;
	info->audiobufsize = max_samples_per_track * 4;
	info->audiobuffer[0] = auto_malloc(info->audiobufsize * sizeof(info->audiobuffer[0][0]));
	info->audiobuffer[1] = auto_malloc(info->audiobufsize * sizeof(info->audiobuffer[1][0]));

	/* attempt to wire up the audio */
	if (sndti_exists(SOUND_CUSTOM, info->audiocustom))
	{
		sound_token *token = custom_get_token(info->audiocustom);
		token->info = info;
		stream_set_sample_rate(token->stream, rate);
	}

	/* reset the disc */
	laserdisc_reset(info, type);
	return info;
}


/*-------------------------------------------------
    laserdisc_reset - reset laserdisc,
    potentially changing the type
-------------------------------------------------*/

void laserdisc_reset(laserdisc_info *info, int type)
{
	int i;

	/* set up the general info */
	if (type != 0)
		info->type = type;
	info->video = VIDEO_ENABLE;
	info->audio = AUDIO_CH1_ENABLE | AUDIO_CH2_ENABLE;
	info->display = DISPLAY_ENABLE;

	/* seek to frame 1 to start with */
	set_state(info, LASERDISC_LOADING, GENERIC_RESET_SPEED, 1);

	/* reset the I/O lines */
	for (i = 0; i < LASERDISC_INPUT_LINES; i++)
		info->linein[i] = CLEAR_LINE;
	for (i = 0; i < LASERDISC_OUTPUT_LINES; i++)
		info->lineout[i] = CLEAR_LINE;

	/* reset callbacks */
	info->writedata = NULL;
	memset(info->writeline, 0, sizeof(info->writeline));
	info->readdata = NULL;
	memset(info->readline, 0, sizeof(info->readline));
	info->statechanged = NULL;

	/* each player can init */
	switch (info->type)
	{
		case LASERDISC_TYPE_PR7820:
			pr7820_init(info);
			break;

		case LASERDISC_TYPE_LDV1000:
			ldv1000_init(info);
			break;

		case LASERDISC_TYPE_LDP1450:
			ldp1450_init(info);
			break;

		case LASERDISC_TYPE_PR8210:
			pr8210_init(info);
			break;

		case LASERDISC_TYPE_22VP932:
			vp932_init(info);
			break;

		default:
			fatalerror("Invalid laserdisc player type!");
			break;
	}

	/* default to track 1 */
	info->curfractrack = ONE_TRACK;
}


/*-------------------------------------------------
    laserdisc_exit - cleanup
-------------------------------------------------*/

void laserdisc_exit(laserdisc_info *info)
{
	/* make sure all async operations have completed */
	if (info->disc != NULL)
		chd_async_complete(info->disc);
}


/*-------------------------------------------------
    laserdisc_vsync - call this once per field
    on the VSYNC signal
-------------------------------------------------*/

void laserdisc_vsync(laserdisc_info *info)
{
	UINT8 origstate = info->state;
	UINT8 hittarget;

	/* remember the time */
	info->lastvsynctime = timer_get_time();

	/* if we're holding, stay in this state until finished */
	if (info->holdfinished.seconds != 0 || info->holdfinished.attoseconds != 0)
	{
		if (attotime_compare(info->lastvsynctime, info->holdfinished) < 0)
			return;
		info->state = info->postholdstate;
		info->curfracspeed = info->postholdfracspeed;
		info->holdfinished.seconds = 0;
		info->holdfinished.attoseconds = 0;
	}

	/* wait for previous read and decode to finish */
	process_track_data(info);

	/* update our position for this field */
	hittarget = update_position(info);

	/* switch off the state */
	switch (info->state)
	{
		/* parked: do nothing */
		case LASERDISC_EJECTED:
		case LASERDISC_EJECTING:
		case LASERDISC_LOADED:
		case LASERDISC_PARKED:
			break;

		/* playing: if we hit our target, indicate so; if we hit the beginning/end, stop */
		case LASERDISC_STOPPED:
		case LASERDISC_AUTOSTOPPED:
		case LASERDISC_SEARCH_FINISHED:
		case LASERDISC_PLAYING_FORWARD:
		case LASERDISC_PLAYING_REVERSE:
		case LASERDISC_PLAYING_SLOW_FORWARD:
		case LASERDISC_PLAYING_SLOW_REVERSE:
		case LASERDISC_PLAYING_FAST_FORWARD:
		case LASERDISC_PLAYING_FAST_REVERSE:
		case LASERDISC_STEPPING_FORWARD:
		case LASERDISC_STEPPING_REVERSE:
		case LASERDISC_SCANNING_FORWARD:
		case LASERDISC_SCANNING_REVERSE:

			/* autostop if we hit the target frame */
			if (hittarget)
				set_state(info, LASERDISC_AUTOSTOPPED, STOP_SPEED, NULL_TARGET_FRAME);
			break;

		/* loading; keep searching until we hit the target, then go into the stopped state */
		case LASERDISC_LOADING:
		case LASERDISC_SPINUP:

			/* if we hit the target, go into search finished state */
			if (hittarget)
				set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
			break;

		/* searching; keep seeking until we hit the target */
		case LASERDISC_SEARCHING_FRAME:

			/* if we hit the target, go into search finished state */
			if (hittarget)
				set_state(info, LASERDISC_SEARCH_FINISHED, STOP_SPEED, NULL_TARGET_FRAME);
			break;
	}

	/* if the state changed, notify */
	if (info->state != origstate)
	{
		/* on a state change, implicity round to the nearest fraction */
		info->curfractrack = INT_TO_FRAC(FRAC_TO_INT(info->curfractrack));
		if (!(info->fieldnum & 1))
			info->curfractrack += FRAC_ONE / 2;

		/* notify the disc handler */
		if (info->statechanged != NULL)
			(*info->statechanged)(info, origstate);
	}

	/* start reading the track data for the next round */
	info->fieldnum++;
	read_track_data(info);
}


/*-------------------------------------------------
    laserdisc_describe_state - return a text
    string describing the current state
-------------------------------------------------*/

const char *laserdisc_describe_state(laserdisc_info *info)
{
	static const struct
	{
		laserdisc_state state;
		const char *string;
	} state_strings[] =
	{
		{ LASERDISC_EJECTED, "Ejected" },
		{ LASERDISC_EJECTING, "Ejecting" },
		{ LASERDISC_LOADED, "Loaded" },
		{ LASERDISC_LOADING, "Loading" },
		{ LASERDISC_SPINUP, "Spinning Up" },
		{ LASERDISC_PARKED, "Parked" },
		{ LASERDISC_SEARCHING_FRAME, "Searching Frame" },
		{ LASERDISC_SEARCH_FINISHED, "Search Finished" },
		{ LASERDISC_STOPPED, "Stopped" },
		{ LASERDISC_AUTOSTOPPED, "Autostopped" },
		{ LASERDISC_PLAYING_FORWARD, "Playing Forward" },
		{ LASERDISC_PLAYING_REVERSE, "Playing Reverse" },
		{ LASERDISC_PLAYING_SLOW_FORWARD, "Playing Slow Forward x" },
		{ LASERDISC_PLAYING_SLOW_REVERSE, "Playing Slow Reverse x" },
		{ LASERDISC_PLAYING_FAST_FORWARD, "Playing Fast Forward x" },
		{ LASERDISC_PLAYING_FAST_REVERSE, "Playing Fast Reverse x" },
		{ LASERDISC_STEPPING_FORWARD, "Stepping Forward" },
		{ LASERDISC_STEPPING_REVERSE, "Stepping Reverse" },
		{ LASERDISC_SCANNING_FORWARD, "Scanning Forward" },
		{ LASERDISC_SCANNING_REVERSE, "Scanning Reverse" }
	};
	const char *description = "Unknown";
	int i;

	/* find the string */
	for (i = 0; i < ARRAY_LENGTH(state_strings); i++)
		if (state_strings[i].state == info->state)
			description = state_strings[i].string;

	/* construct the string */
	if (description[strlen(description) - 1] == 'x')
		sprintf(info->text, "%05d (%s%.1f)", FRAC_TO_INT(info->curfractrack), description, (float)info->curfracspeed / (float)INT_TO_FRAC(1));
	else
		sprintf(info->text, "%05d (%s)", FRAC_TO_INT(info->curfractrack), description);
	return info->text;
}


/*-------------------------------------------------
    laserdisc_data_w - write data to the given
    laserdisc player
-------------------------------------------------*/

void laserdisc_data_w(laserdisc_info *info, UINT8 data)
{
	UINT8 prev = info->datain;
	info->datain = data;

	/* call through to the player-specific write handler */
	if (info->writedata != NULL)
		(*info->writedata)(info, prev, data);
}


/*-------------------------------------------------
    laserdisc_line_w - control an input line
-------------------------------------------------*/

void laserdisc_line_w(laserdisc_info *info, UINT8 line, UINT8 newstate)
{
	assert(line < LASERDISC_INPUT_LINES);
	assert(newstate == ASSERT_LINE || newstate == CLEAR_LINE || newstate == PULSE_LINE);

	/* assert */
	if (newstate == ASSERT_LINE || newstate == PULSE_LINE)
	{
		if (info->linein[line] != ASSERT_LINE)
		{
			/* call through to the player-specific line handler */
			if (info->writeline[line] != NULL)
				(*info->writeline[line])(info, ASSERT_LINE);
		}
		info->linein[line] = ASSERT_LINE;
	}

	/* deassert */
	if (newstate == CLEAR_LINE || newstate == PULSE_LINE)
	{
		if (info->linein[line] != CLEAR_LINE)
		{
			/* call through to the player-specific line handler */
			if (info->writeline[line] != NULL)
				(*info->writeline[line])(info, CLEAR_LINE);
		}
		info->linein[line] = CLEAR_LINE;
	}
}


/*-------------------------------------------------
    laserdisc_data_r - return the current
    data byte
-------------------------------------------------*/

UINT8 laserdisc_data_r(laserdisc_info *info)
{
	UINT8 result = info->dataout;

	/* call through to the player-specific data handler */
	if (info->readdata != NULL)
		result = (*info->readdata)(info);

	return result;
}


/*-------------------------------------------------
    laserdisc_line_r - return the current state
    of an output line
-------------------------------------------------*/

UINT8 laserdisc_line_r(laserdisc_info *info, UINT8 line)
{
	UINT8 result;

	assert(line < LASERDISC_OUTPUT_LINES);
	result = info->lineout[line];

	/* call through to the player-specific data handler */
	if (info->readline[line] != NULL)
		result = (*info->readline[line])(info);

	return result;
}


/*-------------------------------------------------
    laserdisc_get_video - return the current
    video frame
-------------------------------------------------*/

UINT32 laserdisc_get_video(laserdisc_info *info, mame_bitmap **bitmap)
{
	int frameindex;

	/* determine the most recent live set of frames */
	frameindex = info->videoindex;
	if (info->videofields[frameindex] < 2)
		frameindex ^= 1;

	/* if no video present, return the empty frame */
	if (!video_active(info) || info->videofields[frameindex] < 2)
	{
		*bitmap = info->emptyframe;
		return 0;
	}
	else
	{
		*bitmap = info->videoframe[frameindex];
		return info->videoframenum[frameindex];
	}
}


/*-------------------------------------------------
    laserdisc_get_field_code - return raw field
    information read from the disc
-------------------------------------------------*/

UINT32 laserdisc_get_field_code(laserdisc_info *info, UINT8 code)
{
	int field = (info->fieldnum & 1) ^ 1;

	/* if no video present, return */
	if (!video_active(info))
		return 0;

	switch (code)
	{
		case LASERDISC_CODE_FRAME_FLAGS:
			return info->metadata[field].frameflags;

		case LASERDISC_CODE_WHITE_FLAG:
			return info->metadata[field].whiteflag;

		case LASERDISC_CODE_LINE16:
			return info->metadata[field].line16;

		case LASERDISC_CODE_LINE17:
			return info->metadata[field].line17;

		case LASERDISC_CODE_LINE18:
			return info->metadata[field].line18;
	}

	return 0;
}



/***************************************************************************
    GENERIC HELPER FUNCTIONS
***************************************************************************/

/*-------------------------------------------------
    process_number - scan a list of bytecodes
    and treat them as single digits if we get
    a match
-------------------------------------------------*/

static int process_number(laserdisc_info *info, UINT8 byte, const UINT8 numbers[])
{
	int value;

	/* look for a match in the list of number values; if we got one, append it to the parameter */
	for (value = 0; value < 10; value++)
		if (numbers[value] == byte)
		{
			info->parameter = (info->parameter == -1) ? value : (info->parameter * 10 + value);
			return TRUE;
		}

	/* no match; return FALSE */
	return FALSE;
}


/*-------------------------------------------------
    update_position - update the head position
    for this VSYNC
-------------------------------------------------*/

static int update_position(laserdisc_info *info)
{
	UINT32 tracknum = FRAC_TO_INT(info->curfractrack);
	UINT32 fieldnum = info->fieldnum & 1;
	INT32 speed = info->curfracspeed;
	INT32 framedelta;

	/* if video isn't active, we don't need to be careful about fields and frames */
	if (!video_active(info))
	{
		int frame = frame_from_metadata(&info->metadata[fieldnum]);
		int direction;

		/* if we have no target, don't do anything */
		if (info->targetframe == 0)
			return TRUE;

		/* if we didn't get any frame information this field, move onto the next */
		if (frame == -1)
		{
			/* if we're in the lead-in section, advance more aggressively */
			if (info->metadata[fieldnum].line17 == 0x88ffff || info->metadata[fieldnum].line18 == 0x88ffff)
				add_to_current_track(info, 10 * ONE_TRACK);

			/* if we're on the second field of a frame, the next frame is on the next track */
			/* but don't do it if we're seeking to frame 1, since that might be the very first frame */
			else if (fieldnum == 1 && (info->last_frame == 1 || info->targetframe != 1))
				add_to_current_track(info, ONE_TRACK);
			return FALSE;
		}

		/* if we hit our frame, we're done */
		if (frame == info->targetframe)
		{
			info->curfractrack = INT_TO_FRAC(tracknum);
			return TRUE;
		}

		/* determine the frame delta and direction */
		framedelta = info->targetframe - frame;
		direction = (framedelta < 0) ? -1 : 1;

		/* if we're going backwards, go back at least 2 frames since we tend to move forward */
		if (framedelta == -1)
			framedelta = -2;

		/* if we're only going forward one frame, only advance if we're on the second field */
		/* otherwise, we might overshoot */
		else if (framedelta == 1 && fieldnum == 0)
			framedelta = 0;

		/* scale up to fractional bits */
		framedelta = INT_TO_FRAC(abs(framedelta));

		/* determine the stepdelta */
		add_to_current_track(info, MIN(framedelta, speed / 2) * direction);
		return FALSE;
	}

	/* only advance on the second field */
	if (fieldnum != 1)
		return FALSE;

	/* if we're moving backwards, advance back until we hit track 1 or the target */
	if (speed < 0)
	{
		/* if we've hit the target, stop now */
		if (info->targetframe != 0 && info->last_frame <= info->targetframe)
		{
			info->curfractrack = INT_TO_FRAC(tracknum);
			return TRUE;
		}

		/* otherwise, clamp our delta so we don't overshoot */
		else
		{
			framedelta = INT_TO_FRAC((info->targetframe == 0) ? -1000 : (info->targetframe - info->last_frame));
			add_to_current_track(info, MAX(framedelta, speed));
		}
	}

	/* if we're moving forwards, advance forward until we hit the max track or the target */
	else if (speed > 0)
	{
		/* if we've hit the target, stop now */
		if (info->targetframe != 0 && info->last_frame >= info->targetframe)
		{
			info->curfractrack = INT_TO_FRAC(tracknum);
			return TRUE;
		}

		/* otherwise, clamp our delta so we don't overshoot */
		else
		{
			framedelta = INT_TO_FRAC((info->targetframe == 0) ? 1000 : (info->targetframe - info->last_frame));
			add_to_current_track(info, MIN(framedelta, speed));
		}
	}
	return FALSE;
}


/*-------------------------------------------------
    read_track_data - read and process data for
    a particular video track
-------------------------------------------------*/

static void read_track_data(laserdisc_info *info)
{
	UINT32 tracknum = FRAC_TO_INT(info->curfractrack);
	UINT32 fieldnum = info->fieldnum & 1;
	UINT32 chdhunk = (tracknum - 1) * 2 + fieldnum;
	chd_error err;

	/* initialize the decompression structure */
	info->avconfig.decode_mask = AVCOMP_DECODE_META;
	info->avconfig.video_buffer = NULL;
	info->avconfig.video_xor = BYTE_XOR_LE(0);
	info->avconfig.audio_xor = BYTE_XOR_BE(0);

	/* if video is active, enable video decoding */
	if (video_active(info))
	{
		/* if the flags on the previous field indicate that the next field
           is part of the same frame, then keep steady; otherwise, bump to
           the next videoindex */
		if (info->videofields[info->videoindex] > 0 && !(info->metadata[fieldnum ^ 1].frameflags & FRAMEFLAG_NEXT_SAME_FRAME))
		{
			info->videoindex ^= 1;
			info->videofields[info->videoindex] = 0;
		}

		/* enable video and configure it to read into the videoframe */
		info->avconfig.decode_mask |= AVCOMP_DECODE_VIDEO;
		info->avconfig.video_buffer = (UINT8 *)BITMAP_ADDR16(info->videoframe[info->videoindex], fieldnum, 0);
		info->avconfig.video_stride = 4 * info->videoframe[info->videoindex]->rowpixels;
	}
	else
		info->videofields[0] = info->videofields[1] = 0;

	/* if audio is active, enable audio decoding */
	if (audio_channel_active(info, 0))
		info->avconfig.decode_mask |= AVCOMP_DECODE_AUDIO(0);
	if (audio_channel_active(info, 1))
		info->avconfig.decode_mask |= AVCOMP_DECODE_AUDIO(1);

	/* configure the codec and then read */
	if (info->disc != NULL)
	{
		err = chd_codec_config(info->disc, AV_CODEC_DECOMPRESS_CONFIG, &info->avconfig);
		if (err == CHDERR_NONE)
		{
			err = chd_read_async(info->disc, chdhunk, info->framebuffer);
			info->readpending = TRUE;
		}
	}
}


/*-------------------------------------------------
    process_track_data - process data from a
    track after it has been read
-------------------------------------------------*/

static void process_track_data(laserdisc_info *info)
{
	UINT32 tracknum = FRAC_TO_INT(info->curfractrack);
	UINT32 fieldnum = info->fieldnum & 1;
	const UINT8 *rawdata = NULL;
	const INT16 *sampsource[2];
	int frame, chapter;
	chd_error chderr;
	int samples;

	/* wait for the async operation to complete */
	if (info->disc != NULL && info->readpending)
	{
		/* complete the async operation */
		chderr = chd_async_complete(info->disc);
		if (chderr == CHDERR_NONE || chderr == CHDERR_NO_ASYNC_OPERATION)
			rawdata = info->framebuffer;
	}
	info->readpending = FALSE;

	/* parse the metadata */
	parse_metadata(rawdata, tracknum, fieldnum, &info->metadata[fieldnum]);

	/* update the last seen frame and chapter */
	frame = frame_from_metadata(&info->metadata[fieldnum]);
	if (frame != -1)
		info->last_frame = frame;
	chapter = chapter_from_metadata(&info->metadata[fieldnum]);
	if (chapter != -1)
		info->last_chapter = chapter;

	/* update video info */
	if (rawdata != NULL && (info->avconfig.decode_mask & AVCOMP_DECODE_VIDEO))
	{
		info->videofields[info->videoindex]++;
		info->videoframenum[info->videoindex] = info->last_frame;
	}

	/* stream the audio into our ring buffers */
	if (rawdata != NULL)
	{
		samples = (rawdata[6] << 8) + rawdata[7];
		sampsource[0] = (const INT16 *)(rawdata + 12 + rawdata[4]) + 0 * samples;
		sampsource[1] = sampsource[0] + samples;

		/* loop until all samples are copied */
		while (samples != 0)
		{
			int samples_to_copy = MIN(info->audiobufsize - info->audiobufin, samples);
			int channum;

			/* don't overrun the output pointer */
			if (info->audiobufout > info->audiobufin)
			{
				samples_to_copy = MIN(samples_to_copy, info->audiobufout - info->audiobufin);
				if (samples_to_copy == 0)
					break;
			}

			/* for reach channel, copy the data or clear to 0 */
			for (channum = 0; channum < 2; channum++)
			{
				if (audio_channel_active(info, channum))
				{
					memcpy(&info->audiobuffer[channum][info->audiobufin], sampsource[channum], samples_to_copy * 2);
					sampsource[channum] += samples_to_copy;
				}
				else
					memset(&info->audiobuffer[channum][info->audiobufin], 0, samples_to_copy * 2);
			}
			samples -= samples_to_copy;

			/* point past the data */
			info->audiobufin += samples_to_copy;
			if (info->audiobufin >= info->audiobufsize)
				info->audiobufin = 0;
		}
	}
}


/*-------------------------------------------------
    parse_metadata - parse raw metadata into
    something more useful
-------------------------------------------------*/

static int parse_metadata(const UINT8 *rawdata, UINT32 track, UINT8 which, field_metadata *metadata)
{
	/* verify we have data to parse */
	if (rawdata == NULL || rawdata[4] < 1)
		goto fakeit;

	/* first byte is the version */
	metadata->version = rawdata[12+0];

	/* version 1 data */
	if (metadata->version == 2 && rawdata[4] == 12)
	{
		/*
            1 byte = version
            1 byte = internal flags
               D0 = prev field is same frame
               D1 = next field is same frame
            1 byte = white flag
            3 bytes = line 16 Philips code
            3 bytes = line 17 Philips code
            3 bytes = line 18 Philips code
        */
		metadata->frameflags = rawdata[12+1];
		metadata->whiteflag = rawdata[12+2];
		metadata->line16 = (rawdata[12+3] << 16) | (rawdata[12+4] << 8) | rawdata[12+5];
		metadata->line17 = (rawdata[12+6] << 16) | (rawdata[12+7] << 8) | rawdata[12+8];
		metadata->line18 = (rawdata[12+9] << 16) | (rawdata[12+10] << 8) | rawdata[12+11];
		return TRUE;
	}

fakeit:
	metadata->frameflags = (which == 0) ? 0x02 : 0x01;
	metadata->whiteflag = (which == 0) ? 1 : 0;
	metadata->line16 = 0x000000;
	if (which == 0)
		metadata->line17 = metadata->line18 = 0xf80000 | (((track / 10000) % 10) << 16) | (((track / 1000) % 10) << 12) | (((track / 100) % 10) << 8) | (((track / 10) % 10) << 4) | (track % 10);
	else
		metadata->line17 = metadata->line18 = 0x000000;
	return FALSE;
}


/*-------------------------------------------------
    custom_start - custom audio start
    for laserdiscs
-------------------------------------------------*/

static void *custom_start(int clock, const struct CustomSound_interface *config)
{
	sound_token *token = auto_malloc(sizeof(*token));
	token->stream = stream_create(0, 2, 44100, token, custom_stream_callback);
	token->info = NULL;
	return token;
}


/*-------------------------------------------------
    custom_stream_callback - audio streamer
    for laserdiscs
-------------------------------------------------*/

static void custom_stream_callback(void *param, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	sound_token *token = param;
	laserdisc_info *info = token->info;
	stream_sample_t *dst0 = outputs[0];
	stream_sample_t *dst1 = outputs[1];
	int samples_avail = 0;

	/* see if we have enough samples to fill the buffer; if not, drop out */
	if (info != NULL)
	{
		samples_avail = info->audiobufin - info->audiobufout;
		if (samples_avail < 0)
			samples_avail += info->audiobufsize;
	}

	/* if no attached info, just clear the buffers */
	if (samples_avail < samples)
	{
		memset(dst0, 0, samples * sizeof(dst0[0]));
		memset(dst1, 0, samples * sizeof(dst1[0]));
	}

	/* otherwise, stream from our buffer */
	else
	{
		const INT16 *buffer0 = info->audiobuffer[0];
		const INT16 *buffer1 = info->audiobuffer[1];
		int sampout = info->audiobufout;

		/* copy samples */
		while (sampout != info->audiobufin && samples-- > 0)
		{
			*dst0++ = buffer0[sampout];
			*dst1++ = buffer1[sampout];
			sampout++;
			if (sampout >= info->audiobufsize)
				sampout = 0;
		}
		info->audiobufout = sampout;

		/* clear out the rest of the buffer */
		if (samples > 0)
		{
			int sampout = (info->audiobufout == 0) ? info->audiobufsize - 1 : info->audiobufout - 1;
			stream_sample_t fill0 = buffer0[sampout];
			stream_sample_t fill1 = buffer1[sampout];

			while (samples-- > 0)
			{
				*dst0++ = fill0;
				*dst1++ = fill1;
			}
		}
	}
}



/***************************************************************************
    PIONEER PR-7820 IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    pr7820_init - Pioneer PR-7820-specific
    initialization
-------------------------------------------------*/

static void pr7820_init(laserdisc_info *info)
{
	pr7820_info *pr7820 = &info->u.pr7820;

	/* set up the write callbacks */
	info->writeline[LASERDISC_LINE_ENTER] = pr7820_enter_w;

	/* set up the read callbacks */
	info->readdata = pr7820_status_r;
	info->readline[LASERDISC_LINE_READY] = pr7820_ready_r;

	/* do a soft reset */
	pr7820->configspeed = PLAY_SPEED / 2;
	pr7820_soft_reset(info);
}


/*-------------------------------------------------
    pr7820_soft_reset - Pioneer PR-7820-specific
    soft reset
-------------------------------------------------*/

static void pr7820_soft_reset(laserdisc_info *info)
{
	pr7820_info *pr7820 = &info->u.pr7820;

	info->audio = AUDIO_CH1_ENABLE  | AUDIO_CH2_ENABLE;
	info->display = 0;
	pr7820->mode = PR7820_MODE_MANUAL;
	pr7820->activereg = 0;
	write_16bits_to_ram_be(pr7820->ram, 0, 1);
}


/*-------------------------------------------------
    pr7820_enter_w - write callback when the
    ENTER state is asserted
-------------------------------------------------*/

static void pr7820_enter_w(laserdisc_info *info, UINT8 newstate)
{
	static const UINT8 numbers[10] = { 0x3f, 0x0f, 0x8f, 0x4f, 0x2f, 0xaf, 0x6f, 0x1f, 0x9f, 0x5f };
	pr7820_info *pr7820 = &info->u.pr7820;
	UINT8 data = (pr7820->mode == PR7820_MODE_AUTOMATIC) ? pr7820->ram[pr7820->curpc++ % 1024] : info->datain;

	/* we only care about assertions */
	if (newstate != ASSERT_LINE)
		return;

	/* if we're in program mode, just write data */
	if (pr7820->mode == PR7820_MODE_PROGRAM && data != 0xef)
	{
		pr7820->ram[info->parameter++ % 1024] = data;
		return;
	}

	/* look for and process numbers */
	if (process_number(info, data, numbers))
		return;

	/* handle commands */
	switch (data)
	{
		case 0x7f:  CMDPRINTF(("pr7820: %d Recall\n", info->parameter));
			/* set the active register */
			pr7820->activereg = info->parameter;
			break;

		case 0xa0:
		case 0xa1:
		case 0xa2:
		case 0xa3:	CMDPRINTF(("pr7820: Direct audio control %d\n", data & 0x03));
			/* control both channels directly */
			info->audio = 0;
			if (data & 0x01)
				info->audio |= AUDIO_CH1_ENABLE;
			if (data & 0x02)
				info->audio |= AUDIO_CH2_ENABLE;
			break;

		case 0xbf:	CMDPRINTF(("pr7820: %d Halt\n", info->parameter));
			/* stop automatic mode */
			pr7820->mode = PR7820_MODE_MANUAL;
			break;

		case 0xcc:	CMDPRINTF(("pr7820: Load\n"));
			/* load program from disc -- not implemented */
			break;

		case 0xcf:	CMDPRINTF(("pr7820: %d Branch\n", info->parameter));
			/* branch to a new PC */
			if (pr7820->mode == PR7820_MODE_AUTOMATIC)
				pr7820->curpc = (info->parameter == -1) ? 0 : info->parameter;
			break;

		case 0xdf:	CMDPRINTF(("pr7820: %d Write program\n", info->parameter));
			/* enter program mode */
			pr7820->mode = PR7820_MODE_PROGRAM;
			break;

		case 0xe1:  CMDPRINTF(("pr7820: Soft Reset\n"));
			/* soft reset */
			pr7820_soft_reset(info);
			break;

		case 0xe3:  CMDPRINTF(("pr7820: Display off\n"));
			/* turn off frame display */
			info->display = 0x00;
			break;

		case 0xe4:  CMDPRINTF(("pr7820: Display on\n"));
			/* turn on frame display */
			info->display = 0x01;
			break;

		case 0xe5:  CMDPRINTF(("pr7820: Audio 2 off\n"));
			/* turn off audio channel 2 */
			info->audio &= ~AUDIO_CH2_ENABLE;
			break;

		case 0xe6:  CMDPRINTF(("pr7820: Audio 2 on\n"));
			/* turn on audio channel 2 */
			info->audio |= AUDIO_CH2_ENABLE;
			break;

		case 0xe7:  CMDPRINTF(("pr7820: Audio 1 off\n"));
			/* turn off audio channel 1 */
			info->audio &= ~AUDIO_CH1_ENABLE;
			break;

		case 0xe8:  CMDPRINTF(("pr7820: Audio 1 on\n"));
			/* turn on audio channel 1 */
			info->audio |= AUDIO_CH1_ENABLE;
			break;

		case 0xe9:	CMDPRINTF(("pr7820: %d Dump RAM\n", info->parameter));
			/* not implemented */
			break;

		case 0xea:	CMDPRINTF(("pr7820: %d Dump frame\n", info->parameter));
			/* not implemented */
			break;

		case 0xeb:	CMDPRINTF(("pr7820: %d Dump player status\n", info->parameter));
			/* not implemented */
			break;

		case 0xef:	CMDPRINTF(("pr7820: %d End program\n", info->parameter));
			/* exit programming mode */
			pr7820->mode = PR7820_MODE_MANUAL;
			break;

		case 0xf0:	CMDPRINTF(("pr7820: %d Decrement reg\n", info->parameter));
			/* decrement register; if we hit 0, skip past the next branch statement */
			if (pr7820->mode == PR7820_MODE_AUTOMATIC)
			{
				UINT16 tempreg = read_16bits_from_ram_be(pr7820->ram, (info->parameter * 2) % 1024);
				tempreg = (tempreg == 0) ? 0 : tempreg - 1;
				write_16bits_to_ram_be(pr7820->ram, (info->parameter * 2) % 1024, tempreg);
				if (tempreg == 0)
					while (pr7820->ram[pr7820->curpc++ % 1024] != 0xcf) ;
			}
			break;

		case 0xf1:  CMDPRINTF(("pr7820: %d Display\n", info->parameter));
			/* toggle or set the frame display */
			info->display = (info->parameter == -1) ? !info->display : (info->parameter & 1);
			break;

		case 0xf2:	CMDPRINTF(("pr7820: %d Slow forward\n", info->parameter));
			/* play forward at slow speed (controlled by lever on the front of the player) */
			if (laserdisc_ready(info))
			{
				if (info->parameter != -1)
					set_state(info, LASERDISC_PLAYING_SLOW_FORWARD, pr7820->configspeed, info->parameter);
				else
					set_state(info, LASERDISC_PLAYING_SLOW_FORWARD, pr7820->configspeed, NULL_TARGET_FRAME);
			}
			break;

		case 0xf3:  CMDPRINTF(("pr7820: %d Autostop\n", info->parameter));
			/* play to a particular location and stop there */
			if (laserdisc_ready(info))
			{
				INT32 targetframe = info->parameter;

				if (targetframe == -1)
					targetframe = read_16bits_from_ram_be(pr7820->ram, (pr7820->activereg * 2) % 1024);
				pr7820->activereg++;

				if (targetframe > info->last_frame)
					set_state(info, LASERDISC_PLAYING_FORWARD, PLAY_SPEED, targetframe);
				else
					set_state(info, LASERDISC_SEARCHING_FRAME, PR7820_SEARCH_SPEED, targetframe);
			}
			break;

		case 0xf4:  CMDPRINTF(("pr7820: %d Audio track 1\n", info->parameter));
			/* toggle or set the state of audio channel 1 */
			if (info->parameter == -1)
				info->audio ^= AUDIO_CH1_ENABLE;
			else
				info->audio = (info->audio & ~AUDIO_CH1_ENABLE) | ((info->parameter & 1) ? AUDIO_CH1_ENABLE : 0);
			break;

		case 0xf5:	CMDPRINTF(("pr7820: %d Store\n", info->parameter));
			/* store either the current frame number or an explicit value into the active register */
			if (info->parameter == -1)
				write_16bits_to_ram_be(pr7820->ram, (pr7820->activereg * 2) % 1024, info->last_frame);
			else
				write_16bits_to_ram_be(pr7820->ram, (pr7820->activereg * 2) % 1024, info->parameter);
			pr7820->activereg++;
			break;

		case 0xf6:	CMDPRINTF(("pr7820: Step forward\n"));
			/* step forward one frame */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_FORWARD, STOP_SPEED, NULL_TARGET_FRAME);
				add_to_current_track(info, ONE_TRACK);
			}
			break;

		case 0xf7:  CMDPRINTF(("pr7820: %d Search\n", info->parameter));
			/* search to a particular frame number */
			if (laserdisc_ready(info))
			{
				INT32 targetframe = info->parameter;

				if (targetframe == -1)
					targetframe = read_16bits_from_ram_be(pr7820->ram, (pr7820->activereg * 2) % 1024);
				pr7820->activereg++;

				if (targetframe == 0)
					targetframe = 1;
				set_state(info, LASERDISC_SEARCHING_FRAME, PR7820_SEARCH_SPEED, targetframe);
			}
			break;

		case 0xf8:	CMDPRINTF(("pr7820: %d Input\n", info->parameter));
			/* wait for user input -- not implemented */
			break;

		case 0xf9:	CMDPRINTF(("pr7820: Reject\n"));
			/* eject the disc */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_EJECTING, STOP_SPEED, NULL_TARGET_FRAME);
				set_hold_state(info, GENERIC_LOAD_TIME, LASERDISC_EJECTED, STOP_SPEED);
			}
			break;

		case 0xfa:	CMDPRINTF(("pr7820: %d Slow reverse\n", info->parameter));
			/* play backwards at slow speed (controlled by lever on the front of the player) */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_REVERSE, -pr7820->configspeed, (info->parameter != -1) ? info->parameter : NULL_TARGET_FRAME);
			break;

		case 0xfb:	CMDPRINTF(("pr7820: %d Stop/Wait\n", info->parameter));
			/* pause at the current location for a fixed amount of time (in 1/10ths of a second) */
			if (laserdisc_ready(info))
			{
				laserdisc_state prevstate = info->state;
				INT32 prevspeed = info->curfracspeed;
				set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
				if (info->parameter != -1)
					set_hold_state(info, double_to_attotime(info->parameter * 0.1), prevstate, prevspeed);
			}
			break;

		case 0xfc:  CMDPRINTF(("pr7820: %d Audio track 2\n", info->parameter));
			/* toggle or set the state of audio channel 2 */
			if (info->parameter == -1)
				info->audio ^= AUDIO_CH2_ENABLE;
			else
				info->audio = (info->audio & ~AUDIO_CH2_ENABLE) | ((info->parameter & 1) ? AUDIO_CH2_ENABLE : 0);
			break;

		case 0xfd:  CMDPRINTF(("pr7820: Play\n"));
			/* begin playing at regular speed, or load the disc if it is parked */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_FORWARD, PLAY_SPEED, NULL_TARGET_FRAME);
			else
			{
				if (info->state == LASERDISC_PARKED)
				{
					set_state(info, LASERDISC_SPINUP, STOP_SPEED, NULL_TARGET_FRAME);
					set_hold_state(info, GENERIC_SPINUP_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
				}
				else
				{
					set_state(info, LASERDISC_LOADING, STOP_SPEED, NULL_TARGET_FRAME);
					set_hold_state(info, GENERIC_LOAD_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
				}

				info->curfractrack = ONE_TRACK;
			}
			break;

		case 0xfe:	CMDPRINTF(("pr7820: Step reverse\n"));
			/* step backwards one frame */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_REVERSE, STOP_SPEED, NULL_TARGET_FRAME);
				add_to_current_track(info, -ONE_TRACK);
			}
			break;

		default:	CMDPRINTF(("pr7820: %d Unknown command %02X\n", info->parameter, data));
			/* unknown command */
			break;
	}

	/* reset the parameter after executing a command */
	info->parameter = -1;
}


/*-------------------------------------------------
    pr7820_ready_r - return state of the ready
    line
-------------------------------------------------*/

static UINT8 pr7820_ready_r(laserdisc_info *info)
{
	return (info->state != LASERDISC_SEARCHING_FRAME) ? ASSERT_LINE : CLEAR_LINE;
}


/*-------------------------------------------------
    pr7820_status_r - return state of the ready
    line
-------------------------------------------------*/

static UINT8 pr7820_status_r(laserdisc_info *info)
{
	pr7820_info *pr7820 = &info->u.pr7820;

	/* top 3 bits reflect audio and display states */
	UINT8 status = (info->audio << 6) | (info->display << 5);

	/* low 5 bits reflect player states */

	/* handle program mode */
	if (pr7820->mode == PR7820_MODE_PROGRAM)
		status |= 0x0c;
	else
	{
		/* convert generic status into specific state equivalents */
		switch (info->state)
		{
			case LASERDISC_EJECTED:					status |= 0x0d;		break;
			case LASERDISC_EJECTING:				status |= 0x0d;		break;
			case LASERDISC_LOADED:					status |= 0x01;		break;
			case LASERDISC_SPINUP:					status |= 0x01;		break;
			case LASERDISC_LOADING:					status |= 0x01;		break;
			case LASERDISC_PARKED:					status |= 0x01;		break;

			case LASERDISC_SEARCHING_FRAME:			status |= 0x06;		break;
			case LASERDISC_SEARCH_FINISHED:			status |= 0x07;		break;

			case LASERDISC_STOPPED:					status |= 0x03;		break;
			case LASERDISC_AUTOSTOPPED:				status |= 0x09;		break;

			case LASERDISC_PLAYING_FORWARD:			status |= 0x02;		break;
			case LASERDISC_PLAYING_REVERSE:			status |= 0x02;		break;
			case LASERDISC_PLAYING_SLOW_FORWARD:	status |= 0x04;		break;
			case LASERDISC_PLAYING_SLOW_REVERSE:	status |= 0x05;		break;
			case LASERDISC_PLAYING_FAST_FORWARD:	status |= 0x02;		break;
			case LASERDISC_PLAYING_FAST_REVERSE:	status |= 0x02;		break;
			case LASERDISC_STEPPING_FORWARD:		status |= 0x03;		break;
			case LASERDISC_STEPPING_REVERSE:		status |= 0x03;		break;
			case LASERDISC_SCANNING_FORWARD:		status |= 0x02;		break;
			case LASERDISC_SCANNING_REVERSE:		status |= 0x02;		break;
			default:
				fatalerror("Unexpected disc state in pr7820_status_r\n");
				break;
		}
	}
	return status;
}


/*-------------------------------------------------
    pr7820_set_slow_speed - set the speed of
    "slow" playback, which is controlled by a
    slider on the device
-------------------------------------------------*/

void pr7820_set_slow_speed(laserdisc_info *info, double frame_rate_scaler)
{
	pr7820_info *pr7820 = &info->u.pr7820;
	pr7820->configspeed = PLAY_SPEED * frame_rate_scaler;
}



/***************************************************************************
    PIONEER PR-8210 IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    pr8210_init - Pioneer PR-8210-specific
    initialization
-------------------------------------------------*/

static void pr8210_init(laserdisc_info *info)
{
	/* set up the write callbacks */
	info->writeline[LASERDISC_LINE_CONTROL] = pr8210_control_w;	/* serial access */

	/* do a soft reset */
	pr8210_soft_reset(info);
}


/*-------------------------------------------------
    pr8210_soft_reset - Pioneer PR-8210-specific
    soft reset
-------------------------------------------------*/

static void pr8210_soft_reset(laserdisc_info *info)
{
	pr8210_info *pr8210 = &info->u.pr8210;

	info->audio = AUDIO_CH1_ENABLE  | AUDIO_CH2_ENABLE;
	info->display = 0;
	pr8210->mode = PR8210_MODE_GET_1ST;
	pr8210->commandtime = timer_get_time();
	pr8210->commandbits = 0;
	memset( pr8210->commandtriplet, 0, 3*sizeof(UINT16) );
	pr8210->seekstate = 0;
}


/*-------------------------------------------------
    pr8210_command - Pioneer PR-8210-specific
    command processing
-------------------------------------------------*/

static void pr8210_command(laserdisc_info *info)
{
	pr8210_info *pr8210 = &info->u.pr8210;

	/* if we don't have the entire command triplet yet, keep going */
	if ( pr8210->mode < PR8210_MODE_GET_3RD )
	{
		/* do some sanity checks on the command data */
		if ( pr8210->mode == PR8210_MODE_GET_2ND )
		{
			/* if the commands don't match, then reassign the new command to the first word, then keep fetching */
			if ( pr8210->commandtriplet[PR8210_MODE_GET_1ST] != pr8210->commandtriplet[PR8210_MODE_GET_2ND] )
			{
				pr8210->commandtriplet[PR8210_MODE_GET_1ST] = pr8210->commandtriplet[PR8210_MODE_GET_2ND];
				pr8210->mode = PR8210_MODE_GET_2ND;
				return;
			}
		}

		pr8210->mode++;
	}
	else /* we're ready to process the command */
	{
		/* do some sanity checks on the command data */
		static const UINT8 numbers[10] = { 0x01,0x11,0x09,0x19,0x05,0x15,0x0d,0x1d,0x03,0x13 };
		UINT16	cmd = pr8210->commandtriplet[PR8210_MODE_GET_1ST];

		/* more sanity checking: bit 7 must be set for a valid command */
		if ( cmd & 0x80 )
		{
			/* extract the actual command number */
			cmd = ( cmd >> 2 ) & 0x1f;

			/* look for and process numbers */
			if (!process_number(info, cmd, numbers))
			{
				switch( cmd )
				{
					case 0x00:	CMDPRINTF(("pr8210: EOC\n"));
						/* EOC marker - can be safely ignored */
						break;

					case 0x02:	CMDPRINTF(("pr8210: Slow reverse\n"));
						/* slow reverse */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_PLAYING_SLOW_REVERSE, -PR8210_SLOW_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x04:	CMDPRINTF(("pr8210: Step forward\n"));
						/* step forward */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_STEPPING_FORWARD, PR8210_STEP_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x06 :	CMDPRINTF(("pr8210: Chapter\n"));
						/* chapter -- not implemented */
						break;

					case 0x08:	CMDPRINTF(("pr8210: Scan forward\n"));
						/* scan forward */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_SCANNING_FORWARD, PR8210_SCAN_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x0a:	CMDPRINTF(("pr8210: Pause\n"));
						/* still picture */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x0b :	CMDPRINTF(("pr8210: Frame\n"));
						/* frame -- not implemented */
						break;

					case 0x0c:	CMDPRINTF(("pr8210: Fast reverse\n"));
						/* play reverse fast speed */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_PLAYING_FAST_REVERSE, -PR8210_FAST_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x0e:	CMDPRINTF(("pr8210: Ch1 toggle\n"));
						/* channel 1 audio toggle */
						info->audio ^= AUDIO_CH1_ENABLE;
						break;

					case 0x10:	CMDPRINTF(("pr8210: Fast forward\n"));
						/* play forward fast speed */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_PLAYING_FAST_FORWARD, PR8210_FAST_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x12:	CMDPRINTF(("pr8210: Step reverse\n"));
						/* step backwards one frame */
						if (laserdisc_ready(info))
						{
							set_state(info, LASERDISC_STEPPING_REVERSE, STOP_SPEED, NULL_TARGET_FRAME);
							add_to_current_track(info, -ONE_TRACK);
						}
						break;

					case 0x14:  CMDPRINTF(("pr8210: Play\n"));
						/* begin playing at regular speed, or load the disc if it is parked */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_PLAYING_FORWARD, PLAY_SPEED, NULL_TARGET_FRAME);
						else
						{
							/* if we're already spinning up or loading, ignore */
							if (info->state != LASERDISC_SPINUP && info->state != LASERDISC_LOADING)
							{
								if (info->state == LASERDISC_PARKED)
								{
									set_state(info, LASERDISC_SPINUP, STOP_SPEED, NULL_TARGET_FRAME);
									set_hold_state(info, GENERIC_SPINUP_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
								}
								else
								{
									set_state(info, LASERDISC_LOADING, STOP_SPEED, NULL_TARGET_FRAME);
									set_hold_state(info, GENERIC_LOAD_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
								}

								info->curfractrack = ONE_TRACK;
							}
						}
						break;

					case 0x16:	CMDPRINTF(("pr8210: Ch2 toggle\n"));
						/* channel 1 audio toggle */
						info->audio ^= AUDIO_CH2_ENABLE;
						break;

					case 0x18:	CMDPRINTF(("pr8210: Slow forward\n"));
						/* slow forward */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_PLAYING_SLOW_FORWARD, PR8210_SLOW_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x1a:	CMDPRINTF(("pr8210: Seek\n"));
						/* seek */
						if ( pr8210->seekstate )
						{
							CMDPRINTF(("pr8210: Seeking to frame:%d\n", info->parameter));
							/* we're ready to seek */
							set_state(info, LASERDISC_SEARCHING_FRAME, PR8210_SEARCH_SPEED, info->parameter);
						}
						else
						{
							/* waiting for digits indicating position */
							info->parameter = 0;
						}
						pr8210->seekstate ^=1 ;
						break;

					case 0x1c:	CMDPRINTF(("pr8210: Scan reverse\n"));
						/* scan reverse */
						if (laserdisc_ready(info))
							set_state(info, LASERDISC_SCANNING_REVERSE, -PR8210_SCAN_SPEED, NULL_TARGET_FRAME);
						break;

					case 0x1e:	CMDPRINTF(("pr8210: Reject\n"));
						/* eject the disc */
						if (laserdisc_ready(info))
						{
							set_state(info, LASERDISC_EJECTING, STOP_SPEED, NULL_TARGET_FRAME);
							set_hold_state(info, GENERIC_LOAD_TIME, LASERDISC_EJECTED, STOP_SPEED);
						}
						break;

					default:	CMDPRINTF(("pr8210: Unknown command %02X\n", cmd));
						/* unknown command */
						break;
				}
			}
		}

		/* reset our command data */
		memset( pr8210->commandtriplet, 0, 3*sizeof(UINT16) );
		pr8210->mode = PR8210_MODE_GET_1ST;
	}
}


/*-------------------------------------------------
    pr8210_command_w - write callback when the
    CONTROL line is toggled
-------------------------------------------------*/

static void pr8210_control_w(laserdisc_info *info, UINT8 data)
{
	pr8210_info *pr8210 = &info->u.pr8210;

	if ( data == ASSERT_LINE )
	{
		/* get the time difference from the last assert */
		attotime delta = attotime_sub(timer_get_time(), pr8210->commandtime);

		/* and update our internal command time */
		pr8210->commandtime = timer_get_time();

#if 0
		{
			int usecdiff = (int)(delta.attoseconds / ATTOSECONDS_IN_USEC(1));

			printf( "bitdelta = %d\n", usecdiff );
		}
#endif

		/* if the delay is less than 3 msec, we're receiving data */
		if ( delta.attoseconds < ATTOTIME_IN_MSEC(3).attoseconds )
		{
			/* 0 bit delta is 1.05 msec, 1 bit delta is 2.11 msec */
			int longpulse = ( delta.attoseconds < ATTOTIME_IN_USEC(1500).attoseconds ) ? 0 : 1;
			pr8210->commandtriplet[pr8210->mode] <<= 1;
			pr8210->commandtriplet[pr8210->mode] |= longpulse;

			/* if we received 10 bits, see what we need to do */
			if ( ++pr8210->commandbits >= 10 )
			{
				/* reset bit shift count */
				pr8210->commandbits = 0;

				/* mask just the 10 bits */
				pr8210->commandtriplet[pr8210->mode] &= 0x3ff;

				/* execute command */
				pr8210_command( info );
			}
		}
	}
}



/***************************************************************************
    PIONEER LD-V1000 IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    ldv1000_init - Pioneer LDV-1000-specific
    initialization
-------------------------------------------------*/

static void ldv1000_init(laserdisc_info *info)
{
	/* set up the write callbacks */
	info->writedata = ldv1000_data_w;

	/* set up the read callbacks */
	info->readdata = ldv1000_status_r;
	info->readline[LASERDISC_LINE_STATUS] = ldv1000_status_strobe_r;
	info->readline[LASERDISC_LINE_COMMAND] = ldv1000_command_strobe_r;

	/* do a soft reset */
	ldv1000_soft_reset(info);
}


/*-------------------------------------------------
    ldv1000_soft_reset - Pioneer LDV-1000-specific
    soft reset
-------------------------------------------------*/

static void ldv1000_soft_reset(laserdisc_info *info)
{
	ldv1000_info *ldv1000 = &info->u.ldv1000;

	info->audio = AUDIO_CH1_ENABLE | AUDIO_CH2_ENABLE;
	info->display = FALSE;
	ldv1000->mode = LDV1000_MODE_STATUS;
	ldv1000->activereg = 0;
	ldv1000->statusmask = 0xff;
	write_16bits_to_ram_be(ldv1000->ram, 0, 1);
}


/*-------------------------------------------------
    ldv1000_data_w - write callback when the
    ENTER state is written
-------------------------------------------------*/

static void ldv1000_data_w(laserdisc_info *info, UINT8 prev, UINT8 data)
{
	static const UINT8 numbers[10] = { 0x3f, 0x0f, 0x8f, 0x4f, 0x2f, 0xaf, 0x6f, 0x1f, 0x9f, 0x5f };
	ldv1000_info *ldv1000 = &info->u.ldv1000;

	/* 0xFF bytes are used for synchronization */
	if (data == 0xff)
	{
		ldv1000->statusmask = 0xff;	/* EHC 012207: reset our status mask */
		return;
	}

	/* if we're not ready, return, otherwise signal ourselves busy */
	if (ldv1000->statusmask == 0x7f)
		return;
	ldv1000->statusmask = 0x7f;

	/* look for and process numbers */
	if (process_number(info, data, numbers))
		return;

	/* handle commands */
	switch (data)
	{
		case 0x7f:  CMDPRINTF(("ldv1000: %d Recall\n", info->parameter));
			/* set the active register */
			ldv1000->activereg = info->parameter;
			/* should also display the register value */
			break;

		case 0x20:  CMDPRINTF(("ldv1000: x0 reverse (stop) - Badlands special\n"));
			/* play reverse at 0 speed (stop) */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_REVERSE, STOP_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x21:  CMDPRINTF(("ldv1000: x1/4 reverse - Badlands special\n"));
			/* play reverse at 1/4 speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_REVERSE, -PLAY_SPEED / 4, NULL_TARGET_FRAME);
			break;

		case 0x22:  CMDPRINTF(("ldv1000: x1/2 reverse - Badlands special\n"));
			/* play reverse at 1/2 speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_REVERSE, -PLAY_SPEED / 2, NULL_TARGET_FRAME);
			break;

		case 0x23:
		case 0x24:
		case 0x25:
		case 0x26:
		case 0x27:  CMDPRINTF(("ldv1000: x%d reverse - Badlands special\n", (data & 0x07) - 2));
			/* play reverse at 1-5x speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_FAST_REVERSE, -PLAY_SPEED * ((data & 0x07) - 2), NULL_TARGET_FRAME);
			break;

		case 0xa0:  CMDPRINTF(("ldv1000: x0 forward (stop)\n"));
			/* play forward at 0 speed (stop) */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_FORWARD, STOP_SPEED, NULL_TARGET_FRAME);
			break;

		case 0xa1:  CMDPRINTF(("ldv1000: x1/4 forward\n"));
			/* play forward at 1/4 speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_FORWARD, PLAY_SPEED / 4, NULL_TARGET_FRAME);
			break;

		case 0xa2:  CMDPRINTF(("ldv1000: x1/2 forward\n"));
			/* play forward at 1/2 speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_FORWARD, PLAY_SPEED / 2, NULL_TARGET_FRAME);
			break;

		case 0xa3:
		case 0xa4:
		case 0xa5:
		case 0xa6:
		case 0xa7:  CMDPRINTF(("ldv1000: x%d forward\n", (data & 0x07) - 2));
			/* play forward at 1-5x speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_FAST_FORWARD, PLAY_SPEED * ((data & 0x07) - 2), NULL_TARGET_FRAME);
			break;

		case 0xb1:
		case 0xb2:
		case 0xb3:
		case 0xb4:
		case 0xb5:
		case 0xb6:
		case 0xb7:
		case 0xb8:
		case 0xb9:
		case 0xba:  CMDPRINTF(("ldv1000: Skip forward %d0\n", data & 0x0f));
			/* skip forward */
			if (laserdisc_active(info))
			{
				/* note that this skips tracks, not frames; the track->frame count is not 1:1 */
				/* in the case of 3:2 pulldown or other effects; for now, we just ignore the diff */
				add_to_current_track(info, INT_TO_FRAC(10 * (data & 0x0f)));
			}
			break;

		case 0x31:
		case 0x32:
		case 0x33:
		case 0x34:
		case 0x35:
		case 0x36:
		case 0x37:
		case 0x38:
		case 0x39:
		case 0x3a:  CMDPRINTF(("ldv1000: Skip backwards %d0 - Badlands special\n", data & 0x0f));
			/* skip backward */
			if (laserdisc_active(info))
			{
				/* note that this skips tracks, not frames; the track->frame count is not 1:1 */
				/* in the case of 3:2 pulldown or other effects; for now, we just ignore the diff */
				add_to_current_track(info, -INT_TO_FRAC(10 * (data & 0x0f)));
			}
			break;

		case 0xbf:	CMDPRINTF(("ldv1000: %d Clear\n", info->parameter));
			/* clears register display and removes pending arguments */
			break;

		case 0xc2:	CMDPRINTF(("ldv1000: Get frame no.\n"));
			/* returns the current frame number */
			ldv1000->mode = LDV1000_MODE_GET_FRAME;
			ldv1000->readpos = 0;
			ldv1000->readtotal = 5;
			sprintf((char *)ldv1000->readbuf, "%05d", info->last_frame);
			break;

		case 0xc3:	CMDPRINTF(("ldv1000: Get 2nd display\n"));
			/* returns the data from the 2nd display line */
			ldv1000->mode = LDV1000_MODE_GET_2ND;
			ldv1000->readpos = 0;
			ldv1000->readtotal = 8;
			sprintf((char *)ldv1000->readbuf, "	 \x1c\x1c\x1c");
			break;

		case 0xc4:	CMDPRINTF(("ldv1000: Get 1st display\n"));
			/* returns the data from the 1st display line */
			ldv1000->mode = LDV1000_MODE_GET_1ST;
			ldv1000->readpos = 0;
			ldv1000->readtotal = 8;
			sprintf((char *)ldv1000->readbuf, "	 \x1c\x1c\x1c");
			break;

		case 0xc8:	CMDPRINTF(("ldv1000: Transfer memory\n"));
			/* returns the data from the 1st display line */
			ldv1000->mode = LDV1000_MODE_GET_RAM;
			ldv1000->readpos = 0;
			ldv1000->readtotal = 1024;
			break;

		case 0xcc:	CMDPRINTF(("ldv1000: Load\n"));
			/* load program from disc -- not implemented */
			break;

		case 0xcd:	CMDPRINTF(("ldv1000: Display disable\n"));
			/* disables the display of current command -- not implemented */
			break;

		case 0xce:	CMDPRINTF(("ldv1000: Display enable\n"));
			/* enables the display of current command -- not implemented */
			break;

		case 0xf0:	CMDPRINTF(("ldv1000: Scan forward\n"));
			/* scan forward */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_SCANNING_FORWARD, LDV1000_SCAN_SPEED, NULL_TARGET_FRAME);
			break;

		case 0xf1:  CMDPRINTF(("ldv1000: %d Display\n", info->parameter));
			/* toggle or set the frame display */
			info->display = (info->parameter == -1) ? !info->display : (info->parameter & 1);
			break;

		case 0xf3:  CMDPRINTF(("ldv1000: %d Autostop\n", info->parameter));
			/* play to a particular location and stop there */
			if (laserdisc_ready(info))
			{
				INT32 targetframe = info->parameter;

				if (targetframe == -1)
					targetframe = read_16bits_from_ram_be(ldv1000->ram, (ldv1000->activereg++ * 2) % 1024);

				if (targetframe > info->last_frame)
					set_state(info, LASERDISC_PLAYING_FORWARD, PLAY_SPEED, targetframe);
				else
					set_state(info, LASERDISC_SEARCHING_FRAME, PR7820_SEARCH_SPEED, targetframe);
	   		}
			break;

		case 0xf4:  CMDPRINTF(("ldv1000: %d Audio track 1\n", info->parameter));
			/* toggle or set the state of audio channel 1 */
			if (info->parameter == -1)
				info->audio ^= AUDIO_CH1_ENABLE;
			else
				info->audio = (info->audio & ~AUDIO_CH1_ENABLE) | ((info->parameter & 1) ? AUDIO_CH1_ENABLE : 0);
			break;

		case 0xf5:	CMDPRINTF(("ldv1000: %d Store\n", info->parameter));
			/* store either the current frame number or an explicit value into the active register */
			if (info->parameter == -1)
				write_16bits_to_ram_be(ldv1000->ram, (ldv1000->activereg * 2) % 1024, info->last_frame);
			else
				write_16bits_to_ram_be(ldv1000->ram, (ldv1000->activereg * 2) % 1024, info->parameter);
			ldv1000->activereg++;
			break;

		case 0xf6:	CMDPRINTF(("ldv1000: Step forward\n"));
			/* step forward one frame */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_FORWARD, STOP_SPEED, NULL_TARGET_FRAME);
				add_to_current_track(info, ONE_TRACK);
			}
			break;

		case 0xf7:  CMDPRINTF(("ldv1000: %d Search\n", info->parameter));
			/* search to a particular frame number */
			if (laserdisc_ready(info))
			{
				INT32 targetframe = info->parameter;

				if (targetframe == -1)
					targetframe = read_16bits_from_ram_be(ldv1000->ram, (ldv1000->activereg++ * 2) % 1024);
				ldv1000->activereg++;

				set_state(info, LASERDISC_SEARCHING_FRAME, LDV1000_SEARCH_SPEED, targetframe);
			}
			break;

		case 0xf8:	CMDPRINTF(("ldv1000: Scan reverse\n"));
			/* scan reverse */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_SCANNING_REVERSE, -LDV1000_SCAN_SPEED, NULL_TARGET_FRAME);
			break;

		case 0xf9:	CMDPRINTF(("ldv1000: Reject\n"));
			/* move the head to parked position, and stop rotation */
			set_state(info, LASERDISC_PARKED, STOP_SPEED, NULL_TARGET_FRAME);
			break;

		case 0xfb:	CMDPRINTF(("ldv1000: %d Stop/Wait\n", info->parameter));
			/* pause at the current location for a fixed amount of time (in 1/10ths of a second) */
			if (laserdisc_ready(info))
			{
				laserdisc_state prevstate = info->state;
				INT32 prevspeed = info->curfracspeed;
				set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
				if (info->parameter != -1)
					set_hold_state(info, double_to_attotime(info->parameter * 0.1), prevstate, prevspeed);
			}
			break;

		case 0xfc:  CMDPRINTF(("ldv1000: %d Audio track 2\n", info->parameter));
			/* toggle or set the state of audio channel 2 */
			if (info->parameter == -1)
				info->audio ^= AUDIO_CH2_ENABLE;
			else
				info->audio = (info->audio & ~AUDIO_CH2_ENABLE) | ((info->parameter & 1) ? AUDIO_CH2_ENABLE : 0);
			break;

		case 0xfd:  CMDPRINTF(("ldv1000: Play\n"));
			/* begin playing at regular speed, or load the disc if it is parked */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_FORWARD, PLAY_SPEED, NULL_TARGET_FRAME);
			else
			{
				if (info->state == LASERDISC_PARKED)
				{
					set_state(info, LASERDISC_SPINUP, STOP_SPEED, NULL_TARGET_FRAME);
					set_hold_state(info, GENERIC_SPINUP_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
				}
				else
				{
					set_state(info, LASERDISC_LOADING, STOP_SPEED, NULL_TARGET_FRAME);
					set_hold_state(info, GENERIC_LOAD_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
				}

				info->curfractrack = ONE_TRACK;
			}
			break;

		case 0xfe:	CMDPRINTF(("ldv1000: Step reverse\n"));
			/* step backwards one frame */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_REVERSE, STOP_SPEED, NULL_TARGET_FRAME);
				add_to_current_track(info, -ONE_TRACK);
			}
			break;

		default:	CMDPRINTF(("ldv1000: %d Unknown command %02X\n", info->parameter, data));
			/* unknown command */
			break;
	}

	/* reset the parameter after executing a command */
	info->parameter = -1;
}


/*-------------------------------------------------
    ldv1000_status_strobe_r - return state of the
    status strobe
-------------------------------------------------*/

static UINT8 ldv1000_status_strobe_r(laserdisc_info *info)
{
	/* the status strobe is asserted (active low) 500-650usec after VSYNC */
	/* for a duration of 26usec; we pick 600-626usec */
	attotime delta = attotime_sub(timer_get_time(), info->lastvsynctime);
	if (delta.attoseconds >= ATTOTIME_IN_USEC(600).attoseconds &&
		delta.attoseconds < ATTOTIME_IN_USEC(626).attoseconds)
		return ASSERT_LINE;

	return CLEAR_LINE;
}


/*-------------------------------------------------
    ldv1000_command_strobe_r - return state of the
    command strobe
-------------------------------------------------*/

static UINT8 ldv1000_command_strobe_r(laserdisc_info *info)
{
	/* the command strobe is asserted (active low) 54 or 84usec after the status */
	/* strobe for a duration of 25usec; we pick 600+84 = 684-709usec */
	/* for a duration of 26usec; we pick 600-626usec */
	attotime delta = attotime_sub(timer_get_time(), info->lastvsynctime);
	if (delta.attoseconds >= ATTOTIME_IN_USEC(684).attoseconds &&
		delta.attoseconds < ATTOTIME_IN_USEC(709).attoseconds)
		return ASSERT_LINE;

	return CLEAR_LINE;
}


/*-------------------------------------------------
    ldv1000_status_r - return state of the ready
    line
-------------------------------------------------*/

static UINT8 ldv1000_status_r(laserdisc_info *info)
{
	ldv1000_info *ldv1000 = &info->u.ldv1000;
	UINT8 status = 0xff;

	/* switch off the current mode */
	switch (ldv1000->mode)
	{
		/* reading frame number returns 5 characters */
		/* reading display lines returns 8 characters */
		case LDV1000_MODE_GET_FRAME:
		case LDV1000_MODE_GET_1ST:
		case LDV1000_MODE_GET_2ND:
			assert(ldv1000->readpos < ldv1000->readtotal);
			status = ldv1000->readbuf[ldv1000->readpos++];
			if (ldv1000->readpos == ldv1000->readtotal)
				ldv1000->mode = LDV1000_MODE_STATUS;
			break;

		/* reading RAM returns 1024 bytes */
		case LDV1000_MODE_GET_RAM:
			assert(ldv1000->readpos < ldv1000->readtotal);
			status = ldv1000->ram[1023 - ldv1000->readpos++];
			if (ldv1000->readpos == ldv1000->readtotal)
				ldv1000->mode = LDV1000_MODE_STATUS;
			break;

		/* otherwise, we just compute a status code */
		default:
		case LDV1000_MODE_STATUS:
			switch (info->state)
			{
				case LASERDISC_EJECTED:					status = 0xe0;		break;
				case LASERDISC_EJECTING:				status = 0x60;		break;
				case LASERDISC_LOADED:					status = 0xc8;		break;
				case LASERDISC_SPINUP:
				case LASERDISC_LOADING:					status = 0x48;		break;
				case LASERDISC_PARKED:					status = 0xfc;		break;

				case LASERDISC_SEARCHING_FRAME:			status = 0x50;		break;
				case LASERDISC_SEARCH_FINISHED:			status = 0xd0;		break;

				case LASERDISC_STOPPED:					status = 0xe5;		break;
				case LASERDISC_AUTOSTOPPED:				status = 0x54;		break;

				case LASERDISC_PLAYING_FORWARD:			status = 0xe4;		break;
				case LASERDISC_PLAYING_REVERSE:			status = 0xe4;		break;
				case LASERDISC_PLAYING_SLOW_FORWARD:	status = 0xae;		break;
				case LASERDISC_PLAYING_SLOW_REVERSE:	status = 0xae;		break;
				case LASERDISC_PLAYING_FAST_FORWARD:	status = 0xae;		break;
				case LASERDISC_PLAYING_FAST_REVERSE:	status = 0xae;		break;
				case LASERDISC_STEPPING_FORWARD:		status = 0xe5;		break;
				case LASERDISC_STEPPING_REVERSE:		status = 0xe5;		break;
				case LASERDISC_SCANNING_FORWARD:		status = 0x4c;		break;
				case LASERDISC_SCANNING_REVERSE:		status = 0x4c;		break;
				default:
					fatalerror("Unexpected disc state in ldv1000_status_r\n");
					break;
			}
			break;
	}

	/* bit 7 indicates our busy status */
	return status & ldv1000->statusmask;
}



/***************************************************************************
    SONY LDP-1450 IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    ldp1450_init - Sony LDP-1450-specific
    initialization
-------------------------------------------------*/

static void ldp1450_init(laserdisc_info *info)
{
	/* set up the write callbacks */
	info->writedata = ldp1450_data_w;

	/* set up the read callbacks */
	info->readdata = ldp1450_data_r;
	info->readline[LASERDISC_LINE_DATA_AVAIL] = ldp1450_data_avail_r;

	/* use a state changed callback */
	info->statechanged = ldp1450_state_changed;

	/* do a soft reset */
	ldp1450_soft_reset(info);
}


/*-------------------------------------------------
    ldp1450_soft_reset - Sony LDP-1450-specific
    soft reset
-------------------------------------------------*/

static void ldp1450_soft_reset(laserdisc_info *info)
{
	info->audio = AUDIO_CH1_ENABLE | AUDIO_CH2_ENABLE;
	info->display = FALSE;
	set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
	info->curfractrack = ONE_TRACK;
}


/*-------------------------------------------------
    ldp1450_compute_status - compute the current
    status bytes on the LDP-1450
-------------------------------------------------*/

static void ldp1450_compute_status(laserdisc_info *info)
{
	/*
       Byte 0: LDP ready status:
        0x80 Ready
        0x82 Disc Spinning down
        0x86 Tray closed & no disc loaded
        0x8A Disc ejecting, tray opening
        0x92 Disc loading, tray closing
        0x90 Disc spinning up
        0xC0 Busy Searching

       Byte 1: Error status:
        0x00 No error
        0x01 Focus unlock
        0x02 Communication Error

       Byte 2: Disc/motor status:
        0x01 Motor Off (disc spinning down or tray opening/ed)
        0x02 Tray closed & motor Off (no disc loaded)
        0x11 Motor On

       Byte 3: Command/argument status:
        0x00 Normal
        0x01 Relative search or Mark Set pending (Enter not sent)
        0x03 Search pending (Enter not sent)
        0x05 Repeat pending (Enter not sent) or Memory Search in progress
        0x80 FWD or REV Step

       Byte 4: Playback mode:
        0x00 Searching, Motor On (spinning up), motor Off (spinning down), tray opened/ing (no motor phase lock)
        0x01 FWD Play
        0x02 FWD Fast
        0x04 FWD Slow
        0x08 FWD Step
        0x10 FWD Scan
        0x20 Still
        0x81 REV Play
        0x82 REV Fast
        0x84 REV Slow
        0x88 REV Step
        0x90 REV Scan
    */

	ldp1450_info *ldp1450 = &info->u.ldp1450;
	UINT32 statusbytes = 0;

	switch (info->state)
	{
		case LASERDISC_EJECTED:					statusbytes = 0x86000200;	break;
		case LASERDISC_EJECTING:				statusbytes = 0x8a000100;	break;
		case LASERDISC_LOADED:					statusbytes = 0x80000100;	break;
		case LASERDISC_SPINUP:
		case LASERDISC_LOADING:					statusbytes = 0x92001100;	break;
		case LASERDISC_PARKED:					statusbytes = 0x80000100;	break;
		case LASERDISC_SEARCHING_FRAME:			statusbytes = 0xc0001100;	break;

		case LASERDISC_SEARCH_FINISHED:
		case LASERDISC_STOPPED:
		case LASERDISC_AUTOSTOPPED:				statusbytes = 0x80001120;	break;

		case LASERDISC_PLAYING_FORWARD:			statusbytes = 0x80001101;	break;
		case LASERDISC_PLAYING_REVERSE:			statusbytes = 0x80001181;	break;
		case LASERDISC_PLAYING_SLOW_FORWARD:	statusbytes = 0x80001104;	break;
		case LASERDISC_PLAYING_SLOW_REVERSE:	statusbytes = 0x80001184;	break;
		case LASERDISC_PLAYING_FAST_FORWARD:	statusbytes = 0x80001102;	break;
		case LASERDISC_PLAYING_FAST_REVERSE:	statusbytes = 0x80001182;	break;
		case LASERDISC_STEPPING_FORWARD:		statusbytes = 0x80001108;	break;
		case LASERDISC_STEPPING_REVERSE:		statusbytes = 0x80001188;	break;
		case LASERDISC_SCANNING_FORWARD:		statusbytes = 0x80001110;	break;
		case LASERDISC_SCANNING_REVERSE:		statusbytes = 0x80001190;	break;

		default:
			fatalerror("Unexpected disc state in ldp1450_compute_status\n");
			break;
	}

	/* copy in the result bytes */
	ldp1450->readtotal = 0;
	ldp1450->readbuf[ldp1450->readtotal++] = (statusbytes >> 24) & 0xff;
	ldp1450->readbuf[ldp1450->readtotal++] = (statusbytes >> 16) & 0xff;
	ldp1450->readbuf[ldp1450->readtotal++] = (statusbytes >> 8) & 0xff;
	switch (info->command)
	{
		case 0x43:	ldp1450->readbuf[ldp1450->readtotal++] = 0x03;	break;
		default:	ldp1450->readbuf[ldp1450->readtotal++] = 0x00;	break;
	}
	ldp1450->readbuf[ldp1450->readtotal++] = (statusbytes >> 0) & 0xff;
}


/*-------------------------------------------------
    ldp1450_data_w - write callback when the
    ENTER state is written
-------------------------------------------------*/

static void ldp1450_data_w(laserdisc_info *info, UINT8 prev, UINT8 data)
{
	static const UINT8 numbers[10] = { 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39 };
	ldp1450_info *ldp1450 = &info->u.ldp1450;

	/* by default, we return an ack on each command */
	ldp1450->readpos = ldp1450->readtotal = 0;
	ldp1450->readbuf[ldp1450->readtotal++] = 0x0a;

	/* look for and process numbers */
	if (process_number(info, data, numbers))
	{
		CMDPRINTF(("%c", data));
		return;
	}

	/* handle commands */
	switch (data)
	{
		case 0x24:  CMDPRINTF(("ldp1450: Audio Mute On\n"));
			/* mute audio */
			info->audio |= AUDIO_EXPLICIT_MUTE;
			break;

		case 0x25:  CMDPRINTF(("ldp1450: Audio Mute Off\n"));
			/* unmute audio */
			info->audio &= ~AUDIO_EXPLICIT_MUTE;
			break;

		case 0x26:  CMDPRINTF(("ldp1450: Video Mute Off\n"));
			/* mute video -- not implemented */
			break;

		case 0x27:  CMDPRINTF(("ldp1450: Video Mute On\n"));
			/* unmute video -- not implemented */
			break;

		case 0x28:  CMDPRINTF(("ldp1450: PSC Enable\n"));
			/* enable Picture Stop Codes (PSC) -- not implemented */
			break;

		case 0x29:  CMDPRINTF(("ldp1450: PSC Disable\n"));
			/* disable Picture Stop Codes (PSC) -- not implemented */
			break;

		case 0x2a:	CMDPRINTF(("ldp1450: %d Eject\n", info->parameter));
			/* eject the disc */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_EJECTING, STOP_SPEED, NULL_TARGET_FRAME);
				set_hold_state(info, GENERIC_LOAD_TIME, LASERDISC_EJECTED, STOP_SPEED);
			}
			break;

		case 0x2b:	CMDPRINTF(("ldp1450: %d Step forward\n", info->parameter));
			/* step forward one frame */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_FORWARD, STOP_SPEED, NULL_TARGET_FRAME);
				add_to_current_track(info, ONE_TRACK);
			}
			break;

		case 0x2c:	CMDPRINTF(("ldp1450: %d Step reverse\n", info->parameter));
			/* step backwards one frame */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_REVERSE, STOP_SPEED, NULL_TARGET_FRAME);
				add_to_current_track(info, -ONE_TRACK);
			}
			break;

		case 0x2d:  CMDPRINTF(("ldp1450: %d Search multiple tracks forward\n", info->parameter));
			/* enable Picture Stop Codes (PSC) -- not implemented */
			break;

		case 0x2e:  CMDPRINTF(("ldp1450: %d Search multiple tracks reverse\n", info->parameter));
			/* disable Picture Stop Codes (PSC) -- not implemented */
			break;

		case 0x3a:  CMDPRINTF(("ldp1450: %d Play\n", info->parameter));
			/* begin playing at regular speed, or load the disc if it is parked */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_FORWARD, PLAY_SPEED, NULL_TARGET_FRAME);
			else
			{
				if (info->state == LASERDISC_PARKED)
				{
					set_state(info, LASERDISC_SPINUP, STOP_SPEED, NULL_TARGET_FRAME);
					set_hold_state(info, GENERIC_SPINUP_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
				}
				else
				{
					set_state(info, LASERDISC_LOADING, STOP_SPEED, NULL_TARGET_FRAME);
					set_hold_state(info, GENERIC_LOAD_TIME, LASERDISC_PLAYING_FORWARD, PLAY_SPEED);
				}

				info->curfractrack = ONE_TRACK;
			}
			break;

		case 0x3b:  CMDPRINTF(("ldp1450: %d Fast forward play\n", info->parameter));
			/* play forward fast speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_FAST_FORWARD, LDP1450_FAST_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x3c:  CMDPRINTF(("ldp1450: %d Slow forward play\n", info->parameter));
			/* play forward slow speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_FORWARD, LDP1450_SLOW_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x3d:  CMDPRINTF(("ldp1450: %d Variable forward play\n", info->parameter));
			/* play forward variable speed */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_FORWARD, LDP1450_STEP_SPEED, NULL_TARGET_FRAME);
				info->command = 0x3d;
			}
			break;

		case 0x3e:  CMDPRINTF(("ldp1450: %d Scan forward\n", info->parameter));
			/* scan forward */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_SCANNING_FORWARD, LDP1450_SCAN_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x3f:	CMDPRINTF(("ldp1450: Stop\n"));
			/* pause at the current location */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x40:  CMDPRINTF((" ... Enter\n"));
			/* Enter -- execute command with parameter */
			switch (info->command)
			{
				case 0x3d:	/* forward variable speed */
					if (info->parameter != 0)
						info->curfracspeed = PLAY_SPEED / info->parameter;
					break;

				case 0x43:	/* search */
					set_state(info, LASERDISC_SEARCHING_FRAME, LDP1450_SEARCH_SPEED, info->parameter);
					break;

				case 0x4d:	/* reverse variable speed */
					if (info->parameter != 0)
						info->curfracspeed = -PLAY_SPEED / info->parameter;
					break;

				default: CMDPRINTF(("Unknown command: %02X\n", info->command));
					break;
			}
			break;

		case 0x41:  CMDPRINTF(("ldp1450: %d Clear entry\n", info->parameter));
			/* Clear entry */
			break;

		case 0x42:  CMDPRINTF(("ldp1450: %d Menu\n", info->parameter));
			/* Menu -- not implemented */
			break;

		case 0x43:  CMDPRINTF(("ldp1450: Search ... "));
			/* search to a particular frame number */
			if (laserdisc_ready(info))
			{
				info->command = data;

				/* Note that the disc stops as soon as the search command is issued */
				set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
			}
			break;

		case 0x44:  CMDPRINTF(("ldp1450: %d Repeat\n", info->parameter));
			/* Repeat -- not implemented */
			break;

		case 0x46:  CMDPRINTF(("ldp1450: Ch1 On\n"));
			/* channel 1 audio on */
			info->audio |= AUDIO_CH1_ENABLE;
			break;

		case 0x47:  CMDPRINTF(("ldp1450: Ch1 Off\n"));
			/* channel 1 audio off */
			info->audio &= ~AUDIO_CH1_ENABLE;
			break;

		case 0x48:  CMDPRINTF(("ldp1450: Ch2 On\n"));
			/* channel 1 audio on */
			info->audio |= AUDIO_CH2_ENABLE;
			break;

		case 0x49:  CMDPRINTF(("ldp1450: Ch2 Off\n"));
			/* channel 1 audio off */
			info->audio &= ~AUDIO_CH2_ENABLE;
			break;

		case 0x4a:  CMDPRINTF(("ldp1450: %d Reverse Play\n", info->parameter));
			/* begin playing at regular speed, or load the disc if it is parked */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_REVERSE, -PLAY_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x4b:  CMDPRINTF(("ldp1450: %d Fast reverse play\n", info->parameter));
			/* play reverse fast speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_FAST_REVERSE, -LDP1450_FAST_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x4c:  CMDPRINTF(("ldp1450: %d Slow reverse play\n", info->parameter));
			/* play reverse slow speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_PLAYING_SLOW_REVERSE, -LDP1450_SLOW_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x4d:  CMDPRINTF(("ldp1450: %d Variable reverse play\n", info->parameter));
			/* play reverse variable speed */
			if (laserdisc_ready(info))
			{
				set_state(info, LASERDISC_STEPPING_REVERSE, -LDP1450_STEP_SPEED, NULL_TARGET_FRAME);
				info->command = 0x4d;
			}
			break;

		case 0x4e:  CMDPRINTF(("ldp1450: %d Scan reverse\n", info->parameter));
			/* play forward variable speed */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_SCANNING_REVERSE, -LDP1450_SCAN_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x4f:	CMDPRINTF(("ldp1450: %d Still\n", info->parameter));
			/* still picture */
			if (laserdisc_ready(info))
				set_state(info, LASERDISC_STOPPED, STOP_SPEED, NULL_TARGET_FRAME);
			break;

		case 0x50:  CMDPRINTF(("ldp1450: Index On\n"));
			/* index on -- not implemented */
			break;

		case 0x51:  CMDPRINTF(("ldp1450: Index Off\n"));
			/* index off -- not implemented */
			break;

		case 0x55:  CMDPRINTF(("ldp1450: %d Set to frame number mode\n", info->parameter));
			/* set to frame number mode -- not implemented */
			break;

		case 0x56:  CMDPRINTF(("ldp1450: %d Clear all\n", info->parameter));
			/* clear all */
			ldp1450_soft_reset(info);
			break;

		case 0x5a:  CMDPRINTF(("ldp1450: %d Memory\n", info->parameter));
			/* memorize current position -- not implemented */
			break;

		case 0x5b:  CMDPRINTF(("ldp1450: %d Memory Search\n", info->parameter));
			/* locate memorized position -- not implemented */
			break;

		case 0x60:  CMDPRINTF(("ldp1450: %d Address Inquire\n", info->parameter));
			/* inquire for current address */
			ldp1450->readtotal = 0;
			ldp1450->readbuf[ldp1450->readtotal++] = '0' + (info->last_frame / 10000) % 10;
			ldp1450->readbuf[ldp1450->readtotal++] = '0' + (info->last_frame / 1000) % 10;
			ldp1450->readbuf[ldp1450->readtotal++] = '0' + (info->last_frame / 100) % 10;
			ldp1450->readbuf[ldp1450->readtotal++] = '0' + (info->last_frame / 10) % 10;
			ldp1450->readbuf[ldp1450->readtotal++] = '0' + (info->last_frame / 1) % 10;
			break;

		case 0x61:  CMDPRINTF(("ldp1450: %d Continue\n", info->parameter));
			/* resume the mode prior to still -- not implemented */
			break;

		case 0x62:  CMDPRINTF(("ldp1450: Motor On\n"));
			/* motor on -- not implemented */
			break;

		case 0x63:  CMDPRINTF(("ldp1450: Motor Off\n"));
			/* motor off -- not implemented */
			break;

		case 0x67:  CMDPRINTF(("ldp1450: %d Status Inquire\n", info->parameter));
			/* inquire status of player */
			ldp1450_compute_status(info);
			break;

		case 0x69:  CMDPRINTF(("ldp1450: %d Chapter Mode\n", info->parameter));
			/* set to chapter number mode -- not implemented */
			break;

		case 0x6e:  CMDPRINTF(("ldp1450: CX On\n"));
			/* CX noise reduction on -- not implemented */
			break;

		case 0x6f:  CMDPRINTF(("ldp1450: CX Off\n"));
			/* CX noise reduction off -- not implemented */
			break;

		case 0x71:  CMDPRINTF(("ldp1450: %d Non-CF Play\n", info->parameter));
			/* disengage color framing -- not implemented */
			break;

		case 0x72:  CMDPRINTF(("ldp1450: %d ROM Version\n", info->parameter));
			/* inquire ROM version -- not implemented */
			break;

		case 0x73:  CMDPRINTF(("ldp1450: %d Mark Set\n", info->parameter));
			/* set mark position -- not implemented */
			break;

		case 0x74:  CMDPRINTF(("ldp1450: Eject Enable On\n"));
			/* activate eject function -- not implemented */
			break;

		case 0x75:  CMDPRINTF(("ldp1450: Eject Disable Off\n"));
			/* deactivate eject function -- not implemented */
			break;

		case 0x76:  CMDPRINTF(("ldp1450: %d Chapter Inquire\n", info->parameter));
			/* inquire current chapter -- not implemented */
			break;

		case 0x79:  CMDPRINTF(("ldp1450: %d User Code Inquire\n", info->parameter));
			/* inquire user's code -- not implemented */
			break;

		case 0x80:  CMDPRINTF(("ldp1450: %d User Index Control\n", info->parameter));
			/* set user defined index -- not implemented */
			break;

		case 0x81:  CMDPRINTF(("ldp1450: Activate user defined index\n"));
			/* activate eject function -- not implemented */
			break;

		case 0x82:  CMDPRINTF(("ldp1450: Deactivate user defined index\n"));
			/* deactivate eject function -- not implemented */
			break;

		default:	CMDPRINTF(("ldp1450: %d Unknown command %02X\n", info->parameter, data));
			/* unknown command -- respond with a NAK */
			ldp1450->readtotal = 0;
			ldp1450->readbuf[ldp1450->readtotal++] = 0x0b;
			break;
	}

	/* reset the parameter after executing a command */
	info->parameter = -1;
}


/*-------------------------------------------------
    ldp1450_data_avail_r - return ASSERT_LINE if
    serial data is available
-------------------------------------------------*/

static UINT8 ldp1450_data_avail_r(laserdisc_info *info)
{
	ldp1450_info *ldp1450 = &info->u.ldp1450;
	return (ldp1450->readpos < ldp1450->readtotal) ? ASSERT_LINE : CLEAR_LINE;
}


/*-------------------------------------------------
    ldp1450_data_r - return data from the player
-------------------------------------------------*/

static UINT8 ldp1450_data_r(laserdisc_info *info)
{
	ldp1450_info *ldp1450 = &info->u.ldp1450;

	if (ldp1450->readpos < ldp1450->readtotal)
		return ldp1450->readbuf[ldp1450->readpos++];
	return 0xff;
}


/*-------------------------------------------------
    ldp1450_state_changed - Sony LDP-1450-specific
    state changed callback
-------------------------------------------------*/

static void ldp1450_state_changed(laserdisc_info *info, UINT8 oldstate)
{
	ldp1450_info *ldp1450 = &info->u.ldp1450;

	/* look for searching -> search finished state */
	if (info->state == LASERDISC_SEARCH_FINISHED && oldstate == LASERDISC_SEARCHING_FRAME)
	{
		ldp1450->readpos = ldp1450->readtotal = 0;
		ldp1450->readbuf[ldp1450->readtotal++] = 0x01;
	}
}



/***************************************************************************
    PHILPS 22VP932 IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    vp932_init - Philips 22VP932-specific
    initialization
-------------------------------------------------*/

static void vp932_init(laserdisc_info *info)
{
	/* set up the write callbacks */
	info->writedata = vp932_data_w;

	/* set up the read callbacks */
	info->readdata = vp932_data_r;
	info->readline[LASERDISC_LINE_DATA_AVAIL] = vp932_data_avail_r;

	/* use a state changed callback */
	info->statechanged = vp932_state_changed;

	/* do a soft reset */
	vp932_soft_reset(info);
}


/*-------------------------------------------------
    vp932_soft_reset - Philips 22VP932-specific
    soft reset
-------------------------------------------------*/

static void vp932_soft_reset(laserdisc_info *info)
{
	vp932_info *vp932 = &info->u.vp932;

	info->audio = AUDIO_CH1_ENABLE  | AUDIO_CH2_ENABLE;
	info->display = 0;

	/* reset the pending count */
	vp932->incount = 0;
	vp932->outcount = 0;
}


/*-------------------------------------------------
    vp932_data_w - write callback when data is
    written
-------------------------------------------------*/

static void vp932_data_w(laserdisc_info *info, UINT8 prev, UINT8 data)
{
	vp932_info *vp932 = &info->u.vp932;

	/* ignore 0's */
	if (data == 0x00)
		return;

	/* if this isn't a CR, just accumulate it */
	if (data != 0x0d)
	{
		if (vp932->incount < ARRAY_LENGTH(vp932->inbuffer))
			vp932->inbuffer[vp932->incount++] = data;
	}

	/* otherwise, execute the command */
	else
	{
		vp932->inbuffer[vp932->incount] = 0;
		CMDPRINTF(("22vp932: %s\n", vp932->inbuffer));

		switch (vp932->inbuffer[vp932->incount - 1])
		{
			case 'R':	/* seek */
				if (laserdisc_ready(info))
				{
					INT32 targetframe = 0, i;

					for (i = 0; i < vp932->incount - 1; i++)
						if (vp932->inbuffer[i] >= '0' && vp932->inbuffer[i] <= '9')
							targetframe = (targetframe * 10) + (vp932->inbuffer[i] - '0');
					set_state(info, LASERDISC_SEARCHING_FRAME, VP932_SEARCH_SPEED, targetframe);

					vp932->outcount = 0;
					vp932->outbuffer[vp932->outcount++] = 'A';
					vp932->outbuffer[vp932->outcount++] = '0';
					vp932->outbuffer[vp932->outcount++] = 0x0d;
				}
				break;
		}

		/* reset the command buffer */
		vp932->incount = 0;
	}
}


/*-------------------------------------------------
    vp932_data_r - read callback when data is
    returned
-------------------------------------------------*/

static UINT8 vp932_data_r(laserdisc_info *info)
{
	vp932_info *vp932 = &info->u.vp932;
	UINT8 result = 0;

	/* grab data if we can */
	if (vp932->outcount > 0)
	{
		result = vp932->outbuffer[0];
		if (--vp932->outcount > 0)
			memmove(&vp932->outbuffer[0], &vp932->outbuffer[1], vp932->outcount);
	}

	return result;
}


/*-------------------------------------------------
    vp932_data_avail_r - return ASSERT_LINE if
    serial data is available
-------------------------------------------------*/

static UINT8 vp932_data_avail_r(laserdisc_info *info)
{
	vp932_info *vp932 = &info->u.vp932;
	return (vp932->outcount > 0) ? ASSERT_LINE : CLEAR_LINE;
}


/*-------------------------------------------------
    vp932_state_changed - Sony LDP-1450-specific
    state changed callback
-------------------------------------------------*/

static void vp932_state_changed(laserdisc_info *info, UINT8 oldstate)
{
	vp932_info *vp932 = &info->u.vp932;

	/* look for searching -> search finished state */
	if (info->state == LASERDISC_SEARCH_FINISHED && oldstate == LASERDISC_SEARCHING_FRAME)
	{
		vp932->outcount = 0;
		vp932->outbuffer[vp932->outcount++] = 'A';
		vp932->outbuffer[vp932->outcount++] = '0';
		vp932->outbuffer[vp932->outcount++] = 0x0d;
	}
}