}
+static void sb_dma_update (CPU8086 *cpu) {
+
+ DMA8237 *dma = &cpu->dma[0];
+
+ uint64_t now;
+ unsigned bytes;
+
+ 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 ();
+
+ if (now < cpu->sb_dsp_next_block_ns) {
+ return;
+ }
+
+ bytes = cpu->sb_dsp_block_length;
+
+ while (bytes) {
+
+ unsigned remaining = (unsigned) dma->count[1] + 1;
+ unsigned transferred = bytes < remaining ? bytes : remaining;
+
+ if (dma->mode[1] & 0x20) {
+ dma->address[1] = (uint16_t) (dma->address[1] - transferred);
+ } else {
+ dma->address[1] = (uint16_t) (dma->address[1] + transferred);
+ }
+
+ dma->count[1] = (uint16_t) (dma->count[1] - transferred);
+ bytes -= transferred;
+
+ if (transferred == remaining) {
+
+ if (dma->mode[1] & 0x10) {
+
+ dma->address[1] = dma->base_address[1];
+ dma->count[1] = dma->base_count[1];
+
+ } else {
+
+ dma->mask |= 2;
+
+ cpu->sb_dsp_dma_active = 0;
+ break;
+
+ }
+
+ }
+
+ }
+
+ cpu->sb_dsp_irq_pending = 1;
+ cpu->pic[0].irr |= 0x80;
+
+ if (!cpu->sb_dsp_dma_auto) {
+ cpu->sb_dsp_dma_active = 0;
+ }
+
+ cpu->sb_dsp_next_block_ns = now + (uint64_t) cpu->sb_dsp_block_length * (256 - cpu->sb_dsp_time_constant) * 1000ULL;
+
+}
+
static int cpu_service_timer_irq (CPU8086 *cpu) {
uint64_t elapsed;
uint64_t now;
+ sb_dma_update (cpu);
+
if (cpu->pit[0].null_count) {
return 0;
}
}
+ if (!(cpu->pic[0].mask & 0x80) && !cpu->pic[0].isr && (cpu->pic[0].irr & 0x80)) {
+
+ cpu->pic[0].irr &= (uint8_t) ~0x80;
+ cpu->pic[0].isr |= 0x80;
+
+ if (cpu->cr0 & 1) {
+ return cpu_protected_interrupt (cpu, (uint8_t) (cpu->pic[0].offset + 7)) == 0 ? 1 : -1;
+ }
+
+ handle_interrupt (cpu, (uint8_t) (cpu->pic[0].offset + 7));
+ return 1;
+
+ }
+
return 0;
}
}
if (count) {
+
dma->count[channel] = reg;
+ dma->base_count[channel] = reg;
+
} else {
+
dma->address[channel] = reg;
+ dma->base_address[channel] = reg;
+
}
} else {
return 0x7F; /* DSP command port ready */
- case 0x22E: /* Sound Blaster DSP read-buffer status */
+ case 0x22E: /* Sound Blaster DSP read-buffer status / IRQ acknowledge */
+ cpu->sb_dsp_irq_pending = 0;
return cpu->sb_dsp_reply_count ? 0x80 : 0x00;
case 0x330: /* MPU-401 data port */
cpu->sb_dsp_reply_head = 0;
cpu->sb_dsp_reply_count = 0;
cpu->sb_dsp_speaker = 0;
+ cpu->sb_dsp_dma_active = 0;
+ cpu->sb_dsp_dma_auto = 0;
+ cpu->sb_dsp_dma_paused = 0;
+ cpu->sb_dsp_irq_pending = 0;
+ cpu->sb_dsp_param_count = 0;
} else if (cpu->sb_dsp_reset) {
case 0x22C: /* Sound Blaster DSP command */
+ if (cpu->sb_dsp_param_count) {
+
+ cpu->sb_dsp_params[cpu->sb_dsp_param_index++] = value;
+
+ if (--cpu->sb_dsp_param_count == 0) {
+
+ if (cpu->sb_dsp_command == 0x40) {
+ cpu->sb_dsp_time_constant = cpu->sb_dsp_params[0];
+ } else if (cpu->sb_dsp_command == 0x48) {
+ cpu->sb_dsp_block_length = (uint32_t) (cpu->sb_dsp_params[0] | ((uint16_t) cpu->sb_dsp_params[1] << 8)) + 1;
+ } 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;
+
+ }
+
+ }
+
+ break;
+
+ }
+
switch (value) {
+ case 0x40: /* Set DSP time constant */
+ case 0x48: /* Set DMA transfer block length */
+ case 0x14: /* Single-cycle 8-bit DMA output */
+
+ cpu->sb_dsp_command = value;
+ cpu->sb_dsp_param_index = 0;
+ cpu->sb_dsp_param_count = value == 0x40 ? 1 : 2;
+
+ break;
+
+ 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;
+
+ break;
+
+ case 0xD0: /* Halt 8-bit DMA */
+
+ cpu->sb_dsp_dma_paused = 1;
+ break;
+
case 0xD1: /* Enable DSP speaker */
cpu->sb_dsp_speaker = 1;
cpu->sb_dsp_speaker = 0;
break;
+ 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;
+
+ break;
+
case 0xD8: /* Read DSP speaker status */
sb_dsp_reply (cpu, cpu->sb_dsp_speaker ? 0xFF : 0x00);
break;
+ case 0xDA: /* Exit auto-initialize after current block */
+
+ cpu->sb_dsp_dma_auto = 0;
+ break;
+
case 0xE1: /* Get DSP version: Sound Blaster 2.01 */
sb_dsp_reply (cpu, 2);
}
+ case 0xC8: case 0xC9: case 0xCA: case 0xCB:
+ case 0xCC: case 0xCD: case 0xCE: case 0xCF: { /* BSWAP r32 */
+
+ uint32_t value = get_reg32 (cpu, op2 - 0xC8);
+
+ set_reg32 (cpu, op2 - 0xC8, (value << 24) | ((value & 0x0000FF00UL) << 8) |
+ ((value & 0x00FF0000UL) >> 8) | (value >> 24));
+
+ break;
+
+ }
+
default:
printf ("unhandled 0F opcode 0x%02X at %04X%04X\n", op2, cpu->cs, old_ip);
break;
+ case 3: /* RCR */
+
+ for (int i = 0; i < count % 9; i++) {
+
+ uint8_t curr_cf = (cpu->flags & FLAG_CF) ? 1 : 0;
+ uint8_t next_cf = result & 1;
+
+ result = (uint8_t) ((result >> 1) | (curr_cf << 7));
+
+ if (next_cf) {
+ cpu->flags |= FLAG_CF;
+ } else {
+ cpu->flags &= (uint16_t) ~FLAG_CF;
+ }
+
+ }
+
+ if (count == 1) {
+
+ if ((result ^ (result << 1)) & 0x80) {
+ cpu->flags |= FLAG_OF;
+ } else {
+ cpu->flags &= (uint16_t) ~FLAG_OF;
+ }
+
+ }
+
+ break;
+
case 4: /* SHL / SAL */
if (count > 8) {
typedef struct __attribute__ ((packed)) DMA8237 {
uint16_t address[4], count[4];
+ uint16_t base_address[4], base_count[4];
+
uint8_t page[4], mode[4];
uint8_t command, request, mask, flipflop;
uint8_t sb_dsp_reset, sb_dsp_speaker;
uint8_t sb_dsp_reply[4], sb_dsp_reply_head, sb_dsp_reply_count;
+ uint8_t sb_dsp_command, sb_dsp_param_count, sb_dsp_param_index, sb_dsp_params[2];
+ uint8_t sb_dsp_time_constant, sb_dsp_dma_active, sb_dsp_dma_auto, sb_dsp_dma_paused;
+
+ uint8_t sb_dsp_irq_pending;
+ uint32_t sb_dsp_block_length;
+ uint64_t sb_dsp_next_block_ns;
uint8_t opl_index, opl_timer1, opl_timer2, opl_control, opl_status;
uint64_t opl_timer1_started_ns, opl_timer2_started_ns;