DMA8237 *dma = &cpu->dma[0];
- uint64_t now;
- unsigned bytes;
+ uint8_t samples[4096];
+ uint64_t now, period;
+
+ unsigned bytes, transferred = 0;
if (!cpu->sb_dsp_dma_active || cpu->sb_dsp_dma_paused || !cpu->sb_dsp_block_length || (dma->mask & 2) || (dma->mode[1] & 0x0F) != 0x09) {
return;
}
now = pit_clock_ns ();
+ period = (uint64_t) (256 - cpu->sb_dsp_time_constant) * 1000ULL;
- if (now < cpu->sb_dsp_next_block_ns) {
+ if (now < cpu->sb_dsp_next_sample_ns) {
return;
}
- bytes = cpu->sb_dsp_block_length;
+ bytes = (unsigned) ((now - cpu->sb_dsp_next_sample_ns) / period + 1);
- while (bytes) {
+ if (bytes > sizeof (samples)) {
- unsigned remaining = (unsigned) dma->count[1] + 1;
- unsigned transferred = bytes < remaining ? bytes : remaining;
+ bytes = sizeof (samples);
+ cpu->sb_dsp_next_sample_ns = now - (bytes - 1) * period;
+
+ }
+
+ while (transferred < bytes) {
+
+ uint32_t addr = ((uint32_t) dma->page[1] << 16) | dma->address[1];
+ samples[transferred++] = addr < cpu->mem_size ? cpu->mem[addr] : 0x80;
if (dma->mode[1] & 0x20) {
- dma->address[1] = (uint16_t) (dma->address[1] - transferred);
+ dma->address[1]--;
} else {
- dma->address[1] = (uint16_t) (dma->address[1] + transferred);
+ dma->address[1]++;
}
- dma->count[1] = (uint16_t) (dma->count[1] - transferred);
- bytes -= transferred;
-
- if (transferred == remaining) {
+ if (dma->count[1]-- == 0) {
if (dma->mode[1] & 0x10) {
} else {
dma->mask |= 2;
-
cpu->sb_dsp_dma_active = 0;
- break;
}
}
+
+ if (++cpu->sb_dsp_block_progress == cpu->sb_dsp_block_length) {
+
+ cpu->sb_dsp_block_progress = 0;
+ cpu->sb_dsp_irq_pending = 1;
+ cpu->pic[0].irr |= 0x80;
+
+ if (!cpu->sb_dsp_dma_auto) {
+ cpu->sb_dsp_dma_active = 0;
+ }
+
+ }
+
+ if (!cpu->sb_dsp_dma_active || (dma->mask & 2)) {
+ break;
+ }
}
- cpu->sb_dsp_irq_pending = 1;
- cpu->pic[0].irr |= 0x80;
+ cpu->sb_dsp_next_sample_ns += (uint64_t) transferred * period;
- if (!cpu->sb_dsp_dma_auto) {
- cpu->sb_dsp_dma_active = 0;
+ if (cpu->sb_dsp_speaker) {
+ speaker_sb_write (samples, transferred, (unsigned) (1000000ULL / (256 - cpu->sb_dsp_time_constant)));
}
-
- cpu->sb_dsp_next_block_ns = now + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
+
+}
+
+static void sb_dma_start (CPU8086 *cpu, int auto_init) {
+
+ cpu->sb_dsp_dma_active = 1;
+ cpu->sb_dsp_dma_auto = auto_init;
+ cpu->sb_dsp_dma_paused = 0;
+ cpu->sb_dsp_block_progress = 0;
+ cpu->sb_dsp_next_sample_ns = pit_clock_ns () + (uint64_t) (256 - cpu->sb_dsp_time_constant) * 1000ULL;
}
cpu->sb_dsp_dma_paused = 0;
cpu->sb_dsp_irq_pending = 0;
cpu->sb_dsp_param_count = 0;
+ cpu->sb_dsp_block_progress = 0;
+
+ speaker_sb_reset ();
} else if (cpu->sb_dsp_reset) {
} else if (cpu->sb_dsp_command == 0x14) {
cpu->sb_dsp_block_length = (uint32_t) (cpu->sb_dsp_params[0] | ((uint16_t) cpu->sb_dsp_params[1] << 8)) + 1;
- cpu->sb_dsp_dma_active = 1;
- cpu->sb_dsp_dma_auto = 0;
- cpu->sb_dsp_dma_paused = 0;
- cpu->sb_dsp_next_block_ns = pit_clock_ns () + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
+ sb_dma_start (cpu, 0);
}
case 0x1C: /* Auto-initialize 8-bit DMA output */
- cpu->sb_dsp_dma_active = 1;
- cpu->sb_dsp_dma_auto = 1;
- cpu->sb_dsp_dma_paused = 0;
- cpu->sb_dsp_next_block_ns = pit_clock_ns () + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
-
+ sb_dma_start (cpu, 1);
break;
case 0xD0: /* Halt 8-bit DMA */
case 0xD4: /* Continue 8-bit DMA */
cpu->sb_dsp_dma_paused = 0;
- cpu->sb_dsp_next_block_ns = pit_clock_ns () + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
+ cpu->sb_dsp_next_sample_ns = pit_clock_ns () + (uint64_t) (256 - cpu->sb_dsp_time_constant) * 1000ULL;
break;
case 0xD8: /* Read DSP speaker status */
#define SPEAKER_RATE 48000
#define SPEAKER_VOLUME 4096
-#if defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
+#define SB_QUEUE_SAMPLES 65536
+#if defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
typedef struct Speaker {
double elapsed;
static Speaker speaker = { 0, 65536, 0, 0 };
+static int16_t sb_samples[SB_QUEUE_SAMPLES];
+static unsigned sb_head, sb_tail, sb_count, sb_phase;
+
static void speaker_render (int16_t *samples, int count) {
double step = PIT_FREQUENCY / SPEAKER_RATE;
}
- samples[i] = enabled ? (int16_t) (output ? SPEAKER_VOLUME : -SPEAKER_VOLUME) : 0;
+ {
+
+ int mixed = enabled ? (output ? SPEAKER_VOLUME : -SPEAKER_VOLUME) : 0;
+
+ if (sb_count) {
+
+ mixed += sb_samples[sb_tail];
+
+ sb_tail = (sb_tail + 1) % SB_QUEUE_SAMPLES;
+ sb_count--;
+
+ }
+
+ if (mixed > 32767) {
+ mixed = 32767;
+ }
+
+ if (mixed < -32768) {
+ mixed = -32768;
+ }
+
+ samples[i] = (int16_t) mixed;
+
+ }
if (speaker.port61 & 1) {
#endif
}
+
+void speaker_sb_write (const uint8_t *samples, size_t count, unsigned rate) {
+
+#if defined (_WIN32)
+ if (!speaker_device || !rate) {
+ return;
+ }
+
+ EnterCriticalSection (&speaker_lock);
+#elif defined (__APPLE__)
+ if (!speaker_queue || !rate) {
+ return;
+ }
+
+ pthread_mutex_lock (&speaker_lock);
+#elif defined (__linux__)
+ if (!speaker_running || !rate) {
+ return;
+ }
+
+ pthread_mutex_lock (&speaker_lock);
+#else
+ (void) samples;
+ (void) count;
+ (void) rate;
+
+ return;
+#endif
+
+#if defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
+ for (size_t i = 0; i < count; i++) {
+
+ int16_t sample = (int16_t) (((int) samples[i] - 128) * 128);
+ sb_phase += SPEAKER_RATE;
+
+ while (sb_phase >= rate) {
+
+ sb_phase -= rate;
+
+ if (sb_count < SB_QUEUE_SAMPLES) {
+
+ sb_samples[sb_head] = sample;
+
+ sb_head = (sb_head + 1) % SB_QUEUE_SAMPLES;
+ sb_count++;
+
+ }
+
+ }
+
+ }
+#endif
+
+#if defined (_WIN32)
+ LeaveCriticalSection (&speaker_lock);
+#elif defined (__APPLE__) || defined (__linux__)
+ pthread_mutex_unlock (&speaker_lock);
+#endif
+
+}
+
+void speaker_sb_reset (void) {
+
+#if defined (_WIN32)
+ if (!speaker_device) {
+ return;
+ }
+
+ EnterCriticalSection (&speaker_lock);
+#elif defined (__APPLE__)
+ if (!speaker_queue) {
+ return;
+ }
+
+ pthread_mutex_lock (&speaker_lock);
+#elif defined (__linux__)
+ if (!speaker_running) {
+ return;
+ }
+
+ pthread_mutex_lock (&speaker_lock);
+#endif
+
+#if defined (_WIN32) || defined (__APPLE__) || defined (__linux__)
+ sb_head = sb_tail = sb_count = sb_phase = 0;
+#endif
+
+#if defined (_WIN32)
+ LeaveCriticalSection (&speaker_lock);
+#elif defined (__APPLE__) || defined (__linux__)
+ pthread_mutex_unlock (&speaker_lock);
+#endif
+
+}