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