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