code cleanup

This commit is contained in:
ekeeke31 2010-01-13 22:08:35 +00:00
parent 7a4c208a34
commit ac4de61944
5 changed files with 70 additions and 55 deletions

View File

@ -25,6 +25,7 @@ static double ratio = 1.0;
static void gen_sinc(double rolloff, int width, double offset, double spacing, double scale, int count, short *out ) static void gen_sinc(double rolloff, int width, double offset, double spacing, double scale, int count, short *out )
{ {
double w, rolloff_cos_a, num, den, sinc;
double const maxh = 256; double const maxh = 256;
double const fstep = M_PI / maxh * spacing; double const fstep = M_PI / maxh * spacing;
double const to_w = maxh * 2 / width; double const to_w = maxh * 2 / width;
@ -32,18 +33,19 @@ static void gen_sinc(double rolloff, int width, double offset, double spacing, d
scale /= maxh * 2; scale /= maxh * 2;
double angle = (count / 2 - 1 + offset) * -fstep; double angle = (count / 2 - 1 + offset) * -fstep;
while ( count-- ) while ( count-- )
{ {
*out++ = 0; *out++ = 0;
double w = angle * to_w; w = angle * to_w;
if ( fabs( w ) < M_PI ) if ( fabs( w ) < M_PI )
{ {
double rolloff_cos_a = rolloff * cos( angle ); rolloff_cos_a = rolloff * cos( angle );
double num = 1 - rolloff_cos_a - num = 1 - rolloff_cos_a -
pow_a_n * cos( maxh * angle ) + pow_a_n * cos( maxh * angle ) +
pow_a_n * rolloff * cos( (maxh - 1) * angle ); pow_a_n * rolloff * cos( (maxh - 1) * angle );
double den = 1 - rolloff_cos_a - rolloff_cos_a + rolloff * rolloff; den = 1 - rolloff_cos_a - rolloff_cos_a + rolloff * rolloff;
double sinc = scale * num / den - scale; sinc = scale * num / den - scale;
out [-1] = (short) (cos( w ) * sinc + sinc); out [-1] = (short) (cos( w ) * sinc + sinc);
} }
@ -51,7 +53,7 @@ static void gen_sinc(double rolloff, int width, double offset, double spacing, d
} }
} }
static int available( unsigned long input_count ) static int available( long input_count )
{ {
int cycle_count = input_count / input_per_cycle; int cycle_count = input_count / input_per_cycle;
int output_count = cycle_count * res * STEREO; int output_count = cycle_count * res * STEREO;
@ -75,14 +77,15 @@ static int available( unsigned long input_count )
int Fir_Resampler_initialize( int new_size ) int Fir_Resampler_initialize( int new_size )
{ {
buffer = (sample_t *) realloc( buffer, (new_size + WRITE_OFFSET) * sizeof (sample_t) );
write_pos = 0;
if ( !buffer && new_size ) return 0;
buffer_size = new_size + WRITE_OFFSET;
res = 1; res = 1;
skip_bits = 0; skip_bits = 0;
imp_phase = 0;
step = STEREO; step = STEREO;
ratio = 1.0; ratio = 1.0;
buffer = (sample_t *) realloc( buffer, (new_size + WRITE_OFFSET) * sizeof (sample_t) );
write_pos = 0;
if ( !buffer ) return 0;
buffer_size = new_size + WRITE_OFFSET;
Fir_Resampler_clear(); Fir_Resampler_clear();
return 1; return 1;
} }
@ -101,7 +104,7 @@ void Fir_Resampler_clear()
if ( buffer_size ) if ( buffer_size )
{ {
write_pos = &buffer [WRITE_OFFSET]; write_pos = &buffer [WRITE_OFFSET];
memset( buffer, 0, buffer_size * sizeof (sample_t) ); memset( buffer, 0, WRITE_OFFSET * sizeof (sample_t) );
} }
} }
@ -110,6 +113,7 @@ double Fir_Resampler_time_ratio( double new_factor )
ratio = new_factor; ratio = new_factor;
int i, r; int i, r;
double nearest, error;
double fstep = 0.0; double fstep = 0.0;
double least_error = 2; double least_error = 2;
double pos = 0.0; double pos = 0.0;
@ -118,8 +122,8 @@ double Fir_Resampler_time_ratio( double new_factor )
for ( r = 1; r <= MAX_RES; r++ ) for ( r = 1; r <= MAX_RES; r++ )
{ {
pos += ratio; pos += ratio;
double nearest = floor( pos + 0.5 ); nearest = floor( pos + 0.5 );
double error = fabs( pos - nearest ); error = fabs( pos - nearest );
if ( error < least_error ) if ( error < least_error )
{ {
res = r; res = r;
@ -138,6 +142,7 @@ double Fir_Resampler_time_ratio( double new_factor )
double filter = (ratio < 1.0) ? 1.0 : 1.0 / ratio; double filter = (ratio < 1.0) ? 1.0 : 1.0 / ratio;
pos = 0.0; pos = 0.0;
input_per_cycle = 0; input_per_cycle = 0;
for ( i = 0; i < res; i++ ) for ( i = 0; i < res; i++ )
{ {
gen_sinc( ROLLOFF, (int) (WIDTH * filter + 1) & ~1, pos, filter, gen_sinc( ROLLOFF, (int) (WIDTH * filter + 1) & ~1, pos, filter,
@ -195,7 +200,7 @@ void Fir_Resampler_write( long count )
assert( write_pos <= ( buffer + buffer_size ) ); assert( write_pos <= ( buffer + buffer_size ) );
} }
int Fir_Resampler_read( sample_t** out, unsigned long count ) int Fir_Resampler_read( sample_t** out, long count )
{ {
sample_t* out_l = out[0]; sample_t* out_l = out[0];
sample_t* out_r = out[1]; sample_t* out_r = out[1];
@ -207,13 +212,18 @@ int Fir_Resampler_read( sample_t** out, unsigned long count )
int n; int n;
int pt0,pt1; int pt0,pt1;
sample_t* i; sample_t* i;
unsigned long l,r; long l,r;
if ( end_pos - in >= WIDTH * STEREO ) if ( end_pos - in >= WIDTH * STEREO )
{ {
end_pos -= WIDTH * STEREO; end_pos -= WIDTH * STEREO;
do do
{ {
count--;
if ( count < 0 )
break;
/* accumulate in extended precision */ /* accumulate in extended precision */
l = 0; l = 0;
r = 0; r = 0;
@ -251,7 +261,7 @@ int Fir_Resampler_read( sample_t** out, unsigned long count )
*out_l++ = (sample_t) l; *out_l++ = (sample_t) l;
*out_r++ = (sample_t) r; *out_r++ = (sample_t) r;
} }
while ( (in <= end_pos) && (--count > 0) ); while ( in <= end_pos );
} }
imp_phase = res - remain; imp_phase = res - remain;
@ -263,9 +273,9 @@ int Fir_Resampler_read( sample_t** out, unsigned long count )
return out_l - out[0]; return out_l - out[0];
} }
int Fir_Resampler_input_needed( unsigned long output_count ) int Fir_Resampler_input_needed( long output_count )
{ {
unsigned long input_count = 0; long input_count = 0;
unsigned long skip = skip_bits >> imp_phase; unsigned long skip = skip_bits >> imp_phase;
int remain = res - imp_phase; int remain = res - imp_phase;

View File

@ -23,8 +23,8 @@ extern sample_t* Fir_Resampler_buffer( void );
extern int Fir_Resampler_written( void ); extern int Fir_Resampler_written( void );
extern int Fir_Resampler_avail( void ); extern int Fir_Resampler_avail( void );
extern void Fir_Resampler_write( long count ); extern void Fir_Resampler_write( long count );
extern int Fir_Resampler_read( sample_t** out, unsigned long count ); extern int Fir_Resampler_read( sample_t** out, long count );
extern int Fir_Resampler_input_needed( unsigned long output_count ); extern int Fir_Resampler_input_needed( long output_count );
extern int Fir_Resampler_skip_input( long count ); extern int Fir_Resampler_skip_input( long count );
#endif #endif

View File

@ -590,9 +590,9 @@ typedef struct
/* OPN/A/B common state */ /* OPN/A/B common state */
typedef struct typedef struct
{ {
FM_ST ST; /* general state */ FM_ST ST; /* general state */
FM_3SLOT SL3; /* 3 slot mode state */ FM_3SLOT SL3; /* 3 slot mode state */
unsigned int pan[6*2]; /* fm channels output masks (0xffffffff = enable) */ unsigned int pan[6*2]; /* fm channels output masks (0xffffffff = enable) */
UINT32 eg_cnt; /* global envelope generator counter */ UINT32 eg_cnt; /* global envelope generator counter */
UINT32 eg_timer; /* global envelope generator counter works at frequency = chipclock/144/3 */ UINT32 eg_timer; /* global envelope generator counter works at frequency = chipclock/144/3 */
@ -601,15 +601,15 @@ typedef struct
/* there are 2048 FNUMs that can be generated using FNUM/BLK registers /* there are 2048 FNUMs that can be generated using FNUM/BLK registers
but LFO works with one more bit of a precision so we really need 4096 elements */ but LFO works with one more bit of a precision so we really need 4096 elements */
UINT32 fn_table[4096]; /* fnumber->increment counter */ UINT32 fn_table[4096]; /* fnumber->increment counter */
UINT32 fn_max; /* max increment (required for calculating phase overflow) */ UINT32 fn_max; /* max increment (required for calculating phase overflow) */
/* LFO */ /* LFO */
UINT8 lfo_cnt; /* current LFO phase (out of 128) */ UINT8 lfo_cnt; /* current LFO phase (out of 128) */
UINT32 lfo_timer; /* current LFO phase runs at LFO frequency */ UINT32 lfo_timer; /* current LFO phase runs at LFO frequency */
UINT32 lfo_timer_add; /* step of lfo_timer */ UINT32 lfo_timer_add; /* step of lfo_timer */
UINT32 lfo_timer_overflow; /* LFO timer overflows every N samples (depends on LFO frequency) */ UINT32 lfo_timer_overflow; /* LFO timer overflows every N samples (depends on LFO frequency) */
UINT32 LFO_AM; /* current LFO AM step */ UINT32 LFO_AM; /* current LFO AM step */
UINT32 LFO_PM; /* current LFO PM step */ UINT32 LFO_PM; /* current LFO PM step */
} FM_OPN; } FM_OPN;
@ -1032,7 +1032,7 @@ INLINE void advance_lfo()
if (ym2612.OPN.lfo_timer_overflow) /* LFO enabled ? */ if (ym2612.OPN.lfo_timer_overflow) /* LFO enabled ? */
{ {
/* increment LFO timer */ /* increment LFO timer */
ym2612.OPN.lfo_timer += ym2612.OPN.lfo_timer_add; ym2612.OPN.lfo_timer += ym2612.OPN.lfo_timer_add;
/* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */ /* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */
while (ym2612.OPN.lfo_timer >= ym2612.OPN.lfo_timer_overflow) while (ym2612.OPN.lfo_timer >= ym2612.OPN.lfo_timer_overflow)
@ -1590,7 +1590,7 @@ INLINE void OPNWriteReg(int r, int v)
SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl; SLOT->vol_out = ((UINT32)(0x200 - SLOT->volume) & MAX_ATT_INDEX) + SLOT->tl;
else else
SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl; SLOT->vol_out = (UINT32)SLOT->volume + SLOT->tl;
} }
/* SSG-EG envelope shapes : /* SSG-EG envelope shapes :
@ -1828,13 +1828,13 @@ static void init_tables(void)
/* we never reach (1<<16) here due to the (x+1) */ /* we never reach (1<<16) here due to the (x+1) */
/* result fits within 16 bits at maximum */ /* result fits within 16 bits at maximum */
n = (int)m; /* 16 bits here */ n = (int)m; /* 16 bits here */
n >>= 4; /* 12 bits here */ n >>= 4; /* 12 bits here */
if (n&1) /* round to nearest */ if (n&1) /* round to nearest */
n = (n>>1)+1; n = (n>>1)+1;
else else
n = n>>1; n = n>>1;
/* 11 bits here (rounded) */ /* 11 bits here (rounded) */
n <<= 2; /* 13 bits here (as in real chip) */ n <<= 2; /* 13 bits here (as in real chip) */
/* 14 bits (with sign bit) */ /* 14 bits (with sign bit) */
@ -1889,7 +1889,7 @@ static void init_tables(void)
UINT32 offset_fnum_bit; UINT32 offset_fnum_bit;
UINT32 bit_tmp; UINT32 bit_tmp;
for (step=0; step<8; step++) for (step=0; step<8; step++)
{ {
value = 0; value = 0;
for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */ for (bit_tmp=0; bit_tmp<7; bit_tmp++) /* 7 bits */
@ -1908,7 +1908,7 @@ static void init_tables(void)
} }
} }
} }
} }
@ -1969,7 +1969,7 @@ int YM2612ResetChip(void)
/* a = address */ /* a = address */
/* v = value */ /* v = value */
void YM2612Write(unsigned int a, unsigned int v) void YM2612Write(unsigned int a, unsigned int v)
{ {
v &= 0xff; /* adjust to 8 bit bus */ v &= 0xff; /* adjust to 8 bit bus */
switch( a ) switch( a )
@ -1980,7 +1980,7 @@ void YM2612Write(unsigned int a, unsigned int v)
case 2: /* address port 1 */ case 2: /* address port 1 */
ym2612.OPN.ST.address = v | 0x100; ym2612.OPN.ST.address = v | 0x100;
break; break;
default: /* data port */ default: /* data port */
{ {
@ -1992,24 +1992,24 @@ void YM2612Write(unsigned int a, unsigned int v)
{ {
case 0x2a: /* DAC data (ym2612) */ case 0x2a: /* DAC data (ym2612) */
ym2612.dacout = ((int)v - 0x80) << 6; /* level unknown (5 is too low, 8 is too loud) */ ym2612.dacout = ((int)v - 0x80) << 6; /* level unknown (5 is too low, 8 is too loud) */
break; break;
case 0x2b: /* DAC Sel (ym2612) */ case 0x2b: /* DAC Sel (ym2612) */
/* b7 = dac enable */ /* b7 = dac enable */
ym2612.dacen = v & 0x80; ym2612.dacen = v & 0x80;
break; break;
default: /* OPN section */ default: /* OPN section */
/* write register */ /* write register */
OPNWriteMode(addr,v); OPNWriteMode(addr,v);
} }
break; break;
default: /* 0x30-0xff OPN section */ default: /* 0x30-0xff OPN section */
/* write register */ /* write register */
OPNWriteReg(addr,v); OPNWriteReg(addr,v);
} }
break; break;
} }
}
} }
}
unsigned int YM2612Read(void) unsigned int YM2612Read(void)
{ {

View File

@ -150,14 +150,17 @@ int audio_init (int rate, double fps)
if (!rate || ((rate < 8000) | (rate > 48000))) return (-1); if (!rate || ((rate < 8000) | (rate > 48000))) return (-1);
snd.sample_rate = rate; snd.sample_rate = rate;
/* Calculate the sound buffer size (for one frame) */
snd.buffer_size = (rate / vdp_rate) + 8;
#ifndef NGC #ifndef NGC
/* Calculate the sound buffer size (for one frame) */
snd.buffer_size = (rate / vdp_rate);
/* Output buffers */ /* Output buffers */
snd.buffer[0] = (int16 *) malloc(SND_SIZE); snd.buffer[0] = (int16 *) malloc(SND_SIZE);
snd.buffer[1] = (int16 *) malloc(SND_SIZE); snd.buffer[1] = (int16 *) malloc(SND_SIZE);
if (!snd.buffer[0] || !snd.buffer[1]) return (-1); if (!snd.buffer[0] || !snd.buffer[1]) return (-1);
#else
/* Calculate the sound buffer size (for one frame) */
snd.buffer_size = (rate / vdp_rate) + 32;
#endif #endif
/* SN76489 stream buffers */ /* SN76489 stream buffers */

View File

@ -218,8 +218,6 @@ void vdp_reset(void)
/* reset display area */ /* reset display area */
bitmap.viewport.w = 256; bitmap.viewport.w = 256;
bitmap.viewport.h = 224; bitmap.viewport.h = 224;
bitmap.viewport.oh = 256;
bitmap.viewport.ow = 224;
/* reset border area */ /* reset border area */
bitmap.viewport.x = config.overscan ? ((reg[12] & 1) ? 16 : 12) : 0; bitmap.viewport.x = config.overscan ? ((reg[12] & 1) ? 16 : 12) : 0;
@ -293,7 +291,7 @@ void vdp_update_dma()
int index = (4 * dma_type) + ((reg[12] & 1)*2); int index = (4 * dma_type) + ((reg[12] & 1)*2);
if ((status&8) || !(reg[1] & 0x40)) index++; if ((status&8) || !(reg[1] & 0x40)) index++;
/* DMA transfer rate */ /* DMA transfer rate (bytes per line) */
int rate = dma_rates[index]; int rate = dma_rates[index];
/* 68k cycles left */ /* 68k cycles left */
@ -412,7 +410,8 @@ void vdp_ctrl_w(unsigned int data)
seems to work fine (see Chaos Engine/Soldier of Fortune) seems to work fine (see Chaos Engine/Soldier of Fortune)
*/ */
fifo_latency = (reg[12] & 1) ? 27 : 30; fifo_latency = (reg[12] & 1) ? 27 : 30;
if ((code & 0x0F) == 0x01) fifo_latency = fifo_latency * 2; if ((code & 0x0F) == 0x01)
fifo_latency = fifo_latency * 2;
} }
/* /*
@ -444,17 +443,17 @@ unsigned int vdp_ctrl_r(void)
/* update DMA Busy flag */ /* update DMA Busy flag */
if ((status & 2) && !dma_length && (count_m68k >= dma_endCycles)) if ((status & 2) && !dma_length && (count_m68k >= dma_endCycles))
{
status &= 0xFFFD; status &= 0xFFFD;
}
unsigned int temp = status; unsigned int temp = status;
/* display OFF: VBLANK flag is set */ /* display OFF: VBLANK flag is set */
if (!(reg[1] & 0x40)) temp |= 0x8; if (!(reg[1] & 0x40))
temp |= 0x08;
/* HBLANK flag (Sonic 3 and Sonic 2 "VS Modes", Lemmings 2, Mega Turrican) */ /* HBLANK flag (Sonic 3 and Sonic 2 "VS Modes", Lemmings 2, Mega Turrican) */
if ((count_m68k % m68cycles_per_line) < 84) temp |= 0x4; if ((count_m68k % m68cycles_per_line) < 84)
temp |= 0x04;
/* clear pending flag */ /* clear pending flag */
pending = 0; pending = 0;
@ -522,7 +521,8 @@ void vdp_data_w(unsigned int data)
status |= 0x100; status |= 0x100;
/* VDP latency (Chaos Engine, Soldiers of Fortune, Double Clutch) */ /* VDP latency (Chaos Engine, Soldiers of Fortune, Double Clutch) */
if (fifo_write_cnt > 4) count_m68k = fifo_lastwrite + fifo_latency; if (fifo_write_cnt > 4)
count_m68k = fifo_lastwrite + fifo_latency;
} }
} }
@ -598,8 +598,10 @@ int vdp_int_ack_callback(int int_level)
/* update IRQ status */ /* update IRQ status */
irq_status = 0x10; irq_status = 0x10;
if (vint_pending && (reg[1] & 0x20)) irq_status |= 6; if (vint_pending && (reg[1] & 0x20))
else if (hint_pending && (reg[0] & 0x10)) irq_status |= 4; irq_status |= 6;
else if (hint_pending && (reg[0] & 0x10))
irq_status |= 4;
return M68K_INT_ACK_AUTOVECTOR; return M68K_INT_ACK_AUTOVECTOR;
} }