ws2_32: Enable IP_DONTFRAGMENT by default for SOCK_STREAM sockets.
[wine.git] / dlls / krnl386.exe16 / soundblaster.c
blobbf1432dfb46eaf7e36037f8472759006b52c1087
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "dosexe.h"
28 #include "wine/debug.h"
29 #include "wingdi.h"
30 #include "mmsystem.h"
31 #include "dsound.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(sblaster);
35 /* Board Configuration */
36 /* FIXME: Should be in a config file */
37 #define SB_IRQ 5
38 #define SB_IRQ_PRI 11
39 #define SB_DMA 1
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 BOOL end_sound_loop = FALSE;
52 static BOOL dma_enable = FALSE;
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;
69 static UINT buf_off;
70 extern HWND vga_hwnd;
72 /* SB_Poll performs DMA transfers and fills the Direct Sound Buffer */
73 static DWORD CALLBACK SB_Poll( void *dummy )
75 HRESULT result;
76 LPBYTE lpbuf1 = NULL;
77 LPBYTE lpbuf2 = NULL;
78 DWORD dwsize1 = 0;
79 DWORD dwsize2 = 0;
80 DWORD dwbyteswritten1 = 0;
81 DWORD dwbyteswritten2 = 0;
82 int size;
84 /* FIXME: this loop must be improved */
85 while(!end_sound_loop)
87 Sleep(10);
89 if (dma_enable) {
90 size = DMA_Transfer(SB_DMA,min(DMATRFSIZE,SamplesCount),dma_buffer);
91 } else
92 continue;
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");
97 continue;
100 dwbyteswritten1 = min(size,dwsize1);
101 memcpy(lpbuf1,dma_buffer,dwbyteswritten1);
102 if (size>dwsize1) {
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);
109 if (result!=DS_OK)
110 ERR("Unable to unlock sound buffer !\n");
112 SamplesCount -= size;
113 if (!SamplesCount) dma_enable = FALSE;
115 return 0;
118 static BOOL SB_Init(void)
120 HRESULT result;
122 if (!lpdsound) {
123 result = DirectSoundCreate(NULL,&lpdsound,NULL);
124 if (result != DS_OK) {
125 ERR("Unable to initialize Sound Subsystem err = %x !\n",result);
126 return FALSE;
129 /* FIXME: To uncomment when :
130 - SetCooperative level is correctly implemented
131 - an always valid and non changing handle to a windows (vga_hwnd) is available
132 (this surely needs some work in vga.c)
133 result = IDirectSound_SetCooperativeLevel(lpdsound,vga_hwnd,DSSCL_EXCLUSIVE|DSSCL_PRIORITY);
134 if (result != DS_OK) {
135 ERR("Can't set cooperative level !\n");
136 return FALSE;
140 /* Default format */
141 wav_fmt.wFormatTag = WAVE_FORMAT_PCM;
142 wav_fmt.nChannels = 1;
143 wav_fmt.nSamplesPerSec = 22050;
144 wav_fmt.nAvgBytesPerSec = 22050;
145 wav_fmt.nBlockAlign = 1;
146 wav_fmt.wBitsPerSample = 8;
147 wav_fmt.cbSize = 0;
149 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
150 buf_desc.dwSize = sizeof(DSBUFFERDESC);
151 buf_desc.dwBufferBytes = DSBUFLEN;
152 buf_desc.lpwfxFormat = &wav_fmt;
153 result = IDirectSound_CreateSoundBuffer(lpdsound,&buf_desc,&lpdsbuf,NULL);
154 if (result != DS_OK) {
155 ERR("Can't create sound buffer !\n");
156 return FALSE;
159 result = IDirectSoundBuffer_Play(lpdsbuf,0, 0, DSBPLAY_LOOPING);
160 if (result != DS_OK) {
161 ERR("Can't start playing !\n");
162 return FALSE;
165 buf_off = 0;
166 end_sound_loop = FALSE;
167 SB_Thread = CreateThread(NULL, 0, SB_Poll, NULL, 0, NULL);
168 TRACE("thread\n");
169 if (!SB_Thread) {
170 ERR("Can't create thread !\n");
171 return FALSE;
174 return TRUE;
177 static void SB_Reset(void)
179 int i;
181 for(i=0;i<256;i++)
182 DSP_Command[i]=0;
184 /* Set Time Constant */
185 DSP_Command[0x40]=1;
186 /* Generate IRQ */
187 DSP_Command[0xF2]=0;
188 /* DMA DAC 8-bits */
189 DSP_Command[0x14]=2;
190 /* Generic DAC/ADC DMA (16-bit, 8-bit) */
191 for(i=0xB0;i<=0xCF;i++)
192 DSP_Command[i]=3;
193 /* DSP Identification */
194 DSP_Command[0xE0]=1;
196 /* Clear command and input buffer */
197 command = -1;
198 InSize = 0;
200 /* Put a garbage value in the output buffer */
201 OutSize = 1;
202 if (SB_Init())
203 /* All right, let's put the magic value for autodetection */
204 DSP_OutBuffer[0] = 0xaa;
205 else
206 /* Something is wrong, put 0 to failed autodetection */
207 DSP_OutBuffer[0] = 0x00;
210 /* Find a standard sampling rate for DirectSound */
211 static int SB_StdSampleRate(int SampleRate)
213 if (SampleRate>((44100+48000)/2)) return 48000;
214 if (SampleRate>((32000+44100)/2)) return 44100;
215 if (SampleRate>((24000+32000)/2)) return 32000;
216 if (SampleRate>((22050+24000)/2)) return 24000;
217 if (SampleRate>((16000+22050)/2)) return 22050;
218 if (SampleRate>((12000+16000)/2)) return 16000;
219 if (SampleRate>((11025+12000)/2)) return 12000;
220 if (SampleRate>((8000+11025)/2)) return 11025;
221 return 8000;
224 void SB_ioport_out( WORD port, BYTE val )
226 switch(port)
228 /* DSP - Reset */
229 case 0x226:
230 TRACE("Resetting DSP.\n");
231 SB_Reset();
232 break;
233 /* DSP - Write Data or Command */
234 case 0x22c:
235 TRACE("val=%x\n",val);
236 if (command == -1) {
237 /* Clear input buffer and set the current command */
238 command = val;
239 InSize = 0;
241 if (InSize!=DSP_Command[command])
242 /* Fill the input buffer the command parameters if any */
243 DSP_InBuffer[InSize++]=val;
244 else {
245 /* Process command */
246 switch(command)
248 case 0x10: /* SB */
249 FIXME("Direct DAC (8-bit) - Not Implemented\n");
250 break;
251 case 0x14: /* SB */
252 SamplesCount = DSP_InBuffer[1]+(val<<8)+1;
253 TRACE("DMA DAC (8-bit) for %x samples\n",SamplesCount);
254 dma_enable = TRUE;
255 break;
256 case 0x20:
257 FIXME("Direct ADC (8-bit) - Not Implemented\n");
258 break;
259 case 0x24: /* SB */
260 FIXME("DMA ADC (8-bit) - Not Implemented\n");
261 break;
262 case 0x40: /* SB */
263 SampleRate = 1000000/(256-val);
264 TRACE("Set Time Constant (%d <-> %d Hz => %d Hz)\n",DSP_InBuffer[0],
265 SampleRate,SB_StdSampleRate(SampleRate));
266 SampleRate = SB_StdSampleRate(SampleRate);
267 wav_fmt.nSamplesPerSec = SampleRate;
268 wav_fmt.nAvgBytesPerSec = SampleRate;
269 IDirectSoundBuffer_SetFormat(lpdsbuf,&wav_fmt);
270 break;
271 /* case 0xBX/0xCX -> See below */
272 case 0xD0: /* SB */
273 TRACE("Halt DMA operation (8-bit)\n");
274 dma_enable = FALSE;
275 break;
276 case 0xD1: /* SB */
277 FIXME("Enable Speaker - Not Implemented\n");
278 break;
279 case 0xD3: /* SB */
280 FIXME("Disable Speaker - Not Implemented\n");
281 break;
282 case 0xD4: /* SB */
283 FIXME("Continue DMA operation (8-bit) - Not Implemented\n");
284 break;
285 case 0xD8: /* SB */
286 FIXME("Speaker Status - Not Implemented\n");
287 break;
288 case 0xE0: /* SB 2.0 */
289 TRACE("DSP Identification\n");
290 DSP_OutBuffer[OutSize++] = ~val;
291 break;
292 case 0xE1: /* SB */
293 TRACE("DSP Version\n");
294 OutSize=2;
295 DSP_OutBuffer[0]=0; /* returns version 1.0 */
296 DSP_OutBuffer[1]=1;
297 break;
298 case 0xF2: /* SB */
299 TRACE("IRQ Request (8-bit)\n");
300 break;
301 default:
302 if (((command&0xF0)==0xB0)||((DSP_InBuffer[0]&0xF0)==0xC0)) {
303 /* SB16 */
304 FIXME("Generic DAC/ADC DMA (16-bit, 8-bit) - %d % d\n",command,DSP_InBuffer[1]);
305 if (command&0x02)
306 FIXME("Generic DAC/ADC fifo mode not supported\n");
307 if (command&0x04)
308 FIXME("Generic DAC/ADC autoinit dma mode not supported\n");
309 if (command&0x08)
310 FIXME("Generic DAC/ADC adc mode not supported\n");
311 switch(command>>4) {
312 case 0xB:
313 FIXME("Generic DAC/ADC 8-bit not supported\n");
314 SampleMode = 0;
315 break;
316 case 0xC:
317 FIXME("Generic DAC/ADC 16-bit not supported\n");
318 SampleMode = 1;
319 break;
320 default:
321 ERR("Generic DAC/ADC resolution unknown\n");
322 break;
324 if (DSP_InBuffer[1]&0x010)
325 FIXME("Generic DAC/ADC signed sample mode not supported\n");
326 if (DSP_InBuffer[1]&0x020)
327 FIXME("Generic DAC/ADC stereo mode not supported\n");
328 SamplesCount = DSP_InBuffer[2]+(val<<8)+1;
329 TRACE("Generic DMA for %x samples\n",SamplesCount);
330 dma_enable = TRUE;
332 else
333 FIXME("DSP command %x not supported\n",val);
335 /* Empty the input buffer and end the command */
336 InSize = 0;
337 command = -1;
342 BYTE SB_ioport_in( WORD port )
344 BYTE res = 0;
346 switch(port)
348 /* DSP Read Data */
349 case 0x22a:
350 /* Value in the read buffer */
351 if (OutSize)
352 res = DSP_OutBuffer[--OutSize];
353 else
354 /* return the last byte */
355 res = DSP_OutBuffer[0];
356 break;
357 /* DSP - Write Buffer Status */
358 case 0x22c:
359 /* DSP always ready for writing */
360 res = 0x00;
361 break;
362 /* DSP - Data Available Status */
363 /* DSP - IRQ Acknowledge, 8-bit */
364 case 0x22e:
365 /* DSP data availability check */
366 if (OutSize)
367 res = 0x80;
368 else
369 res = 0x00;
370 break;
372 return res;