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