home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / comm / zedzap05.lha / latticelib.a < prev    next >
Text File  |  1992-11-26  |  10KB  |  259 lines

  1. ; Latticelib.a: Amiga shared runtime library shell for Lattice 'C' 5.02+
  2. ; By Rick Huebner, 28 October 1989.  Released to the Public Domain.
  3. ; Derived from sample.library.asm in ROM Kernal Reference Manual and other
  4. ; sources.
  5.  
  6. ; Provides RomTag and other magic thingies needed to make a set of
  7. ; C routines into a library.  Should work with most any set of routines;
  8. ; just substitute in the proper external labels.  This file should be the
  9. ; first one given in the link list, so that the RomTag struct can be quickly
  10. ; & easily found by the system when the library is opened.
  11.  
  12. ; 5.04 note: I just got done trying to use the new libent and libinit modules
  13. ; provided with Lattice C 5.04, and ran into a few problems.  I'm probably
  14. ; not using them quite right (the update docs are pretty sketchy), but I'm
  15. ; not going to waste any more time on them.  I'd already written and tested
  16. ; this stuff before 5.04 came, and it works well.  If it ain't broke...
  17.  
  18.         INCLUDE "exec/types.i"
  19.         INCLUDE "exec/libraries.i"
  20.         INCLUDE "exec/lists.i"
  21.         INCLUDE "exec/initializers.i"
  22.         INCLUDE "exec/resident.i"
  23.  
  24.         ; Things we define for external reference (for easier debuggging)
  25.         XDEF    LibStart
  26.         XDEF    RomTag
  27.         XDEF    LibName
  28.         XDEF    LibIDString
  29.         XDEF    LibInit
  30.         XDEF    LibOpen
  31.         XDEF    LibClose
  32.         XDEF    LibExpunge
  33.         XDEF    LibExtFunc
  34.         XDEF    XProtocolCleanup
  35.         XDEF    XProtocolSetup
  36.         XDEF    XProtocolSend
  37.         XDEF    XProtocolReceive
  38.         XDEF    XProtocolHostMon
  39.         XDEF    XProtocolUserMon
  40.         XDEF    LibEnd
  41.  
  42.         ; Our library base structure description
  43.         STRUCTURE XPRBase,LIB_SIZE
  44.         ULONG   xb_SegList
  45.         LABEL   XPRBase_SIZEOF
  46.  
  47.  
  48.         SECTION text,code
  49.  
  50.         ; Things we need others to define
  51.         XREF    @XProtocolCleanup
  52.         XREF    @XProtocolSetup
  53.         XREF    @XProtocolSend
  54.         XREF    @XProtocolReceive
  55.         XREF    @XProtocolHostMon
  56.         XREF    @XProtocolUserMon
  57.         XREF    __BSSBAS
  58.         XREF    __BSSLEN
  59.         XREF    _AbsExecBase
  60.         XREF    _LVOOpenLibrary
  61.         XREF    _LVOCloseLibrary
  62.         XREF    _LVORemove
  63.         XREF    _LVOFreeMem
  64.  
  65.  
  66.   ; seems to be missing??
  67. SysBase    ds.b 4
  68.  
  69.         ; Supposed start of executable code.  This is where execution
  70.         ; will start if anybody's silly enough to try and run the library
  71.         ; from the command line.  Just return the highest error code we
  72.         ; conveniently can to tell them they screwed up.
  73. LibStart:
  74.         moveq   #$7F,d0
  75.         rts
  76.  
  77.         ; Where the magic begins.  OpenLibrary actually looks through the
  78.         ; library file contents trying to find this table by locating the
  79.         ; magic RTC_MATCHWORD value followed by its own address.  This
  80.         ; table then tells OpenLibrary where to find other things it needs.
  81.         ; This needs to be close to the front of your library file to cut
  82.         ; down on the amount of searching OpenLibrary does; that's why
  83.         ; this object file should be first in the link list.
  84. RomTag: dc.w    RTC_MATCHWORD   ; Magic value to search for to find table
  85.         dc.l    RomTag          ; Address of matchword; the two together prevent accidental matches
  86.         dc.l    LibEnd          ; Address of end of library handler code
  87.         dc.b    RTF_AUTOINIT    ; Request system to automatically initialize our library
  88.         dc.b    VERSION         ; Version number of our library (defined below)
  89.         dc.b    NT_LIBRARY      ; Node type = Library
  90.         dc.b    0               ; Node priority = 0 (normal)
  91.         dc.l    LibName         ; Name of this library file, for debugging info
  92.         dc.l    LibIDString     ; More debugging info
  93.         dc.l    inittable       ; Initialization table used by RTF_AUTOINIT
  94.  
  95. VERSION  EQU    0
  96. REVISION EQU    90
  97. LibName:
  98.         dc.b    "xprzedzap.library",0
  99. LibIDString:
  100.         dc.b    "XprZedZap 0.90 (26 Nov 1992)",13,10,0
  101.  
  102.         ds.w    0
  103.  
  104. inittable:
  105.         dc.l    XPRBase_SIZEOF  ; Size of our library base struct
  106.         dc.l    functable       ; Where our function addresses are
  107.         dc.l    datatable       ; Initialization info for our library base struct
  108.         dc.l    LibInit         ; Library initialization routine address
  109.  
  110. functable:
  111.         dc.l    LibOpen                 ; Addresses of all library functions
  112.         dc.l    LibClose                ; First 4 MUST be provided, in this order
  113.         dc.l    LibExpunge
  114.         dc.l    LibExtFunc
  115.         dc.l    XProtocolCleanup        ; The meat of the library.
  116.         dc.l    XProtocolSetup
  117.         dc.l    XProtocolSend
  118.         dc.l    XProtocolReceive
  119.         dc.l    XProtocolHostMon
  120.         dc.l    XProtocolUserMon
  121.         dc.l    -1
  122.  
  123.         ; Things for the system to automatically initialize for us via RTF_AUTOINIT request
  124. datatable:
  125.         INITBYTE        LN_TYPE,NT_LIBRARY
  126.         INITLONG        LN_NAME,LibName
  127.         INITBYTE        LIB_FLAGS,LIBF_SUMUSED | LIBF_CHANGED
  128.         INITWORD        LIB_VERSION,VERSION
  129.         INITWORD        LIB_REVISION,REVISION
  130.         INITLONG        LIB_IDSTRING,LibIDString
  131.         dc.l    0
  132.  
  133.  
  134.         ; System interface routines.  Exec has performed Forbid() before
  135.         ; getting here, so do this stuff quick and get the hell out.
  136.  
  137.  
  138.         ; Routine which is executed by the system as part of first OpenLibrary
  139.         ; call, when the library is first loaded into RAM from disk.
  140.         ; D0 = library base pointer, A0 = segment list pointer.  Must return
  141.         ; nonzero in D0 if initialization worked, or zero if it failed.
  142. LibInit:
  143.         move.l  d0,-(sp)                ; Save D0 for return code
  144.         move.l  d0,a1                   ; A1 = library base
  145.         move.l  a0,xb_SegList(a1)       ; Save seglist addr for future expunge
  146.         lea     __BSSBAS,a0             ; Initialize BSS memory to 0s
  147.         move.l  #__BSSLEN,d0
  148.         moveq   #0,d1
  149.         bra.s   clr_lp
  150. clr_bss move.l  d1,(a0)+
  151. clr_lp  dbf     d0,clr_bss
  152.         move.l  (sp)+,d0                ; Return nonzero = success
  153.         rts
  154.  
  155.         ; Open the library.  Executed by system as part of OpenLibrary call,
  156.         ; after loading the library into RAM and calling LibInit if necessary.
  157.         ; D0 = version, A6 = library base.  Return nonzero if open worked,
  158.         ; or zero if open failed for some reason.
  159. LibOpen:
  160.         addq.w  #1,LIB_OPENCNT(a6)              ; Increment open count
  161.         bclr    #LIBB_DELEXP,LIB_FLAGS(a6)      ; Clear delayed expunge flag
  162.         move.l  a6,d0                           ; Return nonzero = success
  163.         rts
  164.  
  165.         ; Close the library.  Executed by system as part of CloseLibrary call.
  166.         ; A6 = library base.  Return seglist address if a delayed expunge
  167.         ; was performed, else return 0 if library is still loaded.
  168. LibClose:
  169.         moveq   #0,d0                           ; Init return value = 0
  170.         subq.w  #1,LIB_OPENCNT(a6)              ; Decrement open count
  171.         bne.s   dontexpunge                     ; If still open by others, can't expunge
  172.         btst    #LIBB_DELEXP,LIB_FLAGS(a6)      ; Was an expunge requested while we were open?
  173.         beq.s   dontexpunge
  174.         bsr.s   LibExpunge                      ; If so, do it now
  175. dontexpunge:
  176.         rts
  177.  
  178.         ; Expunge the library (remove it from RAM).  Executed by system
  179.         ; memory allocation routine when memory gets tight.  A6 = library
  180.         ; base.  Return seglist address if library was closed and we were
  181.         ; able to expunge, else return 0 if library is still loaded.
  182. LibExpunge:
  183.         movem.l d2/a5/a6,-(sp)
  184.         move.l  a6,a5
  185.         move.l  _AbsExecBase,a6                 ; Get ExecBase for future use
  186.         tst.w   LIB_OPENCNT(a5)                 ; Is library open?
  187.         beq.s   doexpunge
  188.         bset    #LIBB_DELEXP,LIB_FLAGS(a5)      ; If so, set delayed expunge flag
  189.         moveq   #0,d0                           ; and return 0
  190.         bra.s   expungedone
  191. doexpunge:
  192.         move.l  xb_SegList(a5),d2               ; Save seglist address in D2
  193.         move.l  a5,a1                           ; A1 = library base address
  194.         jsr     _LVORemove(a6)                  ; Unlink library node from system library list
  195.         moveq   #0,d0                           ; Compute total size of library node
  196.         move.w  LIB_NEGSIZE(a5),d0              ;   D0 = bytes used before base
  197.         move.l  a5,a1
  198.         sub.l   d0,a1                           ;   A1 = base - negsize
  199.         add.w   LIB_POSSIZE(a5),d0              ;   D0 = possize + negsize
  200.         jsr     _LVOFreeMem(a6)                 ; Free library node memory
  201.         move.l  d2,d0                           ; Return segment list address
  202. expungedone:
  203.         movem.l (sp)+,d2/a5/a6
  204.         rts
  205.  
  206.         ; "Extra" function (room for future growth?).  We don't need it,
  207.         ; so dummy it out.
  208. LibExtFunc:
  209.         moveq   #0,d0
  210.         rts
  211.  
  212.  
  213.  
  214. ; Added glue to protect the comm program from having us munge its registers
  215. ; when it calls our code, due to bug which caused us to munge the A6
  216. ; register in an earlier version.  Since the XPR spec requires the arguments
  217. ; to be passed in the same registers that Lattice uses for function call
  218. ; register arguments, all we have to do is save the non-changeable registers
  219. ; on entry and restore them on exit.
  220.  
  221. XProtocolCleanup:
  222.         movem.l d2-d7/a2-a6,-(sp)
  223.         jsr     @XProtocolCleanup
  224.         movem.l (sp)+,d2-d7/a2-a6
  225.         rts
  226.  
  227. XProtocolSetup:
  228.         movem.l d2-d7/a2-a6,-(sp)
  229.         jsr     @XProtocolSetup
  230.         movem.l (sp)+,d2-d7/a2-a6
  231.         rts
  232.  
  233. XProtocolSend:
  234.         movem.l d2-d7/a2-a6,-(sp)
  235.         jsr     @XProtocolSend
  236.         movem.l (sp)+,d2-d7/a2-a6
  237.         rts
  238.  
  239. XProtocolReceive:
  240.         movem.l d2-d7/a2-a6,-(sp)
  241.         jsr     @XProtocolReceive
  242.         movem.l (sp)+,d2-d7/a2-a6
  243.         rts
  244.  
  245. XProtocolHostMon:
  246.         movem.l d2-d7/a2-a6,-(sp)
  247.         jsr     @XProtocolHostMon
  248.         movem.l (sp)+,d2-d7/a2-a6
  249.         rts
  250.  
  251. XProtocolUserMon:
  252.         movem.l d2-d7/a2-a6,-(sp)
  253.         jsr     @XProtocolUserMon
  254.         movem.l (sp)+,d2-d7/a2-a6
  255.         rts
  256.  
  257. LibEnd:
  258.         END
  259.