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