home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 5 / memwatch / wbc.a < prev    next >
Encoding:
Text File  |  1994-01-27  |  7.2 KB  |  315 lines

  1.     section    text,CODE
  2. * C initial startup procedure under AmigaDOS
  3. * Requirements:
  4.  
  5.     INCLUDE    "exec/types.i"
  6.     INCLUDE "exec/alerts.i"
  7.     INCLUDE "exec/nodes.i"
  8.     INCLUDE "exec/lists.i"
  9.     INCLUDE "exec/ports.i"
  10.     INCLUDE "exec/libraries.i"
  11.     INCLUDE "exec/tasks.i"
  12.     INCLUDE "libraries/dos.i"
  13.     INCLUDE "libraries/dosextens.i"
  14.     INCLUDE "workbench/startup.i"
  15.  
  16. ; some usefull macros:
  17. xlib    macro
  18.     xref    _LVO\1
  19.     endm
  20.  
  21. callsys    macro
  22.     CALLLIB    _LVO\1
  23.     endm
  24.     
  25.  
  26.     xdef    _XCEXIT            * exit(code) is standard way to leave C.
  27.     xdef    _SaveMem
  28.     xdef    _ValidateMem
  29.  
  30.     xref    _LinkerDB        * linker defined base value
  31.  
  32.     xref    _Debug
  33.     xref    __main            * Name of C program to start with.
  34.     xref    _AbsExecBase
  35.     xref    _FindTask
  36.     xref    _DOSBase
  37.  
  38.     xlib    Alert
  39.     xlib    FindTask
  40.     xlib    Forbid
  41.     xlib    GetMsg
  42.     xlib    OpenLibrary
  43.     xlib    CloseLibrary
  44.     xlib    ReplyMsg
  45.     xlib    Wait
  46.     xlib    WaitPort
  47.     xlib    Open
  48.     xlib    Close
  49.     xlib    CurrentDir
  50.  
  51. *** this is the number of LONGWORDS at the start of memory to be checked
  52. MEMSAVESIZE    equ    64
  53. start:
  54.     move.l    d0,dosCmdLen
  55.     move.l    a0,dosCmdBuf
  56.     move.l    a7,d0            ; save old stack ptr
  57.     movem.l    d1-d6/a0-a6,-(a7)
  58.     move.l    d0,a5
  59.     move.l    _AbsExecBase,a6
  60.     move.l    a6,_SysBase
  61.     move.l    a7,__StackPtr        * Save stack ptr
  62.     clr.l    _WBenchMsg
  63.  
  64. ;------ get the address of our task
  65.     suba.l    a1,a1
  66.     callsys    FindTask
  67.     move.l    d0,a4
  68.  
  69. ;------ are we running as a son of Workbench?
  70.     move.l    pr_CurrentDir(A4),_curdir
  71.     tst.l    pr_CLI(A4)
  72.     beq    fromWorkbench
  73.  
  74. ;=======================================================================
  75. ;====== CLI Startup Code ===============================================
  76. ;=======================================================================
  77.  
  78. fromCLI:
  79.     move.l    a5,D0        ; get top of stack
  80.     sub.l    4(a5),D0    ; compute bottom 
  81.     move.l    D0,__base    ; save for stack checking
  82. ;------    attempt to open DOS library:
  83.     bsr    openDOS
  84.  
  85. ;------ find command name:
  86.     move.l    #start,a0
  87.     clr.l    -(sp)
  88.     jsr    _FindTask
  89.     addq.l    #4,sp
  90.     move.l    d0,a0
  91.     move.l    pr_CLI(a0),a0
  92.     add.l   a0,a0        ; bcpl pointer conversion
  93.     add.l   a0,a0
  94.     move.l    cli_CommandName(a0),a1
  95.     add.l   a1,a1        ; bcpl pointer conversion
  96.     add.l   a1,a1
  97.  
  98. ;------    collect parameters:
  99.     move.l    dosCmdLen,d0        ; get command line length
  100.     move.l    a1,__ProgramName
  101.     addq.l    #1,d0            ; allow for space after command    
  102.  
  103.     clr.w    -(A7)            ; set null terminator for command line
  104.     addq.l    #1,D0            ; force to even number of bytes
  105.     andi.w    #$fffe,D0        ;(round up)
  106.     sub.l    D0,A7            ; make room on stack for command line
  107.     subq.l    #2,D0
  108.     clr.w    0(A7,D0)
  109.  
  110. ;------ copy command line onto stack
  111.     move.l    dosCmdLen,d0        ; get command line length
  112.     move.l    dosCmdBuf,a0
  113.     move.l    d0,d2
  114.     subq.l    #1,d0
  115.  
  116. copy_line:
  117.     move.b    0(A0,D0.W),0(A7,D2.W)    ; copy command line to stack
  118.     subq.l    #1,d2
  119.     dbf    d0,copy_line
  120.     move.b    #' ',0(a7,d2.w)        ; add space between command and parms
  121.     subq.l    #1,d2
  122.  
  123. copy_cmd:
  124.     move.l    A7,A1
  125.     move.l    A1,-(A7)        ; push command line address
  126.     bra    goto_main                * call C entrypoint
  127.  
  128. ;=======================================================================
  129. ;====== Workbench Startup Code =========================================
  130. ;=======================================================================
  131.  
  132. fromWorkbench:
  133.  
  134.     move.l    TC_SPLOWER(a4),__base    ; set base of stack
  135. ;------ open the DOS library:
  136.     bsr    openDOS
  137.  
  138. ;------ we are now set up.  wait for a message from our starter
  139.     bsr    waitmsg
  140.     move.l    d0,_WBenchMsg
  141.     move.l    d0,-(SP)
  142. ;
  143.     move.l    d0,a2            ; get first argument
  144.     move.l    sm_ArgList(a2),d0
  145.     beq.s    do_cons
  146.     move.l    _DOSBase,a6
  147.     move.l    d0,a0
  148.     move.l    wa_Lock(a0),d1
  149.     move.l    d1,_curdir
  150.     callsys    CurrentDir
  151. do_cons:
  152.     move.l    sm_ToolWindow(a2),d1    ; get the window argument
  153.     beq.s    do_main
  154.     move.l    #MODE_OLDFILE,d2
  155.     callsys    Open
  156.     move.l    d0,stdin
  157.     beq.s    do_main
  158.     lsl.l    #2,d0
  159.     move.l    d0,a0
  160.     move.l    fh_Type(a0),pr_ConsoleTask(A4)
  161. do_main:
  162.     move.l    _WBenchMsg,a0        ; get address of workbench message
  163.     move.l    sm_ArgList(a0),a0    ; get address of arguments
  164.     move.l    wa_Name(a0),__ProgramName    ; get name of program
  165.     moveq    #0,d0
  166.     move.l    d0,-(A7)
  167.  
  168. goto_main:
  169.     lea    _LinkerDB,a6        ; load base register
  170.     jsr    __main                * call C entrypoint
  171.     moveq.l    #0,d0            ; set successful status
  172.     bra.s    exit2
  173. ;
  174.  
  175. _XCEXIT:
  176.     move.l    4(SP),d0    ; extract return code
  177. exit2:
  178.     move.l    d0,-(a7)
  179.     move.l    _SysBase,a6
  180.     move.l    _DOSBase,a1
  181.     callsys    CloseLibrary        ; close Dos library
  182. done_1b:
  183. ;------ if we ran from CLI, skip workbench cleanup:
  184.     tst.l    _WBenchMsg
  185.     beq.s    exitToDOS
  186.     move.l    _console_dev,d1
  187.     beq.s    done_2
  188.     callsys    Close
  189. done_2:
  190.     move.l    stdin,d1
  191.     beq.s    done_4
  192.     callsys    Close
  193. done_4:
  194.  
  195. ;------ return the startup message to our parent
  196. ;    we forbid so workbench can't UnLoadSeg() us
  197. ;    before we are done:
  198.     move.l    _AbsExecBase,A6
  199.     callsys Forbid
  200.     move.l    _WBenchMsg,a1
  201.     callsys    ReplyMsg
  202.  
  203. ;------ this rts sends us back to DOS:
  204. exitToDOS:
  205.     MOVE.L    (A7)+,D0
  206.     movea.l  __StackPtr,SP        * restore stack ptr
  207.     movem.l    (a7)+,d1-d6/a0-a6
  208.     rts                * and exit
  209.  
  210. ;-----------------------------------------------------------------------
  211. noDOS:
  212.         ALERT    (AG_OpenLib!AO_DOSLib)
  213.         moveq.l    #100,d0
  214.         bra    exit2
  215.  
  216.  
  217. ;-----------------------------------------------------------------------
  218. ; This routine gets the message that workbench will send to us
  219. ; called with task id in A4
  220.  
  221. waitmsg:
  222.         lea    pr_MsgPort(A4),a0      * our process base
  223.         callsys    WaitPort
  224.         lea    pr_MsgPort(A4),a0      * our process base
  225.         callsys GetMsg
  226.         rts
  227.  
  228. ;-----------------------------------------------------------------------
  229. ;  Open the DOS library:
  230.  
  231. openDOS
  232.         lea    DOSName,A1
  233.         moveq.l    #0,D0
  234.         callsys OpenLibrary
  235.         move.l    D0,_DOSBase
  236.         beq    noDOS
  237.         rts
  238.  
  239. *************************************************************************
  240. * Validate the current memory information and put up an alert if it is
  241. * in sad shape
  242. *************************************************************************
  243. _ValidateMem:
  244.     movem.L    A0/A1,-(A7)
  245.     moveq.l    #MEMSAVESIZE,D0
  246.     LEA    0,A0
  247.     LEA    savearea,A1
  248. checkit:
  249.     cmp.l    (A0)+,(A1)+
  250.     dbne    D0,checkit
  251.     addq    #1,D0
  252.     beq    allok
  253. * restore what got scrod
  254. *    first format the message to be printed
  255. *    we get a pointer to the address and the data portion
  256.     move.l    -(A0),D0    get the value that was put in
  257.     move.l    -(A1),(A0)    and replace with what should have been there
  258.     move.l    12(A7),A1    point to where we should save the result
  259.     move.l    D0,(A1)        save the value
  260.     move.l    A0,4(A1)    and the address for the routine to munge
  261.     moveq.l    #1,D0        set a result value to indicate failure
  262. allok:
  263.     movem.l    (a7)+,A0/A1
  264.     rts
  265.  
  266. _SaveMem:
  267.     movem.L    D0/A0/A1,-(A7)
  268.     moveq.l    #MEMSAVESIZE,D0
  269.     LEA    0,A0
  270.     LEA    savearea,A1
  271. savelp:
  272.     move.l    (A0)+,(A1)+
  273.     DBF    D0,savelp
  274.     movem.l    (A7)+,D0/A0/A1
  275.     rts
  276.  
  277.     section    "__MERGED",DATA
  278. ;
  279.     XDEF    _NULL,_SysBase,_LoadAddress,_console_dev,_WBenchMsg
  280.     XDEF    _curdir,__mbase,__mnext,__msize,__tsize
  281.     XDEF    __oserr,__OSERR,__FPERR,__SIGFPE,__ONERR,__ONEXIT,__ONBREAK
  282.     XDEF    __SIGINT,__ECS
  283.     XDEF    __ProgramName,__StackPtr,__base
  284. ;
  285. _NULL        dc.l    0        ;
  286. savearea    ds.l    MEMSAVESIZE+2    ;for save and compare of memory
  287. __base        dc.l    0        ; base of stack
  288. __mbase        dc.l    0        ; base of memory pool
  289. __mnext        dc.l    0        ; next available memory location
  290. __msize        dc.l    0        ; size of memory pool
  291. __tsize        dc.l    0        ; total size?
  292. __oserr        equ    *
  293. __OSERR        dc.l    0
  294. __FPERR        dc.l    0
  295. __SIGFPE    dc.l    0
  296. __SIGINT    dc.l    0
  297. __ONERR        dc.l    0
  298. __ONEXIT    dc.l    0
  299. __ONBREAK    dc.l    0
  300. __ECS        dc.l    0        ; extended character set flag
  301. _curdir        dc.l    0
  302. _console_dev    dc.l    0
  303. _SysBase    dc.l    0
  304. _LoadAddress    dc.l    0            ; program load address
  305. _WBenchMsg    dc.l    0
  306. __StackPtr    dc.l    0
  307. dosCmdLen    dc.l    0
  308. dosCmdBuf    dc.l    0
  309. stdin        dc.l    0
  310. __ProgramName    dc.l    0
  311. DOSName     DOSNAME
  312.  
  313.     END
  314.