home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaMiscModsCD3.iso / _PROGRAMME / CONVERTER / pro2fast11.lha / Pro2Fast / Source / Startup.a < prev    next >
Encoding:
Text File  |  1996-02-18  |  14.1 KB  |  578 lines

  1. ;*************************************************************************
  2. ;*                                                                       *
  3. ;* This is my private C startup routine. If you use this routine,        *
  4. ;* remember to credit me. Below is a desciption of all the defines you   *
  5. ;* can use when you assemble it. When you compile, link with the link    *
  6. ;* file (Startup.o) as a custom startup.                                 *
  7. ;*                                                                       *
  8. ;* When you use this routine, you don't need to open the dos & intuition *
  9. ;* library. This is done for you.                                        *
  10. ;*                                                                       *
  11. ;* Assemble with the following command: asm [defines] Startup.a          *
  12. ;*                                                                       *
  13. ;* where [defines] can be one of more of the following arguments:        *
  14. ;*                                                                       *
  15. ;* -dUTILLIB      = If you use the utility library for math, define this.*
  16. ;*                                                                       *
  17. ;* -dBACK         = Run the main function as a background process.       *
  18. ;*                  You need to define this variable:                    *
  19. ;*                  char ProgramName[] = "You program";                  *
  20. ;*                                                                       *
  21. ;*                  Note that you must NOT use smallcode when you link.  *
  22. ;*                                                                       *
  23. ;* -dWBARGS       = WorkBench arguments (tooltypes). You have to make    *
  24. ;*                  the following initialized variables:                 *
  25. ;*                  ULONG ArgList[] = {0,0,0,....,0};                    *
  26. ;*                                                                       *
  27. ;*                  You also need this function to parse the tooltypes:  *
  28. ;*                  void __asm TestToolTypes                             *
  29. ;*                             (__register a0 struct DiskObject* dobj);  *
  30. ;*                  You don't need to open the icon.library, this is     *
  31. ;*                  already done by the startup routine.                 *
  32. ;*                                                                       *
  33. ;* -dCLIARGS      = CLI arguments. You have to make the following        *
  34. ;*                  initialized variables:                               *
  35. ;*                  char CLIString[] = "SWITCH/S";                       *
  36. ;*                  ULONG ArgList[] = {0,0,0,....,0};                    *
  37. ;*                                                                       *
  38. ;* -dNOPAR        = If you use CLI arguments and you want to startup     *
  39. ;*                  when the user haven't give you any arguments.        *
  40. ;*                                                                       *
  41. ;* -dPRIGET=xx    = This is the number of the argument in your CLI       *
  42. ;*                  ArgList, which defines the priority.                 *
  43. ;*                                                                       *
  44. ;* -dNOCLIBACK=xx = Set this to the number of arguments you have. This   *
  45. ;*                  is used to test if the user has gived you any        *
  46. ;*                  arguments so the startup routine won't startup as a  *
  47. ;*                  background process. Do only use this together with   *
  48. ;*                  the BACK define.                                     *
  49. ;*                                                                       *
  50. ;* -dPRI=xx       = Set this to your task priority. Default is 0.        *
  51. ;*                                                                       *
  52. ;* -dKICKVER=xx   = Which kickstart version you require. Has to be       *
  53. ;*                  minimum 37 which is the default.                     *
  54. ;*                                                                       *
  55. ;* If you want to change the stack size, do as following:                *
  56. ;*                                                                       *
  57. ;* long __stack = xxxxx;                                                 *
  58. ;*                                                                       *
  59. ;*************************************************************************
  60. ;-------------------------------------------------------------------------
  61. ; Include all the stuff we need
  62. ;
  63.  
  64.     include    "exec/types.i"
  65.     include "exec/libraries.i"
  66.     include    "exec/memory.i"
  67.     include "exec/funcdef.i"
  68.     include    "workbench/startup.i"
  69.  
  70.     include    "dos/dosextens.i"
  71.     include    "dos/dostags.i"
  72.  
  73.     include    "lvo/exec_lib.i"
  74.     include    "lvo/dos_lib.i"
  75.     include    "lvo/intuition_lib.i"
  76.     include    "lvo/icon_lib.i"
  77. ;-------------------------------------------------------------------------
  78. ; Make the XDEF and XREF
  79. ;
  80.  
  81.     IFD    BACK
  82.     SECTION    Startup,code
  83.     ELSE
  84.     SECTION    Text,code
  85.     ENDC
  86.  
  87.     XDEF    __startup
  88.  
  89.     XREF    _LinkerDB        ;Linker Defined Base Value
  90.     XREF    __BSSBAS        ;Linker Defined Base Of BSS
  91.     XREF    __BSSLEN        ;Linker Defined Length Of BSS
  92.     XREF    ___stack
  93.  
  94.     XREF    @main            ;Start Address Of Main Program
  95.  
  96.     XREF    _CLIString        ;CLI Argument String
  97.     XREF    _ArgList        ;Argument List Array
  98.     XREF    _ProgramName        ;Name Of The Program
  99.  
  100.     XREF    @TestToolTypes        ;ToolType Test Routine
  101. ;-------------------------------------------------------------------------
  102. ; Some usefull macros
  103. ;
  104.  
  105. CALLSYS    macro
  106.     CALLLIB    _LVO\1
  107.     endm
  108. ;-------------------------------------------------------------------------
  109.     IFD    BACK
  110. BACK1    EQU    0
  111.     ELSE
  112. BACK1    EQU    1
  113.     ENDC
  114.  
  115.     IFD    NOCLIBACK
  116. NOCLI    EQU    1
  117.     ELSE
  118. NOCLI    EQU    0
  119.     ENDC
  120.  
  121. __startup
  122.     bra.s    .run
  123. ;-------------------------------------------------------------------------
  124.     dc.b    "Startup code written by Thomas Neumann in 1996",0
  125.     cnop    0,2
  126. ;-------------------------------------------------------------------------
  127. ; The startup routine itself
  128. ;
  129.  
  130. .run    lea    _LinkerDB,a4        ;Base Register
  131.     lea    __BSSBAS,a0
  132.     moveq    #0,d0
  133.     move.l    #__BSSLEN,d1
  134.     beq.s    .noclr
  135. .clrbss    move.l    d0,(a0)+        ;Clear BSS Area
  136.     subq.l    #1,d1
  137.     bne.s    .clrbss
  138.  
  139. .noclr    move.l    4.w,a6
  140.     move.l    a6,_SysBase(a4)
  141.  
  142.     sub.l    a1,a1
  143.     CALLSYS    FindTask        ;Find Our Process
  144.     move.l    d0,a5
  145.  
  146.     lea    .intnam(pc),a1
  147.     moveq    #0,d0
  148.     CALLSYS    OpenLibrary        ;Open Intuition Library
  149.     move.l    d0,_IntuitionBase(a4)
  150.     beq    .errout
  151.  
  152.     IFD    KICKVER
  153.     cmp.w    #KICKVER,LIB_VERSION(a6) ;Test For Kickstart Version
  154.     ELSE
  155.     cmp.w    #37,LIB_VERSION(a6)
  156.     ENDC
  157.     blt    .kicker
  158.  
  159.     lea    .dosnam(pc),a1
  160.     moveq    #0,d0
  161.     CALLSYS    OpenLibrary        ;Open Dos Library
  162.     IF    BACK1!NOCLI
  163.     move.l    d0,_DOSBase(a4)
  164.     ELSE
  165.     tst.l    d0
  166.     ENDC
  167.     beq    .errou1
  168.     move.l    d0,d6
  169.  
  170.     IFD    UTILLIB
  171.     lea    .utinam(pc),a1
  172.     moveq    #0,d0
  173.     CALLSYS    OpenLibrary        ;Open Utility Library
  174.     move.l    d0,_UtilityBase(a4)
  175.     bne.s    .utilok
  176.     move.l    d6,a1
  177.     CALLSYS    CloseLibrary        ;Close Dos Library
  178.     bra    .errou1
  179.  
  180. .utilok
  181.     ENDC
  182.  
  183.     IFD    CLIARGS
  184.     sub.l    a2,a2            ;Clear CLI Arguments Pointer
  185.     ENDC
  186.  
  187.     sub.l    a3,a3            ;Clear WorkBench Message Pointer
  188.     tst.l    pr_CLI(a5)        ;Started From WorkBench?
  189.     bne.s    .itcli            ;Nup!
  190.  
  191. ;*************************************************************************
  192. ;* WorkBench Startup                                                     *
  193. ;*************************************************************************
  194.  
  195.     move.l    pr_StackSize(a5),-(sp)
  196.     bsr    .gework            ;Get Message
  197.  
  198.     move.l    sm_ArgList(a3),d0
  199.     beq.s    .wbout1
  200.  
  201.     move.l    d6,a6            ;DOSBase
  202.     move.l    d0,a0
  203.     move.l    wa_Lock(a0),d1
  204.     CALLSYS    CurrentDir        ;Set Current Directory
  205.  
  206.     IFD    WBARGS
  207.     move.l    _SysBase(a4),a6
  208.     lea    .icnnam(pc),a1
  209.     moveq    #0,d0
  210.     CALLSYS    OpenLibrary        ;Open Icon.library
  211.     move.l    d0,_IconBase(a4)
  212.     beq.s    .wbout1
  213.  
  214.     move.l    d0,a6
  215.     move.l    sm_ArgList(a3),a0
  216.     move.l    wa_Name(a0),a0
  217.     CALLSYS    GetDiskObject        ;Read Icon File
  218.     tst.l    d0
  219.     beq.s    .wbout
  220.  
  221. ; Parse The ToolTypes
  222.  
  223.     move.l    d0,a0
  224.     movem.l    d0-d7/a0-a6,-(sp)
  225.     bsr    @TestToolTypes
  226.     movem.l    (sp)+,d0-d7/a0-a6
  227.  
  228. ; Clean Up Again
  229.  
  230.     CALLSYS    FreeDiskObject        ;Free Icon Memory Again
  231.  
  232. .wbout    move.l    a6,a1
  233.     move.l    _SysBase(a4),a6
  234.     CALLSYS    CloseLibrary        ;Close Icon.library
  235.     ENDC
  236.  
  237. .wbout1    bra.s    .wecont
  238.  
  239. ;*************************************************************************
  240. ;* CLI Startup                                                           *
  241. ;*************************************************************************
  242.  
  243. .itcli
  244.     IFD    CLIARGS
  245.     IFD    NOCLIBACK
  246.     moveq    #0,d5
  247.     ENDC
  248.  
  249.     move.l    d6,a6            ;DOSBase
  250.     move.l    #_CLIString,d1
  251.     move.l    #_ArgList,d2
  252.     moveq    #0,d3
  253.     CALLSYS    ReadArgs        ;Read & Parse Arguments
  254.     tst.l    d0
  255.     bne.s    .argok
  256.     CALLSYS    IoErr            ;Get Error Code
  257.  
  258.     IFD    NOPAR
  259.     cmp.w    #ERROR_REQUIRED_ARG_MISSING,d0
  260.     beq.s    .getstk
  261.     ENDC
  262.  
  263.     move.l    d0,d1
  264.     moveq    #0,d2
  265.     CALLSYS    PrintFault        ;Print Error Text
  266.     bra    .geout1
  267.  
  268. .argok    move.l    d0,a2
  269.  
  270.     IFD    NOCLIBACK
  271.     lea    _ArgList(a4),a0
  272.     moveq    #NOCLIBACK-1,d0
  273. .loop    tst.l    (a0)+
  274.     bne.s    .gotarg
  275.     dbra    d0,.loop
  276.     bra.s    .getstk
  277.  
  278. .gotarg    moveq    #1,d5
  279.     ENDC
  280.     ENDC
  281.  
  282. .getstk    move.l    pr_CLI(a5),d0
  283.     lsl.l    #2,d0
  284.     move.l    d0,a0
  285.     move.l    cli_DefaultStack(a0),-(sp)
  286.  
  287. ;*************************************************************************
  288. ;* Start Main Program                                                    *
  289. ;*************************************************************************
  290.  
  291. .wecont
  292.     IFD    BACK
  293.     IFD    CLIARGS
  294.     move.l    a2,CliArg(a4)
  295.     ENDC
  296.  
  297.     IFD    NOCLIBACK
  298.     tst.l    d5
  299.     bne.s    .normst
  300.     ENDC
  301.  
  302.     move.l    d6,a6            ;DOSBase
  303.     lea    __startup-4(pc),a0
  304.     clr.l    (a0)
  305.  
  306.     lea    .protag(pc),a1
  307.     move.l    #.Main,4(a1)        ;Store Start Address To Run
  308.  
  309.     move.l    (sp)+,d0        ;Get Stack Size
  310.     cmp.l    ___stack(a4),d0
  311.     bge.s    .stkok
  312.     move.l    ___stack(a4),d0
  313. .stkok    move.l    d0,12(a1)        ;Store Stack Size
  314.  
  315.     IFD    PRIGET
  316.     move.l    _ArgList+(PRIGET-1)*4(a4),d1
  317.     beq.s    .nopri
  318.     move.l    d1,a0
  319.     move.l    (a0),20(a1)        ;Store Priority
  320.     ENDC
  321.  
  322. .nopri    move.l    a1,d1
  323.     CALLSYS    CreateNewProc        ;Start New Process
  324.  
  325.     move.l    _SysBase(a4),a6
  326.     move.l    d6,a1            ;DOSBase
  327.     CALLSYS    CloseLibrary        ;Close Dos.library
  328.  
  329.     IFD    NOCLIBACK
  330.     bra    .geout1
  331.     ENDC
  332.     ENDC
  333.  
  334. ; No Background Process Startup
  335.  
  336.     IF    BACK1!NOCLI
  337. .normst    move.l    _SysBase(a4),a6
  338.     move.l    (sp)+,d0        ;Get Stack Size
  339.     cmp.l    ___stack(a4),d0
  340.     bge.s    .main
  341.  
  342. ; The Stack Isn't Big Enough, so We Allocate A New One.
  343.  
  344.     move.l    ___stack(a4),d0
  345.     move.l    #MEMF_CLEAR!MEMF_PUBLIC,d1
  346.     CALLSYS    AllocMem
  347.     move.l    d0,NewStk(a4)
  348.     beq.s    .stkfail
  349.  
  350.     move.l    d0,StkLower(a4)
  351.     add.l    ___stack(a4),d0
  352.     move.l    d0,StkPointer(a4)
  353.     move.l    d0,StkUpper(a4)
  354.     lea    StkLower(a4),a0
  355.     CALLSYS    StackSwap        ;Swap The Stacks
  356.  
  357. ; Setup New Priority And Start The Main Function
  358.  
  359. .main
  360.     IFD    PRI
  361.     move.l    a5,a1
  362.     moveq    #PRI,d0
  363.     CALLSYS    SetTaskPri
  364.     ENDC
  365.  
  366.     movem.l    d2-d7/a2-a6,-(sp)
  367.     bsr    @main
  368.     movem.l    (sp)+,d2-d7/a2-a6
  369.  
  370. ; Remove Stack Again
  371.  
  372.     tst.l    NewStk(a4)
  373.     beq.s    .norem
  374.     move.l    _SysBase(a4),a6
  375.     lea    StkLower(a4),a0
  376.     CALLSYS    StackSwap        ;Swap Back To Old Stack
  377.  
  378.     move.l    NewStk(a4),a1
  379.     move.l    ___stack(a4),d0
  380.     CALLSYS    FreeMem            ;Free Stack
  381.  
  382. .norem
  383. .stkfail
  384.     IFD    CLIARGS
  385.     move.l    d6,a6            ;DOSBase
  386.     move.l    a2,d1
  387.     CALLSYS    FreeArgs        ;Free CLI Arguments
  388.     ENDC
  389.  
  390.     move.l    _SysBase(a4),a6
  391.  
  392.     IFD    UTILLIB
  393.     move.l    _UtilityBase(a4),a1
  394.     CALLSYS    CloseLibrary        ;Close Utility.library
  395.     ENDC
  396.  
  397.     move.l    _IntuitionBase(a4),a1
  398.     CALLSYS    CloseLibrary        ;Close Intuition.library
  399.  
  400.     move.l    d6,a1            ;DOSBase
  401.     CALLSYS    CloseLibrary        ;Close Dos.library
  402.     ENDC
  403.  
  404. .geout1    move.l    a3,d0
  405.     beq.s    .getout
  406.     CALLSYS    Forbid
  407.     move.l    a3,a1
  408.     CALLSYS    ReplyMsg        ;Reply WorkBench Message
  409.  
  410. .getout    moveq    #0,d0
  411.     rts
  412. ;-------------------------------------------------------------------------
  413. ; Get WorkBench Message
  414. ;
  415.  
  416. .gework    lea    pr_MsgPort(a5),a0
  417.     CALLSYS    WaitPort        ;Wait For Message
  418.     lea    pr_MsgPort(a5),a0
  419.     CALLSYS    GetMsg            ;And Get It
  420.     move.l    d0,a3
  421.     rts
  422. ;-------------------------------------------------------------------------
  423. ; Show Kickstart Error Message
  424. ;
  425.  
  426. .kicker    move.l    _IntuitionBase(a4),a6
  427.     sub.l    a0,a0
  428.     lea    .bodstr(pc),a1
  429.     sub.l    a2,a2
  430.     lea    .negstr(pc),a3
  431.     moveq    #0,d0
  432.     moveq    #0,d1
  433.     move.w    #320,d2
  434.     moveq    #60,d3
  435.     CALLSYS    AutoRequest        ;Show Error Requester
  436. ;-------------------------------------------------------------------------
  437. ; Error Out
  438. ;
  439.  
  440. .errou1    move.l    _SysBase(a4),a6
  441.     move.l    _IntuitionBase(a4),a1
  442.     CALLSYS    CloseLibrary        ;Close Intuition.library
  443.  
  444. .errout    tst.l    pr_CLI(a5)        ;Started From WorkBench
  445.     bne.s    .nowork
  446.     bsr.s    .gework
  447.     CALLSYS    Forbid
  448.     move.l    a3,a1
  449.     CALLSYS    ReplyMsg
  450.  
  451. .nowork    moveq    #0,d0
  452.     rts
  453. ;-------------------------------------------------------------------------
  454. ; Data Area
  455. ;
  456.  
  457.     IFD    BACK
  458. .protag    dc.l    NP_Entry,0        ;Do NOT Change Order Of The Tags!!
  459.     dc.l    NP_StackSize,0
  460.     dc.l    NP_Priority
  461.     IFND    PRI
  462.     dc.l    0
  463.     ELSE
  464.     dc.l    PRI
  465.     ENDC
  466.     dc.l    NP_Name,_ProgramName
  467.     dc.l    TAG_END
  468.     ENDC
  469.  
  470. .bodstr    dc.b    -1,-1,0,0
  471.     dc.w    14,14
  472.     dc.l    0,.bodtxt,0
  473.  
  474. .negstr    dc.b    -1,-1,0,0
  475.     dc.w    4,4
  476.     dc.l    0,.negtxt,0
  477.  
  478. .bodtxt    dc.b    "You need Kickstart "
  479.     IFD    KICKVER
  480.     dc.b    KICKVER/10+48,KICKVER-(KICKVER/10*10)+48
  481.     ELSE
  482.     dc.b    "37"
  483.     ENDC
  484.     dc.b    " or higher to run this program.",0
  485.  
  486. .negtxt    dc.b    "ABORT",0
  487.  
  488. .dosnam    dc.b    "dos.library",0
  489. .intnam    dc.b    "intuition.library",0
  490.  
  491.     IFD    UTILLIB
  492. .utinam    dc.b    "utility.library",0
  493.     ENDC
  494.  
  495.     IFD    WBARGS
  496. .icnnam    dc.b    "icon.library",0
  497.     ENDC
  498. ;-------------------------------------------------------------------------
  499. ; Background Process Startup
  500. ;
  501.  
  502.     IFD    BACK
  503.     SECTION    text,CODE
  504.  
  505. .Main    lea    _LinkerDB,a4        ;Base Register
  506.     move.l    _SysBase(a4),a6
  507.     lea    .DOSNAM(a4),a1
  508.     moveq    #0,d0
  509.     CALLSYS    OpenLibrary        ;Open Dos Library
  510.     move.l    d0,_DOSBase(a4)
  511.  
  512.     bsr    @main            ;Call Main Function
  513.  
  514.     IFD    CLIARGS
  515.     move.l    _DOSBase(a4),a6
  516.     move.l    CliArg(a4),d1
  517.     CALLSYS    FreeArgs        ;Free CLI Arguments
  518.     ENDC
  519.  
  520.     move.l    _SysBase(a4),a6
  521.     IFD    UTILLIB
  522.     move.l    _UtilityBase(a4),a1
  523.     CALLSYS    CloseLibrary        ;Close Utility.library
  524.     ENDC
  525.  
  526.     move.l    _IntuitionBase(a4),a1
  527.     CALLSYS    CloseLibrary        ;Close Intuition.library
  528.  
  529.     move.l    _DOSBase(a4),a5
  530.     move.l    a5,a1
  531.     CALLSYS    CloseLibrary        ;Close Dos.library
  532.  
  533.     move.l    a5,a6
  534.     lea    .Main-4(pc),a0
  535.     move.l    a0,d1
  536.     lsr.l    #2,d1
  537.     jmp    _LVOUnLoadSeg(a6)    ;Free Program Memory
  538.     ENDC
  539. ;-------------------------------------------------------------------------
  540.     SECTION    __MERGED,DATA
  541.  
  542. .DOSNAM    dc.b    "dos.library",0
  543. ;-------------------------------------------------------------------------
  544.     SECTION    __MERGED,BSS
  545.  
  546.     XDEF    _SysBase
  547.     XDEF    _DOSBase
  548.     XDEF    _IntuitionBase
  549.  
  550. _SysBase    ds.l    1
  551. _DOSBase    ds.l    1
  552. _IntuitionBase    ds.l    1
  553.  
  554.     IFD    UTILLIB
  555.     XDEF    _UtilityBase
  556. _UtilityBase    ds.l    1
  557.     ENDC
  558.  
  559.     IFD    WBARGS
  560.     XDEF    _IconBase
  561. _IconBase    ds.l    1
  562.     ENDC
  563.  
  564.     IFD    BACK
  565.     IFD    CLIARGS
  566. CliArg    ds.l    1
  567.     ENDC
  568.     ENDC
  569.  
  570.     IF    BACK1!NOCLI
  571. NewStk        ds.l    1
  572. StkLower    ds.l    1
  573. StkUpper    ds.l    1
  574. StkPointer    ds.l    1
  575.     ENDC
  576. ;-------------------------------------------------------------------------
  577.     END
  578.