Make sound control deterministic by having proper reset

This commit is contained in:
David Guillen Fandos 2023-06-12 20:24:18 +02:00
parent 34eb7a3bf3
commit 7f07da9ade
5 changed files with 27 additions and 13 deletions

View File

@ -2447,7 +2447,19 @@ bool gamepak_must_swap(void)
void init_memory(void) void init_memory(void)
{ {
u32 map_offset = 0; u32 map_offset = 0, i;
for (i = 0; i < DMA_CHAN_CNT; i++)
{
dma[i].start_type = DMA_INACTIVE;
dma[i].irq = DMA_NO_IRQ;
dma[i].source_address = dma[i].dest_address = 0;
dma[i].source_direction = dma[i].dest_direction = 0;
dma[i].length = 0;
dma[i].length_type = DMA_16BIT;
dma[i].repeat_type = DMA_NO_REPEAT;
dma[i].direct_sound_channel = DMA_NO_DIRECT_SOUND;
}
// Fill memory map regions, areas marked as NULL must be checked directly // Fill memory map regions, areas marked as NULL must be checked directly
map_region(read, 0x0000000, 0x1000000, 1, bios_rom); map_region(read, 0x0000000, 0x1000000, 1, bios_rom);

View File

@ -566,7 +566,7 @@ void retro_init(void)
#endif #endif
init_gamepak_buffer(); init_gamepak_buffer();
init_sound(1); init_sound();
if(!gba_screen_pixels) if(!gba_screen_pixels)
#ifdef _3DS #ifdef _3DS

9
main.c
View File

@ -82,13 +82,14 @@ static unsigned update_timers(irq_type *irq_raised, unsigned completed_cycles)
void init_main(void) void init_main(void)
{ {
u32 i; u32 i;
for(i = 0; i < 4; i++) for(i = 0; i < 4; i++)
{ {
dma[i].start_type = DMA_INACTIVE;
dma[i].direct_sound_channel = DMA_NO_DIRECT_SOUND;
timer[i].status = TIMER_INACTIVE; timer[i].status = TIMER_INACTIVE;
timer[i].reload = 0x10000; timer[i].prescale = 0;
timer[i].irq = 0;
timer[i].reload = timer[i].count = 0x10000;
timer[i].direct_sound_channels = TIMER_DS_CHANNEL_NONE;
timer[i].frequency_step = 0;
} }
timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH; timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH;

12
sound.c
View File

@ -549,6 +549,7 @@ void reset_sound(void)
ds->fifo_top = 0; ds->fifo_top = 0;
ds->fifo_base = 0; ds->fifo_base = 0;
ds->fifo_fractional = 0; ds->fifo_fractional = 0;
ds->volume_halve = 1;
memset(ds->fifo, 0, 32); memset(ds->fifo, 0, 32);
} }
@ -561,6 +562,7 @@ void reset_sound(void)
gbc_sound_master_volume = 0; gbc_sound_master_volume = 0;
memset(wave_samples, 0, 64); memset(wave_samples, 0, 64);
memset(&gbc_sound_channel[0], 0, sizeof(gbc_sound_channel));
for(i = 0; i < 4; i++, gs++) for(i = 0; i < 4; i++, gs++)
{ {
gs->status = GBC_SOUND_INACTIVE; gs->status = GBC_SOUND_INACTIVE;
@ -569,7 +571,7 @@ void reset_sound(void)
} }
} }
void init_sound(int need_reset) void init_sound()
{ {
gbc_sound_tick_step = gbc_sound_tick_step =
float_to_fp16_16(256.0f / sound_frequency); float_to_fp16_16(256.0f / sound_frequency);
@ -577,8 +579,7 @@ void init_sound(int need_reset)
init_noise_table(noise_table15, 32767, 14); init_noise_table(noise_table15, 32767, 14);
init_noise_table(noise_table7, 127, 6); init_noise_table(noise_table7, 127, 6);
if (need_reset) reset_sound();
reset_sound();
} }
bool sound_read_savestate(const u8 *src) bool sound_read_savestate(const u8 *src)
@ -633,7 +634,6 @@ bool sound_read_savestate(const u8 *src)
bson_read_int32(sndchan, "env-vol", &gs->envelope_volume) && bson_read_int32(sndchan, "env-vol", &gs->envelope_volume) &&
bson_read_int32(sndchan, "env-dir", &gs->envelope_direction) && bson_read_int32(sndchan, "env-dir", &gs->envelope_direction) &&
bson_read_int32(sndchan, "env-status", &gs->envelope_status) && bson_read_int32(sndchan, "env-status", &gs->envelope_status) &&
bson_read_int32(sndchan, "env-step", &gs->envelope_step) &&
bson_read_int32(sndchan, "env-ticks0", &gs->envelope_initial_ticks) && bson_read_int32(sndchan, "env-ticks0", &gs->envelope_initial_ticks) &&
bson_read_int32(sndchan, "env-ticks", &gs->envelope_ticks) && bson_read_int32(sndchan, "env-ticks", &gs->envelope_ticks) &&
bson_read_int32(sndchan, "sweep-status", &gs->sweep_status) && bson_read_int32(sndchan, "sweep-status", &gs->sweep_status) &&
@ -705,7 +705,6 @@ unsigned sound_write_savestate(u8 *dst)
bson_write_int32(dst, "env-vol", gs->envelope_volume); bson_write_int32(dst, "env-vol", gs->envelope_volume);
bson_write_int32(dst, "env-dir", gs->envelope_direction); bson_write_int32(dst, "env-dir", gs->envelope_direction);
bson_write_int32(dst, "env-status", gs->envelope_status); bson_write_int32(dst, "env-status", gs->envelope_status);
bson_write_int32(dst, "env-step", gs->envelope_step);
bson_write_int32(dst, "env-ticks0", gs->envelope_initial_ticks); bson_write_int32(dst, "env-ticks0", gs->envelope_initial_ticks);
bson_write_int32(dst, "env-ticks", gs->envelope_ticks); bson_write_int32(dst, "env-ticks", gs->envelope_ticks);
bson_write_int32(dst, "sweep-status", gs->sweep_status); bson_write_int32(dst, "sweep-status", gs->sweep_status);
@ -721,6 +720,9 @@ unsigned sound_write_savestate(u8 *dst)
bson_write_int32(dst, "len-ticks", gs->length_ticks); bson_write_int32(dst, "len-ticks", gs->length_ticks);
bson_write_int32(dst, "noise-type", gs->noise_type); bson_write_int32(dst, "noise-type", gs->noise_type);
bson_write_int32(dst, "sample-tbl", gs->sample_table_idx); bson_write_int32(dst, "sample-tbl", gs->sample_table_idx);
// No longer used fields, keep for backwards compatibility.
bson_write_int32(dst, "env-step", 0);
bson_finish_document(dst, wbptr2); bson_finish_document(dst, wbptr2);
} }

View File

@ -66,7 +66,6 @@ typedef struct
u32 envelope_volume; u32 envelope_volume;
u32 envelope_direction; u32 envelope_direction;
u32 envelope_status; u32 envelope_status;
u32 envelope_step;
u32 envelope_ticks; u32 envelope_ticks;
u32 envelope_initial_ticks; u32 envelope_initial_ticks;
u32 sweep_status; u32 sweep_status;
@ -102,7 +101,7 @@ void sound_timer_queue32(u32 channel, u32 value);
unsigned sound_timer(fixed8_24 frequency_step, u32 channel); unsigned sound_timer(fixed8_24 frequency_step, u32 channel);
void sound_reset_fifo(u32 channel); void sound_reset_fifo(u32 channel);
void update_gbc_sound(u32 cpu_ticks); void update_gbc_sound(u32 cpu_ticks);
void init_sound(int need_reset); void init_sound();
unsigned sound_write_savestate(u8 *dst); unsigned sound_write_savestate(u8 *dst);
bool sound_read_savestate(const u8 *src); bool sound_read_savestate(const u8 *src);