]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
New loadable mouse drivers
[cc65] / libsrc / c64 / crt0.s
1 ;
2 ; Startup code for cc65 (C64 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         callmain
11         .import         RESTOR, BSOUT, CLRCH
12         .import         __RAM_START__, __RAM_SIZE__     ; Linker generated
13
14         .include        "zeropage.inc"
15         .include        "c64.inc"
16
17
18 ; ------------------------------------------------------------------------
19 ; Place the startup code in a special segment.
20
21 .segment        "STARTUP"
22
23 ; BASIC header with a SYS call
24
25         .word   Head            ; Load address
26 Head:   .word   @Next
27         .word   1000            ; Line number
28         .byte   $9E,"2061"      ; SYS 2061
29         .byte   $00             ; End of BASIC line
30 @Next:  .word   0               ; BASIC end marker
31
32 ; ------------------------------------------------------------------------
33 ; Actual code
34
35         ldx     #zpspace-1
36 L1:     lda     sp,x
37         sta     zpsave,x        ; Save the zero page locations we need
38         dex
39         bpl     L1
40
41 ; Close open files
42
43         jsr     CLRCH
44
45 ; Switch to second charset
46
47         lda     #14
48         jsr     BSOUT
49
50 ; Switch off the BASIC ROM
51
52         lda     $01
53         pha                     ; Remember the value
54         and     #$F8
55         ora     #$06            ; Enable kernal+I/O, disable basic
56         sta     $01
57
58 ; Clear the BSS data
59
60         jsr     zerobss
61
62 ; Save system settings and setup the stack
63
64         pla
65         sta     mmusave         ; Save the memory configuration
66
67         tsx
68         stx     spsave          ; Save the system stack ptr
69
70         lda     #<(__RAM_START__ + __RAM_SIZE__)
71         sta     sp
72         lda     #>(__RAM_START__ + __RAM_SIZE__)
73         sta     sp+1            ; Set argument stack ptr
74
75 ; Call module constructors
76
77         jsr     initlib
78
79 ; Push arguments and call main
80
81         jsr     callmain
82
83 ; Call module destructors. This is also the _exit entry.
84
85 _exit:  jsr     donelib         ; Run module destructors
86
87 ; Restore system stuff
88
89         ldx     spsave
90         txs                     ; Restore stack pointer
91         lda     mmusave
92         sta     $01             ; Restore memory configuration
93
94 ; Copy back the zero page stuff
95
96         ldx     #zpspace-1
97 L2:     lda     zpsave,x
98         sta     sp,x
99         dex
100         bpl     L2
101
102 ; Reset changed vectors, back to basic
103
104         jmp     RESTOR
105
106
107 ; ------------------------------------------------------------------------
108 ; Data
109
110 .data
111
112 zpsave: .res    zpspace
113
114 .bss
115
116 spsave: .res    1
117 mmusave:.res    1