home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 300.lha / xprzmodem.library_v2.0 / latticelib.a < prev    next >
Encoding:
Text File  |  1980-12-01  |  7.0 KB  |  205 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    LibEnd
  35.  
  36.     ; Our library base structure description
  37.     STRUCTURE XPRBase,LIB_SIZE
  38.     ULONG    xb_SegList
  39.     LABEL    XPRBase_SIZEOF
  40.  
  41.  
  42.     SECTION    text,code
  43.  
  44.     ; Things we need others to define
  45.     XREF    @XProtocolCleanup
  46.     XREF    @XProtocolSetup
  47.     XREF    @XProtocolSend
  48.     XREF    @XProtocolReceive
  49.     XREF    @XProtocolHostMon
  50.     XREF    @XProtocolUserMon
  51.     XREF    __BSSBAS
  52.     XREF    __BSSLEN
  53.     XREF    _AbsExecBase
  54.     XREF    _LVOOpenLibrary
  55.     XREF    _LVOCloseLibrary
  56.     XREF    _LVORemove
  57.     XREF    _LVOFreeMem
  58.  
  59.  
  60.     ; Supposed start of executable code.  This is where execution
  61.     ; will start if anybody's silly enough to try and run the library
  62.     ; from the command line.  Just return the highest error code we
  63.     ; conveniently can to tell them they screwed up.
  64. LibStart:
  65.     moveq    #$7F,d0
  66.     rts
  67.  
  68.     ; Where the magic begins.  OpenLibrary actually looks through the
  69.     ; library file contents trying to find this table by locating the
  70.     ; magic RTC_MATCHWORD value followed by its own address.  This
  71.     ; table then tells OpenLibrary where to find other things it needs.
  72.     ; This needs to be close to the front of your library file to cut
  73.     ; down on the amount of searching OpenLibrary does; that's why
  74.     ; this object file should be first in the link list.
  75. RomTag:    dc.w    RTC_MATCHWORD    ; Magic value to search for to find table
  76.     dc.l    RomTag        ; Address of matchword; the two together prevent accidental matches
  77.     dc.l    LibEnd        ; Address of end of library handler code
  78.     dc.b    RTF_AUTOINIT    ; Request system to automatically initialize our library
  79.     dc.b    VERSION        ; Version number of our library (defined below)
  80.     dc.b    NT_LIBRARY    ; Node type = Library
  81.     dc.b    0        ; Node priority = 0 (normal)
  82.     dc.l    LibName        ; Name of this library file, for debugging info
  83.     dc.l    LibIDString    ; More debugging info
  84.     dc.l    inittable    ; Initialization table used by RTF_AUTOINIT
  85.  
  86. VERSION     EQU    2
  87. REVISION EQU    0
  88. LibName:
  89.     dc.b    "xprzmodem.library",0
  90. LibIDString:
  91.     dc.b    "xprzmodem 2.0 (28 Oct 1989)",13,10,0
  92.  
  93.     ds.w    0
  94.  
  95. inittable:
  96.     dc.l    XPRBase_SIZEOF    ; Size of our library base struct
  97.     dc.l    functable    ; Where our function addresses are
  98.     dc.l    datatable    ; Initialization info for our library base struct
  99.     dc.l    LibInit        ; Library initialization routine address
  100.  
  101. functable:
  102.     dc.l    LibOpen            ; Addresses of all library functions
  103.     dc.l    LibClose        ; First 4 MUST be provided, in this order
  104.     dc.l    LibExpunge
  105.     dc.l    LibExtFunc
  106.     dc.l    @XProtocolCleanup    ; The meat of the library.
  107.     dc.l    @XProtocolSetup        ; Since the XPR spec requires the args
  108.     dc.l    @XProtocolSend        ; to be passed in the same registers that
  109.     dc.l    @XProtocolReceive    ; Lattice uses for -rr, we can just let the
  110.     dc.l    @XProtocolHostMon    ; calling program jump straight into the
  111.     dc.l    @XProtocolUserMon    ; C routines, with no glue code required.
  112.     dc.l    -1
  113.  
  114.     ; Things for the system to automatically initialize for us via RTF_AUTOINIT request
  115. datatable:
  116.     INITBYTE    LN_TYPE,NT_LIBRARY
  117.     INITLONG    LN_NAME,LibName
  118.     INITBYTE    LIB_FLAGS,LIBF_SUMUSED | LIBF_CHANGED
  119.     INITWORD    LIB_VERSION,VERSION
  120.     INITWORD    LIB_REVISION,REVISION
  121.     INITLONG    LIB_IDSTRING,LibIDString
  122.     dc.l    0
  123.  
  124.  
  125.     ; System interface routines.  Exec has performed Forbid() before
  126.     ; getting here, so do this stuff quick and get the hell out.
  127.  
  128.  
  129.     ; Routine which is executed by the system as part of first OpenLibrary
  130.     ; call, when the library is first loaded into RAM from disk.
  131.     ; D0 = library base pointer, A0 = segment list pointer.  Must return
  132.         ; nonzero in D0 if initialization worked, or zero if it failed.
  133. LibInit:
  134.     move.l    d0,-(sp)        ; Save D0 for return code
  135.     move.l    d0,a1            ; A1 = library base
  136.     move.l    a0,xb_SegList(a1)    ; Save seglist addr for future expunge
  137.     lea    __BSSBAS,a0        ; Initialize BSS memory to 0s
  138.     move.l    #__BSSLEN,d0
  139.     moveq    #0,d1
  140.     bra.s    clr_lp
  141. clr_bss    move.l    d1,(a0)+
  142. clr_lp    dbf    d0,clr_bss
  143.     move.l    (sp)+,d0        ; Return nonzero = success
  144.     rts
  145.  
  146.     ; Open the library.  Executed by system as part of OpenLibrary call,
  147.     ; after loading the library into RAM and calling LibInit if necessary.
  148.     ; D0 = version, A6 = library base.  Return nonzero if open worked,
  149.     ; or zero if open failed for some reason.
  150. LibOpen:
  151.     addq.w    #1,LIB_OPENCNT(a6)        ; Increment open count
  152.     bclr    #LIBB_DELEXP,LIB_FLAGS(a6)    ; Clear delayed expunge flag
  153.     move.l    a6,d0                ; Return nonzero = success
  154.     rts
  155.  
  156.     ; Close the library.  Executed by system as part of CloseLibrary call.
  157.     ; A6 = library base.  Return seglist address if a delayed expunge
  158.     ; was performed, else return 0 if library is still loaded.
  159. LibClose:
  160.     moveq    #0,d0                ; Init return value = 0
  161.     subq.w    #1,LIB_OPENCNT(a6)        ; Decrement open count
  162.     bne.s    dontexpunge            ; If still open by others, can't expunge
  163.     btst    #LIBB_DELEXP,LIB_FLAGS(a6)    ; Was an expunge requested while we were open?
  164.     beq.s    dontexpunge
  165.     bsr.s    LibExpunge            ; If so, do it now
  166. dontexpunge:
  167.     rts
  168.  
  169.     ; Expunge the library (remove it from RAM).  Executed by system
  170.     ; memory allocation routine when memory gets tight.  A6 = library
  171.     ; base.  Return seglist address if library was closed and we were
  172.     ; able to expunge, else return 0 if library is still loaded.
  173. LibExpunge:
  174.     movem.l    d2/a5/a6,-(sp)
  175.     move.l    a6,a5
  176.     move.l    _AbsExecBase,a6            ; Get ExecBase for future use
  177.     tst.w    LIB_OPENCNT(a5)            ; Is library open?
  178.     beq.s    doexpunge
  179.     bset    #LIBB_DELEXP,LIB_FLAGS(a5)    ; If so, set delayed expunge flag
  180.     moveq    #0,d0                ; and return 0
  181.     bra.s    expungedone
  182. doexpunge:
  183.     move.l    xb_SegList(a5),d2        ; Save seglist address in D2
  184.     move.l    a5,a1                ; A1 = library base address
  185.     jsr    _LVORemove(a6)            ; Unlink library node from system library list
  186.     moveq    #0,d0                ; Compute total size of library node
  187.     move.w    LIB_NEGSIZE(a5),d0        ;   D0 = bytes used before base
  188.     move.l    a5,a1
  189.     sub.l    d0,a1                ;   A1 = base - negsize
  190.     add.w    LIB_POSSIZE(a5),d0        ;   D0 = possize + negsize
  191.     jsr    _LVOFreeMem(a6)            ; Free library node memory
  192.     move.l    d2,d0                ; Return segment list address
  193. expungedone:
  194.     movem.l    (sp)+,d2/a5/a6
  195.     rts
  196.  
  197.     ; "Extra" function (room for future growth?).  We don't need it,
  198.     ; so dummy it out.
  199. LibExtFunc:
  200.     moveq    #0,d0
  201.     rts
  202.  
  203. LibEnd:
  204.     END
  205.