2 * Soundblaster Emulation
4 * Copyright 2002 Christian Costa
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(sblaster);
35 /* Board Configuration */
36 /* FIXME: Should be in a config file */
41 /* Soundblaster state */
42 static int SampleMode; /* Mono / Stereo */
43 static int SampleRate;
44 static int SamplesCount;
45 static BYTE DSP_Command[256]; /* Store param numbers in bytes for each command */
46 static BYTE DSP_InBuffer[10]; /* Store DSP command bytes parameters from host */
47 static int InSize; /* Nb of bytes in InBuffer */
48 static BYTE DSP_OutBuffer[10]; /* Store DSP information bytes to host */
49 static int OutSize; /* Nb of bytes in InBuffer */
50 static int command; /* Current command */
51 static int end_sound_loop = 0;
52 static int dma_enable = 0;
54 /* The maximum size of a dma transfer can be 65536 */
55 #define DMATRFSIZE 1024
57 /* DMA can perform 8 or 16-bit transfer */
58 static BYTE dma_buffer[DMATRFSIZE*2];
60 /* Direct Sound buffer config */
61 #define DSBUFLEN 4096 /* FIXME: Only this value seems to work */
63 /* Direct Sound playback stuff */
64 static LPDIRECTSOUND lpdsound;
65 static LPDIRECTSOUNDBUFFER lpdsbuf;
66 static DSBUFFERDESC buf_desc;
67 static WAVEFORMATEX wav_fmt;
68 static HANDLE SB_Thread;
72 /* SB_Poll performs DMA transfers and fills the Direct Sound Buffer */
73 static DWORD CALLBACK SB_Poll( void *dummy )
80 DWORD dwbyteswritten1 = 0;
81 DWORD dwbyteswritten2 = 0;
84 /* FIXME: this loop must be improved */
85 while(!end_sound_loop)
90 size = DMA_Transfer(SB_DMA,min(DMATRFSIZE,SamplesCount),dma_buffer);
94 result = IDirectSoundBuffer_Lock(lpdsbuf,buf_off,size,(LPVOID *)&lpbuf1,&dwsize1,(LPVOID *)&lpbuf2,&dwsize2,0);
95 if (result != DS_OK) {
96 ERR("Unable to lock sound buffer !\n");
100 dwbyteswritten1 = min(size,dwsize1);
101 memcpy(lpbuf1,dma_buffer,dwbyteswritten1);
103 dwbyteswritten2 = min(size - dwbyteswritten1,dwsize2);
104 memcpy(lpbuf2,dma_buffer+dwbyteswritten1,dwbyteswritten2);
106 buf_off = (buf_off + dwbyteswritten1 + dwbyteswritten2) % DSBUFLEN;
108 result = IDirectSoundBuffer_Unlock(lpdsbuf,lpbuf1,dwbyteswritten1,lpbuf2,dwbyteswritten2);
110 ERR("Unable to unlock sound buffer !\n");
112 SamplesCount -= size;
114 DOSVM_QueueEvent(SB_IRQ,SB_IRQ_PRI,NULL,NULL);
121 static BOOL SB_Init(void)
126 result = DirectSoundCreate(NULL,&lpdsound,NULL);
127 if (result != DS_OK) {
128 ERR("Unable to initialize Sound Subsystem err = %x !\n",result);
132 /* FIXME: To uncomment when :
133 - SetCooperative level is correctly implemented
134 - an always valid and non changing handle to a windows (vga_hwnd) is available
135 (this surely needs some work in vga.c)
136 result = IDirectSound_SetCooperativeLevel(lpdsound,vga_hwnd,DSSCL_EXCLUSIVE|DSSCL_PRIORITY);
137 if (result != DS_OK) {
138 ERR("Can't set cooperative level !\n");
144 wav_fmt.wFormatTag = WAVE_FORMAT_PCM;
145 wav_fmt.nChannels = 1;
146 wav_fmt.nSamplesPerSec = 22050;
147 wav_fmt.nAvgBytesPerSec = 22050;
148 wav_fmt.nBlockAlign = 1;
149 wav_fmt.wBitsPerSample = 8;
152 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
153 buf_desc.dwSize = sizeof(DSBUFFERDESC);
154 buf_desc.dwBufferBytes = DSBUFLEN;
155 buf_desc.lpwfxFormat = &wav_fmt;
156 result = IDirectSound_CreateSoundBuffer(lpdsound,&buf_desc,&lpdsbuf,NULL);
157 if (result != DS_OK) {
158 ERR("Can't create sound buffer !\n");
162 result = IDirectSoundBuffer_Play(lpdsbuf,0, 0, DSBPLAY_LOOPING);
163 if (result != DS_OK) {
164 ERR("Can't start playing !\n");
170 SB_Thread = CreateThread(NULL, 0, SB_Poll, NULL, 0, NULL);
173 ERR("Can't create thread !\n");
180 static void SB_Reset(void)
187 /* Set Time Constant */
193 /* Generic DAC/ADC DMA (16-bit, 8-bit) */
194 for(i=0xB0;i<=0xCF;i++)
196 /* DSP Indentification */
199 /* Clear command and input buffer */
203 /* Put a garbage value in the output buffer */
206 /* All right, let's put the magic value for autodetection */
207 DSP_OutBuffer[0] = 0xaa;
209 /* Something is wrong, put 0 to failed autodetection */
210 DSP_OutBuffer[0] = 0x00;
213 /* Find a standard sampling rate for DirectSound */
214 static int SB_StdSampleRate(int SampleRate)
216 if (SampleRate>((44100+48000)/2)) return 48000;
217 if (SampleRate>((32000+44100)/2)) return 44100;
218 if (SampleRate>((24000+32000)/2)) return 32000;
219 if (SampleRate>((22050+24000)/2)) return 24000;
220 if (SampleRate>((16000+22050)/2)) return 22050;
221 if (SampleRate>((12000+16000)/2)) return 16000;
222 if (SampleRate>((11025+12000)/2)) return 12000;
223 if (SampleRate>((8000+11025)/2)) return 11025;
227 void SB_ioport_out( WORD port, BYTE val )
233 TRACE("Resetting DSP.\n");
236 /* DSP - Write Data or Command */
238 TRACE("val=%x\n",val);
240 /* Clear input buffer and set the current command */
244 if (InSize!=DSP_Command[command])
245 /* Fill the input buffer the command parameters if any */
246 DSP_InBuffer[InSize++]=val;
248 /* Process command */
252 FIXME("Direct DAC (8-bit) - Not Implemented\n");
255 SamplesCount = DSP_InBuffer[1]+(val<<8)+1;
256 TRACE("DMA DAC (8-bit) for %x samples\n",SamplesCount);
260 FIXME("Direct ADC (8-bit) - Not Implemented\n");
263 FIXME("DMA ADC (8-bit) - Not Implemented\n");
266 SampleRate = 1000000/(256-val);
267 TRACE("Set Time Constant (%d <-> %d Hz => %d Hz)\n",DSP_InBuffer[0],
268 SampleRate,SB_StdSampleRate(SampleRate));
269 SampleRate = SB_StdSampleRate(SampleRate);
270 wav_fmt.nSamplesPerSec = SampleRate;
271 wav_fmt.nAvgBytesPerSec = SampleRate;
272 IDirectSoundBuffer_SetFormat(lpdsbuf,&wav_fmt);
274 /* case 0xBX/0xCX -> See below */
276 TRACE("Halt DMA operation (8-bit)\n");
280 FIXME("Enable Speaker - Not Implemented\n");
283 FIXME("Disable Speaker - Not Implemented\n");
286 FIXME("Continue DMA operation (8-bit) - Not Implemented\n");
289 FIXME("Speaker Status - Not Implemented\n");
291 case 0xE0: /* SB 2.0 */
292 TRACE("DSP Identification\n");
293 DSP_OutBuffer[OutSize++] = ~val;
296 TRACE("DSP Version\n");
298 DSP_OutBuffer[0]=0; /* returns version 1.0 */
302 TRACE("IRQ Request (8-bit)\n");
303 DOSVM_QueueEvent(SB_IRQ,SB_IRQ_PRI,NULL,NULL);
306 if (((command&0xF0)==0xB0)||((DSP_InBuffer[0]&0xF0)==0xC0)) {
308 FIXME("Generic DAC/ADC DMA (16-bit, 8-bit) - %d % d\n",command,DSP_InBuffer[1]);
310 FIXME("Generic DAC/ADC fifo mode not supported\n");
312 FIXME("Generic DAC/ADC autoinit dma mode not supported\n");
314 FIXME("Generic DAC/ADC adc mode not supported\n");
317 FIXME("Generic DAC/ADC 8-bit not supported\n");
321 FIXME("Generic DAC/ADC 16-bit not supported\n");
325 ERR("Generic DAC/ADC resolution unknown\n");
328 if (DSP_InBuffer[1]&0x010)
329 FIXME("Generic DAC/ADC signed sample mode not supported\n");
330 if (DSP_InBuffer[1]&0x020)
331 FIXME("Generic DAC/ADC stereo mode not supported\n");
332 SamplesCount = DSP_InBuffer[2]+(val<<8)+1;
333 TRACE("Generic DMA for %x samples\n",SamplesCount);
337 FIXME("DSP command %x not supported\n",val);
339 /* Empty the input buffer and end the command */
346 BYTE SB_ioport_in( WORD port )
354 /* Value in the read buffer */
356 res = DSP_OutBuffer[--OutSize];
358 /* return the last byte */
359 res = DSP_OutBuffer[0];
361 /* DSP - Write Buffer Status */
363 /* DSP always ready for writing */
366 /* DSP - Data Available Status */
367 /* DSP - IRQ Acknowledge, 8-bit */
369 /* DSP data availability check */