home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 106 / EnigmaAmiga106CD.iso / software / sviluppo / ahisrc / device / header.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  10.8 KB  |  458 lines

  1. /* $Id: header.c,v 1.15 1999/09/11 20:35:29 lcs Exp $ */
  2.  
  3. /*
  4.      AHI - Hardware independent audio subsystem
  5.      Copyright (C) 1996-1999 Martin Blom <martin@blom.org>
  6.      
  7.      This library is free software; you can redistribute it and/or
  8.      modify it under the terms of the GNU Library General Public
  9.      License as published by the Free Software Foundation; either
  10.      version 2 of the License, or (at your option) any later version.
  11.      
  12.      This library is distributed in the hope that it will be useful,
  13.      but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.      Library General Public License for more details.
  16.      
  17.      You should have received a copy of the GNU Library General Public
  18.      License along with this library; if not, write to the
  19.      Free Software Foundation, Inc., 59 Temple Place - Suite 330, Cambridge,
  20.      MA 02139, USA.
  21. */
  22.  
  23. #include <config.h>
  24. #include <CompilerSpecific.h>
  25.  
  26. #include <exec/resident.h>
  27. #include <exec/alerts.h>
  28. #include <exec/execbase.h>
  29. #include <proto/exec.h>
  30.  
  31. #ifndef VERSION68K
  32. # include <powerup/ppclib/object.h>
  33. # include <powerup/ppclib/interface.h>
  34. # include <proto/ppc.h>
  35. #endif
  36.  
  37. #include "ahi_def.h"
  38. #include "version.h"
  39. #include "device.h"
  40. #include "localize.h"
  41. #include "misc.h"
  42.  
  43. static BOOL
  44. OpenLibs ( void );
  45.  
  46. static void
  47. CloseLibs ( void );
  48.  
  49.  
  50. /******************************************************************************
  51. ** Device entry ***************************************************************
  52. ******************************************************************************/
  53.  
  54. int Start( void )
  55. {
  56.   return -1;
  57. }
  58.  
  59.  
  60. /******************************************************************************
  61. ** Device residend strcuture **************************************************
  62. ******************************************************************************/
  63.  
  64. extern void _etext;
  65. extern const char DevName[];
  66. extern const char IDString[];
  67. static const APTR InitTable[4];
  68.  
  69. static const struct Resident RomTag =
  70. {
  71.   RTC_MATCHWORD,
  72.   (struct Resident *) &RomTag,
  73.   (APTR) &_etext,
  74.   RTF_AUTOINIT,
  75.   VERSION,
  76.   NT_DEVICE,
  77.   0,                      /* priority */
  78.   (BYTE *) DevName,
  79.   (BYTE *) IDString,
  80.   (APTR) &InitTable
  81. };
  82.  
  83.  
  84. /******************************************************************************
  85. ** Globals ********************************************************************
  86. ******************************************************************************/
  87.  
  88. struct ExecBase           *SysBase        = NULL;
  89. struct AHIBase            *AHIBase        = NULL;
  90. struct DosLibrary         *DOSBase        = NULL;
  91. struct Library            *GadToolsBase   = NULL;
  92. struct GfxBase            *GfxBase        = NULL;
  93. struct Library            *IFFParseBase   = NULL;
  94. struct IntuitionBase      *IntuitionBase  = NULL;
  95. struct LocaleBase         *LocaleBase     = NULL;
  96. struct Device             *TimerBase      = NULL;
  97. struct UtilityBase        *UtilityBase    = NULL;
  98. #ifndef VERSION68K
  99. struct Library            *PowerPCBase    = NULL;
  100. struct Library            *PPCLibBase     = NULL;
  101. void                      *PPCObject      = NULL;
  102. #endif
  103.  
  104. /* linker can use symbol b for symbol a if a is not defined */
  105. #define ALIAS(a,b) asm(".stabs \"_" #a "\",11,0,0,0\n.stabs \"_" #b "\",1,0,0,0")
  106.  
  107. ALIAS( __UtilityBase, UtilityBase );
  108.  
  109. const ULONG                       DriverVersion  = 2;
  110. const ULONG                       Version        = VERSION;
  111. const ULONG                       Revision       = REVISION;
  112.  
  113. const char DevName[]   = AHINAME;
  114. const char IDString[]  = "ahi.device " VERS "\r\n";
  115. static const char VersTag[] =
  116.  "$VER: ahi.device " VERS " ©1994-1999 Martin Blom. "
  117.  CPU 
  118. #ifndef VERSION68K
  119.  "/PPC"
  120. #endif
  121.  " version.\r\n";
  122.  
  123.  
  124. /******************************************************************************
  125. ** Device code ****************************************************************
  126. ******************************************************************************/
  127.  
  128. static struct AHIBase * ASMCALL
  129. initRoutine( REG( d0, struct AHIBase* device ),
  130.          REG( a0, APTR seglist ),
  131.          REG( a6, struct ExecBase* sysbase ) )
  132. {
  133.   SysBase = sysbase;
  134.   AHIBase = device;
  135.  
  136.   AHIBase->ahib_Library.lib_Node.ln_Type = NT_DEVICE;
  137.   AHIBase->ahib_Library.lib_Node.ln_Name = (STRPTR) DevName;
  138.   AHIBase->ahib_Library.lib_Flags        = LIBF_SUMUSED | LIBF_CHANGED;
  139.   AHIBase->ahib_Library.lib_Version      = VERSION;
  140.   AHIBase->ahib_Library.lib_Revision     = REVISION;
  141.   AHIBase->ahib_Library.lib_IdString     = (STRPTR) IDString;
  142.   AHIBase->ahib_SysLib  = sysbase;
  143.   AHIBase->ahib_SegList = (ULONG) seglist;
  144.  
  145. #ifdef MC68020_PLUS
  146.   if( ( SysBase->AttnFlags & AFF_68020 ) == 0 )
  147.   {
  148.     Alert( ( AN_Unknown | ACPU_InstErr ) & (~AT_DeadEnd) );
  149.     return NULL;
  150.   }
  151. #endif
  152.  
  153.   InitSemaphore( &AHIBase->ahib_Lock );
  154.  
  155.   if( !OpenLibs() )
  156.   {
  157.     return NULL;
  158.   }
  159.  
  160.   return AHIBase;
  161. }
  162.  
  163.  
  164. BPTR ASMCALL
  165. DevExpunge( REG( a6, struct AHIBase* device ) )
  166. {
  167.   BPTR seglist = 0;
  168.  
  169.   device->ahib_Library.lib_Flags |= LIBF_DELEXP;
  170.  
  171.   if( device->ahib_Library.lib_OpenCnt == 0)
  172.   {
  173.     seglist = device->ahib_SegList;
  174.  
  175.     Remove( (struct Node *) device );
  176.  
  177.     CloseLibs();
  178.  
  179.     FreeMem( (APTR) ( ( (char*) device ) - device->ahib_Library.lib_NegSize ),
  180.          device->ahib_Library.lib_NegSize + device->ahib_Library.lib_PosSize );
  181.   }
  182.  
  183.   return seglist;
  184. }
  185.  
  186.  
  187. int Null( void )
  188. {
  189.   return 0;
  190. }
  191.  
  192.  
  193. extern APTR DevOpen;
  194. extern APTR DevClose;
  195.  
  196. extern APTR DevBeginIO;
  197. extern APTR DevAbortIO;
  198.  
  199. extern APTR AllocAudioA;
  200. extern APTR FreeAudio;
  201. extern APTR KillAudio;
  202. extern APTR ControlAudioA;
  203. extern APTR SetVol;
  204. extern APTR SetFreq;
  205. extern APTR SetSound;
  206. extern APTR SetEffect;
  207. extern APTR LoadSound;
  208. extern APTR UnloadSound;
  209. extern APTR NextAudioID;
  210. extern APTR GetAudioAttrsA;
  211. extern APTR BestAudioIDA;
  212. extern APTR AllocAudioRequestA;
  213. extern APTR AudioRequestA;
  214. extern APTR FreeAudioRequest;
  215. extern APTR PlayA;
  216. extern APTR SampleFrameSize;
  217. extern APTR AddAudioMode;
  218. extern APTR RemoveAudioMode;
  219. extern APTR LoadModeFile;
  220.  
  221.  
  222. static const APTR funcTable[] =
  223. {
  224.   &DevOpen,
  225.   &DevClose,
  226.   &DevExpunge,
  227.   &Null,
  228.  
  229.   &DevBeginIO,
  230.   &DevAbortIO,
  231.  
  232.   &AllocAudioA,
  233.   &FreeAudio,
  234.   &KillAudio,
  235.   &ControlAudioA,
  236.   &SetVol,
  237.   &SetFreq,
  238.   &SetSound,
  239.   &SetEffect,
  240.   &LoadSound,
  241.   &UnloadSound,
  242.   &NextAudioID,
  243.   &GetAudioAttrsA,
  244.   &BestAudioIDA,
  245.   &AllocAudioRequestA,
  246.   &AudioRequestA,
  247.   &FreeAudioRequest,
  248.   &PlayA,
  249.   &SampleFrameSize,
  250.   &AddAudioMode,
  251.   &RemoveAudioMode,
  252.   &LoadModeFile,
  253.   (APTR) -1
  254. };
  255.  
  256.  
  257. static const APTR InitTable[4] =
  258. {
  259.   (APTR) sizeof( struct AHIBase ),
  260.   (APTR) &funcTable,
  261.   0,
  262.   (APTR) initRoutine
  263. };
  264.  
  265.  
  266. static struct timerequest *TimerIO        = NULL;
  267. static struct timeval     *timeval        = NULL;
  268.  
  269. /******************************************************************************
  270. ** OpenLibs *******************************************************************
  271. ******************************************************************************/
  272.  
  273. // This function is called by the device startup code when the device is
  274. // first loaded into memory.
  275.  
  276. static BOOL
  277. OpenLibs ( void )
  278. {
  279.   /* Intuition Library */
  280.  
  281.   IntuitionBase = (struct IntuitionBase *) OpenLibrary( "intuition.library", 37 );
  282.  
  283.   if( IntuitionBase == NULL)
  284.   {
  285.     Alert(AN_Unknown|AG_OpenLib|AO_Intuition);
  286.     return FALSE;
  287.   }
  288.  
  289.   /* DOS Library */
  290.  
  291.   DOSBase = (struct DosLibrary *) OpenLibrary( "dos.library", 37 );
  292.  
  293.   if( DOSBase == NULL)
  294.   {
  295.     Req( "Unable to open dos.library." );
  296.     return FALSE;
  297.   }
  298.  
  299.   /* Graphics Library */
  300.  
  301.   GfxBase = (struct GfxBase *) OpenLibrary( "graphics.library", 37 );
  302.  
  303.   if( GfxBase == NULL)
  304.   {
  305.     Req( "Unable to open graphics.library." );
  306.     return FALSE;
  307.   }
  308.  
  309.   /* GadTools Library */
  310.  
  311.   GadToolsBase = OpenLibrary( "gadtools.library", 37 );
  312.  
  313.   if( GadToolsBase == NULL)
  314.   {
  315.     Req( "Unable to open gadtools.library." );
  316.     return FALSE;
  317.   }
  318.  
  319.   /* IFFParse Library */
  320.  
  321.   IFFParseBase = OpenLibrary( "iffparse.library", 37 );
  322.  
  323.   if( IFFParseBase == NULL)
  324.   {
  325.     Req( "Unable to open iffparse.library." );
  326.     return FALSE;
  327.   }
  328.  
  329.   /* Locale Library */
  330.  
  331.   LocaleBase = (struct LocaleBase*) OpenLibrary( "locale.library", 38 );
  332.  
  333.   /* Timer Device */
  334.  
  335.   TimerIO = (struct timerequest *) AllocVec( sizeof(struct timerequest),
  336.                                              MEMF_PUBLIC | MEMF_CLEAR );
  337.  
  338.   if( TimerIO == NULL)
  339.   {
  340.     Req( "Out of memory." );
  341.     return FALSE;
  342.   }
  343.  
  344.   timeval = (struct timeval *) AllocVec( sizeof(struct timeval),
  345.                                          MEMF_PUBLIC | MEMF_CLEAR);
  346.  
  347.   if( timeval == NULL)
  348.   {
  349.     Req( "Out of memory." );
  350.     return FALSE;
  351.   }
  352.  
  353.   if( OpenDevice( "timer.device",
  354.                   UNIT_MICROHZ,
  355.                   (struct IORequest *)
  356.                   TimerIO,
  357.                   0) != 0 )
  358.   {
  359.     Req( "Unable to open timer.device." );
  360.     return FALSE;
  361.   }
  362.  
  363.   TimerBase = (struct Device *) TimerIO->tr_node.io_Device;
  364.  
  365.   /* Utility Library */
  366.  
  367.   UtilityBase = (struct UtilityBase *) OpenLibrary( "utility.library", 37 );
  368.  
  369.   if( UtilityBase == NULL)
  370.   {
  371.     Req( "Unable to open utility.library." );
  372.     return FALSE;
  373.   }
  374.  
  375.  
  376. #ifndef VERSION68K
  377.   /* PPC/PowerPC library */
  378.   /* This code preferes PowerUp, unless WarpUp is already in memory */
  379.  
  380.   // Check if WarpUp already installed...
  381.  
  382. //  Forbid();
  383. //  PowerPCBase = (struct Library *) FindName( &SysBase->LibList,
  384. //                                             "powerpc.library" );
  385. //  Permit();
  386.  
  387.  
  388. //  if( PowerPCBase != NULL )
  389. //  {
  390. //    // If so, open it properly.
  391. //    PowerPCBase = OpenLibrary( "powerpc.library", 14 );
  392. //  }
  393. //  else
  394.   {
  395.     // If not, try PowerUp (pray it's not the emulator!).
  396.     PPCLibBase = OpenLibrary( "ppc.library", 46 );
  397.   }
  398.  
  399. //  if( PPCLibBase == NULL )
  400. //  {
  401. //    // If WarpUp was not loaded and PowerUp could not be opened, try
  402. //    // WarpUp.
  403. //    PowerPCBase = OpenLibrary( "powerpc.library", 14 );
  404. //  }
  405.  
  406.   if( PPCLibBase != NULL || PowerPCBase != NULL )
  407.   {
  408.     /* Load our code to PPC..  */
  409.  
  410.     PPCObject = AHILoadObject( "DEVS:ahi.elf" );
  411.   }
  412.  
  413. #endif
  414.  
  415.   OpenahiCatalog(NULL, NULL);
  416.  
  417.   return TRUE;
  418. }
  419.  
  420.  
  421. /******************************************************************************
  422. ** CloseLibs *******************************************************************
  423. ******************************************************************************/
  424.  
  425. // This function is called by DevExpunge() when the device is about to be
  426. // flushed
  427.  
  428. static void
  429. CloseLibs ( void )
  430. {
  431.   CloseahiCatalog();
  432.  
  433. #ifndef VERSION68K
  434.   if( PPCObject != NULL )
  435.   {
  436.     AHIUnLoadObject( PPCObject );
  437.   }
  438.  
  439.   CloseLibrary( PPCLibBase );
  440.   CloseLibrary( PowerPCBase );
  441. #endif
  442.  
  443.   CloseLibrary( (struct Library *) UtilityBase );
  444.   if( TimerIO  != NULL )
  445.   {
  446.     CloseDevice( (struct IORequest *) TimerIO );
  447.   }
  448.   FreeVec( timeval );
  449.   FreeVec( TimerIO );
  450.   CloseLibrary( (struct Library *) LocaleBase );
  451.   CloseLibrary( (struct Library *) IntuitionBase );
  452.   CloseLibrary( IFFParseBase );
  453.   CloseLibrary( GadToolsBase );
  454.   CloseLibrary( (struct Library *) GfxBase );
  455.   CloseLibrary( (struct Library *) DOSBase );
  456.  
  457. }
  458.