]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
Added o65 symbol export capability
[cc65] / libsrc / atari / crt0.s
1 ;
2 ; Startup code for cc65 (ATARI version)
3 ;
4 ; Contributing authors:
5 ;       Mark Keates
6 ;       Freddy Offenga
7 ;       Christian Groessler
8 ;
9 ; This must be the *first* file on the linker command line
10 ;
11
12         .export         _exit
13         .constructor    initsp,26
14
15         .import         getargs, argc, argv
16         .import         initlib, donelib
17         .import         zerobss, pushax
18         .import         _main,__filetab,getfd
19         .import         __CODE_LOAD__, __BSS_LOAD__
20
21         .include        "atari.inc"
22
23 ; ------------------------------------------------------------------------
24 ; Define and export the ZP variables for the runtime
25
26         .exportzp       sp, sreg, regsave
27         .exportzp       ptr1, ptr2, ptr3, ptr4
28         .exportzp       tmp1, tmp2, tmp3, tmp4
29         .exportzp       fntemp, regbank, zpspace
30
31
32 .zeropage
33
34 zpstart = *
35 sp:             .res    2       ; Stack pointer
36 sreg:           .res    2       ; Secondary register/high 16 bit for longs
37 regsave:        .res    2       ; slot to save/restore (E)AX into
38 ptr1:           .res    2
39 ptr2:           .res    2
40 ptr3:           .res    2
41 ptr4:           .res    2
42 tmp1:           .res    1
43 tmp2:           .res    1
44 tmp3:           .res    1
45 tmp4:           .res    1
46 fntemp:         .res    2       ; Pointer to file name
47 regbank:        .res    6       ; 6 byte register bank
48
49 zpspace = * - zpstart           ; Zero page space allocated
50
51 .code
52
53 ; ------------------------------------------------------------------------
54 ; EXE header
55
56         .segment "EXEHDR"
57         .word   $FFFF
58         .word   __CODE_LOAD__
59         .word   __BSS_LOAD__ - 1
60         .code
61         .reloc
62
63 ; ------------------------------------------------------------------------
64 ; Actual code
65
66         rts     ; fix for SpartaDOS / OS/A+
67                 ; they first call the entry point from AUTOSTRT and
68                 ; then the load addess (this rts here).
69                 ; We point AUTOSTRT directly after the rts.
70
71 ; Real entry point:
72
73 ; Save the zero page locations we need
74
75         ldx     #zpspace-1
76 L1:     lda     sp,x
77         sta     zpsave,x
78         dex
79         bpl     L1
80
81 ; Clear the BSS data
82
83         jsr     zerobss
84
85 ; setup the stack
86
87         tsx
88         stx     spsave
89
90 ; report memory usage
91
92         lda     APPMHI
93         sta     appmsav                 ; remember old APPMHI value
94         lda     APPMHI+1
95         sta     appmsav+1
96
97         lda     MEMTOP
98         sta     APPMHI                  ; initialize our APPMHI value
99         ldx     MEMTOP+1
100         stx     APPMHI+1
101
102 ; Call module constructors
103
104         jsr     initlib
105
106 ; set left margin to 0
107
108         lda     LMARGN
109         sta     old_lmargin
110         lda     #0
111         sta     LMARGN
112
113 ; set keyb to upper/lowercase mode
114
115         ldx     SHFLOK
116         stx     old_shflok
117         sta     SHFLOK
118
119 ; Initialize conio stuff
120
121         lda     #$FF
122         sta     CH
123
124 ; set stdio stream handles
125
126         lda     #0
127         jsr     getfd
128         sta     __filetab               ; setup stdin
129         lda     #0
130         jsr     getfd
131         sta     __filetab + 2           ; setup stdout
132         lda     #0
133         jsr     getfd
134         sta     __filetab + 4           ; setup stderr
135
136 ; Pass command line if present
137
138         jsr     getargs
139
140         lda     argc
141         ldx     argc+1
142         jsr     pushax          ; argc
143         lda     #<argv
144         ldx     #>argv
145         jsr     pushax          ; argv
146
147         ldy     #4              ; Argument size
148         jsr     _main           ; call the users code
149
150 ; Call module destructors. This is also the _exit entry.
151
152 _exit:  jsr     donelib         ; Run module destructors
153
154 ; Restore system stuff
155
156         ldx     spsave
157         txs                     ; Restore stack pointer
158
159 ; restore left margin
160
161         lda     old_lmargin
162         sta     LMARGN
163
164 ; restore kb mode
165
166         lda     old_shflok
167         sta     SHFLOK
168
169 ; restore APPMHI
170
171         lda     appmsav
172         sta     APPMHI
173         lda     appmsav+1
174         sta     APPMHI+1
175
176 ; Copy back the zero page stuff
177
178         ldx     #zpspace-1
179 L2:     lda     zpsave,x
180         sta     sp,x
181         dex
182         bpl     L2
183
184 ; Back to DOS
185
186         rts
187
188 ; *** end of main startup code
189
190 ; setup sp
191
192 initsp:
193         lda     APPMHI
194         sta     sp
195         lda     APPMHI+1
196         sta     sp+1
197         rts
198
199         .data
200
201 zpsave: .res    zpspace
202
203         .bss
204
205 spsave:         .res    1
206 appmsav:        .res    1
207 old_shflok:     .res    1
208 old_lmargin:    .res    1
209
210         .segment "AUTOSTRT"
211         .word   $02E0
212         .word   $02E1
213         .word   __CODE_LOAD__ + 1