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

@ -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;
} }