]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
Move zp space out of crt0.s
[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        "zeropage.inc"
22         .include        "atari.inc"
23         .include        "_file.inc"
24
25 ; ------------------------------------------------------------------------
26 ; EXE header
27
28         .segment "EXEHDR"
29         .word   $FFFF
30         .word   __CODE_LOAD__
31         .word   __BSS_LOAD__ - 1
32
33 ; ------------------------------------------------------------------------
34 ; Actual code
35
36 .code
37
38         rts     ; fix for SpartaDOS / OS/A+
39                 ; they first call the entry point from AUTOSTRT and
40                 ; then the load addess (this rts here).
41                 ; We point AUTOSTRT directly after the rts.
42
43 ; Real entry point:
44
45 ; Save the zero page locations we need
46
47         ldx     #zpspace-1
48 L1:     lda     sp,x
49         sta     zpsave,x
50         dex
51         bpl     L1
52
53 ; Clear the BSS data
54
55         jsr     zerobss
56
57 ; setup the stack
58
59         tsx
60         stx     spsave
61
62 ; report memory usage
63
64         lda     APPMHI
65         sta     appmsav                 ; remember old APPMHI value
66         lda     APPMHI+1
67         sta     appmsav+1
68
69         lda     MEMTOP
70         sta     APPMHI                  ; initialize our APPMHI value
71         ldx     MEMTOP+1
72         stx     APPMHI+1
73
74 ; Call module constructors
75
76         jsr     initlib
77
78 ; set left margin to 0
79
80         lda     LMARGN
81         sta     old_lmargin
82         lda     #0
83         sta     LMARGN
84
85 ; set keyb to upper/lowercase mode
86
87         ldx     SHFLOK
88         stx     old_shflok
89         sta     SHFLOK
90
91 ; Initialize conio stuff
92
93         lda     #$FF
94         sta     CH
95
96 ; set stdio stream handles
97
98         lda     #0
99         jsr     getfd
100         sta     __filetab + (0 * _FILE_size)    ; setup stdin
101         lda     #0
102         jsr     getfd
103         sta     __filetab + (1 * _FILE_size)    ; setup stdout
104         lda     #0
105         jsr     getfd
106         sta     __filetab + (2 * _FILE_size)    ; setup stderr
107
108 ; Pass command line if present
109
110         jsr     getargs
111
112         lda     argc
113         ldx     argc+1
114         jsr     pushax          ; argc
115         lda     #<argv
116         ldx     #>argv
117         jsr     pushax          ; argv
118
119         ldy     #4              ; Argument size
120         jsr     _main           ; call the users code
121
122 ; Call module destructors. This is also the _exit entry.
123
124 _exit:  jsr     donelib         ; Run module destructors
125
126 ; Restore system stuff
127
128         ldx     spsave
129         txs                     ; Restore stack pointer
130
131 ; restore left margin
132
133         lda     old_lmargin
134         sta     LMARGN
135
136 ; restore kb mode
137
138         lda     old_shflok
139         sta     SHFLOK
140
141 ; restore APPMHI
142
143         lda     appmsav
144         sta     APPMHI
145         lda     appmsav+1
146         sta     APPMHI+1
147
148 ; Copy back the zero page stuff
149
150         ldx     #zpspace-1
151 L2:     lda     zpsave,x
152         sta     sp,x
153         dex
154         bpl     L2
155
156 ; Back to DOS
157
158         rts
159
160 ; *** end of main startup code
161
162 ; setup sp
163
164 initsp:
165         lda     APPMHI
166         sta     sp
167         lda     APPMHI+1
168         sta     sp+1
169         rts
170
171         .data
172
173 zpsave: .res    zpspace
174
175         .bss
176
177 spsave:         .res    1
178 appmsav:        .res    1
179 old_shflok:     .res    1
180 old_lmargin:    .res    1
181
182         .segment "AUTOSTRT"
183         .word   $02E0
184         .word   $02E1
185         .word   __CODE_LOAD__ + 1