]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
use first free IOCB for "GRAPHICS 0" call instead of using a hard-coded one
[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         .import         findfreeiocb
24 .endif
25
26         .include        "zeropage.inc"
27         .include        "atari.inc"
28         .include        "save_area.inc"
29
30 ; ------------------------------------------------------------------------
31 ; EXE header
32
33 .segment        "EXEHDR"
34
35         .word   $FFFF
36
37 .if .defined(__ATARIXL__)
38 .segment        "MAINHDR"
39 .endif
40
41         .word   __STARTUP_LOAD__
42 .if .defined(__ATARIXL__)
43         .word   __BSS_LOAD__ - 1
44 .else
45         .word   __ZPSAVE_LOAD__ - 1
46 .endif
47
48 ; ------------------------------------------------------------------------
49 ; Actual code
50
51 .segment        "STARTUP"
52
53         rts     ; fix for SpartaDOS / OS/A+
54                 ; they first call the entry point from AUTOSTRT and
55                 ; then the load addess (this rts here).
56                 ; We point AUTOSTRT directly after the rts.
57
58 ; Real entry point:
59
60 .if .not .defined(__ATARIXL__)          ; already done in previous load chunk
61
62 ; Save the zero page locations we need
63
64         ldx     #zpspace-1
65 L1:     lda     sp,x
66         sta     zpsave,x
67         dex
68         bpl     L1
69
70 .else
71
72         jsr     sram_init
73
74 .endif
75
76 ; Clear the BSS data
77
78         jsr     zerobss
79
80 ; Setup the stack
81
82         tsx
83         stx     spsave
84
85 .if .not .defined(__ATARIXL__)
86
87 ; Report memory usage
88
89         lda     APPMHI
90         sta     appmsav                 ; remember old APPMHI value
91         lda     APPMHI+1
92         sta     appmsav+1
93
94         sec
95         lda     MEMTOP
96         sbc     #<__RESERVED_MEMORY__
97         sta     APPMHI                  ; initialize our APPMHI value
98         sta     sp                      ; setup runtime stack part 1
99         lda     MEMTOP+1
100         sbc     #>__RESERVED_MEMORY__
101         sta     APPMHI+1
102         sta     sp+1                    ; setup runtime stack part 2
103
104 .else
105
106         lda     #<(__RAM_START__ + __RAM_SIZE__ - 1)
107         sta     sp
108         lda     #>(__RAM_START__ + __RAM_SIZE__ - 1)
109         sta     sp+1
110
111 .endif
112
113 ; Call module constructors
114
115         jsr     initlib
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 .if .not .defined(__ATARIXL__)
159
160 ; Restore APPMHI
161
162         lda     appmsav
163         sta     APPMHI
164         lda     appmsav+1
165         sta     APPMHI+1
166
167 .else
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         lda     APPMHI_save
180         sta     APPMHI
181         lda     APPMHI_save+1
182         sta     APPMHI+1
183
184
185 ; ... issue a GRAPHICS 0 call (copied'n'pasted from TGI drivers)
186
187         jsr     findfreeiocb
188
189         ; Reopen it in Graphics 0
190         lda     #OPEN
191         sta     ICCOM,x
192         lda     #OPNIN | OPNOT
193         sta     ICAX1,x
194         lda     #0
195         sta     ICAX2,x
196         lda     #<scrdev
197         sta     ICBAL,x
198         lda     #>scrdev
199         sta     ICBAH,x
200         lda     #3
201         sta     ICBLL,x
202         lda     #0
203         sta     ICBLH,x
204         jsr     CIOV_org
205 ; add error checking here...
206         lda     #CLOSE
207         sta     ICCOM,x
208         jsr     CIOV_org
209
210 .endif
211
212
213 ; Copy back the zero page stuff
214
215         ldx     #zpspace-1
216 L2:     lda     zpsave,x
217         sta     sp,x
218         dex
219         bpl     L2
220
221 ; Turn on cursor
222
223         inx
224         stx     CRSINH
225
226 ; Back to DOS
227
228         rts
229
230 ; *** end of main startup code
231
232 ; ------------------------------------------------------------------------
233
234 .bss
235
236 spsave:         .res    1
237 old_shflok:     .res    1
238 old_lmargin:    .res    1
239 .if .not .defined(__ATARIXL__)
240 appmsav:        .res    1
241 .endif
242
243         .segment "AUTOSTRT"
244         .word   RUNAD                   ; defined in atari.h
245         .word   RUNAD+1
246         .word   __STARTUP_LOAD__ + 1