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