]> git.sur5r.net Git - cc65/blob - libsrc/atari/crt0.s
add MyDOS error codes, contributed by Stefan Haubenthal
[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         .constructor    initsp, 26
14
15         .import         initlib, donelib, callmain
16         .import         zerobss, pushax
17         .import         _main, __filetab, getfd
18         .import         __CODE_LOAD__, __BSS_LOAD__
19
20         .include        "zeropage.inc"
21         .include        "atari.inc"
22         .include        "_file.inc"
23
24 ; ------------------------------------------------------------------------
25 ; EXE header
26
27         .segment "EXEHDR"
28         .word   $FFFF
29         .word   __CODE_LOAD__
30         .word   __BSS_LOAD__ - 1
31
32 ; ------------------------------------------------------------------------
33 ; Actual code
34
35 .code
36
37         rts     ; fix for SpartaDOS / OS/A+
38                 ; they first call the entry point from AUTOSTRT and
39                 ; then the load addess (this rts here).
40                 ; We point AUTOSTRT directly after the rts.
41
42 ; Real entry point:
43
44 ; Save the zero page locations we need
45
46         ldx     #zpspace-1
47 L1:     lda     sp,x
48         sta     zpsave,x
49         dex
50         bpl     L1
51
52 ; Clear the BSS data
53
54         jsr     zerobss
55
56 ; setup the stack
57
58         tsx
59         stx     spsave
60
61 ; report memory usage
62
63         lda     APPMHI
64         sta     appmsav                 ; remember old APPMHI value
65         lda     APPMHI+1
66         sta     appmsav+1
67
68         lda     MEMTOP
69         sta     APPMHI                  ; initialize our APPMHI value
70         ldx     MEMTOP+1
71         stx     APPMHI+1
72
73 ; Call module constructors
74
75         jsr     initlib
76
77 ; set left margin to 0
78
79         lda     LMARGN
80         sta     old_lmargin
81         lda     #0
82         sta     LMARGN
83
84 ; set keyb to upper/lowercase mode
85
86         ldx     SHFLOK
87         stx     old_shflok
88         sta     SHFLOK
89
90 ; Initialize conio stuff
91
92         lda     #$FF
93         sta     CH
94
95 ; set stdio stream handles
96
97         lda     #0
98         jsr     getfd
99         sta     __filetab + (0 * .sizeof(_FILE)); setup stdin
100         lda     #0
101         jsr     getfd
102         sta     __filetab + (1 * .sizeof(_FILE)); setup stdout
103         lda     #0
104         jsr     getfd
105         sta     __filetab + (2 * .sizeof(_FILE)); setup stderr
106
107 ; Push arguments and call main
108
109         jsr     callmain
110
111 ; Call module destructors. This is also the _exit entry.
112
113 _exit:  jsr     donelib         ; Run module destructors
114
115 ; Restore system stuff
116
117         ldx     spsave
118         txs                     ; Restore stack pointer
119
120 ; restore left margin
121
122         lda     old_lmargin
123         sta     LMARGN
124
125 ; restore kb mode
126
127         lda     old_shflok
128         sta     SHFLOK
129
130 ; restore APPMHI
131
132         lda     appmsav
133         sta     APPMHI
134         lda     appmsav+1
135         sta     APPMHI+1
136
137 ; Copy back the zero page stuff
138
139         ldx     #zpspace-1
140 L2:     lda     zpsave,x
141         sta     sp,x
142         dex
143         bpl     L2
144
145 ; turn on cursor
146
147         inx
148         stx     CRSINH
149
150 ; Back to DOS
151
152         rts
153
154 ; *** end of main startup code
155
156 ; setup sp
157
158 initsp:
159         lda     APPMHI
160         sta     sp
161         lda     APPMHI+1
162         sta     sp+1
163         rts
164
165         .data
166
167 zpsave: .res    zpspace
168
169         .bss
170
171 spsave:         .res    1
172 appmsav:        .res    1
173 old_shflok:     .res    1
174 old_lmargin:    .res    1
175
176         .segment "AUTOSTRT"
177         .word   $02E0
178         .word   $02E1
179         .word   __CODE_LOAD__ + 1