home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / a2ixlibrary-1.0-bin.lha / share / a2ixlibrary / start.S < prev    next >
Text File  |  1996-10-12  |  8KB  |  295 lines

  1. /* definitions for the assembler startup file */
  2.  
  3. #include "a2ixlibrary.h"
  4.  
  5. /* Library IDs 0-19 can be freely used for private libraries, IDs 20 and up
  6.    are reserved */
  7.  
  8.     .globl    ___a4_offset
  9.     .equ    ___a4_offset, (-4 - 4 * LIBRARY_ID)
  10.  
  11. /* amazingly works, contains only defines ;-)) */
  12. #include <exec/alerts.h>
  13.  
  14. #define _LVOOpenLibrary        -0x228
  15. #define _LVOCloseLibrary     -0x19e
  16. #define _LVOAlert        -0x6c
  17. #define _LVOFreeMem        -0xd2
  18. #define _LVORemove        -0xfc
  19.  
  20. #define RTC_MATCHWORD    0x4afc
  21. #define RTF_AUTOINIT    (1<<7)
  22.  
  23. #define LIBF_CHANGED    (1<<1)
  24. #define LIBF_SUMUSED    (1<<2)
  25. /* seems there is an assembler bug in expression evaluation here.. */
  26. #define LIBF_CHANGED_SUMUSED 0x6
  27. #define LIBF_DELEXP    (1<<3)
  28. #define LIBB_DELEXP    3
  29.  
  30. #define LN_TYPE        8
  31. #define LN_NAME        10
  32. #define NT_LIBRARY    9
  33. #define MP_FLAGS    14
  34. #define PA_IGNORE    2
  35.  
  36. #define LIST_SIZEOF    14
  37.  
  38. #define THISTASK    276
  39.  
  40. #define INITBYTE(field,val)    .word 0xe000; .word (field); .byte (val); .byte 0
  41. #define INITWORD(field,val)    .word 0xd000; .word (field); .word (val)
  42. #define INITLONG(field,val)    .word 0xc000; .word (field); .long (val)
  43.  
  44. /*
  45.  * our library base.. 
  46.  */
  47.  
  48. /* struct library */
  49. #define    BASE_NODE        0
  50. #define BASE_FLAGS        14
  51. #define BASE_NEGSIZE        16
  52. #define BASE_POSSIZE        18
  53. #define BASE_VERSION        20
  54. #define BASE_REVISION        22
  55. #define BASE_IDSTRING        24
  56. #define BASE_SUM        28
  57. #define BASE_OPENCNT        32
  58. #define BASE_LIBRARY        34    /* size of library */
  59.  
  60. /* custom part */
  61. #define BASE_MYFLAGS        (BASE_LIBRARY + 0)
  62. #define BASE_COOKIE        (BASE_MYFLAGS + 2)
  63. #define BASE_SEGLIST        (BASE_MYFLAGS + 6)
  64. #define BASE_LIB_ID        (BASE_MYFLAGS + 10)
  65. #define BASE_SIZEOF        (BASE_MYFLAGS + 14)
  66.  
  67. #define PRIORITY    0
  68.  
  69.     .text
  70.  
  71.    | The first executable location.  This should return an error
  72.    | in case someone tried to run you as a program (instead of
  73.    | loading you as a library).
  74.     .globl    Start        | we use this to force inclusion of start.s
  75.     .globl    _SysBase
  76. Start:
  77.    movel   #-1,d0
  78.    rts
  79.  
  80. |-----------------------------------------------------------------------
  81. | A romtag structure.  Both "exec" and "ramlib" look for
  82. | this structure to discover magic constants about you
  83. | (such as where to start running you from...).
  84. |-----------------------------------------------------------------------
  85.  
  86. initDDescrip:
  87.               |STRUCTURE RT,0
  88.      .word    RTC_MATCHWORD      | UWORD RT_MATCHWORD
  89.      .long    initDDescrip       | APTR  RT_MATCHTAG
  90.      .long    EndCode            | APTR  RT_ENDSKIP
  91.      .byte    RTF_AUTOINIT       | UBYTE RT_FLAGS
  92.      .byte    VERSION            | UBYTE RT_VERSION
  93.      .byte    NT_LIBRARY         | UBYTE RT_TYPE
  94.      .byte    PRIORITY           | BYTE  RT_PRI
  95.      .long    Name             | APTR  RT_NAME
  96.      .long    idString           | APTR  RT_IDSTRING
  97.      .long    Init               | APTR  RT_INIT
  98. | this is just fool proof, and this library will never make it to ROM
  99. | anyway, so resident tags are not that important ;-)
  100. EndCode:
  101.  
  102.  
  103.    | this is the name that the library will have
  104. Name:        .asciz FULLNAME
  105.  
  106.    | this is an identifier tag to help in supporting the library
  107.    | format is 'name version.revision (dd.mm.yy),<cr>,<lf>,<null>'
  108.    | without any leading zeros in dd.mm.yy
  109. idString:
  110.     .ascii IDSTRING
  111.     .byte  13
  112.     .byte  10
  113.     .byte  0
  114.  
  115.    | force word alignment
  116.    .even
  117.  
  118.  
  119.    | The romtag specified that we were "RTF_AUTOINIT".  This means
  120.    | that the RT_INIT structure member points to one of these
  121.    | tables below.  If the AUTOINIT bit was not set then RT_INIT
  122.    | would point to a routine to run.
  123.  
  124. Init:
  125.    .long   BASE_SIZEOF        | size of library base data space
  126.    .long   funcTable        | pointer to function initializers
  127.    .long   dataTable            | pointer to data initializers
  128.    .long   initRoutine            | routine to run
  129.  
  130.  
  131. funcTable:
  132.  
  133.    |------ standard system routines
  134.    .long   Open
  135.    .long   Close
  136.    .long   Expunge
  137.    .long   Null
  138.  
  139.    |------ These two functions do all the hard work
  140.    .long   ___LibCloseInstance
  141.    .long   ___LibSetVarsInstance
  142.  
  143.    |------ function table end marker
  144.    .long   -1
  145.  
  146.    | The data table initializes static data structures.
  147.    | The format is specified in exec/InitStruct routines
  148.    | manual pages.  The INITBYTE/INITWORD/INITLONG routines
  149.    | are in the file "exec/initializers.i".  The first argument
  150.    | is the offset from the library base for this byte/word/long.
  151.    | The second argument is the value to put in that cell.
  152.    | The table is null terminated
  153.    | NOTE - LN_TYPE below is a correction - old example had LH_TYPE
  154.  
  155. dataTable:
  156.     INITBYTE (LN_TYPE,         NT_LIBRARY)
  157.     INITLONG (LN_NAME,         Name)
  158.     INITBYTE (BASE_FLAGS,         0x6) |LIBF_CHANGED_SUMUSED
  159.     INITWORD (BASE_VERSION,     VERSION)
  160.     INITWORD (BASE_REVISION,     REVISION)
  161.     INITLONG (BASE_IDSTRING,     idString)
  162.     .long   0
  163.  
  164.  
  165.    | This routine gets called after the library has been allocated.
  166.    | The library pointer is in D0.  The segment list is in A0.
  167.    | If it returns non-zero then the library will be linked into
  168.    | the library list.
  169. initRoutine:
  170.  
  171.    |------ get the library pointer into a convenient A register
  172.    movel   a5,sp@-
  173.    movel   d0,a5
  174.  
  175.    |------ save a pointer to our loaded code
  176.    movel   a0,a5@(BASE_SEGLIST)
  177.  
  178.    |------ Init the magic cookie and the library id
  179.    movel   #0x4a4a5600,a5@(BASE_COOKIE)        | 'JJV\0' = Jacob Johan Verkuil :-)
  180.    movel   #___a4_offset,a5@(BASE_LIB_ID)
  181.  
  182.    |------ Init SysBase
  183.    movel   4:w,a1
  184.    movel   a1,_SysBase
  185.  
  186.    movel   sp@+,a5
  187.    rts
  188.  
  189. |----------------------------------------------------------------------
  190. |
  191. | here begins the system interface commands.  When the user calls
  192. | OpenLibrary/CloseLibrary/RemoveLibrary, this eventually gets translated
  193. | into a call to the following routines (Open/Close/Expunge).  Exec
  194. | has already put our library pointer in A6 for us.  Exec has turned
  195. | off task switching while in these routines (via Forbid/Permit), so
  196. | we should not take too long in them.
  197. |
  198. |----------------------------------------------------------------------
  199.  
  200.  
  201.    | Open returns the library pointer in d0 if the open
  202.    | was successful.  If the open failed then null is returned.
  203.    | It might fail if we allocated memory on each open, or
  204.    | if only open application could have the library open
  205.    | at a time...
  206.  
  207. Open:      | ( libptr:a6, version:d0 )
  208.  
  209.  
  210.    |------ mark us as having another opener
  211.    addqw   #1,a6@(BASE_OPENCNT)
  212.  
  213.    |------ prevent delayed expunges
  214.    bclr    #LIBB_DELEXP,a6@(BASE_FLAGS)
  215.  
  216.    |------ do other things in C
  217.    pea       a6@
  218.    jsr       _lib_open
  219.    addqw   #4,sp
  220.    |--- lib_open() should return the library base, if all ok
  221.  
  222.    rts
  223.  
  224.    | There are two different things that might be returned from
  225.    | the Close routine.  If the library is no longer open and
  226.    | there is a delayed expunge then Close should return the
  227.    | segment list (as given to Init).  Otherwise close should
  228.    | return NULL.
  229.  
  230. Close:      | ( libptr:a6 )
  231.  
  232.    |------ mark us as having one fewer openers
  233.    subqw   #1,a6@(BASE_OPENCNT)
  234.  
  235.    |------ see if there is anyone left with us open
  236.    bne     Null
  237.  
  238.    |------ see if we have a delayed expunge pending
  239.    btst    #LIBB_DELEXP,a6@(BASE_FLAGS)
  240.    bne     Expunge
  241.  
  242.    | reserved entry
  243.  
  244. Null:
  245.    moveq   #0,d0
  246.    rts
  247.  
  248.    | There are two different things that might be returned from
  249.    | the Expunge routine.  If the library is no longer open
  250.    | then Expunge should return the segment list (as given to
  251.    | Init).  Otherwise Expunge should set the delayed expunge
  252.    | flag and return NULL.
  253.    |
  254.    | One other important note: because Expunge is called from
  255.    | the memory allocator, it may NEVER Wait() or otherwise
  256.    | take long time to complete.
  257.  
  258. Expunge:   | ( libptr: a6 )
  259.    moveml  a2/a5/a6,sp@-
  260.    movel   a6,a5
  261.  
  262.    |------ assume we cannot expunge
  263.    subal   a2,a2
  264.    bset    #LIBB_DELEXP,a5@(BASE_FLAGS)
  265.  
  266.    |------ see if anyone has us open
  267.    tstw    a5@(BASE_OPENCNT)
  268.    bne     L21
  269.  
  270.    |------ go ahead and get rid of us.  Store our seglist in a2
  271.    movel   a5@(BASE_SEGLIST),a2
  272.    movel   4:w,a6
  273.  
  274.    |------ unlink from library list
  275.    movel   a5,a1
  276.    jsr       a6@(_LVORemove)
  277.  
  278.    |------ free our memory
  279.    movel   a5,a1
  280.    moveq   #0,d0
  281.    movew   a5@(BASE_NEGSIZE),d0
  282.    subl    d0,a1
  283.    addw    a5@(BASE_POSSIZE),d0
  284.    jsr       a6@(_LVOFreeMem)
  285.  
  286. L21:       |------ set up our return value
  287.    movel   a2,d0
  288.  
  289.    moveml  sp@+,a2/a5/a6
  290.    rts
  291.  
  292. .data
  293. _SysBase:
  294.     .long 0
  295.