summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/ym2612_emu.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libgme/ym2612_emu.c')
-rw-r--r--lib/rbcodec/codecs/libgme/ym2612_emu.c1374
1 files changed, 1374 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/ym2612_emu.c b/lib/rbcodec/codecs/libgme/ym2612_emu.c
new file mode 100644
index 0000000000..60df30a33c
--- /dev/null
+++ b/lib/rbcodec/codecs/libgme/ym2612_emu.c
@@ -0,0 +1,1374 @@
1// Game_Music_Emu $vers. http://www.slack.net/~ant/
2
3// Based on Gens 2.10 ym2612.c
4
5#include <stdlib.h>
6#include <string.h>
7#include <limits.h>
8#include <stdio.h>
9#include <math.h>
10
11#include "ym2612_emu.h"
12
13/* Copyright (C) 2002 Stéphane Dallongeville (gens AT consolemul.com) */
14/* Copyright (C) 2004-2007 Shay Green. This module is free software; you
15can redistribute it and/or modify it under the terms of the GNU Lesser
16General Public License as published by the Free Software Foundation; either
17version 2.1 of the License, or (at your option) any later version. This
18module is distributed in the hope that it will be useful, but WITHOUT ANY
19WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21details. You should have received a copy of the GNU Lesser General Public
22License along with this module; if not, write to the Free Software Foundation,
23Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
24
25// This is mostly the original source in its C style and all.
26//
27// Somewhat optimized and simplified. Uses a template to generate the many
28// variants of Update_Chan. Rewrote header file. In need of full rewrite by
29// someone more familiar with FM sound and the YM2612. Has some inaccuracies
30// compared to the Sega Genesis sound, particularly being mixed at such a
31// high sample accuracy (the Genesis sounds like it has only 8 bit samples).
32// - Shay
33
34// Ported again to c by gama.
35// Not sure if performance is better than the original c version.
36
37#if !defined(YM2612_CALCUL_TABLES)
38 #include "ymtables.h"
39#endif
40
41#ifdef YM2612_CALCUL_TABLES
42 #define FREQ_TAB_LOOKUP g->LFO_FREQ_TAB
43 #define ENV_TAB_LOOKUP g->LFO_ENV_TAB
44#else
45 #define FREQ_TAB_LOOKUP lfo_freq_coeff
46 #define ENV_TAB_LOOKUP lfo_env_coeff
47#endif
48
49const int output_bits = 14;
50
51static const unsigned char DT_DEF_TAB [4 * 32] =
52{
53// FD = 0
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56
57// FD = 1
58 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
59 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
60
61// FD = 2
62 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
63 5, 6, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 16, 16, 16, 16,
64
65// FD = 3
66 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
67 8 , 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 20, 22, 22, 22, 22
68};
69
70static const unsigned char FKEY_TAB [16] =
71{
72 0, 0, 0, 0,
73 0, 0, 0, 1,
74 2, 3, 3, 3,
75 3, 3, 3, 3
76};
77
78static const unsigned char LFO_AMS_TAB [4] =
79{
80 31, 4, 1, 0
81};
82
83static const unsigned char LFO_FMS_TAB [8] =
84{
85 LFO_FMS_BASE * 0, LFO_FMS_BASE * 1,
86 LFO_FMS_BASE * 2, LFO_FMS_BASE * 3,
87 LFO_FMS_BASE * 4, LFO_FMS_BASE * 6,
88 LFO_FMS_BASE * 12, LFO_FMS_BASE * 24
89};
90
91int in0, in1, in2, in3; // current phase calculation
92// int en0, en1, en2, en3; // current enveloppe calculation
93
94static inline void set_seg( struct slot_t* s, int seg )
95{
96 s->env_xor = 0;
97 s->env_max = INT_MAX;
98 s->SEG = seg;
99 if ( seg & 4 )
100 {
101 s->env_xor = ENV_MASK;
102 s->env_max = ENV_MASK;
103 }
104}
105
106static inline void YM2612_Special_Update(void) { }
107
108static void KEY_ON( struct channel_* ch, struct tables_t *g, int nsl )
109{
110 struct slot_t *SL = &(ch->SLOT [nsl]); // on recupere le bon pointeur de slot
111
112 if (SL->Ecurp == RELEASE) // la touche est-elle rel'chee ?
113 {
114 SL->Fcnt = 0;
115
116 // Fix Ecco 2 splash sound
117
118 SL->Ecnt = (g->DECAY_TO_ATTACK [g->ENV_TAB [SL->Ecnt >> ENV_LBITS]] + ENV_ATTACK) & SL->ChgEnM;
119 SL->ChgEnM = ~0;
120
121// SL->Ecnt = g.DECAY_TO_ATTACK [g.ENV_TAB [SL->Ecnt >> ENV_LBITS]] + ENV_ATTACK;
122// SL->Ecnt = 0;
123
124 SL->Einc = SL->EincA;
125 SL->Ecmp = ENV_DECAY;
126 SL->Ecurp = ATTACK;
127 }
128}
129
130
131static void KEY_OFF( struct channel_* ch, struct tables_t *g, int nsl )
132{
133 struct slot_t *SL = &(ch->SLOT [nsl]); // on recupere le bon pointeur de slot
134
135 if (SL->Ecurp != RELEASE) // la touche est-elle appuyee ?
136 {
137 if (SL->Ecnt < ENV_DECAY) // attack phase ?
138 {
139 SL->Ecnt = (g->ENV_TAB [SL->Ecnt >> ENV_LBITS] << ENV_LBITS) + ENV_DECAY;
140 }
141
142 SL->Einc = SL->EincR;
143 SL->Ecmp = ENV_END;
144 SL->Ecurp = RELEASE;
145 }
146}
147
148
149static int SLOT_SET( struct Ym2612_Impl* impl, int Adr, int data )
150{
151 int nch = Adr & 3;
152 if ( nch == 3 )
153 return 1;
154
155 struct tables_t *g = &impl->g;
156 struct state_t *YM2612 = &impl->YM2612;
157 struct channel_* ch = &YM2612->CHANNEL [nch + (Adr & 0x100 ? 3 : 0)];
158 struct slot_t* sl = &ch->SLOT [(Adr >> 2) & 3];
159
160 switch ( Adr & 0xF0 )
161 {
162 case 0x30:
163 if ( (sl->MUL = (data & 0x0F)) != 0 ) sl->MUL <<= 1;
164 else sl->MUL = 1;
165
166 sl->DT = (int*) g->DT_TAB [(data >> 4) & 7];
167
168 ch->SLOT [0].Finc = -1;
169
170 break;
171
172 case 0x40:
173 sl->TL = data & 0x7F;
174
175 // SOR2 do a lot of TL adjustement and this fix R.Shinobi jump sound...
176 YM2612_Special_Update();
177
178#if ((ENV_HBITS - 7) < 0)
179 sl->TLL = sl->TL >> (7 - ENV_HBITS);
180#else
181 sl->TLL = sl->TL << (ENV_HBITS - 7);
182#endif
183
184 break;
185
186 case 0x50:
187 sl->KSR_S = 3 - (data >> 6);
188
189 ch->SLOT [0].Finc = -1;
190
191 if (data &= 0x1F) sl->AR = (int*) &g->AR_TAB [data << 1];
192 else sl->AR = (int*) &g->NULL_RATE [0];
193
194 sl->EincA = sl->AR [sl->KSR];
195 if (sl->Ecurp == ATTACK) sl->Einc = sl->EincA;
196 break;
197
198 case 0x60:
199 if ( (sl->AMSon = (data & 0x80)) != 0 ) sl->AMS = ch->AMS;
200 else sl->AMS = 31;
201
202 if (data &= 0x1F) sl->DR = (int*) &g->DR_TAB [data << 1];
203 else sl->DR = (int*) &g->NULL_RATE [0];
204
205 sl->EincD = sl->DR [sl->KSR];
206 if (sl->Ecurp == DECAY) sl->Einc = sl->EincD;
207 break;
208
209 case 0x70:
210 if (data &= 0x1F) sl->SR = (int*) &g->DR_TAB [data << 1];
211 else sl->SR = (int*) &g->NULL_RATE [0];
212
213 sl->EincS = sl->SR [sl->KSR];
214 if ((sl->Ecurp == SUBSTAIN) && (sl->Ecnt < ENV_END)) sl->Einc = sl->EincS;
215 break;
216
217 case 0x80:
218 sl->SLL = g->SL_TAB [data >> 4];
219
220 sl->RR = (int*) &g->DR_TAB [((data & 0xF) << 2) + 2];
221
222 sl->EincR = sl->RR [sl->KSR];
223 if ((sl->Ecurp == RELEASE) && (sl->Ecnt < ENV_END)) sl->Einc = sl->EincR;
224 break;
225
226 case 0x90:
227 // SSG-EG envelope shapes :
228 /*
229 E At Al H
230
231 1 0 0 0 \\\\
232 1 0 0 1 \___
233 1 0 1 0 \/\/
234 1 0 1 1 \
235 1 1 0 0 ////
236 1 1 0 1 /
237 1 1 1 0 /\/\
238 1 1 1 1 /___
239
240 E = SSG-EG enable
241 At = Start negate
242 Al = Altern
243 H = Hold */
244
245 set_seg( sl, (data & 8) ? (data & 0x0F) : 0 );
246 break;
247 }
248
249 return 0;
250}
251
252
253static int CHANNEL_SET( struct state_t* YM2612, int Adr, int data )
254{
255 int num = Adr & 3;
256 if ( num == 3 )
257 return 1;
258
259 struct channel_* ch = &YM2612->CHANNEL [num + (Adr & 0x100 ? 3 : 0)];
260
261 switch ( Adr & 0xFC )
262 {
263 case 0xA0:
264 YM2612_Special_Update();
265
266 ch->FNUM [0] = (ch->FNUM [0] & 0x700) + data;
267 ch->KC [0] = (ch->FOCT [0] << 2) | FKEY_TAB [ch->FNUM [0] >> 7];
268
269 ch->SLOT [0].Finc = -1;
270 break;
271
272 case 0xA4:
273 YM2612_Special_Update();
274
275 ch->FNUM [0] = (ch->FNUM [0] & 0x0FF) + ((data & 0x07) << 8);
276 ch->FOCT [0] = (data & 0x38) >> 3;
277 ch->KC [0] = (ch->FOCT [0] << 2) | FKEY_TAB [ch->FNUM [0] >> 7];
278
279 ch->SLOT [0].Finc = -1;
280 break;
281
282 case 0xA8:
283 if ( Adr < 0x100 )
284 {
285 num++;
286
287 YM2612_Special_Update();
288
289 YM2612->CHANNEL [2].FNUM [num] = (YM2612->CHANNEL [2].FNUM [num] & 0x700) + data;
290 YM2612->CHANNEL [2].KC [num] = (YM2612->CHANNEL [2].FOCT [num] << 2) |
291 FKEY_TAB [YM2612->CHANNEL [2].FNUM [num] >> 7];
292
293 YM2612->CHANNEL [2].SLOT [0].Finc = -1;
294 }
295 break;
296
297 case 0xAC:
298 if ( Adr < 0x100 )
299 {
300 num++;
301
302 YM2612_Special_Update();
303
304 YM2612->CHANNEL [2].FNUM [num] = (YM2612->CHANNEL [2].FNUM [num] & 0x0FF) + ((data & 0x07) << 8);
305 YM2612->CHANNEL [2].FOCT [num] = (data & 0x38) >> 3;
306 YM2612->CHANNEL [2].KC [num] = (YM2612->CHANNEL [2].FOCT [num] << 2) |
307 FKEY_TAB [YM2612->CHANNEL [2].FNUM [num] >> 7];
308
309 YM2612->CHANNEL [2].SLOT [0].Finc = -1;
310 }
311 break;
312
313 case 0xB0:
314 if ( ch->ALGO != (data & 7) )
315 {
316 // Fix VectorMan 2 heli sound (level 1)
317 YM2612_Special_Update();
318
319 ch->ALGO = data & 7;
320
321 ch->SLOT [0].ChgEnM = 0;
322 ch->SLOT [1].ChgEnM = 0;
323 ch->SLOT [2].ChgEnM = 0;
324 ch->SLOT [3].ChgEnM = 0;
325 }
326
327 ch->FB = 9 - ((data >> 3) & 7); // Real thing ?
328
329// if (ch->FB = ((data >> 3) & 7)) ch->FB = 9 - ch->FB; // Thunder force 4 (music stage 8), Gynoug, Aladdin bug sound...
330// else ch->FB = 31;
331 break;
332
333 case 0xB4: {
334 YM2612_Special_Update();
335
336 ch->LEFT = 0 - ((data >> 7) & 1);
337 ch->RIGHT = 0 - ((data >> 6) & 1);
338
339 ch->AMS = LFO_AMS_TAB [(data >> 4) & 3];
340 ch->FMS = LFO_FMS_TAB [data & 7];
341
342 int i;
343 for ( i = 0; i < 4; i++ )
344 {
345 struct slot_t* sl = &ch->SLOT [i];
346 sl->AMS = (sl->AMSon ? ch->AMS : 31);
347 }
348 break;
349 }
350 }
351
352 return 0;
353}
354
355
356static int YM_SET( struct Ym2612_Impl* impl, int Adr, int data )
357{
358 struct state_t* YM2612 = &impl->YM2612;
359 struct tables_t* g = &impl->g;
360 switch ( Adr )
361 {
362 case 0x22:
363 if (data & 8) // LFO enable
364 {
365 // Cool Spot music 1, LFO modified severals time which
366 // distord the sound, have to check that on a real genesis...
367
368 g->LFOinc = g->LFO_INC_TAB [data & 7];
369 }
370 else
371 {
372 g->LFOinc = g->LFOcnt = 0;
373 }
374 break;
375
376 case 0x24:
377 YM2612->TimerA = (YM2612->TimerA & 0x003) | (((int) data) << 2);
378
379 if (YM2612->TimerAL != (1024 - YM2612->TimerA) << 12)
380 {
381 YM2612->TimerAcnt = YM2612->TimerAL = (1024 - YM2612->TimerA) << 12;
382 }
383 break;
384
385 case 0x25:
386 YM2612->TimerA = (YM2612->TimerA & 0x3FC) | (data & 3);
387
388 if (YM2612->TimerAL != (1024 - YM2612->TimerA) << 12)
389 {
390 YM2612->TimerAcnt = YM2612->TimerAL = (1024 - YM2612->TimerA) << 12;
391 }
392 break;
393
394 case 0x26:
395 YM2612->TimerB = data;
396
397 if (YM2612->TimerBL != (256 - YM2612->TimerB) << (4 + 12))
398 {
399 YM2612->TimerBcnt = YM2612->TimerBL = (256 - YM2612->TimerB) << (4 + 12);
400 }
401 break;
402
403 case 0x27:
404 // Parametre divers
405 // b7 = CSM MODE
406 // b6 = 3 slot mode
407 // b5 = reset b
408 // b4 = reset a
409 // b3 = timer enable b
410 // b2 = timer enable a
411 // b1 = load b
412 // b0 = load a
413
414 if ((data ^ YM2612->Mode) & 0x40)
415 {
416 // We changed the channel 2 mode, so recalculate phase step
417 // This fix the punch sound in Street of Rage 2
418
419 YM2612_Special_Update();
420
421 YM2612->CHANNEL [2].SLOT [0].Finc = -1; // recalculate phase step
422 }
423
424// if ((data & 2) && (YM2612->Status & 2)) YM2612->TimerBcnt = YM2612->TimerBL;
425// if ((data & 1) && (YM2612->Status & 1)) YM2612->TimerAcnt = YM2612->TimerAL;
426
427// YM2612->Status &= (~data >> 4); // Reset du Status au cas ou c'est demande
428 YM2612->Status &= (~data >> 4) & (data >> 2); // Reset Status
429
430 YM2612->Mode = data;
431 break;
432
433 case 0x28: {
434 int nch = data & 3;
435 if ( nch == 3 )
436 return 1;
437 if ( data & 4 )
438 nch += 3;
439 struct channel_* ch = &YM2612->CHANNEL [nch];
440
441 YM2612_Special_Update();
442
443 if (data & 0x10) KEY_ON(ch, g, S0); // On appuie sur la touche pour le slot 1
444 else KEY_OFF(ch, g, S0); // On rel'che la touche pour le slot 1
445 if (data & 0x20) KEY_ON(ch, g, S1); // On appuie sur la touche pour le slot 3
446 else KEY_OFF(ch, g, S1); // On rel'che la touche pour le slot 3
447 if (data & 0x40) KEY_ON(ch, g, S2); // On appuie sur la touche pour le slot 2
448 else KEY_OFF(ch, g, S2); // On rel'che la touche pour le slot 2
449 if (data & 0x80) KEY_ON(ch, g, S3); // On appuie sur la touche pour le slot 4
450 else KEY_OFF(ch, g, S3); // On rel'che la touche pour le slot 4
451 break;
452 }
453
454 case 0x2B:
455 if (YM2612->DAC ^ (data & 0x80)) YM2612_Special_Update();
456
457 YM2612->DAC = data & 0x80; // activation/desactivation du DAC
458 break;
459 }
460
461 return 0;
462}
463
464void impl_reset( struct Ym2612_Impl* impl );
465static void impl_set_rate( struct Ym2612_Impl* impl, int sample_rate, int clock_rate )
466{
467 assert( sample_rate );
468 assert( !clock_rate || clock_rate > sample_rate );
469
470 int i;
471
472 // 144 = 12 * (prescale * 2) = 12 * 6 * 2
473 // prescale set to 6 by default
474
475 int Frequency = (clock_rate ? (int)((FP_ONE_CLOCK * clock_rate) / sample_rate / 144) : (int)FP_ONE_CLOCK);
476 if ( abs( Frequency - FP_ONE_CLOCK ) < 1 )
477 Frequency = FP_ONE_CLOCK;
478 impl->YM2612.TimerBase = Frequency;
479
480 /* double Frequence = (double)Frequency / FP_ONE_CLOCK; */
481
482 // Tableau TL :
483 // [0 - 4095] = +output [4095 - ...] = +output overflow (fill with 0)
484 // [12288 - 16383] = -output [16384 - ...] = -output overflow (fill with 0)
485
486#ifdef YM2612_USE_TL_TAB
487 for ( i = 0; i < TL_LENGHT; i++ )
488 {
489 if (i >= PG_CUT_OFF) // YM2612 cut off sound after 78 dB (14 bits output ?)
490 {
491 impl->g.TL_TAB [TL_LENGHT + i] = impl->g.TL_TAB [i] = 0;
492 }
493 else
494 {
495 // Decibel -> Voltage
496 #ifdef YM2612_CALCUL_TABLES
497 impl->g.TL_TAB [i] = (int) (MAX_OUT / pow( 10.0, ENV_STEP / 20.0f * i ));
498 #else
499 impl->g.TL_TAB [i] = tl_coeff [i];
500 #endif
501 impl->g.TL_TAB [TL_LENGHT + i] = -impl->g.TL_TAB [i];
502 }
503 }
504#endif
505
506 // Tableau SIN :
507 // impl->g.SIN_TAB [x] [y] = sin(x) * y;
508 // x = phase and y = volume
509
510 impl->g.SIN_TAB [0] = impl->g.SIN_TAB [SIN_LENGHT / 2] = PG_CUT_OFF;
511
512 for ( i = 1; i <= SIN_LENGHT / 4; i++ )
513 {
514 // Sinus in dB
515 #ifdef YM2612_CALCUL_TABLES
516 double x = 20 * log10( 1 / sin( 2.0 * PI * i / SIN_LENGHT ) ); // convert to dB
517
518 int j = (int) (x / ENV_STEP); // Get TL range
519
520 if (j > PG_CUT_OFF) j = (int) PG_CUT_OFF;
521 #else
522 int j = sindb_coeff [i-1];
523 #endif
524
525 impl->g.SIN_TAB [i] = impl->g.SIN_TAB [(SIN_LENGHT / 2) - i] = j;
526 impl->g.SIN_TAB [(SIN_LENGHT / 2) + i] = impl->g.SIN_TAB [SIN_LENGHT - i] = TL_LENGHT + j;
527 }
528
529 #ifdef YM2612_CALCUL_TABLES
530 // Tableau LFO (LFO wav) :
531 for ( i = 0; i < LFO_LENGHT; i++ )
532 {
533 double x = 1 + sin( 2.0 * PI * i * (1.0 / LFO_LENGHT) ); // Sinus
534 x *= 11.8 / ENV_STEP / 2; // ajusted to MAX enveloppe modulation
535
536 impl->g.LFO_ENV_TAB [i] = (int) x;
537
538 x = sin( 2.0 * PI * i * (1.0 / LFO_LENGHT) ); // Sinus
539 x *= (1 << (LFO_HBITS - 1)) - 1;
540
541 impl->g.LFO_FREQ_TAB [i] = (int) x;
542 }
543 #endif
544
545 // Tableau Enveloppe :
546 // impl->g.ENV_TAB [0] -> impl->g.ENV_TAB [ENV_LENGHT - 1] = attack curve
547 // impl->g.ENV_TAB [ENV_LENGHT] -> impl->g.ENV_TAB [2 * ENV_LENGHT - 1] = decay curve
548
549 for ( i = 0; i < ENV_LENGHT; i++ )
550 {
551 // Attack curve (x^8 - music level 2 Vectorman 2)
552 #if defined(ROCKBOX)
553 int k;
554 int prescale = (31 - 2*ENV_HBITS); /* used to gain higher precision */
555 int x = ENV_LENGHT * (1 << prescale);
556 for ( k = 0; k < 8; ++k)
557 {
558 x = ( x * ((ENV_LENGHT - 1) - i) ) / ENV_LENGHT;
559 }
560 x >>= prescale;
561 #else
562 double x = pow( ((ENV_LENGHT - 1) - i) / (double) ENV_LENGHT, 8.0 );
563 x *= ENV_LENGHT;
564 #endif
565
566 impl->g.ENV_TAB [i] = (int) x;
567
568 // Decay curve (just linear)
569 impl->g.ENV_TAB [ENV_LENGHT + i] = i;
570 }
571 for ( i = 0; i < 8; i++ )
572 impl->g.ENV_TAB [i + ENV_LENGHT * 2] = 0;
573
574 impl->g.ENV_TAB [ENV_END >> ENV_LBITS] = ENV_LENGHT - 1; // for the stopped state
575
576 // Tableau pour la conversion Attack -> Decay and Decay -> Attack
577
578 int j = ENV_LENGHT - 1;
579 for ( i = 0; i < ENV_LENGHT; i++ )
580 {
581 while ( j && impl->g.ENV_TAB [j] < i )
582 j--;
583
584 impl->g.DECAY_TO_ATTACK [i] = j << ENV_LBITS;
585 }
586
587 // Tableau pour le Substain Level
588
589 for ( i = 0; i < 15; i++ )
590 {
591 int x = i * 3 * (int)( (1 << ENV_LBITS) / ENV_STEP); // 3 and not 6 (Mickey Mania first music for test)
592
593 impl->g.SL_TAB [i] = x + ENV_DECAY;
594 }
595
596 impl->g.SL_TAB [15] = ((ENV_LENGHT - 1) << ENV_LBITS) + ENV_DECAY; // special case : volume off
597
598 // Tableau Frequency Step
599
600 {
601 // * 1 / 2 because MUL = value * 2
602 #if SIN_LBITS + SIN_HBITS - (21 - 7) < 0
603 /* double const factor = Frequence / 2.0 / (1 << ((21 - 7) - SIN_LBITS - SIN_HBITS)); */
604 int const factor = (int)(Frequency / 2 / (1 << ((21 - 7) - SIN_LBITS - SIN_HBITS)) / FP_ONE_CLOCK);
605 #else
606 /* double const factor = Frequence / 2.0 * (1 << (SIN_LBITS + SIN_HBITS - (21 - 7))); */
607 int const factor = (int)(Frequency / 2 * (1 << (SIN_LBITS + SIN_HBITS - (21 - 7))) / FP_ONE_CLOCK);
608 #endif
609 for ( i = 0; i < 2048; i++ )
610 {
611 impl->g.FINC_TAB [i] = i * factor;
612 }
613 }
614
615 // Tableaux Attack & Decay Rate
616
617 for ( i = 0; i < 4; i++ )
618 {
619 impl->g.AR_TAB [i] = 0;
620 impl->g.DR_TAB [i] = 0;
621 }
622
623 for ( i = 0; i < 60; i++ )
624 {
625 long long x =
626 (4LL + ((i & 3))) * // bits 0-1 : 4*(x1.00, x1.25, x1.50, x1.75)
627 (ENV_LENGHT << ENV_LBITS) * // on ajuste pour le tableau impl->g.ENV_TAB
628 Frequency *
629 (1 << (i >> 2)) / // bits 2-5 : shift bits (x2^0 - x2^15)
630 FP_ONE_CLOCK / 4;
631
632 long long x_AR = x / AR_RATE;
633 long long x_DR = x / DR_RATE;
634
635 impl->g.AR_TAB [i + 4] = (unsigned int) ( x_AR > ((1LL<<32) - 1) ? ((1LL<<32) - 1) : x_AR );
636 impl->g.DR_TAB [i + 4] = (unsigned int) ( x_DR > ((1LL<<32) - 1) ? ((1LL<<32) - 1) : x_DR );
637 }
638
639 for ( i = 64; i < 96; i++ )
640 {
641 impl->g.AR_TAB [i] = impl->g.AR_TAB [63];
642 impl->g.DR_TAB [i] = impl->g.DR_TAB [63];
643
644 impl->g.NULL_RATE [i - 64] = 0;
645 }
646
647 for ( i = 96; i < 128; i++ )
648 impl->g.AR_TAB [i] = 0;
649
650 // Tableau Detune
651 {
652 #if SIN_LBITS + SIN_HBITS - 21 < 0
653 /* double const factor = 1.0 / (1 << (21 - SIN_LBITS - SIN_HBITS)) * Frequence; */
654 int const factor = Frequency / (1 << (21 - SIN_LBITS - SIN_HBITS)) / FP_ONE_CLOCK;
655 #else
656 /* double const factor = (1 << (SIN_LBITS + SIN_HBITS - 21)) * Frequence; */
657 int const factor = Frequency * (1 << (SIN_LBITS + SIN_HBITS - 21)) / FP_ONE_CLOCK;
658 #endif
659 for ( i = 0; i < 4; i++ )
660 {
661 int j;
662 for ( j = 0; j < 32; j++ )
663 {
664 /* double y = DT_DEF_TAB [(i << 5) + j] * factor; */
665 int y = DT_DEF_TAB [(i << 5) + j] * factor;
666
667 impl->g.DT_TAB [i + 0] [j] = (int) y;
668 impl->g.DT_TAB [i + 4] [j] = (int) -y;
669 }
670 }
671 }
672
673 // Tableau LFO
674 impl->g.LFO_INC_TAB [0] = (int) (3.98 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
675 impl->g.LFO_INC_TAB [1] = (int) (5.56 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
676 impl->g.LFO_INC_TAB [2] = (int) (6.02 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
677 impl->g.LFO_INC_TAB [3] = (int) (6.37 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
678 impl->g.LFO_INC_TAB [4] = (int) (6.88 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
679 impl->g.LFO_INC_TAB [5] = (int) (9.63 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
680 impl->g.LFO_INC_TAB [6] = (int) (48.1 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
681 impl->g.LFO_INC_TAB [7] = (int) (72.2 * (1 << (LFO_HBITS + LFO_LBITS))) / sample_rate;
682
683 impl_reset( impl );
684}
685
686const char* Ym2612_set_rate( struct Ym2612_Emu* this, int sample_rate, int clock_rate )
687{
688// Only set rates if necessary
689#if defined(ROCKBOX)
690 static int last_sample_rate = 0, last_clock_rate = 0;
691 if (last_sample_rate == sample_rate && last_clock_rate == clock_rate) return 0;
692#endif
693 memset( &this->impl.YM2612, 0, sizeof this->impl.YM2612 );
694 impl_set_rate( &this->impl, sample_rate, clock_rate );
695
696 return 0;
697}
698
699static inline void write0( struct Ym2612_Impl* impl, int opn_addr, int data )
700{
701 assert( (unsigned) data <= 0xFF );
702
703 if ( opn_addr < 0x30 )
704 {
705 impl->YM2612.REG [0] [opn_addr] = data;
706 YM_SET( impl, opn_addr, data );
707 }
708 else if ( impl->YM2612.REG [0] [opn_addr] != data )
709 {
710 impl->YM2612.REG [0] [opn_addr] = data;
711
712 if ( opn_addr < 0xA0 )
713 SLOT_SET( impl, opn_addr, data );
714 else
715 CHANNEL_SET( &impl->YM2612, opn_addr, data );
716 }
717}
718
719static inline void write1( struct Ym2612_Impl* impl, int opn_addr, int data )
720{
721 assert( (unsigned) data <= 0xFF );
722
723 if ( opn_addr >= 0x30 && impl->YM2612.REG [1] [opn_addr] != data )
724 {
725 impl->YM2612.REG [1] [opn_addr] = data;
726
727 if ( opn_addr < 0xA0 )
728 SLOT_SET( impl, opn_addr + 0x100, data );
729 else
730 CHANNEL_SET( &impl->YM2612, opn_addr + 0x100, data );
731 }
732}
733
734void impl_reset( struct Ym2612_Impl* impl )
735{
736 impl->g.LFOcnt = 0;
737 impl->YM2612.TimerA = 0;
738 impl->YM2612.TimerAL = 0;
739 impl->YM2612.TimerAcnt = 0;
740 impl->YM2612.TimerB = 0;
741 impl->YM2612.TimerBL = 0;
742 impl->YM2612.TimerBcnt = 0;
743 impl->YM2612.DAC = 0;
744
745 impl->YM2612.Status = 0;
746
747 int i;
748 for ( i = 0; i < ym2612_channel_count; i++ )
749 {
750 struct channel_* ch = &impl->YM2612.CHANNEL [i];
751
752 ch->LEFT = ~0;
753 ch->RIGHT = ~0;
754 ch->ALGO = 0;
755 ch->FB = 31;
756 ch->FMS = 0;
757 ch->AMS = 0;
758
759 int j;
760 for ( j = 0 ;j < 4 ; j++ )
761 {
762 ch->S0_OUT [j] = 0;
763 ch->FNUM [j] = 0;
764 ch->FOCT [j] = 0;
765 ch->KC [j] = 0;
766
767 ch->SLOT [j].Fcnt = 0;
768 ch->SLOT [j].Finc = 0;
769 ch->SLOT [j].Ecnt = ENV_END; // Put it at the end of Decay phase...
770 ch->SLOT [j].Einc = 0;
771 ch->SLOT [j].Ecmp = 0;
772 ch->SLOT [j].Ecurp = RELEASE;
773
774 ch->SLOT [j].ChgEnM = 0;
775 }
776 }
777
778 for ( i = 0; i < 0x100; i++ )
779 {
780 impl->YM2612.REG [0] [i] = -1;
781 impl->YM2612.REG [1] [i] = -1;
782 }
783
784 for ( i = 0xB6; i >= 0xB4; i-- )
785 {
786 write0( impl, i, 0xC0 );
787 write1( impl, i, 0xC0 );
788 }
789
790 for ( i = 0xB2; i >= 0x22; i-- )
791 {
792 write0( impl, i, 0 );
793 write1( impl, i, 0 );
794 }
795
796 write0( impl, 0x2A, 0x80 );
797}
798
799void Ym2612_reset( struct Ym2612_Emu* this )
800{
801 impl_reset( &this->impl );
802}
803
804void Ym2612_write0( struct Ym2612_Emu* this, int addr, int data )
805{
806 write0( &this->impl, addr, data );
807}
808
809void Ym2612_write1( struct Ym2612_Emu* this, int addr, int data )
810{
811 write1( &this->impl, addr, data );
812}
813
814void Ym2612_mute_voices( struct Ym2612_Emu* this, int mask ) { this->impl.mute_mask = mask; }
815
816static void update_envelope_( struct slot_t* sl )
817{
818 switch ( sl->Ecurp )
819 {
820 case 0:
821 // Env_Attack_Next
822
823 // Verified with Gynoug even in HQ (explode SFX)
824 sl->Ecnt = ENV_DECAY;
825
826 sl->Einc = sl->EincD;
827 sl->Ecmp = sl->SLL;
828 sl->Ecurp = DECAY;
829 break;
830
831 case 1:
832 // Env_Decay_Next
833
834 // Verified with Gynoug even in HQ (explode SFX)
835 sl->Ecnt = sl->SLL;
836
837 sl->Einc = sl->EincS;
838 sl->Ecmp = ENV_END;
839 sl->Ecurp = SUBSTAIN;
840 break;
841
842 case 2:
843 // Env_Substain_Next(slot_t *SL)
844 if (sl->SEG & 8) // SSG envelope type
845 {
846 int release = sl->SEG & 1;
847
848 if ( !release )
849 {
850 // re KEY ON
851
852 // sl->Fcnt = 0;
853 // sl->ChgEnM = ~0;
854
855 sl->Ecnt = 0;
856 sl->Einc = sl->EincA;
857 sl->Ecmp = ENV_DECAY;
858 sl->Ecurp = ATTACK;
859 }
860
861 set_seg( sl, (sl->SEG << 1) & 4 );
862
863 if ( !release )
864 break;
865 }
866 // fall through
867
868 case 3:
869 // Env_Release_Next
870 sl->Ecnt = ENV_END;
871 sl->Einc = 0;
872 sl->Ecmp = ENV_END + 1;
873 break;
874
875 // default: no op
876 }
877}
878
879static inline void update_envelope( struct slot_t* sl )
880{
881 int ecmp = sl->Ecmp;
882 if ( (sl->Ecnt += sl->Einc) >= ecmp )
883 update_envelope_( sl );
884}
885
886
887typedef void (*ym2612_update_chan_t)( struct tables_t*, struct channel_*, short*, int );
888
889#define GET_CURRENT_PHASE \
890int in0 = ch->SLOT[S0].Fcnt; \
891int in1 = ch->SLOT[S1].Fcnt; \
892int in2 = ch->SLOT[S2].Fcnt; \
893int in3 = ch->SLOT[S3].Fcnt; \
894
895#define GET_CURRENT_LFO \
896int YM2612_LFOinc = g->LFOinc; \
897int YM2612_LFOcnt = g->LFOcnt + YM2612_LFOinc;
898
899#define CALC_EN( x ) \
900 int temp##x = ENV_TAB [ch->SLOT [S##x].Ecnt >> ENV_LBITS] + ch->SLOT [S##x].TLL; \
901 int en##x = ((temp##x ^ ch->SLOT [S##x].env_xor) + (env_LFO >> ch->SLOT [S##x].AMS)) & \
902 ((temp##x - ch->SLOT [S##x].env_max) >> 31);
903
904#define GET_ENV \
905int const env_LFO = ENV_TAB_LOOKUP [YM2612_LFOcnt >> LFO_LBITS & LFO_MASK]; \
906short const* const ENV_TAB = g->ENV_TAB; \
907CALC_EN( 0 ) \
908CALC_EN( 1 ) \
909CALC_EN( 2 ) \
910CALC_EN( 3 )
911
912#ifndef YM2612_USE_TL_TAB
913static inline int tl_level( int i )
914{
915 if (i >= (PG_CUT_OFF + TL_LENGHT)) {
916 return 0;
917 } else if (i >= TL_LENGHT) {
918 return -tl_coeff [i - TL_LENGHT];
919 } else if (i >= PG_CUT_OFF) {
920 return 0;
921 } else
922 return tl_coeff [i];
923}
924#define SINT( i, o ) (tl_level (g->SIN_TAB [(i)] + (o)))
925#else
926#define SINT( i, o ) (g->TL_TAB [g->SIN_TAB [(i)] + (o)])
927#endif
928
929#define DO_FEEDBACK \
930int CH_S0_OUT_0 = ch->S0_OUT [0]; \
931{ \
932 int temp = in0 + ((CH_S0_OUT_0 + CH_S0_OUT_1) >> ch->FB); \
933 CH_S0_OUT_1 = CH_S0_OUT_0; \
934 CH_S0_OUT_0 = SINT( (temp >> SIN_LBITS) & SIN_MASK, en0 ); \
935} \
936
937#define DO_LIMIT \
938CH_OUTd >>= MAX_OUT_BITS - output_bits + 2; \
939
940#define UPDATE_PHASE_CYCLE \
941unsigned freq_LFO = ((FREQ_TAB_LOOKUP [YM2612_LFOcnt >> LFO_LBITS & LFO_MASK] * \
942 ch->FMS) >> (LFO_HBITS - 1 + 1)) + (1 << (LFO_FMS_LBITS - 1)); \
943YM2612_LFOcnt += YM2612_LFOinc; \
944in0 += (ch->SLOT [S0].Finc * freq_LFO) >> (LFO_FMS_LBITS - 1); \
945in1 += (ch->SLOT [S1].Finc * freq_LFO) >> (LFO_FMS_LBITS - 1); \
946in2 += (ch->SLOT [S2].Finc * freq_LFO) >> (LFO_FMS_LBITS - 1); \
947in3 += (ch->SLOT [S3].Finc * freq_LFO) >> (LFO_FMS_LBITS - 1);
948
949#define UPDATE_ENV \
950int t0 = buf [0] + (CH_OUTd & ch->LEFT); \
951int t1 = buf [1] + (CH_OUTd & ch->RIGHT); \
952update_envelope( &ch->SLOT [0] ); \
953update_envelope( &ch->SLOT [1] ); \
954update_envelope( &ch->SLOT [2] ); \
955update_envelope( &ch->SLOT [3] );
956
957#define DO_OUTPUT_0 \
958ch->S0_OUT [0] = CH_S0_OUT_0; \
959buf [0] = t0; \
960buf [1] = t1; \
961buf += 2; \
962
963#define DO_OUTPUT_1 \
964ch->S0_OUT [1] = CH_S0_OUT_1;
965
966#define UPDATE_PHASE \
967ch->SLOT [S0].Fcnt = in0; \
968ch->SLOT [S1].Fcnt = in1; \
969ch->SLOT [S2].Fcnt = in2; \
970ch->SLOT [S3].Fcnt = in3;
971
972static void ym2612_update_chan0( struct tables_t* g, struct channel_* ch,
973 short* buf, int length )
974{
975 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
976 int CH_S0_OUT_1 = ch->S0_OUT [1];
977
978 GET_CURRENT_PHASE
979 GET_CURRENT_LFO
980
981 if ( !not_end )
982 return;
983
984 do
985 {
986 GET_ENV
987 DO_FEEDBACK
988
989 int CH_OUTd;
990 int temp = in1 + CH_S0_OUT_1;
991 temp = in2 + SINT( (temp >> SIN_LBITS) & SIN_MASK, en1 );
992 temp = in3 + SINT( (temp >> SIN_LBITS) & SIN_MASK, en2 );
993 CH_OUTd = SINT( (temp >> SIN_LBITS) & SIN_MASK, en3 );
994
995 DO_LIMIT
996 UPDATE_PHASE_CYCLE
997 UPDATE_ENV
998 DO_OUTPUT_0
999 }
1000 while ( --length );
1001 DO_OUTPUT_1
1002 UPDATE_PHASE
1003}
1004
1005static void ym2612_update_chan1( struct tables_t* g, struct channel_* ch,
1006 short* buf, int length )
1007{
1008 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
1009 int CH_S0_OUT_1 = ch->S0_OUT [1];
1010
1011 GET_CURRENT_PHASE
1012 GET_CURRENT_LFO
1013
1014 if ( !not_end )
1015 return;
1016
1017 do
1018 {
1019 GET_ENV
1020 DO_FEEDBACK
1021
1022 int CH_OUTd;
1023 int temp = in2 + CH_S0_OUT_1 + SINT( (in1 >> SIN_LBITS) & SIN_MASK, en1 );
1024 temp = in3 + SINT( (temp >> SIN_LBITS) & SIN_MASK, en2 );
1025 CH_OUTd = SINT( (temp >> SIN_LBITS) & SIN_MASK, en3 );
1026
1027 DO_LIMIT
1028 UPDATE_PHASE_CYCLE
1029 UPDATE_ENV
1030 DO_OUTPUT_0
1031 }
1032 while ( --length );
1033 DO_OUTPUT_1
1034 UPDATE_PHASE
1035}
1036
1037static void ym2612_update_chan2( struct tables_t* g, struct channel_* ch,
1038 short* buf, int length )
1039{
1040 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
1041 int CH_S0_OUT_1 = ch->S0_OUT [1];
1042
1043 GET_CURRENT_PHASE
1044 GET_CURRENT_LFO
1045
1046 if ( !not_end )
1047 return;
1048
1049 do
1050 {
1051 GET_ENV
1052 DO_FEEDBACK
1053
1054 int CH_OUTd;
1055 int temp = in2 + SINT( (in1 >> SIN_LBITS) & SIN_MASK, en1 );
1056 temp = in3 + CH_S0_OUT_1 + SINT( (temp >> SIN_LBITS) & SIN_MASK, en2 );
1057 CH_OUTd = SINT( (temp >> SIN_LBITS) & SIN_MASK, en3 );
1058
1059 DO_LIMIT
1060 UPDATE_PHASE_CYCLE
1061 UPDATE_ENV
1062 DO_OUTPUT_0
1063 }
1064 while ( --length );
1065 DO_OUTPUT_1
1066 UPDATE_PHASE
1067}
1068
1069static void ym2612_update_chan3( struct tables_t* g, struct channel_* ch,
1070 short* buf, int length )
1071{
1072 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
1073 int CH_S0_OUT_1 = ch->S0_OUT [1];
1074
1075 GET_CURRENT_PHASE
1076 GET_CURRENT_LFO
1077
1078 if ( !not_end )
1079 return;
1080
1081 do
1082 {
1083 GET_ENV
1084 DO_FEEDBACK
1085
1086 int CH_OUTd;
1087 int temp = in1 + CH_S0_OUT_1;
1088 temp = in3 + SINT( (temp >> SIN_LBITS) & SIN_MASK, en1 ) +
1089 SINT( (in2 >> SIN_LBITS) & SIN_MASK, en2 );
1090 CH_OUTd = SINT( (temp >> SIN_LBITS) & SIN_MASK, en3 );
1091
1092 DO_LIMIT
1093 UPDATE_PHASE_CYCLE
1094 UPDATE_ENV
1095 DO_OUTPUT_0
1096 }
1097 while ( --length );
1098 DO_OUTPUT_1
1099 UPDATE_PHASE
1100}
1101
1102static void ym2612_update_chan4( struct tables_t* g, struct channel_* ch,
1103 short* buf, int length )
1104{
1105 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
1106 not_end |= ch->SLOT [S1].Ecnt - ENV_END;
1107
1108 int CH_S0_OUT_1 = ch->S0_OUT [1];
1109
1110 GET_CURRENT_PHASE
1111 GET_CURRENT_LFO
1112
1113 if ( !not_end )
1114 return;
1115
1116 do
1117 {
1118 GET_ENV
1119 DO_FEEDBACK
1120
1121 int CH_OUTd;
1122 int temp = in3 + SINT( (in2 >> SIN_LBITS) & SIN_MASK, en2 );
1123 CH_OUTd = SINT( (temp >> SIN_LBITS) & SIN_MASK, en3 ) +
1124 SINT( ((in1 + CH_S0_OUT_1) >> SIN_LBITS) & SIN_MASK, en1 );
1125
1126 DO_LIMIT
1127 UPDATE_PHASE_CYCLE
1128 UPDATE_ENV
1129 DO_OUTPUT_0
1130 }
1131 while ( --length );
1132 DO_OUTPUT_1
1133 UPDATE_PHASE
1134}
1135
1136static void ym2612_update_chan5( struct tables_t* g, struct channel_* ch,
1137 short* buf, int length )
1138{
1139 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
1140 not_end |= ch->SLOT [S2].Ecnt - ENV_END;
1141 not_end |= ch->SLOT [S1].Ecnt - ENV_END;
1142
1143 int CH_S0_OUT_1 = ch->S0_OUT [1];
1144
1145 GET_CURRENT_PHASE
1146 GET_CURRENT_LFO
1147
1148 if ( !not_end )
1149 return;
1150
1151 do
1152 {
1153 GET_ENV
1154 DO_FEEDBACK
1155
1156 int CH_OUTd;
1157 int temp = CH_S0_OUT_1;
1158 CH_OUTd = SINT( ((in3 + temp) >> SIN_LBITS) & SIN_MASK, en3 ) +
1159 SINT( ((in1 + temp) >> SIN_LBITS) & SIN_MASK, en1 ) +
1160 SINT( ((in2 + temp) >> SIN_LBITS) & SIN_MASK, en2 );
1161
1162 DO_LIMIT
1163 UPDATE_PHASE_CYCLE
1164 UPDATE_ENV
1165 DO_OUTPUT_0
1166 }
1167 while ( --length );
1168 DO_OUTPUT_1
1169 UPDATE_PHASE
1170}
1171
1172static void ym2612_update_chan6( struct tables_t* g, struct channel_* ch,
1173 short* buf, int length )
1174{
1175 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
1176 not_end |= ch->SLOT [S2].Ecnt - ENV_END;
1177 not_end |= ch->SLOT [S1].Ecnt - ENV_END;
1178
1179 int CH_S0_OUT_1 = ch->S0_OUT [1];
1180
1181 GET_CURRENT_PHASE
1182 GET_CURRENT_LFO
1183
1184 if ( !not_end )
1185 return;
1186
1187 do
1188 {
1189 GET_ENV
1190 DO_FEEDBACK
1191
1192 int CH_OUTd;
1193 CH_OUTd = SINT( (in3 >> SIN_LBITS) & SIN_MASK, en3 ) +
1194 SINT( ((in1 + CH_S0_OUT_1) >> SIN_LBITS) & SIN_MASK, en1 ) +
1195 SINT( (in2 >> SIN_LBITS) & SIN_MASK, en2 );
1196
1197 DO_LIMIT
1198 UPDATE_PHASE_CYCLE
1199 UPDATE_ENV
1200 DO_OUTPUT_0
1201 }
1202 while ( --length );
1203 DO_OUTPUT_1
1204 UPDATE_PHASE
1205}
1206
1207static void ym2612_update_chan7( struct tables_t* g, struct channel_* ch,
1208 short* buf, int length )
1209{
1210 int not_end = ch->SLOT [S3].Ecnt - ENV_END;
1211 not_end |= ch->SLOT [S0].Ecnt - ENV_END;
1212 not_end |= ch->SLOT [S2].Ecnt - ENV_END;
1213 not_end |= ch->SLOT [S1].Ecnt - ENV_END;
1214
1215 int CH_S0_OUT_1 = ch->S0_OUT [1];
1216
1217 GET_CURRENT_PHASE
1218 GET_CURRENT_LFO
1219
1220 if ( !not_end )
1221 return;
1222
1223 do
1224 {
1225 GET_ENV
1226 DO_FEEDBACK
1227
1228 int CH_OUTd;
1229 CH_OUTd = SINT( (in3 >> SIN_LBITS) & SIN_MASK, en3 ) +
1230 SINT( (in1 >> SIN_LBITS) & SIN_MASK, en1 ) +
1231 SINT( (in2 >> SIN_LBITS) & SIN_MASK, en2 ) + CH_S0_OUT_1;
1232
1233 DO_LIMIT
1234 UPDATE_PHASE_CYCLE
1235 UPDATE_ENV
1236 DO_OUTPUT_0
1237 }
1238 while ( --length );
1239 DO_OUTPUT_1
1240 UPDATE_PHASE
1241}
1242
1243static void (*UPDATE_CHAN[8])(struct tables_t* g, struct channel_* ch,
1244 short* buf, int length) =
1245{
1246 (void *)ym2612_update_chan0,
1247 (void *)ym2612_update_chan1,
1248 (void *)ym2612_update_chan2,
1249 (void *)ym2612_update_chan3,
1250 (void *)ym2612_update_chan4,
1251 (void *)ym2612_update_chan5,
1252 (void *)ym2612_update_chan6,
1253 (void *)ym2612_update_chan7
1254};
1255
1256static void run_timer( struct Ym2612_Impl* impl, int length )
1257{
1258 int const step = 6;
1259 int remain = length;
1260 do
1261 {
1262 int n = step;
1263 if ( n > remain )
1264 n = remain;
1265 remain -= n;
1266
1267 int i = n * impl->YM2612.TimerBase;
1268 if (impl->YM2612.Mode & 1) // Timer A ON ?
1269 {
1270 // if ((impl->YM2612.TimerAcnt -= 14073) <= 0) // 13879=NTSC (old: 14475=NTSC 14586=PAL)
1271 if ((impl->YM2612.TimerAcnt -= i) <= 0)
1272 {
1273 // timer a overflow
1274
1275 impl->YM2612.Status |= (impl->YM2612.Mode & 0x04) >> 2;
1276 impl->YM2612.TimerAcnt += impl->YM2612.TimerAL;
1277
1278 if (impl->YM2612.Mode & 0x80)
1279 {
1280 KEY_ON( &impl->YM2612.CHANNEL [2], &impl->g, 0 );
1281 KEY_ON( &impl->YM2612.CHANNEL [2], &impl->g, 1 );
1282 KEY_ON( &impl->YM2612.CHANNEL [2], &impl->g, 2 );
1283 KEY_ON( &impl->YM2612.CHANNEL [2], &impl->g, 3 );
1284 }
1285 }
1286 }
1287
1288 if (impl->YM2612.Mode & 2) // Timer B ON ?
1289 {
1290 // if ((impl->YM2612.TimerBcnt -= 14073) <= 0) // 13879=NTSC (old: 14475=NTSC 14586=PAL)
1291 if ((impl->YM2612.TimerBcnt -= i) <= 0)
1292 {
1293 // timer b overflow
1294 impl->YM2612.Status |= (impl->YM2612.Mode & 0x08) >> 2;
1295 impl->YM2612.TimerBcnt += impl->YM2612.TimerBL;
1296 }
1297 }
1298 }
1299 while ( remain > 0 );
1300}
1301
1302static void impl_run( struct Ym2612_Impl* impl, int pair_count, short out [] )
1303{
1304 if ( pair_count <= 0 )
1305 return;
1306
1307 if ( impl->YM2612.Mode & 3 )
1308 run_timer( impl, pair_count );
1309
1310 // Mise à jour des pas des compteurs-frequences s'ils ont ete modifies
1311
1312 int chi;
1313 for ( chi = 0; chi < ym2612_channel_count; chi++ )
1314 {
1315 struct channel_* ch = &impl->YM2612.CHANNEL [chi];
1316 if ( ch->SLOT [0].Finc != -1 )
1317 continue;
1318
1319 int i2 = 0;
1320 if ( chi == 2 && (impl->YM2612.Mode & 0x40) )
1321 i2 = 2;
1322
1323 int i;
1324 for ( i = 0; i < 4; i++ )
1325 {
1326 // static int seq [4] = { 2, 1, 3, 0 };
1327 // if ( i2 ) i2 = seq [i];
1328
1329 struct slot_t* sl = &ch->SLOT [i];
1330 int finc = impl->g.FINC_TAB [ch->FNUM [i2]] >> (7 - ch->FOCT [i2]);
1331 int ksr = ch->KC [i2] >> sl->KSR_S; // keycode attenuation
1332 sl->Finc = (finc + sl->DT [ch->KC [i2]]) * sl->MUL;
1333 if (sl->KSR != ksr) // si le KSR a change alors
1334 { // les differents taux pour l'enveloppe sont mis à jour
1335 sl->KSR = ksr;
1336
1337 sl->EincA = sl->AR [ksr];
1338 sl->EincD = sl->DR [ksr];
1339 sl->EincS = sl->SR [ksr];
1340 sl->EincR = sl->RR [ksr];
1341
1342 if (sl->Ecurp == ATTACK)
1343 {
1344 sl->Einc = sl->EincA;
1345 }
1346 else if (sl->Ecurp == DECAY)
1347 {
1348 sl->Einc = sl->EincD;
1349 }
1350 else if (sl->Ecnt < ENV_END)
1351 {
1352 if (sl->Ecurp == SUBSTAIN)
1353 sl->Einc = sl->EincS;
1354 else if (sl->Ecurp == RELEASE)
1355 sl->Einc = sl->EincR;
1356 }
1357 }
1358
1359 if ( i2 )
1360 i2 = (i2 ^ 2) ^ (i2 >> 1);
1361 }
1362 }
1363
1364 int i;
1365 for ( i = 0; i < ym2612_channel_count; i++ )
1366 {
1367 if ( !(impl->mute_mask & (1 << i)) && (i != 5 || !impl->YM2612.DAC) )
1368 UPDATE_CHAN [impl->YM2612.CHANNEL [i].ALGO]( &impl->g, &impl->YM2612.CHANNEL [i], out, pair_count );
1369 }
1370
1371 impl->g.LFOcnt += impl->g.LFOinc * pair_count;
1372}
1373
1374void Ym2612_run( struct Ym2612_Emu* this, int pair_count, short out [] ) { impl_run( &this->impl, pair_count, out ); }