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