d3d9: Use more reasonable texture dimensions.
[wine/wine64.git] / dlls / winedos / soundblaster.c
blobe48095f24927a5c11f6179681064de5e68ba54af
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 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 HMODULE hmodule;
65 typedef HRESULT (WINAPI* fnDirectSoundCreate) (LPGUID,LPDIRECTSOUND*,LPUNKNOWN);
66 static fnDirectSoundCreate lpDirectSoundCreate;
67 static LPDIRECTSOUND lpdsound;
68 static LPDIRECTSOUNDBUFFER lpdsbuf;
69 static DSBUFFERDESC buf_desc;
70 static WAVEFORMATEX wav_fmt;
71 static HANDLE SB_Thread;
72 static UINT buf_off;
73 extern HWND vga_hwnd;
75 /* SB_Poll performs DMA transfers and fills the Direct Sound Buffer */
76 static DWORD CALLBACK SB_Poll( void *dummy )
78 HRESULT result;
79 LPBYTE lpbuf1 = NULL;
80 LPBYTE lpbuf2 = NULL;
81 DWORD dwsize1 = 0;
82 DWORD dwsize2 = 0;
83 DWORD dwbyteswritten1 = 0;
84 DWORD dwbyteswritten2 = 0;
85 int size;
87 /* FIXME: this loop must be improved */
88 while(!end_sound_loop)
90 Sleep(10);
92 if (dma_enable) {
93 size = DMA_Transfer(SB_DMA,min(DMATRFSIZE,SamplesCount),dma_buffer);
94 } else
95 continue;
97 result = IDirectSoundBuffer_Lock(lpdsbuf,buf_off,size,(LPVOID *)&lpbuf1,&dwsize1,(LPVOID *)&lpbuf2,&dwsize2,0);
98 if (result != DS_OK) {
99 ERR("Unable to lock sound buffer !\n");
100 continue;
103 dwbyteswritten1 = min(size,dwsize1);
104 memcpy(lpbuf1,dma_buffer,dwbyteswritten1);
105 if (size>dwsize1) {
106 dwbyteswritten2 = min(size - dwbyteswritten1,dwsize2);
107 memcpy(lpbuf2,dma_buffer+dwbyteswritten1,dwbyteswritten2);
109 buf_off = (buf_off + dwbyteswritten1 + dwbyteswritten2) % DSBUFLEN;
111 result = IDirectSoundBuffer_Unlock(lpdsbuf,lpbuf1,dwbyteswritten1,lpbuf2,dwbyteswritten2);
112 if (result!=DS_OK)
113 ERR("Unable to unlock sound buffer !\n");
115 SamplesCount -= size;
116 if (!SamplesCount) {
117 DOSVM_QueueEvent(SB_IRQ,SB_IRQ_PRI,NULL,NULL);
118 dma_enable = 0;
121 return 0;
124 static BOOL SB_Init(void)
126 HRESULT result;
128 if (!lpdsound) {
129 hmodule = LoadLibraryA("dsound.dll");
130 if (!hmodule) {
131 ERR("Can't load dsound.dll !\n");
132 return 0;
134 lpDirectSoundCreate = (fnDirectSoundCreate)GetProcAddress(hmodule,"DirectSoundCreate");
135 if (!lpDirectSoundCreate) {
136 /* CloseHandle(hmodule); */
137 ERR("Can't find DirectSoundCreate function !\n");
138 return 0;
140 result = (*lpDirectSoundCreate)(NULL,&lpdsound,NULL);
141 if (result != DS_OK) {
142 ERR("Unable to initialize Sound Subsystem err = %x !\n",result);
143 return 0;
146 /* FIXME: To uncomment when :
147 - SetCooperative level is correctly implemented
148 - an always valid and non changing handle to a windows (vga_hwnd) is available
149 (this surely needs some work in vga.c)
150 result = IDirectSound_SetCooperativeLevel(lpdsound,vga_hwnd,DSSCL_EXCLUSIVE|DSSCL_PRIORITY);
151 if (result != DS_OK) {
152 ERR("Can't set cooperative level !\n");
153 return 0;
157 /* Default format */
158 wav_fmt.wFormatTag = WAVE_FORMAT_PCM;
159 wav_fmt.nChannels = 1;
160 wav_fmt.nSamplesPerSec = 22050;
161 wav_fmt.nAvgBytesPerSec = 22050;
162 wav_fmt.nBlockAlign = 1;
163 wav_fmt.wBitsPerSample = 8;
164 wav_fmt.cbSize = 0;
166 memset(&buf_desc,0,sizeof(DSBUFFERDESC));
167 buf_desc.dwSize = sizeof(DSBUFFERDESC);
168 buf_desc.dwBufferBytes = DSBUFLEN;
169 buf_desc.lpwfxFormat = &wav_fmt;
170 result = IDirectSound_CreateSoundBuffer(lpdsound,&buf_desc,&lpdsbuf,NULL);
171 if (result != DS_OK) {
172 ERR("Can't create sound buffer !\n");
173 return 0;
176 result = IDirectSoundBuffer_Play(lpdsbuf,0, 0, DSBPLAY_LOOPING);
177 if (result != DS_OK) {
178 ERR("Can't start playing !\n");
179 return 0;
182 buf_off = 0;
183 end_sound_loop = 0;
184 SB_Thread = CreateThread(NULL, 0, SB_Poll, NULL, 0, NULL);
185 TRACE("thread\n");
186 if (!SB_Thread) {
187 ERR("Can't create thread !\n");
188 return 0;
191 return 1;
194 static void SB_Reset(void)
196 int i;
198 for(i=0;i<256;i++)
199 DSP_Command[i]=0;
201 /* Set Time Constant */
202 DSP_Command[0x40]=1;
203 /* Generate IRQ */
204 DSP_Command[0xF2]=0;
205 /* DMA DAC 8-bits */
206 DSP_Command[0x14]=2;
207 /* Generic DAC/ADC DMA (16-bit, 8-bit) */
208 for(i=0xB0;i<=0xCF;i++)
209 DSP_Command[i]=3;
210 /* DSP Indentification */
211 DSP_Command[0xE0]=1;
213 /* Clear command and input buffer */
214 command = -1;
215 InSize = 0;
217 /* Put a garbage value in the output buffer */
218 OutSize = 1;
219 if (SB_Init())
220 /* All right, let's put the magic value for autodetection */
221 DSP_OutBuffer[0] = 0xaa;
222 else
223 /* Something is wrong, put 0 to failed autodetection */
224 DSP_OutBuffer[0] = 0x00;
227 /* Find a standard sampling rate for DirectSound */
228 static int SB_StdSampleRate(int SampleRate)
230 if (SampleRate>((44100+48000)/2)) return 48000;
231 if (SampleRate>((32000+44100)/2)) return 44100;
232 if (SampleRate>((24000+32000)/2)) return 32000;
233 if (SampleRate>((22050+24000)/2)) return 24000;
234 if (SampleRate>((16000+22050)/2)) return 22050;
235 if (SampleRate>((12000+16000)/2)) return 16000;
236 if (SampleRate>((11025+12000)/2)) return 12000;
237 if (SampleRate>((8000+11025)/2)) return 11025;
238 return 8000;
241 void SB_ioport_out( WORD port, BYTE val )
243 switch(port)
245 /* DSP - Reset */
246 case 0x226:
247 TRACE("Resetting DSP.\n");
248 SB_Reset();
249 break;
250 /* DSP - Write Data or Command */
251 case 0x22c:
252 TRACE("val=%x\n",val);
253 if (command == -1) {
254 /* Clear input buffer and set the current command */
255 command = val;
256 InSize = 0;
258 if (InSize!=DSP_Command[command])
259 /* Fill the input buffer the command parameters if any */
260 DSP_InBuffer[InSize++]=val;
261 else {
262 /* Process command */
263 switch(command)
265 case 0x10: /* SB */
266 FIXME("Direct DAC (8-bit) - Not Implemented\n");
267 break;
268 case 0x14: /* SB */
269 SamplesCount = DSP_InBuffer[1]+(val<<8)+1;
270 TRACE("DMA DAC (8-bit) for %x samples\n",SamplesCount);
271 dma_enable = 1;
272 break;
273 case 0x20:
274 FIXME("Direct ADC (8-bit) - Not Implemented\n");
275 break;
276 case 0x24: /* SB */
277 FIXME("DMA ADC (8-bit) - Not Implemented\n");
278 break;
279 case 0x40: /* SB */
280 SampleRate = 1000000/(256-val);
281 TRACE("Set Time Constant (%d <-> %d Hz => %d Hz)\n",DSP_InBuffer[0],
282 SampleRate,SB_StdSampleRate(SampleRate));
283 SampleRate = SB_StdSampleRate(SampleRate);
284 wav_fmt.nSamplesPerSec = SampleRate;
285 wav_fmt.nAvgBytesPerSec = SampleRate;
286 IDirectSoundBuffer_SetFormat(lpdsbuf,&wav_fmt);
287 break;
288 /* case 0xBX/0xCX -> See below */
289 case 0xD0: /* SB */
290 TRACE("Halt DMA operation (8-bit)\n");
291 dma_enable = 0;
292 break;
293 case 0xD1: /* SB */
294 FIXME("Enable Speaker - Not Implemented\n");
295 break;
296 case 0xD3: /* SB */
297 FIXME("Disable Speaker - Not Implemented\n");
298 break;
299 case 0xD4: /* SB */
300 FIXME("Continue DMA operation (8-bit) - Not Implemented\n");
301 break;
302 case 0xD8: /* SB */
303 FIXME("Speaker Status - Not Implemented\n");
304 break;
305 case 0xE0: /* SB 2.0 */
306 TRACE("DSP Identification\n");
307 DSP_OutBuffer[OutSize++] = ~val;
308 break;
309 case 0xE1: /* SB */
310 TRACE("DSP Version\n");
311 OutSize=2;
312 DSP_OutBuffer[0]=0; /* returns version 1.0 */
313 DSP_OutBuffer[1]=1;
314 break;
315 case 0xF2: /* SB */
316 TRACE("IRQ Request (8-bit)\n");
317 DOSVM_QueueEvent(SB_IRQ,SB_IRQ_PRI,NULL,NULL);
318 break;
319 default:
320 if (((command&0xF0)==0xB0)||((DSP_InBuffer[0]&0xF0)==0xC0)) {
321 /* SB16 */
322 FIXME("Generic DAC/ADC DMA (16-bit, 8-bit) - %d % d\n",command,DSP_InBuffer[1]);
323 if (command&0x02)
324 FIXME("Generic DAC/ADC fifo mode not supported\n");
325 if (command&0x04)
326 FIXME("Generic DAC/ADC autoinit dma mode not supported\n");
327 if (command&0x08)
328 FIXME("Generic DAC/ADC adc mode not supported\n");
329 switch(command>>4) {
330 case 0xB:
331 FIXME("Generic DAC/ADC 8-bit not supported\n");
332 SampleMode = 0;
333 break;
334 case 0xC:
335 FIXME("Generic DAC/ADC 16-bit not supported\n");
336 SampleMode = 1;
337 break;
338 default:
339 ERR("Generic DAC/ADC resolution unknown\n");
340 break;
342 if (DSP_InBuffer[1]&0x010)
343 FIXME("Generic DAC/ADC signed sample mode not supported\n");
344 if (DSP_InBuffer[1]&0x020)
345 FIXME("Generic DAC/ADC stereo mode not supported\n");
346 SamplesCount = DSP_InBuffer[2]+(val<<8)+1;
347 TRACE("Generic DMA for %x samples\n",SamplesCount);
348 dma_enable = 1;
350 else
351 FIXME("DSP command %x not supported\n",val);
353 /* Empty the input buffer and end the command */
354 InSize = 0;
355 command = -1;
360 BYTE SB_ioport_in( WORD port )
362 BYTE res = 0;
364 switch(port)
366 /* DSP Read Data */
367 case 0x22a:
368 /* Value in the read buffer */
369 if (OutSize)
370 res = DSP_OutBuffer[--OutSize];
371 else
372 /* return the last byte */
373 res = DSP_OutBuffer[0];
374 break;
375 /* DSP - Write Buffer Status */
376 case 0x22c:
377 /* DSP always ready for writing */
378 res = 0x00;
379 break;
380 /* DSP - Data Available Status */
381 /* DSP - IRQ Acknowledge, 8-bit */
382 case 0x22e:
383 /* DSP data availability check */
384 if (OutSize)
385 res = 0x80;
386 else
387 res = 0x00;
388 break;
390 return res;