From 5fe5121c8181b7ba3d8bd70501210800c82f15fd Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Tue, 27 Jul 2021 21:45:15 +0200 Subject: [PATCH] Remove pointer from sound struct, use index instead This makes serialization possible, previously it was broken. --- gba_memory.c | 5 ++--- sound.c | 6 +++--- sound.h | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/gba_memory.c b/gba_memory.c index e8aba58..75f7426 100644 --- a/gba_memory.c +++ b/gba_memory.c @@ -25,8 +25,7 @@ u32 initial_volume = (value >> 12) & 0x0F; \ u32 envelope_ticks = ((value >> 8) & 0x07) * 4; \ gbc_sound_channel[channel].length_ticks = 64 - (value & 0x3F); \ - gbc_sound_channel[channel].sample_data = \ - square_pattern_duty[(value >> 6) & 0x03]; \ + gbc_sound_channel[channel].sample_table_idx = ((value >> 6) & 0x03); \ gbc_sound_channel[channel].envelope_direction = (value >> 11) & 0x01; \ gbc_sound_channel[channel].envelope_initial_volume = initial_volume; \ gbc_sound_channel[channel].envelope_volume = initial_volume; \ @@ -3183,7 +3182,7 @@ void gba_load_state(const void* src) // Oops, these contain raw pointers for(i = 0; i < 4; i++) - gbc_sound_channel[i].sample_data = square_pattern_duty[2]; + gbc_sound_channel[i].sample_table_idx = 2; instruction_count = 0; diff --git a/sound.c b/sound.c index 3ee90cb..5a872a4 100644 --- a/sound.c +++ b/sound.c @@ -456,7 +456,7 @@ void update_gbc_sound(u32 cpu_ticks) if(gs->active_flag) { sound_status |= 0x01; - sample_data = gs->sample_data; + sample_data = &square_pattern_duty[gs->sample_table_idx][0]; envelope_volume = gs->envelope_volume; gbc_sound_render_channel(samples, 8, envelope, sweep); } @@ -465,7 +465,7 @@ void update_gbc_sound(u32 cpu_ticks) if(gs->active_flag) { sound_status |= 0x02; - sample_data = gs->sample_data; + sample_data = &square_pattern_duty[gs->sample_table_idx][0]; envelope_volume = gs->envelope_volume; gbc_sound_render_channel(samples, 8, envelope, nosweep); } @@ -594,7 +594,7 @@ void reset_sound(void) for(i = 0; i < 4; i++, gs++) { gs->status = GBC_SOUND_INACTIVE; - gs->sample_data = square_pattern_duty[2]; + gs->sample_table_idx = 2; gs->active_flag = 0; } } diff --git a/sound.h b/sound.h index 466ffc1..08d1de5 100644 --- a/sound.h +++ b/sound.h @@ -92,7 +92,7 @@ typedef struct gbc_sound_status_type status; u32 active_flag; u32 master_enable; - const s8* sample_data; + u32 sample_table_idx; } gbc_sound_struct; const extern s8 square_pattern_duty[4][8];