]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
Minor clarification.
[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, excexit, SP_save
13
14         .import         initlib, donelib
15         .import         callmain, zerobss
16         .import         __RESERVED_MEMORY__
17         .import         __MAIN_START__, __MAIN_SIZE__
18         .import         __LOWCODE_RUN__, __LOWCODE_SIZE__
19 .ifdef __ATARIXL__
20         .import         __STACKSIZE__
21         .import         sram_init
22         .import         scrdev
23         .import         findfreeiocb
24         .forceimport    sramprep                        ; force inclusion of the "shadow RAM preparation" load chunk
25         .include        "save_area.inc"
26 .endif
27
28         .include        "zeropage.inc"
29         .include        "atari.inc"
30
31 ; ------------------------------------------------------------------------
32
33 .segment        "STARTUP"
34
35         rts     ; fix for SpartaDOS / OS/A+
36                 ; They first call the entry point from AUTOSTRT; and
37                 ; then, the load address (this rts here).
38                 ; We point AUTOSTRT directly after the rts.
39
40 ; Real entry point:
41
42 start:
43
44 .ifdef __ATARIXL__
45         jsr     sram_init
46 .endif
47
48 ; Clear the BSS data.
49
50         jsr     zerobss
51
52 ; Set up the stack.
53
54         tsx
55         stx     SP_save
56
57 .ifdef __ATARIXL__
58
59         lda     #<(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
60         ldx     #>(__MAIN_START__ + __MAIN_SIZE__ + __STACKSIZE__)
61         sta     sp
62         stx     sp+1
63
64 .else
65
66 ; Report the memory usage.
67
68         lda     APPMHI
69         ldx     APPMHI+1
70         sta     APPMHI_save             ; remember old APPMHI value
71         stx     APPMHI_save+1
72
73         sec
74         lda     MEMTOP
75         sbc     #<__RESERVED_MEMORY__
76         sta     APPMHI                  ; initialize our APPMHI value
77         sta     sp                      ; set up runtime stack part 1
78         lda     MEMTOP+1
79         sbc     #>__RESERVED_MEMORY__
80         sta     APPMHI+1
81         sta     sp+1                    ; set up runtime stack part 2
82
83 .endif
84
85 ; Call the module constructors.
86
87         jsr     initlib
88
89 ; Set the left margin to 0.
90
91         lda     LMARGN
92         sta     LMARGN_save
93         ldy     #0
94         sty     LMARGN
95
96 ; Set the keyboard to upper-/lower-case mode.
97
98         ldx     SHFLOK
99         stx     SHFLOK_save
100         sty     SHFLOK
101
102 ; Initialize the conio stuff.
103
104         dey                     ; Set Y to $FF
105         sty     CH              ; remove keypress which might be in the input buffer
106
107 ; Push the command-line arguments; and, call main().
108
109         jsr     callmain
110
111 ; Call the module destructors. This is also the exit() entry.
112
113 _exit:  ldx     SP_save
114         txs                     ; Restore stack pointer
115
116 ; Restore the system stuff.
117
118 excexit:jsr     donelib         ; Run module destructors; 'excexit' is called from the exec routine
119
120 ; Restore the left margin.
121
122         lda     LMARGN_save
123         sta     LMARGN
124
125 ; Restore the kb mode.
126
127         lda     SHFLOK_save
128         sta     SHFLOK
129
130 ; Restore APPMHI.
131
132         lda     APPMHI_save
133         ldx     APPMHI_save+1
134         sta     APPMHI
135         stx     APPMHI+1
136
137 .ifdef __ATARIXL__
138
139 ; Atari XL target stuff...
140
141         lda     PORTB_save
142         sta     PORTB
143         lda     RAMTOP_save
144         sta     RAMTOP
145         lda     MEMTOP_save
146         ldx     MEMTOP_save+1
147         sta     MEMTOP
148         stx     MEMTOP+1
149
150
151 ; Issue a GRAPHICS 0 call (copied'n'pasted from the TGI drivers), in
152 ; order to restore screen memory to its default location just
153 ; before the ROM.
154
155         jsr     findfreeiocb
156
157         ; Reopen it in Graphics 0
158         lda     #OPEN
159         sta     ICCOM,x
160         lda     #OPNIN | OPNOT
161         sta     ICAX1,x
162         lda     #0
163         sta     ICAX2,x
164         lda     #<scrdev
165         sta     ICBAL,x
166         lda     #>scrdev
167         sta     ICBAH,x
168         lda     #3
169         sta     ICBLL,x
170         lda     #0
171         sta     ICBLH,x
172         jsr     CIOV_org
173 ; No error checking here, shouldn't happen(TM); and, no way to
174 ; recover anyway.
175
176         lda     #CLOSE
177         sta     ICCOM,x
178         jsr     CIOV_org
179
180 .endif
181
182 ; Turn on the cursor.
183
184         ldx     #0
185         stx     CRSINH
186
187 ; Back to DOS.
188
189         rts
190
191 ; *** end of main startup code
192
193 ; ------------------------------------------------------------------------
194
195 .bss
196
197 SP_save:        .res    1
198 SHFLOK_save:    .res    1
199 LMARGN_save:    .res    1
200 .ifndef __ATARIXL__
201 APPMHI_save:    .res    2
202 .endif
203
204 ; ------------------------------------------------------------------------
205
206 .segment "LOWCODE"       ; have at least one (empty) segment of LOWCODE, so that the next line works even if the program doesn't make use of this segment
207 .assert (__LOWCODE_RUN__ + __LOWCODE_SIZE__ <= $4000 || __LOWCODE_RUN__ > $7FFF || __LOWCODE_SIZE__ = 0), warning, "'lowcode area' reaches into $4000..$7FFF bank memory window"
208 ; check for LOWBSS_SIZE = 0 not needed since the only file which uses LOWBSS (irq.s) also uses LOWCODE
209 ; check for LOWCODE_RUN > $7FFF is mostly for cartridges, where this segment is loaded high (into cart ROM)
210 ; there is a small chance that if the user loads the program really high, LOWCODE is above $7FFF, but LOWBSS is below -- no warning emitted in this case