]> git.sur5r.net Git - cc65/blob - libsrc/apple2/crt0.s
d51c282a2968468dde5278020d7d9709f0d0f252
[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         _main
11
12         .include        "zeropage.inc"
13         .include        "apple2.inc"
14
15 ; ------------------------------------------------------------------------
16 ; Actual code
17
18 .code
19
20         ldx     #zpspace-1
21 L1:     lda     sp,x
22         sta     zpsave,x        ; Save the zero page locations we need
23         dex
24         bpl     L1
25
26 ; Clear the BSS data
27
28         jsr     zerobss
29
30 ; Save system stuff and setup the stack
31
32         tsx
33         stx     spsave          ; Save the system stack ptr
34
35         lda     #<TOPMEM
36         sta     sp
37         lda     #>TOPMEM
38         sta     sp+1            ; Set argument stack ptr
39
40 ; Call module constructors
41
42         jsr     initlib
43
44 ; Initialize conio stuff
45
46         lda     #$ff
47         sta     TEXTTYP
48
49 ; Set up to use Apple ROM $C000-$CFFF
50
51         ;;      sta     USEROM
52
53 ; Pass an empty command line
54
55         jsr     push0           ; argc
56         jsr     push0           ; argv
57
58         ldy     #4              ; Argument size
59         jsr     _main           ; call the users code
60
61 ; Call module destructors. This is also the _exit entry.
62
63 _exit:  jsr     donelib
64
65 ; Restore system stuff
66
67         lda     #$ff            ; Reset text mode
68         sta     TEXTTYP
69
70         ldx     spsave
71         txs                     ; Restore stack pointer
72
73 ; Copy back the zero page stuff
74
75         ldx     #zpspace-1
76 L2:     lda     zpsave,x
77         sta     sp,x
78         dex
79         bpl     L2
80
81 ; Reset changed vectors, back to basic
82
83         jmp     RESTOR
84
85
86 .data
87
88 zpsave: .res    zpspace
89
90 .bss
91
92 spsave: .res    1