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