X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dos_auplay;a=blobdiff_plain;f=src%2Fau_sb.c;h=c8ca586f8b405fa616539585b4c06c3d937d5e80;hp=a1ea8301bba31ad4aa077a574a8349ee121c6285;hb=3932cfd54ab14cec36729db7bb32461a58658675;hpb=1f1c8809a0b3b183b60dedf291df9ea8ddfdad74 diff --git a/src/au_sb.c b/src/au_sb.c index a1ea830..c8ca586 100644 --- a/src/au_sb.c +++ b/src/au_sb.c @@ -4,7 +4,7 @@ #include #include #include "audio.h" -#include "au_sb.h" +#include "audrv.h" #include "dpmi.h" #include "dma.h" #include "intr.h" @@ -27,6 +27,7 @@ #define DSP_RATE 0x40 #define DSP4_OUT_RATE 0x41 #define DSP4_IN_RATE 0x42 +#define DSP_BLOCKSZ 0x48 #define DSP_GET_VER 0xe1 /* start DMA playback/recording. combine with fifo/auto/input flags */ @@ -61,8 +62,15 @@ #define MIX_SB16_MASTER_R 0x31 #define MIX_SB16_VOICE_L 0x32 #define MIX_SB16_VOICE_R 0x33 + +#define MIX_SBPRO_STEREO 0x0e #define MIX_SB16_IRQ_SEL 0x80 #define MIX_SB16_DMA_SEL 0x81 +#define MIX_SB16_INTSTAT 0x82 + +#define INTSTAT_DMA8 0x01 +#define INTSTAT_DMA16 0x02 +#define INTSTAT_MPU401 0x04 #define VER_MAJOR(x) ((x) >> 8) #define VER_MINOR(x) ((x) & 0xff) @@ -70,12 +78,24 @@ /* delay for about 1us */ #define iodelay() outp(0x80, 0) + +static int reset_dsp(void); +static void *sb_buffer(int *size); +static void start(int rate, int bits, int nchan); +static void pause(void); +static void cont(void); +static void stop(void); +static int isplaying(void); +static void setvolume(int ctl, int vol); +static int getvolume(int ctl); + +static void set_sbpro_stereo(void); +static int start_dsp4(int rate, int bits, unsigned int mode, int num_samples); +static int start_dsp(int rate, int nchan, int num_samples); static void INTERRUPT intr_handler(); -static void start_dsp4(int bits, unsigned int mode, int num_samples); -static void start_dsp(int nchan, int num_samples); static void write_dsp(unsigned char val); static unsigned char read_dsp(void); -static void write_mix(unsigned char val, int reg); +static void write_mix(int reg, unsigned char val); static unsigned char read_mix(int reg); static int get_dsp_version(void); static int dsp4_detect_irq(void); @@ -83,21 +103,31 @@ static int dsp4_detect_dma(void); static void print_dsp_info(void); static const char *sbname(int ver); + +static const struct audrv sbdrv = { + start, + pause, + cont, + stop, + setvolume, + getvolume, + isplaying +}; + + static int base_port; static int irq, dma_chan, dma16_chan, dsp_ver; -static int dsp4; -static int xfer_mode; static void *buffer[2]; -static int wrbuf; +static volatile int wrbuf; -static int isplaying; -static int cur_bits, cur_mode; +static volatile int playing; +static int cur_bits, auto_init, high_speed; static void (INTERRUPT *prev_intr_handler)(); -int sb_detect(void) +int sb_detect(struct audrv *drv) { int i; char *env; @@ -105,10 +135,10 @@ int sb_detect(void) if((env = getenv("BLASTER"))) { dma16_chan = -1; if(sscanf(env, "A%x I%d D%d H%d", &base_port, &irq, &dma_chan, &dma16_chan) >= 3) { - if(sb_reset_dsp() == 0) { + if(reset_dsp() == 0) { dsp_ver = get_dsp_version(); - dsp4 = VER_MAJOR(dsp_ver) >= 4 ? 1 : 0; print_dsp_info(); + *drv = sbdrv; return 1; } } else { @@ -118,11 +148,10 @@ int sb_detect(void) for(i=0; i<6; i++) { base_port = 0x200 + ((i + 1) << 4); - if(sb_reset_dsp() == 0) { + if(reset_dsp() == 0) { dsp_ver = get_dsp_version(); - dsp4 = VER_MAJOR(dsp_ver) >= 4 ? 1 : 0; - if(dsp4) { + if(dsp_ver >= 0x400) { if(dsp4_detect_irq() == -1) { printf("sb_detect: failed to configure IRQ\n"); return 0; @@ -141,6 +170,7 @@ int sb_detect(void) printf("sb_detect: old sound blaster dsp. assuming: irq 5, dma 1\n"); } print_dsp_info(); + *drv = sbdrv; return 1; } @@ -149,7 +179,7 @@ int sb_detect(void) return 0; } -int sb_reset_dsp(void) +static int reset_dsp(void) { int i; @@ -168,109 +198,93 @@ int sb_reset_dsp(void) return -1; } -void sb_set_output_rate(int rate) -{ - if(dsp4) { - write_dsp(DSP4_OUT_RATE); - write_dsp(rate >> 8); - write_dsp(rate & 0xff); - } else { - int tcon = 256 - 1000000 / rate; - write_dsp(DSP_RATE); - write_dsp(tcon); - } -} - -void *sb_buffer(int *size) -{ - *size = BUFSIZE / 2; - return buffer[wrbuf]; -} - -void sb_start(int rate, int bits, int nchan) +static void start(int rate, int bits, int nchan) { uint16_t seg, pmsel; - uint32_t addr; - int size; + uint32_t addr, next64k; + int size, mode, num_samples, dsp_num_samples; - if(!buffer[0]) { - /* allocate a 64k-aligned buffer in low memory */ + if(!buffer[1]) { + /* allocate a buffer in low memory that doesn't cross 64k boundaries */ if(!(seg = dpmi_alloc(BUFSIZE * 2 / 16, &pmsel))) { - fprintf(stderr, "sb_start: failed to allocate DMA buffer\n"); + fprintf(stderr, "SB start: failed to allocate DMA buffer\n"); return; } - printf("DBG allocated seg: %x, addr: %lx\n", (unsigned int)seg, - (unsigned long)seg << 4); + addr = (uint32_t)seg << 4; + next64k = (addr + 0x10000) & 0xffff0000; + if(next64k - addr < BUFSIZE) { + addr = next64k; + } - addr = ((uint32_t)(seg << 4) + 0xffff) & 0xffff0000; - printf("DBG aligned: %lx\n", (unsigned long)addr); buffer[0] = (void*)addr; buffer[1] = (void*)(addr + BUFSIZE / 2); wrbuf = 0; } - if(!(size = audio_callback(buffer[wrbuf], BUFSIZE / 2))) { + + wrbuf = 0; + if(!(size = audio_callback(buffer[wrbuf], BUFSIZE))) { return; } addr = (uint32_t)buffer[wrbuf]; - wrbuf ^= 1; + num_samples = bits == 8 ? size : size / 2; + + if(size < BUFSIZE) { + auto_init = 0; /* single transfer mode */ + dsp_num_samples = num_samples; + } else { + /* Auto-init playback mode: + * fill the whole buffer with data, program the DMA for BUFSIZE transfer, + * and program the DSP for BUFSIZE/2 transfer. We'll get an interrupt in the + * middle, while the DSP uses the upper half, and we'll refill the bottom half. + * Then continue ping-ponging the two halves of the buffer until we run out of + * data. + */ + auto_init = 1; + dsp_num_samples = num_samples / 2; + } _disable(); if(!prev_intr_handler) { prev_intr_handler = _dos_getvect(IRQ_TO_INTR(irq)); } - printf("setting interrupt vector: %d\n", IRQ_TO_INTR(irq)); _dos_setvect(IRQ_TO_INTR(irq), intr_handler); _enable(); unmask_irq(irq); cur_bits = bits; - cur_mode = bits == 8 ? 0 : DSP4_MODE_SIGNED; - if(nchan > 1) { - cur_mode |= DSP4_MODE_STEREO; - } write_dsp(DSP_ENABLE_OUTPUT); - dma_out(bits == 8 ? dma_chan : dma16_chan, addr, size, DMA_SINGLE); - sb_set_output_rate(rate); - start_dsp4(cur_bits, cur_mode, bits == 8 ? size : size / 2); - isplaying = 1; -} - -static void start_dsp4(int bits, unsigned int mode, int num_samples) -{ - unsigned char cmd = bits == 8 ? DSP4_START_DMA8 : DSP4_START_DMA16; - assert(bits == 8 || bits == 16); + dma_out(cur_bits == 8 ? dma_chan : dma16_chan, addr, BUFSIZE, DMA_SINGLE | auto_init ? DMA_AUTO : 0); - /*cmd |= DSP4_AUTO | DSP4_FIFO;*/ - - /* program the DSP to start the DMA transfer */ - write_dsp(cmd); - write_dsp(mode); - num_samples--; - write_dsp(num_samples & 0xff); - write_dsp((num_samples >> 8) & 0xff); -} - -static void start_dsp(int nchan, int num_samples) -{ - /* program the DSP to start the DMA transfer */ - /* TODO */ + if(dsp_ver >= 0x400) { + int mode = bits == 8 ? 0 : DSP4_MODE_SIGNED; + if(nchan > 1) { + mode |= DSP4_MODE_STEREO; + } + start_dsp4(rate, cur_bits, mode, dsp_num_samples); + } else { + if(nchan > 1 && dsp_ver >= 0x300) { + set_sbpro_stereo(); + } + start_dsp(rate, cur_bits, dsp_num_samples); + } + playing = 1; } -void sb_pause(void) +static void pause(void) { write_dsp(cur_bits == 8 ? DSP_PAUSE_DMA8 : DSP_PAUSE_DMA16); } -void sb_continue(void) +static void cont(void) { write_dsp(cur_bits == 8 ? DSP_CONT_DMA8 : DSP_CONT_DMA16); } -void sb_stop(void) +static void stop(void) { write_dsp(DSP_DISABLE_OUTPUT); @@ -280,23 +294,151 @@ void sb_stop(void) _dos_setvect(IRQ_TO_INTR(irq), prev_intr_handler); _enable(); - /* no need to stop anything if we're in single-cycle mode */ - if(cur_mode & DSP4_AUTO) { - write_dsp(cur_bits == 8 ? DSP_END_DMA8 : DSP_END_DMA16); + write_dsp(cur_bits == 8 ? DSP_END_DMA8 : DSP_END_DMA16); + playing = 0; +} + +static int isplaying(void) +{ + return playing; +} + +static void setvolume(int ctl, int vol) +{ + unsigned char val; + + if(VER_MAJOR(dsp_ver) >= 4) { + /* DSP 4.x - SB16 */ + val = vol & 0xf8; + if(ctl == AUDIO_PCM) { + write_mix(MIX_SB16_VOICE_L, val); + write_mix(MIX_SB16_VOICE_R, val); + } else { + write_mix(MIX_SB16_MASTER_L, val); + write_mix(MIX_SB16_MASTER_R, val); + } + + } else if(VER_MAJOR(dsp_ver) >= 3) { + /* DSP 3.x - SBPro */ + val = vol & 0xe0; + val |= val >> 4; + + if(ctl == AUDIO_PCM) { + write_mix(MIX_SBPRO_VOICE, val); + } else { + write_mix(MIX_SBPRO_MASTER, val); + } + + } else { + /* DSP 2.x - SB 2.0 */ + if(ctl == AUDIO_PCM) { + val = (vol >> 5) & 6; + write_mix(MIX_VOICE, val); + } else { + val = (vol >> 4) & 0xe; + write_mix(MIX_MASTER, val); + } } - isplaying = 0; } -int sb_isplaying(void) +static int getvolume(int ctl) { - return isplaying; + int left, right; + unsigned char val; + + if(dsp_ver >= 0x400) { + /* DSP 4.x - SB16 */ + int lreg, rreg; + if(ctl == AUDIO_PCM) { + lreg = MIX_SB16_VOICE_L; + rreg = MIX_SB16_VOICE_R; + } else { /* MASTER or DEFAULT */ + lreg = MIX_SB16_MASTER_L; + rreg = MIX_SB16_MASTER_R; + } + + val = read_mix(lreg); + left = (val & 0xf8) | (val >> 5); + val = read_mix(rreg); + right = (val & 0xf8) | (val >> 5); + + } else if(dsp_ver >= 0x300) { + /* DSP 3.x - SBPro */ + val = read_mix(ctl == AUDIO_PCM ? MIX_SBPRO_VOICE : MIX_SBPRO_MASTER); + + /* left is top 3 bits, duplicate twice(-ish) */ + left = (val & 0xe0) | ((val >> 3) & 0x1c) | (val >> 6); + /* right is top 3 bits of the lower nibble */ + right = (val << 4) | ((val << 1) & 0x1c) | ((val >> 2) & 3); + + } else { + int volume; + + /* DSP 2.x - SB 2.0 */ + if(ctl == AUDIO_PCM) { + /* voice is in bits 1 and 2 */ + val = read_mix(MIX_VOICE); + volume = (val << 5) | ((val << 3) & 0x30) | ((val << 1) & 0xc) | ((val >> 1) & 3); + } else { + /* master is in the 3 top bits of the lower nibble */ + val = read_mix(MIX_MASTER); + volume = (val << 4) | ((val << 1) & 0x1c) | ((val >> 2) & 3); + } + return volume; + } + + return (left + right) >> 1; +} + +static void set_sbpro_stereo(void) +{ +} + +static int start_dsp4(int rate, int bits, unsigned int mode, int num_samples) +{ + unsigned char cmd = bits == 8 ? DSP4_START_DMA8 : DSP4_START_DMA16; + if(auto_init) { + cmd |= DSP4_AUTO | DSP4_FIFO; + } + + /* set output rate */ + write_dsp(DSP4_OUT_RATE); + write_dsp(rate >> 8); + write_dsp(rate & 0xff); + + /* program the DSP to start the DMA transfer */ + write_dsp(cmd); + write_dsp(mode); + num_samples--; + write_dsp(num_samples & 0xff); + write_dsp((num_samples >> 8) & 0xff); + + return 0; } -void sb_volume(int vol) +static int start_dsp(int rate, int nchan, int num_samples) { - /* TODO */ + /* set time constant */ + int tcon = 256 - 1000000 / rate; + write_dsp(DSP_RATE); + write_dsp(tcon); + + if(nchan > 1) { + /* stereo */ + if(dsp_ver < 0x300) return -1; /* need a sb pro for stereo */ + + set_sbpro_stereo(); + high_speed = rate >= 11025; + } else { + /* mono */ + high_speed = rate >= 23000; + } + + /* program the DSP to start the DMA transfer */ + return -1; /* TODO */ } + static void INTERRUPT intr_handler() { int size; @@ -307,22 +449,17 @@ static void INTERRUPT intr_handler() /* ask for more data */ if(!(size = audio_callback(bptr, BUFSIZE / 2))) { - sb_stop(); - } else { - if(cur_bits == 8) { - dma_out(dma_chan, addr, size, DMA_SINGLE); - start_dsp4(cur_bits, cur_mode, size); - } else { - dma_out(dma16_chan, addr, size, DMA_SINGLE); - start_dsp4(cur_bits, cur_mode, size / 2); - } + stop(); } /* acknowledge the interrupt */ if(cur_bits == 8) { inp(REG_INTACK); } else { - inp(REG_INT16ACK); + unsigned char istat = read_mix(MIX_SB16_INTSTAT); + if(istat & INTSTAT_DMA16) { + inp(REG_INT16ACK); + } } if(irq > 7) { @@ -343,7 +480,7 @@ static unsigned char read_dsp(void) return inp(REG_RDATA); } -static void write_mix(unsigned char val, int reg) +static void write_mix(int reg, unsigned char val) { outp(REG_MIXPORT, reg); outp(REG_MIXDATA, val); @@ -381,7 +518,7 @@ static int dsp4_detect_irq(void) } if(!irq) { /* try to force IRQ 5 */ - write_mix(2, MIX_SB16_IRQ_SEL); /* bit1 selects irq 5 */ + write_mix(MIX_SB16_IRQ_SEL, 2); /* bit1 selects irq 5 */ /* re-read to verify */ irqsel = read_mix(MIX_SB16_IRQ_SEL); @@ -424,7 +561,7 @@ static int dsp4_detect_dma(void) } if(dma_chan == -1 || dma16_chan == -1) { - write_mix(dmasel, MIX_SB16_DMA_SEL); + write_mix(MIX_SB16_DMA_SEL, dmasel); /* re-read to verify */ tmp = read_mix(MIX_SB16_DMA_SEL); @@ -440,9 +577,6 @@ static int dsp4_detect_dma(void) static void print_dsp_info(void) { - int master[2], voice[2]; - int val; - printf("sb_detect: %s (DSP v%d.%02d) port %xh, irq %d", sbname(dsp_ver), VER_MAJOR(dsp_ver), VER_MINOR(dsp_ver), base_port, irq); if(VER_MAJOR(dsp_ver) >= 4) { @@ -450,27 +584,6 @@ static void print_dsp_info(void) } else { printf(" dma %d\n", dma_chan); } - - if(VER_MAJOR(dsp_ver) >= 4) { - master[0] = 100 * (int)(read_mix(MIX_SB16_MASTER_L) >> 3) / 31; - master[1] = 100 * (int)(read_mix(MIX_SB16_MASTER_R) >> 3) / 31; - voice[0] = 100 * (int)(read_mix(MIX_SB16_VOICE_L) >> 3) / 31; - voice[1] = 100 * (int)(read_mix(MIX_SB16_VOICE_R) >> 3) / 31; - } else if(VER_MAJOR(dsp_ver) >= 3) { - val = read_mix(MIX_SBPRO_MASTER); - master[0] = 100 * (val >> 5) / 7; - master[1] = 100 * ((val >> 1) & 7) / 7; - val = read_mix(MIX_SBPRO_VOICE); - voice[0] = 100 * (val >> 5) / 7; - voice[1] = 100 * ((val >> 1) & 7) / 7; - } else { - val = read_mix(MIX_MASTER); - master[0] = master[1] = 100 * ((val >> 1) & 7) / 7; - val = read_mix(MIX_VOICE); - voice[0] = voice[1] = 100 * ((val >> 1) & 3) / 3; - } - printf(" mixer: master(%d|%d) voice(%d|%d)\n", master[0], master[1], - voice[0], voice[1]); } #define V(maj, min) (((maj) << 8) | (min))