home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 387b.lha / dice_v2.02 / lib / amiga / c.a next >
Encoding:
Text File  |  1990-05-30  |  5.8 KB  |  236 lines

  1.  
  2.         ;   C.A
  3.         ;
  4.         ;   (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  5.         ;
  6.         ;   Amiga startup code for non-resident and resident DCC
  7.         ;   programs.
  8.         ;
  9.         ;   DLINK:
  10.         ;
  11.         ;   * __BSS_LEN   : # bytes of bss (occurs after DATA)
  12.         ;   * __DATA_LEN  : # bytes of initialized data to copy (if resident)
  13.         ;   * __DATA_BAS  : base of data
  14.         ;   * __RESIDENT  : is this code residentable?    If this variable
  15.         ;            is set then there are no absolute data/bss
  16.         ;            references except __DATA_BAS and there is
  17.         ;            NO BSS MEMORY ALLOCATED AFTER THE DATA
  18.         ;            SECTION.
  19.         ;
  20.         ;            If this variable is not set then absolute
  21.         ;            references refer to the same storage as
  22.         ;            A4 relative accesses and uninitialized
  23.         ;            BSS space exists just after the data section.
  24.         ;
  25.         ;            Note that in the large-data model + fragmented
  26.         ;            sections, there are usually real BSS hunks
  27.         ;            floating about that have been zerod by
  28.         ;            LoadSeg() (in this case __BSS_LEN could be 0)
  29.         ;
  30.         ;   COMPILER:
  31.         ;
  32.         ;   * If -r option specified when compiling then the compiler
  33.         ;     will generate autoinit code to handle data initialization
  34.         ;     that would otherwise require a reloc32 hunk.
  35.         ;
  36.         ;   * If -r or -md specified compiler references all data items
  37.         ;     using A4-Relative addressing.
  38.         ;
  39.         ;   C LIB CODE:
  40.         ;
  41.         ;   * Resident: Allocate (__DATA_LEN+__BSS_LEN)*4
  42.         ;     Not Res : zero out preallocated BSS.
  43.         ;
  44.         ;   * Generate A4 ref ptr to bss base + 32766 (uses Aztec's
  45.         ;     conventions).
  46.         ;
  47.         ;   * All library code uses the SMALL-DATA with alternate
  48.         ;     BSS and DATA section names (based in this source
  49.         ;     file) so the linker puts library related data/bss
  50.         ;     first.  BUT, BSS always comes after DATA unless
  51.         ;     you -frag the link (can't resident a frag'd link),
  52.         ;     so if there is more than 64KBytes of *initialized*
  53.         ;     data you must -frag the link.
  54.  
  55.         section text,code
  56.  
  57.         xref    __RESIDENT        ; (dlink), executable is residentable
  58.         xref    __BSS_LEN        ; (dlink), length of BSS
  59.         xref    __DATA_BAS        ; (dlink), base of initialized data
  60.         xref    __DATA_LEN        ; (dlink), length of data
  61.  
  62.         xref    __main            ; we call _main()
  63.  
  64.         xref    _LVOSetSignal
  65.         xref    _LVOAllocMem
  66.         xref    _LVOFreeMem
  67.  
  68.         xdef    __exit            ; we are _exit()
  69.         xdef    start
  70.         xdef    _SysBase        ; we export _SysBase
  71.  
  72. MEMF_CLEAR    equ    $00000001
  73. MEMF_PUBLIC    equ    $00010000
  74.  
  75. start:
  76.         movem.l D2-D7/A2-A6,-(sp)
  77.  
  78.         move.l    4,A6            ; EXEC base
  79.  
  80.         move.l    sp,A2
  81.         move.l    A0,-(sp)            ; save arg for _main() call
  82.         move.l    D0,-(sp)            ; save arglen for _main() call
  83.  
  84.         ;   If we are flagged resident then there is NO BSS ALLOCATED
  85.         ;   If we are not flagged resident then BSS IS ALLOCATED AFTER DATA,
  86.         ;    BUT NOT CLEARED.
  87.  
  88.         move.w    #__RESIDENT,D0
  89.         beq    snotres
  90.  
  91.         ;   Allocate BSS+DATA space and then copy static data.
  92.  
  93.         move.l    #__BSS_LEN,D0
  94.         add.l    #__DATA_LEN,D0
  95.         asl.l    #2,D0            ; x4
  96.         addq.l    #8,D0            ; MemList header extra
  97.         move.l    D0,D5            ; D5 = #bytes
  98.         move.l    #MEMF_PUBLIC,D1
  99.         jsr    _LVOAllocMem(A6)
  100.         tst.l    D0
  101.         bne    alok
  102.         move.l    A2,sp
  103.         moveq.l #-1,D0
  104.         bra    exfail
  105.  
  106. alok        move.l    D0,A0
  107.         clr.l    (A0)+               ; MemList entry next ptr
  108.         move.l    D5,(A0)+            ; MemList entry #bytes
  109.         lea    32766(A0),A4        ; SET A4
  110.         lea    -8(A0),A3           ; A3 = MemList entry base
  111.                         ; can't copy to MemList(A4) yet
  112.  
  113.  
  114.         ;   Copy data to allocated copy
  115.  
  116.                         ; A0 = dst
  117.         lea    __DATA_BAS,A1        ; A1 = src
  118.         move.l    #__DATA_LEN,D0        ; D0 = long words
  119.         bra    bssent
  120. bsslop        move.l    (A1)+,(A0)+
  121. bssent        dbf    D0,bsslop
  122.         sub.l    #$10000,D0
  123.         bcc    bsslop
  124.         bra    clrbss
  125.  
  126.  
  127.         ;   Not resident, BSS space has been allocated for us
  128.         ;   beyond the specified data, just load the base ptr
  129.  
  130. snotres     lea    __DATA_BAS,A4
  131.         lea    32766(A4),A4
  132.         sub.l    A3,A3
  133.  
  134. clrbss
  135.         ;   CLEAR BSS    &-32766(A4) + __DATA_LEN*4
  136.  
  137.         lea    -32766(A4),A0
  138.         move.l    #__DATA_LEN,D0
  139.         asl.l    #2,D0
  140.         add.l    D0,A0
  141.  
  142.         move.l    #__BSS_LEN,D0        ; longwords of bss
  143.         moveq.l #0,D1
  144.         bra    clrent
  145. clrlop        move.l    D1,(A0)+
  146. clrent        dbf    D0,clrlop
  147.         sub.l    #$10000,D0
  148.         bcc    clrlop
  149.  
  150.         move.l    A3,___MemList(A4)   ; memlist entry (if resident)
  151.         move.l    A2,__ExitSP(A4)     ; sp to restore
  152.  
  153.         moveq.l #0,D0            ; new signals
  154.         move.l    #$1000,D1        ; signal mask
  155.         jsr    _LVOSetSignal(A6)   ; clear ^C
  156.  
  157.         move.l    A6,_SysBase(A4)     ; resident segment.
  158.  
  159.         jsr    __AutoInit0        ; A6 has SYSBase
  160.         jsr    __AutoInit1        ; A6 has SYSBase
  161.         jsr    __main(PC)
  162.  
  163.         ;   fall through to low level exit... this avoids referencing
  164.         ;   exit() if the user overides _main().
  165.  
  166.         pea    0
  167.         pea    0
  168.  
  169.         ;   _EXIT()
  170.         ;
  171.         ;   since entry uses malloc we must free any incidental memory
  172.         ;   at __exit instead of _exit.
  173.  
  174. __exit:
  175.         move.l    _SysBase(A4),A6
  176.         jsr    __AutoExit1        ; A6 has SysBase
  177.         jsr    __AutoExit0        ; A6 has SysBase
  178.  
  179.         move.l    __ExitSP(A4),A5     ; get sp... because we might free
  180.                         ; the space taken by the variable!
  181.  
  182.         move.l    ___MemList(A4),D0   ; free memory
  183.         beq    ex20
  184. ex10        move.l    D0,A2
  185.         move.l    (A2),A3             ; next...
  186.  
  187.         move.l    4(A2),D0            ; bytes
  188.         move.l    A2,A1            ; ptr
  189.         jsr    _LVOFreeMem(A6)
  190.  
  191.         move.l    A3,D0            ; next...
  192.         bne    ex10
  193. ex20
  194.  
  195.         move.l    4(sp),D0            ; get exit code
  196.         move.l    A5,sp            ; restore sp
  197.  
  198.         ;   FINIS, poof.
  199.  
  200. exfail
  201.         movem.l (sp)+,D2-D7/A2-A6
  202.         rts
  203.  
  204.  
  205.         ;   Base of autoinit section
  206.  
  207.         section autoinit0,code
  208. __AutoInit0:
  209.         section autoinit1,code
  210. __AutoInit1:
  211.         section autoexit0,code
  212. __AutoExit0:
  213.         section autoexit1,code
  214. __AutoExit1:
  215.  
  216.         ;   All library C code is compiled with the -S option
  217.         ;   which uses 'libdata' and 'libbss' section names,
  218.         ;   forcing library data to come before program data
  219.         ;   and library bss to come before program bss (because
  220.         ;   library data/bss sections are declared here first
  221.         ;   and sections of like name are coagulated).
  222.  
  223.         section libdata,data
  224. _Reserved    dc.l    0        ; force section to exist (dummy)
  225.  
  226.         section libbss,bss
  227.  
  228.         xdef    ___MemList    ; used by malloc/free
  229.  
  230. _SysBase    ds.l    1
  231. __ExitSP    ds.l    1
  232. ___MemList    ds.l    1
  233.  
  234.         END
  235.  
  236.