home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / samplelibrary / library / sample.library.asm < prev   
Assembly Source File  |  1992-09-03  |  11KB  |  331 lines

  1. *******************************************************************************************
  2. *   sample.library.asm -- Example run-time library source code
  3. *
  4. *   Assemble and link, without startup code, to create Sample.library,
  5. *     a LIBS: drawer run-time shared library
  6. *
  7. *  Linkage Info:
  8. *  FROM     sample.library.o
  9. *  LIBRARY  LIB:Amiga.lib
  10. *  TO       sample.library
  11. *
  12. *BLink from sample.library.o  LIB LIB:amiga.lib TO sample.library
  13. * Copyright (c) 1992 Commodore-Amiga, Inc.
  14. *
  15. * This example is provided in electronic form by Commodore-Amiga, Inc. for
  16. * use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  17. * published by Addison-Wesley (ISBN 0-201-56774-1).
  18. *
  19. * The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  20. * information on the correct usage of the techniques and operating system
  21. * functions presented in these examples.  The source and executable code
  22. * of these examples may only be distributed in free electronic form, via
  23. * bulletin board or as part of a fully non-commercial and freely
  24. * redistributable diskette.  Both the source and executable code (including
  25. * comments) must be included, without modification, in any copy.  This
  26. * example may not be published in printed form or distributed with any
  27. * commercial product.  However, the programming techniques and support
  28. * routines set forth in these examples may be used in the development
  29. * of original executable software products for Commodore Amiga computers.
  30. *
  31. * All other rights reserved.
  32. *
  33. * This example is provided "as-is" and is subject to change; no
  34. * warranties are made.  All use is at your own risk. No liability or
  35. * responsibility is assumed.
  36. *******************************************************************************************
  37.  
  38.    SECTION   code
  39.  
  40.    NOLIST
  41.    INCLUDE "exec/types.i"
  42.    INCLUDE "exec/initializers.i"
  43.    INCLUDE "exec/libraries.i"
  44.    INCLUDE "exec/lists.i"
  45.    INCLUDE "exec/alerts.i"
  46.    INCLUDE "exec/resident.i"
  47.    INCLUDE "libraries/dos.i"
  48.  
  49.    INCLUDE "/sampleinclude/asmsupp.i"
  50.    INCLUDE "/sampleinclude/samplebase.i"
  51.    INCLUDE "/sampleinclude/sample_rev.i"
  52.  
  53.    LIST
  54.  
  55.    XDEF   InitTable                    ;------ These don't have to be external but it helps
  56.    XDEF   Open                         ;------ some debuggers to have them globally visible
  57.    XDEF   Close
  58.    XDEF   Expunge
  59.    XDEF   Null
  60.    XDEF   LibName
  61.    XDEF   Double
  62.    XDEF   AddThese
  63.  
  64.    XREF   _AbsExecBase
  65.  
  66.    XLIB   OpenLibrary
  67.    XLIB   CloseLibrary
  68.    XLIB   Alert
  69.    XLIB   FreeMem
  70.    XLIB   Remove
  71.  
  72.    ; The first executable location.  This should return an error in case someone tried to
  73.    ; run you as a program (instead of loading you as a library).
  74. Start:
  75.    MOVEQ   #-1,d0
  76.    rts
  77.  
  78. ;------------------------------------------------------------------------------------------
  79. ; A romtag structure.  Both "exec" and "ramlib" look for this structure to discover magic
  80. ; constants about you (such as where to start running you from...).  The include file
  81. ; sample_rev.i (created by hand or preferable with the developer tool ``bumprev''
  82. ; resolves the VERSION, REVISION, and VSTRING.
  83. ;------------------------------------------------------------------------------------------
  84.    ; Few people will need a priority and should leave it at zero.  The RT_PRI field is used
  85.    ; in configuring the ROMs.  Use "mods" from wack to look at other romtags in the system.
  86. MYPRI   EQU   0
  87.  
  88. RomTag:
  89.                ;STRUCTURE RT,0
  90.      DC.W    RTC_MATCHWORD      ; UWORD RT_MATCHWORD
  91.      DC.L    RomTag             ; APTR  RT_MATCHTAG
  92.      DC.L    EndCode            ; APTR  RT_ENDSKIP
  93.      DC.B    RTF_AUTOINIT       ; UBYTE RT_FLAGS
  94.      DC.B    VERSION            ; UBYTE RT_VERSION  (defined in sample_rev.i)
  95.      DC.B    NT_LIBRARY         ; UBYTE RT_TYPE
  96.      DC.B    MYPRI              ; BYTE  RT_PRI
  97.      DC.L    LibName            ; APTR  RT_NAME
  98.      DC.L    IDString           ; APTR  RT_IDSTRING
  99.      DC.L    InitTable          ; APTR  RT_INIT  table for InitResident()
  100.  
  101.    ; this is the name that the library will have
  102. LibName:   SAMPLENAME
  103.    ; standard name/version/date ID string from bumprev-created sample_rev.i
  104. IDString:  VSTRING
  105.  
  106. dosName:   DOSNAME
  107.  
  108.    ; force word alignment
  109.    ds.w   0
  110.  
  111.    ; The romtag specified that we were "RTF_AUTOINIT".  This means that the RT_INIT
  112.    ; structure member points to one of these tables below.  If the AUTOINIT bit was not
  113.    ; set then RT_INIT would point to a routine to run.
  114.  
  115. InitTable:
  116.    DC.L   SampleBase_SIZEOF ; size of library base data space
  117.    DC.L   funcTable         ; pointer to function initializers
  118.    DC.L   dataTable         ; pointer to data initializers
  119.    DC.L   initRoutine       ; routine to run
  120.  
  121.  
  122. funcTable:
  123.  
  124.    ;------ standard system routines
  125.    dc.l   Open
  126.    dc.l   Close
  127.    dc.l   Expunge
  128.    dc.l   Null
  129.  
  130.    ;------ my libraries definitions
  131.    dc.l   Double
  132.    dc.l   AddThese
  133.  
  134.    ;------ function table end marker
  135.    dc.l   -1
  136.  
  137.  
  138.    ; The data table initializes static data structures.  The format is specified in
  139.    ; exec/InitStruct routine's manual pages.  The INITBYTE/INITWORD/INITLONG routines are
  140.    ; in the file "exec/initializers.i".  The first argument is the offset from the library
  141.    ; base for this byte/word/long.  The second argument is the value to put in that cell.
  142.    ; The table is null terminated.
  143.    ; NOTE - LN_TYPE below is a correction - old example had LH_TYPE.
  144.  
  145. dataTable:
  146.         INITBYTE        LN_TYPE,NT_LIBRARY
  147.         INITLONG        LN_NAME,LibName
  148.         INITBYTE        LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
  149.         INITWORD        LIB_VERSION,VERSION
  150.         INITWORD        LIB_REVISION,REVISION
  151.         INITLONG        LIB_IDSTRING,IDString
  152.         DC.L   0
  153.  
  154.    ; This routine gets called after the library has been allocated.  The library pointer is
  155.    ; in D0.  The segment list is in A0.  If it returns non-zero then the library will be
  156.    ; linked into the library list.
  157.  
  158. initRoutine:
  159.  
  160.    ;------ get the library pointer into a convenient A register
  161.    move.l   a5,-(sp)
  162.    move.l   d0,a5
  163.  
  164.    ;------ save a pointer to exec
  165.    move.l   a6,sb_SysLib(a5)
  166.  
  167.    ;------ save a pointer to our loaded code
  168.    move.l   a0,sb_SegList(a5)
  169.  
  170.    ;------ open the dos library
  171.    lea   dosName(pc),a1
  172.    CLEAR   d0
  173.    CALLSYS   OpenLibrary
  174.  
  175.    move.l   d0,sb_DosLib(a5)
  176.    bne.s   1$
  177.  
  178.    ;------ can't open the dos!  what gives
  179.    ALERT   AG_OpenLib!AO_DOSLib
  180.  
  181. 1$:
  182.    ;------ now build the static data that we need
  183.    ;
  184.    ; put your initialization here...
  185.    ;
  186.  
  187.    move.l   a5,d0
  188.    move.l   (sp)+,a5
  189.    rts
  190.  
  191. ;------------------------------------------------------------------------------------------
  192. ; here begins the system interface commands.  When the user calls OpenLibrary/CloseLibrary/
  193. ; RemoveLibrary, this eventually gets translated into a call to the following routines
  194. ; (Open/Close/Expunge).  Exec has already put our library pointer in A6 for us.  Exec has
  195. ; turned off task switching while in these routines (via Forbid/Permit), so we should not
  196. ; take too long in them.
  197. ;------------------------------------------------------------------------------------------
  198.  
  199.    ; Open returns the library pointer in d0 if the open was successful.  If the open failed
  200.    ; then null is returned.  It might fail if we allocated memory on each open, or if only
  201.    ; open application could have the library open at a time...
  202.  
  203. Open:      ; ( libptr:a6, version:d0 )
  204.  
  205.    ;------ mark us as having another opener
  206.    addq.w   #1,LIB_OPENCNT(a6)
  207.  
  208.    ;------ prevent delayed expunges
  209.    bclr   #LIBB_DELEXP,sb_Flags(a6)
  210.  
  211.    move.l   a6,d0
  212.    rts
  213.  
  214.    ; There are two different things that might be returned from the Close routine.  If the
  215.    ; library is no longer open and there is a delayed expunge then Close should return the
  216.    ; segment list (as given to Init).  Otherwise close should return NULL.
  217.  
  218. Close:      ; ( libptr:a6 )
  219.  
  220.    ;------ set the return value
  221.    CLEAR   d0
  222.  
  223.    ;------ mark us as having one fewer openers
  224.    subq.w   #1,LIB_OPENCNT(a6)
  225.  
  226.    ;------ see if there is anyone left with us open
  227.    bne.s   1$
  228.  
  229.    ;------ see if we have a delayed expunge pending
  230.    btst   #LIBB_DELEXP,sb_Flags(a6)
  231.    beq.s   1$
  232.  
  233.    ;------ do the expunge
  234.