Skip to contentSkip to conversation

hw/audio/sb16: reduce the CPU load

PATCH In review Volker Rümelin 3 participants 7 messages Jul 7, 2025 – Jul 8, 2025

lore
Share & tools
lore
#0 lore raw

cover hw/audio/sb16: reduce the CPU load

Three patches for the Sound Blaster 16 emulation.

Patch 2/3 "improve DMA handling" reduces the CPU load and fixes issue #469 at https://gitlab.com/qemu-project/qemu/-/issues/469.

Don't forget to add the AdLib device to the QEMU command line if you want to test the patches. The OPL2 chip is part of the Sound Blaster 16 emulation. The real Sound Blaster 16 used an OPL3 chip.

Volker Rümelin (3):
  hw/audio/sb16: introduce DMA helper functions hw/audio/sb16: improve DMA handling hw/audio/sb16: block structure coding style fixes

 hw/audio/sb16.c | 105 ++++++++++++++++++++++++++++--------------------

 1 file changed, 61 insertions(+), 44 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 hw/audio/sb16: introduce DMA helper functions

Introduce the hold_DREQ() and release_DREQ() functions to ease the review of the next patch.

---
 hw/audio/sb16.c | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)
hw/audio/sb16.c +21 -6
index bac64118fe..5c51940596 100644
@@ -154,6 +154,22 @@ static int irq_of_magic (int magic)
     }
 }
 
+static void hold_DREQ(SB16State *s, int nchan)
+{
+    IsaDma *isa_dma = nchan == s->dma ? s->isa_dma : s->isa_hdma;
+    IsaDmaClass *k = ISADMA_GET_CLASS(isa_dma);
+
+    k->hold_DREQ(isa_dma, nchan);
+}
+
+static void release_DREQ(SB16State *s, int nchan)
+{
+    IsaDma *isa_dma = nchan == s->dma ? s->isa_dma : s->isa_hdma;
+    IsaDmaClass *k = ISADMA_GET_CLASS(isa_dma);
+
+    k->release_DREQ(isa_dma, nchan);
+}
+
 #if 0
 static void log_dsp (SB16State *dsp)
 {
@@ -177,19 +193,18 @@ static void speaker (SB16State *s, int on)
 
 static void control (SB16State *s, int hold)
 {
-    int dma = s->use_hdma ? s->hdma : s->dma;
-    IsaDma *isa_dma = s->use_hdma ? s->isa_hdma : s->isa_dma;
-    IsaDmaClass *k = ISADMA_GET_CLASS(isa_dma);
+    int nchan = s->use_hdma ? s->hdma : s->dma;
+
     s->dma_running = hold;
 
-    ldebug ("hold %d high %d dma %d\n", hold, s->use_hdma, dma);
+    ldebug("hold %d high %d nchan %d\n", hold, s->use_hdma, nchan);
 
     if (hold) {
-        k->hold_DREQ(isa_dma, dma);
+        hold_DREQ(s, nchan);
         AUD_set_active_out (s->voice, 1);
     }
     else {
-        k->release_DREQ(isa_dma, dma);
+        release_DREQ(s, nchan);
         AUD_set_active_out (s->voice, 0);
     }
 }
Signature · 1 line
2.43.0
#3 lore raw

PATCH 2/3 hw/audio/sb16: improve DMA handling

In reply to Volker Rümelin

The Soundblaster 16 audio device is strange and works differently compared to the other audio frontends. For audio playback, a BH routine copies the audio samples from the DMA buffer to the audio backend. The BH routine then immediately reschedules itself. As far fewer samples are required from the backend than the BH routine can supply, this means that most of the BH routine calls don't copy any audio samples. The buffers of the audio backend are simply full. This is a very inefficient design.

Use the DREQ signal of the ISA DMA controller to throttle the audio stream. The callback routine of the audio subsystem raises the DREQ signal as soon as new samples are needed and also starts the DMA callback routine.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/469

---
 hw/audio/sb16.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
hw/audio/sb16.c +21 -2
index 5c51940596..714144eb61 100644
@@ -200,7 +200,9 @@ static void control (SB16State *s, int hold)
     ldebug("hold %d high %d nchan %d\n", hold, s->use_hdma, nchan);
 
     if (hold) {
-        hold_DREQ(s, nchan);
+        if (!s->voice) {
+            hold_DREQ(s, nchan);
+        }
         AUD_set_active_out (s->voice, 1);
     }
     else {
@@ -890,6 +892,8 @@ static void legacy_reset (SB16State *s)
     s->fmt_bits = 8;
     s->fmt_stereo = 0;
 
+    s->audio_free = 0;
+
     as.freq = s->freq;
     as.nchannels = 1;
     as.fmt = AUDIO_FORMAT_U8;
@@ -1243,8 +1247,12 @@ static int SB_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
     }
 
     if (s->voice) {
+        if (!dma_len) {
+            return dma_pos;
+        }
         free = s->audio_free & ~s->align;
-        if ((free <= 0) || !dma_len) {
+        if (free <= 0) {
+            release_DREQ(s, nchan);
             return dma_pos;
         }
     }
@@ -1269,6 +1277,7 @@ static int SB_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
     written = write_audio (s, nchan, dma_pos, dma_len, copy);
     dma_pos = (dma_pos + written) % dma_len;
     s->left_till_irq -= written;
+    s->audio_free -= written;
 
     if (s->left_till_irq <= 0) {
         s->mixer_regs[0x82] |= (nchan & 4) ? 2 : 1;
@@ -1289,13 +1298,23 @@ static int SB_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
         s->left_till_irq = s->block_size + s->left_till_irq;
     }
 
+    if (s->voice) {
+        free = s->audio_free & ~s->align;
+        if (free <= 0) {
+            release_DREQ(s, nchan);
+        }
+    }
     return dma_pos;
 }
 
 static void SB_audio_callback (void *opaque, int free)
 {
     SB16State *s = opaque;
+    int nchan = s->use_hdma ? s->hdma : s->dma;
+
     s->audio_free = free;
+    /* run the DMA engine to call SB_read_DMA immediately */
+    hold_DREQ(s, nchan);
 }
 
 static int sb16_post_load (void *opaque, int version_id)
Signature · 1 line
2.43.0
#4 lore raw

PATCH 3/3 hw/audio/sb16: block structure coding style fixes

In reply to Volker Rümelin

Change the block structure according to the QEMU Coding Style documentation.

---
 hw/audio/sb16.c | 57 +++++++++++++++++--------------------------------
 1 file changed, 20 insertions(+), 37 deletions(-)
hw/audio/sb16.c +20 -37
index 714144eb61..e23d86674e 100644
@@ -204,8 +204,7 @@ static void control (SB16State *s, int hold)
             hold_DREQ(s, nchan);
         }
         AUD_set_active_out (s->voice, 1);
-    }
-    else {
+    } else {
         release_DREQ(s, nchan);
         AUD_set_active_out (s->voice, 0);
     }
@@ -271,10 +270,10 @@ static void dma_cmd8 (SB16State *s, int mask, int dma_len)
     s->fmt_signed = 0;
     s->fmt_stereo = (s->mixer_regs[0x0e] & 2) != 0;
     if (-1 == s->time_const) {
-        if (s->freq <= 0)
+        if (s->freq <= 0) {
             s->freq = 11025;
-    }
-    else {
+        }
+    } else {
         int tmp = (256 - s->time_const);
         s->freq = (1000000 + (tmp / 2)) / tmp;
     }
@@ -282,8 +281,7 @@ static void dma_cmd8 (SB16State *s, int mask, int dma_len)
 
     if (dma_len != -1) {
         s->block_size = dma_len << s->fmt_stereo;
-    }
-    else {
+    } else {
         /* This is apparently the only way to make both Act1/PL
            and SecondReality/FC work
 
@@ -362,16 +360,13 @@ static void dma_cmd (SB16State *s, uint8_t cmd, uint8_t d0, int dma_len)
     if (16 == s->fmt_bits) {
         if (s->fmt_signed) {
             s->fmt = AUDIO_FORMAT_S16;
-        }
-        else {
+        } else {
             s->fmt = AUDIO_FORMAT_U16;
         }
-    }
-    else {
+    } else {
         if (s->fmt_signed) {
             s->fmt = AUDIO_FORMAT_S8;
-        }
-        else {
+        } else {
             s->fmt = AUDIO_FORMAT_U8;
         }
     }
@@ -422,8 +417,7 @@ static inline uint8_t dsp_get_data (SB16State *s)
 {
     if (s->in_index) {
         return s->in2_data[--s->in_index];
-    }
-    else {
+    } else {
         dolog ("buffer underflow\n");
         return 0;
     }
@@ -447,8 +441,7 @@ static void command (SB16State *s, uint8_t cmd)
             qemu_log_mask(LOG_GUEST_ERROR, "%#x wrong bits\n", cmd);
         }
         s->needed_bytes = 3;
-    }
-    else {
+    } else {
         s->needed_bytes = 0;
 
         switch (cmd) {
@@ -674,8 +667,7 @@ static void command (SB16State *s, uint8_t cmd)
  exit:
     if (!s->needed_bytes) {
         s->cmd = -1;
-    }
-    else {
+    } else {
         s->cmd = cmd;
     }
     return;
@@ -715,14 +707,12 @@ static void complete (SB16State *s)
         if (s->cmd & 8) {
             dolog ("ADC params cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
                    s->cmd, d0, d1, d2);
-        }
-        else {
+        } else {
             ldebug ("cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
                     s->cmd, d0, d1, d2);
             dma_cmd (s, s->cmd, d0, d1 + (d2 << 8));
         }
-    }
-    else {
+    } else {
         switch (s->cmd) {
         case 0x04:
             s->csp_mode = dsp_get_data (s);
@@ -747,8 +737,7 @@ static void complete (SB16State *s)
                 ldebug ("0x83[%d] <- %#x\n", s->csp_reg83r, d0);
                 s->csp_reg83[s->csp_reg83r % 4] = d0;
                 s->csp_reg83r += 1;
-            }
-            else {
+            } else {
                 s->csp_regs[d1] = d0;
             }
             break;
@@ -763,8 +752,7 @@ static void complete (SB16State *s)
                         s->csp_reg83[s->csp_reg83w % 4]);
                 dsp_out_data (s, s->csp_reg83[s->csp_reg83w % 4]);
                 s->csp_reg83w += 1;
-            }
-            else {
+            } else {
                 dsp_out_data (s, s->csp_regs[d0]);
             }
             break;
@@ -819,8 +807,7 @@ static void complete (SB16State *s)
                 ticks = muldiv64(bytes, NANOSECONDS_PER_SECOND, freq);
                 if (ticks < NANOSECONDS_PER_SECOND / 1024) {
                     qemu_irq_raise (s->pic);
-                }
-                else {
+                } else {
                     if (s->aux_ts) {
                         timer_mod (
                             s->aux_ts,
@@ -992,12 +979,10 @@ static void dsp_write(void *opaque, uint32_t nport, uint32_t val)
                 log_dsp (s);
             }
 #endif
-        }
-        else {
+        } else {
             if (s->in_index == sizeof (s->in2_data)) {
                 dolog ("in data overrun\n");
-            }
-            else {
+            } else {
                 s->in2_data[s->in_index++] = val;
                 if (s->in_index == s->needed_bytes) {
                     s->needed_bytes = 0;
@@ -1032,8 +1017,7 @@ static uint32_t dsp_read(void *opaque, uint32_t nport)
         if (s->out_data_len) {
             retval = s->out_data[--s->out_data_len];
             s->last_read_byte = retval;
-        }
-        else {
+        } else {
             if (s->cmd != -1) {
                 dolog ("empty output buffer for command %#x\n",
                        s->cmd);
@@ -1255,8 +1239,7 @@ static int SB_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
             release_DREQ(s, nchan);
             return dma_pos;
         }
-    }
-    else {
+    } else {
         free = dma_len;
     }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH 3/3] hw/audio/sb16: block structure coding style fixes

Quoted 180 lines · Volker Rümelin

Change the block structure according to the QEMU Coding Style documentation.

---
 hw/audio/sb16.c | 57 +++++++++++++++++--------------------------------
 1 file changed, 20 insertions(+), 37 deletions(-)
hw/audio/sb16.c +20 -38
index 714144eb61..e23d86674e 100644
@@ -204,8 +204,7 @@ static void control (SB16State *s, int hold)
             hold_DREQ(s, nchan);
         }
         AUD_set_active_out (s->voice, 1);
-    }
-    else {
+    } else {
         release_DREQ(s, nchan);
         AUD_set_active_out (s->voice, 0);
     }
@@ -271,10 +270,10 @@ static void dma_cmd8 (SB16State *s, int mask, int dma_len)
     s->fmt_signed = 0;
     s->fmt_stereo = (s->mixer_regs[0x0e] & 2) != 0;
     if (-1 == s->time_const) {
-        if (s->freq <= 0)
+        if (s->freq <= 0) {
             s->freq = 11025;
-    }
-    else {
+        }
+    } else {
         int tmp = (256 - s->time_const);
         s->freq = (1000000 + (tmp / 2)) / tmp;
     }
@@ -282,8 +281,7 @@ static void dma_cmd8 (SB16State *s, int mask, int dma_len)
 
     if (dma_len != -1) {
         s->block_size = dma_len << s->fmt_stereo;
-    }
-    else {
+    } else {
         /* This is apparently the only way to make both Act1/PL
            and SecondReality/FC work
 
@@ -362,16 +360,13 @@ static void dma_cmd (SB16State *s, uint8_t cmd, uint8_t d0, int dma_len)
     if (16 == s->fmt_bits) {
         if (s->fmt_signed) {
             s->fmt = AUDIO_FORMAT_S16;
-        }
-        else {
+        } else {
             s->fmt = AUDIO_FORMAT_U16;
         }
-    }
-    else {
+    } else {
         if (s->fmt_signed) {
             s->fmt = AUDIO_FORMAT_S8;
-        }
-        else {
+        } else {
             s->fmt = AUDIO_FORMAT_U8;
         }
     }
@@ -422,8 +417,7 @@ static inline uint8_t dsp_get_data (SB16State *s)
 {
     if (s->in_index) {
         return s->in2_data[--s->in_index];
-    }
-    else {
+    } else {
         dolog ("buffer underflow\n");
         return 0;
     }
@@ -447,8 +441,7 @@ static void command (SB16State *s, uint8_t cmd)
             qemu_log_mask(LOG_GUEST_ERROR, "%#x wrong bits\n", cmd);
         }
         s->needed_bytes = 3;
-    }
-    else {
+    } else {
         s->needed_bytes = 0;
 
         switch (cmd) {
@@ -674,8 +667,7 @@ static void command (SB16State *s, uint8_t cmd)
  exit:
     if (!s->needed_bytes) {
         s->cmd = -1;
-    }
-    else {
+    } else {
         s->cmd = cmd;
     }
     return;
@@ -715,14 +707,12 @@ static void complete (SB16State *s)
         if (s->cmd & 8) {
             dolog ("ADC params cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
                    s->cmd, d0, d1, d2);
-        }
-        else {
+        } else {
             ldebug ("cmd = %#x d0 = %d, d1 = %d, d2 = %d\n",
                     s->cmd, d0, d1, d2);
             dma_cmd (s, s->cmd, d0, d1 + (d2 << 8));
         }
-    }
-    else {
+    } else {
         switch (s->cmd) {
         case 0x04:
             s->csp_mode = dsp_get_data (s);
@@ -747,8 +737,7 @@ static void complete (SB16State *s)
                 ldebug ("0x83[%d] <- %#x\n", s->csp_reg83r, d0);
                 s->csp_reg83[s->csp_reg83r % 4] = d0;
                 s->csp_reg83r += 1;
-            }
-            else {
+            } else {
                 s->csp_regs[d1] = d0;
             }
             break;
@@ -763,8 +752,7 @@ static void complete (SB16State *s)
                         s->csp_reg83[s->csp_reg83w % 4]);
                 dsp_out_data (s, s->csp_reg83[s->csp_reg83w % 4]);
                 s->csp_reg83w += 1;
-            }
-            else {
+            } else {
                 dsp_out_data (s, s->csp_regs[d0]);
             }
             break;
@@ -819,8 +807,7 @@ static void complete (SB16State *s)
                 ticks = muldiv64(bytes, NANOSECONDS_PER_SECOND, freq);
                 if (ticks < NANOSECONDS_PER_SECOND / 1024) {
                     qemu_irq_raise (s->pic);
-                }
-                else {
+                } else {
                     if (s->aux_ts) {
                         timer_mod (
                             s->aux_ts,
@@ -992,12 +979,10 @@ static void dsp_write(void *opaque, uint32_t nport, uint32_t val)
                 log_dsp (s);
             }
 #endif
-        }
-        else {
+        } else {
             if (s->in_index == sizeof (s->in2_data)) {
                 dolog ("in data overrun\n");
-            }
-            else {
+            } else {
                 s->in2_data[s->in_index++] = val;
                 if (s->in_index == s->needed_bytes) {
                     s->needed_bytes = 0;
@@ -1032,8 +1017,7 @@ static uint32_t dsp_read(void *opaque, uint32_t nport)
         if (s->out_data_len) {
             retval = s->out_data[--s->out_data_len];
             s->last_read_byte = retval;
-        }
-        else {
+        } else {
             if (s->cmd != -1) {
                 dolog ("empty output buffer for command %#x\n",
                        s->cmd);
@@ -1255,8 +1239,7 @@ static int SB_read_DMA (void *opaque, int nchan, int dma_pos, int dma_len)
             release_DREQ(s, nchan);
             return dma_pos;
         }
-    }
-    else {
+    } else {
         free = dma_len;
     }
 
--
Quoted 3 lines

2.43.0

Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] hw/audio/sb16: block structure coding style fixes

In reply to Volker Rümelin
Quoted 7 lines · Volker Rümelin

Change the block structure according to the QEMU Coding Style documentation.

---
  hw/audio/sb16.c | 57 +++++++++++++++++--------------------------------
  1 file changed, 20 insertions(+), 37 deletions(-)