]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
Use the condes feature
[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 ;
9 ; This must be the *first* file on the linker command line
10 ;
11
12 RESERVE_MOUSE_MEMORY    = 1     ; for P/M
13
14 .ifdef RESERVE_MOUSE_MEMORY
15         .export         mouse_pm0
16 .endif
17         .export         _exit
18         .import         getargs, argc, argv       
19         .import         initlib, donelib
20         .import         initconio, zerobss, pushax
21         .import         _main,__filetab,getfd
22         .import         __CODE_LOAD__, __BSS_LOAD__
23         .import         __graphmode_used
24
25         .include        "atari.inc"
26
27 ; ------------------------------------------------------------------------
28 ; Define and export the ZP variables for the runtime
29
30         .exportzp       sp, sreg, regsave
31         .exportzp       ptr1, ptr2, ptr3, ptr4
32         .exportzp       tmp1, tmp2, tmp3, tmp4
33         .exportzp       fntemp, regbank, zpspace
34
35
36 .zeropage
37
38 zpstart = *
39 sp:             .res    2       ; Stack pointer
40 sreg:           .res    2       ; Secondary register/high 16 bit for longs
41 regsave:        .res    2       ; slot to save/restore (E)AX into
42 ptr1:           .res    2
43 ptr2:           .res    2
44 ptr3:           .res    2
45 ptr4:           .res    2
46 tmp1:           .res    1
47 tmp2:           .res    1
48 tmp3:           .res    1
49 tmp4:           .res    1
50 fntemp:         .res    2       ; Pointer to file name
51 regbank:        .res    6       ; 6 byte register bank
52
53 zpspace = * - zpstart           ; Zero page space allocated
54
55 .code
56
57 ; ------------------------------------------------------------------------
58 ; EXE header
59
60         .segment "EXEHDR"
61         .word   $FFFF
62         .word   __CODE_LOAD__
63         .word   __BSS_LOAD__ - 1
64         .code
65         .reloc
66
67 ; ------------------------------------------------------------------------
68 ; Actual code
69
70         rts     ; fix for SpartaDOS / OS/A+
71                 ; they first call the entry point from AUTOSTRT and
72                 ; then the load addess (this rts here).
73                 ; We point AUTOSTRT directly after the rts.
74
75 ; Real entry point:
76
77 ; Save the zero page locations we need
78
79         ldx     #zpspace-1
80 L1:     lda     sp,x
81         sta     zpsave,x
82         dex
83         bpl     L1
84
85 ; Clear the BSS data
86
87         jsr     zerobss
88
89 ; setup the stack
90
91         tsx
92         stx     spsave
93
94 ; report memory usage and initialize stack pointer
95
96         lda     APPMHI
97         sta     appmsav
98         lda     APPMHI+1
99         sta     appmsav+1
100
101         jsr     getmemtop       ; adjust for graphics mode to use
102
103         sta     sp
104         sta     APPMHI
105         stx     sp+1            ; Set argument stack ptr
106         stx     APPMHI+1
107
108 ; set left margin to 0
109
110         lda     LMARGN
111         sta     old_lmargin
112         lda     #0
113         sta     LMARGN
114
115 ; set keyb to upper/lowercase mode
116
117         ldx     SHFLOK
118         stx     old_shflok
119         sta     SHFLOK
120
121 ; Call module constructors
122
123         jsr     initlib
124
125 ; Initialize conio stuff
126
127         jsr     initconio
128
129         lda     #$FF
130         sta     CH
131
132 ; set stdio stream handles
133
134         lda     #0
135         jsr     getfd
136         sta     __filetab               ; setup stdin
137         lda     #0
138         jsr     getfd
139         sta     __filetab + 2           ; setup stdout
140         lda     #0
141         jsr     getfd
142         sta     __filetab + 4           ; setup stderr
143
144 ; Pass command line if present
145
146         jsr     getargs
147
148         lda     argc
149         ldx     argc+1
150         jsr     pushax          ; argc
151         lda     #<argv
152         ldx     #>argv
153         jsr     pushax          ; argv
154
155         ldy     #4              ; Argument size
156         jsr     _main           ; call the users code
157
158 ; Call module destructors. This is also the _exit entry.
159
160 _exit:  jsr     donelib         ; Run module destructors
161
162 ; Restore system stuff
163
164         ldx     spsave
165         txs                     ; Restore stack pointer
166
167 ; restore left margin
168
169         lda     old_lmargin
170         sta     LMARGN
171
172 ; restore kb mode
173
174         lda     old_shflok
175         sta     SHFLOK
176
177 ; restore APPMHI
178
179         lda     appmsav
180         sta     APPMHI
181         lda     appmsav+1
182         sta     APPMHI+1
183
184 ; Copy back the zero page stuff
185
186         ldx     #zpspace-1
187 L2:     lda     zpsave,x
188         sta     sp,x
189         dex
190         bpl     L2
191
192 ; Back to DOS
193
194         rts
195
196 ; *** end of main startup code
197
198
199 ; calc. upper memory limit to use
200
201 .proc   getmemtop
202
203         ldy     __graphmode_used
204         beq     ignore          ; mode 0 doesn't need adjustment
205         cpy     #32
206         bcs     ignore          ; invalid value
207
208         tya
209         asl     a
210         tay
211         lda     MEMTOP
212         sec
213         sbc     grmemusage,y
214         pha
215         lda     MEMTOP+1
216         sbc     grmemusage+1,y
217         tax
218         pla
219 .ifdef RESERVE_MOUSE_MEMORY
220
221 adj_mouse:
222         txa                     ; get upper byte of address
223         and     #%11111000      ; make 2k aligned
224         sec
225         sbc     #%00001000      ; reserve 2k
226         tax
227         adc     #3              ; add 4 (C = 1)
228         sta     mouse_pm0
229         lda     #0
230 .endif
231         rts
232
233 ignore: lda     MEMTOP
234         ldx     MEMTOP+1
235 .ifdef RESERVE_MOUSE_MEMORY
236         bne     adj_mouse
237 .else
238         rts
239 .endif
240
241 .endproc
242
243         .data
244
245 zpsave: .res    zpspace
246
247         .rodata
248
249 ; memory usage of the different graphics modes (0-31)
250 ; values < 0 of "bytes needed" are mappped to 0
251 ;               bytes needed    ; mode  ; val. of MEMTOP
252 grmemusage:
253         .word   0               ; 0     ; 39967
254         .word   0 ;-318         ; 1     ; 40285
255         .word   0 ;-568         ; 2     ; 40535
256         .word   0 ;-558         ; 3     ; 40525
257         .word   0 ;-298         ; 4     ; 40265
258         .word   182             ; 5     ; 39785
259         .word   1182            ; 6     ; 38785
260         .word   3198            ; 7     ; 36769
261         .word   7120            ; 8     ; 32847
262         .word   7146            ; 9     ; 32821
263         .word   7146            ; 10    ; 32821
264         .word   7146            ; 11    ; 32821
265         .word   162             ; 12    ; 39805
266         .word   0 ;-328         ; 13    ; 40295
267         .word   3278            ; 14    ; 36689
268         .word   7120            ; 15    ; 32847
269         .word   0               ; 16    ; 39967
270         .word   0 ;-320         ; 17    ; 40287
271         .word   0 ;-572         ; 18    ; 40539
272         .word   0 ;-560         ; 19    ; 40527
273         .word   0 ;-296         ; 20    ; 40263
274         .word   184             ; 21    ; 39783
275         .word   1192            ; 22    ; 38775
276         .word   3208            ; 23    ; 36759
277         .word   7146            ; 24    ; 32821
278         .word   7146            ; 25    ; 32821
279         .word   7146            ; 26    ; 32821
280         .word   7146            ; 27    ; 32821
281         .word   160             ; 28    ; 39807
282         .word   0 ;-332         ; 29    ; 40299
283         .word   3304            ; 30    ; 36663
284         .word   7146            ; 31    ; 32821
285
286 ; the program used to get these values (Atari BASIC):
287 ;  100 FILE=0
288 ;  110 IF FILE=1 THEN OPEN #1,8,0,"D:FREEMEM.OUT"
289 ;  120 IF FILE<>1 THEN OPEN #1,8,0,"E:"
290 ;  200 DIM G(32)
291 ;  210 FOR I=0 TO 32:GRAPHICS I:GOSUB 1000:G(I)=VAL:NEXT I
292 ;  220 GRAPHICS 0
293 ;  230 FOR I=0 TO 31:PRINT #1;I;":",G(I);" - ";G(0)-G(I):NEXT I
294 ;  240 CLOSE #1
295 ;  999 END
296 ;  1000 VAL=PEEK(741)+256*PEEK(742)
297 ;  1010 RETURN
298
299         .bss
300
301 spsave:         .res    1
302 appmsav:        .res    1
303 old_shflok:     .res    1
304 old_lmargin:    .res    1
305 .ifdef RESERVE_MOUSE_MEMORY
306 mouse_pm0:      .res    1
307 .endif
308
309         .segment "AUTOSTRT"
310         .word   $02E0
311         .word   $02E1
312         .word   __CODE_LOAD__ + 1