home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d178 / ls.lha / Ls / c2.a < prev    next >
Text File  |  1989-02-04  |  9KB  |  397 lines

  1. *
  2. * C initial startup procedure under AmigaDOS
  3. * Use the following command line to make c.o
  4. * asm -u -iINCLUDE: c.a
  5. *
  6. * Use the following command line to make cres.o
  7. * asm -u -dRESIDENT -iINCLUDE: -ocres.o c.a
  8.  
  9.     IDNT    "c2.a"
  10.  
  11.     OPTION    L
  12.     LISTSYMS
  13.     BASEREG    B
  14.     SMALLOBJ
  15.     OPTIMON
  16.     ADDSYM
  17.     DEBUG
  18.  
  19. * --------------------------------------------------------------------- *
  20. * Macros:
  21. * --------------------------------------------------------------------- *
  22. SYS    MACRO    *
  23.     IFGT    NARG-2
  24.     FAIL    !!!
  25.     ENDC
  26.     IFEQ    NARG-2
  27.     MOVE.L    \2,a6
  28.     ENDC
  29.     JSR    _LVO\1(a6)
  30.     ENDM
  31.  
  32. XLVO    MACRO    *
  33.     XREF    _LVO\1
  34.     ENDM
  35.  
  36. * --------------------------------------------------------------------- *
  37. * Equates:
  38. * --------------------------------------------------------------------- *
  39. RESIDENT    EQU    1
  40.  
  41. MEMF_PUBLIC    EQU    $1
  42. MEMF_CLEAR    EQU    $10000
  43. MEMFLAGS    EQU    MEMF_CLEAR+MEMF_PUBLIC
  44. AbsExecBase    EQU    $0004
  45. cli_CommandName    EQU    $10
  46. pr_CLI        EQU    $AC
  47. pr_CurrentDir    EQU    $98
  48. ThisTask    EQU    $114
  49.  
  50. _LVOCloseLibrary EQU    $FE62
  51. _LVOAllocMem    EQU    $FF3A
  52. _LVOFreeMem    EQU    $FF2E
  53. _LVOSetSignal    EQU    $FECE
  54. _LVOOpenLibrary    EQU    $FDD8
  55.  
  56.     XREF    _DOSBase
  57.     XREF    _LinkerDB        ; linker defined base value
  58.     XREF    __BSSBAS        ; linker defined base of BSS
  59.     XREF    __BSSLEN        ; linker defined length of BSS
  60.  
  61.      IFD    RESIDENT
  62.     XREF    _RESLEN
  63.     XREF    _RESBASE
  64.     XREF    _NEWDATAL
  65.      ENDC
  66.     
  67.     XREF    __main            ; Name of C program to start with.
  68.     XREF    _MemCleanup        ; Free all allocated memory
  69.     XREF    ___fpinit        ; initialize floating point
  70.     XREF    ___fpterm        ; terminate floating point
  71.  
  72. * --------------------------------------------------------------------- *
  73.     SECTION    TEXT,CODE
  74. * --------------------------------------------------------------------- *
  75.     XDEF    zzstart
  76. zzstart:
  77.     move.l    a0,a2            ; save command pointer
  78.     move.l    d0,d2            ; and command length
  79.     lea    _LinkerDB,a4        ; load base register
  80.  
  81.      IFND    RESIDENT
  82.     lea    __BSSBAS,a3        ; get base of BSS
  83.     moveq    #0,d1
  84.     move.l    #__BSSLEN,d0        ; get length of BSS in longwords
  85.      bra.b    clr_lp             ; and clear for length given
  86. clr_bss move.l    d1,(a3)+
  87. clr_lp    dbf    d0,clr_bss
  88.      ENDC
  89.  
  90.      IFD    RESIDENT
  91.     movea.l    AbsExecBase,a6
  92.  
  93.     movem.l    d0-d1/a0-a2,-(sp)
  94.     sub.l    #_RESBASE,a4
  95.     move.l    #_RESLEN,d0
  96.     move.l    #MEMFLAGS,d1
  97.     SYS    AllocMem
  98.     tst.l    d0
  99.      beq.w    abort
  100.     move.l    d0,a0
  101.     move.l     d0,a2
  102.  
  103. ;a2 now has difference
  104.     move.l    d0,a1
  105.     move.l    #_NEWDATAL,d0
  106. ;copy data over
  107. cpy:    move.l    (a4)+,(a0)+
  108.     subq.l    #1,d0
  109.      bne    cpy
  110. ;a4 now points at number of relocs
  111.     move.l    (a4)+,d0
  112. reloc:     beq.b    nreloc
  113.     move.l    a1,a0
  114.     add.l    (a4)+,a0        ; a0 now has add of reloc
  115.     add.l    (a0),a2
  116.     move.l    a2,(a0) 
  117.     move.l    a1,a2            ; restore offset
  118.     subq.l    #1,d0
  119.     bra    reloc
  120.     
  121. nreloc: move.l    a1,a4            ; set up new base register
  122.     add.l    #_RESBASE,a4
  123.     movem.l    (sp)+,d0-d1/a0-a2
  124.      ENDC
  125.  
  126.     movea.l    AbsExecBase,a6
  127.     move.l    a6,_SysBase(a4)
  128.     move.l    sp,__StackPtr(a4)    ; Save stack ptr
  129.     clr.l    _WBenchMsg(a4)
  130.  
  131. * get the address of our task
  132.     move.l    ThisTask(a6),a3
  133.  
  134. *-    clear any pending signals
  135.     moveq    #0,d0
  136.     move.l    #$00003000,d1
  137.     SYS    SetSignal
  138.     
  139. * are we running as a son of Workbench?
  140.     move.l    pr_CurrentDir(a3),_curdir(a4)
  141.  
  142.     IFD    WBENCH
  143.     tst.l    pr_CLI(a3)
  144.      beq    fromWorkbench
  145.     ENDC
  146.  
  147. *=======================================================================
  148. *====== CLI Startup Code ===============================================
  149. *=======================================================================
  150. *
  151. ; Entry: d2 = command length
  152. ;     a2 = Command pointer
  153. fromCLI:
  154.     move.l    sp,d0             ; get top of stack
  155.     sub.l    4(sp),d0        ; compute bottom 
  156.     add.l    #128,d0             ; allow for parms overflow
  157.     move.l    d0,__base(a4)        ; save for stack checking
  158.  
  159. * attempt to open DOS library:
  160.     bsr.w    openDOS
  161.  
  162. * find command name:
  163.     move.l    pr_CLI(a3),a0
  164.     add.l    a0,a0             ; bcpl pointer conversion
  165.     add.l    a0,a0
  166.     move.l    cli_CommandName(a0),a1
  167.     add.l    a1,a1             ; bcpl pointer conversion
  168.     add.l    a1,a1
  169.  
  170. * collect parameters:
  171.     move.l    d2,d0            ; get command line length
  172.     moveq.l    #0,d1
  173.     move.b    (a1)+,d1
  174.     move.l    a1,__ProgramName(a4)
  175.     add.l    d1,d0            ; add length of command name
  176.     addq.l    #1,d0            ; allow for space after command 
  177.  
  178.     clr.w    -(sp)            ; set null terminator for command line
  179.     addq.l    #1,d0            ; force to even number of bytes
  180.     andi.w    #$fffe,d0        ; (round up)
  181.     sub.l    d0,sp            ; make room on stack for command line
  182.     subq.l    #2,d0
  183.     clr.w    0(sp,d0)
  184.  
  185. * copy command line onto stack
  186.     move.l    d2,d0            ; get command line length
  187.     subq.l    #1,d0
  188.     add.l    d1,d2
  189.  
  190. copy_line:
  191.     move.b    0(a2,d0.w),0(sp,d2.w)    ; copy command line to stack
  192.     subq.l    #1,d2
  193.     dbf    d0,copy_line
  194.     move.b    #' ',0(sp,d2.w)         ; add space between command and parms
  195.     subq.l    #1,d2
  196.  
  197. copy_cmd:
  198.     move.b    0(a1,d2.w),0(sp,d2.w)    ; copy command name to stack
  199.     dbf    d2,copy_cmd
  200.     move.l    sp,a1
  201.     move.l    a1,-(sp)        ; push command line address
  202.  
  203.     IFD    WBENCH
  204.     bra    main            ; call C entrypoint
  205. * --------------------------------------------------------------------- *
  206. * Workbench Startup Code
  207. * --------------------------------------------------------------------- *
  208. fromWorkbench:
  209.     move.l    TC_SPLOWER(a3),__base(a4) ; set base of stack
  210.     moveq     #127,d0
  211.     addq.l    #1,d0            ; Efficient way of getting in 128
  212.     add.l    d0,__base(a4)        ; allow for parms overflow
  213.  
  214. * open the DOS library:
  215.     bsr.w    openDOS
  216.  
  217. * we are now set up. wait for a message from our starter
  218.     lea    pr_MsgPort(a3),a0    ; our process base
  219.     SYS    WaitPort
  220.     lea    pr_MsgPort(a3),a0    ; our process base
  221.     SYS    GetMsg
  222.     move.l    d0,_WBenchMsg(a4)
  223.     move.l    d0,-(sp)
  224.  
  225.     move.l    d0,a2            ; get first argument
  226.     move.l    sm_ArgList(a2),d0
  227.      beq    do_cons
  228.     move.l    _DOSBase(a4),a6
  229.     move.l    d0,a0
  230.     move.l    wa_Lock(a0),d1
  231.     move.l    d1,_curdir(a4)
  232.     SYS    CurrentDir
  233. do_cons:
  234.     move.l    sm_ToolWindow(a2),d1    ; get the window argument
  235.      beq    do_main
  236.     move.l    #MODE_OLDFILE,d2
  237.     SYS    Open
  238.     move.l    d0,_stdin(a4)
  239.      beq    do_main
  240.     lsl.l    #2,d0
  241.     move.l    d0,a0
  242.     move.l    fh_Type(a0),pr_ConsoleTask(a3)
  243. do_main:
  244.     move.l    _WBenchMsg(a4),a0    ; get address of workbench message
  245.     move.l    a0,-(sp)        ; push argv
  246.     pea    _NULL(a4)        ; push argc
  247.     move.l    sm_ArgList(a0),a0    ; get address of arguments
  248.     move.l    wa_Name(a0),__ProgramName(a4)     ; get name of program
  249.     ENDC
  250.  
  251. * --------------------------------------------------------------------- *
  252. * Common CLI/WBENCH code
  253. * --------------------------------------------------------------------- *
  254. main:
  255.      IFD    FLOAT
  256.     jsr    ___fpinit(pc)        ; Initialize floating point
  257.      ENDC
  258.     jsr    __main(pc)        ; call C entrypoint
  259.     moveq.l    #0,d0            ; set successful status
  260.     bra.b    exit2
  261.  
  262.     XDEF    _XCEXIT
  263. _XCEXIT:
  264.     move.l    4(sp),d0        ; extract return code
  265.  
  266.     XDEF    xXCEXIT
  267. xXCEXIT:
  268. exit2:
  269.      IFD    ERRTRAPS
  270.     move.l    d0,-(sp)
  271.     move.l    __ONEXIT(a4),d0        ; exit trap function?
  272.      beq.b    exit3
  273.     move.l    d0,a0
  274.     jsr    (a0)
  275. exit3:
  276.      ENDC
  277.  
  278.      IFD    MALLOC
  279.     jsr    _MemCleanup(pc)        ; cleanup leftover memory alloc.
  280.      ENDC
  281.  
  282.     movea.l    AbsExecBase,a6
  283.     move.l    _DOSBase(a4),a1
  284.     SYS    CloseLibrary        ; close Dos library
  285.  
  286.      IFD    FLOAT
  287.     jsr    ___fpterm(pc)        ; clean up any floating point
  288.      ENDC
  289.  
  290.      IFD    WBENCH
  291. done_1c:
  292. * if we ran from CLI, skip workbench cleanup:
  293.     tst.l    _WBenchMsg(a4)
  294.      beq    exitToDOS
  295.     move.l    _stdin(a4),d1
  296.      beq.b    done_4
  297.     SYS    Close
  298. done_4:
  299. * return the startup message to our parent
  300. *     we forbid so workbench can't UnLoadSeg() us
  301. *     before we are done:
  302.     movea.l    AbsExecBase,a6
  303.     SYS    Forbid
  304.     move.l     _WBenchMsg(a4),a1
  305.     SYS    ReplyMsg
  306.      ENDC
  307.  
  308. * this rts sends us back to DOS:
  309. exitToDOS:
  310.      IFD    RESIDENT
  311.     move.l    #_RESLEN,d0
  312.     move.l     a4,a1
  313.     sub.l    #_RESBASE,a1
  314.     movea.l    AbsExecBase,a6
  315.     SYS    FreeMem
  316.      ENDC
  317.     
  318.     move.l    (sp)+,d0
  319.     movea.l    __StackPtr(a4),sp    ; restore stack ptr
  320.  
  321.     XDEF    xchkabort
  322. xchkabort:
  323.     rts
  324.  
  325.      IFD    RESIDENT
  326. abort:
  327.     movem.l    (sp)+,d0-d1/a0-a2
  328.     rts
  329.      ENDC
  330.  
  331. * --------------------------------------------------------------------- *
  332. noDOS:
  333.     moveq.l    #100,d0
  334.     bra    exit2
  335.  
  336. * --------------------------------------------------------------------- *
  337. *    Open the DOS library:
  338. * --------------------------------------------------------------------- *
  339. openDOS:
  340.     lea    DOSName(pc),a1
  341.     moveq.l #0,d0
  342.     SYS    OpenLibrary
  343.     move.l    d0,_DOSBase(a4)
  344.      beq    noDOS
  345.     rts
  346.  
  347. DOSName:    dc.b    'dos.library',0
  348.  
  349. * --------------------------------------------------------------------- *
  350.     section __MERGED,BSS
  351. * --------------------------------------------------------------------- *
  352.  
  353.     XDEF    _NULL,_SysBase,_WBenchMsg
  354.     XDEF    _curdir,__mbase,__mnext,__msize,__tsize
  355.     XDEF    __oserr,__OSERR
  356.      IFD    FLOAT
  357.     XDEF    __FPERR
  358.     XDEF    __SIGFPE
  359.      ENDC
  360.      IFD    ERRTRAPS
  361.     XDEF    __ONERR,__ONEXIT,__ONBREAK
  362.     XDEF    __SIGINT
  363.      ENDC
  364.     XDEF    _stdin
  365.     XDEF    __ProgramName,__StackPtr,__base
  366.  
  367. * --------------------------------------------------------------------- *
  368. _NULL        ds.l    1         ; Huh?
  369. __base        ds.l    1         ; base of stack
  370. __mbase        ds.l    1         ; base of memory pool
  371. __mnext        ds.l    1         ; next available memory location
  372. __msize        ds.l    1         ; size of memory pool
  373. __tsize        ds.l    1         ; total size?
  374. __oserr
  375. __OSERR        ds.l    1
  376.      IFD    FLOAT
  377. __FPERR        ds.l    1
  378. __SIGFPE    ds.l    1
  379.      ENDC
  380.      IFD    ERRTRAPS
  381. __SIGINT    ds.l    1
  382. __ONERR        ds.l    1
  383. __ONEXIT    ds.l    1
  384. __ONBREAK    ds.l    1
  385.      ENDC
  386. _curdir        ds.l    1
  387. _SysBase    ds.l    1
  388. _WBenchMsg    ds.l    1
  389. __StackPtr    ds.l    1
  390. _stdin        ds.l    1
  391. __ProgramName    ds.l    1
  392.  
  393. * --------------------------------------------------------------------- *
  394.     END
  395. * --------------------------------------------------------------------- *
  396.