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