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