root / hw / fmopl.c @ 84f15818
History | View | Annotate | Download (35 kB)
1 | 85571bc7 | bellard | /*
|
---|---|---|---|
2 | 85571bc7 | bellard | **
|
3 | 85571bc7 | bellard | ** File: fmopl.c -- software implementation of FM sound generator
|
4 | 85571bc7 | bellard | **
|
5 | 85571bc7 | bellard | ** Copyright (C) 1999,2000 Tatsuyuki Satoh , MultiArcadeMachineEmurator development
|
6 | 85571bc7 | bellard | **
|
7 | 85571bc7 | bellard | ** Version 0.37a
|
8 | 85571bc7 | bellard | **
|
9 | 85571bc7 | bellard | */
|
10 | 85571bc7 | bellard | |
11 | 85571bc7 | bellard | /*
|
12 | 85571bc7 | bellard | preliminary :
|
13 | 85571bc7 | bellard | Problem :
|
14 | 85571bc7 | bellard | note:
|
15 | 85571bc7 | bellard | */
|
16 | 85571bc7 | bellard | |
17 | 85571bc7 | bellard | /* This version of fmopl.c is a fork of the MAME one, relicensed under the LGPL.
|
18 | 85571bc7 | bellard | *
|
19 | 85571bc7 | bellard | * This library is free software; you can redistribute it and/or
|
20 | 85571bc7 | bellard | * modify it under the terms of the GNU Lesser General Public
|
21 | 85571bc7 | bellard | * License as published by the Free Software Foundation; either
|
22 | 85571bc7 | bellard | * version 2.1 of the License, or (at your option) any later version.
|
23 | 85571bc7 | bellard | *
|
24 | 85571bc7 | bellard | * This library is distributed in the hope that it will be useful,
|
25 | 85571bc7 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
26 | 85571bc7 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
27 | 85571bc7 | bellard | * Lesser General Public License for more details.
|
28 | 85571bc7 | bellard | *
|
29 | 85571bc7 | bellard | * You should have received a copy of the GNU Lesser General Public
|
30 | 8167ee88 | Blue Swirl | * License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
31 | 85571bc7 | bellard | */
|
32 | 85571bc7 | bellard | |
33 | 947f5fcb | malc | #define INLINE static inline |
34 | 85571bc7 | bellard | #define HAS_YM3812 1 |
35 | 85571bc7 | bellard | |
36 | 85571bc7 | bellard | #include <stdio.h> |
37 | 85571bc7 | bellard | #include <stdlib.h> |
38 | 85571bc7 | bellard | #include <string.h> |
39 | 85571bc7 | bellard | #include <stdarg.h> |
40 | 85571bc7 | bellard | #include <math.h> |
41 | 85571bc7 | bellard | //#include "driver.h" /* use M.A.M.E. */
|
42 | 85571bc7 | bellard | #include "fmopl.h" |
43 | 85571bc7 | bellard | |
44 | 85571bc7 | bellard | #ifndef PI
|
45 | 85571bc7 | bellard | #define PI 3.14159265358979323846 |
46 | 85571bc7 | bellard | #endif
|
47 | 85571bc7 | bellard | |
48 | 85571bc7 | bellard | /* -------------------- for debug --------------------- */
|
49 | 85571bc7 | bellard | /* #define OPL_OUTPUT_LOG */
|
50 | 85571bc7 | bellard | #ifdef OPL_OUTPUT_LOG
|
51 | 85571bc7 | bellard | static FILE *opl_dbg_fp = NULL; |
52 | 85571bc7 | bellard | static FM_OPL *opl_dbg_opl[16]; |
53 | 85571bc7 | bellard | static int opl_dbg_maxchip,opl_dbg_chip; |
54 | 85571bc7 | bellard | #endif
|
55 | 85571bc7 | bellard | |
56 | 85571bc7 | bellard | /* -------------------- preliminary define section --------------------- */
|
57 | 85571bc7 | bellard | /* attack/decay rate time rate */
|
58 | 85571bc7 | bellard | #define OPL_ARRATE 141280 /* RATE 4 = 2826.24ms @ 3.6MHz */ |
59 | 85571bc7 | bellard | #define OPL_DRRATE 1956000 /* RATE 4 = 39280.64ms @ 3.6MHz */ |
60 | 85571bc7 | bellard | |
61 | 85571bc7 | bellard | #define DELTAT_MIXING_LEVEL (1) /* DELTA-T ADPCM MIXING LEVEL */ |
62 | 85571bc7 | bellard | |
63 | 85571bc7 | bellard | #define FREQ_BITS 24 /* frequency turn */ |
64 | 85571bc7 | bellard | |
65 | 85571bc7 | bellard | /* counter bits = 20 , octerve 7 */
|
66 | 85571bc7 | bellard | #define FREQ_RATE (1<<(FREQ_BITS-20)) |
67 | 85571bc7 | bellard | #define TL_BITS (FREQ_BITS+2) |
68 | 85571bc7 | bellard | |
69 | 85571bc7 | bellard | /* final output shift , limit minimum and maximum */
|
70 | 85571bc7 | bellard | #define OPL_OUTSB (TL_BITS+3-16) /* OPL output final shift 16bit */ |
71 | 85571bc7 | bellard | #define OPL_MAXOUT (0x7fff<<OPL_OUTSB) |
72 | 85571bc7 | bellard | #define OPL_MINOUT (-0x8000<<OPL_OUTSB) |
73 | 85571bc7 | bellard | |
74 | 85571bc7 | bellard | /* -------------------- quality selection --------------------- */
|
75 | 85571bc7 | bellard | |
76 | 85571bc7 | bellard | /* sinwave entries */
|
77 | 85571bc7 | bellard | /* used static memory = SIN_ENT * 4 (byte) */
|
78 | 85571bc7 | bellard | #define SIN_ENT 2048 |
79 | 85571bc7 | bellard | |
80 | 85571bc7 | bellard | /* output level entries (envelope,sinwave) */
|
81 | 85571bc7 | bellard | /* envelope counter lower bits */
|
82 | 85571bc7 | bellard | #define ENV_BITS 16 |
83 | 85571bc7 | bellard | /* envelope output entries */
|
84 | 85571bc7 | bellard | #define EG_ENT 4096 |
85 | 85571bc7 | bellard | /* used dynamic memory = EG_ENT*4*4(byte)or EG_ENT*6*4(byte) */
|
86 | 85571bc7 | bellard | /* used static memory = EG_ENT*4 (byte) */
|
87 | 85571bc7 | bellard | |
88 | 85571bc7 | bellard | #define EG_OFF ((2*EG_ENT)<<ENV_BITS) /* OFF */ |
89 | 85571bc7 | bellard | #define EG_DED EG_OFF
|
90 | 85571bc7 | bellard | #define EG_DST (EG_ENT<<ENV_BITS) /* DECAY START */ |
91 | 85571bc7 | bellard | #define EG_AED EG_DST
|
92 | 85571bc7 | bellard | #define EG_AST 0 /* ATTACK START */ |
93 | 85571bc7 | bellard | |
94 | 85571bc7 | bellard | #define EG_STEP (96.0/EG_ENT) /* OPL is 0.1875 dB step */ |
95 | 85571bc7 | bellard | |
96 | 85571bc7 | bellard | /* LFO table entries */
|
97 | 85571bc7 | bellard | #define VIB_ENT 512 |
98 | 85571bc7 | bellard | #define VIB_SHIFT (32-9) |
99 | 85571bc7 | bellard | #define AMS_ENT 512 |
100 | 85571bc7 | bellard | #define AMS_SHIFT (32-9) |
101 | 85571bc7 | bellard | |
102 | 85571bc7 | bellard | #define VIB_RATE 256 |
103 | 85571bc7 | bellard | |
104 | 85571bc7 | bellard | /* -------------------- local defines , macros --------------------- */
|
105 | 85571bc7 | bellard | |
106 | 85571bc7 | bellard | /* register number to channel number , slot offset */
|
107 | 85571bc7 | bellard | #define SLOT1 0 |
108 | 85571bc7 | bellard | #define SLOT2 1 |
109 | 85571bc7 | bellard | |
110 | 85571bc7 | bellard | /* envelope phase */
|
111 | 85571bc7 | bellard | #define ENV_MOD_RR 0x00 |
112 | 85571bc7 | bellard | #define ENV_MOD_DR 0x01 |
113 | 85571bc7 | bellard | #define ENV_MOD_AR 0x02 |
114 | 85571bc7 | bellard | |
115 | 85571bc7 | bellard | /* -------------------- tables --------------------- */
|
116 | 85571bc7 | bellard | static const int slot_array[32]= |
117 | 85571bc7 | bellard | { |
118 | 85571bc7 | bellard | 0, 2, 4, 1, 3, 5,-1,-1, |
119 | 85571bc7 | bellard | 6, 8,10, 7, 9,11,-1,-1, |
120 | 85571bc7 | bellard | 12,14,16,13,15,17,-1,-1, |
121 | 85571bc7 | bellard | -1,-1,-1,-1,-1,-1,-1,-1 |
122 | 85571bc7 | bellard | }; |
123 | 85571bc7 | bellard | |
124 | 85571bc7 | bellard | /* key scale level */
|
125 | 85571bc7 | bellard | /* table is 3dB/OCT , DV converts this in TL step at 6dB/OCT */
|
126 | 85571bc7 | bellard | #define DV (EG_STEP/2) |
127 | 85571bc7 | bellard | static const UINT32 KSL_TABLE[8*16]= |
128 | 85571bc7 | bellard | { |
129 | 85571bc7 | bellard | /* OCT 0 */
|
130 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV, |
131 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV, |
132 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV, |
133 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV, |
134 | 85571bc7 | bellard | /* OCT 1 */
|
135 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV, |
136 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV, |
137 | 85571bc7 | bellard | 0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV, |
138 | 85571bc7 | bellard | 1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV, |
139 | 85571bc7 | bellard | /* OCT 2 */
|
140 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV, |
141 | 85571bc7 | bellard | 0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV, |
142 | 85571bc7 | bellard | 3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV, |
143 | 85571bc7 | bellard | 4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV, |
144 | 85571bc7 | bellard | /* OCT 3 */
|
145 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV, |
146 | 85571bc7 | bellard | 3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV, |
147 | 85571bc7 | bellard | 6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV, |
148 | 85571bc7 | bellard | 7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV, |
149 | 85571bc7 | bellard | /* OCT 4 */
|
150 | 85571bc7 | bellard | 0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV, |
151 | 85571bc7 | bellard | 6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV, |
152 | 85571bc7 | bellard | 9.000/DV, 9.750/DV,10.125/DV,10.500/DV, |
153 | 85571bc7 | bellard | 10.875/DV,11.250/DV,11.625/DV,12.000/DV, |
154 | 85571bc7 | bellard | /* OCT 5 */
|
155 | 85571bc7 | bellard | 0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV, |
156 | 85571bc7 | bellard | 9.000/DV,10.125/DV,10.875/DV,11.625/DV, |
157 | 85571bc7 | bellard | 12.000/DV,12.750/DV,13.125/DV,13.500/DV, |
158 | 85571bc7 | bellard | 13.875/DV,14.250/DV,14.625/DV,15.000/DV, |
159 | 85571bc7 | bellard | /* OCT 6 */
|
160 | 85571bc7 | bellard | 0.000/DV, 6.000/DV, 9.000/DV,10.875/DV, |
161 | 85571bc7 | bellard | 12.000/DV,13.125/DV,13.875/DV,14.625/DV, |
162 | 85571bc7 | bellard | 15.000/DV,15.750/DV,16.125/DV,16.500/DV, |
163 | 85571bc7 | bellard | 16.875/DV,17.250/DV,17.625/DV,18.000/DV, |
164 | 85571bc7 | bellard | /* OCT 7 */
|
165 | 85571bc7 | bellard | 0.000/DV, 9.000/DV,12.000/DV,13.875/DV, |
166 | 85571bc7 | bellard | 15.000/DV,16.125/DV,16.875/DV,17.625/DV, |
167 | 85571bc7 | bellard | 18.000/DV,18.750/DV,19.125/DV,19.500/DV, |
168 | 85571bc7 | bellard | 19.875/DV,20.250/DV,20.625/DV,21.000/DV |
169 | 85571bc7 | bellard | }; |
170 | 85571bc7 | bellard | #undef DV
|
171 | 85571bc7 | bellard | |
172 | 85571bc7 | bellard | /* sustain lebel table (3db per step) */
|
173 | 85571bc7 | bellard | /* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
|
174 | 85571bc7 | bellard | #define SC(db) (db*((3/EG_STEP)*(1<<ENV_BITS)))+EG_DST |
175 | 85571bc7 | bellard | static const INT32 SL_TABLE[16]={ |
176 | 85571bc7 | bellard | SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7), |
177 | 85571bc7 | bellard | SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31) |
178 | 85571bc7 | bellard | }; |
179 | 85571bc7 | bellard | #undef SC
|
180 | 85571bc7 | bellard | |
181 | 85571bc7 | bellard | #define TL_MAX (EG_ENT*2) /* limit(tl + ksr + envelope) + sinwave */ |
182 | 85571bc7 | bellard | /* TotalLevel : 48 24 12 6 3 1.5 0.75 (dB) */
|
183 | 85571bc7 | bellard | /* TL_TABLE[ 0 to TL_MAX ] : plus section */
|
184 | 85571bc7 | bellard | /* TL_TABLE[ TL_MAX to TL_MAX+TL_MAX-1 ] : minus section */
|
185 | 85571bc7 | bellard | static INT32 *TL_TABLE;
|
186 | 85571bc7 | bellard | |
187 | 85571bc7 | bellard | /* pointers to TL_TABLE with sinwave output offset */
|
188 | 85571bc7 | bellard | static INT32 **SIN_TABLE;
|
189 | 85571bc7 | bellard | |
190 | 85571bc7 | bellard | /* LFO table */
|
191 | 85571bc7 | bellard | static INT32 *AMS_TABLE;
|
192 | 85571bc7 | bellard | static INT32 *VIB_TABLE;
|
193 | 85571bc7 | bellard | |
194 | 85571bc7 | bellard | /* envelope output curve table */
|
195 | 85571bc7 | bellard | /* attack + decay + OFF */
|
196 | 85571bc7 | bellard | static INT32 ENV_CURVE[2*EG_ENT+1]; |
197 | 85571bc7 | bellard | |
198 | 85571bc7 | bellard | /* multiple table */
|
199 | 85571bc7 | bellard | #define ML 2 |
200 | 85571bc7 | bellard | static const UINT32 MUL_TABLE[16]= { |
201 | 85571bc7 | bellard | /* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15 */
|
202 | 85571bc7 | bellard | 0.50*ML, 1.00*ML, 2.00*ML, 3.00*ML, 4.00*ML, 5.00*ML, 6.00*ML, 7.00*ML, |
203 | 85571bc7 | bellard | 8.00*ML, 9.00*ML,10.00*ML,10.00*ML,12.00*ML,12.00*ML,15.00*ML,15.00*ML |
204 | 85571bc7 | bellard | }; |
205 | 85571bc7 | bellard | #undef ML
|
206 | 85571bc7 | bellard | |
207 | 85571bc7 | bellard | /* dummy attack / decay rate ( when rate == 0 ) */
|
208 | 85571bc7 | bellard | static INT32 RATE_0[16]= |
209 | 85571bc7 | bellard | {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
210 | 85571bc7 | bellard | |
211 | 85571bc7 | bellard | /* -------------------- static state --------------------- */
|
212 | 85571bc7 | bellard | |
213 | 85571bc7 | bellard | /* lock level of common table */
|
214 | 85571bc7 | bellard | static int num_lock = 0; |
215 | 85571bc7 | bellard | |
216 | 85571bc7 | bellard | /* work table */
|
217 | 85571bc7 | bellard | static void *cur_chip = NULL; /* current chip point */ |
218 | 85571bc7 | bellard | /* currenct chip state */
|
219 | 85571bc7 | bellard | /* static OPLSAMPLE *bufL,*bufR; */
|
220 | 85571bc7 | bellard | static OPL_CH *S_CH;
|
221 | 85571bc7 | bellard | static OPL_CH *E_CH;
|
222 | 85571bc7 | bellard | OPL_SLOT *SLOT7_1,*SLOT7_2,*SLOT8_1,*SLOT8_2; |
223 | 85571bc7 | bellard | |
224 | 85571bc7 | bellard | static INT32 outd[1]; |
225 | 85571bc7 | bellard | static INT32 ams;
|
226 | 85571bc7 | bellard | static INT32 vib;
|
227 | 85571bc7 | bellard | INT32 *ams_table; |
228 | 85571bc7 | bellard | INT32 *vib_table; |
229 | 85571bc7 | bellard | static INT32 amsIncr;
|
230 | 85571bc7 | bellard | static INT32 vibIncr;
|
231 | 85571bc7 | bellard | static INT32 feedback2; /* connect for SLOT 2 */ |
232 | 85571bc7 | bellard | |
233 | 85571bc7 | bellard | /* log output level */
|
234 | 85571bc7 | bellard | #define LOG_ERR 3 /* ERROR */ |
235 | 85571bc7 | bellard | #define LOG_WAR 2 /* WARNING */ |
236 | 85571bc7 | bellard | #define LOG_INF 1 /* INFORMATION */ |
237 | 85571bc7 | bellard | |
238 | 85571bc7 | bellard | //#define LOG_LEVEL LOG_INF
|
239 | 85571bc7 | bellard | #define LOG_LEVEL LOG_ERR
|
240 | 85571bc7 | bellard | |
241 | 85571bc7 | bellard | //#define LOG(n,x) if( (n)>=LOG_LEVEL ) logerror x
|
242 | 85571bc7 | bellard | #define LOG(n,x)
|
243 | 85571bc7 | bellard | |
244 | 85571bc7 | bellard | /* --------------------- subroutines --------------------- */
|
245 | 85571bc7 | bellard | |
246 | 85571bc7 | bellard | INLINE int Limit( int val, int max, int min ) { |
247 | 85571bc7 | bellard | if ( val > max )
|
248 | 85571bc7 | bellard | val = max; |
249 | 85571bc7 | bellard | else if ( val < min ) |
250 | 85571bc7 | bellard | val = min; |
251 | 85571bc7 | bellard | |
252 | 85571bc7 | bellard | return val;
|
253 | 85571bc7 | bellard | } |
254 | 85571bc7 | bellard | |
255 | 85571bc7 | bellard | /* status set and IRQ handling */
|
256 | 85571bc7 | bellard | INLINE void OPL_STATUS_SET(FM_OPL *OPL,int flag) |
257 | 85571bc7 | bellard | { |
258 | 85571bc7 | bellard | /* set status flag */
|
259 | 85571bc7 | bellard | OPL->status |= flag; |
260 | 85571bc7 | bellard | if(!(OPL->status & 0x80)) |
261 | 85571bc7 | bellard | { |
262 | 85571bc7 | bellard | if(OPL->status & OPL->statusmask)
|
263 | 85571bc7 | bellard | { /* IRQ on */
|
264 | 85571bc7 | bellard | OPL->status |= 0x80;
|
265 | 85571bc7 | bellard | /* callback user interrupt handler (IRQ is OFF to ON) */
|
266 | 85571bc7 | bellard | if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,1); |
267 | 85571bc7 | bellard | } |
268 | 85571bc7 | bellard | } |
269 | 85571bc7 | bellard | } |
270 | 85571bc7 | bellard | |
271 | 85571bc7 | bellard | /* status reset and IRQ handling */
|
272 | 85571bc7 | bellard | INLINE void OPL_STATUS_RESET(FM_OPL *OPL,int flag) |
273 | 85571bc7 | bellard | { |
274 | 85571bc7 | bellard | /* reset status flag */
|
275 | 85571bc7 | bellard | OPL->status &=~flag; |
276 | 85571bc7 | bellard | if((OPL->status & 0x80)) |
277 | 85571bc7 | bellard | { |
278 | 85571bc7 | bellard | if (!(OPL->status & OPL->statusmask) )
|
279 | 85571bc7 | bellard | { |
280 | 85571bc7 | bellard | OPL->status &= 0x7f;
|
281 | 85571bc7 | bellard | /* callback user interrupt handler (IRQ is ON to OFF) */
|
282 | 85571bc7 | bellard | if(OPL->IRQHandler) (OPL->IRQHandler)(OPL->IRQParam,0); |
283 | 85571bc7 | bellard | } |
284 | 85571bc7 | bellard | } |
285 | 85571bc7 | bellard | } |
286 | 85571bc7 | bellard | |
287 | 85571bc7 | bellard | /* IRQ mask set */
|
288 | 85571bc7 | bellard | INLINE void OPL_STATUSMASK_SET(FM_OPL *OPL,int flag) |
289 | 85571bc7 | bellard | { |
290 | 85571bc7 | bellard | OPL->statusmask = flag; |
291 | 85571bc7 | bellard | /* IRQ handling check */
|
292 | 85571bc7 | bellard | OPL_STATUS_SET(OPL,0);
|
293 | 85571bc7 | bellard | OPL_STATUS_RESET(OPL,0);
|
294 | 85571bc7 | bellard | } |
295 | 85571bc7 | bellard | |
296 | 85571bc7 | bellard | /* ----- key on ----- */
|
297 | 85571bc7 | bellard | INLINE void OPL_KEYON(OPL_SLOT *SLOT)
|
298 | 85571bc7 | bellard | { |
299 | 85571bc7 | bellard | /* sin wave restart */
|
300 | 85571bc7 | bellard | SLOT->Cnt = 0;
|
301 | 85571bc7 | bellard | /* set attack */
|
302 | 85571bc7 | bellard | SLOT->evm = ENV_MOD_AR; |
303 | 85571bc7 | bellard | SLOT->evs = SLOT->evsa; |
304 | 85571bc7 | bellard | SLOT->evc = EG_AST; |
305 | 85571bc7 | bellard | SLOT->eve = EG_AED; |
306 | 85571bc7 | bellard | } |
307 | 85571bc7 | bellard | /* ----- key off ----- */
|
308 | 85571bc7 | bellard | INLINE void OPL_KEYOFF(OPL_SLOT *SLOT)
|
309 | 85571bc7 | bellard | { |
310 | 85571bc7 | bellard | if( SLOT->evm > ENV_MOD_RR)
|
311 | 85571bc7 | bellard | { |
312 | 85571bc7 | bellard | /* set envelope counter from envleope output */
|
313 | 85571bc7 | bellard | SLOT->evm = ENV_MOD_RR; |
314 | 85571bc7 | bellard | if( !(SLOT->evc&EG_DST) )
|
315 | 85571bc7 | bellard | //SLOT->evc = (ENV_CURVE[SLOT->evc>>ENV_BITS]<<ENV_BITS) + EG_DST;
|
316 | 85571bc7 | bellard | SLOT->evc = EG_DST; |
317 | 85571bc7 | bellard | SLOT->eve = EG_DED; |
318 | 85571bc7 | bellard | SLOT->evs = SLOT->evsr; |
319 | 85571bc7 | bellard | } |
320 | 85571bc7 | bellard | } |
321 | 85571bc7 | bellard | |
322 | 85571bc7 | bellard | /* ---------- calcrate Envelope Generator & Phase Generator ---------- */
|
323 | 85571bc7 | bellard | /* return : envelope output */
|
324 | 85571bc7 | bellard | INLINE UINT32 OPL_CALC_SLOT( OPL_SLOT *SLOT ) |
325 | 85571bc7 | bellard | { |
326 | 85571bc7 | bellard | /* calcrate envelope generator */
|
327 | 85571bc7 | bellard | if( (SLOT->evc+=SLOT->evs) >= SLOT->eve )
|
328 | 85571bc7 | bellard | { |
329 | 85571bc7 | bellard | switch( SLOT->evm ){
|
330 | 85571bc7 | bellard | case ENV_MOD_AR: /* ATTACK -> DECAY1 */ |
331 | 85571bc7 | bellard | /* next DR */
|
332 | 85571bc7 | bellard | SLOT->evm = ENV_MOD_DR; |
333 | 85571bc7 | bellard | SLOT->evc = EG_DST; |
334 | 85571bc7 | bellard | SLOT->eve = SLOT->SL; |
335 | 85571bc7 | bellard | SLOT->evs = SLOT->evsd; |
336 | 85571bc7 | bellard | break;
|
337 | 85571bc7 | bellard | case ENV_MOD_DR: /* DECAY -> SL or RR */ |
338 | 85571bc7 | bellard | SLOT->evc = SLOT->SL; |
339 | 85571bc7 | bellard | SLOT->eve = EG_DED; |
340 | 85571bc7 | bellard | if(SLOT->eg_typ)
|
341 | 85571bc7 | bellard | { |
342 | 85571bc7 | bellard | SLOT->evs = 0;
|
343 | 85571bc7 | bellard | } |
344 | 85571bc7 | bellard | else
|
345 | 85571bc7 | bellard | { |
346 | 85571bc7 | bellard | SLOT->evm = ENV_MOD_RR; |
347 | 85571bc7 | bellard | SLOT->evs = SLOT->evsr; |
348 | 85571bc7 | bellard | } |
349 | 85571bc7 | bellard | break;
|
350 | 85571bc7 | bellard | case ENV_MOD_RR: /* RR -> OFF */ |
351 | 85571bc7 | bellard | SLOT->evc = EG_OFF; |
352 | 85571bc7 | bellard | SLOT->eve = EG_OFF+1;
|
353 | 85571bc7 | bellard | SLOT->evs = 0;
|
354 | 85571bc7 | bellard | break;
|
355 | 85571bc7 | bellard | } |
356 | 85571bc7 | bellard | } |
357 | 85571bc7 | bellard | /* calcrate envelope */
|
358 | 85571bc7 | bellard | return SLOT->TLL+ENV_CURVE[SLOT->evc>>ENV_BITS]+(SLOT->ams ? ams : 0); |
359 | 85571bc7 | bellard | } |
360 | 85571bc7 | bellard | |
361 | 85571bc7 | bellard | /* set algorythm connection */
|
362 | 85571bc7 | bellard | static void set_algorythm( OPL_CH *CH) |
363 | 85571bc7 | bellard | { |
364 | 85571bc7 | bellard | INT32 *carrier = &outd[0];
|
365 | 85571bc7 | bellard | CH->connect1 = CH->CON ? carrier : &feedback2; |
366 | 85571bc7 | bellard | CH->connect2 = carrier; |
367 | 85571bc7 | bellard | } |
368 | 85571bc7 | bellard | |
369 | 85571bc7 | bellard | /* ---------- frequency counter for operater update ---------- */
|
370 | 85571bc7 | bellard | INLINE void CALC_FCSLOT(OPL_CH *CH,OPL_SLOT *SLOT)
|
371 | 85571bc7 | bellard | { |
372 | 85571bc7 | bellard | int ksr;
|
373 | 85571bc7 | bellard | |
374 | 85571bc7 | bellard | /* frequency step counter */
|
375 | 85571bc7 | bellard | SLOT->Incr = CH->fc * SLOT->mul; |
376 | 85571bc7 | bellard | ksr = CH->kcode >> SLOT->KSR; |
377 | 85571bc7 | bellard | |
378 | 85571bc7 | bellard | if( SLOT->ksr != ksr )
|
379 | 85571bc7 | bellard | { |
380 | 85571bc7 | bellard | SLOT->ksr = ksr; |
381 | 85571bc7 | bellard | /* attack , decay rate recalcration */
|
382 | 85571bc7 | bellard | SLOT->evsa = SLOT->AR[ksr]; |
383 | 85571bc7 | bellard | SLOT->evsd = SLOT->DR[ksr]; |
384 | 85571bc7 | bellard | SLOT->evsr = SLOT->RR[ksr]; |
385 | 85571bc7 | bellard | } |
386 | 85571bc7 | bellard | SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl); |
387 | 85571bc7 | bellard | } |
388 | 85571bc7 | bellard | |
389 | 85571bc7 | bellard | /* set multi,am,vib,EG-TYP,KSR,mul */
|
390 | 85571bc7 | bellard | INLINE void set_mul(FM_OPL *OPL,int slot,int v) |
391 | 85571bc7 | bellard | { |
392 | 85571bc7 | bellard | OPL_CH *CH = &OPL->P_CH[slot/2];
|
393 | 85571bc7 | bellard | OPL_SLOT *SLOT = &CH->SLOT[slot&1];
|
394 | 85571bc7 | bellard | |
395 | 85571bc7 | bellard | SLOT->mul = MUL_TABLE[v&0x0f];
|
396 | 85571bc7 | bellard | SLOT->KSR = (v&0x10) ? 0 : 2; |
397 | 85571bc7 | bellard | SLOT->eg_typ = (v&0x20)>>5; |
398 | 85571bc7 | bellard | SLOT->vib = (v&0x40);
|
399 | 85571bc7 | bellard | SLOT->ams = (v&0x80);
|
400 | 85571bc7 | bellard | CALC_FCSLOT(CH,SLOT); |
401 | 85571bc7 | bellard | } |
402 | 85571bc7 | bellard | |
403 | 85571bc7 | bellard | /* set ksl & tl */
|
404 | 85571bc7 | bellard | INLINE void set_ksl_tl(FM_OPL *OPL,int slot,int v) |
405 | 85571bc7 | bellard | { |
406 | 85571bc7 | bellard | OPL_CH *CH = &OPL->P_CH[slot/2];
|
407 | 85571bc7 | bellard | OPL_SLOT *SLOT = &CH->SLOT[slot&1];
|
408 | 85571bc7 | bellard | int ksl = v>>6; /* 0 / 1.5 / 3 / 6 db/OCT */ |
409 | 85571bc7 | bellard | |
410 | 85571bc7 | bellard | SLOT->ksl = ksl ? 3-ksl : 31; |
411 | 85571bc7 | bellard | SLOT->TL = (v&0x3f)*(0.75/EG_STEP); /* 0.75db step */ |
412 | 85571bc7 | bellard | |
413 | 85571bc7 | bellard | if( !(OPL->mode&0x80) ) |
414 | 85571bc7 | bellard | { /* not CSM latch total level */
|
415 | 85571bc7 | bellard | SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl); |
416 | 85571bc7 | bellard | } |
417 | 85571bc7 | bellard | } |
418 | 85571bc7 | bellard | |
419 | 85571bc7 | bellard | /* set attack rate & decay rate */
|
420 | 85571bc7 | bellard | INLINE void set_ar_dr(FM_OPL *OPL,int slot,int v) |
421 | 85571bc7 | bellard | { |
422 | 85571bc7 | bellard | OPL_CH *CH = &OPL->P_CH[slot/2];
|
423 | 85571bc7 | bellard | OPL_SLOT *SLOT = &CH->SLOT[slot&1];
|
424 | 85571bc7 | bellard | int ar = v>>4; |
425 | 85571bc7 | bellard | int dr = v&0x0f; |
426 | 85571bc7 | bellard | |
427 | 85571bc7 | bellard | SLOT->AR = ar ? &OPL->AR_TABLE[ar<<2] : RATE_0;
|
428 | 85571bc7 | bellard | SLOT->evsa = SLOT->AR[SLOT->ksr]; |
429 | 85571bc7 | bellard | if( SLOT->evm == ENV_MOD_AR ) SLOT->evs = SLOT->evsa;
|
430 | 85571bc7 | bellard | |
431 | 85571bc7 | bellard | SLOT->DR = dr ? &OPL->DR_TABLE[dr<<2] : RATE_0;
|
432 | 85571bc7 | bellard | SLOT->evsd = SLOT->DR[SLOT->ksr]; |
433 | 85571bc7 | bellard | if( SLOT->evm == ENV_MOD_DR ) SLOT->evs = SLOT->evsd;
|
434 | 85571bc7 | bellard | } |
435 | 85571bc7 | bellard | |
436 | 85571bc7 | bellard | /* set sustain level & release rate */
|
437 | 85571bc7 | bellard | INLINE void set_sl_rr(FM_OPL *OPL,int slot,int v) |
438 | 85571bc7 | bellard | { |
439 | 85571bc7 | bellard | OPL_CH *CH = &OPL->P_CH[slot/2];
|
440 | 85571bc7 | bellard | OPL_SLOT *SLOT = &CH->SLOT[slot&1];
|
441 | 85571bc7 | bellard | int sl = v>>4; |
442 | 85571bc7 | bellard | int rr = v & 0x0f; |
443 | 85571bc7 | bellard | |
444 | 85571bc7 | bellard | SLOT->SL = SL_TABLE[sl]; |
445 | 85571bc7 | bellard | if( SLOT->evm == ENV_MOD_DR ) SLOT->eve = SLOT->SL;
|
446 | 85571bc7 | bellard | SLOT->RR = &OPL->DR_TABLE[rr<<2];
|
447 | 85571bc7 | bellard | SLOT->evsr = SLOT->RR[SLOT->ksr]; |
448 | 85571bc7 | bellard | if( SLOT->evm == ENV_MOD_RR ) SLOT->evs = SLOT->evsr;
|
449 | 85571bc7 | bellard | } |
450 | 85571bc7 | bellard | |
451 | 85571bc7 | bellard | /* operator output calcrator */
|
452 | 85571bc7 | bellard | #define OP_OUT(slot,env,con) slot->wavetable[((slot->Cnt+con)/(0x1000000/SIN_ENT))&(SIN_ENT-1)][env] |
453 | 85571bc7 | bellard | /* ---------- calcrate one of channel ---------- */
|
454 | 85571bc7 | bellard | INLINE void OPL_CALC_CH( OPL_CH *CH )
|
455 | 85571bc7 | bellard | { |
456 | 85571bc7 | bellard | UINT32 env_out; |
457 | 85571bc7 | bellard | OPL_SLOT *SLOT; |
458 | 85571bc7 | bellard | |
459 | 85571bc7 | bellard | feedback2 = 0;
|
460 | 85571bc7 | bellard | /* SLOT 1 */
|
461 | 85571bc7 | bellard | SLOT = &CH->SLOT[SLOT1]; |
462 | 85571bc7 | bellard | env_out=OPL_CALC_SLOT(SLOT); |
463 | 85571bc7 | bellard | if( env_out < EG_ENT-1 ) |
464 | 85571bc7 | bellard | { |
465 | 85571bc7 | bellard | /* PG */
|
466 | 85571bc7 | bellard | if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
|
467 | 85571bc7 | bellard | else SLOT->Cnt += SLOT->Incr;
|
468 | 85571bc7 | bellard | /* connectoion */
|
469 | 85571bc7 | bellard | if(CH->FB)
|
470 | 85571bc7 | bellard | { |
471 | 85571bc7 | bellard | int feedback1 = (CH->op1_out[0]+CH->op1_out[1])>>CH->FB; |
472 | 85571bc7 | bellard | CH->op1_out[1] = CH->op1_out[0]; |
473 | 85571bc7 | bellard | *CH->connect1 += CH->op1_out[0] = OP_OUT(SLOT,env_out,feedback1);
|
474 | 85571bc7 | bellard | } |
475 | 85571bc7 | bellard | else
|
476 | 85571bc7 | bellard | { |
477 | 85571bc7 | bellard | *CH->connect1 += OP_OUT(SLOT,env_out,0);
|
478 | 85571bc7 | bellard | } |
479 | 85571bc7 | bellard | }else
|
480 | 85571bc7 | bellard | { |
481 | 85571bc7 | bellard | CH->op1_out[1] = CH->op1_out[0]; |
482 | 85571bc7 | bellard | CH->op1_out[0] = 0; |
483 | 85571bc7 | bellard | } |
484 | 85571bc7 | bellard | /* SLOT 2 */
|
485 | 85571bc7 | bellard | SLOT = &CH->SLOT[SLOT2]; |
486 | 85571bc7 | bellard | env_out=OPL_CALC_SLOT(SLOT); |
487 | 85571bc7 | bellard | if( env_out < EG_ENT-1 ) |
488 | 85571bc7 | bellard | { |
489 | 85571bc7 | bellard | /* PG */
|
490 | 85571bc7 | bellard | if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
|
491 | 85571bc7 | bellard | else SLOT->Cnt += SLOT->Incr;
|
492 | 85571bc7 | bellard | /* connectoion */
|
493 | 85571bc7 | bellard | outd[0] += OP_OUT(SLOT,env_out, feedback2);
|
494 | 85571bc7 | bellard | } |
495 | 85571bc7 | bellard | } |
496 | 85571bc7 | bellard | |
497 | 85571bc7 | bellard | /* ---------- calcrate rythm block ---------- */
|
498 | 85571bc7 | bellard | #define WHITE_NOISE_db 6.0 |
499 | 85571bc7 | bellard | INLINE void OPL_CALC_RH( OPL_CH *CH )
|
500 | 85571bc7 | bellard | { |
501 | 85571bc7 | bellard | UINT32 env_tam,env_sd,env_top,env_hh; |
502 | 85571bc7 | bellard | int whitenoise = (rand()&1)*(WHITE_NOISE_db/EG_STEP); |
503 | 85571bc7 | bellard | INT32 tone8; |
504 | 85571bc7 | bellard | |
505 | 85571bc7 | bellard | OPL_SLOT *SLOT; |
506 | 85571bc7 | bellard | int env_out;
|
507 | 85571bc7 | bellard | |
508 | 85571bc7 | bellard | /* BD : same as FM serial mode and output level is large */
|
509 | 85571bc7 | bellard | feedback2 = 0;
|
510 | 85571bc7 | bellard | /* SLOT 1 */
|
511 | 85571bc7 | bellard | SLOT = &CH[6].SLOT[SLOT1];
|
512 | 85571bc7 | bellard | env_out=OPL_CALC_SLOT(SLOT); |
513 | 85571bc7 | bellard | if( env_out < EG_ENT-1 ) |
514 | 85571bc7 | bellard | { |
515 | 85571bc7 | bellard | /* PG */
|
516 | 85571bc7 | bellard | if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
|
517 | 85571bc7 | bellard | else SLOT->Cnt += SLOT->Incr;
|
518 | 85571bc7 | bellard | /* connectoion */
|
519 | 85571bc7 | bellard | if(CH[6].FB) |
520 | 85571bc7 | bellard | { |
521 | 85571bc7 | bellard | int feedback1 = (CH[6].op1_out[0]+CH[6].op1_out[1])>>CH[6].FB; |
522 | 85571bc7 | bellard | CH[6].op1_out[1] = CH[6].op1_out[0]; |
523 | 85571bc7 | bellard | feedback2 = CH[6].op1_out[0] = OP_OUT(SLOT,env_out,feedback1); |
524 | 85571bc7 | bellard | } |
525 | 85571bc7 | bellard | else
|
526 | 85571bc7 | bellard | { |
527 | 85571bc7 | bellard | feedback2 = OP_OUT(SLOT,env_out,0);
|
528 | 85571bc7 | bellard | } |
529 | 85571bc7 | bellard | }else
|
530 | 85571bc7 | bellard | { |
531 | 85571bc7 | bellard | feedback2 = 0;
|
532 | 85571bc7 | bellard | CH[6].op1_out[1] = CH[6].op1_out[0]; |
533 | 85571bc7 | bellard | CH[6].op1_out[0] = 0; |
534 | 85571bc7 | bellard | } |
535 | 85571bc7 | bellard | /* SLOT 2 */
|
536 | 85571bc7 | bellard | SLOT = &CH[6].SLOT[SLOT2];
|
537 | 85571bc7 | bellard | env_out=OPL_CALC_SLOT(SLOT); |
538 | 85571bc7 | bellard | if( env_out < EG_ENT-1 ) |
539 | 85571bc7 | bellard | { |
540 | 85571bc7 | bellard | /* PG */
|
541 | 85571bc7 | bellard | if(SLOT->vib) SLOT->Cnt += (SLOT->Incr*vib/VIB_RATE);
|
542 | 85571bc7 | bellard | else SLOT->Cnt += SLOT->Incr;
|
543 | 85571bc7 | bellard | /* connectoion */
|
544 | 85571bc7 | bellard | outd[0] += OP_OUT(SLOT,env_out, feedback2)*2; |
545 | 85571bc7 | bellard | } |
546 | 85571bc7 | bellard | |
547 | 85571bc7 | bellard | // SD (17) = mul14[fnum7] + white noise
|
548 | 85571bc7 | bellard | // TAM (15) = mul15[fnum8]
|
549 | 85571bc7 | bellard | // TOP (18) = fnum6(mul18[fnum8]+whitenoise)
|
550 | 85571bc7 | bellard | // HH (14) = fnum7(mul18[fnum8]+whitenoise) + white noise
|
551 | 85571bc7 | bellard | env_sd =OPL_CALC_SLOT(SLOT7_2) + whitenoise; |
552 | 85571bc7 | bellard | env_tam=OPL_CALC_SLOT(SLOT8_1); |
553 | 85571bc7 | bellard | env_top=OPL_CALC_SLOT(SLOT8_2); |
554 | 85571bc7 | bellard | env_hh =OPL_CALC_SLOT(SLOT7_1) + whitenoise; |
555 | 85571bc7 | bellard | |
556 | 85571bc7 | bellard | /* PG */
|
557 | 85571bc7 | bellard | if(SLOT7_1->vib) SLOT7_1->Cnt += (2*SLOT7_1->Incr*vib/VIB_RATE); |
558 | 85571bc7 | bellard | else SLOT7_1->Cnt += 2*SLOT7_1->Incr; |
559 | 85571bc7 | bellard | if(SLOT7_2->vib) SLOT7_2->Cnt += ((CH[7].fc*8)*vib/VIB_RATE); |
560 | 85571bc7 | bellard | else SLOT7_2->Cnt += (CH[7].fc*8); |
561 | 85571bc7 | bellard | if(SLOT8_1->vib) SLOT8_1->Cnt += (SLOT8_1->Incr*vib/VIB_RATE);
|
562 | 85571bc7 | bellard | else SLOT8_1->Cnt += SLOT8_1->Incr;
|
563 | 85571bc7 | bellard | if(SLOT8_2->vib) SLOT8_2->Cnt += ((CH[8].fc*48)*vib/VIB_RATE); |
564 | 85571bc7 | bellard | else SLOT8_2->Cnt += (CH[8].fc*48); |
565 | 85571bc7 | bellard | |
566 | 85571bc7 | bellard | tone8 = OP_OUT(SLOT8_2,whitenoise,0 );
|
567 | 85571bc7 | bellard | |
568 | 85571bc7 | bellard | /* SD */
|
569 | 85571bc7 | bellard | if( env_sd < EG_ENT-1 ) |
570 | 85571bc7 | bellard | outd[0] += OP_OUT(SLOT7_1,env_sd, 0)*8; |
571 | 85571bc7 | bellard | /* TAM */
|
572 | 85571bc7 | bellard | if( env_tam < EG_ENT-1 ) |
573 | 85571bc7 | bellard | outd[0] += OP_OUT(SLOT8_1,env_tam, 0)*2; |
574 | 85571bc7 | bellard | /* TOP-CY */
|
575 | 85571bc7 | bellard | if( env_top < EG_ENT-1 ) |
576 | 85571bc7 | bellard | outd[0] += OP_OUT(SLOT7_2,env_top,tone8)*2; |
577 | 85571bc7 | bellard | /* HH */
|
578 | 85571bc7 | bellard | if( env_hh < EG_ENT-1 ) |
579 | 85571bc7 | bellard | outd[0] += OP_OUT(SLOT7_2,env_hh,tone8)*2; |
580 | 85571bc7 | bellard | } |
581 | 85571bc7 | bellard | |
582 | 85571bc7 | bellard | /* ----------- initialize time tabls ----------- */
|
583 | 85571bc7 | bellard | static void init_timetables( FM_OPL *OPL , int ARRATE , int DRRATE ) |
584 | 85571bc7 | bellard | { |
585 | 85571bc7 | bellard | int i;
|
586 | 85571bc7 | bellard | double rate;
|
587 | 85571bc7 | bellard | |
588 | 85571bc7 | bellard | /* make attack rate & decay rate tables */
|
589 | 85571bc7 | bellard | for (i = 0;i < 4;i++) OPL->AR_TABLE[i] = OPL->DR_TABLE[i] = 0; |
590 | 85571bc7 | bellard | for (i = 4;i <= 60;i++){ |
591 | 85571bc7 | bellard | rate = OPL->freqbase; /* frequency rate */
|
592 | 85571bc7 | bellard | if( i < 60 ) rate *= 1.0+(i&3)*0.25; /* b0-1 : x1 , x1.25 , x1.5 , x1.75 */ |
593 | 85571bc7 | bellard | rate *= 1<<((i>>2)-1); /* b2-5 : shift bit */ |
594 | 85571bc7 | bellard | rate *= (double)(EG_ENT<<ENV_BITS);
|
595 | 85571bc7 | bellard | OPL->AR_TABLE[i] = rate / ARRATE; |
596 | 85571bc7 | bellard | OPL->DR_TABLE[i] = rate / DRRATE; |
597 | 85571bc7 | bellard | } |
598 | 85571bc7 | bellard | for (i = 60;i < 76;i++) |
599 | 85571bc7 | bellard | { |
600 | 85571bc7 | bellard | OPL->AR_TABLE[i] = EG_AED-1;
|
601 | 85571bc7 | bellard | OPL->DR_TABLE[i] = OPL->DR_TABLE[60];
|
602 | 85571bc7 | bellard | } |
603 | 85571bc7 | bellard | #if 0
|
604 | 85571bc7 | bellard | for (i = 0;i < 64 ;i++){ /* make for overflow area */
|
605 | 85571bc7 | bellard | LOG(LOG_WAR,("rate %2d , ar %f ms , dr %f ms \n",i,
|
606 | 85571bc7 | bellard | ((double)(EG_ENT<<ENV_BITS) / OPL->AR_TABLE[i]) * (1000.0 / OPL->rate),
|
607 | 85571bc7 | bellard | ((double)(EG_ENT<<ENV_BITS) / OPL->DR_TABLE[i]) * (1000.0 / OPL->rate) ));
|
608 | 85571bc7 | bellard | }
|
609 | 85571bc7 | bellard | #endif
|
610 | 85571bc7 | bellard | } |
611 | 85571bc7 | bellard | |
612 | 85571bc7 | bellard | /* ---------- generic table initialize ---------- */
|
613 | 85571bc7 | bellard | static int OPLOpenTable( void ) |
614 | 85571bc7 | bellard | { |
615 | 85571bc7 | bellard | int s,t;
|
616 | 85571bc7 | bellard | double rate;
|
617 | 85571bc7 | bellard | int i,j;
|
618 | 85571bc7 | bellard | double pom;
|
619 | 85571bc7 | bellard | |
620 | 85571bc7 | bellard | /* allocate dynamic tables */
|
621 | 809c130c | aliguori | if( (TL_TABLE = malloc(TL_MAX*2*sizeof(INT32))) == NULL) |
622 | 809c130c | aliguori | return 0; |
623 | 809c130c | aliguori | if( (SIN_TABLE = malloc(SIN_ENT*4 *sizeof(INT32 *))) == NULL) |
624 | 809c130c | aliguori | { |
625 | 809c130c | aliguori | free(TL_TABLE); |
626 | 809c130c | aliguori | return 0; |
627 | 809c130c | aliguori | } |
628 | 809c130c | aliguori | if( (AMS_TABLE = malloc(AMS_ENT*2 *sizeof(INT32))) == NULL) |
629 | 809c130c | aliguori | { |
630 | 809c130c | aliguori | free(TL_TABLE); |
631 | 809c130c | aliguori | free(SIN_TABLE); |
632 | 809c130c | aliguori | return 0; |
633 | 809c130c | aliguori | } |
634 | 809c130c | aliguori | if( (VIB_TABLE = malloc(VIB_ENT*2 *sizeof(INT32))) == NULL) |
635 | 809c130c | aliguori | { |
636 | 809c130c | aliguori | free(TL_TABLE); |
637 | 809c130c | aliguori | free(SIN_TABLE); |
638 | 809c130c | aliguori | free(AMS_TABLE); |
639 | 809c130c | aliguori | return 0; |
640 | 809c130c | aliguori | } |
641 | 85571bc7 | bellard | /* make total level table */
|
642 | 85571bc7 | bellard | for (t = 0;t < EG_ENT-1 ;t++){ |
643 | 85571bc7 | bellard | rate = ((1<<TL_BITS)-1)/pow(10,EG_STEP*t/20); /* dB -> voltage */ |
644 | 85571bc7 | bellard | TL_TABLE[ t] = (int)rate;
|
645 | 85571bc7 | bellard | TL_TABLE[TL_MAX+t] = -TL_TABLE[t]; |
646 | 85571bc7 | bellard | /* LOG(LOG_INF,("TotalLevel(%3d) = %x\n",t,TL_TABLE[t]));*/
|
647 | 85571bc7 | bellard | } |
648 | 85571bc7 | bellard | /* fill volume off area */
|
649 | 85571bc7 | bellard | for ( t = EG_ENT-1; t < TL_MAX ;t++){ |
650 | 85571bc7 | bellard | TL_TABLE[t] = TL_TABLE[TL_MAX+t] = 0;
|
651 | 85571bc7 | bellard | } |
652 | 85571bc7 | bellard | |
653 | 85571bc7 | bellard | /* make sinwave table (total level offet) */
|
654 | 85571bc7 | bellard | /* degree 0 = degree 180 = off */
|
655 | 85571bc7 | bellard | SIN_TABLE[0] = SIN_TABLE[SIN_ENT/2] = &TL_TABLE[EG_ENT-1]; |
656 | 85571bc7 | bellard | for (s = 1;s <= SIN_ENT/4;s++){ |
657 | 85571bc7 | bellard | pom = sin(2*PI*s/SIN_ENT); /* sin */ |
658 | 85571bc7 | bellard | pom = 20*log10(1/pom); /* decibel */ |
659 | 85571bc7 | bellard | j = pom / EG_STEP; /* TL_TABLE steps */
|
660 | 85571bc7 | bellard | |
661 | 85571bc7 | bellard | /* degree 0 - 90 , degree 180 - 90 : plus section */
|
662 | 85571bc7 | bellard | SIN_TABLE[ s] = SIN_TABLE[SIN_ENT/2-s] = &TL_TABLE[j];
|
663 | 85571bc7 | bellard | /* degree 180 - 270 , degree 360 - 270 : minus section */
|
664 | 85571bc7 | bellard | SIN_TABLE[SIN_ENT/2+s] = SIN_TABLE[SIN_ENT -s] = &TL_TABLE[TL_MAX+j];
|
665 | 85571bc7 | bellard | /* LOG(LOG_INF,("sin(%3d) = %f:%f db\n",s,pom,(double)j * EG_STEP));*/
|
666 | 85571bc7 | bellard | } |
667 | 85571bc7 | bellard | for (s = 0;s < SIN_ENT;s++) |
668 | 85571bc7 | bellard | { |
669 | 85571bc7 | bellard | SIN_TABLE[SIN_ENT*1+s] = s<(SIN_ENT/2) ? SIN_TABLE[s] : &TL_TABLE[EG_ENT]; |
670 | 85571bc7 | bellard | SIN_TABLE[SIN_ENT*2+s] = SIN_TABLE[s % (SIN_ENT/2)]; |
671 | 85571bc7 | bellard | SIN_TABLE[SIN_ENT*3+s] = (s/(SIN_ENT/4))&1 ? &TL_TABLE[EG_ENT] : SIN_TABLE[SIN_ENT*2+s]; |
672 | 85571bc7 | bellard | } |
673 | 85571bc7 | bellard | |
674 | 85571bc7 | bellard | /* envelope counter -> envelope output table */
|
675 | 85571bc7 | bellard | for (i=0; i<EG_ENT; i++) |
676 | 85571bc7 | bellard | { |
677 | 85571bc7 | bellard | /* ATTACK curve */
|
678 | 85571bc7 | bellard | pom = pow( ((double)(EG_ENT-1-i)/EG_ENT) , 8 ) * EG_ENT; |
679 | 85571bc7 | bellard | /* if( pom >= EG_ENT ) pom = EG_ENT-1; */
|
680 | 85571bc7 | bellard | ENV_CURVE[i] = (int)pom;
|
681 | 85571bc7 | bellard | /* DECAY ,RELEASE curve */
|
682 | 85571bc7 | bellard | ENV_CURVE[(EG_DST>>ENV_BITS)+i]= i; |
683 | 85571bc7 | bellard | } |
684 | 85571bc7 | bellard | /* off */
|
685 | 85571bc7 | bellard | ENV_CURVE[EG_OFF>>ENV_BITS]= EG_ENT-1;
|
686 | 85571bc7 | bellard | /* make LFO ams table */
|
687 | 85571bc7 | bellard | for (i=0; i<AMS_ENT; i++) |
688 | 85571bc7 | bellard | { |
689 | 85571bc7 | bellard | pom = (1.0+sin(2*PI*i/AMS_ENT))/2; /* sin */ |
690 | 85571bc7 | bellard | AMS_TABLE[i] = (1.0/EG_STEP)*pom; /* 1dB */ |
691 | 85571bc7 | bellard | AMS_TABLE[AMS_ENT+i] = (4.8/EG_STEP)*pom; /* 4.8dB */ |
692 | 85571bc7 | bellard | } |
693 | 85571bc7 | bellard | /* make LFO vibrate table */
|
694 | 85571bc7 | bellard | for (i=0; i<VIB_ENT; i++) |
695 | 85571bc7 | bellard | { |
696 | 85571bc7 | bellard | /* 100cent = 1seminote = 6% ?? */
|
697 | 85571bc7 | bellard | pom = (double)VIB_RATE*0.06*sin(2*PI*i/VIB_ENT); /* +-100sect step */ |
698 | 85571bc7 | bellard | VIB_TABLE[i] = VIB_RATE + (pom*0.07); /* +- 7cent */ |
699 | 85571bc7 | bellard | VIB_TABLE[VIB_ENT+i] = VIB_RATE + (pom*0.14); /* +-14cent */ |
700 | 85571bc7 | bellard | /* LOG(LOG_INF,("vib %d=%d\n",i,VIB_TABLE[VIB_ENT+i])); */
|
701 | 85571bc7 | bellard | } |
702 | 85571bc7 | bellard | return 1; |
703 | 85571bc7 | bellard | } |
704 | 85571bc7 | bellard | |
705 | 85571bc7 | bellard | |
706 | 85571bc7 | bellard | static void OPLCloseTable( void ) |
707 | 85571bc7 | bellard | { |
708 | 85571bc7 | bellard | free(TL_TABLE); |
709 | 85571bc7 | bellard | free(SIN_TABLE); |
710 | 85571bc7 | bellard | free(AMS_TABLE); |
711 | 85571bc7 | bellard | free(VIB_TABLE); |
712 | 85571bc7 | bellard | } |
713 | 85571bc7 | bellard | |
714 | 85571bc7 | bellard | /* CSM Key Controll */
|
715 | 85571bc7 | bellard | INLINE void CSMKeyControll(OPL_CH *CH)
|
716 | 85571bc7 | bellard | { |
717 | 85571bc7 | bellard | OPL_SLOT *slot1 = &CH->SLOT[SLOT1]; |
718 | 85571bc7 | bellard | OPL_SLOT *slot2 = &CH->SLOT[SLOT2]; |
719 | 85571bc7 | bellard | /* all key off */
|
720 | 85571bc7 | bellard | OPL_KEYOFF(slot1); |
721 | 85571bc7 | bellard | OPL_KEYOFF(slot2); |
722 | 85571bc7 | bellard | /* total level latch */
|
723 | 85571bc7 | bellard | slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl); |
724 | 85571bc7 | bellard | slot1->TLL = slot1->TL + (CH->ksl_base>>slot1->ksl); |
725 | 85571bc7 | bellard | /* key on */
|
726 | 85571bc7 | bellard | CH->op1_out[0] = CH->op1_out[1] = 0; |
727 | 85571bc7 | bellard | OPL_KEYON(slot1); |
728 | 85571bc7 | bellard | OPL_KEYON(slot2); |
729 | 85571bc7 | bellard | } |
730 | 85571bc7 | bellard | |
731 | 85571bc7 | bellard | /* ---------- opl initialize ---------- */
|
732 | 85571bc7 | bellard | static void OPL_initalize(FM_OPL *OPL) |
733 | 85571bc7 | bellard | { |
734 | 85571bc7 | bellard | int fn;
|
735 | 85571bc7 | bellard | |
736 | 85571bc7 | bellard | /* frequency base */
|
737 | 85571bc7 | bellard | OPL->freqbase = (OPL->rate) ? ((double)OPL->clock / OPL->rate) / 72 : 0; |
738 | 85571bc7 | bellard | /* Timer base time */
|
739 | 85571bc7 | bellard | OPL->TimerBase = 1.0/((double)OPL->clock / 72.0 ); |
740 | 85571bc7 | bellard | /* make time tables */
|
741 | 85571bc7 | bellard | init_timetables( OPL , OPL_ARRATE , OPL_DRRATE ); |
742 | 85571bc7 | bellard | /* make fnumber -> increment counter table */
|
743 | 85571bc7 | bellard | for( fn=0 ; fn < 1024 ; fn++ ) |
744 | 85571bc7 | bellard | { |
745 | 85571bc7 | bellard | OPL->FN_TABLE[fn] = OPL->freqbase * fn * FREQ_RATE * (1<<7) / 2; |
746 | 85571bc7 | bellard | } |
747 | 85571bc7 | bellard | /* LFO freq.table */
|
748 | 85571bc7 | bellard | OPL->amsIncr = OPL->rate ? (double)AMS_ENT*(1<<AMS_SHIFT) / OPL->rate * 3.7 * ((double)OPL->clock/3600000) : 0; |
749 | 85571bc7 | bellard | OPL->vibIncr = OPL->rate ? (double)VIB_ENT*(1<<VIB_SHIFT) / OPL->rate * 6.4 * ((double)OPL->clock/3600000) : 0; |
750 | 85571bc7 | bellard | } |
751 | 85571bc7 | bellard | |
752 | 85571bc7 | bellard | /* ---------- write a OPL registers ---------- */
|
753 | 85571bc7 | bellard | static void OPLWriteReg(FM_OPL *OPL, int r, int v) |
754 | 85571bc7 | bellard | { |
755 | 85571bc7 | bellard | OPL_CH *CH; |
756 | 85571bc7 | bellard | int slot;
|
757 | 85571bc7 | bellard | int block_fnum;
|
758 | 85571bc7 | bellard | |
759 | 85571bc7 | bellard | switch(r&0xe0) |
760 | 85571bc7 | bellard | { |
761 | 85571bc7 | bellard | case 0x00: /* 00-1f:controll */ |
762 | 85571bc7 | bellard | switch(r&0x1f) |
763 | 85571bc7 | bellard | { |
764 | 85571bc7 | bellard | case 0x01: |
765 | 85571bc7 | bellard | /* wave selector enable */
|
766 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_WAVESEL)
|
767 | 85571bc7 | bellard | { |
768 | 85571bc7 | bellard | OPL->wavesel = v&0x20;
|
769 | 85571bc7 | bellard | if(!OPL->wavesel)
|
770 | 85571bc7 | bellard | { |
771 | 85571bc7 | bellard | /* preset compatible mode */
|
772 | 85571bc7 | bellard | int c;
|
773 | 85571bc7 | bellard | for(c=0;c<OPL->max_ch;c++) |
774 | 85571bc7 | bellard | { |
775 | 85571bc7 | bellard | OPL->P_CH[c].SLOT[SLOT1].wavetable = &SIN_TABLE[0];
|
776 | 85571bc7 | bellard | OPL->P_CH[c].SLOT[SLOT2].wavetable = &SIN_TABLE[0];
|
777 | 85571bc7 | bellard | } |
778 | 85571bc7 | bellard | } |
779 | 85571bc7 | bellard | } |
780 | 85571bc7 | bellard | return;
|
781 | 85571bc7 | bellard | case 0x02: /* Timer 1 */ |
782 | 85571bc7 | bellard | OPL->T[0] = (256-v)*4; |
783 | 85571bc7 | bellard | break;
|
784 | 85571bc7 | bellard | case 0x03: /* Timer 2 */ |
785 | 85571bc7 | bellard | OPL->T[1] = (256-v)*16; |
786 | 85571bc7 | bellard | return;
|
787 | 85571bc7 | bellard | case 0x04: /* IRQ clear / mask and Timer enable */ |
788 | 85571bc7 | bellard | if(v&0x80) |
789 | 85571bc7 | bellard | { /* IRQ flag clear */
|
790 | 85571bc7 | bellard | OPL_STATUS_RESET(OPL,0x7f);
|
791 | 85571bc7 | bellard | } |
792 | 85571bc7 | bellard | else
|
793 | 85571bc7 | bellard | { /* set IRQ mask ,timer enable*/
|
794 | 85571bc7 | bellard | UINT8 st1 = v&1;
|
795 | 85571bc7 | bellard | UINT8 st2 = (v>>1)&1; |
796 | 85571bc7 | bellard | /* IRQRST,T1MSK,t2MSK,EOSMSK,BRMSK,x,ST2,ST1 */
|
797 | 85571bc7 | bellard | OPL_STATUS_RESET(OPL,v&0x78);
|
798 | 85571bc7 | bellard | OPL_STATUSMASK_SET(OPL,((~v)&0x78)|0x01); |
799 | 85571bc7 | bellard | /* timer 2 */
|
800 | 85571bc7 | bellard | if(OPL->st[1] != st2) |
801 | 85571bc7 | bellard | { |
802 | 85571bc7 | bellard | double interval = st2 ? (double)OPL->T[1]*OPL->TimerBase : 0.0; |
803 | 85571bc7 | bellard | OPL->st[1] = st2;
|
804 | 85571bc7 | bellard | if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+1,interval); |
805 | 85571bc7 | bellard | } |
806 | 85571bc7 | bellard | /* timer 1 */
|
807 | 85571bc7 | bellard | if(OPL->st[0] != st1) |
808 | 85571bc7 | bellard | { |
809 | 85571bc7 | bellard | double interval = st1 ? (double)OPL->T[0]*OPL->TimerBase : 0.0; |
810 | 85571bc7 | bellard | OPL->st[0] = st1;
|
811 | 85571bc7 | bellard | if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+0,interval); |
812 | 85571bc7 | bellard | } |
813 | 85571bc7 | bellard | } |
814 | 85571bc7 | bellard | return;
|
815 | 85571bc7 | bellard | #if BUILD_Y8950
|
816 | 85571bc7 | bellard | case 0x06: /* Key Board OUT */ |
817 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_KEYBOARD)
|
818 | 85571bc7 | bellard | { |
819 | 85571bc7 | bellard | if(OPL->keyboardhandler_w)
|
820 | 85571bc7 | bellard | OPL->keyboardhandler_w(OPL->keyboard_param,v); |
821 | 85571bc7 | bellard | else
|
822 | 85571bc7 | bellard | LOG(LOG_WAR,("OPL:write unmapped KEYBOARD port\n"));
|
823 | 85571bc7 | bellard | } |
824 | 85571bc7 | bellard | return;
|
825 | 85571bc7 | bellard | case 0x07: /* DELTA-T controll : START,REC,MEMDATA,REPT,SPOFF,x,x,RST */ |
826 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_ADPCM)
|
827 | 85571bc7 | bellard | YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
|
828 | 85571bc7 | bellard | return;
|
829 | 85571bc7 | bellard | case 0x08: /* MODE,DELTA-T : CSM,NOTESEL,x,x,smpl,da/ad,64k,rom */ |
830 | 85571bc7 | bellard | OPL->mode = v; |
831 | 85571bc7 | bellard | v&=0x1f; /* for DELTA-T unit */ |
832 | 85571bc7 | bellard | case 0x09: /* START ADD */ |
833 | 85571bc7 | bellard | case 0x0a: |
834 | 85571bc7 | bellard | case 0x0b: /* STOP ADD */ |
835 | 85571bc7 | bellard | case 0x0c: |
836 | 85571bc7 | bellard | case 0x0d: /* PRESCALE */ |
837 | 85571bc7 | bellard | case 0x0e: |
838 | 85571bc7 | bellard | case 0x0f: /* ADPCM data */ |
839 | 85571bc7 | bellard | case 0x10: /* DELTA-N */ |
840 | 85571bc7 | bellard | case 0x11: /* DELTA-N */ |
841 | 85571bc7 | bellard | case 0x12: /* EG-CTRL */ |
842 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_ADPCM)
|
843 | 85571bc7 | bellard | YM_DELTAT_ADPCM_Write(OPL->deltat,r-0x07,v);
|
844 | 85571bc7 | bellard | return;
|
845 | 85571bc7 | bellard | #if 0
|
846 | 85571bc7 | bellard | case 0x15: /* DAC data */
|
847 | 85571bc7 | bellard | case 0x16:
|
848 | 85571bc7 | bellard | case 0x17: /* SHIFT */
|
849 | 85571bc7 | bellard | return;
|
850 | 85571bc7 | bellard | case 0x18: /* I/O CTRL (Direction) */
|
851 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_IO)
|
852 | 85571bc7 | bellard | OPL->portDirection = v&0x0f;
|
853 | 85571bc7 | bellard | return;
|
854 | 85571bc7 | bellard | case 0x19: /* I/O DATA */
|
855 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_IO)
|
856 | 85571bc7 | bellard | {
|
857 | 85571bc7 | bellard | OPL->portLatch = v;
|
858 | 85571bc7 | bellard | if(OPL->porthandler_w)
|
859 | 85571bc7 | bellard | OPL->porthandler_w(OPL->port_param,v&OPL->portDirection);
|
860 | 85571bc7 | bellard | }
|
861 | 85571bc7 | bellard | return;
|
862 | 85571bc7 | bellard | case 0x1a: /* PCM data */
|
863 | 85571bc7 | bellard | return;
|
864 | 85571bc7 | bellard | #endif
|
865 | 85571bc7 | bellard | #endif
|
866 | 85571bc7 | bellard | } |
867 | 85571bc7 | bellard | break;
|
868 | 85571bc7 | bellard | case 0x20: /* am,vib,ksr,eg type,mul */ |
869 | 85571bc7 | bellard | slot = slot_array[r&0x1f];
|
870 | 85571bc7 | bellard | if(slot == -1) return; |
871 | 85571bc7 | bellard | set_mul(OPL,slot,v); |
872 | 85571bc7 | bellard | return;
|
873 | 85571bc7 | bellard | case 0x40: |
874 | 85571bc7 | bellard | slot = slot_array[r&0x1f];
|
875 | 85571bc7 | bellard | if(slot == -1) return; |
876 | 85571bc7 | bellard | set_ksl_tl(OPL,slot,v); |
877 | 85571bc7 | bellard | return;
|
878 | 85571bc7 | bellard | case 0x60: |
879 | 85571bc7 | bellard | slot = slot_array[r&0x1f];
|
880 | 85571bc7 | bellard | if(slot == -1) return; |
881 | 85571bc7 | bellard | set_ar_dr(OPL,slot,v); |
882 | 85571bc7 | bellard | return;
|
883 | 85571bc7 | bellard | case 0x80: |
884 | 85571bc7 | bellard | slot = slot_array[r&0x1f];
|
885 | 85571bc7 | bellard | if(slot == -1) return; |
886 | 85571bc7 | bellard | set_sl_rr(OPL,slot,v); |
887 | 85571bc7 | bellard | return;
|
888 | 85571bc7 | bellard | case 0xa0: |
889 | 85571bc7 | bellard | switch(r)
|
890 | 85571bc7 | bellard | { |
891 | 85571bc7 | bellard | case 0xbd: |
892 | 85571bc7 | bellard | /* amsep,vibdep,r,bd,sd,tom,tc,hh */
|
893 | 85571bc7 | bellard | { |
894 | 85571bc7 | bellard | UINT8 rkey = OPL->rythm^v; |
895 | 85571bc7 | bellard | OPL->ams_table = &AMS_TABLE[v&0x80 ? AMS_ENT : 0]; |
896 | 85571bc7 | bellard | OPL->vib_table = &VIB_TABLE[v&0x40 ? VIB_ENT : 0]; |
897 | 85571bc7 | bellard | OPL->rythm = v&0x3f;
|
898 | 85571bc7 | bellard | if(OPL->rythm&0x20) |
899 | 85571bc7 | bellard | { |
900 | 85571bc7 | bellard | #if 0
|
901 | 85571bc7 | bellard | usrintf_showmessage("OPL Rythm mode select");
|
902 | 85571bc7 | bellard | #endif
|
903 | 85571bc7 | bellard | /* BD key on/off */
|
904 | 85571bc7 | bellard | if(rkey&0x10) |
905 | 85571bc7 | bellard | { |
906 | 85571bc7 | bellard | if(v&0x10) |
907 | 85571bc7 | bellard | { |
908 | 85571bc7 | bellard | OPL->P_CH[6].op1_out[0] = OPL->P_CH[6].op1_out[1] = 0; |
909 | 85571bc7 | bellard | OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT1]);
|
910 | 85571bc7 | bellard | OPL_KEYON(&OPL->P_CH[6].SLOT[SLOT2]);
|
911 | 85571bc7 | bellard | } |
912 | 85571bc7 | bellard | else
|
913 | 85571bc7 | bellard | { |
914 | 85571bc7 | bellard | OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT1]);
|
915 | 85571bc7 | bellard | OPL_KEYOFF(&OPL->P_CH[6].SLOT[SLOT2]);
|
916 | 85571bc7 | bellard | } |
917 | 85571bc7 | bellard | } |
918 | 85571bc7 | bellard | /* SD key on/off */
|
919 | 85571bc7 | bellard | if(rkey&0x08) |
920 | 85571bc7 | bellard | { |
921 | 85571bc7 | bellard | if(v&0x08) OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT2]); |
922 | 85571bc7 | bellard | else OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT2]); |
923 | 85571bc7 | bellard | }/* TAM key on/off */
|
924 | 85571bc7 | bellard | if(rkey&0x04) |
925 | 85571bc7 | bellard | { |
926 | 85571bc7 | bellard | if(v&0x04) OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT1]); |
927 | 85571bc7 | bellard | else OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT1]); |
928 | 85571bc7 | bellard | } |
929 | 85571bc7 | bellard | /* TOP-CY key on/off */
|
930 | 85571bc7 | bellard | if(rkey&0x02) |
931 | 85571bc7 | bellard | { |
932 | 85571bc7 | bellard | if(v&0x02) OPL_KEYON(&OPL->P_CH[8].SLOT[SLOT2]); |
933 | 85571bc7 | bellard | else OPL_KEYOFF(&OPL->P_CH[8].SLOT[SLOT2]); |
934 | 85571bc7 | bellard | } |
935 | 85571bc7 | bellard | /* HH key on/off */
|
936 | 85571bc7 | bellard | if(rkey&0x01) |
937 | 85571bc7 | bellard | { |
938 | 85571bc7 | bellard | if(v&0x01) OPL_KEYON(&OPL->P_CH[7].SLOT[SLOT1]); |
939 | 85571bc7 | bellard | else OPL_KEYOFF(&OPL->P_CH[7].SLOT[SLOT1]); |
940 | 85571bc7 | bellard | } |
941 | 85571bc7 | bellard | } |
942 | 85571bc7 | bellard | } |
943 | 85571bc7 | bellard | return;
|
944 | 85571bc7 | bellard | } |
945 | 85571bc7 | bellard | /* keyon,block,fnum */
|
946 | 85571bc7 | bellard | if( (r&0x0f) > 8) return; |
947 | 85571bc7 | bellard | CH = &OPL->P_CH[r&0x0f];
|
948 | 85571bc7 | bellard | if(!(r&0x10)) |
949 | 85571bc7 | bellard | { /* a0-a8 */
|
950 | 85571bc7 | bellard | block_fnum = (CH->block_fnum&0x1f00) | v;
|
951 | 85571bc7 | bellard | } |
952 | 85571bc7 | bellard | else
|
953 | 85571bc7 | bellard | { /* b0-b8 */
|
954 | 85571bc7 | bellard | int keyon = (v>>5)&1; |
955 | 85571bc7 | bellard | block_fnum = ((v&0x1f)<<8) | (CH->block_fnum&0xff); |
956 | 85571bc7 | bellard | if(CH->keyon != keyon)
|
957 | 85571bc7 | bellard | { |
958 | 85571bc7 | bellard | if( (CH->keyon=keyon) )
|
959 | 85571bc7 | bellard | { |
960 | 85571bc7 | bellard | CH->op1_out[0] = CH->op1_out[1] = 0; |
961 | 85571bc7 | bellard | OPL_KEYON(&CH->SLOT[SLOT1]); |
962 | 85571bc7 | bellard | OPL_KEYON(&CH->SLOT[SLOT2]); |
963 | 85571bc7 | bellard | } |
964 | 85571bc7 | bellard | else
|
965 | 85571bc7 | bellard | { |
966 | 85571bc7 | bellard | OPL_KEYOFF(&CH->SLOT[SLOT1]); |
967 | 85571bc7 | bellard | OPL_KEYOFF(&CH->SLOT[SLOT2]); |
968 | 85571bc7 | bellard | } |
969 | 85571bc7 | bellard | } |
970 | 85571bc7 | bellard | } |
971 | 85571bc7 | bellard | /* update */
|
972 | 85571bc7 | bellard | if(CH->block_fnum != block_fnum)
|
973 | 85571bc7 | bellard | { |
974 | 85571bc7 | bellard | int blockRv = 7-(block_fnum>>10); |
975 | 85571bc7 | bellard | int fnum = block_fnum&0x3ff; |
976 | 85571bc7 | bellard | CH->block_fnum = block_fnum; |
977 | 85571bc7 | bellard | |
978 | 85571bc7 | bellard | CH->ksl_base = KSL_TABLE[block_fnum>>6];
|
979 | 85571bc7 | bellard | CH->fc = OPL->FN_TABLE[fnum]>>blockRv; |
980 | 85571bc7 | bellard | CH->kcode = CH->block_fnum>>9;
|
981 | 85571bc7 | bellard | if( (OPL->mode&0x40) && CH->block_fnum&0x100) CH->kcode |=1; |
982 | 85571bc7 | bellard | CALC_FCSLOT(CH,&CH->SLOT[SLOT1]); |
983 | 85571bc7 | bellard | CALC_FCSLOT(CH,&CH->SLOT[SLOT2]); |
984 | 85571bc7 | bellard | } |
985 | 85571bc7 | bellard | return;
|
986 | 85571bc7 | bellard | case 0xc0: |
987 | 85571bc7 | bellard | /* FB,C */
|
988 | 85571bc7 | bellard | if( (r&0x0f) > 8) return; |
989 | 85571bc7 | bellard | CH = &OPL->P_CH[r&0x0f];
|
990 | 85571bc7 | bellard | { |
991 | 85571bc7 | bellard | int feedback = (v>>1)&7; |
992 | 85571bc7 | bellard | CH->FB = feedback ? (8+1) - feedback : 0; |
993 | 85571bc7 | bellard | CH->CON = v&1;
|
994 | 85571bc7 | bellard | set_algorythm(CH); |
995 | 85571bc7 | bellard | } |
996 | 85571bc7 | bellard | return;
|
997 | 85571bc7 | bellard | case 0xe0: /* wave type */ |
998 | 85571bc7 | bellard | slot = slot_array[r&0x1f];
|
999 | 85571bc7 | bellard | if(slot == -1) return; |
1000 | 85571bc7 | bellard | CH = &OPL->P_CH[slot/2];
|
1001 | 85571bc7 | bellard | if(OPL->wavesel)
|
1002 | 85571bc7 | bellard | { |
1003 | 85571bc7 | bellard | /* LOG(LOG_INF,("OPL SLOT %d wave select %d\n",slot,v&3)); */
|
1004 | 85571bc7 | bellard | CH->SLOT[slot&1].wavetable = &SIN_TABLE[(v&0x03)*SIN_ENT]; |
1005 | 85571bc7 | bellard | } |
1006 | 85571bc7 | bellard | return;
|
1007 | 85571bc7 | bellard | } |
1008 | 85571bc7 | bellard | } |
1009 | 85571bc7 | bellard | |
1010 | 85571bc7 | bellard | /* lock/unlock for common table */
|
1011 | 85571bc7 | bellard | static int OPL_LockTable(void) |
1012 | 85571bc7 | bellard | { |
1013 | 85571bc7 | bellard | num_lock++; |
1014 | 85571bc7 | bellard | if(num_lock>1) return 0; |
1015 | 85571bc7 | bellard | /* first time */
|
1016 | 85571bc7 | bellard | cur_chip = NULL;
|
1017 | 85571bc7 | bellard | /* allocate total level table (128kb space) */
|
1018 | 85571bc7 | bellard | if( !OPLOpenTable() )
|
1019 | 85571bc7 | bellard | { |
1020 | 85571bc7 | bellard | num_lock--; |
1021 | 85571bc7 | bellard | return -1; |
1022 | 85571bc7 | bellard | } |
1023 | 85571bc7 | bellard | return 0; |
1024 | 85571bc7 | bellard | } |
1025 | 85571bc7 | bellard | |
1026 | 85571bc7 | bellard | static void OPL_UnLockTable(void) |
1027 | 85571bc7 | bellard | { |
1028 | 85571bc7 | bellard | if(num_lock) num_lock--;
|
1029 | 85571bc7 | bellard | if(num_lock) return; |
1030 | 85571bc7 | bellard | /* last time */
|
1031 | 85571bc7 | bellard | cur_chip = NULL;
|
1032 | 85571bc7 | bellard | OPLCloseTable(); |
1033 | 85571bc7 | bellard | } |
1034 | 85571bc7 | bellard | |
1035 | 85571bc7 | bellard | #if (BUILD_YM3812 || BUILD_YM3526)
|
1036 | 85571bc7 | bellard | /*******************************************************************************/
|
1037 | 85571bc7 | bellard | /* YM3812 local section */
|
1038 | 85571bc7 | bellard | /*******************************************************************************/
|
1039 | 85571bc7 | bellard | |
1040 | 85571bc7 | bellard | /* ---------- update one of chip ----------- */
|
1041 | 85571bc7 | bellard | void YM3812UpdateOne(FM_OPL *OPL, INT16 *buffer, int length) |
1042 | 85571bc7 | bellard | { |
1043 | 85571bc7 | bellard | int i;
|
1044 | 85571bc7 | bellard | int data;
|
1045 | 85571bc7 | bellard | OPLSAMPLE *buf = buffer; |
1046 | 85571bc7 | bellard | UINT32 amsCnt = OPL->amsCnt; |
1047 | 85571bc7 | bellard | UINT32 vibCnt = OPL->vibCnt; |
1048 | 85571bc7 | bellard | UINT8 rythm = OPL->rythm&0x20;
|
1049 | 85571bc7 | bellard | OPL_CH *CH,*R_CH; |
1050 | 85571bc7 | bellard | |
1051 | 85571bc7 | bellard | if( (void *)OPL != cur_chip ){ |
1052 | 85571bc7 | bellard | cur_chip = (void *)OPL;
|
1053 | 85571bc7 | bellard | /* channel pointers */
|
1054 | 85571bc7 | bellard | S_CH = OPL->P_CH; |
1055 | 85571bc7 | bellard | E_CH = &S_CH[9];
|
1056 | 85571bc7 | bellard | /* rythm slot */
|
1057 | 85571bc7 | bellard | SLOT7_1 = &S_CH[7].SLOT[SLOT1];
|
1058 | 85571bc7 | bellard | SLOT7_2 = &S_CH[7].SLOT[SLOT2];
|
1059 | 85571bc7 | bellard | SLOT8_1 = &S_CH[8].SLOT[SLOT1];
|
1060 | 85571bc7 | bellard | SLOT8_2 = &S_CH[8].SLOT[SLOT2];
|
1061 | 85571bc7 | bellard | /* LFO state */
|
1062 | 85571bc7 | bellard | amsIncr = OPL->amsIncr; |
1063 | 85571bc7 | bellard | vibIncr = OPL->vibIncr; |
1064 | 85571bc7 | bellard | ams_table = OPL->ams_table; |
1065 | 85571bc7 | bellard | vib_table = OPL->vib_table; |
1066 | 85571bc7 | bellard | } |
1067 | 85571bc7 | bellard | R_CH = rythm ? &S_CH[6] : E_CH;
|
1068 | 85571bc7 | bellard | for( i=0; i < length ; i++ ) |
1069 | 85571bc7 | bellard | { |
1070 | 85571bc7 | bellard | /* channel A channel B channel C */
|
1071 | 85571bc7 | bellard | /* LFO */
|
1072 | 85571bc7 | bellard | ams = ams_table[(amsCnt+=amsIncr)>>AMS_SHIFT]; |
1073 | 85571bc7 | bellard | vib = vib_table[(vibCnt+=vibIncr)>>VIB_SHIFT]; |
1074 | 85571bc7 | bellard | outd[0] = 0; |
1075 | 85571bc7 | bellard | /* FM part */
|
1076 | 85571bc7 | bellard | for(CH=S_CH ; CH < R_CH ; CH++)
|
1077 | 85571bc7 | bellard | OPL_CALC_CH(CH); |
1078 | 85571bc7 | bellard | /* Rythn part */
|
1079 | 85571bc7 | bellard | if(rythm)
|
1080 | 85571bc7 | bellard | OPL_CALC_RH(S_CH); |
1081 | 85571bc7 | bellard | /* limit check */
|
1082 | 85571bc7 | bellard | data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
|
1083 | 85571bc7 | bellard | /* store to sound buffer */
|
1084 | 85571bc7 | bellard | buf[i] = data >> OPL_OUTSB; |
1085 | 85571bc7 | bellard | } |
1086 | 85571bc7 | bellard | |
1087 | 85571bc7 | bellard | OPL->amsCnt = amsCnt; |
1088 | 85571bc7 | bellard | OPL->vibCnt = vibCnt; |
1089 | 85571bc7 | bellard | #ifdef OPL_OUTPUT_LOG
|
1090 | 85571bc7 | bellard | if(opl_dbg_fp)
|
1091 | 85571bc7 | bellard | { |
1092 | 85571bc7 | bellard | for(opl_dbg_chip=0;opl_dbg_chip<opl_dbg_maxchip;opl_dbg_chip++) |
1093 | 85571bc7 | bellard | if( opl_dbg_opl[opl_dbg_chip] == OPL) break; |
1094 | 85571bc7 | bellard | fprintf(opl_dbg_fp,"%c%c%c",0x20+opl_dbg_chip,length&0xff,length/256); |
1095 | 85571bc7 | bellard | } |
1096 | 85571bc7 | bellard | #endif
|
1097 | 85571bc7 | bellard | } |
1098 | 85571bc7 | bellard | #endif /* (BUILD_YM3812 || BUILD_YM3526) */ |
1099 | 85571bc7 | bellard | |
1100 | 85571bc7 | bellard | #if BUILD_Y8950
|
1101 | 85571bc7 | bellard | |
1102 | 85571bc7 | bellard | void Y8950UpdateOne(FM_OPL *OPL, INT16 *buffer, int length) |
1103 | 85571bc7 | bellard | { |
1104 | 85571bc7 | bellard | int i;
|
1105 | 85571bc7 | bellard | int data;
|
1106 | 85571bc7 | bellard | OPLSAMPLE *buf = buffer; |
1107 | 85571bc7 | bellard | UINT32 amsCnt = OPL->amsCnt; |
1108 | 85571bc7 | bellard | UINT32 vibCnt = OPL->vibCnt; |
1109 | 85571bc7 | bellard | UINT8 rythm = OPL->rythm&0x20;
|
1110 | 85571bc7 | bellard | OPL_CH *CH,*R_CH; |
1111 | 85571bc7 | bellard | YM_DELTAT *DELTAT = OPL->deltat; |
1112 | 85571bc7 | bellard | |
1113 | 85571bc7 | bellard | /* setup DELTA-T unit */
|
1114 | 85571bc7 | bellard | YM_DELTAT_DECODE_PRESET(DELTAT); |
1115 | 85571bc7 | bellard | |
1116 | 85571bc7 | bellard | if( (void *)OPL != cur_chip ){ |
1117 | 85571bc7 | bellard | cur_chip = (void *)OPL;
|
1118 | 85571bc7 | bellard | /* channel pointers */
|
1119 | 85571bc7 | bellard | S_CH = OPL->P_CH; |
1120 | 85571bc7 | bellard | E_CH = &S_CH[9];
|
1121 | 85571bc7 | bellard | /* rythm slot */
|
1122 | 85571bc7 | bellard | SLOT7_1 = &S_CH[7].SLOT[SLOT1];
|
1123 | 85571bc7 | bellard | SLOT7_2 = &S_CH[7].SLOT[SLOT2];
|
1124 | 85571bc7 | bellard | SLOT8_1 = &S_CH[8].SLOT[SLOT1];
|
1125 | 85571bc7 | bellard | SLOT8_2 = &S_CH[8].SLOT[SLOT2];
|
1126 | 85571bc7 | bellard | /* LFO state */
|
1127 | 85571bc7 | bellard | amsIncr = OPL->amsIncr; |
1128 | 85571bc7 | bellard | vibIncr = OPL->vibIncr; |
1129 | 85571bc7 | bellard | ams_table = OPL->ams_table; |
1130 | 85571bc7 | bellard | vib_table = OPL->vib_table; |
1131 | 85571bc7 | bellard | } |
1132 | 85571bc7 | bellard | R_CH = rythm ? &S_CH[6] : E_CH;
|
1133 | 85571bc7 | bellard | for( i=0; i < length ; i++ ) |
1134 | 85571bc7 | bellard | { |
1135 | 85571bc7 | bellard | /* channel A channel B channel C */
|
1136 | 85571bc7 | bellard | /* LFO */
|
1137 | 85571bc7 | bellard | ams = ams_table[(amsCnt+=amsIncr)>>AMS_SHIFT]; |
1138 | 85571bc7 | bellard | vib = vib_table[(vibCnt+=vibIncr)>>VIB_SHIFT]; |
1139 | 85571bc7 | bellard | outd[0] = 0; |
1140 | 85571bc7 | bellard | /* deltaT ADPCM */
|
1141 | 85571bc7 | bellard | if( DELTAT->portstate )
|
1142 | 85571bc7 | bellard | YM_DELTAT_ADPCM_CALC(DELTAT); |
1143 | 85571bc7 | bellard | /* FM part */
|
1144 | 85571bc7 | bellard | for(CH=S_CH ; CH < R_CH ; CH++)
|
1145 | 85571bc7 | bellard | OPL_CALC_CH(CH); |
1146 | 85571bc7 | bellard | /* Rythn part */
|
1147 | 85571bc7 | bellard | if(rythm)
|
1148 | 85571bc7 | bellard | OPL_CALC_RH(S_CH); |
1149 | 85571bc7 | bellard | /* limit check */
|
1150 | 85571bc7 | bellard | data = Limit( outd[0] , OPL_MAXOUT, OPL_MINOUT );
|
1151 | 85571bc7 | bellard | /* store to sound buffer */
|
1152 | 85571bc7 | bellard | buf[i] = data >> OPL_OUTSB; |
1153 | 85571bc7 | bellard | } |
1154 | 85571bc7 | bellard | OPL->amsCnt = amsCnt; |
1155 | 85571bc7 | bellard | OPL->vibCnt = vibCnt; |
1156 | 85571bc7 | bellard | /* deltaT START flag */
|
1157 | 85571bc7 | bellard | if( !DELTAT->portstate )
|
1158 | 85571bc7 | bellard | OPL->status &= 0xfe;
|
1159 | 85571bc7 | bellard | } |
1160 | 85571bc7 | bellard | #endif
|
1161 | 85571bc7 | bellard | |
1162 | 85571bc7 | bellard | /* ---------- reset one of chip ---------- */
|
1163 | 85571bc7 | bellard | void OPLResetChip(FM_OPL *OPL)
|
1164 | 85571bc7 | bellard | { |
1165 | 85571bc7 | bellard | int c,s;
|
1166 | 85571bc7 | bellard | int i;
|
1167 | 85571bc7 | bellard | |
1168 | 85571bc7 | bellard | /* reset chip */
|
1169 | 85571bc7 | bellard | OPL->mode = 0; /* normal mode */ |
1170 | 85571bc7 | bellard | OPL_STATUS_RESET(OPL,0x7f);
|
1171 | 85571bc7 | bellard | /* reset with register write */
|
1172 | 85571bc7 | bellard | OPLWriteReg(OPL,0x01,0); /* wabesel disable */ |
1173 | 85571bc7 | bellard | OPLWriteReg(OPL,0x02,0); /* Timer1 */ |
1174 | 85571bc7 | bellard | OPLWriteReg(OPL,0x03,0); /* Timer2 */ |
1175 | 85571bc7 | bellard | OPLWriteReg(OPL,0x04,0); /* IRQ mask clear */ |
1176 | 85571bc7 | bellard | for(i = 0xff ; i >= 0x20 ; i-- ) OPLWriteReg(OPL,i,0); |
1177 | 85571bc7 | bellard | /* reset OPerator paramater */
|
1178 | 85571bc7 | bellard | for( c = 0 ; c < OPL->max_ch ; c++ ) |
1179 | 85571bc7 | bellard | { |
1180 | 85571bc7 | bellard | OPL_CH *CH = &OPL->P_CH[c]; |
1181 | 85571bc7 | bellard | /* OPL->P_CH[c].PAN = OPN_CENTER; */
|
1182 | 85571bc7 | bellard | for(s = 0 ; s < 2 ; s++ ) |
1183 | 85571bc7 | bellard | { |
1184 | 85571bc7 | bellard | /* wave table */
|
1185 | 85571bc7 | bellard | CH->SLOT[s].wavetable = &SIN_TABLE[0];
|
1186 | 85571bc7 | bellard | /* CH->SLOT[s].evm = ENV_MOD_RR; */
|
1187 | 85571bc7 | bellard | CH->SLOT[s].evc = EG_OFF; |
1188 | 85571bc7 | bellard | CH->SLOT[s].eve = EG_OFF+1;
|
1189 | 85571bc7 | bellard | CH->SLOT[s].evs = 0;
|
1190 | 85571bc7 | bellard | } |
1191 | 85571bc7 | bellard | } |
1192 | 85571bc7 | bellard | #if BUILD_Y8950
|
1193 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_ADPCM)
|
1194 | 85571bc7 | bellard | { |
1195 | 85571bc7 | bellard | YM_DELTAT *DELTAT = OPL->deltat; |
1196 | 85571bc7 | bellard | |
1197 | 85571bc7 | bellard | DELTAT->freqbase = OPL->freqbase; |
1198 | 85571bc7 | bellard | DELTAT->output_pointer = outd; |
1199 | 85571bc7 | bellard | DELTAT->portshift = 5;
|
1200 | 85571bc7 | bellard | DELTAT->output_range = DELTAT_MIXING_LEVEL<<TL_BITS; |
1201 | 85571bc7 | bellard | YM_DELTAT_ADPCM_Reset(DELTAT,0);
|
1202 | 85571bc7 | bellard | } |
1203 | 85571bc7 | bellard | #endif
|
1204 | 85571bc7 | bellard | } |
1205 | 85571bc7 | bellard | |
1206 | 85571bc7 | bellard | /* ---------- Create one of vietual YM3812 ---------- */
|
1207 | 85571bc7 | bellard | /* 'rate' is sampling rate and 'bufsiz' is the size of the */
|
1208 | 85571bc7 | bellard | FM_OPL *OPLCreate(int type, int clock, int rate) |
1209 | 85571bc7 | bellard | { |
1210 | 85571bc7 | bellard | char *ptr;
|
1211 | 85571bc7 | bellard | FM_OPL *OPL; |
1212 | 85571bc7 | bellard | int state_size;
|
1213 | 85571bc7 | bellard | int max_ch = 9; /* normaly 9 channels */ |
1214 | 85571bc7 | bellard | |
1215 | 85571bc7 | bellard | if( OPL_LockTable() ==-1) return NULL; |
1216 | 85571bc7 | bellard | /* allocate OPL state space */
|
1217 | 85571bc7 | bellard | state_size = sizeof(FM_OPL);
|
1218 | 85571bc7 | bellard | state_size += sizeof(OPL_CH)*max_ch;
|
1219 | 85571bc7 | bellard | #if BUILD_Y8950
|
1220 | 85571bc7 | bellard | if(type&OPL_TYPE_ADPCM) state_size+= sizeof(YM_DELTAT); |
1221 | 85571bc7 | bellard | #endif
|
1222 | 85571bc7 | bellard | /* allocate memory block */
|
1223 | 809c130c | aliguori | ptr = malloc(state_size); |
1224 | 809c130c | aliguori | if(ptr==NULL) return NULL; |
1225 | 85571bc7 | bellard | /* clear */
|
1226 | 85571bc7 | bellard | memset(ptr,0,state_size);
|
1227 | 85571bc7 | bellard | OPL = (FM_OPL *)ptr; ptr+=sizeof(FM_OPL);
|
1228 | 85571bc7 | bellard | OPL->P_CH = (OPL_CH *)ptr; ptr+=sizeof(OPL_CH)*max_ch;
|
1229 | 85571bc7 | bellard | #if BUILD_Y8950
|
1230 | 85571bc7 | bellard | if(type&OPL_TYPE_ADPCM) OPL->deltat = (YM_DELTAT *)ptr; ptr+=sizeof(YM_DELTAT); |
1231 | 85571bc7 | bellard | #endif
|
1232 | 85571bc7 | bellard | /* set channel state pointer */
|
1233 | 85571bc7 | bellard | OPL->type = type; |
1234 | 85571bc7 | bellard | OPL->clock = clock; |
1235 | 85571bc7 | bellard | OPL->rate = rate; |
1236 | 85571bc7 | bellard | OPL->max_ch = max_ch; |
1237 | 85571bc7 | bellard | /* init grobal tables */
|
1238 | 85571bc7 | bellard | OPL_initalize(OPL); |
1239 | 85571bc7 | bellard | /* reset chip */
|
1240 | 85571bc7 | bellard | OPLResetChip(OPL); |
1241 | 85571bc7 | bellard | #ifdef OPL_OUTPUT_LOG
|
1242 | 85571bc7 | bellard | if(!opl_dbg_fp)
|
1243 | 85571bc7 | bellard | { |
1244 | 85571bc7 | bellard | opl_dbg_fp = fopen("opllog.opl","wb"); |
1245 | 85571bc7 | bellard | opl_dbg_maxchip = 0;
|
1246 | 85571bc7 | bellard | } |
1247 | 85571bc7 | bellard | if(opl_dbg_fp)
|
1248 | 85571bc7 | bellard | { |
1249 | 85571bc7 | bellard | opl_dbg_opl[opl_dbg_maxchip] = OPL; |
1250 | 85571bc7 | bellard | fprintf(opl_dbg_fp,"%c%c%c%c%c%c",0x00+opl_dbg_maxchip, |
1251 | 85571bc7 | bellard | type, |
1252 | 85571bc7 | bellard | clock&0xff,
|
1253 | 85571bc7 | bellard | (clock/0x100)&0xff, |
1254 | 85571bc7 | bellard | (clock/0x10000)&0xff, |
1255 | 85571bc7 | bellard | (clock/0x1000000)&0xff); |
1256 | 85571bc7 | bellard | opl_dbg_maxchip++; |
1257 | 85571bc7 | bellard | } |
1258 | 85571bc7 | bellard | #endif
|
1259 | 85571bc7 | bellard | return OPL;
|
1260 | 85571bc7 | bellard | } |
1261 | 85571bc7 | bellard | |
1262 | 85571bc7 | bellard | /* ---------- Destroy one of vietual YM3812 ---------- */
|
1263 | 85571bc7 | bellard | void OPLDestroy(FM_OPL *OPL)
|
1264 | 85571bc7 | bellard | { |
1265 | 85571bc7 | bellard | #ifdef OPL_OUTPUT_LOG
|
1266 | 85571bc7 | bellard | if(opl_dbg_fp)
|
1267 | 85571bc7 | bellard | { |
1268 | 85571bc7 | bellard | fclose(opl_dbg_fp); |
1269 | 85571bc7 | bellard | opl_dbg_fp = NULL;
|
1270 | 85571bc7 | bellard | } |
1271 | 85571bc7 | bellard | #endif
|
1272 | 85571bc7 | bellard | OPL_UnLockTable(); |
1273 | 85571bc7 | bellard | free(OPL); |
1274 | 85571bc7 | bellard | } |
1275 | 85571bc7 | bellard | |
1276 | 85571bc7 | bellard | /* ---------- Option handlers ---------- */
|
1277 | 85571bc7 | bellard | |
1278 | 85571bc7 | bellard | void OPLSetTimerHandler(FM_OPL *OPL,OPL_TIMERHANDLER TimerHandler,int channelOffset) |
1279 | 85571bc7 | bellard | { |
1280 | 85571bc7 | bellard | OPL->TimerHandler = TimerHandler; |
1281 | 85571bc7 | bellard | OPL->TimerParam = channelOffset; |
1282 | 85571bc7 | bellard | } |
1283 | 85571bc7 | bellard | void OPLSetIRQHandler(FM_OPL *OPL,OPL_IRQHANDLER IRQHandler,int param) |
1284 | 85571bc7 | bellard | { |
1285 | 85571bc7 | bellard | OPL->IRQHandler = IRQHandler; |
1286 | 85571bc7 | bellard | OPL->IRQParam = param; |
1287 | 85571bc7 | bellard | } |
1288 | 85571bc7 | bellard | void OPLSetUpdateHandler(FM_OPL *OPL,OPL_UPDATEHANDLER UpdateHandler,int param) |
1289 | 85571bc7 | bellard | { |
1290 | 85571bc7 | bellard | OPL->UpdateHandler = UpdateHandler; |
1291 | 85571bc7 | bellard | OPL->UpdateParam = param; |
1292 | 85571bc7 | bellard | } |
1293 | 85571bc7 | bellard | #if BUILD_Y8950
|
1294 | 85571bc7 | bellard | void OPLSetPortHandler(FM_OPL *OPL,OPL_PORTHANDLER_W PortHandler_w,OPL_PORTHANDLER_R PortHandler_r,int param) |
1295 | 85571bc7 | bellard | { |
1296 | 85571bc7 | bellard | OPL->porthandler_w = PortHandler_w; |
1297 | 85571bc7 | bellard | OPL->porthandler_r = PortHandler_r; |
1298 | 85571bc7 | bellard | OPL->port_param = param; |
1299 | 85571bc7 | bellard | } |
1300 | 85571bc7 | bellard | |
1301 | 85571bc7 | bellard | void OPLSetKeyboardHandler(FM_OPL *OPL,OPL_PORTHANDLER_W KeyboardHandler_w,OPL_PORTHANDLER_R KeyboardHandler_r,int param) |
1302 | 85571bc7 | bellard | { |
1303 | 85571bc7 | bellard | OPL->keyboardhandler_w = KeyboardHandler_w; |
1304 | 85571bc7 | bellard | OPL->keyboardhandler_r = KeyboardHandler_r; |
1305 | 85571bc7 | bellard | OPL->keyboard_param = param; |
1306 | 85571bc7 | bellard | } |
1307 | 85571bc7 | bellard | #endif
|
1308 | 85571bc7 | bellard | /* ---------- YM3812 I/O interface ---------- */
|
1309 | 85571bc7 | bellard | int OPLWrite(FM_OPL *OPL,int a,int v) |
1310 | 85571bc7 | bellard | { |
1311 | 85571bc7 | bellard | if( !(a&1) ) |
1312 | 85571bc7 | bellard | { /* address port */
|
1313 | 85571bc7 | bellard | OPL->address = v & 0xff;
|
1314 | 85571bc7 | bellard | } |
1315 | 85571bc7 | bellard | else
|
1316 | 85571bc7 | bellard | { /* data port */
|
1317 | 85571bc7 | bellard | if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0); |
1318 | 85571bc7 | bellard | #ifdef OPL_OUTPUT_LOG
|
1319 | 85571bc7 | bellard | if(opl_dbg_fp)
|
1320 | 85571bc7 | bellard | { |
1321 | 85571bc7 | bellard | for(opl_dbg_chip=0;opl_dbg_chip<opl_dbg_maxchip;opl_dbg_chip++) |
1322 | 85571bc7 | bellard | if( opl_dbg_opl[opl_dbg_chip] == OPL) break; |
1323 | 85571bc7 | bellard | fprintf(opl_dbg_fp,"%c%c%c",0x10+opl_dbg_chip,OPL->address,v); |
1324 | 85571bc7 | bellard | } |
1325 | 85571bc7 | bellard | #endif
|
1326 | 85571bc7 | bellard | OPLWriteReg(OPL,OPL->address,v); |
1327 | 85571bc7 | bellard | } |
1328 | 85571bc7 | bellard | return OPL->status>>7; |
1329 | 85571bc7 | bellard | } |
1330 | 85571bc7 | bellard | |
1331 | 85571bc7 | bellard | unsigned char OPLRead(FM_OPL *OPL,int a) |
1332 | 85571bc7 | bellard | { |
1333 | 85571bc7 | bellard | if( !(a&1) ) |
1334 | 85571bc7 | bellard | { /* status port */
|
1335 | 85571bc7 | bellard | return OPL->status & (OPL->statusmask|0x80); |
1336 | 85571bc7 | bellard | } |
1337 | 85571bc7 | bellard | /* data port */
|
1338 | 85571bc7 | bellard | switch(OPL->address)
|
1339 | 85571bc7 | bellard | { |
1340 | 85571bc7 | bellard | case 0x05: /* KeyBoard IN */ |
1341 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_KEYBOARD)
|
1342 | 85571bc7 | bellard | { |
1343 | 85571bc7 | bellard | if(OPL->keyboardhandler_r)
|
1344 | 85571bc7 | bellard | return OPL->keyboardhandler_r(OPL->keyboard_param);
|
1345 | 85571bc7 | bellard | else
|
1346 | 85571bc7 | bellard | LOG(LOG_WAR,("OPL:read unmapped KEYBOARD port\n"));
|
1347 | 85571bc7 | bellard | } |
1348 | 85571bc7 | bellard | return 0; |
1349 | 85571bc7 | bellard | #if 0
|
1350 | 85571bc7 | bellard | case 0x0f: /* ADPCM-DATA */
|
1351 | 85571bc7 | bellard | return 0;
|
1352 | 85571bc7 | bellard | #endif
|
1353 | 85571bc7 | bellard | case 0x19: /* I/O DATA */ |
1354 | 85571bc7 | bellard | if(OPL->type&OPL_TYPE_IO)
|
1355 | 85571bc7 | bellard | { |
1356 | 85571bc7 | bellard | if(OPL->porthandler_r)
|
1357 | 85571bc7 | bellard | return OPL->porthandler_r(OPL->port_param);
|
1358 | 85571bc7 | bellard | else
|
1359 | 85571bc7 | bellard | LOG(LOG_WAR,("OPL:read unmapped I/O port\n"));
|
1360 | 85571bc7 | bellard | } |
1361 | 85571bc7 | bellard | return 0; |
1362 | 85571bc7 | bellard | case 0x1a: /* PCM-DATA */ |
1363 | 85571bc7 | bellard | return 0; |
1364 | 85571bc7 | bellard | } |
1365 | 85571bc7 | bellard | return 0; |
1366 | 85571bc7 | bellard | } |
1367 | 85571bc7 | bellard | |
1368 | 85571bc7 | bellard | int OPLTimerOver(FM_OPL *OPL,int c) |
1369 | 85571bc7 | bellard | { |
1370 | 85571bc7 | bellard | if( c )
|
1371 | 85571bc7 | bellard | { /* Timer B */
|
1372 | 85571bc7 | bellard | OPL_STATUS_SET(OPL,0x20);
|
1373 | 85571bc7 | bellard | } |
1374 | 85571bc7 | bellard | else
|
1375 | 85571bc7 | bellard | { /* Timer A */
|
1376 | 85571bc7 | bellard | OPL_STATUS_SET(OPL,0x40);
|
1377 | 85571bc7 | bellard | /* CSM mode key,TL controll */
|
1378 | 85571bc7 | bellard | if( OPL->mode & 0x80 ) |
1379 | 85571bc7 | bellard | { /* CSM mode total level latch and auto key on */
|
1380 | 85571bc7 | bellard | int ch;
|
1381 | 85571bc7 | bellard | if(OPL->UpdateHandler) OPL->UpdateHandler(OPL->UpdateParam,0); |
1382 | 85571bc7 | bellard | for(ch=0;ch<9;ch++) |
1383 | 85571bc7 | bellard | CSMKeyControll( &OPL->P_CH[ch] ); |
1384 | 85571bc7 | bellard | } |
1385 | 85571bc7 | bellard | } |
1386 | 85571bc7 | bellard | /* reload timer */
|
1387 | 85571bc7 | bellard | if (OPL->TimerHandler) (OPL->TimerHandler)(OPL->TimerParam+c,(double)OPL->T[c]*OPL->TimerBase); |
1388 | 85571bc7 | bellard | return OPL->status>>7; |
1389 | 85571bc7 | bellard | } |