From 9268b4ef42d880e33d33322650b7275295801f0c Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Tue, 15 Oct 2024 13:34:42 -0700 Subject: [PATCH] Only fill FIFO up to the amount of samples needed. --- backends/playback/zsm/x16emu/vera_pcm.c | 9 +++++++++ backends/playback/zsm/x16emu/vera_pcm.h | 17 +++++++++-------- backends/playback/zsm/zsm_backend.hpp | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/backends/playback/zsm/x16emu/vera_pcm.c b/backends/playback/zsm/x16emu/vera_pcm.c index 24b6212..8dbe1e2 100644 --- a/backends/playback/zsm/x16emu/vera_pcm.c +++ b/backends/playback/zsm/x16emu/vera_pcm.c @@ -180,3 +180,12 @@ pcm_render(int16_t *buf, unsigned num_samples) *(buf++) = (int16_t)((int32_t)cur_r * volume_lut[ctrl & 0xF] / 64); } } +uint32_t pcm_fifo_avail(void) { + switch ((ctrl >> 4) & 3) { + case 0: return fifo_cnt; + case 1: return fifo_cnt / 2; + case 2: return fifo_cnt / 2; + case 3: return fifo_cnt / 4; + } + return 0; +} diff --git a/backends/playback/zsm/x16emu/vera_pcm.h b/backends/playback/zsm/x16emu/vera_pcm.h index ffddcc7..79c3b30 100644 --- a/backends/playback/zsm/x16emu/vera_pcm.h +++ b/backends/playback/zsm/x16emu/vera_pcm.h @@ -7,11 +7,12 @@ #include #include -void pcm_reset(void); -void pcm_write_ctrl(uint8_t val); -uint8_t pcm_read_ctrl(void); -void pcm_write_rate(uint8_t val); -uint8_t pcm_read_rate(void); -void pcm_write_fifo(uint8_t val); -void pcm_render(int16_t *buf, unsigned num_samples); -bool pcm_is_fifo_almost_empty(void); +void pcm_reset(void); +void pcm_write_ctrl(uint8_t val); +uint8_t pcm_read_ctrl(void); +void pcm_write_rate(uint8_t val); +uint8_t pcm_read_rate(void); +void pcm_write_fifo(uint8_t val); +void pcm_render(int16_t *buf, unsigned num_samples); +uint32_t pcm_fifo_avail(void); +bool pcm_is_fifo_almost_empty(void); diff --git a/backends/playback/zsm/zsm_backend.hpp b/backends/playback/zsm/zsm_backend.hpp index 64148a0..f914d80 100644 --- a/backends/playback/zsm/zsm_backend.hpp +++ b/backends/playback/zsm/zsm_backend.hpp @@ -84,7 +84,7 @@ class ZsmBackend : public PlaybackBackend { return (int16_t)((((int32_t)a) + ((int32_t)b)) >> 1); } void audio_step(size_t samples) { - while (((pcm_read_ctrl() & 0x80) == 0) && remain > 0) { + while (((pcm_read_ctrl() & 0x80) == 0 || pcm_fifo_avail() < samples) && remain > 0) { remain--; size_t oldpos = file->get_pos(); file->seek((cur++) + pcm_data_offs, SeekType::SET);