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