home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / ahisrc / device / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-11  |  7.0 KB  |  291 lines

  1. /* $Id: misc.c,v 4.18 1999/10/11 21:48:03 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 <stdarg.h>
  27.  
  28. #include <exec/lists.h>
  29. #include <exec/nodes.h>
  30. #include <powerup/ppclib/memory.h>
  31. #include <powerup/ppclib/interface.h>
  32. #include <powerup/ppclib/object.h>
  33. #include <powerpc/powerpc.h>
  34. #include <powerpc/memoryPPC.h>
  35. #include <intuition/intuition.h>
  36. #include <proto/exec.h>
  37. #include <proto/intuition.h>
  38. #include <proto/ppc.h>
  39. #include <proto/powerpc.h>
  40.  
  41. #include "ahi_def.h"
  42. #include "header.h"
  43. #include "elfloader.h"
  44.  
  45.  
  46. /******************************************************************************
  47. ** Findnode *******************************************************************
  48. ******************************************************************************/
  49.  
  50. // Find a node in a list
  51.  
  52. struct Node *
  53. FindNode ( struct List *list,
  54.            struct Node *node )
  55. {
  56.   struct Node *currentnode;
  57.  
  58.   for(currentnode = list->lh_Head;
  59.       currentnode->ln_Succ;
  60.       currentnode = currentnode->ln_Succ)
  61.   {
  62.     if(currentnode == node)
  63.     {
  64.       return currentnode;
  65.     }
  66.   }
  67.   return NULL;
  68. }
  69.  
  70.  
  71. /******************************************************************************
  72. ** Fixed2Shift ****************************************************************
  73. ******************************************************************************/
  74.  
  75. // Returns host many steps to right-shift a value to approximate
  76. // scaling with the Fixed argument.
  77.  
  78. int
  79. Fixed2Shift( Fixed f )
  80. {
  81.   int   i   = 0;
  82.   Fixed ref = 0x10000;
  83.  
  84.   while( f < ref )
  85.   {
  86.     i++;
  87.     ref >>= 1;
  88.   }
  89.  
  90.   return i;
  91. }
  92.  
  93. /******************************************************************************
  94. ** Req ************************************************************************
  95. ******************************************************************************/
  96.  
  97. void
  98. Req( const char* text, ... )
  99. {
  100.   struct EasyStruct es = 
  101.   {
  102.     sizeof (struct EasyStruct),
  103.     0,
  104.     (STRPTR) DevName,
  105.     (STRPTR) text,
  106.     "OK"
  107.   };
  108.  
  109.   va_list ap;
  110.  
  111.   va_start( ap, text );
  112.  
  113.   EasyRequestArgs( NULL, &es, NULL, ap );
  114.   
  115.   va_end( ap );
  116. }
  117.  
  118. /******************************************************************************
  119. ** AHIAllocVec ****************************************************************
  120. ******************************************************************************/
  121.  
  122. APTR
  123. AHIAllocVec( ULONG byteSize, ULONG requirements )
  124. {
  125.   switch( MixBackend )
  126.   {
  127.     case MB_NATIVE:
  128.       return AllocVec( byteSize, requirements & ~MEMF_PPCMASK );
  129.  
  130.     case MB_POWERUP:
  131.       return PPCAllocVec( byteSize, requirements );
  132.  
  133.     case MB_WARPUP:
  134.     {
  135.       ULONG new_requirements;
  136.       void* v;
  137.  
  138.       new_requirements = requirements & ~MEMF_PPCMASK;
  139.  
  140.       if( requirements & ( MEMF_WRITETHROUGHPPC | MEMF_WRITETHROUGHM68K ) )
  141.       {
  142.         Req( "Internal error: Illegal memory attribute in AHIAllocVec()." );
  143.       }
  144.  
  145.       if( requirements & 
  146.           ( MEMF_NOCACHEPPC | MEMF_NOCACHEM68K |
  147.             MEMF_NOCACHESYNCPPC | MEMF_NOCACHESYNCM68K ) )
  148.       {
  149.         new_requirements |= MEMF_CHIP;            // Sucks!
  150.       }
  151.  
  152.       v = AllocVec32( byteSize, new_requirements );
  153.       CacheClearU();
  154.       return v;
  155.     }
  156.   }
  157.  
  158.   Req( "Internal error: Unknown MixBackend in AHIAllocVec()." );
  159.   return NULL;
  160. }
  161.  
  162. /******************************************************************************
  163. ** AHIFreeVec *****************************************************************
  164. ******************************************************************************/
  165.  
  166. void
  167. AHIFreeVec( APTR memoryBlock )
  168. {
  169.   switch( MixBackend )
  170.   {
  171.     case MB_NATIVE:
  172.       FreeVec( memoryBlock );
  173.       return;
  174.  
  175.     case MB_POWERUP:
  176.       PPCFreeVec( memoryBlock );
  177.       return;
  178.  
  179.     case MB_WARPUP:
  180.       FreeVec32( memoryBlock );
  181.       return;
  182.   }
  183.  
  184.   Req( "Internal error: Unknown MixBackend in AHIFreeVec()." );
  185. }
  186.  
  187.  
  188. /******************************************************************************
  189. ** AHILoadObject **************************************************************
  190. ******************************************************************************/
  191.  
  192. void*
  193. AHILoadObject( const char* objname )
  194. {
  195.   switch( MixBackend )
  196.   {
  197.     case MB_NATIVE:
  198.       Req( "Internal error: Illegal MixBackend in AHILoadObject()" );
  199.       return NULL;
  200.  
  201.     case MB_POWERUP:
  202.       return PPCLoadObject( (char*) objname );
  203.  
  204.     case MB_WARPUP:
  205.     {
  206.       void* o;
  207.  
  208.       o = ELFLoadObject( objname );
  209.       CacheClearU();
  210.  
  211.       return o;
  212.     }
  213.   }    
  214.  
  215.   Req( "Internal error: Unknown MixBackend in AHILoadObject()." );
  216.   return NULL;
  217. }
  218.  
  219. /******************************************************************************
  220. ** AHIUnLoadObject ************************************************************
  221. ******************************************************************************/
  222.  
  223. void
  224. AHIUnloadObject( void* obj )
  225. {
  226.   switch( MixBackend )
  227.   {
  228.     case MB_NATIVE:
  229.       Req( "Internal error: Illegal MixBackend in AHIUnloadObject()" );
  230.       return;
  231.  
  232.     case MB_POWERUP:
  233.       PPCUnLoadObject( obj );
  234.       return;
  235.  
  236.     case MB_WARPUP:
  237.       ELFUnLoadObject( obj );
  238.       return;
  239.   }
  240.  
  241.   Req( "Internal error: Unknown MixBackend in AHIUnloadObject()" );
  242. }
  243.  
  244. /******************************************************************************
  245. ** AHIGetELFSymbol ************************************************************
  246. ******************************************************************************/
  247.  
  248. BOOL
  249. AHIGetELFSymbol( const char* name,
  250.                  void** ptr )
  251. {
  252.   switch( MixBackend )
  253.   {
  254.     case MB_NATIVE:
  255.       Req( "Internal error: Illegal MixBackend in AHIUnloadObject()" );
  256.       return FALSE;
  257.  
  258.     case MB_POWERUP:
  259.     {
  260.       struct PPCObjectInfo oi =
  261.       {
  262.         0,
  263.         NULL,
  264.         PPCELFINFOTYPE_SYMBOL,
  265.         STT_SECTION,
  266.         STB_GLOBAL,
  267.         0
  268.       };
  269.       
  270.       struct TagItem tag_done =
  271.       {
  272.         TAG_DONE, 0
  273.       };
  274.  
  275.       BOOL rc;
  276.  
  277.       oi.Name = (char*) name;
  278.       rc = PPCGetObjectAttrs( PPCObject, &oi, &tag_done );
  279.       *ptr = (void*) oi.Address;
  280.       
  281.       return rc;
  282.     }
  283.  
  284.     case MB_WARPUP:
  285.       return ELFGetSymbol( PPCObject, name, ptr );
  286.   }
  287.  
  288.   Req( "Internal error: Unknown MixBackend in AHIUnloadObject()" );
  289.   return FALSE;
  290. }
  291.