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