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