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