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