home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / xprzmodem_459.lzh / XprZmodem / latticelib.a < prev    next >
Text File  |  1991-02-18  |  8KB  |  256 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.     ; Supposed start of executable code.  This is where execution
  67.     ; will start if anybody's silly enough to try and run the library
  68.     ; from the command line.  Just return the highest error code we
  69.     ; conveniently can to tell them they screwed up.
  70. LibStart:
  71.     moveq    #$7F,d0
  72.     rts
  73.  
  74.     ; Where the magic begins.  OpenLibrary actually looks through the
  75.     ; library file contents trying to find this table by locating the
  76.     ; magic RTC_MATCHWORD value followed by its own address.  This
  77.     ; table then tells OpenLibrary where to find other things it needs.
  78.     ; This needs to be close to the front of your library file to cut
  79.     ; down on the amount of searching OpenLibrary does; that's why
  80.     ; this object file should be first in the link list.
  81. RomTag:    dc.w    RTC_MATCHWORD    ; Magic value to search for to find table
  82.     dc.l    RomTag        ; Address of matchword; the two together prevent accidental matches
  83.     dc.l    LibEnd        ; Address of end of library handler code
  84.     dc.b    RTF_AUTOINIT    ; Request system to automatically initialize our library
  85.     dc.b    VERSION        ; Version number of our library (defined below)
  86.     dc.b    NT_LIBRARY    ; Node type = Library
  87.     dc.b    0        ; Node priority = 0 (normal)
  88.     dc.l    LibName        ; Name of this library file, for debugging info
  89.     dc.l    LibIDString    ; More debugging info
  90.     dc.l    inittable    ; Initialization table used by RTF_AUTOINIT
  91.  
  92. VERSION     EQU    2
  93. REVISION EQU    10
  94. LibName:
  95.     dc.b    "xprzmodem.library",0
  96. LibIDString:
  97.     dc.b    "xprzmodem 2.10 (12 Feb 1991)",13,10,0
  98.  
  99.     ds.w    0
  100.  
  101. inittable:
  102.     dc.l    XPRBase_SIZEOF    ; Size of our library base struct
  103.     dc.l    functable    ; Where our function addresses are
  104.     dc.l    datatable    ; Initialization info for our library base struct
  105.     dc.l    LibInit        ; Library initialization routine address
  106.  
  107. functable:
  108.     dc.l    LibOpen            ; Addresses of all library functions
  109.     dc.l    LibClose        ; First 4 MUST be provided, in this order
  110.     dc.l    LibExpunge
  111.     dc.l    LibExtFunc
  112.     dc.l    XProtocolCleanup    ; The meat of the library.
  113.     dc.l    XProtocolSetup
  114.     dc.l    XProtocolSend
  115.     dc.l    XProtocolReceive
  116.     dc.l    XProtocolHostMon
  117.     dc.l    XProtocolUserMon
  118.     dc.l    -1
  119.  
  120.     ; Things for the system to automatically initialize for us via RTF_AUTOINIT request
  121. datatable:
  122.     INITBYTE    LN_TYPE,NT_LIBRARY
  123.     INITLONG    LN_NAME,LibName
  124.     INITBYTE    LIB_FLAGS,LIBF_SUMUSED | LIBF_CHANGED
  125.     INITWORD    LIB_VERSION,VERSION
  126.     INITWORD    LIB_REVISION,REVISION
  127.     INITLONG    LIB_IDSTRING,LibIDString
  128.     dc.l    0
  129.  
  130.  
  131.     ; System interface routines.  Exec has performed Forbid() before
  132.     ; getting here, so do this stuff quick and get the hell out.
  133.  
  134.  
  135.     ; Routine which is executed by the system as part of first OpenLibrary
  136.     ; call, when the library is first loaded into RAM from disk.
  137.     ; D0 = library base pointer, A0 = segment list pointer.  Must return
  138.         ; nonzero in D0 if initialization worked, or zero if it failed.
  139. LibInit:
  140.     move.l    d0,-(sp)        ; Save D0 for return code
  141.     move.l    d0,a1            ; A1 = library base
  142.     move.l    a0,xb_SegList(a1)    ; Save seglist addr for future expunge
  143.     lea    __BSSBAS,a0        ; Initialize BSS memory to 0s
  144.     move.l    #__BSSLEN,d0
  145.     moveq    #0,d1
  146.     bra.s    clr_lp
  147. clr_bss    move.l    d1,(a0)+
  148. clr_lp    dbf    d0,clr_bss
  149.     move.l    (sp)+,d0        ; Return nonzero = success
  150.     rts
  151.  
  152.     ; Open the library.  Executed by system as part of OpenLibrary call,
  153.     ; after loading the library into RAM and calling LibInit if necessary.
  154.     ; D0 = version, A6 = library base.  Return nonzero if open worked,
  155.     ; or zero if open failed for some reason.
  156. LibOpen:
  157.     addq.w    #1,LIB_OPENCNT(a6)        ; Increment open count
  158.     bclr    #LIBB_DELEXP,LIB_FLAGS(a6)    ; Clear delayed expunge flag
  159.     move.l    a6,d0                ; Return nonzero = success
  160.     rts
  161.  
  162.     ; Close the library.  Executed by system as part of CloseLibrary call.
  163.     ; A6 = library base.  Return seglist address if a delayed expunge
  164.     ; was performed, else return 0 if library is still loaded.
  165. LibClose:
  166.     moveq    #0,d0                ; Init return value = 0
  167.     subq.w    #1,LIB_OPENCNT(a6)        ; Decrement open count
  168.     bne.s    dontexpunge            ; If still open by others, can't expunge
  169.     btst    #LIBB_DELEXP,LIB_FLAGS(a6)    ; Was an expunge requested while we were open?
  170.     beq.s    dontexpunge
  171.     bsr.s    LibExpunge            ; If so, do it now
  172. dontexpunge:
  173.     rts
  174.  
  175.     ; Expunge the library (remove it from RAM).  Executed by system
  176.     ; memory allocation routine when memory gets tight.  A6 = library
  177.     ; base.  Return seglist address if library was closed and we were
  178.     ; able to expunge, else return 0 if library is still loaded.
  179. LibExpunge:
  180.     movem.l    d2/a5/a6,-(sp)
  181.     move.l    a6,a5
  182.     move.l    _AbsExecBase,a6            ; Get ExecBase for future use
  183.     tst.w    LIB_OPENCNT(a5)            ; Is library open?
  184.     beq.s    doexpunge
  185.     bset    #LIBB_DELEXP,LIB_FLAGS(a5)    ; If so, set delayed expunge flag
  186.     moveq    #0,d0                ; and return 0
  187.     bra.s    expungedone
  188. doexpunge:
  189.     move.l    xb_SegList(a5),d2        ; Save seglist address in D2
  190.     move.l    a5,a1                ; A1 = library base address
  191.     jsr    _LVORemove(a6)            ; Unlink library node from system library list
  192.     moveq    #0,d0                ; Compute total size of library node
  193.     move.w    LIB_NEGSIZE(a5),d0        ;   D0 = bytes used before base
  194.     move.l    a5,a1
  195.     sub.l    d0,a1                ;   A1 = base - negsize
  196.     add.w    LIB_POSSIZE(a5),d0        ;   D0 = possize + negsize
  197.     jsr    _LVOFreeMem(a6)            ; Free library node memory
  198.     move.l    d2,d0                ; Return segment list address
  199. expungedone:
  200.     movem.l    (sp)+,d2/a5/a6
  201.     rts
  202.  
  203.     ; "Extra" function (room for future growth?).  We don't need it,
  204.     ; so dummy it out.
  205. LibExtFunc:
  206.     moveq    #0,d0
  207.     rts
  208.  
  209.  
  210.  
  211. ; Added glue to protect the comm program from having us munge its registers
  212. ; when it calls our code, due to bug which caused us to munge the A6
  213. ; register in an earlier version.  Since the XPR spec requires the arguments
  214. ; to be passed in the same registers that Lattice uses for function call
  215. ; register arguments, all we have to do is save the non-changeable registers
  216. ; on entry and restore them on exit.
  217.  
  218. XProtocolCleanup:
  219.     movem.l    d2-d7/a2-a6,-(sp)
  220.     jsr    @XProtocolCleanup
  221.     movem.l    (sp)+,d2-d7/a2-a6
  222.     rts
  223.  
  224. XProtocolSetup:
  225.     movem.l    d2-d7/a2-a6,-(sp)
  226.     jsr    @XProtocolSetup
  227.     movem.l    (sp)+,d2-d7/a2-a6
  228.     rts
  229.  
  230. XProtocolSend:
  231.     movem.l    d2-d7/a2-a6,-(sp)
  232.     jsr    @XProtocolSend
  233.     movem.l    (sp)+,d2-d7/a2-a6
  234.     rts
  235.  
  236. XProtocolReceive:
  237.     movem.l    d2-d7/a2-a6,-(sp)
  238.     jsr    @XProtocolReceive
  239.     movem.l    (sp)+,d2-d7/a2-a6
  240.     rts
  241.  
  242. XProtocolHostMon:
  243.     movem.l    d2-d7/a2-a6,-(sp)
  244.     jsr    @XProtocolHostMon
  245.     movem.l    (sp)+,d2-d7/a2-a6
  246.     rts
  247.  
  248. XProtocolUserMon:
  249.     movem.l    d2-d7/a2-a6,-(sp)
  250.     jsr    @XProtocolUserMon
  251.     movem.l    (sp)+,d2-d7/a2-a6
  252.     rts
  253.  
  254. LibEnd:
  255.     END
  256.