]> git.sur5r.net Git - cc65/blob - libsrc/apple2/crt0.s
Add new maintainer
[cc65] / libsrc / apple2 / crt0.s
1 ;
2 ; Startup code for cc65 (Apple2 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .import         initlib, donelib
9         .import         zerobss
10         .import         __STARTUP_LOAD__, __BSS_LOAD__  ; Linker generated
11         .import         callmain
12
13         .include        "zeropage.inc"
14         .include        "apple2.inc"
15
16 ; ------------------------------------------------------------------------
17 ; The executable header
18
19 .segment        "EXEHDR"
20
21         .word   __STARTUP_LOAD__                ; Start address
22         .word   __BSS_LOAD__ - __STARTUP_LOAD__ ; Size
23
24 ; ------------------------------------------------------------------------
25 ; Place the startup code in a special segment.
26
27 .segment        "STARTUP"
28
29 ; ProDOS TechRefMan, chapter 5.2.1:
30 ; "For maximum interrupt efficiency, a system program should not use more
31 ;  than the upper 3/4 of the stack."
32
33         ldx     #$FF
34         txs                     ; Init stack pointer
35
36 ; Save the zero page locations we need
37
38         ldx     #zpspace-1
39 L1:     lda     sp,x
40         sta     zpsave,x
41         dex
42         bpl     L1
43
44 ; Clear the BSS data
45
46         jsr     zerobss
47
48 ; Save system stuff and setup the stack
49
50         lda     HIMEM
51         sta     sp
52         lda     HIMEM+1
53         sta     sp+1            ; Set argument stack ptr
54
55 ; Call module constructors
56
57         jsr     initlib
58
59 ; Push arguments and call main()
60
61         jsr     callmain
62
63 ; Call module destructors. This is also the _exit entry.
64
65 _exit:  jsr     donelib
66
67 ; Copy back the zero page stuff
68
69         ldx     #zpspace-1
70 L2:     lda     zpsave,x
71         sta     sp,x
72         dex
73         bpl     L2
74
75 ; ProDOS TechRefMan, chapter 5.2.1:
76 ; "System programs should set the stack pointer to $FF at the warm-start
77 ;  entry point."
78
79         ldx     #$FF
80         txs                     ; Re-init stack pointer
81
82 ; Back to DOS
83
84         jmp     DOSWARM
85
86 ; ------------------------------------------------------------------------
87 ; Data
88
89 .data
90
91 zpsave: .res    zpspace