]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
Use labels instead of segment load addresses to specify entry points
[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 ;       Stefan Haubenthal
9 ;
10
11         .export         _exit
12         .export         __STARTUP__ : absolute = 1      ; Mark as startup
13
14         .import         initlib, donelib
15         .import         callmain, zerobss
16         .import         __STARTUP_LOAD__, __BSS_LOAD__
17         .import         __RESERVED_MEMORY__
18         .import         __RAM_START__, __RAM_SIZE__
19 .if .defined(__ATARIXL__)
20         .import         sram_init
21         .import         scrdev
22         .import         findfreeiocb
23         .include        "save_area.inc"
24 .endif
25
26         .include        "zeropage.inc"
27         .include        "atari.inc"
28
29 ; ------------------------------------------------------------------------
30 ; EXE header
31
32 .segment        "EXEHDR"
33
34         .word   $FFFF
35
36 .if .defined(__ATARIXL__)
37 .segment        "MAINHDR"
38 .endif
39
40         .word   __STARTUP_LOAD__
41         .word   __BSS_LOAD__ - 1
42
43 ; ------------------------------------------------------------------------
44 ; Actual code
45
46 .segment        "STARTUP"
47
48         rts     ; fix for SpartaDOS / OS/A+
49                 ; they first call the entry point from AUTOSTRT and
50                 ; then the load addess (this rts here).
51                 ; We point AUTOSTRT directly after the rts.
52
53 ; Real entry point:
54
55 start:
56
57 .if .defined(__ATARIXL__)
58         jsr     sram_init
59 .endif
60
61 ; Clear the BSS data
62
63         jsr     zerobss
64
65 ; Setup the stack
66
67         tsx
68         stx     spsave
69
70 .if .not .defined(__ATARIXL__)
71
72 ; Report memory usage
73
74         lda     APPMHI
75         sta     APPMHI_save             ; remember old APPMHI value
76         lda     APPMHI+1
77         sta     APPMHI_save+1
78
79         sec
80         lda     MEMTOP
81         sbc     #<__RESERVED_MEMORY__
82         sta     APPMHI                  ; initialize our APPMHI value
83         sta     sp                      ; setup runtime stack part 1
84         lda     MEMTOP+1
85         sbc     #>__RESERVED_MEMORY__
86         sta     APPMHI+1
87         sta     sp+1                    ; setup runtime stack part 2
88
89 .else
90
91         lda     #<(__RAM_START__ + __RAM_SIZE__ - 1)
92         sta     sp
93         lda     #>(__RAM_START__ + __RAM_SIZE__ - 1)
94         sta     sp+1
95
96 .endif
97
98 ; Call module constructors
99
100         jsr     initlib
101
102 .if 0
103 .if .defined(__ATARIXL__)
104         .import __heapadd
105         .import pushax
106         .import __RAM_BELOW_ROM_START__
107         .import __RAM_BELOW_ROM_SIZE__
108         .import __RAM_BELOW_ROM_LAST__
109
110         lda     #<__RAM_BELOW_ROM_LAST__
111         ldx     #>__RAM_BELOW_ROM_LAST__
112         jsr     pushax
113         lda     #<(__RAM_BELOW_ROM_SIZE__ - (__RAM_BELOW_ROM_LAST__ - __RAM_BELOW_ROM_START__))
114         ldx     #>(__RAM_BELOW_ROM_SIZE__ - (__RAM_BELOW_ROM_LAST__ - __RAM_BELOW_ROM_START__))
115         jsr     __heapadd
116 .endif
117 .endif
118
119 ; Set left margin to 0
120
121         lda     LMARGN
122         sta     old_lmargin
123         ldy     #0
124         sty     LMARGN
125
126 ; Set keyb to upper/lowercase mode
127
128         ldx     SHFLOK
129         stx     old_shflok
130         sty     SHFLOK
131
132 ; Initialize conio stuff
133
134         dey                             ; Set X to $FF
135         sty     CH
136
137 ; Push arguments and call main
138
139         jsr     callmain
140
141 ; Call module destructors. This is also the _exit entry.
142
143 _exit:  jsr     donelib         ; Run module destructors
144
145 ; Restore system stuff
146
147         ldx     spsave
148         txs                     ; Restore stack pointer
149
150 ; Restore left margin
151
152         lda     old_lmargin
153         sta     LMARGN
154
155 ; Restore kb mode
156
157         lda     old_shflok
158         sta     SHFLOK
159
160 ; Restore APPMHI
161
162         lda     APPMHI_save
163         sta     APPMHI
164         lda     APPMHI_save+1
165         sta     APPMHI+1
166
167 .if .defined(__ATARIXL__)
168
169 ; Atari XL target stuff...
170
171         lda     PORTB_save
172         sta     PORTB
173         lda     RAMTOP_save
174         sta     RAMTOP
175         lda     MEMTOP_save
176         sta     MEMTOP
177         lda     MEMTOP_save+1
178         sta     MEMTOP+1
179
180
181 ; ... issue a GRAPHICS 0 call (copied'n'pasted from TGI drivers)
182
183         jsr     findfreeiocb
184
185         ; Reopen it in Graphics 0
186         lda     #OPEN
187         sta     ICCOM,x
188         lda     #OPNIN | OPNOT
189         sta     ICAX1,x
190         lda     #0
191         sta     ICAX2,x
192         lda     #<scrdev
193         sta     ICBAL,x
194         lda     #>scrdev
195         sta     ICBAH,x
196         lda     #3
197         sta     ICBLL,x
198         lda     #0
199         sta     ICBLH,x
200         jsr     CIOV_org
201 ; add error checking here...
202         lda     #CLOSE
203         sta     ICCOM,x
204         jsr     CIOV_org
205
206 .endif
207
208 ; Turn on cursor
209
210         ldx     #0
211         stx     CRSINH
212
213 ; Back to DOS
214
215         rts
216
217 ; *** end of main startup code
218
219 ; ------------------------------------------------------------------------
220
221 .bss
222
223 spsave:         .res    1
224 old_shflok:     .res    1
225 old_lmargin:    .res    1
226 .if .not .defined(__ATARIXL__)
227 APPMHI_save:    .res    2
228 .endif
229
230
231 .segment "AUTOSTRT"
232         .word   RUNAD                   ; defined in atari.inc
233         .word   RUNAD+1
234         .word   start