]> git.sur5r.net Git - cc65/blob - libsrc/apple2/crt0.s
New loadable mouse drivers
[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, push0
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         ldx     #zpspace-1
30 L1:     lda     sp,x
31         sta     zpsave,x        ; Save the zero page locations we need
32         dex
33         bpl     L1
34
35 ; Clear the BSS data
36
37         jsr     zerobss
38
39 ; Save system stuff and setup the stack
40
41         tsx
42         stx     spsave          ; Save the system stack ptr
43
44         lda     MEMSIZE
45         sta     sp
46         lda     MEMSIZE+1
47         sta     sp+1            ; Set argument stack ptr
48
49 ; Call module constructors
50
51         jsr     initlib
52
53 ; Initialize conio stuff
54
55         lda     #$ff
56         sta     TEXTTYP
57
58 ; Set up to use Apple ROM $C000-$CFFF
59
60         ;;      sta     USEROM
61
62 ; Push arguments and call main()
63
64         jsr     callmain
65
66 ; Call module destructors. This is also the _exit entry.
67
68 _exit:  jsr     donelib
69
70 ; Restore system stuff
71
72         lda     #$ff            ; Reset text mode
73         sta     TEXTTYP
74
75         ldx     spsave
76         txs                     ; Restore stack pointer
77
78 ; Copy back the zero page stuff
79
80         ldx     #zpspace-1
81 L2:     lda     zpsave,x
82         sta     sp,x
83         dex
84         bpl     L2
85
86 ; Reset changed vectors, back to basic
87
88         jmp     RESTOR
89
90 ; ------------------------------------------------------------------------
91 ; Data
92
93 .data
94
95 zpsave: .res    zpspace
96
97 .bss
98
99 spsave: .res    1