comparison Plugins/Input/adplug/core/realopl.cpp @ 359:8df427a314a8 trunk

[svn] Adlib synthesizer (AdPlug) support.
author chainsaw
date Fri, 30 Dec 2005 16:31:39 -0800
parents
children 15ca2ea93a30
comparison
equal deleted inserted replaced
358:70075730e187 359:8df427a314a8
1 /*
2 * Adplug - Replayer for many OPL2/OPL3 audio file formats.
3 * Copyright (C) 1999, 2000, 2001 Simon Peter, <dn.tlp@gmx.net>, et al.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 *
20 * realopl.cpp - Real hardware OPL, by Simon Peter (dn.tlp@gmx.net)
21 */
22
23 #include <conio.h>
24 #include "realopl.h"
25
26 #ifdef _MSC_VER
27 #define INP _inp
28 #define OUTP _outp
29 #elif defined(__WATCOMC__)
30 #define INP inp
31 #define OUTP outp
32 #endif
33
34 /*
35 * chris: TODO: This isn't quite right. According to Jeff Lee's doc:
36 *
37 * "After writing to the register port, you must wait twelve cycles before
38 * sending the data; after writing the data, eighty-four cycles must elapse
39 * before any other sound card operation may be performed.
40 *
41 * | The AdLib manual gives the wait times in microseconds: three point three
42 * | (3.3) microseconds for the address, and twenty-three (23) microseconds
43 * | for the data.
44 * |
45 * | The most accurate method of producing the delay is to read the register
46 * | port six times after writing to the register port, and read the register
47 * | port thirty-five times after writing to the data port."
48 *
49 *
50 * In other words, the delay constants represented by {SHORT|LONG}DELAY below
51 * aren't given in microseconds, but rather direct reads (INB) from the Adlib
52 * I/O ports.
53 *
54 * Translation: SHORTDELAY should be 6, and LONGDELAY is just fine. :-)
55 */
56
57 #define SHORTDELAY 6 // short delay in I/O port-reads after OPL hardware output
58 #define LONGDELAY 35 // long delay in I/O port-reads after OPL hardware output
59
60 // the 9 operators as expected by the OPL2
61 static const unsigned char op_table[9] = {0x00, 0x01, 0x02, 0x08, 0x09, 0x0a, 0x10, 0x11, 0x12};
62
63 CRealopl::CRealopl(unsigned short initport): adlport(initport), hardvol(0), bequiet(false), nowrite(false)
64 {
65 for(int i=0;i<22;i++) {
66 hardvols[i][0] = 0;
67 hardvols[i][1] = 0;
68 }
69 }
70
71 bool CRealopl::detect()
72 {
73 unsigned char stat1,stat2,i;
74
75 hardwrite(4,0x60); hardwrite(4,0x80);
76 stat1 = INP(adlport);
77 hardwrite(2,0xff); hardwrite(4,0x21);
78 for(i=0;i<80;i++) // wait for adlib
79 INP(adlport);
80 stat2 = INP(adlport);
81 hardwrite(4,0x60); hardwrite(4,0x80);
82
83 if(((stat1 & 0xe0) == 0) && ((stat2 & 0xe0) == 0xc0))
84 return true;
85 else
86 return false;
87 }
88
89 void CRealopl::setvolume(int volume)
90 {
91 int i;
92
93 hardvol = volume;
94 for(i=0;i<9;i++) {
95 hardwrite(0x43+op_table[i],((hardvols[op_table[i]+3][0] & 63) + volume) > 63 ? 63 : hardvols[op_table[i]+3][0] + volume);
96 if(hardvols[i][1] & 1) // modulator too?
97 hardwrite(0x40+op_table[i],((hardvols[op_table[i]][0] & 63) + volume) > 63 ? 63 : hardvols[op_table[i]][0] + volume);
98 }
99 }
100
101 void CRealopl::setquiet(bool quiet)
102 {
103 bequiet = quiet;
104
105 if(quiet) {
106 oldvol = hardvol;
107 setvolume(63);
108 } else
109 setvolume(oldvol);
110 }
111
112 void CRealopl::hardwrite(int reg, int val)
113 {
114 int i;
115
116 OUTP(adlport,reg); // set register
117 for(i=0;i<SHORTDELAY;i++) // wait for adlib
118 INP(adlport);
119 OUTP(adlport+1,val); // set value
120 for(i=0;i<LONGDELAY;i++) // wait for adlib
121 INP(adlport);
122 }
123
124 void CRealopl::write(int reg, int val)
125 {
126 int i;
127
128 if(nowrite)
129 return;
130
131 if(bequiet && (reg >= 0xb0 && reg <= 0xb8)) // filter all key-on commands
132 val &= ~32;
133 if(reg >= 0x40 && reg <= 0x55) // cache volumes
134 hardvols[reg-0x40][0] = val;
135 if(reg >= 0xc0 && reg <= 0xc8)
136 hardvols[reg-0xc0][1] = val;
137 if(hardvol) // reduce volume
138 for(i=0;i<9;i++) {
139 if(reg == 0x43 + op_table[i])
140 val = ((val & 63) + hardvol) > 63 ? 63 : val + hardvol;
141 else
142 if((reg == 0x40 + op_table[i]) && (hardvols[i][1] & 1))
143 val = ((val & 63) + hardvol) > 63 ? 63 : val + hardvol;
144 }
145
146 hardwrite(reg,val);
147 }
148
149 void CRealopl::init()
150 {
151 int i;
152
153 for (i=0;i<9;i++) { // stop instruments
154 hardwrite(0xb0 + i,0); // key off
155 hardwrite(0x80 + op_table[i],0xff); // fastest release
156 }
157 hardwrite(0xbd,0); // clear misc. register
158 }