1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
|
/*
* A low level driver for Yamaha YM3812 and OPL-3 -chips
*
* Copyright by Hannu Savolainen 1993
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. 2.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* Major improvements to the FM handling 30AUG92 by Rob Hooft,
*/
/*
* hooft@chem.ruu.nl
*/
/*
*
* Ported to the new Audio Driver by Luigi Rizzo:
* (C) 1999 Seigo Tanimura
*
* This is the OPL2/3/4 chip driver for FreeBSD, based on the Luigi Sound Driver.
* This handles io against /dev/midi, the midi {in, out}put event queues
* and the event/message operation to the OPL chip.
*
* $FreeBSD$
*
*/
#include <dev/sound/midi/midi.h>
#include <dev/sound/chip.h>
#include <isa/isavar.h>
static devclass_t midi_devclass;
#ifndef DDB
#undef DDB
#define DDB(x)
#endif /* DDB */
/*
* The OPL-3 mode is switched on by writing 0x01, to the offset 5
* of the right side.
*
* Another special register at the right side is at offset 4. It contains
* a bit mask defining which voices are used as 4 OP voices.
*
* The percussive mode is implemented in the left side only.
*
* With the above exeptions the both sides can be operated independently.
*
* A 4 OP voice can be created by setting the corresponding
* bit at offset 4 of the right side.
*
* For example setting the rightmost bit (0x01) changes the
* first voice on the right side to the 4 OP mode. The fourth
* voice is made inaccessible.
*
* If a voice is set to the 2 OP mode, it works like 2 OP modes
* of the original YM3812 (AdLib). In addition the voice can
* be connected the left, right or both stereo channels. It can
* even be left unconnected. This works with 4 OP voices also.
*
* The stereo connection bits are located in the FEEDBACK_CONNECTION
* register of the voice (0xC0-0xC8). In 4 OP voices these bits are
* in the second half of the voice.
*/
/*
* Register numbers for the global registers
*/
#define TEST_REGISTER 0x01
#define ENABLE_WAVE_SELECT 0x20
#define TIMER1_REGISTER 0x02
#define TIMER2_REGISTER 0x03
#define TIMER_CONTROL_REGISTER 0x04 /* Left side */
#define IRQ_RESET 0x80
#define TIMER1_MASK 0x40
#define TIMER2_MASK 0x20
#define TIMER1_START 0x01
#define TIMER2_START 0x02
#define CONNECTION_SELECT_REGISTER 0x04 /* Right side */
#define RIGHT_4OP_0 0x01
#define RIGHT_4OP_1 0x02
#define RIGHT_4OP_2 0x04
#define LEFT_4OP_0 0x08
#define LEFT_4OP_1 0x10
#define LEFT_4OP_2 0x20
#define OPL3_MODE_REGISTER 0x05 /* Right side */
#define OPL3_ENABLE 0x01
#define OPL4_ENABLE 0x02
#define KBD_SPLIT_REGISTER 0x08 /* Left side */
#define COMPOSITE_SINE_WAVE_MODE 0x80 /* Don't use with OPL-3? */
#define KEYBOARD_SPLIT 0x40
#define PERCUSSION_REGISTER 0xbd /* Left side only */
#define TREMOLO_DEPTH 0x80
#define VIBRATO_DEPTH 0x40
#define PERCUSSION_ENABLE 0x20
#define BASSDRUM_ON 0x10
#define SNAREDRUM_ON 0x08
#define TOMTOM_ON 0x04
#define CYMBAL_ON 0x02
#define HIHAT_ON 0x01
/*
* Offsets to the register banks for operators. To get the
* register number just add the operator offset to the bank offset
*
* AM/VIB/EG/KSR/Multiple (0x20 to 0x35)
*/
#define AM_VIB 0x20
#define TREMOLO_ON 0x80
#define VIBRATO_ON 0x40
#define SUSTAIN_ON 0x20
#define KSR 0x10 /* Key scaling rate */
#define MULTIPLE_MASK 0x0f /* Frequency multiplier */
/*
* KSL/Total level (0x40 to 0x55)
*/
#define KSL_LEVEL 0x40
#define KSL_MASK 0xc0 /* Envelope scaling bits */
#define TOTAL_LEVEL_MASK 0x3f /* Strength (volume) of OP */
/*
* Attack / Decay rate (0x60 to 0x75)
*/
#define ATTACK_DECAY 0x60
#define ATTACK_MASK 0xf0
#define DECAY_MASK 0x0f
/*
* Sustain level / Release rate (0x80 to 0x95)
*/
#define SUSTAIN_RELEASE 0x80
#define SUSTAIN_MASK 0xf0
#define RELEASE_MASK 0x0f
/*
* Wave select (0xE0 to 0xF5)
*/
#define WAVE_SELECT 0xe0
/*
* Offsets to the register banks for voices. Just add to the
* voice number to get the register number.
*
* F-Number low bits (0xA0 to 0xA8).
*/
#define FNUM_LOW 0xa0
/*
* F-number high bits / Key on / Block (octave) (0xB0 to 0xB8)
*/
#define KEYON_BLOCK 0xb0
#define KEYON_BIT 0x20
#define BLOCKNUM_MASK 0x1c
#define FNUM_HIGH_MASK 0x03
/*
* Feedback / Connection (0xc0 to 0xc8)
*
* These registers have two new bits when the OPL-3 mode
* is selected. These bits controls connecting the voice
* to the stereo channels. For 4 OP voices this bit is
* defined in the second half of the voice (add 3 to the
* register offset).
*
* For 4 OP voices the connection bit is used in the
* both halfs (gives 4 ways to connect the operators).
*/
#define FEEDBACK_CONNECTION 0xc0
#define FEEDBACK_MASK 0x0e /* Valid just for 1st OP of a voice */
#define CONNECTION_BIT 0x01
/*
* In the 4 OP mode there is four possible configurations how the
* operators can be connected together (in 2 OP modes there is just
* AM or FM). The 4 OP connection mode is defined by the rightmost
* bit of the FEEDBACK_CONNECTION (0xC0-0xC8) on the both halfs.
*
* First half Second half Mode
*
* +---+
* v |
* 0 0 >+-1-+--2--3--4-->
*
*
*
* +---+
* | |
* 0 1 >+-1-+--2-+
* |->
* >--3----4-+
*
* +---+
* | |
* 1 0 >+-1-+-----+
* |->
* >--2--3--4-+
*
* +---+
* | |
* 1 1 >+-1-+--+
* |
* >--2--3-+->
* |
* >--4----+
*/
#define STEREO_BITS 0x30 /* OPL-3 only */
#define VOICE_TO_LEFT 0x10
#define VOICE_TO_RIGHT 0x20
/*
* Definition table for the physical voices
*/
struct physical_voice_info {
unsigned char voice_num;
unsigned char voice_mode; /* 0=unavailable, 2=2 OP, 4=4 OP */
int ch; /* channel (left=USE_LEFT, right=USE_RIGHT) */
unsigned char op[4]; /* Operator offsets */
};
/*
* There is 18 possible 2 OP voices
* (9 in the left and 9 in the right).
* The first OP is the modulator and 2nd is the carrier.
*
* The first three voices in the both sides may be connected
* with another voice to a 4 OP voice. For example voice 0
* can be connected with voice 3. The operators of voice 3 are
* used as operators 3 and 4 of the new 4 OP voice.
* In this case the 2 OP voice number 0 is the 'first half' and
* voice 3 is the second.
*/
#define USE_LEFT 0
#define USE_RIGHT 1
static struct physical_voice_info pv_map[18] =
{
/* No Mode Side OP1 OP2 OP3 OP4 */
/* --------------------------------------------------- */
{ 0, 2, USE_LEFT, {0x00, 0x03, 0x08, 0x0b}},
{ 1, 2, USE_LEFT, {0x01, 0x04, 0x09, 0x0c}},
{ 2, 2, USE_LEFT, {0x02, 0x05, 0x0a, 0x0d}},
{ 3, 2, USE_LEFT, {0x08, 0x0b, 0x00, 0x00}},
{ 4, 2, USE_LEFT, {0x09, 0x0c, 0x00, 0x00}},
{ 5, 2, USE_LEFT, {0x0a, 0x0d, 0x00, 0x00}},
{ 6, 2, USE_LEFT, {0x10, 0x13, 0x00, 0x00}}, /* Used by percussive voices */
{ 7, 2, USE_LEFT, {0x11, 0x14, 0x00, 0x00}}, /* if the percussive mode */
{ 8, 2, USE_LEFT, {0x12, 0x15, 0x00, 0x00}}, /* is selected */
{ 0, 2, USE_RIGHT, {0x00, 0x03, 0x08, 0x0b}},
{ 1, 2, USE_RIGHT, {0x01, 0x04, 0x09, 0x0c}},
{ 2, 2, USE_RIGHT, {0x02, 0x05, 0x0a, 0x0d}},
{ 3, 2, USE_RIGHT, {0x08, 0x0b, 0x00, 0x00}},
{ 4, 2, USE_RIGHT, {0x09, 0x0c, 0x00, 0x00}},
{ 5, 2, USE_RIGHT, {0x0a, 0x0d, 0x00, 0x00}},
{ 6, 2, USE_RIGHT, {0x10, 0x13, 0x00, 0x00}},
{ 7, 2, USE_RIGHT, {0x11, 0x14, 0x00, 0x00}},
{ 8, 2, USE_RIGHT, {0x12, 0x15, 0x00, 0x00}}
};
/* These are the tuning parameters. */
static unsigned short semitone_tuning[24] =
{
/* 0 */ 10000, 10595, 11225, 11892, 12599, 13348, 14142, 14983,
/* 8 */ 15874, 16818, 17818, 18877, 20000, 21189, 22449, 23784,
/* 16 */ 25198, 26697, 28284, 29966, 31748, 33636, 35636, 37755
};
static unsigned short cent_tuning[100] =
{
/* 0 */ 10000, 10006, 10012, 10017, 10023, 10029, 10035, 10041,
/* 8 */ 10046, 10052, 10058, 10064, 10070, 10075, 10081, 10087,
/* 16 */ 10093, 10099, 10105, 10110, 10116, 10122, 10128, 10134,
/* 24 */ 10140, 10145, 10151, 10157, 10163, 10169, 10175, 10181,
/* 32 */ 10187, 10192, 10198, 10204, 10210, 10216, 10222, 10228,
/* 40 */ 10234, 10240, 10246, 10251, 10257, 10263, 10269, 10275,
/* 48 */ 10281, 10287, 10293, 10299, 10305, 10311, 10317, 10323,
/* 56 */ 10329, 10335, 10341, 10347, 10353, 10359, 10365, 10371,
/* 64 */ 10377, 10383, 10389, 10395, 10401, 10407, 10413, 10419,
/* 72 */ 10425, 10431, 10437, 10443, 10449, 10455, 10461, 10467,
/* 80 */ 10473, 10479, 10485, 10491, 10497, 10503, 10509, 10515,
/* 88 */ 10521, 10528, 10534, 10540, 10546, 10552, 10558, 10564,
/* 96 */ 10570, 10576, 10582, 10589
};
/*
* The next table looks magical, but it certainly is not. Its values have
* been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
* for i=0. This log-table converts a linear volume-scaling (0..127) to a
* logarithmic scaling as present in the FM-synthesizer chips. so : Volume
* 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
* volume -8 it was implemented as a table because it is only 128 bytes and
* it saves a lot of log() calculations. (RH)
*/
static char opl_volumetable[128] =
{
-64, -48, -40, -35, -32, -29, -27, -26,
-24, -23, -21, -20, -19, -18, -18, -17,
-16, -15, -15, -14, -13, -13, -12, -12,
-11, -11, -10, -10, -10, -9, -9, -8,
-8, -8, -7, -7, -7, -6, -6, -6,
-5, -5, -5, -5, -4, -4, -4, -4,
-3, -3, -3, -3, -2, -2, -2, -2,
-2, -1, -1, -1, -1, 0, 0, 0,
0, 0, 0, 1, 1, 1, 1, 1,
1, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
6, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8};
#define MAX_VOICE 18
#define OFFS_4OP 11
#define SBFM_MAXINSTR 256
/* These are the OPL Models. */
#define MODEL_NONE 0
#define MODEL_OPL2 2
#define MODEL_OPL3 3
#define MODEL_OPL4 4
/* These are the OPL Voice modes. */
#define VOICE_NONE 0
#define VOICE_2OP 2
#define VOICE_4OP 4
/* PnP IDs */
static struct isa_pnp_id opl_ids[] = {
{0x01200001, "@H@2001 FM Synthesizer"}, /* @H@2001 */
{0x01100001, "@H@1001 FM Synthesizer"}, /* @H@1001 */
#if notdef
/* TODO: write bridge drivers for these devices. */
{0x0000630e, "CSC0000 FM Synthesizer"}, /* CSC0000 */
{0x68187316, "ESS1868 FM Synthesizer"}, /* ESS1868 */
{0x79187316, "ESS1879 FM Synthesizer"}, /* ESS1879 */
{0x2100a865, "YMH0021 FM Synthesizer"}, /* YMH0021 */
{0x80719304, "ADS7180 FM Synthesizer"}, /* ADS7180 */
{0x0300561e, "GRV0003 FM Synthesizer"}, /* GRV0003 */
#endif /* notdef */
};
/* These are the default io bases. */
static int opl_defaultiobase[] = {
0x388,
0x380,
};
/* These are the per-voice information. */
struct voice_info {
u_char keyon_byte;
long bender;
long bender_range;
u_long orig_freq;
u_long current_freq;
int volume;
int mode;
};
/* These are the synthesizer and the midi device information. */
static struct synth_info opl_synthinfo = {
"OPL FM Synthesizer",
0,
SYNTH_TYPE_FM,
FM_TYPE_ADLIB,
0,
9,
0,
SBFM_MAXINSTR,
0,
};
static struct midi_info opl_midiinfo = {
"OPL FM Synthesizer",
0,
0,
0,
};
/*
* These functions goes into oplsynthdev_op_desc.
*/
static mdsy_killnote_t opl_killnote;
static mdsy_setinstr_t opl_setinstr;
static mdsy_startnote_t opl_startnote;
static mdsy_reset_t opl_reset;
static mdsy_hwcontrol_t opl_hwcontrol;
static mdsy_loadpatch_t opl_loadpatch;
static mdsy_panning_t opl_panning;
static mdsy_aftertouch_t opl_aftertouch;
static mdsy_controller_t opl_controller;
static mdsy_patchmgr_t opl_patchmgr;
static mdsy_bender_t opl_bender;
static mdsy_allocvoice_t opl_allocvoice;
static mdsy_setupvoice_t opl_setupvoice;
static mdsy_sendsysex_t opl_sendsysex;
static mdsy_prefixcmd_t opl_prefixcmd;
static mdsy_volumemethod_t opl_volumemethod;
/*
* This is the synthdev_info for an OPL3 chip.
*/
static synthdev_info oplsynth_op_desc = {
opl_killnote,
opl_setinstr,
opl_startnote,
opl_reset,
opl_hwcontrol,
opl_loadpatch,
opl_panning,
opl_aftertouch,
opl_controller,
opl_patchmgr,
opl_bender,
opl_allocvoice,
opl_setupvoice,
opl_sendsysex,
opl_prefixcmd,
opl_volumemethod,
};
/* Here is the parameter structure per a device. */
struct opl_softc {
device_t dev; /* device information */
mididev_info *devinfo; /* midi device information */
struct resource *io; /* Base of io port */
int io_rid; /* Io resource ID */
int model; /* OPL model */
struct synth_info synthinfo; /* Synthesizer information */
struct sbi_instrument i_map[SBFM_MAXINSTR]; /* Instrument map */
struct sbi_instrument *act_i[SBFM_MAXINSTR]; /* Active instruments */
struct physical_voice_info pv_map[MAX_VOICE]; /* Physical voice map */
int cmask; /* Connection mask */
int lv_map[MAX_VOICE]; /* Level map */
struct voice_info voc[MAX_VOICE]; /* Voice information */
};
typedef struct opl_softc *sc_p;
/*
* These functions goes into opl_op_desc to get called
* from sound.c.
*/
static int opl_probe(device_t dev);
static int opl_probe1(sc_p scp);
static int opl_attach(device_t dev);
static int oplsbc_probe(device_t dev);
static int oplsbc_attach(device_t dev);
static d_open_t opl_open;
static d_close_t opl_close;
static d_read_t opl_read;
static d_write_t opl_write;
static d_ioctl_t opl_ioctl;
static midi_callback_t opl_callback;
/* These go to snddev_info. */
static mdsy_readraw_t opl_readraw;
static mdsy_writeraw_t opl_writeraw;
/* These functions are local. */
static void opl_startplay(sc_p scp) __unused;
static void opl_command(sc_p scp, int ch, int addr, u_int val);
static int opl_status(sc_p scp);
static void opl_enter4opmode(sc_p scp);
static void opl_storeinstr(sc_p scp, int instr_no, struct sbi_instrument *instr);
static void opl_calcvol(u_char *regbyte, int volume, int main_vol);
static void opl_setvoicevolume(sc_p scp, int voice, int volume, int main_vol);
static void opl_freqtofnum(int freq, int *block, int *fnum);
static int opl_bendpitch(sc_p scp, int voice, int val);
static int opl_notetofreq(int note_num);
static u_long opl_computefinetune(u_long base_freq, int bend, int range);
static int opl_allocres(sc_p scp, device_t dev);
static void opl_releaseres(sc_p scp, device_t dev);
/*
* This is the device descriptor for the midi device.
*/
static mididev_info opl_op_desc = {
"OPL FM Synthesizer",
SNDCARD_OPL,
opl_open,
opl_close,
opl_read,
opl_write,
opl_ioctl,
NULL,
opl_callback,
MIDI_BUFFSIZE, /* Queue Length */
0, /* XXX This is not an *audio* device! */
};
/*
* Here are the main functions to interact to the user process.
*/
static int
opl_probe(device_t dev)
{
sc_p scp;
int unit, i;
/* Check isapnp ids */
if (isa_get_logicalid(dev) != 0)
return (ISA_PNP_PROBE(device_get_parent(dev), dev, opl_ids));
scp = device_get_softc(dev);
unit = device_get_unit(dev);
device_set_desc(dev, opl_op_desc.name);
bzero(scp, sizeof(*scp));
DEB(printf("opl%d: probing.\n", unit));
scp->io_rid = 0;
scp->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &scp->io_rid, 0, ~0, 4, RF_ACTIVE);
if (opl_allocres(scp, dev)) {
/* We try the defaults in opl_defaultiobase. */
DEB(printf("opl%d: port is omitted, trying the defaults.\n", unit));
for (i = 0 ; i < sizeof(opl_defaultiobase) / sizeof(*opl_defaultiobase) ; i++) {
scp->io_rid = 0;
scp->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &scp->io_rid, opl_defaultiobase[i], opl_defaultiobase[i] + 1, 4, RF_ACTIVE);
if (scp->io != NULL) {
if (opl_probe1(scp))
opl_releaseres(scp, dev);
else
break;
}
}
if (scp->io == NULL)
return (ENXIO);
} else if(opl_probe1(scp)) {
opl_releaseres(scp, dev);
return (ENXIO);
}
/* We now have some kind of OPL. */
DEB(printf("opl%d: probed.\n", unit));
return (0);
}
/* We do probe in this function. */
static int
opl_probe1(sc_p scp)
{
u_char stat1, stat2;
/* Reset the timers and the interrupt. */
opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, IRQ_RESET);
/* Read the status. */
stat1 = opl_status(scp);
if ((stat1 & 0xe0) != 0)
return (1);
/* Try firing the timer1. */
opl_command(scp, USE_LEFT, TIMER1_REGISTER, 0xff); /* Set the timer value. */
opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, TIMER1_START | TIMER2_MASK); /* Start the timer. */
DELAY(150); /* Wait for the timer. */
/* Read the status. */
stat2 = opl_status(scp);
/* Reset the timers and the interrupt. */
opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
opl_command(scp, USE_LEFT, TIMER_CONTROL_REGISTER, IRQ_RESET);
if ((stat2 & 0xe0) != 0xc0)
return (1);
return (0);
}
static int
oplsbc_probe(device_t dev)
{
char *s;
sc_p scp;
struct sndcard_func *func;
/* The parent device has already been probed. */
func = device_get_ivars(dev);
if (func == NULL || func->func != SCF_SYNTH)
return (ENXIO);
s = "SB OPL FM Synthesizer";
scp = device_get_softc(dev);
bzero(scp, sizeof(*scp));
scp->io_rid = 2;
device_set_desc(dev, s);
return (0);
}
static int
opl_attach(device_t dev)
{
sc_p scp;
mididev_info *devinfo;
int unit, i, opl4_io, opl4_id;
struct resource *opl4;
u_char signature, tmp;
scp = device_get_softc(dev);
unit = device_get_unit(dev);
DEB(printf("opl%d: attaching.\n", unit));
/* Fill the softc for this unit. */
scp->dev = dev;
scp->devinfo = devinfo = &midi_info[unit];
/* Allocate other resources. */
if (opl_allocres(scp, dev)) {
opl_releaseres(scp, dev);
return (ENXIO);
}
/* Detect the OPL type. */
signature = opl_status(scp);
if (signature == 0x06)
scp->model = MODEL_OPL2;
else {
/* OPL3 or later, might be OPL4. */
/* Enable OPL3 and OPL4. */
opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, 0);
opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
tmp = opl_status(scp);
if (tmp != 0x02)
scp->model = MODEL_OPL3;
#if notdef
else {
#endif /* notdef */
/* Alloc OPL4 ID register. */
opl4_id = 2;
opl4_io = rman_get_start(scp->io) - 8;
opl4 = bus_alloc_resource(dev, SYS_RES_IOPORT, &opl4_id, opl4_io, opl4_io + 1, 2, RF_ACTIVE);
if (opl4 != NULL) {
/* Select OPL4 ID register. */
bus_space_write_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 0, 0x02);
DELAY(10);
tmp = bus_space_read_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 1);
DELAY(10);
if (tmp != 0x20)
scp->model = MODEL_OPL3;
else {
scp->model = MODEL_OPL4;
/* Select back OPL4 FM mixer control. */
bus_space_write_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 0, 0xf8);
DELAY(10);
bus_space_write_1(rman_get_bustag(opl4), rman_get_bushandle(opl4), 1, 0x1b);
DELAY(10);
}
bus_release_resource(dev, SYS_RES_IOPORT, opl4_id, opl4);
}
#if notdef
}
#endif /* notdef */
opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, 0);
}
/* Kill any previous notes. */
for (i = 0 ; i < 9 ; i++)
opl_command(scp, USE_RIGHT, KEYON_BLOCK + i, 0);
/* Select melodic mode. */
opl_command(scp, USE_LEFT, TEST_REGISTER, ENABLE_WAVE_SELECT);
opl_command(scp, USE_LEFT, PERCUSSION_REGISTER, 0);
for (i = 0 ; i < SBFM_MAXINSTR ; i++)
scp->i_map[i].channel = -1;
/* Fill the softc. */
bcopy(&opl_synthinfo, &scp->synthinfo, sizeof(opl_synthinfo));
snprintf(scp->synthinfo.name, 64, "Yamaha OPL%d FM", scp->model);
bcopy(pv_map, scp->pv_map, sizeof(pv_map));
if (scp->model < MODEL_OPL3) { /* OPL2. */
scp->synthinfo.nr_voices = 9;
scp->synthinfo.nr_drums = 0;
for (i = 0 ; i < MAX_VOICE ; i++)
scp->pv_map[i].ch = USE_LEFT;
} else { /* OPL3 or later. */
scp->synthinfo.capabilities |= SYNTH_CAP_OPL3;
scp->synthinfo.nr_voices = 18;
scp->synthinfo.nr_drums = 0;
#if notdef
for (i = 0 ; i < MAX_VOICE ; i++) {
if (scp->pv_map[i].ch == USE_LEFT)
scp->pv_map[i].ch = USE_LEFT;
else
scp->pv_map[i].ch = USE_RIGHT;
}
#endif /* notdef */
opl_command(scp, USE_RIGHT, OPL3_MODE_REGISTER, OPL3_ENABLE);
opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, 0);
}
/* Fill the midi info. */
bcopy(&opl_op_desc, devinfo, sizeof(opl_op_desc));
midiinit(devinfo, dev);
devinfo->flags = 0;
bcopy(&oplsynth_op_desc, &devinfo->synth, sizeof(oplsynth_op_desc));
devinfo->synth.readraw = opl_readraw;
devinfo->synth.writeraw = opl_writeraw;
devinfo->synth.alloc.max_voice = scp->synthinfo.nr_voices;
strcpy(devinfo->name, scp->synthinfo.name);
snprintf(devinfo->midistat, sizeof(devinfo->midistat), "at 0x%x", (u_int)rman_get_start(scp->io));
/* Init the queue. */
devinfo->midi_dbuf_in.unit_size = devinfo->midi_dbuf_out.unit_size = 1;
midibuf_init(&devinfo->midi_dbuf_in);
midibuf_init(&devinfo->midi_dbuf_out);
/* Increase the number of the synthesizers. */
nsynth++;
DEB(printf("opl%d: attached.\n", unit));
DEB(printf("opl%d: the chip is OPL%d.\n", unit, scp->model));
return (0);
}
static int
oplsbc_attach(device_t dev)
{
return (opl_attach(dev));
}
static int
opl_open(dev_t i_dev, int flags, int mode, struct proc *p)
{
sc_p scp;
mididev_info *devinfo;
int unit, i;
unit = MIDIUNIT(i_dev);
DEB(printf("opl%d: opening.\n", unit));
if (unit >= nmidi + nsynth) {
DEB(printf("opl_open: unit %d does not exist.\n", unit));
return (ENXIO);
}
devinfo = get_mididev_info(i_dev, &unit);
if (devinfo == NULL) {
DEB(printf("opl_open: unit %d is not configured.\n", unit));
return (ENXIO);
}
scp = devinfo->softc;
if (scp->model < MODEL_OPL3)
devinfo->synth.alloc.max_voice = 9;
else
devinfo->synth.alloc.max_voice = 18;
devinfo->synth.alloc.timestamp = 0;
for (i = 0 ; i < MAX_VOICE ; i++) {
devinfo->synth.alloc.map[i] = 0;
devinfo->synth.alloc.alloc_times[i] = 0;
}
scp->cmask = 0; /* We are in 2 OP mode initially. */
if (scp->model >= MODEL_OPL3)
opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, scp->cmask);
DEB(printf("opl%d: opened.\n", unit));
return (0);
}
static int
opl_close(dev_t i_dev, int flags, int mode, struct proc *p)
{
sc_p scp;
mididev_info *devinfo;
int unit;
unit = MIDIUNIT(i_dev);
DEB(printf("opl%d: closing.\n", unit));
if (unit >= nmidi + nsynth) {
DEB(printf("opl_close: unit %d does not exist.\n", unit));
return (ENXIO);
}
devinfo = get_mididev_info(i_dev, &unit);
if (devinfo == NULL) {
DEB(printf("opl_close: unit %d is not configured.\n", unit));
return (ENXIO);
}
scp = devinfo->softc;
if (scp->model < MODEL_OPL3)
devinfo->synth.alloc.max_voice = 9;
else
devinfo->synth.alloc.max_voice = 18;
/* Stop the OPL. */
opl_reset(scp->devinfo);
DEB(printf("opl%d: closed.\n", unit));
return (0);
}
static int
opl_read(dev_t i_dev, struct uio *buf, int flag)
{
sc_p scp;
mididev_info *devinfo;
int unit, ret;
unit = MIDIUNIT(i_dev);
if (unit >= nmidi + nsynth) {
DEB(printf("opl_read: unit %d does not exist.\n", unit));
return (ENXIO);
}
devinfo = get_mididev_info(i_dev, &unit);
if (devinfo == NULL) {
DEB(printf("opl_read: unit %d is not configured.\n", unit));
return (ENXIO);
}
scp = devinfo->softc;
if ((devinfo->fflags & FREAD) == 0) {
DEB(printf("opl_read: unit %d is not for reading.\n", unit));
return (EIO);
}
/* Drain the data. */
midibuf_init(&devinfo->midi_dbuf_in);
ret = 0;
return (ret);
}
static int
opl_write(dev_t i_dev, struct uio *buf, int flag)
{
sc_p scp;
mididev_info *devinfo;
int unit, ret;
unit = MIDIUNIT(i_dev);
if (unit >= nmidi + nsynth) {
DEB(printf("opl_write: unit %d does not exist.\n", unit));
return (ENXIO);
}
devinfo = get_mididev_info(i_dev, &unit);
if (devinfo == NULL) {
DEB(printf("opl_write: unit %d is not configured.\n", unit));
return (ENXIO);
}
scp = devinfo->softc;
if ((devinfo->fflags & FWRITE) == 0) {
DEB(printf("opl_write: unit %d is not for writing.\n", unit));
return (EIO);
}
/* Drain the data. */
midibuf_init(&devinfo->midi_dbuf_out);
midibuf_init(&devinfo->midi_dbuf_passthru);
ret = 0;
return (ret);
}
static int
opl_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc *p)
{
sc_p scp;
mididev_info *devinfo;
int unit;
struct synth_info *synthinfo;
struct midi_info *midiinfo;
struct sbi_instrument ins;
unit = MIDIUNIT(i_dev);
DEB(printf("opl%d: ioctlling, cmd 0x%x.\n", unit, (int)cmd));
if (unit >= nmidi + nsynth) {
DEB(printf("opl_ioctl: unit %d does not exist.\n", unit));
return (ENXIO);
}
devinfo = get_mididev_info(i_dev, &unit);
if (devinfo == NULL) {
DEB(printf("opl_ioctl: unit %d is not configured.\n", unit));
return (ENXIO);
}
scp = devinfo->softc;
switch (cmd) {
case SNDCTL_SYNTH_INFO:
synthinfo = (struct synth_info *)arg;
if (synthinfo->device >= nmidi + nsynth || synthinfo->device != unit)
return (ENXIO);
bcopy(&scp->synthinfo, synthinfo, sizeof(scp->synthinfo));
synthinfo->device = unit;
if (devinfo->synth.alloc.max_voice == 12)
synthinfo->nr_voices = 6;
else
synthinfo->nr_voices = devinfo->synth.alloc.max_voice;
return (0);
break;
case SNDCTL_MIDI_INFO:
midiinfo = (struct midi_info *)arg;
if (midiinfo->device >= nmidi + nsynth || midiinfo->device != unit)
return (ENXIO);
bcopy(&opl_midiinfo, midiinfo, sizeof(opl_midiinfo));
strcpy(midiinfo->name, scp->synthinfo.name);
midiinfo->device = unit;
return (0);
break;
case SNDCTL_FM_LOAD_INSTR:
bcopy(arg, &ins, sizeof(ins));
if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
printf("opl_ioctl: Instrument number %d is not valid.\n", ins.channel);
return (EINVAL);
}
#if notyet
pmgr_inform(scp, PM_E_PATCH_LOADED, inc.channel, 0, 0, 0);
#endif /* notyet */
opl_storeinstr(scp, ins.channel, &ins);
return (0);
break;
case SNDCTL_SYNTH_MEMAVL:
return 0x7fffffff;
break;
case SNDCTL_FM_4OP_ENABLE:
if (scp->model >= MODEL_OPL3)
opl_enter4opmode(scp);
return (0);
break;
default:
return (ENOSYS);
}
/* NOTREACHED */
return (EINVAL);
}
static int
opl_callback(mididev_info *devinfo, int reason)
{
int unit;
sc_p scp;
if (devinfo == NULL) {
DEB(printf("opl_callback: device not configured.\n"));
return (ENXIO);
}
unit = devinfo->unit;
scp = devinfo->softc;
DEB(printf("opl%d: callback, reason 0x%x.\n", unit, reason));
switch (reason & MIDI_CB_REASON_MASK) {
case MIDI_CB_START:
if ((reason & MIDI_CB_RD) != 0 && (devinfo->flags & MIDI_F_READING) == 0)
/* Begin recording. */
devinfo->flags |= MIDI_F_READING;
if ((reason & MIDI_CB_WR) != 0 && (devinfo->flags & MIDI_F_WRITING) == 0)
/* Start playing. */
devinfo->flags |= MIDI_F_WRITING;
break;
case MIDI_CB_STOP:
case MIDI_CB_ABORT:
if ((reason & MIDI_CB_RD) != 0 && (devinfo->flags & MIDI_F_READING) != 0)
/* Stop recording. */
devinfo->flags &= ~MIDI_F_READING;
if ((reason & MIDI_CB_WR) != 0 && (devinfo->flags & MIDI_F_WRITING) != 0)
/* Stop Playing. */
devinfo->flags &= ~MIDI_F_WRITING;
break;
}
return (0);
}
static int
opl_readraw(mididev_info *md, u_char *buf, int len, int nonblock)
{
sc_p scp;
int unit;
if (md == NULL)
return (ENXIO);
unit = md->unit;
scp = md->softc;
if ((md->fflags & FREAD) == 0) {
DEB(printf("opl_readraw: unit %d is not for reading.\n", unit));
return (EIO);
}
/* NOP. */
return (0);
}
static int
opl_writeraw(mididev_info *md, u_char *buf, int len, int nonblock)
{
sc_p scp;
int unit;
if (md == NULL)
return (ENXIO);
unit = md->unit;
scp = md->softc;
if ((md->fflags & FWRITE) == 0) {
DEB(printf("opl_writeraw: unit %d is not for writing.\n", unit));
return (EIO);
}
/* NOP. */
return (0);
}
/* The functions below here are the synthesizer interfaces. */
static int
opl_killnote(mididev_info *md, int voice, int note, int vel)
{
int unit;
sc_p scp;
struct physical_voice_info *map;
scp = md->softc;
unit = md->unit;
DEB(printf("opl%d: killing a note, voice %d, note %d, vel %d.\n", unit, voice, note, vel));
if (voice < 0 || voice >= md->synth.alloc.max_voice)
return (0);
md->synth.alloc.map[voice] = 0;
map = &scp->pv_map[scp->lv_map[voice]];
if (map->voice_mode == VOICE_NONE)
return (0);
opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, scp->voc[voice].keyon_byte & ~0x20);
scp->voc[voice].keyon_byte = 0;
scp->voc[voice].bender = 0;
scp->voc[voice].volume = 64;
scp->voc[voice].bender_range = 200;
scp->voc[voice].orig_freq = 0;
scp->voc[voice].current_freq = 0;
scp->voc[voice].mode = 0;
return (0);
}
static int
opl_setinstr(mididev_info *md, int voice, int instr_no)
{
int unit;
sc_p scp;
scp = md->softc;
unit = md->unit;
DEB(printf("opl%d: setting an instrument, voice %d, instr_no %d.\n", unit, voice, instr_no));
if (voice < 0 || voice >= md->synth.alloc.max_voice || instr_no < 0 || instr_no >= SBFM_MAXINSTR)
return (0);
scp->act_i[voice] = &scp->i_map[instr_no];
return (0);
}
static int
opl_startnote(mididev_info *md, int voice, int note, int volume)
{
u_char fpc;
int unit, block, fnum, freq, voice_mode, voice_shift;
struct sbi_instrument *instr;
struct physical_voice_info *map;
sc_p scp;
scp = md->softc;
unit = md->unit;
DEB(printf("opl%d: starting a note, voice %d, note %d, volume %d.\n", unit, voice, note, volume));
if (voice < 0 || voice >= md->synth.alloc.max_voice)
return (0);
map = &scp->pv_map[scp->lv_map[voice]];
if (map->voice_mode == VOICE_NONE)
return (0);
if (note == 255) {
/* Change the volume. */
opl_setvoicevolume(scp, voice, volume, scp->voc[voice].volume);
return (0);
}
/* Kill the previous note. */
opl_command(scp, map->ch, KSL_LEVEL + map->op[1], 0xff); /* Carrier volume */
opl_command(scp, map->ch, KSL_LEVEL + map->op[0], 0xff); /* Modulator volume */
if (map->voice_mode == VOICE_4OP) {
opl_command(scp, map->ch, KSL_LEVEL + map->op[3], 0xff); /* Carrier volume */
opl_command(scp, map->ch, KSL_LEVEL + map->op[2], 0xff); /* Modulator volume */
}
opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, 0); /* Note off. */
instr = scp->act_i[voice];
if (instr == NULL)
instr = &scp->i_map[0];
if (instr->channel < 0) {
printf("opl_startnote: the instrument for voice %d is undefined.\n", voice);
return (0);
}
if (map->voice_mode == VOICE_2OP && instr->key == OPL3_PATCH) {
printf("opl_startnote: the voice mode %d mismatches the key 0x%x.\n", map->voice_mode, instr->key);
return (0);
}
voice_mode = map->voice_mode;
if (voice_mode == VOICE_4OP) {
if (map->ch == USE_LEFT)
voice_shift = 0;
else
voice_shift = 3;
voice_shift += map->voice_num;
if (instr->key != OPL3_PATCH) {
voice_mode = VOICE_2OP;
scp->cmask &= ~(1 << voice_shift);
} else
scp->cmask |= 1 << voice_shift;
opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, scp->cmask);
}
/* Set the sound characteristics, attack, decay, sustain, release, wave select, feedback, connection. */
opl_command(scp, map->ch, AM_VIB + map->op[0], instr->operators[0]); /* Sound characteristics. */
opl_command(scp, map->ch, AM_VIB + map->op[1], instr->operators[1]);
opl_command(scp, map->ch, ATTACK_DECAY + map->op[0], instr->operators[4]); /* Attack and decay. */
opl_command(scp, map->ch, ATTACK_DECAY + map->op[1], instr->operators[5]);
opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[0], instr->operators[6]); /* Sustain and release. */
opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
opl_command(scp, map->ch, WAVE_SELECT + map->op[0], instr->operators[8]); /* Wave select. */
opl_command(scp, map->ch, WAVE_SELECT + map->op[1], instr->operators[9]);
fpc = instr->operators[10];
if ((fpc & 0x30) == 0)
fpc |= 0x30; /* So that at least one channel is enabled. */
opl_command(scp, map->ch, FEEDBACK_CONNECTION + map->voice_num, fpc); /* Feedback and connection. */
if (voice_mode == VOICE_4OP) {
/* Do not forget the operators 3 and 4. */
opl_command(scp, map->ch, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]); /* Sound characteristics. */
opl_command(scp, map->ch, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]);
opl_command(scp, map->ch, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]); /* Attack and decay. */
opl_command(scp, map->ch, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]);
opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]); /* Sustain and release. */
opl_command(scp, map->ch, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]);
opl_command(scp, map->ch, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]); /* Wave select. */
opl_command(scp, map->ch, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]);
fpc = instr->operators[OFFS_4OP + 10];
if ((fpc & 0x30) == 0)
fpc |= 0x30; /* So that at least one channel is enabled. */
opl_command(scp, map->ch, FEEDBACK_CONNECTION + map->voice_num + 3, fpc); /* Feedback and connection. */
}
scp->voc[voice].mode = voice_mode;
opl_setvoicevolume(scp, voice, volume, scp->voc[voice].volume);
/* Calcurate the frequency. */
scp->voc[voice].orig_freq = opl_notetofreq(note) / 1000;
/* Tune for the pitch bend. */
freq = scp->voc[voice].current_freq = opl_computefinetune(scp->voc[voice].orig_freq, scp->voc[voice].bender, scp->voc[voice].bender_range);
opl_freqtofnum(freq, &block, &fnum);
/* Now we can play the note. */
opl_command(scp, map->ch, FNUM_LOW + map->voice_num, fnum & 0xff);
scp->voc[voice].keyon_byte = 0x20 | ((block & 0x07) << 2) | ((fnum >> 8) & 0x03);
opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, scp->voc[voice].keyon_byte);
if (voice_mode == VOICE_4OP)
opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num + 3, scp->voc[voice].keyon_byte);
return (0);
}
static int
opl_reset(mididev_info *md)
{
int unit, i;
sc_p scp;
scp = md->softc;
unit = md->unit;
DEB(printf("opl%d: resetting.\n", unit));
for (i = 0 ; i < MAX_VOICE ; i++)
scp->lv_map[i] = i;
for (i = 0 ; i < md->synth.alloc.max_voice ; i++) {
opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[0], 0xff);
opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[1], 0xff);
if (scp->pv_map[scp->lv_map[i]].voice_mode == VOICE_4OP) {
opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[2], 0xff);
opl_command(scp, scp->pv_map[scp->lv_map[i]].ch, KSL_LEVEL + scp->pv_map[scp->lv_map[i]].op[3], 0xff);
}
opl_killnote(md, i, 0, 64);
}
if (scp->model >= MODEL_OPL3) {
md->synth.alloc.max_voice = 18;
for (i = 0 ; i < MAX_VOICE ; i++)
scp->pv_map[i].voice_mode = VOICE_2OP;
}
return (0);
}
static int
opl_hwcontrol(mididev_info *md, u_char *event)
{
/* NOP. */
return (0);
}
static int
opl_loadpatch(mididev_info *md, int format, struct uio *buf, int offs, int count, int pmgr_flag)
{
int unit;
struct sbi_instrument ins;
sc_p scp;
scp = md->softc;
unit = md->unit;
if (count < sizeof(ins)) {
printf("opl_loadpatch: The patch record is too short.\n");
return (EINVAL);
}
if (uiomove(&((char *)&ins)[offs], sizeof(ins) - offs, buf) != 0)
printf("opl_loadpatch: User memory mangled?\n");
if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) {
printf("opl_loadpatch: Instrument number %d is not valid.\n", ins.channel);
return (EINVAL);
}
ins.key = format;
opl_storeinstr(scp, ins.channel, &ins);
return (0);
}
static int
opl_panning(mididev_info *md, int chn, int pan)
{
/* NOP. */
return (0);
}
#define SET_VIBRATO(cell) { \
int tmp; \
tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \
if (press > 110) \
tmp |= 0x40; /* Vibrato on */ \
opl_command(scp, map->ch, AM_VIB + map->op[cell-1], tmp);}
static int
opl_aftertouch(mididev_info *md, int voice, int press)
{
int unit, connection;
struct sbi_instrument *instr;
struct physical_voice_info *map;
sc_p scp;
scp = md->softc;
unit = md->unit;
DEB(printf("opl%d: setting the aftertouch, voice %d, press %d.\n", unit, voice, press));
if (voice < 0 || voice >= md->synth.alloc.max_voice)
return (0);
map = &scp->pv_map[scp->lv_map[voice]];
if (map->voice_mode == VOICE_NONE)
return (0);
/* Adjust the vibrato. */
instr = scp->act_i[voice];
if (instr == NULL)
instr = &scp->i_map[0];
if (scp->voc[voice].mode == VOICE_4OP) {
connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
switch (connection) {
case 0:
SET_VIBRATO(4);
break;
case 1:
SET_VIBRATO(2);
SET_VIBRATO(4);
break;
case 2:
SET_VIBRATO(1);
SET_VIBRATO(4);
break;
case 3:
SET_VIBRATO(1);
SET_VIBRATO(3);
SET_VIBRATO(4);
break;
}
} else {
SET_VIBRATO(1);
if ((instr->operators[10] & 0x01))
SET_VIBRATO(2);
}
return (0);
}
static int
opl_bendpitch(sc_p scp, int voice, int value)
{
int unit, block, fnum, freq;
struct physical_voice_info *map;
mididev_info *md;
md = scp->devinfo;
unit = md->unit;
DEB(printf("opl%d: setting the pitch bend, voice %d, value %d.\n", unit, voice, value));
map = &scp->pv_map[scp->lv_map[voice]];
if (map->voice_mode == 0)
return (0);
scp->voc[voice].bender = value;
if (value == 0)
return (0);
if ((scp->voc[voice].keyon_byte & 0x20) == 0)
return (0);
freq = opl_computefinetune(scp->voc[voice].orig_freq, scp->voc[voice].bender, scp->voc[voice].bender_range);
scp->voc[voice].current_freq = freq;
opl_freqtofnum(freq, &block, &fnum);
opl_command(scp, map->ch, FNUM_LOW + map->voice_num, fnum & 0xff);
scp->voc[voice].keyon_byte = 0x20 | ((block & 0x07) << 2) | ((fnum >> 8) & 0x03);
opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num, scp->voc[voice].keyon_byte);
if (map->voice_mode == VOICE_4OP)
opl_command(scp, map->ch, KEYON_BLOCK + map->voice_num + 3, scp->voc[voice].keyon_byte);
return (0);
}
static int
opl_controller(mididev_info *md, int voice, int ctrlnum, int val)
{
int unit;
sc_p scp;
scp = md->softc;
unit = md->unit;
DEB(printf("opl%d: setting the controller, voice %d, ctrlnum %d, val %d.\n", unit, voice, ctrlnum, val));
if (voice < 0 || voice >= md->synth.alloc.max_voice)
return (0);
switch (ctrlnum) {
case CTRL_PITCH_BENDER:
opl_bendpitch(scp, voice, val);
break;
case CTRL_PITCH_BENDER_RANGE:
scp->voc[voice].bender_range = val;
break;
case CTRL_MAIN_VOLUME:
scp->voc[voice].volume = val / 128;
break;
}
return (0);
}
static int
opl_patchmgr(mididev_info *md, struct patmgr_info *rec)
{
return (EINVAL);
}
static int
opl_bender(mididev_info *md, int voice, int val)
{
sc_p scp;
scp = md->softc;
if (voice < 0 || voice >= md->synth.alloc.max_voice)
return (0);
return opl_bendpitch(scp, voice, val - 8192);
}
static int
opl_allocvoice(mididev_info *md, int chn, int note, struct voice_alloc_info *alloc)
{
int i, p, best, first, avail, best_time, is4op, instr_no;
struct sbi_instrument *instr;
sc_p scp;
scp = md->softc;
DEB(printf("opl%d: allocating a voice, chn %d, note %d.\n", unit, chn, note));
best_time = 0x7fffffff;
if (chn < 0 || chn >= 15)
instr_no = 0;
else
instr_no = md->synth.chn_info[chn].pgm_num;
instr = &scp->i_map[instr_no];
if (instr->channel < 0 || md->synth.alloc.max_voice != 12)
is4op = 0;
else if (md->synth.alloc.max_voice == 12) {
if (instr->key == OPL3_PATCH)
is4op = 1;
else
is4op = 0;
} else
is4op = 0;
if (is4op) {
first = p = 0;
avail = 6;
} else {
if (md->synth.alloc.max_voice == 12)
first = p = 6;
else
first = p = 0;
avail = md->synth.alloc.max_voice;
}
/* Look up a free voice. */
best = first;
for (i = 0 ; i < avail ; i++) {
if (alloc->map[p] == 0)
return (p);
}
if (alloc->alloc_times[p] < best_time) {
best_time = alloc->alloc_times[p];
best = p;
}
p = (p + 1) % avail;
if (best < 0)
best = 0;
else if (best > md->synth.alloc.max_voice)
best -= md->synth.alloc.max_voice;
return best;
}
static int
opl_setupvoice(mididev_info *md, int voice, int chn)
{
struct channel_info *info;
sc_p scp;
scp = md->softc;
DEB(printf("opl%d: setting up a voice, voice %d, chn %d.\n", unit, voice, chn));
info = &md->synth.chn_info[chn];
opl_setinstr(md, voice, info->pgm_num);
scp->voc[voice].bender = info->bender_value;
scp->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME];
return (0);
}
static int
opl_sendsysex(mididev_info *md, u_char *sysex, int len)
{
/* NOP. */
return (0);
}
static int
opl_prefixcmd(mididev_info *md, int status)
{
/* NOP. */
return (0);
}
static int
opl_volumemethod(mididev_info *md, int mode)
{
/* NOP. */
return (0);
}
/*
* The functions below here are the libraries for the above ones.
*/
/*
* Starts to play the data in the output queue.
* Call this at >=splmidi.
*/
static void
opl_startplay(sc_p scp)
{
mididev_info *devinfo;
devinfo = scp->devinfo;
/* Can we play now? */
if (devinfo->midi_dbuf_out.rl == 0)
return;
/* Begin playing. */
devinfo->callback(devinfo, MIDI_CB_START | MIDI_CB_WR);
}
/* Writes a command to the OPL chip. */
static void
opl_command(sc_p scp, int ch, int addr, u_int val)
{
int model;
DEB(printf("opl%d: sending a command, iobase 0x%x, addr 0x%x, val 0x%x.\n", unit, iobase, addr, val));
model = scp->model;
/* Write the addr first. */
bus_space_write_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2, (u_char)(addr & 0xff));
if (model < MODEL_OPL3)
DELAY(10);
else {
bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
}
/* Next write the value. */
bus_space_write_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2 + 1, (u_char)(val & 0xff));
if (model < MODEL_OPL3)
DELAY(30);
else {
bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), ch * 2);
}
}
/* Reads the status of the OPL chip. */
static int
opl_status(sc_p scp)
{
DEB(printf("opl%d: reading the status.\n", unit));
return bus_space_read_1(rman_get_bustag(scp->io), rman_get_bushandle(scp->io), 0);
}
static void
opl_enter4opmode(sc_p scp)
{
int i;
mididev_info *devinfo;
static int v4op[MAX_VOICE] = {
0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17,
};
devinfo = scp->devinfo;
DEB(printf("opl%d: entering 4 OP mode.\n", unit));
/* Connect all possible 4 OP voice operators. */
scp->cmask = 0x3f;
opl_command(scp, USE_RIGHT, CONNECTION_SELECT_REGISTER, scp->cmask);
for (i = 0 ; i < 3 ; i++)
scp->pv_map[i].voice_mode = VOICE_4OP;
for (i = 3 ; i < 6 ; i++)
scp->pv_map[i].voice_mode = VOICE_NONE;
for (i = 9 ; i < 12 ; i++)
scp->pv_map[i].voice_mode = VOICE_4OP;
for (i = 12 ; i < 15 ; i++)
scp->pv_map[i].voice_mode = VOICE_NONE;
for (i = 0 ; i < 12 ; i++)
scp->lv_map[i] = v4op[i];
devinfo->synth.alloc.max_voice = 12;
}
static void
opl_storeinstr(sc_p scp, int instr_no, struct sbi_instrument *instr)
{
if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || scp->model < MODEL_OPL3))
printf("opl_storeinstr: The patch format field 0x%x is not valid.\n", instr->key);
bcopy(instr, &scp->i_map[instr_no], sizeof(*instr));
}
static void
opl_calcvol(u_char *regbyte, int volume, int main_vol)
{
int level;
level = (~*regbyte & 0x3f);
if (main_vol > 127)
main_vol = 127;
volume = (volume * main_vol) / 127;
if (level > 0)
level += opl_volumetable[volume];
RANGE(level, 0, 0x3f);
*regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
}
static void
opl_setvoicevolume(sc_p scp, int voice, int volume, int main_vol)
{
u_char vol1, vol2, vol3, vol4;
int connection;
struct sbi_instrument *instr;
struct physical_voice_info *map;
mididev_info *devinfo;
devinfo = scp->devinfo;
if (voice < 0 || voice >= devinfo->synth.alloc.max_voice)
return;
map = &scp->pv_map[scp->lv_map[voice]];
instr = scp->act_i[voice];
if (instr == NULL)
instr = &scp->i_map[0];
if (instr->channel < 0)
return;
if (scp->voc[voice].mode == VOICE_NONE)
return;
if (scp->voc[voice].mode == VOICE_2OP) { /* 2 OP mode. */
vol1 = instr->operators[2];
vol2 = instr->operators[3];
if ((instr->operators[10] & 0x01))
opl_calcvol(&vol1, volume, main_vol);
opl_calcvol(&vol2, volume, main_vol);
opl_command(scp, map->ch, KSL_LEVEL + map->op[0], vol1);
opl_command(scp, map->ch, KSL_LEVEL + map->op[1], vol2);
} else { /* 4 OP mode. */
vol1 = instr->operators[2];
vol2 = instr->operators[3];
vol3 = instr->operators[OFFS_4OP + 2];
vol4 = instr->operators[OFFS_4OP + 3];
connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
switch(connection) {
case 0:
opl_calcvol(&vol4, volume, main_vol);
break;
case 1:
opl_calcvol(&vol2, volume, main_vol);
opl_calcvol(&vol4, volume, main_vol);
break;
case 2:
opl_calcvol(&vol1, volume, main_vol);
opl_calcvol(&vol4, volume, main_vol);
break;
case 3:
opl_calcvol(&vol1, volume, main_vol);
opl_calcvol(&vol3, volume, main_vol);
opl_calcvol(&vol4, volume, main_vol);
break;
}
opl_command(scp, map->ch, KSL_LEVEL + map->op[0], vol1);
opl_command(scp, map->ch, KSL_LEVEL + map->op[1], vol2);
opl_command(scp, map->ch, KSL_LEVEL + map->op[2], vol3);
opl_command(scp, map->ch, KSL_LEVEL + map->op[3], vol4);
}
}
static void
opl_freqtofnum(int freq, int *block, int *fnum)
{
int f, octave;
f = freq;
octave = 5;
if (f == 0)
octave = 0;
else if (f < 261) {
while (f < 261) {
octave--;
f <<= 1;
}
} else if (f > 493) {
while (f > 493) {
octave++;
f >>= 1;
}
}
if (octave > 7)
octave = 7;
*fnum = freq * (1 << (20 - octave)) / 49716;
*block = octave;
}
static int notes[] =
{
261632,
277189,
293671,
311132,
329632,
349232,
369998,
391998,
415306,
440000,
466162,
493880
};
#define BASE_OCTAVE 5
static int
opl_notetofreq(int note_num)
{
int note, octave, note_freq;
octave = note_num / 12;
note = note_num % 12;
note_freq = notes[note];
if (octave < BASE_OCTAVE)
note_freq >>= (BASE_OCTAVE - octave);
else if (octave > BASE_OCTAVE)
note_freq <<= (octave - BASE_OCTAVE);
return (note_freq);
}
static u_long
opl_computefinetune(u_long base_freq, int bend, int range)
{
u_long amount;
int negative, semitones, cents, multiplier;
if (bend == 0 || range == 0 || base_freq == 0)
return (base_freq);
multiplier = 1;
if (range > 8192)
range = 8192;
bend = bend * range / 8192;
if (bend == 0)
return (base_freq);
if (bend < 0) {
negative = 1;
bend = -bend;
}
else
negative = 0;
if (bend > range)
bend = range;
while (bend > 2399) {
multiplier *= 4;
bend -= 2400;
}
semitones = bend / 100;
cents = bend % 100;
amount = (u_long)(semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
if (negative)
return (base_freq * 10000) / amount;
else
return (base_freq * amount) / 10000;
}
/* Allocates resources other than IO ports. */
static int
opl_allocres(sc_p scp, device_t dev)
{
if (scp->io == NULL) {
scp->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &scp->io_rid, 0, ~0, 4, RF_ACTIVE);
if (scp->io == NULL)
return (1);
}
return (0);
}
/* Releases resources. */
static void
opl_releaseres(sc_p scp, device_t dev)
{
if (scp->io != NULL) {
bus_release_resource(dev, SYS_RES_IOPORT, scp->io_rid, scp->io);
scp->io = NULL;
}
}
static device_method_t opl_methods[] = {
/* Device interface */
DEVMETHOD(device_probe , opl_probe ),
DEVMETHOD(device_attach, opl_attach),
{ 0, 0 },
};
static driver_t opl_driver = {
"midi",
opl_methods,
sizeof(struct opl_softc),
};
DRIVER_MODULE(opl, isa, opl_driver, midi_devclass, 0, 0);
static device_method_t oplsbc_methods[] = {
/* Device interface */
DEVMETHOD(device_probe , oplsbc_probe ),
DEVMETHOD(device_attach, oplsbc_attach),
{ 0, 0 },
};
static driver_t oplsbc_driver = {
"midi",
oplsbc_methods,
sizeof(struct opl_softc),
};
DRIVER_MODULE(oplsbc, sbc, oplsbc_driver, midi_devclass, 0, 0);
|