home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gfx / imagefx_sdk-2.0.lha / ImageFX_SDK / sas / libsrc / lib.asm next >
Encoding:
Assembly Source File  |  1993-12-08  |  12.6 KB  |  507 lines

  1. *************************************************************************
  2. * ImageFX Module Startup Code
  3. *
  4. * Based on the Commodore-supplied example library, if you didn't
  5. * notice.
  6. *
  7. * Updated for ImageFX 2.0
  8. *
  9. *************************************************************************
  10.  
  11.  
  12. SAS_63          EQU     0
  13. SAS_FLOAT    EQU    1    Enable SAS floating point init
  14. SAS_FLOAT_1     EQU     0
  15. SAS_STACK    EQU    0    Enable SAS stack depth checking init
  16.  
  17.            NOLIST
  18.  
  19.            INCLUDE "exec/types.i"
  20.            INCLUDE "exec/libraries.i"
  21.            INCLUDE "exec/lists.i"
  22.            INCLUDE "exec/alerts.i"
  23.            INCLUDE "exec/initializers.i"
  24.            INCLUDE "exec/resident.i"
  25.            INCLUDE "exec/tasks.i"
  26.            INCLUDE "libraries/dos.i"
  27.            INCLUDE "libraries/dosextens.i"
  28.  
  29.            INCLUDE "scan/mod.i"
  30.  
  31.  
  32. BHS         MACRO
  33.          bcc.\0  \1 ;\0 is the extension used on the macro (such as ".s")
  34.          ENDM
  35.  
  36. BLO         MACRO
  37.          bcs.\0  \1
  38.          ENDM
  39.  
  40.  
  41.            XREF       _AbsExecBase
  42.  
  43.            XREF    _LVOOpenLibrary
  44.            XREF    _LVOCloseLibrary
  45.            XREF    _LVOAlert
  46.            XREF    _LVOFreeMem
  47.            XREF    _LVORemove
  48.  
  49.            XREF    _LVOFindTask
  50.  
  51.            ; These must be defined in the C code somewhere...
  52.            XREF       _FuncTable            ; Function table array
  53.            XREF       _LibraryType              ; Type of module (NT_#?)
  54.            XREF       _LibraryID                ; Library ID string
  55.            XREF       _UserOpen                 ; Module-specific Open code
  56.            XREF       _UserClose                ; Module-specific Close code
  57.  
  58.         IFNE    SAS_FLOAT
  59.                 ; Used to allow floating point in modules.
  60.            XREF    ___fpinit
  61.            XREF    ___fpterm
  62.            ENDC    SAS_FLOAT
  63.  
  64.                 XREF    _LinkerDB        * linker defined base value
  65.  
  66.  
  67. ***********************************************************************
  68. *
  69. * Library data structures
  70. *
  71. ***********************************************************************
  72.  
  73.            LIST
  74.  
  75. ;----------------------------------------------------------------------
  76. ; The first executable location.  This should return an error
  77. ; in case someone tried to run you as a program (instead of
  78. ; loading you as a library).
  79.  
  80. Start:
  81.            moveq    #-1,d0
  82.            rts
  83.  
  84. ;-----------------------------------------------------------------------
  85. ; A romtag structure.  Both "exec" and "ramlib" look for
  86. ; this structure to discover magic constants about you
  87. ; (such as where to start running you from...).
  88. ;
  89.  
  90. initDDescrip:
  91.              DC.W    RTC_MATCHWORD   ; RT_MATCHWORD
  92.              DC.L    initDDescrip    ; RT_MATCHTAG
  93.              DC.L    EndCode         ; RT_ENDSKIP
  94.              DC.B    RTF_AUTOINIT    ; RT_FLAGS
  95.              DC.B    MOD_VERSION     ; RT_VERSION
  96.              DC.B    NT_LIBRARY      ; RT_TYPE
  97.              DC.B    0        ; RT_PRI
  98.              DC.L    0           ; RT_NAME
  99.              DC.L    _LibraryID      ; RT_IDSTRING
  100.              DC.L    Init            ; RT_INIT
  101.  
  102.  
  103.         ; force word allignment
  104.            DS.W    0
  105.  
  106.  
  107. ;----------------------------------------------------------------------
  108. ; The romtag specified that we were "RTF_AUTOINIT".  This means
  109. ; that the RT_INIT structure member points to one of these
  110. ; tables below.  If the AUTOINIT bit was not set then RT_INIT
  111. ; would point to a routine to run.
  112.  
  113. Init:
  114.            DC.L    xmodbase_SIZEOF   ; size of library base data space
  115.            DC.L    _FuncTable        ; pointer to function initializers (external)
  116.            DC.L    dataTable         ; pointer to data initializers
  117.            DC.L    initRoutine       ; routine to run
  118.  
  119.  
  120. ;----------------------------------------------------------------------
  121. ; The data table initializes static data structures.
  122. ; The format is specified in exec/InitStruct routine's
  123. ; manual pages.  The INITBYTE/INITWORD/INITLONG routines
  124. ; are in the file "exec/initializers.i".  The first argument
  125. ; is the offset from the library base for this byte/word/long.
  126. ; The second argument is the value to put in that cell.
  127. ; The table is null terminated
  128.  
  129. dataTable:
  130.            INITBYTE       LIB_FLAGS,LIBF_SUMUSED!LIBF_CHANGED
  131.            INITWORD       LIB_VERSION,MOD_VERSION
  132.            INITWORD       LIB_REVISION,MOD_REVISION
  133.            INITLONG       LIB_IDSTRING,_LibraryID
  134.            DC.L        0
  135.  
  136.  
  137. ;----------------------------------------------------------------------
  138. ; This routine gets called after the library has been allocated.
  139. ; The library pointer is in D0.  The segment list is in A0.
  140. ; If it returns non-zero then the library will be linked into
  141. ; the library list.
  142.  
  143. initRoutine:
  144.         ;------ get the library pointer into a convenient A register
  145.         movem.l    a4-a5,-(sp)
  146.         move.l    d0,a5
  147.  
  148.                 ; Load Base Register for near data
  149.                 lea     _LinkerDB,a4
  150.  
  151.         ;------ save a pointer to exec
  152.         move.l    a6,mb_SysLib(a5)
  153.         move.l    a6,_SysBase
  154.  
  155.         ;------ save a pointer to our loaded code
  156.         move.l    a0,mb_Segment(a5)
  157.  
  158.         IFNE    SAS_STACK
  159.  
  160.         ;------ initialize SAS stack depth checking
  161.         suba.l    a1,a1
  162.         jsr    _LVOFindTask(a6)
  163.         move.l    d0,a1
  164.         move.l    pr_CLI(a1),d1
  165.         beq.s    11$
  166.         lsl.l    #2,d1
  167.         move.l    d1,a0
  168.         move.l    cli_DefaultStack(a0),d1
  169.         lsl.l    #2,d1
  170.         bra.s    21$
  171. 11$
  172.         move.l    a7,d1
  173.         sub.l    TC_SPLOWER(a1),d1
  174. 21$
  175.         move.l    a7,d0
  176.         sub.l    d1,d0
  177.         add.l    #128,d0
  178.         move.l    d0,___base
  179.  
  180.         ENDC    SAS_STACK
  181.  
  182.         ;------ open the dos library
  183.         lea    dosName(pc),a1
  184.         moveq    #0,d0
  185.         jsr    _LVOOpenLibrary(a6)
  186.  
  187.         move.l    d0,_DOSBase
  188.         bne.s    1$
  189.  
  190.         ;------ can't open the dos!  what gives
  191.         ALERT    AG_OpenLib!AO_DOSLib
  192.  
  193. 1$:
  194.         ;------ initialize floating point libraries
  195.         IFNE    SAS_FLOAT
  196.         jsr      ___fpinit(PC)
  197.                 IFNE    SAS_63
  198.         tst.l   d0                      ; For SAS 6.5+ ...
  199.         beq.s   3$
  200.         moveq   #0,d0
  201.         bra.s   OUT
  202.                 ENDC
  203.                 ENDC    SAS_FLOAT
  204.  
  205. 3$        move.l    a5,d0            ; restore libbase
  206.  
  207. OUT:
  208.         movem.l    (sp)+,a4-a5
  209.         rts
  210.  
  211.  
  212.                 XDEF    __XCEXIT
  213.                 XDEF    @_XCEXIT
  214. __XCEXIT:       ; stdargs entry - clear stack
  215.                 addq.l  #4,sp
  216. @_XCEXIT:       ; regargs entry
  217.                 bra.s   OUT
  218.  
  219.  
  220.         IFNE    SAS_FLOAT_1
  221.  
  222. ;--------------------------------------------------------------------------
  223. ; We need this because of SAS lcmieee.lib floating point initialization.
  224. ; If it fails, it calls XCEXIT(), which we are trapping here and turning
  225. ; into just a standard failure return.  Not nice, but it seems to work.
  226. ;
  227.                 xdef    @_XCEXIT        ; for SAS 6.x
  228.         xdef    __XCEXIT
  229. @_XCEXIT:
  230. __XCEXIT:
  231.         moveq    #0,d0
  232.         addq.l    #8,sp
  233.         bra.s    OUT
  234.  
  235.                 ENDC    SAS_FLOAT
  236.  
  237.  
  238. ************************************************************************
  239. *
  240. * Here begins the system interface commands.  When the user calls
  241. * OpenLibrary/CloseLibrary/RemoveLibrary, this eventually gets translated
  242. * into a call to the following routines (Open/Close/Expunge).  Exec
  243. * has already put our library pointer in A6 for us.  Exec has turned
  244. * off task switching while in these routines (via Forbid/Permit), so
  245. * we should not take too long in them.
  246. *
  247. ************************************************************************
  248.  
  249.            XDEF       _LibOpen
  250.            XDEF       _LibClose
  251.            XDEF       _LibExpunge
  252.            XDEF       _LibNull
  253.  
  254. ;--------------------------------------------------------------------
  255. ; Open returns the library pointer in d0 if the open
  256. ; was successful.  If the open failed then null is returned.
  257. ; It might fail if we allocated memory on each open, or
  258. ; if only open application could have the library open
  259. ; at a time...
  260.  
  261. _LibOpen:    ; ( libptr:a6, version:d0 )
  262.  
  263.         move.l    a4,-(sp)    ; Save opener's A4...
  264.  
  265.                 ; Set type of module.
  266.            move.b  _LibraryType,LN_TYPE(a6)
  267.  
  268.         ; Set module's global pointers...
  269.         move.l    a6,_ModuleBase
  270.         move.l    mb_ScanBase(a6),a0
  271.         move.l    a0,_ScanBase
  272.         move.l    sb_IntuitionBase(a0),_IntuitionBase
  273.         move.l    sb_GfxBase(a0),_GfxBase
  274.         move.l  sb_EGSIntuiBase(a0),_EGSIntuiBase
  275.         move.l  sb_EGSGfxBase(a0),_EGSGfxBase
  276.  
  277.            ; Call module's 'C' open code
  278.            move.l a6,-(sp)
  279.            bsr    _UserOpen
  280.            move.l (sp)+,a6
  281.            tst.l  d0
  282.            bne.s  1$
  283.  
  284.            ; User open failed, abort open
  285.            move.l (sp)+,a4        ; Restore A4!
  286.            moveq  #0,d0        ; Fail code
  287.            rts
  288.  
  289. 1$:
  290.            ; mark us as having another opener
  291.            addq.w   #1,LIB_OPENCNT(a6)
  292.  
  293.            ; prevent delayed expunges
  294. ;           bclr   #LIBB_DELEXP,LIB_FLAGS(a6)
  295.  
  296.            move.l   (sp)+,a4    ; Restore opener's A4
  297.  
  298.            move.l   a6,d0        ; Success
  299.            rts
  300.  
  301.  
  302. ;----------------------------------------------------------------
  303. ; There are two different things that might be returned from
  304. ; the Close routine.  If the library is no longer open and
  305. ; there is a delayed expunge then Close should return the
  306. ; segment list (as given to Init).  Otherwise close should
  307. ; return NULL.
  308.  
  309. _LibClose:    ; ( libptr:a6 )
  310.  
  311.            move.l  a4,-(sp)        ; Save opener's A4...
  312.  
  313.            ;------ Call 'C' close code
  314.            move.l  a6,-(sp)
  315.            bsr     _UserClose
  316.            move.l  (sp)+,a6
  317.  
  318.            ;------ set the return value
  319.            moveq   #0,d0
  320.  
  321.            ;------ mark us as having one fewer openers
  322.            subq.w   #1,LIB_OPENCNT(a6)
  323.  
  324.            ;------ see if there is anyone left with us open
  325.            bne.s   1$
  326.  
  327.            ;------ We expunge the library no matter what on the last Close
  328. ;           ;------ see if we have a delayed expunge pending
  329. ;          btst   #LIBB_DELEXP,LIB_FLAGS(a6)
  330. ;          beq.s   1$
  331.  
  332.            ;------ do the expunge
  333.            bsr   _LibExpunge
  334. 1$:
  335.            move.l (sp)+,a4            ; Restore opener's A4...
  336.            rts
  337.  
  338.  
  339. ;---------------------------------------------------------------
  340. ; There are two different things that might be returned from
  341. ; the Expunge routine.  If the library is no longer open
  342. ; then Expunge should return the segment list (as given to
  343. ; Init).  Otherwise Expunge should set the delayed expunge
  344. ; flag and return NULL.
  345. ;
  346. ; One other important note: because Expunge is called from
  347. ; the memory allocator, it may NEVER Wait() or otherwise
  348. ; take long time to complete.
  349.  
  350. _LibExpunge:    ; ( libptr: a6 )
  351.  
  352.         movem.l    d2/a4/a5/a6,-(sp)
  353.         move.l    a6,a5
  354.         move.l    mb_SysLib(a5),a6
  355.  
  356.                 ; Load Base Register for near data
  357.                 lea     _LinkerDB,a4
  358.  
  359.         ;------ see if anyone has us open
  360.         tst.w    LIB_OPENCNT(a5)
  361.         beq.s    1$
  362.  
  363.         ;------ it is still open.  set the delayed expunge flag
  364.         bset    #LIBB_DELEXP,LIB_FLAGS(a5)
  365.         moveq    #0,d0
  366.         bra.s    Expunge_End
  367.  
  368. 1$:
  369.         IFNE    SAS_FLOAT
  370.         jsr    ___fpterm(PC)
  371.         ENDC    SAS_FLOAT
  372.  
  373.         ;------ go ahead and get rid of us.  Store our seglist in d2
  374.         move.l    mb_Segment(a5),d2
  375.  
  376.         ; no need to remove library - we were never added
  377.         ; to public library list!
  378.  
  379.         ;------ close the dos library
  380.         move.l    _DOSBase,a1
  381.         jsr    _LVOCloseLibrary(a6)
  382.  
  383.         ;------ free our memory
  384.         moveq    #0,d0
  385.         move.l    a5,a1
  386.         move.w    LIB_NEGSIZE(a5),d0
  387.  
  388.         sub.l    d0,a1
  389.         add.w    LIB_POSSIZE(a5),d0
  390.  
  391.         jsr    _LVOFreeMem(a6)
  392.  
  393.         ;------ set up our return value, the segment
  394.         move.l    d2,d0
  395.  
  396. Expunge_End:
  397.         movem.l    (sp)+,d2/a4/a5/a6
  398.         rts
  399.  
  400.  
  401. ;----------------------------------------------------------
  402. ; Reserved library vector should just return 0.
  403. ;
  404.  
  405. _LibNull:
  406.         moveq    #0,d0
  407.         rts
  408.  
  409. ;----------------------------------------------------------
  410. ; If language text has been located and loaded, use the
  411. ; string associated with the given index.  Otherwise use
  412. ; the default string passed in.
  413. ;
  414. ; May be used by a module to provide the ability to alter
  415. ; the module's text strings, for localization purposes.
  416. ;
  417. ; char *GetStr(LONG index, char *default);
  418. ;
  419.  
  420.         XDEF    _GetStr
  421. _GetStr:
  422.         move.l    _ModuleBase,a0
  423.         tst.l    mb_Text(a0)
  424.         beq.s    1$
  425.         move.l    mb_Text(a0),a0
  426.         move.l    4(sp),d0
  427.         add.l    d0,d0
  428.         add.l    d0,d0
  429.         move.l    0(a0,d0.l),d0
  430.         bra.s    2$
  431. 1$        move.l    8(sp),d0
  432. 2$        rts
  433.  
  434.  
  435. **********************************************************************
  436. *
  437. * Data section
  438. *
  439. **********************************************************************
  440.  
  441.            XDEF       _SysBase
  442.            XDEF       _DOSBase
  443.                 XDEF    _IntuitionBase
  444.                 XDEF    _GfxBase
  445.                 XDEF    _ModuleBase
  446.                 XDEF    _ScanBase
  447.                 XDEF    _EGSIntuiBase
  448.                 XDEF    _EGSGfxBase
  449.  
  450.  
  451. dosName:    DC.B    'dos.library',0
  452.            CNOP    0,2
  453.  
  454.  
  455.         SECTION __MERGED,DATA
  456.  
  457. _SysBase:    DC.L    0
  458. _DOSBase:    DC.L    0
  459.  
  460. _IntuitionBase  DC.L    0
  461. _GfxBase        DC.L    0
  462. _ModuleBase     DC.L    0
  463. _ScanBase       DC.L    0
  464. _EGSIntuiBase   DC.L    0
  465. _EGSGfxBase     DC.L    0
  466.  
  467.  
  468. ; For SAS stack depth checking...
  469.         IFNE    SAS_STACK
  470.         XDEF    ___base
  471. ___base:    DC.L    0
  472.         ENDC    SAS_STACK
  473.  
  474. ; These are used for SAS floating point...
  475.         IFNE    SAS_FLOAT
  476.  
  477.         XDEF    __SIGFPE
  478. __SIGFPE    DC.L    0
  479.  
  480. ; FPERR defined in SAS 6.5 sc.lib...
  481. ;               XDEF    __FPERR
  482. ;__FPERR        DC.L    0
  483.  
  484.         ENDC    SAS_FLOAT
  485.  
  486. ;--------------------------------------------------------------------
  487. ; This stuff was added for modules to compile under SAS6.2...
  488.  
  489.         XDEF    __OSERR
  490. __OSERR:    DC.L    0        ; OS error codes (SAS6.2)
  491.         XDEF    __ProgramName
  492. __ProgramName    DC.L    0        ; Program Name??? (SAS6.2)
  493.                 XDEF    __WBenchMsg
  494. __WBenchMsg:    DC.L    0               ; For autolibinit failure
  495.  
  496.  
  497. ; EndCode is a marker that show the end of your code.
  498. ; Make sure it does not span sections nor is before the
  499. ; rom tag in memory!  It is ok to put it right after
  500. ; the rom tag -- that way you are always safe.  I put
  501. ; it here because it happens to be the "right" thing
  502. ; to do, and I know that it is safe in this case.
  503.  
  504. EndCode:
  505.         END
  506.  
  507.