PAS16.C 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /**********************************************************************
  16. module: PAS16.C
  17. author: James R. Dose
  18. date: March 27, 1994
  19. Low level routines to support Pro AudioSpectrum and compatible
  20. sound cards.
  21. (c) Copyright 1994 James R. Dose. All Rights Reserved.
  22. **********************************************************************/
  23. #include <dos.h>
  24. #include <conio.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "dpmi.h"
  29. #include "dma.h"
  30. #include "interrup.h"
  31. #include "irq.h"
  32. #include "pas16.h"
  33. #include "_pas16.h"
  34. #define USESTACK
  35. static const int PAS_Interrupts[ PAS_MaxIrq + 1 ] =
  36. {
  37. INVALID, INVALID, 0xa, 0xb,
  38. INVALID, 0xd, INVALID, 0xf,
  39. INVALID, INVALID, 0x72, 0x73,
  40. 0x74, INVALID, INVALID, 0x77
  41. };
  42. static void ( interrupt far *PAS_OldInt )( void );
  43. static int PAS_IntController1Mask;
  44. static int PAS_IntController2Mask;
  45. static int PAS_Installed = FALSE;
  46. static int PAS_TranslateCode = DEFAULT_BASE;
  47. static int PAS_OriginalPCMLeftVolume = 75;
  48. static int PAS_OriginalPCMRightVolume = 75;
  49. static int PAS_OriginalFMLeftVolume = 75;
  50. static int PAS_OriginalFMRightVolume = 75;
  51. unsigned int PAS_DMAChannel;
  52. static int PAS_Irq;
  53. static MVState *PAS_State = NULL;
  54. static MVFunc *PAS_Func = NULL;
  55. static MVState PAS_OriginalState;
  56. static int PAS_SampleSizeConfig;
  57. static char *PAS_DMABuffer;
  58. static char *PAS_DMABufferEnd;
  59. static char *PAS_CurrentDMABuffer;
  60. static int PAS_TotalDMABufferSize;
  61. static int PAS_TransferLength = 0;
  62. static int PAS_MixMode = PAS_DefaultMixMode;
  63. static unsigned PAS_SampleRate = PAS_DefaultSampleRate;
  64. static int PAS_TimeInterval = 0;
  65. volatile int PAS_SoundPlaying;
  66. void ( *PAS_CallBack )( void );
  67. // adequate stack size
  68. #define kStackSize 2048
  69. static unsigned short StackSelector = NULL;
  70. static unsigned long StackPointer;
  71. static unsigned short oldStackSelector;
  72. static unsigned long oldStackPointer;
  73. // This is defined because we can't create local variables in a
  74. // function that switches stacks.
  75. static int irqstatus;
  76. // These declarations are necessary to use the inline assembly pragmas.
  77. extern void GetStack(unsigned short *selptr,unsigned long *stackptr);
  78. extern void SetStack(unsigned short selector,unsigned long stackptr);
  79. // This function will get the current stack selector and pointer and save
  80. // them off.
  81. #pragma aux GetStack = \
  82. "mov [edi],esp" \
  83. "mov ax,ss" \
  84. "mov [esi],ax" \
  85. parm [esi] [edi] \
  86. modify [eax esi edi];
  87. // This function will set the stack selector and pointer to the specified
  88. // values.
  89. #pragma aux SetStack = \
  90. "mov ss,ax" \
  91. "mov esp,edx" \
  92. parm [ax] [edx] \
  93. modify [eax edx];
  94. int PAS_ErrorCode = PAS_Ok;
  95. #define PAS_SetErrorCode( status ) \
  96. PAS_ErrorCode = ( status );
  97. /*---------------------------------------------------------------------
  98. Function: PAS_ErrorString
  99. Returns a pointer to the error message associated with an error
  100. number. A -1 returns a pointer the current error.
  101. ---------------------------------------------------------------------*/
  102. char *PAS_ErrorString
  103. (
  104. int ErrorNumber
  105. )
  106. {
  107. char *ErrorString;
  108. switch( ErrorNumber )
  109. {
  110. case PAS_Warning :
  111. case PAS_Error :
  112. ErrorString = PAS_ErrorString( PAS_ErrorCode );
  113. break;
  114. case PAS_Ok :
  115. ErrorString = "Pro AudioSpectrum ok.";
  116. break;
  117. case PAS_DriverNotFound :
  118. ErrorString = "MVSOUND.SYS not loaded.";
  119. break;
  120. case PAS_DmaError :
  121. ErrorString = DMA_ErrorString( DMA_Error );
  122. break;
  123. case PAS_InvalidIrq :
  124. ErrorString = "Invalid Pro AudioSpectrum Irq.";
  125. break;
  126. case PAS_UnableToSetIrq :
  127. ErrorString = "Unable to set Pro AudioSpectrum IRQ. Try selecting an IRQ of 7 or below.";
  128. break;
  129. case PAS_Dos4gwIrqError :
  130. ErrorString = "Unsupported Pro AudioSpectrum Irq.";
  131. break;
  132. case PAS_NoSoundPlaying :
  133. ErrorString = "No sound playing on Pro AudioSpectrum.";
  134. break;
  135. case PAS_CardNotFound :
  136. ErrorString = "Could not find Pro AudioSpectrum.";
  137. break;
  138. case PAS_DPMI_Error :
  139. ErrorString = "DPMI Error in PAS16.";
  140. break;
  141. case PAS_OutOfMemory :
  142. ErrorString = "Out of conventional memory in PAS16.";
  143. break;
  144. default :
  145. ErrorString = "Unknown Pro AudioSpectrum error code.";
  146. break;
  147. }
  148. return( ErrorString );
  149. }
  150. /**********************************************************************
  151. Memory locked functions:
  152. **********************************************************************/
  153. #define PAS_LockStart PAS_CheckForDriver
  154. /*---------------------------------------------------------------------
  155. Function: PAS_CheckForDriver
  156. Checks to see if MVSOUND.SYS is installed.
  157. ---------------------------------------------------------------------*/
  158. int PAS_CheckForDriver
  159. (
  160. void
  161. )
  162. {
  163. union REGS regs;
  164. unsigned result;
  165. regs.w.ax = MV_CheckForDriver;
  166. regs.w.bx = 0x3f3f;
  167. #ifdef __386__
  168. int386( MV_SoundInt, &regs, &regs );
  169. #else
  170. int86( MV_SoundInt, &regs, &regs );
  171. #endif
  172. if ( regs.w.ax != MV_CheckForDriver )
  173. {
  174. PAS_SetErrorCode( PAS_DriverNotFound );
  175. return( PAS_Error );
  176. }
  177. result = regs.w.bx ^ regs.w.cx ^ regs.w.dx;
  178. if ( result != MV_Signature )
  179. {
  180. PAS_SetErrorCode( PAS_DriverNotFound );
  181. return( PAS_Error );
  182. }
  183. return( PAS_Ok );
  184. }
  185. /*---------------------------------------------------------------------
  186. Function: PAS_GetStateTable
  187. Returns a pointer to the state table containing hardware state
  188. information. The state table is necessary because the Pro Audio-
  189. Spectrum contains only write-only registers.
  190. ---------------------------------------------------------------------*/
  191. MVState *PAS_GetStateTable
  192. (
  193. void
  194. )
  195. {
  196. union REGS regs;
  197. MVState *ptr;
  198. regs.w.ax = MV_GetPointerToStateTable;
  199. #ifdef __386__
  200. int386( MV_SoundInt, &regs, &regs );
  201. #else
  202. int86( MV_SoundInt, &regs, &regs );
  203. #endif
  204. if ( regs.w.ax != MV_Signature )
  205. {
  206. PAS_SetErrorCode( PAS_DriverNotFound );
  207. return( NULL );
  208. }
  209. #if defined(__WATCOMC__) && defined(__FLAT__)
  210. ptr = ( MVState * )( ( ( ( unsigned )regs.w.dx ) << 4 ) +
  211. ( ( unsigned )regs.w.bx ) );
  212. #else
  213. ptr = MK_FP( regs.w.dx, regs.w.bx );
  214. #endif
  215. return( ptr );
  216. }
  217. /*---------------------------------------------------------------------
  218. Function: PAS_GetFunctionTable
  219. Returns a pointer to the function table containing addresses of
  220. driver functions.
  221. ---------------------------------------------------------------------*/
  222. MVFunc *PAS_GetFunctionTable
  223. (
  224. void
  225. )
  226. {
  227. union REGS regs;
  228. MVFunc *ptr;
  229. regs.w.ax = MV_GetPointerToFunctionTable;
  230. #ifdef __386__
  231. int386( MV_SoundInt, &regs, &regs );
  232. #else
  233. int86( MV_SoundInt, &regs, &regs );
  234. #endif
  235. if ( regs.w.ax != MV_Signature )
  236. {
  237. PAS_SetErrorCode( PAS_DriverNotFound );
  238. return( NULL );
  239. }
  240. #if defined(__WATCOMC__) && defined(__FLAT__)
  241. ptr = ( MVFunc * )( ( ( ( unsigned )regs.w.dx ) << 4 ) +
  242. ( ( unsigned )regs.w.bx ) );
  243. #else
  244. ptr = MK_FP( regs.w.dx, regs.w.bx );
  245. #endif
  246. return( ptr );
  247. }
  248. /*---------------------------------------------------------------------
  249. Function: PAS_GetCardSettings
  250. Returns the DMA and the IRQ channels of the sound card.
  251. ---------------------------------------------------------------------*/
  252. int PAS_GetCardSettings
  253. (
  254. void
  255. )
  256. {
  257. union REGS regs;
  258. int status;
  259. regs.w.ax = MV_GetDmaIrqInt;
  260. #ifdef __386__
  261. int386( MV_SoundInt, &regs, &regs );
  262. #else
  263. int86( MV_SoundInt, &regs, &regs );
  264. #endif
  265. if ( regs.w.ax != MV_Signature )
  266. {
  267. PAS_SetErrorCode( PAS_DriverNotFound );
  268. return( PAS_Error );
  269. }
  270. PAS_DMAChannel = regs.w.bx;
  271. PAS_Irq = regs.w.cx;
  272. if ( PAS_Irq > PAS_MaxIrq )
  273. {
  274. PAS_SetErrorCode( PAS_Dos4gwIrqError );
  275. return( PAS_Error );
  276. }
  277. if ( !VALID_IRQ( PAS_Irq ) )
  278. {
  279. PAS_SetErrorCode( PAS_InvalidIrq );
  280. return( PAS_Error );
  281. }
  282. if ( PAS_Interrupts[ PAS_Irq ] == INVALID )
  283. {
  284. PAS_SetErrorCode( PAS_InvalidIrq );
  285. return( PAS_Error );
  286. }
  287. status = DMA_VerifyChannel( PAS_DMAChannel );
  288. if ( status == DMA_Error )
  289. {
  290. PAS_SetErrorCode( PAS_DmaError );
  291. return( PAS_Error );
  292. }
  293. return( PAS_Ok );
  294. }
  295. /*---------------------------------------------------------------------
  296. Function: PAS_EnableInterrupt
  297. Enables the triggering of the sound card interrupt.
  298. ---------------------------------------------------------------------*/
  299. void PAS_EnableInterrupt
  300. (
  301. void
  302. )
  303. {
  304. int mask;
  305. int data;
  306. unsigned flags;
  307. flags = DisableInterrupts();
  308. if ( PAS_Irq < 8 )
  309. {
  310. mask = inp( 0x21 ) & ~( 1 << PAS_Irq );
  311. outp( 0x21, mask );
  312. }
  313. else
  314. {
  315. mask = inp( 0xA1 ) & ~( 1 << ( PAS_Irq - 8 ) );
  316. outp( 0xA1, mask );
  317. mask = inp( 0x21 ) & ~( 1 << 2 );
  318. outp( 0x21, mask );
  319. }
  320. // Flush any pending interrupts
  321. PAS_Write( InterruptStatus, PAS_Read( InterruptStatus ) & 0x40 );
  322. // Enable the interrupt on the PAS
  323. data = PAS_State->intrctlr;
  324. data |= SampleBufferInterruptFlag;
  325. PAS_Write( InterruptControl, data );
  326. PAS_State->intrctlr = data;
  327. RestoreInterrupts( flags );
  328. }
  329. /*---------------------------------------------------------------------
  330. Function: PAS_DisableInterrupt
  331. Disables the triggering of the sound card interrupt.
  332. ---------------------------------------------------------------------*/
  333. void PAS_DisableInterrupt
  334. (
  335. void
  336. )
  337. {
  338. int mask;
  339. int data;
  340. unsigned flags;
  341. flags = DisableInterrupts();
  342. // Disable the interrupt on the PAS
  343. data = PAS_State->intrctlr;
  344. data &= ~( SampleRateInterruptFlag | SampleBufferInterruptFlag );
  345. PAS_Write( InterruptControl, data );
  346. PAS_State->intrctlr = data;
  347. // Restore interrupt mask
  348. if ( PAS_Irq < 8 )
  349. {
  350. mask = inp( 0x21 ) & ~( 1 << PAS_Irq );
  351. mask |= PAS_IntController1Mask & ( 1 << PAS_Irq );
  352. outp( 0x21, mask );
  353. }
  354. else
  355. {
  356. mask = inp( 0x21 ) & ~( 1 << 2 );
  357. mask |= PAS_IntController1Mask & ( 1 << 2 );
  358. outp( 0x21, mask );
  359. mask = inp( 0xA1 ) & ~( 1 << ( PAS_Irq - 8 ) );
  360. mask |= PAS_IntController2Mask & ( 1 << ( PAS_Irq - 8 ) );
  361. outp( 0xA1, mask );
  362. }
  363. RestoreInterrupts( flags );
  364. }
  365. /*---------------------------------------------------------------------
  366. Function: PAS_ServiceInterrupt
  367. Handles interrupt generated by sound card at the end of a voice
  368. transfer. Calls the user supplied callback function.
  369. ---------------------------------------------------------------------*/
  370. void interrupt far PAS_ServiceInterrupt
  371. (
  372. void
  373. )
  374. {
  375. #ifdef USESTACK
  376. // save stack
  377. GetStack( &oldStackSelector, &oldStackPointer );
  378. // set our stack
  379. SetStack( StackSelector, StackPointer );
  380. #endif
  381. irqstatus = PAS_Read( InterruptStatus );
  382. if ( ( irqstatus & SampleBufferInterruptFlag ) == 0 )
  383. {
  384. #ifdef USESTACK
  385. // restore stack
  386. SetStack( oldStackSelector, oldStackPointer );
  387. #endif
  388. _chain_intr( PAS_OldInt );
  389. }
  390. // Clear the interrupt
  391. irqstatus &= ~SampleBufferInterruptFlag;
  392. PAS_Write( InterruptStatus, irqstatus );
  393. // send EOI to Interrupt Controller
  394. if ( PAS_Irq > 7 )
  395. {
  396. outp( 0xA0, 0x20 );
  397. }
  398. outp( 0x20, 0x20 );
  399. // Keep track of current buffer
  400. PAS_CurrentDMABuffer += PAS_TransferLength;
  401. if ( PAS_CurrentDMABuffer >= PAS_DMABufferEnd )
  402. {
  403. PAS_CurrentDMABuffer = PAS_DMABuffer;
  404. }
  405. // Call the caller's callback function
  406. if ( PAS_CallBack != NULL )
  407. {
  408. PAS_CallBack();
  409. }
  410. #ifdef USESTACK
  411. // restore stack
  412. SetStack( oldStackSelector, oldStackPointer );
  413. #endif
  414. }
  415. /*---------------------------------------------------------------------
  416. Function: PAS_Write
  417. Writes a byte of data to the sound card.
  418. ---------------------------------------------------------------------*/
  419. void PAS_Write
  420. (
  421. int Register,
  422. int Data
  423. )
  424. {
  425. int port;
  426. port = Register ^ PAS_TranslateCode;
  427. outp( port, Data );
  428. }
  429. /*---------------------------------------------------------------------
  430. Function: PAS_Read
  431. Reads a byte of data from the sound card.
  432. ---------------------------------------------------------------------*/
  433. int PAS_Read
  434. (
  435. int Register
  436. )
  437. {
  438. int port;
  439. int data;
  440. port = Register ^ PAS_TranslateCode;
  441. data = inp( port );
  442. return( data );
  443. }
  444. /*---------------------------------------------------------------------
  445. Function: PAS_SetSampleRateTimer
  446. Programs the Sample Rate Timer.
  447. ---------------------------------------------------------------------*/
  448. void PAS_SetSampleRateTimer
  449. (
  450. void
  451. )
  452. {
  453. int LoByte;
  454. int HiByte;
  455. int data;
  456. unsigned flags;
  457. flags = DisableInterrupts();
  458. // Disable the Sample Rate Timer
  459. data = PAS_State->audiofilt;
  460. data &= ~SampleRateTimerGateFlag;
  461. PAS_Write( AudioFilterControl, data );
  462. PAS_State->audiofilt = data;
  463. // Select the Sample Rate Timer
  464. data = SelectSampleRateTimer;
  465. PAS_Write( LocalTimerControl, data );
  466. PAS_State->tmrctlr = data;
  467. LoByte = lobyte( PAS_TimeInterval );
  468. HiByte = hibyte( PAS_TimeInterval );
  469. // Program the Sample Rate Timer
  470. PAS_Write( SampleRateTimer, LoByte );
  471. PAS_Write( SampleRateTimer, HiByte );
  472. PAS_State->samplerate = PAS_TimeInterval;
  473. RestoreInterrupts( flags );
  474. }
  475. /*---------------------------------------------------------------------
  476. Function: PAS_SetSampleBufferCount
  477. Programs the Sample Buffer Count.
  478. ---------------------------------------------------------------------*/
  479. void PAS_SetSampleBufferCount
  480. (
  481. void
  482. )
  483. {
  484. int LoByte;
  485. int HiByte;
  486. int count;
  487. int data;
  488. unsigned flags;
  489. flags = DisableInterrupts();
  490. // Disable the Sample Buffer Count
  491. data = PAS_State->audiofilt;
  492. data &= ~SampleBufferCountGateFlag;
  493. PAS_Write( AudioFilterControl, data );
  494. PAS_State->audiofilt = data;
  495. // Select the Sample Buffer Count
  496. data = SelectSampleBufferCount;
  497. PAS_Write( LocalTimerControl, data );
  498. PAS_State->tmrctlr = data;
  499. count = PAS_TransferLength;
  500. // Check if we're using a 16-bit DMA channel
  501. if ( PAS_DMAChannel > 3 )
  502. {
  503. count >>= 1;
  504. }
  505. LoByte = lobyte( count );
  506. HiByte = hibyte( count );
  507. // Program the Sample Buffer Count
  508. PAS_Write( SampleBufferCount, LoByte );
  509. PAS_Write( SampleBufferCount, HiByte );
  510. PAS_State->samplecnt = count;
  511. RestoreInterrupts( flags );
  512. }
  513. /*---------------------------------------------------------------------
  514. Function: PAS_SetPlaybackRate
  515. Sets the rate at which the digitized sound will be played in
  516. hertz.
  517. ---------------------------------------------------------------------*/
  518. void PAS_SetPlaybackRate
  519. (
  520. unsigned rate
  521. )
  522. {
  523. if ( rate < PAS_MinSamplingRate )
  524. {
  525. rate = PAS_MinSamplingRate;
  526. }
  527. if ( rate > PAS_MaxSamplingRate )
  528. {
  529. rate = PAS_MaxSamplingRate;
  530. }
  531. PAS_TimeInterval = ( unsigned )CalcTimeInterval( rate );
  532. if ( PAS_MixMode & STEREO )
  533. {
  534. PAS_TimeInterval /= 2;
  535. }
  536. // Keep track of what the actual rate is
  537. PAS_SampleRate = CalcSamplingRate( PAS_TimeInterval );
  538. if ( PAS_MixMode & STEREO )
  539. {
  540. PAS_SampleRate /= 2;
  541. }
  542. }
  543. /*---------------------------------------------------------------------
  544. Function: PAS_GetPlaybackRate
  545. Returns the rate at which the digitized sound will be played in
  546. hertz.
  547. ---------------------------------------------------------------------*/
  548. unsigned PAS_GetPlaybackRate
  549. (
  550. void
  551. )
  552. {
  553. return( PAS_SampleRate );
  554. }
  555. /*---------------------------------------------------------------------
  556. Function: PAS_SetMixMode
  557. Sets the sound card to play samples in mono or stereo.
  558. ---------------------------------------------------------------------*/
  559. int PAS_SetMixMode
  560. (
  561. int mode
  562. )
  563. {
  564. mode &= PAS_MaxMixMode;
  565. // Check board revision. Revision # 0 can't play 16-bit data.
  566. if ( ( PAS_State->intrctlr & 0xe0 ) == 0 )
  567. {
  568. // Force the mode to 8-bit data.
  569. mode &= ~SIXTEEN_BIT;
  570. }
  571. PAS_MixMode = mode;
  572. PAS_SetPlaybackRate( PAS_SampleRate );
  573. return( mode );
  574. }
  575. /*---------------------------------------------------------------------
  576. Function: PAS_StopPlayback
  577. Ends the DMA transfer of digitized sound to the sound card.
  578. ---------------------------------------------------------------------*/
  579. void PAS_StopPlayback
  580. (
  581. void
  582. )
  583. {
  584. int data;
  585. // Don't allow anymore interrupts
  586. PAS_DisableInterrupt();
  587. // Stop the transfer of digital data
  588. data = PAS_State->crosschannel;
  589. data &= PAS_PCMStopMask;
  590. PAS_Write( CrossChannelControl, data );
  591. PAS_State->crosschannel = data;
  592. // Turn off 16-bit unsigned data
  593. data = PAS_Read( SampleSizeConfiguration );
  594. data &= PAS_SampleSizeMask;
  595. PAS_Write( SampleSizeConfiguration, data );
  596. // Disable the DMA channel
  597. DMA_EndTransfer( PAS_DMAChannel );
  598. PAS_SoundPlaying = FALSE;
  599. PAS_DMABuffer = NULL;
  600. }
  601. /*---------------------------------------------------------------------
  602. Function: PAS_SetupDMABuffer
  603. Programs the DMAC for sound transfer.
  604. ---------------------------------------------------------------------*/
  605. int PAS_SetupDMABuffer
  606. (
  607. char *BufferPtr,
  608. int BufferSize,
  609. int mode
  610. )
  611. {
  612. int DmaStatus;
  613. int data;
  614. // Enable PAS Dma
  615. data = PAS_State->crosschannel;
  616. data |= PAS_DMAEnable;
  617. PAS_Write( CrossChannelControl, data );
  618. PAS_State->crosschannel = data;
  619. DmaStatus = DMA_SetupTransfer( PAS_DMAChannel, BufferPtr, BufferSize, mode );
  620. if ( DmaStatus == DMA_Error )
  621. {
  622. PAS_SetErrorCode( PAS_DmaError );
  623. return( PAS_Error );
  624. }
  625. PAS_DMABuffer = BufferPtr;
  626. PAS_CurrentDMABuffer = BufferPtr;
  627. PAS_TotalDMABufferSize = BufferSize;
  628. PAS_DMABufferEnd = BufferPtr + BufferSize;
  629. return( PAS_Ok );
  630. }
  631. /*---------------------------------------------------------------------
  632. Function: PAS_GetCurrentPos
  633. Returns the offset within the current sound being played.
  634. ---------------------------------------------------------------------*/
  635. int PAS_GetCurrentPos
  636. (
  637. void
  638. )
  639. {
  640. char *CurrentAddr;
  641. int offset;
  642. if ( !PAS_SoundPlaying )
  643. {
  644. PAS_SetErrorCode( PAS_NoSoundPlaying );
  645. return( PAS_Error );
  646. }
  647. CurrentAddr = DMA_GetCurrentPos( PAS_DMAChannel );
  648. if ( CurrentAddr == NULL )
  649. {
  650. PAS_SetErrorCode( PAS_DmaError );
  651. return( PAS_Error );
  652. }
  653. offset = ( int )( ( ( unsigned long )CurrentAddr ) -
  654. ( ( unsigned long )PAS_CurrentDMABuffer ) );
  655. if ( PAS_MixMode & SIXTEEN_BIT )
  656. {
  657. offset >>= 1;
  658. }
  659. if ( PAS_MixMode & STEREO )
  660. {
  661. offset >>= 1;
  662. }
  663. return( offset );
  664. }
  665. /*---------------------------------------------------------------------
  666. Function: PAS_GetFilterSetting
  667. Returns the bit settings for the appropriate filter level.
  668. ---------------------------------------------------------------------*/
  669. int PAS_GetFilterSetting
  670. (
  671. int rate
  672. )
  673. {
  674. /* CD Quality 17897hz */
  675. if ( ( unsigned long )rate > ( unsigned long )17897L * 2 )
  676. {
  677. /* 00001b 20hz to 17.8khz */
  678. return( 0x01 );
  679. }
  680. /* Cassette Quality 15090hz */
  681. if ( ( unsigned long )rate > ( unsigned long )15909L * 2 )
  682. {
  683. /* 00010b 20hz to 15.9khz */
  684. return( 0x02 );
  685. }
  686. /* FM Radio Quality 11931hz */
  687. if ( ( unsigned long )rate > ( unsigned long )11931L * 2 )
  688. {
  689. /* 01001b 20hz to 11.9khz */
  690. return( 0x09 );
  691. }
  692. /* AM Radio Quality 8948hz */
  693. if ( ( unsigned long )rate > ( unsigned long )8948L * 2 )
  694. {
  695. /* 10001b 20hz to 8.9khz */
  696. return( 0x11 );
  697. }
  698. /* Telphone Quality 5965hz */
  699. if ( ( unsigned long )rate > ( unsigned long )5965L * 2 )
  700. {
  701. /* 00100b 20hz to 5.9khz */
  702. return( 0x19 );
  703. }
  704. /* Male voice quality 2982hz */
  705. /* 111001b 20hz to 2.9khz */
  706. return( 0x04 );
  707. }
  708. /*---------------------------------------------------------------------
  709. Function: PAS_BeginTransfer
  710. Starts playback of digitized sound on the sound card.
  711. ---------------------------------------------------------------------*/
  712. void PAS_BeginTransfer
  713. (
  714. int mode
  715. )
  716. {
  717. int data;
  718. PAS_SetSampleRateTimer();
  719. PAS_SetSampleBufferCount();
  720. PAS_EnableInterrupt();
  721. // Get sample size configuration
  722. data = PAS_Read( SampleSizeConfiguration );
  723. // Check board revision. Revision # 0 can't play 16-bit data.
  724. if ( PAS_State->intrctlr & 0xe0 )
  725. {
  726. data &= PAS_SampleSizeMask;
  727. // set sample size bit
  728. if ( PAS_MixMode & SIXTEEN_BIT )
  729. {
  730. data |= PAS_16BitSampleFlag;
  731. }
  732. }
  733. // set oversampling rate
  734. data &= PAS_OverSamplingMask;
  735. data |= PAS_4xOverSampling;
  736. // Set sample size configuration
  737. PAS_Write( SampleSizeConfiguration, data );
  738. // Get Cross channel setting
  739. data = PAS_State->crosschannel;
  740. data &= PAS_ChannelConnectMask;
  741. if ( mode == RECORD )
  742. {
  743. data |= PAS_PCMStartADC;
  744. }
  745. else
  746. {
  747. data |= PAS_PCMStartDAC;
  748. }
  749. // set stereo mode bit
  750. if ( !( PAS_MixMode & STEREO ) )
  751. {
  752. data |= PAS_StereoFlag;
  753. }
  754. PAS_Write( CrossChannelControl, data );
  755. PAS_State->crosschannel = data;
  756. // Get the filter appropriate filter setting
  757. data = PAS_GetFilterSetting( PAS_SampleRate );
  758. // Enable the Sample Rate Timer and Sample Buffer Count
  759. data |= SampleRateTimerGateFlag | SampleBufferCountGateFlag;
  760. if ( mode != RECORD )
  761. {
  762. // Enable audio (not Audio Mute)
  763. data |= PAS_AudioMuteFlag;
  764. }
  765. PAS_Write( AudioFilterControl, data );
  766. PAS_State->audiofilt = data;
  767. PAS_SoundPlaying = TRUE;
  768. }
  769. /*---------------------------------------------------------------------
  770. Function: PAS_BeginBufferedPlayback
  771. Begins multibuffered playback of digitized sound on the sound card.
  772. ---------------------------------------------------------------------*/
  773. int PAS_BeginBufferedPlayback
  774. (
  775. char *BufferStart,
  776. int BufferSize,
  777. int NumDivisions,
  778. unsigned SampleRate,
  779. int MixMode,
  780. void ( *CallBackFunc )( void )
  781. )
  782. {
  783. int DmaStatus;
  784. PAS_StopPlayback();
  785. PAS_SetMixMode( MixMode );
  786. PAS_SetPlaybackRate( SampleRate );
  787. PAS_TransferLength = BufferSize / NumDivisions;
  788. PAS_SetCallBack( CallBackFunc );
  789. DmaStatus = PAS_SetupDMABuffer( BufferStart, BufferSize, DMA_AutoInitRead );
  790. if ( DmaStatus == PAS_Error )
  791. {
  792. return( PAS_Error );
  793. }
  794. PAS_BeginTransfer( PLAYBACK );
  795. return( PAS_Ok );
  796. }
  797. /*---------------------------------------------------------------------
  798. Function: PAS_BeginBufferedRecord
  799. Begins multibuffered recording of digitized sound on the sound card.
  800. ---------------------------------------------------------------------*/
  801. int PAS_BeginBufferedRecord
  802. (
  803. char *BufferStart,
  804. int BufferSize,
  805. int NumDivisions,
  806. unsigned SampleRate,
  807. int MixMode,
  808. void ( *CallBackFunc )( void )
  809. )
  810. {
  811. int DmaStatus;
  812. PAS_StopPlayback();
  813. PAS_SetMixMode( MixMode );
  814. PAS_SetPlaybackRate( SampleRate );
  815. PAS_TransferLength = BufferSize / NumDivisions;
  816. PAS_SetCallBack( CallBackFunc );
  817. DmaStatus = PAS_SetupDMABuffer( BufferStart, BufferSize, DMA_AutoInitWrite );
  818. if ( DmaStatus == PAS_Error )
  819. {
  820. return( PAS_Error );
  821. }
  822. PAS_BeginTransfer( RECORD );
  823. return( PAS_Ok );
  824. }
  825. /*---------------------------------------------------------------------
  826. Function: PAS_CallInt
  827. Calls interrupt 2fh.
  828. ---------------------------------------------------------------------*/
  829. int PAS_CallInt( int ebx, int ecx, int edx );
  830. #pragma aux PAS_CallInt = \
  831. "int 2fh", \
  832. parm [ ebx ] [ ecx ] [ edx ] modify exact [ eax ebx ecx edx esi edi ] value [ ebx ];
  833. /*---------------------------------------------------------------------
  834. Function: PAS_CallMVFunction
  835. Performs a call to a real mode function.
  836. ---------------------------------------------------------------------*/
  837. int PAS_CallMVFunction
  838. (
  839. unsigned long function,
  840. int ebx,
  841. int ecx,
  842. int edx
  843. )
  844. {
  845. dpmi_regs callregs;
  846. int status;
  847. callregs.EBX = ebx;
  848. callregs.ECX = ecx;
  849. callregs.EDX = edx;
  850. callregs.SS = 0;
  851. callregs.SP = 0;
  852. callregs.DS = 0;
  853. callregs.ES = 0;
  854. callregs.FS = 0;
  855. callregs.GS = 0;
  856. callregs.IP = function;
  857. callregs.CS = function >> 16;
  858. status = DPMI_CallRealModeFunction( &callregs );
  859. if ( status != DPMI_Ok )
  860. {
  861. return( PAS_Error );
  862. }
  863. return( callregs.EBX & 0xff );
  864. }
  865. /*---------------------------------------------------------------------
  866. Function: PAS_SetPCMVolume
  867. Sets the volume of digitized sound playback.
  868. ---------------------------------------------------------------------*/
  869. int PAS_SetPCMVolume
  870. (
  871. int volume
  872. )
  873. {
  874. int status;
  875. volume = max( 0, volume );
  876. volume = min( volume, 255 );
  877. volume *= 100;
  878. volume /= 255;
  879. status = PAS_CallMVFunction( PAS_Func->SetMixer, volume,
  880. OUTPUTMIXER, L_PCM );
  881. if ( status == PAS_Error )
  882. {
  883. return( status );
  884. }
  885. status = PAS_CallMVFunction( PAS_Func->SetMixer, volume,
  886. OUTPUTMIXER, R_PCM );
  887. if ( status == PAS_Error )
  888. {
  889. return( status );
  890. }
  891. return( PAS_Ok );
  892. }
  893. /*---------------------------------------------------------------------
  894. Function: PAS_GetPCMVolume
  895. Returns the current volume of digitized sound playback.
  896. ---------------------------------------------------------------------*/
  897. int PAS_GetPCMVolume
  898. (
  899. void
  900. )
  901. {
  902. int leftvolume;
  903. int rightvolume;
  904. int totalvolume;
  905. if ( PAS_Func == NULL )
  906. {
  907. return( PAS_Error );
  908. }
  909. leftvolume = PAS_CallMVFunction( PAS_Func->GetMixer, 0,
  910. OUTPUTMIXER, L_PCM );
  911. rightvolume = PAS_CallMVFunction( PAS_Func->GetMixer, 0,
  912. OUTPUTMIXER, R_PCM );
  913. if ( ( leftvolume == PAS_Error ) || ( rightvolume == PAS_Error ) )
  914. {
  915. return( PAS_Error );
  916. }
  917. leftvolume &= 0xff;
  918. rightvolume &= 0xff;
  919. totalvolume = ( rightvolume + leftvolume ) / 2;
  920. totalvolume *= 255;
  921. totalvolume /= 100;
  922. return( totalvolume );
  923. }
  924. /*---------------------------------------------------------------------
  925. Function: PAS_SetFMVolume
  926. Sets the volume of FM sound playback.
  927. ---------------------------------------------------------------------*/
  928. void PAS_SetFMVolume
  929. (
  930. int volume
  931. )
  932. {
  933. volume = max( 0, volume );
  934. volume = min( volume, 255 );
  935. volume *= 100;
  936. volume /= 255;
  937. if ( PAS_Func )
  938. {
  939. PAS_CallMVFunction( PAS_Func->SetMixer, volume, OUTPUTMIXER, L_FM );
  940. PAS_CallMVFunction( PAS_Func->SetMixer, volume, OUTPUTMIXER, R_FM );
  941. }
  942. }
  943. /*---------------------------------------------------------------------
  944. Function: PAS_GetFMVolume
  945. Returns the current volume of FM sound playback.
  946. ---------------------------------------------------------------------*/
  947. int PAS_GetFMVolume
  948. (
  949. void
  950. )
  951. {
  952. int leftvolume;
  953. int rightvolume;
  954. int totalvolume;
  955. if ( PAS_Func == NULL )
  956. {
  957. return( 255 );
  958. }
  959. leftvolume = PAS_CallMVFunction( PAS_Func->GetMixer, 0,
  960. OUTPUTMIXER, L_FM ) & 0xff;
  961. rightvolume = PAS_CallMVFunction( PAS_Func->GetMixer, 0,
  962. OUTPUTMIXER, R_FM ) & 0xff;
  963. totalvolume = ( rightvolume + leftvolume ) / 2;
  964. totalvolume *= 255;
  965. totalvolume /= 100;
  966. totalvolume = min( 255, totalvolume );
  967. return( totalvolume );
  968. }
  969. /*---------------------------------------------------------------------
  970. Function: PAS_GetCardInfo
  971. Returns the maximum number of bits that can represent a sample
  972. (8 or 16) and the number of channels (1 for mono, 2 for stereo).
  973. ---------------------------------------------------------------------*/
  974. int PAS_GetCardInfo
  975. (
  976. int *MaxSampleBits,
  977. int *MaxChannels
  978. )
  979. {
  980. int status;
  981. if ( PAS_State == NULL )
  982. {
  983. status = PAS_CheckForDriver();
  984. if ( status != PAS_Ok )
  985. {
  986. return( status );
  987. }
  988. PAS_State = PAS_GetStateTable();
  989. if ( PAS_State == NULL )
  990. {
  991. return( PAS_Error );
  992. }
  993. }
  994. *MaxChannels = 2;
  995. // Check board revision. Revision # 0 can't play 16-bit data.
  996. if ( ( PAS_State->intrctlr & 0xe0 ) == 0 )
  997. {
  998. *MaxSampleBits = 8;
  999. }
  1000. else
  1001. {
  1002. *MaxSampleBits = 16;
  1003. }
  1004. return( PAS_Ok );
  1005. }
  1006. /*---------------------------------------------------------------------
  1007. Function: PAS_SetCallBack
  1008. Specifies the user function to call at the end of a sound transfer.
  1009. ---------------------------------------------------------------------*/
  1010. void PAS_SetCallBack
  1011. (
  1012. void ( *func )( void )
  1013. )
  1014. {
  1015. PAS_CallBack = func;
  1016. }
  1017. /*---------------------------------------------------------------------
  1018. Function: PAS_FindCard
  1019. Auto-detects the port the Pro AudioSpectrum is set for.
  1020. ---------------------------------------------------------------------*/
  1021. int PAS_FindCard
  1022. (
  1023. void
  1024. )
  1025. {
  1026. int status;
  1027. status = PAS_TestAddress( DEFAULT_BASE );
  1028. if ( status == 0 )
  1029. {
  1030. PAS_TranslateCode = DEFAULT_BASE;
  1031. return( PAS_Ok );
  1032. }
  1033. status = PAS_TestAddress( ALT_BASE_1 );
  1034. if ( status == 0 )
  1035. {
  1036. PAS_TranslateCode = ALT_BASE_1;
  1037. return( PAS_Ok );
  1038. }
  1039. status = PAS_TestAddress( ALT_BASE_2 );
  1040. if ( status == 0 )
  1041. {
  1042. PAS_TranslateCode = ALT_BASE_2;
  1043. return( PAS_Ok );
  1044. }
  1045. status = PAS_TestAddress( ALT_BASE_3 );
  1046. if ( status == 0 )
  1047. {
  1048. PAS_TranslateCode = ALT_BASE_3;
  1049. return( PAS_Ok );
  1050. }
  1051. PAS_SetErrorCode( PAS_CardNotFound );
  1052. return( PAS_Error );
  1053. }
  1054. /*---------------------------------------------------------------------
  1055. Function: PAS_SaveMusicVolume
  1056. Saves the user's FM mixer settings.
  1057. ---------------------------------------------------------------------*/
  1058. int PAS_SaveMusicVolume
  1059. (
  1060. void
  1061. )
  1062. {
  1063. int status;
  1064. int data;
  1065. if ( !PAS_Installed )
  1066. {
  1067. status = PAS_CheckForDriver();
  1068. if ( status != PAS_Ok )
  1069. {
  1070. return( status );
  1071. }
  1072. PAS_State = PAS_GetStateTable();
  1073. if ( PAS_State == NULL )
  1074. {
  1075. return( PAS_Error );
  1076. }
  1077. PAS_Func = PAS_GetFunctionTable();
  1078. if ( PAS_Func == NULL )
  1079. {
  1080. return( PAS_Error );
  1081. }
  1082. status = PAS_GetCardSettings();
  1083. if ( status != PAS_Ok )
  1084. {
  1085. return( status );
  1086. }
  1087. status = PAS_FindCard();
  1088. if ( status != PAS_Ok )
  1089. {
  1090. return( status );
  1091. }
  1092. // Enable PAS Sound
  1093. data = PAS_State->audiofilt;
  1094. data |= PAS_AudioMuteFlag;
  1095. PAS_Write( AudioFilterControl, data );
  1096. PAS_State->audiofilt = data;
  1097. }
  1098. status = PAS_CallMVFunction( PAS_Func->GetMixer, 0, OUTPUTMIXER, L_FM );
  1099. if ( status != PAS_Error )
  1100. {
  1101. PAS_OriginalFMLeftVolume = PAS_CallMVFunction( PAS_Func->GetMixer,
  1102. 0, OUTPUTMIXER, L_FM ) & 0xff;
  1103. PAS_OriginalFMRightVolume = PAS_CallMVFunction( PAS_Func->GetMixer,
  1104. 0, OUTPUTMIXER, R_FM ) & 0xff;
  1105. return( PAS_Ok );
  1106. }
  1107. return( PAS_Warning );
  1108. }
  1109. /*---------------------------------------------------------------------
  1110. Function: PAS_RestoreMusicVolume
  1111. Restores the user's FM mixer settings.
  1112. ---------------------------------------------------------------------*/
  1113. void PAS_RestoreMusicVolume
  1114. (
  1115. void
  1116. )
  1117. {
  1118. if ( PAS_Func )
  1119. {
  1120. PAS_CallMVFunction( PAS_Func->SetMixer, PAS_OriginalFMLeftVolume,
  1121. OUTPUTMIXER, L_FM );
  1122. PAS_CallMVFunction( PAS_Func->SetMixer, PAS_OriginalFMRightVolume,
  1123. OUTPUTMIXER, R_FM );
  1124. }
  1125. }
  1126. /*---------------------------------------------------------------------
  1127. Function: PAS_SaveState
  1128. Saves the original state of the PAS prior to use.
  1129. ---------------------------------------------------------------------*/
  1130. void PAS_SaveState
  1131. (
  1132. void
  1133. )
  1134. {
  1135. PAS_OriginalState.intrctlr = PAS_State->intrctlr;
  1136. PAS_OriginalState.audiofilt = PAS_State->audiofilt;
  1137. PAS_OriginalState.tmrctlr = PAS_State->tmrctlr;
  1138. PAS_OriginalState.samplerate = PAS_State->samplerate;
  1139. PAS_OriginalState.samplecnt = PAS_State->samplecnt;
  1140. PAS_OriginalState.crosschannel = PAS_State->crosschannel;
  1141. PAS_SampleSizeConfig = PAS_Read( SampleSizeConfiguration );
  1142. }
  1143. /*---------------------------------------------------------------------
  1144. Function: PAS_RestoreState
  1145. Restores the original state of the PAS after use.
  1146. ---------------------------------------------------------------------*/
  1147. void PAS_RestoreState
  1148. (
  1149. void
  1150. )
  1151. {
  1152. int LoByte;
  1153. int HiByte;
  1154. // Select the Sample Rate Timer
  1155. PAS_Write( LocalTimerControl, SelectSampleRateTimer );
  1156. PAS_State->tmrctlr = SelectSampleRateTimer;
  1157. PAS_Write( SampleRateTimer, PAS_OriginalState.samplerate );
  1158. PAS_State->samplerate = PAS_OriginalState.samplerate;
  1159. // Select the Sample Buffer Count
  1160. PAS_Write( LocalTimerControl, SelectSampleBufferCount );
  1161. PAS_State->tmrctlr = SelectSampleBufferCount;
  1162. LoByte = lobyte( PAS_OriginalState.samplecnt );
  1163. HiByte = hibyte( PAS_OriginalState.samplecnt );
  1164. PAS_Write( SampleRateTimer, LoByte );
  1165. PAS_Write( SampleRateTimer, HiByte );
  1166. PAS_State->samplecnt = PAS_OriginalState.samplecnt;
  1167. PAS_Write( CrossChannelControl, PAS_OriginalState.crosschannel );
  1168. PAS_State->crosschannel = PAS_OriginalState.crosschannel;
  1169. PAS_Write( SampleSizeConfiguration, PAS_SampleSizeConfig );
  1170. PAS_Write( InterruptControl, PAS_OriginalState.intrctlr );
  1171. PAS_State->intrctlr = PAS_OriginalState.intrctlr;
  1172. PAS_Write( AudioFilterControl, PAS_OriginalState.audiofilt );
  1173. PAS_State->audiofilt = PAS_OriginalState.audiofilt;
  1174. PAS_Write( LocalTimerControl, PAS_OriginalState.tmrctlr );
  1175. PAS_State->tmrctlr = PAS_OriginalState.tmrctlr;
  1176. }
  1177. /*---------------------------------------------------------------------
  1178. Function: PAS_LockEnd
  1179. Used for determining the length of the functions to lock in memory.
  1180. ---------------------------------------------------------------------*/
  1181. static void PAS_LockEnd
  1182. (
  1183. void
  1184. )
  1185. {
  1186. }
  1187. /*---------------------------------------------------------------------
  1188. Function: allocateTimerStack
  1189. Allocate a block of memory from conventional (low) memory and return
  1190. the selector (which can go directly into a segment register) of the
  1191. memory block or 0 if an error occured.
  1192. ---------------------------------------------------------------------*/
  1193. static unsigned short allocateTimerStack
  1194. (
  1195. unsigned short size
  1196. )
  1197. {
  1198. union REGS regs;
  1199. // clear all registers
  1200. memset( &regs, 0, sizeof( regs ) );
  1201. // DPMI allocate conventional memory
  1202. regs.w.ax = 0x100;
  1203. // size in paragraphs
  1204. regs.w.bx = ( size + 15 ) / 16;
  1205. int386( 0x31, &regs, &regs );
  1206. if (!regs.w.cflag)
  1207. {
  1208. // DPMI call returns selector in dx
  1209. // (ax contains real mode segment
  1210. // which is ignored here)
  1211. return( regs.w.dx );
  1212. }
  1213. // Couldn't allocate memory.
  1214. return( NULL );
  1215. }
  1216. /*---------------------------------------------------------------------
  1217. Function: deallocateTimerStack
  1218. Deallocate a block of conventional (low) memory given a selector to
  1219. it. Assumes the block was allocated with DPMI function 0x100.
  1220. ---------------------------------------------------------------------*/
  1221. static void deallocateTimerStack
  1222. (
  1223. unsigned short selector
  1224. )
  1225. {
  1226. union REGS regs;
  1227. if ( selector != NULL )
  1228. {
  1229. // clear all registers
  1230. memset( &regs, 0, sizeof( regs ) );
  1231. regs.w.ax = 0x101;
  1232. regs.w.dx = selector;
  1233. int386( 0x31, &regs, &regs );
  1234. }
  1235. }
  1236. /*---------------------------------------------------------------------
  1237. Function: PAS_Init
  1238. Initializes the sound card and prepares the module to play
  1239. digitized sounds.
  1240. ---------------------------------------------------------------------*/
  1241. int PAS_Init
  1242. (
  1243. void
  1244. )
  1245. {
  1246. int Interrupt;
  1247. int status;
  1248. int data;
  1249. if ( PAS_Installed )
  1250. {
  1251. return( PAS_Ok );
  1252. }
  1253. PAS_IntController1Mask = inp( 0x21 );
  1254. PAS_IntController2Mask = inp( 0xA1 );
  1255. status = PAS_CheckForDriver();
  1256. if ( status != PAS_Ok )
  1257. {
  1258. return( status );
  1259. }
  1260. PAS_State = PAS_GetStateTable();
  1261. if ( PAS_State == NULL )
  1262. {
  1263. return( PAS_Error );
  1264. }
  1265. PAS_Func = PAS_GetFunctionTable();
  1266. if ( PAS_Func == NULL )
  1267. {
  1268. return( PAS_Error );
  1269. }
  1270. status = PAS_GetCardSettings();
  1271. if ( status != PAS_Ok )
  1272. {
  1273. return( status );
  1274. }
  1275. status = PAS_FindCard();
  1276. if ( status != PAS_Ok )
  1277. {
  1278. return( status );
  1279. }
  1280. PAS_SaveState();
  1281. PAS_OriginalPCMLeftVolume = PAS_CallMVFunction( PAS_Func->GetMixer, 0,
  1282. OUTPUTMIXER, L_PCM ) & 0xff;
  1283. PAS_OriginalPCMRightVolume = PAS_CallMVFunction( PAS_Func->GetMixer, 0,
  1284. OUTPUTMIXER, R_PCM ) & 0xff;
  1285. PAS_SoundPlaying = FALSE;
  1286. PAS_SetCallBack( NULL );
  1287. PAS_DMABuffer = NULL;
  1288. status = PAS_LockMemory();
  1289. if ( status != PAS_Ok )
  1290. {
  1291. PAS_UnlockMemory();
  1292. return( status );
  1293. }
  1294. StackSelector = allocateTimerStack( kStackSize );
  1295. if ( StackSelector == NULL )
  1296. {
  1297. PAS_UnlockMemory();
  1298. PAS_SetErrorCode( PAS_OutOfMemory );
  1299. return( PAS_Error );
  1300. }
  1301. // Leave a little room at top of stack just for the hell of it...
  1302. StackPointer = kStackSize - sizeof( long );
  1303. // Install our interrupt handler
  1304. Interrupt = PAS_Interrupts[ PAS_Irq ];
  1305. PAS_OldInt = _dos_getvect( Interrupt );
  1306. if ( PAS_Irq < 8 )
  1307. {
  1308. _dos_setvect( Interrupt, PAS_ServiceInterrupt );
  1309. }
  1310. else
  1311. {
  1312. status = IRQ_SetVector( Interrupt, PAS_ServiceInterrupt );
  1313. if ( status != IRQ_Ok )
  1314. {
  1315. PAS_UnlockMemory();
  1316. deallocateTimerStack( StackSelector );
  1317. StackSelector = NULL;
  1318. PAS_SetErrorCode( PAS_UnableToSetIrq );
  1319. return( PAS_Error );
  1320. }
  1321. }
  1322. // Enable PAS Sound
  1323. data = PAS_State->audiofilt;
  1324. data |= PAS_AudioMuteFlag;
  1325. PAS_Write( AudioFilterControl, data );
  1326. PAS_State->audiofilt = data;
  1327. PAS_SetPlaybackRate( PAS_DefaultSampleRate );
  1328. PAS_SetMixMode( PAS_DefaultMixMode );
  1329. PAS_Installed = TRUE;
  1330. PAS_SetErrorCode( PAS_Ok );
  1331. return( PAS_Ok );
  1332. }
  1333. /*---------------------------------------------------------------------
  1334. Function: PAS_Shutdown
  1335. Ends transfer of sound data to the sound card and restores the
  1336. system resources used by the card.
  1337. ---------------------------------------------------------------------*/
  1338. void PAS_Shutdown
  1339. (
  1340. void
  1341. )
  1342. {
  1343. int Interrupt;
  1344. if ( PAS_Installed )
  1345. {
  1346. // Halt the DMA transfer
  1347. PAS_StopPlayback();
  1348. // Restore the original interrupt
  1349. Interrupt = PAS_Interrupts[ PAS_Irq ];
  1350. if ( PAS_Irq >= 8 )
  1351. {
  1352. IRQ_RestoreVector( Interrupt );
  1353. }
  1354. _dos_setvect( Interrupt, PAS_OldInt );
  1355. PAS_SoundPlaying = FALSE;
  1356. PAS_DMABuffer = NULL;
  1357. PAS_SetCallBack( NULL );
  1358. PAS_CallMVFunction( PAS_Func->SetMixer, PAS_OriginalPCMLeftVolume,
  1359. OUTPUTMIXER, L_PCM );
  1360. PAS_CallMVFunction( PAS_Func->SetMixer, PAS_OriginalPCMRightVolume,
  1361. OUTPUTMIXER, R_PCM );
  1362. // DEBUG
  1363. // PAS_RestoreState();
  1364. PAS_UnlockMemory();
  1365. deallocateTimerStack( StackSelector );
  1366. StackSelector = NULL;
  1367. PAS_Installed = FALSE;
  1368. }
  1369. }
  1370. /*---------------------------------------------------------------------
  1371. Function: PAS_UnlockMemory
  1372. Unlocks all neccessary data.
  1373. ---------------------------------------------------------------------*/
  1374. void PAS_UnlockMemory
  1375. (
  1376. void
  1377. )
  1378. {
  1379. DPMI_UnlockMemoryRegion( PAS_LockStart, PAS_LockEnd );
  1380. DPMI_Unlock( PAS_Interrupts );
  1381. DPMI_Unlock( PAS_OldInt );
  1382. DPMI_Unlock( PAS_IntController1Mask );
  1383. DPMI_Unlock( PAS_IntController2Mask );
  1384. DPMI_Unlock( PAS_Installed );
  1385. DPMI_Unlock( PAS_TranslateCode );
  1386. DPMI_Unlock( PAS_OriginalPCMLeftVolume );
  1387. DPMI_Unlock( PAS_OriginalPCMRightVolume );
  1388. DPMI_Unlock( PAS_OriginalFMLeftVolume );
  1389. DPMI_Unlock( PAS_OriginalFMRightVolume );
  1390. DPMI_Unlock( PAS_DMAChannel );
  1391. DPMI_Unlock( PAS_Irq );
  1392. DPMI_Unlock( PAS_State );
  1393. DPMI_Unlock( PAS_Func );
  1394. DPMI_Unlock( PAS_OriginalState );
  1395. DPMI_Unlock( PAS_SampleSizeConfig );
  1396. DPMI_Unlock( PAS_DMABuffer );
  1397. DPMI_Unlock( PAS_DMABufferEnd );
  1398. DPMI_Unlock( PAS_CurrentDMABuffer );
  1399. DPMI_Unlock( PAS_TotalDMABufferSize );
  1400. DPMI_Unlock( PAS_TransferLength );
  1401. DPMI_Unlock( PAS_MixMode );
  1402. DPMI_Unlock( PAS_SampleRate );
  1403. DPMI_Unlock( PAS_TimeInterval );
  1404. DPMI_Unlock( PAS_SoundPlaying );
  1405. DPMI_Unlock( PAS_CallBack );
  1406. DPMI_Unlock( PAS_ErrorCode );
  1407. DPMI_Unlock( irqstatus );
  1408. }
  1409. /*---------------------------------------------------------------------
  1410. Function: PAS_LockMemory
  1411. Locks all neccessary data.
  1412. ---------------------------------------------------------------------*/
  1413. int PAS_LockMemory
  1414. (
  1415. void
  1416. )
  1417. {
  1418. int status;
  1419. status = DPMI_LockMemoryRegion( PAS_LockStart, PAS_LockEnd );
  1420. status |= DPMI_Lock( PAS_Interrupts );
  1421. status |= DPMI_Lock( PAS_OldInt );
  1422. status |= DPMI_Lock( PAS_IntController1Mask );
  1423. status |= DPMI_Lock( PAS_IntController2Mask );
  1424. status |= DPMI_Lock( PAS_Installed );
  1425. status |= DPMI_Lock( PAS_TranslateCode );
  1426. status |= DPMI_Lock( PAS_OriginalPCMLeftVolume );
  1427. status |= DPMI_Lock( PAS_OriginalPCMRightVolume );
  1428. status |= DPMI_Lock( PAS_OriginalFMLeftVolume );
  1429. status |= DPMI_Lock( PAS_OriginalFMRightVolume );
  1430. status |= DPMI_Lock( PAS_DMAChannel );
  1431. status |= DPMI_Lock( PAS_Irq );
  1432. status |= DPMI_Lock( PAS_State );
  1433. status |= DPMI_Lock( PAS_Func );
  1434. status |= DPMI_Lock( PAS_OriginalState );
  1435. status |= DPMI_Lock( PAS_SampleSizeConfig );
  1436. status |= DPMI_Lock( PAS_DMABuffer );
  1437. status |= DPMI_Lock( PAS_DMABufferEnd );
  1438. status |= DPMI_Lock( PAS_CurrentDMABuffer );
  1439. status |= DPMI_Lock( PAS_TotalDMABufferSize );
  1440. status |= DPMI_Lock( PAS_TransferLength );
  1441. status |= DPMI_Lock( PAS_MixMode );
  1442. status |= DPMI_Lock( PAS_SampleRate );
  1443. status |= DPMI_Lock( PAS_TimeInterval );
  1444. status |= DPMI_Lock( PAS_SoundPlaying );
  1445. status |= DPMI_Lock( PAS_CallBack );
  1446. status |= DPMI_Lock( PAS_ErrorCode );
  1447. status |= DPMI_Lock( irqstatus );
  1448. if ( status != DPMI_Ok )
  1449. {
  1450. PAS_UnlockMemory();
  1451. PAS_SetErrorCode( PAS_DPMI_Error );
  1452. return( PAS_Error );
  1453. }
  1454. return( PAS_Ok );
  1455. }