#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "driver.h"
#include "ymf262.h"
namespace mame {
#ifndef PI
#define PI 3.14159265358979323846
#endif
#if (OPL3_SAMPLE_BITS==16)
#define FINAL_SH (0)
#define MAXOUT (+32767)
#define MINOUT (-32768)
#else
#define FINAL_SH (8)
#define MAXOUT (+127)
#define MINOUT (-128)
#endif
#define FREQ_SH 16
#define EG_SH 16
#define LFO_SH 24
#define TIMER_SH 16
#define FREQ_MASK ((1<<FREQ_SH)-1)
#define ENV_BITS 10
#define ENV_LEN (1<<ENV_BITS)
#define ENV_STEP (128.0/ENV_LEN)
#define MAX_ATT_INDEX ((1<<(ENV_BITS-1))-1)
#define MIN_ATT_INDEX (0)
#define SIN_BITS 10
#define SIN_LEN (1<<SIN_BITS)
#define SIN_MASK (SIN_LEN-1)
#define TL_RES_LEN (256)
#define SLOT1 0
#define SLOT2 1
#define EG_ATT 4
#define EG_DEC 3
#define EG_SUS 2
#define EG_REL 1
#define EG_OFF 0
#ifdef SAVE_SAMPLE
static FILE *sample[1];
#if 1
#define SAVE_ALL_CHANNELS \
{ signed int pom = a; \
fputc((unsigned short)pom&0xff,sample[0]); \
fputc(((unsigned short)pom>>8)&0xff,sample[0]); \
}
#else #endif
#endif
#ifdef LOG_CYM_FILE
FILE * cymfile = NULL;
#endif
#define OPL3_TYPE_YMF262 (0)
typedef struct{
UINT32 ar;
UINT32 dr;
UINT32 rr;
UINT8 KSR;
UINT8 ksl;
UINT8 ksr;
UINT8 mul;
UINT32 Cnt;
UINT32 Incr;
UINT8 FB;
INT32 *connect;
INT32 op1_out[2];
UINT8 CON;
UINT8 eg_type;
UINT8 state;
UINT32 TL;
INT32 TLL;
INT32 volume;
UINT32 sl;
UINT32 eg_m_ar;
UINT8 eg_sh_ar;
UINT8 eg_sel_ar;
UINT32 eg_m_dr;
UINT8 eg_sh_dr;
UINT8 eg_sel_dr;
UINT32 eg_m_rr;
UINT8 eg_sh_rr;
UINT8 eg_sel_rr;
UINT32 key;
UINT32 AMmask;
UINT8 vib;
UINT8 waveform_number;
unsigned int wavetable;
unsigned char reserved[128-100];
} OPL3_SLOT;
typedef struct{
OPL3_SLOT SLOT[2];
UINT32 block_fnum;
UINT32 fc;
UINT32 ksl_base;
UINT8 kcode;
UINT8 extended;
unsigned char reserved[512-272];
} OPL3_CH;
typedef struct {
OPL3_CH P_CH[18];
UINT32 pan[18*4];
UINT32 pan_ctrl_value[18];
UINT32 eg_cnt;
UINT32 eg_timer;
UINT32 eg_timer_add;
UINT32 eg_timer_overflow;
UINT32 fn_tab[1024];
UINT8 lfo_am_depth;
UINT8 lfo_pm_depth_range;
UINT32 lfo_am_cnt;
UINT32 lfo_am_inc;
UINT32 lfo_pm_cnt;
UINT32 lfo_pm_inc;
UINT32 noise_rng;
UINT32 noise_p;
UINT32 noise_f;
UINT8 OPL3_mode;
UINT8 rhythm;
int T[2];
UINT8 st[2];
UINT32 address;
UINT8 status;
UINT8 statusmask;
UINT8 nts;
OPL3_TIMERHANDLER TimerHandler;
void *TimerParam;
OPL3_IRQHANDLER IRQHandler;
void *IRQParam;
OPL3_UPDATEHANDLER UpdateHandler;
void *UpdateParam;
UINT8 type;
int clock;
int rate;
double freqbase;
double TimerBase;
signed int phase_modulation;
signed int phase_modulation2;
signed int chanout[18];
UINT32 LFO_AM;
INT32 LFO_PM;
} OPL3;
static const int slot_array[32]=
{
0, 2, 4, 1, 3, 5,-1,-1,
6, 8,10, 7, 9,11,-1,-1,
12,14,16,13,15,17,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1
};
#define DV (0.1875/2.0)
static const UINT32 ksl_tab[8*16]=
{
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.750/DV), (UINT32)(1.125/DV), (UINT32)(1.500/DV),
(UINT32)(1.875/DV), (UINT32)(2.250/DV), (UINT32)(2.625/DV), (UINT32)(3.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV),
(UINT32)(0.000/DV), (UINT32)(1.125/DV), (UINT32)(1.875/DV), (UINT32)(2.625/DV),
(UINT32)(3.000/DV), (UINT32)(3.750/DV), (UINT32)(4.125/DV), (UINT32)(4.500/DV),
(UINT32)(4.875/DV), (UINT32)(5.250/DV), (UINT32)(5.625/DV), (UINT32)(6.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(1.875/DV),
(UINT32)(3.000/DV), (UINT32)(4.125/DV), (UINT32)(4.875/DV), (UINT32)(5.625/DV),
(UINT32)(6.000/DV), (UINT32)(6.750/DV), (UINT32)(7.125/DV), (UINT32)(7.500/DV),
(UINT32)(7.875/DV), (UINT32)(8.250/DV), (UINT32)(8.625/DV), (UINT32)(9.000/DV),
(UINT32)(0.000/DV), (UINT32)(0.000/DV), (UINT32)(3.000/DV), (UINT32)(4.875/DV),
(UINT32)(6.000/DV), (UINT32)(7.125/DV), (UINT32)(7.875/DV), (UINT32)(8.625/DV),
(UINT32)(9.000/DV), (UINT32)(9.750/DV),(UINT32)(10.125/DV),(UINT32)(10.500/DV),
(UINT32)(10.875/DV),(UINT32)(11.250/DV),(UINT32)(11.625/DV),(UINT32)(12.000/DV),
(UINT32)(0.000/DV), (UINT32)(3.000/DV), (UINT32)(6.000/DV), (UINT32)(7.875/DV),
(UINT32)(9.000/DV),(UINT32)(10.125/DV),(UINT32)(10.875/DV),(UINT32)(11.625/DV),
(UINT32)(12.000/DV),(UINT32)(12.750/DV),(UINT32)(13.125/DV),(UINT32)(13.500/DV),
(UINT32)(13.875/DV),(UINT32)(14.250/DV),(UINT32)(14.625/DV),(UINT32)(15.000/DV),
(UINT32)(0.000/DV), (UINT32)(6.000/DV), (UINT32)(9.000/DV),(UINT32)(10.875/DV),
(UINT32)(12.000/DV),(UINT32)(13.125/DV),(UINT32)(13.875/DV),(UINT32)(14.625/DV),
(UINT32)(15.000/DV),(UINT32)(15.750/DV),(UINT32)(16.125/DV),(UINT32)(16.500/DV),
(UINT32)(16.875/DV),(UINT32)(17.250/DV),(UINT32)(17.625/DV),(UINT32)(18.000/DV),
(UINT32)(0.000/DV), (UINT32)(9.000/DV),(UINT32)(12.000/DV),(UINT32)(13.875/DV),
(UINT32)(15.000/DV),(UINT32)(16.125/DV),(UINT32)(16.875/DV),(UINT32)(17.625/DV),
(UINT32)(18.000/DV),(UINT32)(18.750/DV),(UINT32)(19.125/DV),(UINT32)(19.500/DV),
(UINT32)(19.875/DV),(UINT32)(20.250/DV),(UINT32)(20.625/DV),(UINT32)(21.000/DV)
};
#undef DV
#define SC(db) (UINT32) ( db * (2.0/ENV_STEP) )
static const UINT32 sl_tab[16]={
SC( 0),SC( 1),SC( 2),SC(3 ),SC(4 ),SC(5 ),SC(6 ),SC( 7),
SC( 8),SC( 9),SC(10),SC(11),SC(12),SC(13),SC(14),SC(31)
};
#undef SC
#define RATE_STEPS (8)
static const unsigned char eg_inc[15*RATE_STEPS]={
0,1, 0,1, 0,1, 0,1,
0,1, 0,1, 1,1, 0,1,
0,1, 1,1, 0,1, 1,1,
0,1, 1,1, 1,1, 1,1,
1,1, 1,1, 1,1, 1,1,
1,1, 1,2, 1,1, 1,2,
1,2, 1,2, 1,2, 1,2,
1,2, 2,2, 1,2, 2,2,
2,2, 2,2, 2,2, 2,2,
2,2, 2,4, 2,2, 2,4,
2,4, 2,4, 2,4, 2,4,
2,4, 4,4, 2,4, 4,4,
4,4, 4,4, 4,4, 4,4,
8,8, 8,8, 8,8, 8,8,
0,0, 0,0, 0,0, 0,0,
};
#define O(a) (a*RATE_STEPS)
static const unsigned char eg_rate_select[16+64+16]={
O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 4),O( 5),O( 6),O( 7),
O( 8),O( 9),O(10),O(11),
O(12),O(12),O(12),O(12),
O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
};
#undef O
#define O(a) (a*1)
static const unsigned char eg_rate_shift[16+64+16]={
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
O(12),O(12),O(12),O(12),
O(11),O(11),O(11),O(11),
O(10),O(10),O(10),O(10),
O( 9),O( 9),O( 9),O( 9),
O( 8),O( 8),O( 8),O( 8),
O( 7),O( 7),O( 7),O( 7),
O( 6),O( 6),O( 6),O( 6),
O( 5),O( 5),O( 5),O( 5),
O( 4),O( 4),O( 4),O( 4),
O( 3),O( 3),O( 3),O( 3),
O( 2),O( 2),O( 2),O( 2),
O( 1),O( 1),O( 1),O( 1),
O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
};
#undef O
#define ML 2
static const UINT8 mul_tab[16]= {
(UINT8)(0.50*ML), (UINT8)(1.00*ML), (UINT8)(2.00*ML), (UINT8)(3.00*ML), (UINT8)(4.00*ML), (UINT8)(5.00*ML), (UINT8)(6.00*ML), (UINT8)(7.00*ML),
(UINT8)(8.00*ML), (UINT8)(9.00*ML),(UINT8)(10.00*ML),(UINT8)(10.00*ML),(UINT8)(12.00*ML),(UINT8)(12.00*ML),(UINT8)(15.00*ML),(UINT8)(15.00*ML)
};
#undef ML
#define TL_TAB_LEN (13*2*TL_RES_LEN)
static signed int tl_tab[TL_TAB_LEN];
#define ENV_QUIET (TL_TAB_LEN>>4)
static unsigned int sin_tab[SIN_LEN * 8];
#define LFO_AM_TAB_ELEMENTS 210
static const UINT8 lfo_am_table[LFO_AM_TAB_ELEMENTS] = {
0,0,0,0,0,0,0,
1,1,1,1,
2,2,2,2,
3,3,3,3,
4,4,4,4,
5,5,5,5,
6,6,6,6,
7,7,7,7,
8,8,8,8,
9,9,9,9,
10,10,10,10,
11,11,11,11,
12,12,12,12,
13,13,13,13,
14,14,14,14,
15,15,15,15,
16,16,16,16,
17,17,17,17,
18,18,18,18,
19,19,19,19,
20,20,20,20,
21,21,21,21,
22,22,22,22,
23,23,23,23,
24,24,24,24,
25,25,25,25,
26,26,26,
25,25,25,25,
24,24,24,24,
23,23,23,23,
22,22,22,22,
21,21,21,21,
20,20,20,20,
19,19,19,19,
18,18,18,18,
17,17,17,17,
16,16,16,16,
15,15,15,15,
14,14,14,14,
13,13,13,13,
12,12,12,12,
11,11,11,11,
10,10,10,10,
9,9,9,9,
8,8,8,8,
7,7,7,7,
6,6,6,6,
5,5,5,5,
4,4,4,4,
3,3,3,3,
2,2,2,2,
1,1,1,1
};
static const INT8 lfo_pm_table[8*8*2] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0,-1, 0, 0, 0,
1, 0, 0, 0,-1, 0, 0, 0,
2, 1, 0,-1,-2,-1, 0, 1,
1, 0, 0, 0,-1, 0, 0, 0,
3, 1, 0,-1,-3,-1, 0, 1,
2, 1, 0,-1,-2,-1, 0, 1,
4, 2, 0,-2,-4,-2, 0, 2,
2, 1, 0,-1,-2,-1, 0, 1,
5, 2, 0,-2,-5,-2, 0, 2,
3, 1, 0,-1,-3,-1, 0, 1,
6, 3, 0,-3,-6,-3, 0, 3,
3, 1, 0,-1,-3,-1, 0, 1,
7, 3, 0,-3,-7,-3, 0, 3
};
static int num_lock = 0;
#define SLOT7_1 (&chip->P_CH[7].SLOT[SLOT1])
#define SLOT7_2 (&chip->P_CH[7].SLOT[SLOT2])
#define SLOT8_1 (&chip->P_CH[8].SLOT[SLOT1])
#define SLOT8_2 (&chip->P_CH[8].SLOT[SLOT2])
INLINE int limit( int val, int max, int min ) {
if ( val > max )
val = max;
else if ( val < min )
val = min;
return val;
}
INLINE void OPL3_STATUS_SET(OPL3 *chip,int flag)
{
chip->status |= (flag & chip->statusmask);
if(!(chip->status & 0x80))
{
if(chip->status & 0x7f)
{
chip->status |= 0x80;
if(chip->IRQHandler) (chip->IRQHandler)(chip->IRQParam,1);
}
}
}
INLINE void OPL3_STATUS_RESET(OPL3 *chip,int flag)
{
chip->status &= ~flag;
if(chip->status & 0x80)
{
if (!(chip->status & 0x7f))
{
chip->status &= 0x7f;
if(chip->IRQHandler) (chip->IRQHandler)(chip->IRQParam,0);
}
}
}
INLINE void OPL3_STATUSMASK_SET(OPL3 *chip,int flag)
{
chip->statusmask = flag;
OPL3_STATUS_SET(chip,0);
OPL3_STATUS_RESET(chip,0);
}
INLINE void advance_lfo(OPL3 *chip)
{
UINT8 tmp;
chip->lfo_am_cnt += chip->lfo_am_inc;
if (chip->lfo_am_cnt >= (LFO_AM_TAB_ELEMENTS<<LFO_SH) )
chip->lfo_am_cnt -= (LFO_AM_TAB_ELEMENTS<<LFO_SH);
tmp = lfo_am_table[ chip->lfo_am_cnt >> LFO_SH ];
if (chip->lfo_am_depth)
chip->LFO_AM = tmp;
else
chip->LFO_AM = tmp>>2;
chip->lfo_pm_cnt += chip->lfo_pm_inc;
chip->LFO_PM = ((chip->lfo_pm_cnt>>LFO_SH) & 7) | chip->lfo_pm_depth_range;
}
INLINE void advance(OPL3 *chip)
{
OPL3_CH *CH;
OPL3_SLOT *op;
int i;
chip->eg_timer += chip->eg_timer_add;
while (chip->eg_timer >= chip->eg_timer_overflow)
{
chip->eg_timer -= chip->eg_timer_overflow;
chip->eg_cnt++;
for (i=0; i<9*2*2; i++)
{
CH = &chip->P_CH[i/2];
op = &CH->SLOT[i&1];
#if 1
switch(op->state)
{
case EG_ATT:
if ( !(chip->eg_cnt & op->eg_m_ar) )
{
op->volume += (~op->volume *
(eg_inc[op->eg_sel_ar + ((chip->eg_cnt>>op->eg_sh_ar)&7)])
) >>3;
if (op->volume <= MIN_ATT_INDEX)
{
op->volume = MIN_ATT_INDEX;
op->state = EG_DEC;
}
}
break;
case EG_DEC:
if ( !(chip->eg_cnt & op->eg_m_dr) )
{
op->volume += eg_inc[op->eg_sel_dr + ((chip->eg_cnt>>op->eg_sh_dr)&7)];
if ( op->volume >= op->sl )
op->state = EG_SUS;
}
break;
case EG_SUS:
if(op->eg_type)
{
}
else
{
if ( !(chip->eg_cnt & op->eg_m_rr) )
{
op->volume += eg_inc[op->eg_sel_rr + ((chip->eg_cnt>>op->eg_sh_rr)&7)];
if ( op->volume >= MAX_ATT_INDEX )
op->volume = MAX_ATT_INDEX;
}
}
break;
case EG_REL:
if ( !(chip->eg_cnt & op->eg_m_rr) )
{
op->volume += eg_inc[op->eg_sel_rr + ((chip->eg_cnt>>op->eg_sh_rr)&7)];
if ( op->volume >= MAX_ATT_INDEX )
{
op->volume = MAX_ATT_INDEX;
op->state = EG_OFF;
}
}
break;
default:
break;
}
#endif
}
}
for (i=0; i<9*2*2; i++)
{
CH = &chip->P_CH[i/2];
op = &CH->SLOT[i&1];
if(op->vib)
{
UINT8 block;
unsigned int block_fnum = CH->block_fnum;
unsigned int fnum_lfo = (block_fnum&0x0380) >> 7;
signed int lfo_fn_table_index_offset = lfo_pm_table[chip->LFO_PM + 16*fnum_lfo ];
if (lfo_fn_table_index_offset)
{
block_fnum += lfo_fn_table_index_offset;
block = (block_fnum&0x1c00) >> 10;
op->Cnt += (chip->fn_tab[block_fnum&0x03ff] >> (7-block)) * op->mul;
}
else
{
op->Cnt += op->Incr;
}
}
else
{
op->Cnt += op->Incr;
}
}
chip->noise_p += chip->noise_f;
i = chip->noise_p >> FREQ_SH;
chip->noise_p &= FREQ_MASK;
while (i)
{
if (chip->noise_rng & 1) chip->noise_rng ^= 0x800302;
chip->noise_rng >>= 1;
i--;
}
}
INLINE signed int op_calc(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
{
UINT32 p;
p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + (pm<<16))) >> FREQ_SH ) & SIN_MASK) ];
if (p >= TL_TAB_LEN)
return 0;
return tl_tab[p];
}
INLINE signed int op_calc1(UINT32 phase, unsigned int env, signed int pm, unsigned int wave_tab)
{
UINT32 p;
p = (env<<4) + sin_tab[wave_tab + ((((signed int)((phase & ~FREQ_MASK) + pm))>>FREQ_SH) & SIN_MASK)];
if (p >= TL_TAB_LEN)
return 0;
return tl_tab[p];
}
#define volume_calc(OP) ((OP)->TLL + ((UINT32)(OP)->volume) + (chip->LFO_AM & (OP)->AMmask))
INLINE void chan_calc( OPL3 *chip, OPL3_CH *CH )
{
OPL3_SLOT *SLOT;
unsigned int env;
signed int out;
chip->phase_modulation = 0;
chip->phase_modulation2= 0;
SLOT = &CH->SLOT[SLOT1];
env = volume_calc(SLOT);
out = SLOT->op1_out[0] + SLOT->op1_out[1];
SLOT->op1_out[0] = SLOT->op1_out[1];
SLOT->op1_out[1] = 0;
if( env < ENV_QUIET )
{
if (!SLOT->FB)
out = 0;
SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
}
*SLOT->connect += SLOT->op1_out[1];
SLOT++;
env = volume_calc(SLOT);
if( env < ENV_QUIET )
*SLOT->connect += op_calc(SLOT->Cnt, env, chip->phase_modulation, SLOT->wavetable);
}
INLINE void chan_calc_ext( OPL3 *chip, OPL3_CH *CH )
{
OPL3_SLOT *SLOT;
unsigned int env;
chip->phase_modulation = 0;
SLOT = &CH->SLOT[SLOT1];
env = volume_calc(SLOT);
if( env < ENV_QUIET )
*SLOT->connect += op_calc(SLOT->Cnt, env, chip->phase_modulation2, SLOT->wavetable );
SLOT++;
env = volume_calc(SLOT);
if( env < ENV_QUIET )
*SLOT->connect += op_calc(SLOT->Cnt, env, chip->phase_modulation, SLOT->wavetable);
}
INLINE void chan_calc_rhythm( OPL3 *chip, OPL3_CH *CH, unsigned int noise )
{
OPL3_SLOT *SLOT;
signed int out;
unsigned int env;
chip->phase_modulation = 0;
SLOT = &CH[6].SLOT[SLOT1];
env = volume_calc(SLOT);
out = SLOT->op1_out[0] + SLOT->op1_out[1];
SLOT->op1_out[0] = SLOT->op1_out[1];
if (!SLOT->CON)
chip->phase_modulation = SLOT->op1_out[0];
SLOT->op1_out[1] = 0;
if( env < ENV_QUIET )
{
if (!SLOT->FB)
out = 0;
SLOT->op1_out[1] = op_calc1(SLOT->Cnt, env, (out<<SLOT->FB), SLOT->wavetable );
}
SLOT++;
env = volume_calc(SLOT);
if( env < ENV_QUIET )
chip->chanout[6] += op_calc(SLOT->Cnt, env, chip->phase_modulation, SLOT->wavetable) * 2;
env = volume_calc(SLOT7_1);
if( env < ENV_QUIET )
{
unsigned char bit7 = ((SLOT7_1->Cnt>>FREQ_SH)>>7)&1;
unsigned char bit3 = ((SLOT7_1->Cnt>>FREQ_SH)>>3)&1;
unsigned char bit2 = ((SLOT7_1->Cnt>>FREQ_SH)>>2)&1;
unsigned char res1 = (bit2 ^ bit7) | bit3;
UINT32 phase = res1 ? (0x200|(0xd0>>2)) : 0xd0;
unsigned char bit5e= ((SLOT8_2->Cnt>>FREQ_SH)>>5)&1;
unsigned char bit3e= ((SLOT8_2->Cnt>>FREQ_SH)>>3)&1;
unsigned char res2 = (bit3e ^ bit5e);
if (res2)
phase = (0x200|(0xd0>>2));
if (phase&0x200)
{
if (noise)
phase = 0x200|0xd0;
}
else
{
if (noise)
phase = 0xd0>>2;
}
chip->chanout[7] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_1->wavetable) * 2;
}
env = volume_calc(SLOT7_2);
if( env < ENV_QUIET )
{
unsigned char bit8 = ((SLOT7_1->Cnt>>FREQ_SH)>>8)&1;
UINT32 phase = bit8 ? 0x200 : 0x100;
if (noise)
phase ^= 0x100;
chip->chanout[7] += op_calc(phase<<FREQ_SH, env, 0, SLOT7_2->wavetable) * 2;
}
env = volume_calc(SLOT8_1);
if( env < ENV_QUIET )
chip->chanout[8] += op_calc(SLOT8_1->Cnt, env, 0, SLOT8_1->wavetable) * 2;
env = volume_calc(SLOT8_2);
if( env < ENV_QUIET )
{
unsigned char bit7 = ((SLOT7_1->Cnt>>FREQ_SH)>>7)&1;
unsigned char bit3 = ((SLOT7_1->Cnt>>FREQ_SH)>>3)&1;
unsigned char bit2 = ((SLOT7_1->Cnt>>FREQ_SH)>>2)&1;
unsigned char res1 = (bit2 ^ bit7) | bit3;
UINT32 phase = res1 ? 0x300 : 0x100;
unsigned char bit5e= ((SLOT8_2->Cnt>>FREQ_SH)>>5)&1;
unsigned char bit3e= ((SLOT8_2->Cnt>>FREQ_SH)>>3)&1;
unsigned char res2 = (bit3e ^ bit5e);
if (res2)
phase = 0x300;
chip->chanout[8] += op_calc(phase<<FREQ_SH, env, 0, SLOT8_2->wavetable) * 2;
}
}
static int init_tables(void)
{
signed int i,x;
signed int n;
double o,m;
for (x=0; x<TL_RES_LEN; x++)
{
m = (1<<16) / pow(2, (x+1) * (ENV_STEP/4.0) / 8.0);
m = floor(m);
n = (int)m;
n >>= 4;
if (n&1)
n = (n>>1)+1;
else
n = n>>1;
n <<= 1;
tl_tab[ x*2 + 0 ] = n;
tl_tab[ x*2 + 1 ] = ~tl_tab[ x*2 + 0 ];
for (i=1; i<13; i++)
{
tl_tab[ x*2+0 + i*2*TL_RES_LEN ] = tl_tab[ x*2+0 ]>>i;
tl_tab[ x*2+1 + i*2*TL_RES_LEN ] = ~tl_tab[ x*2+0 + i*2*TL_RES_LEN ];
}
#if 0 #endif
}
for (i=0; i<SIN_LEN; i++)
{
m = sin( ((i*2)+1) * PI / SIN_LEN );
if (m>0.0)
o = 8*log(1.0/m)/log(2);
else
o = 8*log(-1.0/m)/log(2);
o = o / (ENV_STEP/4);
n = (int)(2.0*o);
if (n&1)
n = (n>>1)+1;
else
n = n>>1;
sin_tab[ i ] = n*2 + (m>=0.0? 0: 1 );
}
for (i=0; i<SIN_LEN; i++)
{
if (i & (1<<(SIN_BITS-1)) )
sin_tab[1*SIN_LEN+i] = TL_TAB_LEN;
else
sin_tab[1*SIN_LEN+i] = sin_tab[i];
sin_tab[2*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>1) ];
if (i & (1<<(SIN_BITS-2)) )
sin_tab[3*SIN_LEN+i] = TL_TAB_LEN;
else
sin_tab[3*SIN_LEN+i] = sin_tab[i & (SIN_MASK>>2)];
if (i & (1<<(SIN_BITS-1)) )
sin_tab[4*SIN_LEN+i] = TL_TAB_LEN;
else
sin_tab[4*SIN_LEN+i] = sin_tab[i*2];
if (i & (1<<(SIN_BITS-1)) )
sin_tab[5*SIN_LEN+i] = TL_TAB_LEN;
else
sin_tab[5*SIN_LEN+i] = sin_tab[(i*2) & (SIN_MASK>>1) ];
if (i & (1<<(SIN_BITS-1)) )
sin_tab[6*SIN_LEN+i] = 1;
else
sin_tab[6*SIN_LEN+i] = 0;
if (i & (1<<(SIN_BITS-1)) )
x = ((SIN_LEN-1)-i)*16 + 1;
else
x = i*16;
if (x > TL_TAB_LEN)
x = TL_TAB_LEN;
sin_tab[7*SIN_LEN+i] = x;
}
#ifdef SAVE_SAMPLE
sample[0]=fopen("sampsum.pcm","wb");
#endif
return 1;
}
static void OPLCloseTable( void )
{
#ifdef SAVE_SAMPLE
fclose(sample[0]);
#endif
}
static void OPL3_initalize(OPL3 *chip)
{
int i;
chip->freqbase = (chip->rate) ? ((double)chip->clock / (8.0*36)) / chip->rate : 0;
#if 0#endif
chip->TimerBase = 1.0 / ((double)chip->clock / (8.0*36) );
for( i=0 ; i < 1024 ; i++ )
{
chip->fn_tab[i] = (UINT32)( (double)i * 64 * chip->freqbase * (1<<(FREQ_SH-10)) );
#if 0#endif
}
#if 0#endif
chip->lfo_am_inc = (1.0 / 64.0 ) * (1<<LFO_SH) * chip->freqbase;
chip->lfo_pm_inc = (1.0 / 1024.0) * (1<<LFO_SH) * chip->freqbase;
chip->noise_f = (1.0 / 1.0) * (1<<FREQ_SH) * chip->freqbase;
chip->eg_timer_add = (1<<EG_SH) * chip->freqbase;
chip->eg_timer_overflow = ( 1 ) * (1<<EG_SH);
}
INLINE void FM_KEYON(OPL3_SLOT *SLOT, UINT32 key_set)
{
if( !SLOT->key )
{
SLOT->Cnt = 0;
SLOT->state = EG_ATT;
}
SLOT->key |= key_set;
}
INLINE void FM_KEYOFF(OPL3_SLOT *SLOT, UINT32 key_clr)
{
if( SLOT->key )
{
SLOT->key &= key_clr;
if( !SLOT->key )
{
if (SLOT->state>EG_REL)
SLOT->state = EG_REL;
}
}
}
INLINE void CALC_FCSLOT(OPL3_CH *CH,OPL3_SLOT *SLOT)
{
int ksr;
SLOT->Incr = CH->fc * SLOT->mul;
ksr = CH->kcode >> SLOT->KSR;
if( SLOT->ksr != ksr )
{
SLOT->ksr = ksr;
if ((SLOT->ar + SLOT->ksr) < 16+60)
{
SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
SLOT->eg_m_ar = (1<<SLOT->eg_sh_ar)-1;
SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
}
else
{
SLOT->eg_sh_ar = 0;
SLOT->eg_m_ar = (1<<SLOT->eg_sh_ar)-1;
SLOT->eg_sel_ar = 13*RATE_STEPS;
}
SLOT->eg_sh_dr = eg_rate_shift [SLOT->dr + SLOT->ksr ];
SLOT->eg_m_dr = (1<<SLOT->eg_sh_dr)-1;
SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr ];
SLOT->eg_m_rr = (1<<SLOT->eg_sh_rr)-1;
SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
}
}
INLINE void set_mul(OPL3 *chip,int slot,int v)
{
OPL3_CH *CH = &chip->P_CH[slot/2];
OPL3_SLOT *SLOT = &CH->SLOT[slot&1];
SLOT->mul = mul_tab[v&0x0f];
SLOT->KSR = (v&0x10) ? 0 : 2;
SLOT->eg_type = (v&0x20);
SLOT->vib = (v&0x40);
SLOT->AMmask = (v&0x80) ? ~0 : 0;
if (chip->OPL3_mode & 1)
{
int chan_no = slot/2;
switch(chan_no)
{
case 0: case 1: case 2:
case 9: case 10: case 11:
if (CH->extended)
{
CALC_FCSLOT(CH,SLOT);
}
else
{
CALC_FCSLOT(CH,SLOT);
}
break;
case 3: case 4: case 5:
case 12: case 13: case 14:
if ((CH-3)->extended)
{
CALC_FCSLOT(CH-3,SLOT);
}
else
{
CALC_FCSLOT(CH,SLOT);
}
break;
default:
CALC_FCSLOT(CH,SLOT);
break;
}
}
else
{
CALC_FCSLOT(CH,SLOT);
}
}
INLINE void set_ksl_tl(OPL3 *chip,int slot,int v)
{
OPL3_CH *CH = &chip->P_CH[slot/2];
OPL3_SLOT *SLOT = &CH->SLOT[slot&1];
int ksl = v>>6;
SLOT->ksl = ksl ? 3-ksl : 31;
SLOT->TL = (v&0x3f)<<(ENV_BITS-1-7);
if (chip->OPL3_mode & 1)
{
int chan_no = slot/2;
switch(chan_no)
{
case 0: case 1: case 2:
case 9: case 10: case 11:
if (CH->extended)
{
SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
}
else
{
SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
}
break;
case 3: case 4: case 5:
case 12: case 13: case 14:
if ((CH-3)->extended)
{
SLOT->TLL = SLOT->TL + ((CH-3)->ksl_base>>SLOT->ksl);
}
else
{
SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
}
break;
default:
SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
break;
}
}
else
{
SLOT->TLL = SLOT->TL + (CH->ksl_base>>SLOT->ksl);
}
}
INLINE void set_ar_dr(OPL3 *chip,int slot,int v)
{
OPL3_CH *CH = &chip->P_CH[slot/2];
OPL3_SLOT *SLOT = &CH->SLOT[slot&1];
SLOT->ar = (v>>4) ? 16 + ((v>>4) <<2) : 0;
if ((SLOT->ar + SLOT->ksr) < 16+60)
{
SLOT->eg_sh_ar = eg_rate_shift [SLOT->ar + SLOT->ksr ];
SLOT->eg_m_ar = (1<<SLOT->eg_sh_ar)-1;
SLOT->eg_sel_ar = eg_rate_select[SLOT->ar + SLOT->ksr ];
}
else
{
SLOT->eg_sh_ar = 0;
SLOT->eg_m_ar = (1<<SLOT->eg_sh_ar)-1;
SLOT->eg_sel_ar = 13*RATE_STEPS;
}
SLOT->dr = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
SLOT->eg_sh_dr = eg_rate_shift [SLOT->dr + SLOT->ksr ];
SLOT->eg_m_dr = (1<<SLOT->eg_sh_dr)-1;
SLOT->eg_sel_dr = eg_rate_select[SLOT->dr + SLOT->ksr ];
}
INLINE void set_sl_rr(OPL3 *chip,int slot,int v)
{
OPL3_CH *CH = &chip->P_CH[slot/2];
OPL3_SLOT *SLOT = &CH->SLOT[slot&1];
SLOT->sl = sl_tab[ v>>4 ];
SLOT->rr = (v&0x0f)? 16 + ((v&0x0f)<<2) : 0;
SLOT->eg_sh_rr = eg_rate_shift [SLOT->rr + SLOT->ksr ];
SLOT->eg_m_rr = (1<<SLOT->eg_sh_rr)-1;
SLOT->eg_sel_rr = eg_rate_select[SLOT->rr + SLOT->ksr ];
}
static void update_channels(OPL3 *chip, OPL3_CH *CH)
{
if (CH->extended)
{
}
else
{
}
}
static void OPL3WriteReg(OPL3 *chip, int r, int v)
{
OPL3_CH *CH;
unsigned int ch_offset = 0;
int slot;
int block_fnum;
#ifdef LOG_CYM_FILE
if ((cymfile) && ((r&255)!=0) && (r!=255) )
{
if (r>0xff)
fputc( (unsigned char)0xff, cymfile );
fputc( (unsigned char)r&0xff, cymfile );
fputc( (unsigned char)v, cymfile );
}
#endif
if(r&0x100)
{
switch(r)
{
case 0x101:
return;
break;
case 0x104:
{
UINT8 prev;
CH = &chip->P_CH[0];
prev = CH->extended;
CH->extended = (v>>0) & 1;
if(prev != CH->extended)
update_channels(chip, CH);
CH++;
prev = CH->extended;
CH->extended = (v>>1) & 1;
if(prev != CH->extended)
update_channels(chip, CH);
CH++;
prev = CH->extended;
CH->extended = (v>>2) & 1;
if(prev != CH->extended)
update_channels(chip, CH);
CH = &chip->P_CH[9];
prev = CH->extended;
CH->extended = (v>>3) & 1;
if(prev != CH->extended)
update_channels(chip, CH);
CH++;
prev = CH->extended;
CH->extended = (v>>4) & 1;
if(prev != CH->extended)
update_channels(chip, CH);
CH++;
prev = CH->extended;
CH->extended = (v>>5) & 1;
if(prev != CH->extended)
update_channels(chip, CH);
}
return;
break;
case 0x105:
chip->OPL3_mode = v&0x01;
return;
break;
default:
if (r < 0x120)
logerror("YMF262: write to unknown register (set#2): %03x value=%02x\n",r,v);
break;
}
ch_offset = 9;
}
r &= 0xff;
v &= 0xff;
switch(r&0xe0)
{
case 0x00:
switch(r&0x1f)
{
case 0x01:
break;
case 0x02:
chip->T[0] = (256-v)*4;
break;
case 0x03:
chip->T[1] = (256-v)*16;
break;
case 0x04:
if(v&0x80)
{
OPL3_STATUS_RESET(chip,0x60);
}
else
{
UINT8 st1 = v & 1;
UINT8 st2 = (v>>1) & 1;
OPL3_STATUS_RESET(chip, v & 0x60);
OPL3_STATUSMASK_SET(chip, (~v) & 0x60 );
if(chip->st[1] != st2)
{
double interval = st2 ? (double)chip->T[1]*chip->TimerBase : 0.0;
chip->st[1] = st2;
if (chip->TimerHandler) (chip->TimerHandler)(chip->TimerParam,1,interval);
}
if(chip->st[0] != st1)
{
double interval = st1 ? (double)chip->T[0]*chip->TimerBase : 0.0;
chip->st[0] = st1;
if (chip->TimerHandler) (chip->TimerHandler)(chip->TimerParam,0,interval);
}
}
break;
case 0x08:
chip->nts = v;
break;
default:
logerror("YMF262: write to unknown register: %02x value=%02x\n",r,v);
break;
}
break;
case 0x20:
slot = slot_array[r&0x1f];
if(slot < 0) return;
set_mul(chip, slot + ch_offset*2, v);
break;
case 0x40:
slot = slot_array[r&0x1f];
if(slot < 0) return;
set_ksl_tl(chip, slot + ch_offset*2, v);
break;
case 0x60:
slot = slot_array[r&0x1f];
if(slot < 0) return;
set_ar_dr(chip, slot + ch_offset*2, v);
break;
case 0x80:
slot = slot_array[r&0x1f];
if(slot < 0) return;
set_sl_rr(chip, slot + ch_offset*2, v);
break;
case 0xa0:
if (r == 0xbd)
{
if (ch_offset != 0)
return;
chip->lfo_am_depth = v & 0x80;
chip->lfo_pm_depth_range = (v&0x40) ? 8 : 0;
chip->rhythm = v&0x3f;
if(chip->rhythm&0x20)
{
if(v&0x10)
{
FM_KEYON (&chip->P_CH[6].SLOT[SLOT1], 2);
FM_KEYON (&chip->P_CH[6].SLOT[SLOT2], 2);
}
else
{
FM_KEYOFF(&chip->P_CH[6].SLOT[SLOT1],~2);
FM_KEYOFF(&chip->P_CH[6].SLOT[SLOT2],~2);
}
if(v&0x01) FM_KEYON (&chip->P_CH[7].SLOT[SLOT1], 2);
else FM_KEYOFF(&chip->P_CH[7].SLOT[SLOT1],~2);
if(v&0x08) FM_KEYON (&chip->P_CH[7].SLOT[SLOT2], 2);
else FM_KEYOFF(&chip->P_CH[7].SLOT[SLOT2],~2);
if(v&0x04) FM_KEYON (&chip->P_CH[8].SLOT[SLOT1], 2);
else FM_KEYOFF(&chip->P_CH[8].SLOT[SLOT1],~2);
if(v&0x02) FM_KEYON (&chip->P_CH[8].SLOT[SLOT2], 2);
else FM_KEYOFF(&chip->P_CH[8].SLOT[SLOT2],~2);
}
else
{
FM_KEYOFF(&chip->P_CH[6].SLOT[SLOT1],~2);
FM_KEYOFF(&chip->P_CH[6].SLOT[SLOT2],~2);
FM_KEYOFF(&chip->P_CH[7].SLOT[SLOT1],~2);
FM_KEYOFF(&chip->P_CH[7].SLOT[SLOT2],~2);
FM_KEYOFF(&chip->P_CH[8].SLOT[SLOT1],~2);
FM_KEYOFF(&chip->P_CH[8].SLOT[SLOT2],~2);
}
return;
}
if( (r&0x0f) > 8) return;
CH = &chip->P_CH[(r&0x0f) + ch_offset];
if(!(r&0x10))
{
block_fnum = (CH->block_fnum&0x1f00) | v;
}
else
{
block_fnum = ((v&0x1f)<<8) | (CH->block_fnum&0xff);
if (chip->OPL3_mode & 1)
{
int chan_no = (r&0x0f) + ch_offset;
switch(chan_no)
{
case 0: case 1: case 2:
case 9: case 10: case 11:
if (CH->extended)
{
if(v&0x20)
{
FM_KEYON (&CH->SLOT[SLOT1], 1);
FM_KEYON (&CH->SLOT[SLOT2], 1);
FM_KEYON (&(CH+3)->SLOT[SLOT1], 1);
FM_KEYON (&(CH+3)->SLOT[SLOT2], 1);
}
else
{
FM_KEYOFF(&CH->SLOT[SLOT1],~1);
FM_KEYOFF(&CH->SLOT[SLOT2],~1);
FM_KEYOFF(&(CH+3)->SLOT[SLOT1],~1);
FM_KEYOFF(&(CH+3)->SLOT[SLOT2],~1);
}
}
else
{
if(v&0x20)
{
FM_KEYON (&CH->SLOT[SLOT1], 1);
FM_KEYON (&CH->SLOT[SLOT2], 1);
}
else
{
FM_KEYOFF(&CH->SLOT[SLOT1],~1);
FM_KEYOFF(&CH->SLOT[SLOT2],~1);
}
}
break;
case 3: case 4: case 5:
case 12: case 13: case 14:
if ((CH-3)->extended)
{
}
else
{
if(v&0x20)
{
FM_KEYON (&CH->SLOT[SLOT1], 1);
FM_KEYON (&CH->SLOT[SLOT2], 1);
}
else
{
FM_KEYOFF(&CH->SLOT[SLOT1],~1);
FM_KEYOFF(&CH->SLOT[SLOT2],~1);
}
}
break;
default:
if(v&0x20)
{
FM_KEYON (&CH->SLOT[SLOT1], 1);
FM_KEYON (&CH->SLOT[SLOT2], 1);
}
else
{
FM_KEYOFF(&CH->SLOT[SLOT1],~1);
FM_KEYOFF(&CH->SLOT[SLOT2],~1);
}
break;
}
}
else
{
if(v&0x20)
{
FM_KEYON (&CH->SLOT[SLOT1], 1);
FM_KEYON (&CH->SLOT[SLOT2], 1);
}
else
{
FM_KEYOFF(&CH->SLOT[SLOT1],~1);
FM_KEYOFF(&CH->SLOT[SLOT2],~1);
}
}
}
if(CH->block_fnum != block_fnum)
{
UINT8 block = block_fnum >> 10;
CH->block_fnum = block_fnum;
CH->ksl_base = ksl_tab[block_fnum>>6];
CH->fc = chip->fn_tab[block_fnum&0x03ff] >> (7-block);
CH->kcode = (CH->block_fnum&0x1c00)>>9;
if (chip->nts&0x40)
CH->kcode |= (CH->block_fnum&0x100)>>8;
else
CH->kcode |= (CH->block_fnum&0x200)>>9;
if (chip->OPL3_mode & 1)
{
int chan_no = (r&0x0f) + ch_offset;
switch(chan_no)
{
case 0: case 1: case 2:
case 9: case 10: case 11:
if (CH->extended)
{
CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
(CH+3)->SLOT[SLOT1].TLL = (CH+3)->SLOT[SLOT1].TL + (CH->ksl_base>>(CH+3)->SLOT[SLOT1].ksl);
(CH+3)->SLOT[SLOT2].TLL = (CH+3)->SLOT[SLOT2].TL + (CH->ksl_base>>(CH+3)->SLOT[SLOT2].ksl);
CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
CALC_FCSLOT(CH,&(CH+3)->SLOT[SLOT1]);
CALC_FCSLOT(CH,&(CH+3)->SLOT[SLOT2]);
}
else
{
CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
}
break;
case 3: case 4: case 5:
case 12: case 13: case 14:
if ((CH-3)->extended)
{
}
else
{
CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
}
break;
default:
CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
break;
}
}
else
{
CH->SLOT[SLOT1].TLL = CH->SLOT[SLOT1].TL + (CH->ksl_base>>CH->SLOT[SLOT1].ksl);
CH->SLOT[SLOT2].TLL = CH->SLOT[SLOT2].TL + (CH->ksl_base>>CH->SLOT[SLOT2].ksl);
CALC_FCSLOT(CH,&CH->SLOT[SLOT1]);
CALC_FCSLOT(CH,&CH->SLOT[SLOT2]);
}
}
break;
case 0xc0:
if( (r&0xf) > 8) return;
CH = &chip->P_CH[(r&0xf) + ch_offset];
if( chip->OPL3_mode & 1 )
{
int base = ((r&0xf) + ch_offset) * 4;
chip->pan[ base ] = (v & 0x10) ? ~0 : 0;
chip->pan[ base +1 ] = (v & 0x20) ? ~0 : 0;
chip->pan[ base +2 ] = (v & 0x40) ? ~0 : 0;
chip->pan[ base +3 ] = (v & 0x80) ? ~0 : 0;
}
else
{
int base = ((r&0xf) + ch_offset) * 4;
chip->pan[ base ] = ~0;
chip->pan[ base +1 ] = ~0;
chip->pan[ base +2 ] = ~0;
chip->pan[ base +3 ] = ~0;
}
chip->pan_ctrl_value[ (r&0xf) + ch_offset ] = v;
CH->SLOT[SLOT1].FB = (v>>1)&7 ? ((v>>1)&7) + 7 : 0;
CH->SLOT[SLOT1].CON = v&1;
if( chip->OPL3_mode & 1 )
{
int chan_no = (r&0x0f) + ch_offset;
switch(chan_no)
{
case 0: case 1: case 2:
case 9: case 10: case 11:
if (CH->extended)
{
UINT8 conn = (CH->SLOT[SLOT1].CON<<1) || ((CH+3)->SLOT[SLOT1].CON<<0);
switch(conn)
{
case 0:
CH->SLOT[SLOT1].connect = &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->phase_modulation2;
(CH+3)->SLOT[SLOT1].connect = &chip->phase_modulation;
(CH+3)->SLOT[SLOT2].connect = &chip->chanout[ chan_no + 3 ];
break;
case 1:
CH->SLOT[SLOT1].connect = &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[ chan_no ];
(CH+3)->SLOT[SLOT1].connect = &chip->phase_modulation;
(CH+3)->SLOT[SLOT2].connect = &chip->chanout[ chan_no + 3 ];
break;
case 2:
CH->SLOT[SLOT1].connect = &chip->chanout[ chan_no ];
CH->SLOT[SLOT2].connect = &chip->phase_modulation2;
(CH+3)->SLOT[SLOT1].connect = &chip->phase_modulation;
(CH+3)->SLOT[SLOT2].connect = &chip->chanout[ chan_no + 3 ];
break;
case 3:
CH->SLOT[SLOT1].connect = &chip->chanout[ chan_no ];
CH->SLOT[SLOT2].connect = &chip->phase_modulation2;
(CH+3)->SLOT[SLOT1].connect = &chip->chanout[ chan_no + 3 ];
(CH+3)->SLOT[SLOT2].connect = &chip->chanout[ chan_no + 3 ];
break;
}
}
else
{
CH->SLOT[SLOT1].connect = CH->SLOT[SLOT1].CON ? &chip->chanout[(r&0xf)+ch_offset] : &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[(r&0xf)+ch_offset];
}
break;
case 3: case 4: case 5:
case 12: case 13: case 14:
if ((CH-3)->extended)
{
UINT8 conn = ((CH-3)->SLOT[SLOT1].CON<<1) || (CH->SLOT[SLOT1].CON<<0);
switch(conn)
{
case 0:
(CH-3)->SLOT[SLOT1].connect = &chip->phase_modulation;
(CH-3)->SLOT[SLOT2].connect = &chip->phase_modulation2;
CH->SLOT[SLOT1].connect = &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[ chan_no ];
break;
case 1:
(CH-3)->SLOT[SLOT1].connect = &chip->phase_modulation;
(CH-3)->SLOT[SLOT2].connect = &chip->chanout[ chan_no - 3 ];
CH->SLOT[SLOT1].connect = &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[ chan_no ];
break;
case 2:
(CH-3)->SLOT[SLOT1].connect = &chip->chanout[ chan_no - 3 ];
(CH-3)->SLOT[SLOT2].connect = &chip->phase_modulation2;
CH->SLOT[SLOT1].connect = &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[ chan_no ];
break;
case 3:
(CH-3)->SLOT[SLOT1].connect = &chip->chanout[ chan_no - 3 ];
(CH-3)->SLOT[SLOT2].connect = &chip->phase_modulation2;
CH->SLOT[SLOT1].connect = &chip->chanout[ chan_no ];
CH->SLOT[SLOT2].connect = &chip->chanout[ chan_no ];
break;
}
}
else
{
CH->SLOT[SLOT1].connect = CH->SLOT[SLOT1].CON ? &chip->chanout[(r&0xf)+ch_offset] : &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[(r&0xf)+ch_offset];
}
break;
default:
CH->SLOT[SLOT1].connect = CH->SLOT[SLOT1].CON ? &chip->chanout[(r&0xf)+ch_offset] : &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[(r&0xf)+ch_offset];
break;
}
}
else
{
CH->SLOT[SLOT1].connect = CH->SLOT[SLOT1].CON ? &chip->chanout[(r&0xf)+ch_offset] : &chip->phase_modulation;
CH->SLOT[SLOT2].connect = &chip->chanout[(r&0xf)+ch_offset];
}
break;
case 0xe0:
slot = slot_array[r&0x1f];
if(slot < 0) return;
slot += ch_offset*2;
CH = &chip->P_CH[slot/2];
v &= 7;
CH->SLOT[slot&1].waveform_number = v;
if( !(chip->OPL3_mode & 1) )
{
v &= 3;
}
CH->SLOT[slot&1].wavetable = v * SIN_LEN;
break;
}
}
#ifdef LOG_CYM_FILE
static void cymfile_callback (int n)
{
if (cymfile)
{
fputc( (unsigned char)0, cymfile );
}
}
#endif
static int OPL3_LockTable(void)
{
num_lock++;
if(num_lock>1) return 0;
if( !init_tables() )
{
num_lock--;
return -1;
}
#ifdef LOG_CYM_FILE
cymfile = fopen("ymf262_.cym","wb");
if (cymfile)
timer_pulse ( TIME_IN_HZ(110), 0, cymfile_callback);
else
logerror("Could not create ymf262_.cym file\n");
#endif
return 0;
}
static void OPL3_UnLockTable(void)
{
if(num_lock) num_lock--;
if(num_lock) return;
OPLCloseTable();
#ifdef LOG_CYM_FILE
fclose (cymfile);
cymfile = NULL;
#endif
}
static void OPL3ResetChip(OPL3 *chip)
{
int c,s;
chip->eg_timer = 0;
chip->eg_cnt = 0;
chip->noise_rng = 1;
chip->nts = 0;
OPL3_STATUS_RESET(chip,0x60);
OPL3WriteReg(chip,0x01,0);
OPL3WriteReg(chip,0x02,0);
OPL3WriteReg(chip,0x03,0);
OPL3WriteReg(chip,0x04,0);
for(c = 0xff ; c >= 0x20 ; c-- )
OPL3WriteReg(chip,c,0);
for(c = 0x1ff ; c >= 0x120 ; c-- )
OPL3WriteReg(chip,c,0);
for( c = 0 ; c < 9*2 ; c++ )
{
OPL3_CH *CH = &chip->P_CH[c];
for(s = 0 ; s < 2 ; s++ )
{
CH->SLOT[s].state = EG_OFF;
CH->SLOT[s].volume = MAX_ATT_INDEX;
}
}
}
static OPL3 *OPL3Create(int type, int clock, int rate)
{
OPL3 *chip;
if (OPL3_LockTable() ==-1) return NULL;
chip = (OPL3 *)malloc(sizeof(OPL3));
if (chip==NULL)
return NULL;
memset(chip, 0, sizeof(OPL3));
chip->type = type;
chip->clock = clock;
chip->rate = rate;
OPL3_initalize(chip);
OPL3ResetChip(chip);
return chip;
}
static void OPL3Destroy(OPL3 *chip)
{
OPL3_UnLockTable();
free(chip);
}
static void OPL3SetTimerHandler(OPL3 *chip,OPL3_TIMERHANDLER TimerHandler,void *param)
{
chip->TimerHandler = TimerHandler;
chip->TimerParam = param;
}
static void OPL3SetIRQHandler(OPL3 *chip,OPL3_IRQHANDLER IRQHandler,void *param)
{
chip->IRQHandler = IRQHandler;
chip->IRQParam = param;
}
static void OPL3SetUpdateHandler(OPL3 *chip,OPL3_UPDATEHANDLER UpdateHandler,void *param)
{
chip->UpdateHandler = UpdateHandler;
chip->UpdateParam = param;
}
static int OPL3Write(OPL3 *chip, int a, int v)
{
v &= 0xff;
switch(a&3)
{
case 0:
chip->address = v;
break;
case 1:
case 3:
if(chip->UpdateHandler) chip->UpdateHandler(chip->UpdateParam,0);
OPL3WriteReg(chip,chip->address,v);
break;
case 2:
if( chip->OPL3_mode & 1 )
{
chip->address = v | 0x100;
}
else
{
if( v==5 )
chip->address = v | 0x100;
else
chip->address = v;
}
break;
}
return chip->status>>7;
}
static unsigned char OPL3Read(OPL3 *chip,int a)
{
if( a==0 )
{
return chip->status;
}
return 0x00;
}
static int OPL3TimerOver(OPL3 *chip,int c)
{
if( c )
{
OPL3_STATUS_SET(chip,0x20);
}
else
{
OPL3_STATUS_SET(chip,0x40);
}
if (chip->TimerHandler) (chip->TimerHandler)(chip->TimerParam,c,(double)chip->T[c]*chip->TimerBase);
return chip->status>>7;
}
#if (BUILD_YMF262)
void * YMF262Init(int clock, int rate)
{
return OPL3Create(OPL3_TYPE_YMF262,clock,rate);
}
void YMF262Shutdown(void *chip)
{
OPL3Destroy((OPL3*)chip);
}
void YMF262ResetChip(void *chip)
{
OPL3ResetChip((OPL3*)chip);
}
int YMF262Write(void *chip, int a, int v)
{
return OPL3Write((OPL3*)chip, a, v);
}
unsigned char YMF262Read(void *chip, int a)
{
return OPL3Read((OPL3*)chip, a);
}
int YMF262TimerOver(void *chip, int c)
{
return OPL3TimerOver((OPL3*)chip, c);
}
void YMF262SetTimerHandler(void *chip, OPL3_TIMERHANDLER TimerHandler, void *param)
{
OPL3SetTimerHandler((OPL3*)chip, TimerHandler, param);
}
void YMF262SetIRQHandler(void *chip,OPL3_IRQHANDLER IRQHandler,void *param)
{
OPL3SetIRQHandler((OPL3*)chip, IRQHandler, param);
}
void YMF262SetUpdateHandler(void *chip,OPL3_UPDATEHANDLER UpdateHandler,void *param)
{
OPL3SetUpdateHandler((OPL3*)chip, UpdateHandler, param);
}
void YMF262UpdateOne(void *_chip, OPL3SAMPLE **buffers, int length)
{
OPL3 *chip = (OPL3*)_chip;
UINT8 rhythm = chip->rhythm&0x20;
OPL3SAMPLE *ch_a = buffers[0];
OPL3SAMPLE *ch_b = buffers[1];
OPL3SAMPLE *ch_c = buffers[2];
OPL3SAMPLE *ch_d = buffers[3];
int i;
#if 0#endif
for( i=0; i < length ; i++ )
{
int a,b,c,d;
advance_lfo(chip);
memset(chip->chanout, 0, sizeof(signed int) * 18);
#if 1
chan_calc(chip, &chip->P_CH[0]);
if (chip->P_CH[0].extended)
chan_calc_ext(chip, &chip->P_CH[3]);
else
chan_calc(chip, &chip->P_CH[3]);
chan_calc(chip, &chip->P_CH[1]);
if (chip->P_CH[1].extended)
chan_calc_ext(chip, &chip->P_CH[4]);
else
chan_calc(chip, &chip->P_CH[4]);
chan_calc(chip, &chip->P_CH[2]);
if (chip->P_CH[2].extended)
chan_calc_ext(chip, &chip->P_CH[5]);
else
chan_calc(chip, &chip->P_CH[5]);
if(!rhythm)
{
chan_calc(chip, &chip->P_CH[6]);
chan_calc(chip, &chip->P_CH[7]);
chan_calc(chip, &chip->P_CH[8]);
}
else
{
chan_calc_rhythm(chip, &chip->P_CH[0], (chip->noise_rng>>0)&1 );
}
chan_calc(chip, &chip->P_CH[ 9]);
if (chip->P_CH[9].extended)
chan_calc_ext(chip, &chip->P_CH[12]);
else
chan_calc(chip, &chip->P_CH[12]);
chan_calc(chip, &chip->P_CH[10]);
if (chip->P_CH[10].extended)
chan_calc_ext(chip, &chip->P_CH[13]);
else
chan_calc(chip, &chip->P_CH[13]);
chan_calc(chip, &chip->P_CH[11]);
if (chip->P_CH[11].extended)
chan_calc_ext(chip, &chip->P_CH[14]);
else
chan_calc(chip, &chip->P_CH[14]);
chan_calc(chip, &chip->P_CH[15]);
chan_calc(chip, &chip->P_CH[16]);
chan_calc(chip, &chip->P_CH[17]);
#endif
{
signed int *chanout = chip->chanout;
a = chanout[0] & chip->pan[0];
b = chanout[0] & chip->pan[1];
c = chanout[0] & chip->pan[2];
d = chanout[0] & chip->pan[3];
#if 1
a += chanout[1] & chip->pan[4];
b += chanout[1] & chip->pan[5];
c += chanout[1] & chip->pan[6];
d += chanout[1] & chip->pan[7];
a += chanout[2] & chip->pan[8];
b += chanout[2] & chip->pan[9];
c += chanout[2] & chip->pan[10];
d += chanout[2] & chip->pan[11];
a += chanout[3] & chip->pan[12];
b += chanout[3] & chip->pan[13];
c += chanout[3] & chip->pan[14];
d += chanout[3] & chip->pan[15];
a += chanout[4] & chip->pan[16];
b += chanout[4] & chip->pan[17];
c += chanout[4] & chip->pan[18];
d += chanout[4] & chip->pan[19];
a += chanout[5] & chip->pan[20];
b += chanout[5] & chip->pan[21];
c += chanout[5] & chip->pan[22];
d += chanout[5] & chip->pan[23];
a += chanout[6] & chip->pan[24];
b += chanout[6] & chip->pan[25];
c += chanout[6] & chip->pan[26];
d += chanout[6] & chip->pan[27];
a += chanout[7] & chip->pan[28];
b += chanout[7] & chip->pan[29];
c += chanout[7] & chip->pan[30];
d += chanout[7] & chip->pan[31];
a += chanout[8] & chip->pan[32];
b += chanout[8] & chip->pan[33];
c += chanout[8] & chip->pan[34];
d += chanout[8] & chip->pan[35];
a += chanout[9] & chip->pan[36];
b += chanout[9] & chip->pan[37];
c += chanout[9] & chip->pan[38];
d += chanout[9] & chip->pan[39];
a += chanout[10] & chip->pan[40];
b += chanout[10] & chip->pan[41];
c += chanout[10] & chip->pan[42];
d += chanout[10] & chip->pan[43];
a += chanout[11] & chip->pan[44];
b += chanout[11] & chip->pan[45];
c += chanout[11] & chip->pan[46];
d += chanout[11] & chip->pan[47];
a += chanout[12] & chip->pan[48];
b += chanout[12] & chip->pan[49];
c += chanout[12] & chip->pan[50];
d += chanout[12] & chip->pan[51];
a += chanout[13] & chip->pan[52];
b += chanout[13] & chip->pan[53];
c += chanout[13] & chip->pan[54];
d += chanout[13] & chip->pan[55];
a += chanout[14] & chip->pan[56];
b += chanout[14] & chip->pan[57];
c += chanout[14] & chip->pan[58];
d += chanout[14] & chip->pan[59];
a += chanout[15] & chip->pan[60];
b += chanout[15] & chip->pan[61];
c += chanout[15] & chip->pan[62];
d += chanout[15] & chip->pan[63];
a += chanout[16] & chip->pan[64];
b += chanout[16] & chip->pan[65];
c += chanout[16] & chip->pan[66];
d += chanout[16] & chip->pan[67];
a += chanout[17] & chip->pan[68];
b += chanout[17] & chip->pan[69];
c += chanout[17] & chip->pan[70];
d += chanout[17] & chip->pan[71];
#endif
}
a >>= FINAL_SH;
b >>= FINAL_SH;
c >>= FINAL_SH;
d >>= FINAL_SH;
a = limit( a , MAXOUT, MINOUT );
b = limit( b , MAXOUT, MINOUT );
c = limit( c , MAXOUT, MINOUT );
d = limit( d , MAXOUT, MINOUT );
#ifdef SAVE_SAMPLE
if (which==0)
{
SAVE_ALL_CHANNELS
}
#endif
ch_a[i] = a;
ch_b[i] = b;
ch_c[i] = c;
ch_d[i] = d;
advance(chip);
}
}
#endif
}