]> 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__, __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 ; Set left margin to 0
101
102         lda     LMARGN
103         sta     old_lmargin
104         ldy     #0
105         sty     LMARGN
106
107 ; Set keyb to upper/lowercase mode
108
109         ldx     SHFLOK
110         stx     old_shflok
111         sty     SHFLOK
112
113 ; Initialize conio stuff
114
115         dey                             ; Set X to $FF
116         sty     CH
117
118 ; Push arguments and call main
119
120         jsr     callmain
121
122 ; Call module destructors. This is also the _exit entry.
123
124 _exit:  jsr     donelib         ; Run module destructors
125
126 ; Restore system stuff
127
128         ldx     spsave
129         txs                     ; Restore stack pointer
130
131 ; Restore left margin
132
133         lda     old_lmargin
134         sta     LMARGN
135
136 ; Restore kb mode
137
138         lda     old_shflok
139         sta     SHFLOK
140
141 ; Restore APPMHI
142
143         lda     APPMHI_save
144         sta     APPMHI
145         lda     APPMHI_save+1
146         sta     APPMHI+1
147
148 .if .defined(__ATARIXL__)
149
150 ; Atari XL target stuff...
151
152         lda     PORTB_save
153         sta     PORTB
154         lda     RAMTOP_save
155         sta     RAMTOP
156         lda     MEMTOP_save
157         sta     MEMTOP
158         lda     MEMTOP_save+1
159         sta     MEMTOP+1
160
161
162 ; ... issue a GRAPHICS 0 call (copied'n'pasted from TGI drivers)
163
164         jsr     findfreeiocb
165
166         ; Reopen it in Graphics 0
167         lda     #OPEN
168         sta     ICCOM,x
169         lda     #OPNIN | OPNOT
170         sta     ICAX1,x
171         lda     #0
172         sta     ICAX2,x
173         lda     #<scrdev
174         sta     ICBAL,x
175         lda     #>scrdev
176         sta     ICBAH,x
177         lda     #3
178         sta     ICBLL,x
179         lda     #0
180         sta     ICBLH,x
181         jsr     CIOV_org
182 ; add error checking here...
183         lda     #CLOSE
184         sta     ICCOM,x
185         jsr     CIOV_org
186
187 .endif
188
189 ; Turn on cursor
190
191         ldx     #0
192         stx     CRSINH
193
194 ; Back to DOS
195
196         rts
197
198 ; *** end of main startup code
199
200 ; ------------------------------------------------------------------------
201
202 .bss
203
204 spsave:         .res    1
205 old_shflok:     .res    1
206 old_lmargin:    .res    1
207 .if .not .defined(__ATARIXL__)
208 APPMHI_save:    .res    2
209 .endif
210
211
212 .segment "AUTOSTRT"
213         .word   RUNAD                   ; defined in atari.inc
214         .word   RUNAD+1
215         .word   __STARTUP_LOAD__ + 1