]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
39235e013c7ad9e0f4927b73ff14e16997b2959f
[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
16         .import         __CODE_LOAD__, __BSS_LOAD__
17
18         .include        "atari.inc"
19
20 ; ------------------------------------------------------------------------
21 ; Define and export the ZP variables for the runtime
22
23         .exportzp       sp, sreg, regsave
24         .exportzp       ptr1, ptr2, ptr3, ptr4
25         .exportzp       tmp1, tmp2, tmp3, tmp4
26         .exportzp       fntemp, regbank, zpspace
27
28 sp      = $D2           ; (2bytes) stack pointer
29 sreg    = $D4           ; (2bytes) secondary register/high 16 bit for longs
30 regsave = $D6           ; (4bytes) slot to save/restore (E)AX into
31 ptr1    = $DA           ; (2bytes)
32 ptr2    = $DC           ; (2bytes)
33 ptr3    = $DE           ; (2bytes)
34 ptr4    = $E0           ; (2bytes)
35 tmp1    = $E2           ; (1byte)
36 tmp2    = $E3           ; (1byte)
37 tmp3    = $E4           ; (1byte)
38 tmp4    = $E5           ; (1byte)
39 fntemp  = $E6           ; (2bytes) pointer to file name
40 regbank = $E8           ; (6bytes) 6 byte register bank
41 zpspace = $EE - sp      ; Zero page space allocated
42
43 ; ------------------------------------------------------------------------
44 ; EXE header
45
46         .segment "EXEHDR"
47         .word   $FFFF
48         .word   __CODE_LOAD__
49         .word   __BSS_LOAD__ - 1
50         .code
51         .reloc
52
53 ; ------------------------------------------------------------------------
54 ; Actual code
55
56         rts     ; fix for SpartaDOS / OS/A+
57                 ; they first call the entry point from AUTOSTRT and
58                 ; then the load addess (this rts here).
59                 ; We point AUTOSTRT directly after the rts.
60
61 ; Real entry point:
62
63 ; Save the zero page locations we need
64
65         ldy     #zpspace-1
66 L1:     lda     sp,y
67         sta     zpsave,y
68         dey
69         bpl     L1
70
71 ; Clear the BSS data
72
73         jsr     zerobss
74
75 ; setup the stack
76
77         tsx
78         stx     spsave
79
80 ; report memory usage and initialize stack pointer
81
82         lda     APPMHI
83         sta     appmsav
84         lda     APPMHI+1
85         sta     appmsav+1
86         
87         lda     #<$8000
88         sta     sp
89         sta     APPMHI
90         lda     #>$8000
91         sta     sp+1            ; Set argument stack ptr
92         sta     APPMHI+1
93         
94 ; set left margin to 0
95
96         lda     LMARGN
97         sta     old_lmargin
98         lda     #0
99         sta     LMARGN
100
101 ; set keyb to upper/lowercase mode
102
103         ldx     SHFLOK
104         stx     old_shflok
105         sta     SHFLOK
106         
107 ; Initialize the heap
108
109         jsr     __hinit
110
111 ; Initialize conio stuff
112
113         jsr     initconio
114
115         lda     #$FF
116         sta     CH
117
118 ; ugly hack for now: set stdio stream handles
119 ; all to iocb #0
120 ; until we know where to go with fd<->iocb relation
121 ; this won't stay here!
122
123         lda     #0
124         sta     __filetab + 2
125         sta     __filetab + 4
126
127 ; Pass command line if present
128
129         jsr     getargs
130
131         lda     argc
132         ldx     argc+1
133         jsr     pushax          ; argc
134         lda     #<argv
135         ldx     #>argv
136         jsr     pushax          ; argv
137
138         ldy     #4              ; Argument size
139         jsr     _main           ; call the users code
140
141 ; fall thru to exit...
142
143 _exit:  jsr     doatexit        ; call exit functions
144
145         ldx     spsave
146         txs                     ; Restore stack pointer
147
148 ; restore left margin
149
150         lda     old_lmargin
151         sta     LMARGN
152
153 ; restore kb mode
154
155         lda     old_shflok
156         sta     SHFLOK
157
158 ; restore APPMHI
159         
160         lda     appmsav
161         sta     APPMHI
162         lda     appmsav+1
163         sta     APPMHI+1
164
165 ; Copy back the zero page stuff
166
167         ldy     #zpspace-1
168 L2:     lda     zpsave,y
169         sta     sp,y
170         dey
171         bpl     L2
172
173 ; Back to DOS
174
175         rts
176
177 .data
178
179 zpsave: .res    zpspace
180         
181 .bss
182
183 spsave:         .res    1
184 appmsav:        .res    1
185 old_shflok:     .res    1
186 old_lmargin:    .res    1
187
188         .segment "AUTOSTRT"
189         .word   $02E0
190         .word   $02E1
191         .word   __CODE_LOAD__ + 1