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