]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
0ea6e390f0f750fbf47032d77fe58aaa55dd1f68
[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 address (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 ; Set up the stack.
52
53         tsx
54         stx     SP_save
55
56 .ifdef __ATARIXL__
57
58         lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
59         sta     sp
60         lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
61         sta     sp+1
62
63 .else
64
65 ; Report the memory usage.
66
67         lda     APPMHI
68         sta     APPMHI_save             ; remember old APPMHI value
69         lda     APPMHI+1
70         sta     APPMHI_save+1
71
72         sec
73         lda     MEMTOP
74         sbc     #<__RESERVED_MEMORY__
75         sta     APPMHI                  ; initialize our APPMHI value
76         sta     sp                      ; set up runtime stack part 1
77         lda     MEMTOP+1
78         sbc     #>__RESERVED_MEMORY__
79         sta     APPMHI+1
80         sta     sp+1                    ; set up runtime stack part 2
81
82 .endif
83
84 ; Call the module constructors.
85
86         jsr     initlib
87
88 ; Set the left margin to 0.
89
90         lda     LMARGN
91         sta     LMARGN_save
92         ldy     #0
93         sty     LMARGN
94
95 ; Set the keyboard to upper-/lower-case mode.
96
97         ldx     SHFLOK
98         stx     SHFLOK_save
99         sty     SHFLOK
100
101 ; Initialize the conio stuff.
102
103         dey                     ; Set Y to $FF
104         sty     CH              ; remove keypress which might be in the input buffer
105
106 ; Push the command-line arguments; and, call main().
107
108         jsr     callmain
109
110 ; Call the module destructors. This is also the exit() entry.
111
112 _exit:  jsr     donelib         ; Run module destructors
113
114 ; Restore the system stuff.
115
116         ldx     SP_save
117         txs                     ; Restore stack pointer
118
119 ; Restore the left margin.
120
121         lda     LMARGN_save
122         sta     LMARGN
123
124 ; Restore the 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 the TGI drivers), in
151 ; order to restore screen memory to its default 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 the 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