Implement Sound Blaster DSP reset and command interface
authorRobert Pengelly <robertapengelly@hotmail.com>
Sat, 24 Oct 2026 09:48:42 +0000 (10:48 +0100)
committerRobert Pengelly <robertapengelly@hotmail.com>
Sat, 24 Oct 2026 09:48:42 +0000 (10:48 +0100)
cpu.c
cpu.h

diff --git a/cpu.c b/cpu.c
index 758744be8aaee6b9a76e6425d30ce0cbd6cd74ca..b03cf352c4e103931199395889b5d30e7bb6aefb 100644 (file)
--- a/cpu.c
+++ b/cpu.c
@@ -1487,6 +1487,19 @@ static void pic_write_data (PIC8259 *pic, uint8_t value) {
 
 }
 
+static void sb_dsp_reply (CPU8086 *cpu, uint8_t value) {
+
+    if (cpu->sb_dsp_reply_count < sizeof (cpu->sb_dsp_reply)) {
+    
+        unsigned tail = (cpu->sb_dsp_reply_head + cpu->sb_dsp_reply_count) % sizeof (cpu->sb_dsp_reply);
+        
+        cpu->sb_dsp_reply[tail] = value;
+        cpu->sb_dsp_reply_count++;
+    
+    }
+
+}
+
 static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) {
 
     switch (port) {
@@ -1541,6 +1554,33 @@ static uint8_t io_read8 (CPU8086 *cpu, uint16_t port) {
         
             return 0xFF;
         
+        case 0x226:                     /* Sound Blaster DSP reset (write-only) */
+        
+            return 0xFF;
+        
+        case 0x22A:                     /* Sound Blaster DSP read data */
+        
+            if (cpu->sb_dsp_reply_count) {
+            
+                uint8_t result = cpu->sb_dsp_reply[cpu->sb_dsp_reply_head];
+                
+                cpu->sb_dsp_reply_head = (uint8_t) ((cpu->sb_dsp_reply_head + 1) % sizeof (cpu->sb_dsp_reply));
+                cpu->sb_dsp_reply_count--;
+                
+                return result;
+            
+            }
+            
+            return 0xFF;
+        
+        case 0x22C:                     /* Sound Blaster DSP write-buffer status */
+        
+            return 0x7F;                /* DSP command port ready */
+        
+        case 0x22E:                     /* Sound Blaster DSP read-buffer status */
+        
+            return cpu->sb_dsp_reply_count ? 0x80 : 0x00;
+        
         case 0x388:     case 0x389:     /* AdLib OPL2: not installed */
         
             return 0xFF;
@@ -1709,6 +1749,59 @@ static void io_write8 (CPU8086 *cpu, uint16_t port, uint8_t value) {
             /* No joysticks are connected, so there are no timers to reset. */
             break;
         
+        case 0x226:                     /* Sound Blaster DSP reset */
+        
+            if (value & 1) {
+            
+                cpu->sb_dsp_reset = 1;
+                cpu->sb_dsp_reply_head = 0;
+                cpu->sb_dsp_reply_count = 0;
+                cpu->sb_dsp_speaker = 0;
+            
+            } else if (cpu->sb_dsp_reset) {
+            
+                cpu->sb_dsp_reset = 0;
+                sb_dsp_reply (cpu, 0xAA);
+            
+            }
+            
+            break;
+        
+        case 0x22C:                     /* Sound Blaster DSP command */
+        
+            switch (value) {
+            
+                case 0xD1:              /* Enable DSP speaker */
+                
+                    cpu->sb_dsp_speaker = 1;
+                    break;
+                
+                case 0xD3:              /* Disable DSP speaker */
+                
+                    cpu->sb_dsp_speaker = 0;
+                    break;
+                
+                case 0xD8:              /* Read DSP speaker status */
+                
+                    sb_dsp_reply (cpu, cpu->sb_dsp_speaker ? 0xFF : 0x00);
+                    break;
+                
+                case 0xE1:              /* Get DSP version: Sound Blaster 2.01 */
+                
+                    sb_dsp_reply (cpu, 2);
+                    sb_dsp_reply (cpu, 1);
+                    
+                    break;
+                
+                default:
+                
+                    fprintf (stderr, "unhandled Sound Blaster DSP command %02X at %04X:%08X\n", value, cpu->cs, cpu->ip);
+                    exit (1);
+            
+            }
+            
+            break;
+        
         case 0x388:     case 0x389:     /* AdLib OPL2: no device installed */
         
             /* Writes to an unpopulated I/O port have no effect. */
diff --git a/cpu.h b/cpu.h
index a3394cf84f3148053420b96c60faf7c79e7343a3..ef7323008d39e8a64388592b3432189f58c722a1 100644 (file)
--- a/cpu.h
+++ b/cpu.h
@@ -89,6 +89,9 @@ typedef struct __attribute__ ((packed)) CPU8086 {
     uint8_t cmos_index, cmos_regs[128];
     uint8_t port61, port92, a20_enabled, vga_status_flip;
     
+    uint8_t sb_dsp_reset, sb_dsp_speaker;
+    uint8_t sb_dsp_reply[4], sb_dsp_reply_head, sb_dsp_reply_count;
+    
     PITCounter pit[3];
     PIC8259 pic[2];