]> git.sur5r.net Git - cc65/blob - libsrc/apple2/crt0.s
Added getenv()
[cc65] / libsrc / apple2 / crt0.s
1 ;
2 ; Startup code for cc65 (Apple2 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .import         initlib, donelib
9         .import         zerobss, push0
10         .import         __STARTUP_LOAD__, __BSS_LOAD__  ; Linker generated
11         .import         callmain
12
13         .include        "zeropage.inc"
14         .include        "apple2.inc"
15
16 ; ------------------------------------------------------------------------
17 ; The executable header
18
19 .segment        "EXEHDR"
20
21         .word   __STARTUP_LOAD__                ; Start address
22         .word   __BSS_LOAD__ - __STARTUP_LOAD__ ; Size
23
24 ; ------------------------------------------------------------------------
25 ; Create an empty LOWCODE segment to avoid linker warnings
26
27 .segment        "LOWCODE"
28
29 ; ------------------------------------------------------------------------
30 ; Place the startup code in a special segment.
31
32 .segment        "STARTUP"
33
34         ldx     #zpspace-1
35 L1:     lda     sp,x
36         sta     zpsave,x        ; Save the zero page locations we need
37         dex
38         bpl     L1
39
40 ; Clear the BSS data
41
42         jsr     zerobss
43
44 ; Save system stuff and setup the stack
45
46         tsx
47         stx     spsave          ; Save the system stack ptr
48
49         lda     MEMSIZE
50         sta     sp
51         lda     MEMSIZE+1
52         sta     sp+1            ; Set argument stack ptr
53
54 ; Call module constructors
55
56         jsr     initlib
57
58 ; Initialize conio stuff
59
60         lda     #$ff
61         sta     TEXTTYP
62
63 ; Set up to use Apple ROM $C000-$CFFF
64
65         ;;      sta     USEROM
66
67 ; Push arguments and call main()
68
69         jsr     callmain
70
71 ; Call module destructors. This is also the _exit entry.
72
73 _exit:  jsr     donelib
74
75 ; Restore system stuff
76
77         lda     #$ff            ; Reset text mode
78         sta     TEXTTYP
79
80         ldx     spsave
81         txs                     ; Restore stack pointer
82
83 ; Copy back the zero page stuff
84
85         ldx     #zpspace-1
86 L2:     lda     zpsave,x
87         sta     sp,x
88         dex
89         bpl     L2
90
91 ; Reset changed vectors, back to basic
92
93         jmp     RESTOR
94
95 ; ------------------------------------------------------------------------
96 ; Data
97
98 .data
99
100 zpsave: .res    zpspace
101
102 .bss
103
104 spsave: .res    1