GUS.C 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. file: GUS.C
  17. author: James R. Dose
  18. date: September 7, 1994
  19. Gravis Ultrasound initialization routines.
  20. (c) Copyright 1994 James R. Dose. All Rights Reserved.
  21. **********************************************************************/
  22. #include <conio.h>
  23. #include <dos.h>
  24. #include <stdio.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "usrhooks.h"
  30. #include "interrup.h"
  31. #include "newgf1.h"
  32. #include "gusmidi.h"
  33. #include "guswave.h"
  34. #include "_guswave.h"
  35. #define TRUE ( 1 == 1 )
  36. #define FALSE ( !TRUE )
  37. // size of DMA buffer for patch loading
  38. #define DMABUFFSIZE 2048U
  39. struct gf1_dma_buff GUS_HoldBuffer;
  40. static int HoldBufferAllocated = FALSE;
  41. static int GUS_Installed = 0;
  42. extern VoiceNode GUSWAVE_Voices[ VOICES ];
  43. extern int GUSWAVE_Installed;
  44. unsigned long GUS_TotalMemory;
  45. int GUS_MemConfig;
  46. int GUS_AuxError = 0;
  47. int GUS_ErrorCode = GUS_Ok;
  48. #define GUS_SetErrorCode( status ) \
  49. GUS_ErrorCode = ( status );
  50. /*---------------------------------------------------------------------
  51. Function: GUS_ErrorString
  52. Returns a pointer to the error message associated with an error
  53. number. A -1 returns a pointer the current error.
  54. ---------------------------------------------------------------------*/
  55. char *GUS_ErrorString
  56. (
  57. int ErrorNumber
  58. )
  59. {
  60. char *ErrorString;
  61. switch( ErrorNumber )
  62. {
  63. case GUS_Warning :
  64. case GUS_Error :
  65. ErrorString = GUS_ErrorString( GUS_ErrorCode );
  66. break;
  67. case GUS_Ok :
  68. ErrorString = "Ultrasound music ok.";
  69. break;
  70. case GUS_OutOfMemory :
  71. ErrorString = "Out of memory in GusMidi.";
  72. break;
  73. case GUS_OutOfDosMemory :
  74. ErrorString = "Out of conventional (640K) memory in GusMidi.";
  75. break;
  76. case GUS_GF1Error :
  77. ErrorString = gf1_error_str( GUS_AuxError );
  78. break;
  79. case GUS_InvalidIrq :
  80. ErrorString = "Ultrasound IRQ must be 7 or less.";
  81. break;
  82. case GUS_ULTRADIRNotSet :
  83. ErrorString = "ULTRADIR environment variable not set.";
  84. break;
  85. case GUS_MissingConfig :
  86. // ErrorString = "Can't find GUSMIDI.INI file.";
  87. ErrorString = "Can't find ULTRAMID.INI file.";
  88. break;
  89. case GUS_FileError :
  90. ErrorString = strerror( GUS_AuxError );
  91. break;
  92. default :
  93. ErrorString = "Unknown Ultrasound error code.";
  94. break;
  95. }
  96. return( ErrorString );
  97. }
  98. /*---------------------------------------------------------------------
  99. Function: D32DosMemAlloc
  100. Allocate a block of Conventional memory.
  101. ---------------------------------------------------------------------*/
  102. void *D32DosMemAlloc
  103. (
  104. unsigned size
  105. )
  106. {
  107. union REGS r;
  108. // DPMI allocate DOS memory
  109. r.x.eax = 0x0100;
  110. // Number of paragraphs requested
  111. r.x.ebx = ( size + 15 ) >> 4;
  112. int386( 0x31, &r, &r );
  113. if ( r.x.cflag )
  114. {
  115. // Failed
  116. return( NULL );
  117. }
  118. return( ( void * )( ( r.x.eax & 0xFFFF ) << 4 ) );
  119. }
  120. /*---------------------------------------------------------------------
  121. Function: GUS_Init
  122. Initializes the Gravis Ultrasound for sound and music playback.
  123. ---------------------------------------------------------------------*/
  124. int GUS_Init
  125. (
  126. void
  127. )
  128. {
  129. struct load_os os;
  130. int ret;
  131. if ( GUS_Installed > 0 )
  132. {
  133. GUS_Installed++;
  134. return( GUS_Ok );
  135. }
  136. GUS_SetErrorCode( GUS_Ok );
  137. GUS_Installed = 0;
  138. GetUltraCfg( &os );
  139. if ( os.forced_gf1_irq > 7 )
  140. {
  141. GUS_SetErrorCode( GUS_InvalidIrq );
  142. return( GUS_Error );
  143. }
  144. if ( !HoldBufferAllocated )
  145. {
  146. GUS_HoldBuffer.vptr = D32DosMemAlloc( DMABUFFSIZE );
  147. if ( GUS_HoldBuffer.vptr == NULL )
  148. {
  149. GUS_SetErrorCode( GUS_OutOfDosMemory );
  150. return( GUS_Error );
  151. }
  152. GUS_HoldBuffer.paddr = ( unsigned long )GUS_HoldBuffer.vptr;
  153. HoldBufferAllocated = TRUE;
  154. }
  155. os.voices = 24;
  156. ret = gf1_load_os( &os );
  157. if ( ret )
  158. {
  159. GUS_AuxError = ret;
  160. GUS_SetErrorCode( GUS_GF1Error );
  161. return( GUS_Error );
  162. }
  163. GUS_TotalMemory = gf1_mem_avail();
  164. GUS_MemConfig = ( GUS_TotalMemory - 1 ) >> 18;
  165. GUS_Installed = 1;
  166. return( GUS_Ok );
  167. }
  168. /*---------------------------------------------------------------------
  169. Function: GUS_Shutdown
  170. Ends use of the Gravis Ultrasound. Must be called the same number
  171. of times as GUS_Init.
  172. ---------------------------------------------------------------------*/
  173. void GUS_Shutdown
  174. (
  175. void
  176. )
  177. {
  178. if ( GUS_Installed > 0 )
  179. {
  180. GUS_Installed--;
  181. if ( GUS_Installed == 0 )
  182. {
  183. gf1_unload_os();
  184. }
  185. }
  186. }
  187. /*---------------------------------------------------------------------
  188. Function: GUSWAVE_Shutdown
  189. Terminates use of the Gravis Ultrasound for digitized sound playback.
  190. ---------------------------------------------------------------------*/
  191. void GUSWAVE_Shutdown
  192. (
  193. void
  194. )
  195. {
  196. int i;
  197. if ( GUSWAVE_Installed )
  198. {
  199. GUSWAVE_KillAllVoices();
  200. // free memory
  201. for ( i = 0; i < VOICES; i++ )
  202. {
  203. if ( GUSWAVE_Voices[ i ].mem != NULL )
  204. {
  205. gf1_free( GUSWAVE_Voices[ i ].mem );
  206. GUSWAVE_Voices[ i ].mem = NULL;
  207. }
  208. }
  209. GUS_Shutdown();
  210. GUSWAVE_Installed = FALSE;
  211. }
  212. }