]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
The spans do now contain the size of a span, no longer the end offset.
[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, callirq
16         .import         __INTERRUPTOR_COUNT__
17         .import         __STARTUP_LOAD__, __ZPSAVE_LOAD__
18         .import         __RESERVED_MEMORY__
19
20         .include        "zeropage.inc"
21         .include        "atari.inc"
22
23 ; ------------------------------------------------------------------------
24 ; EXE header
25
26         .segment "EXEHDR"
27         .word   $FFFF
28         .word   __STARTUP_LOAD__
29         .word   __ZPSAVE_LOAD__ - 1
30
31 ; ------------------------------------------------------------------------
32 ; Actual code
33
34         .segment        "STARTUP"
35
36         rts     ; fix for SpartaDOS / OS/A+
37                 ; they first call the entry point from AUTOSTRT and
38                 ; then the load addess (this rts here).
39                 ; We point AUTOSTRT directly after the rts.
40
41 ; Real entry point:
42
43 ; Save the zero page locations we need
44
45         ldx     #zpspace-1
46 L1:     lda     sp,x
47         sta     zpsave,x
48         dex
49         bpl     L1
50
51 ; Clear the BSS data
52
53         jsr     zerobss
54
55 ; Setup the stack
56
57         tsx
58         stx     spsave
59
60 ; Report memory usage
61
62         lda     APPMHI
63         sta     appmsav                 ; remember old APPMHI value
64         lda     APPMHI+1
65         sta     appmsav+1
66
67         sec
68         lda     MEMTOP
69         sbc     #<__RESERVED_MEMORY__
70         sta     APPMHI                  ; initialize our APPMHI value
71         sta     sp                      ; setup runtime stack part 1
72         lda     MEMTOP+1
73         sbc     #>__RESERVED_MEMORY__
74         sta     APPMHI+1
75         sta     sp+1                    ; setup runtime stack part 2
76
77 ; If we have IRQ functions, chain our stub into the IRQ vector
78
79         lda     #<__INTERRUPTOR_COUNT__
80         beq     NoIRQ1
81         lda     VVBLKI
82         ldx     VVBLKI+1
83         sta     IRQInd+1
84         stx     IRQInd+2
85         lda     #6
86         ldy     #<IRQStub
87         ldx     #>IRQStub
88         jsr     SETVBV
89
90 ; Call module constructors
91
92 NoIRQ1: jsr     initlib
93
94 ; Set left margin to 0
95
96         lda     LMARGN
97         sta     old_lmargin
98         ldy     #0
99         sty     LMARGN
100
101 ; Set keyb to upper/lowercase mode
102
103         ldx     SHFLOK
104         stx     old_shflok
105         sty     SHFLOK
106
107 ; Initialize conio stuff
108
109         dey                             ; Set X to $FF
110         sty     CH
111
112 ; Push arguments and call main
113
114         jsr     callmain
115
116 ; Call module destructors. This is also the _exit entry.
117
118 _exit:  jsr     donelib         ; Run module destructors
119
120 ; Reset the IRQ vector if we chained it.
121
122         pha                     ; Save the return code on stack
123         lda     #<__INTERRUPTOR_COUNT__
124         beq     NoIRQ2
125         lda     #6
126         ldy     IRQInd+1
127         ldx     IRQInd+2
128         jsr     SETVBV
129
130 ; Restore system stuff
131
132 NoIRQ2: ldx     spsave
133         txs                     ; Restore stack pointer
134
135 ; Restore left margin
136
137         lda     old_lmargin
138         sta     LMARGN
139
140 ; Restore kb mode
141
142         lda     old_shflok
143         sta     SHFLOK
144
145 ; Restore APPMHI
146
147         lda     appmsav
148         sta     APPMHI
149         lda     appmsav+1
150         sta     APPMHI+1
151
152 ; Copy back the zero page stuff
153
154         ldx     #zpspace-1
155 L2:     lda     zpsave,x
156         sta     sp,x
157         dex
158         bpl     L2
159
160 ; Turn on cursor
161
162         inx
163         stx     CRSINH
164
165 ; Back to DOS
166
167         rts
168
169 ; ------------------------------------------------------------------------
170 ; The IRQ vector jumps here, if condes routines are defined with type 2.
171
172 IRQStub:
173         cld                             ; Just to be sure
174         jsr     callirq                 ; Call the functions
175         jmp     IRQInd                  ; Jump to the saved IRQ vector
176
177 ; ------------------------------------------------------------------------
178 ; Data
179
180 .data
181
182 IRQInd: jmp     $0000
183
184 ; *** end of main startup code
185
186 .segment        "ZPSAVE"
187
188 zpsave: .res    zpspace
189
190         .bss
191
192 spsave:         .res    1
193 appmsav:        .res    1
194 old_shflok:     .res    1
195 old_lmargin:    .res    1
196
197         .segment "AUTOSTRT"
198         .word   RUNAD                   ; defined in atari.h
199         .word   RUNAD+1
200         .word   __STARTUP_LOAD__ + 1