]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
Use "override" when appending to CFLAGS, so this works even when CFLAGS is
[cc65] / libsrc / c64 / crt0.s
1 ;
2 ; Startup code for cc65 (C64 version)
3 ;
4
5         .export         _exit
6         .export         __STARTUP__ : absolute = 1      ; Mark as startup
7         .import         initlib, donelib, callirq
8         .import         zerobss
9         .import         callmain
10         .import         RESTOR, BSOUT, CLRCH
11         .import         __INTERRUPTOR_COUNT__
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   .version        ; 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 ; If we have IRQ functions, chain our stub into the IRQ vector
76
77         lda     #<__INTERRUPTOR_COUNT__
78         beq     NoIRQ1
79         lda     IRQVec
80         ldx     IRQVec+1
81         sta     IRQInd+1
82         stx     IRQInd+2
83         lda     #<IRQStub
84         ldx     #>IRQStub
85         sei
86         sta     IRQVec
87         stx     IRQVec+1
88         cli
89
90 ; Call module constructors
91
92 NoIRQ1: jsr     initlib
93
94 ; Push arguments and call main
95
96         jsr     callmain
97
98 ; Back from main (This is also the _exit entry). Run module destructors
99
100 _exit:  jsr     donelib
101
102
103 ; Reset the IRQ vector if we chained it.
104
105         pha                     ; Save the return code on stack
106         lda     #<__INTERRUPTOR_COUNT__
107         beq     NoIRQ2
108         lda     IRQInd+1
109         ldx     IRQInd+2
110         sei
111         sta     IRQVec
112         stx     IRQVec+1
113         cli
114
115 ; Copy back the zero page stuff
116
117 NoIRQ2: ldx     #zpspace-1
118 L2:     lda     zpsave,x
119         sta     sp,x
120         dex
121         bpl     L2
122
123 ; Place the program return code into ST
124
125         pla
126         sta     ST
127
128 ; Restore system stuff
129
130         ldx     spsave
131         txs                     ; Restore stack pointer
132         ldx     mmusave
133         stx     $01             ; Restore memory configuration
134
135 ; Reset changed vectors, back to basic
136
137         jmp     RESTOR
138
139 ; ------------------------------------------------------------------------
140 ; The IRQ vector jumps here, if condes routines are defined with type 2.
141
142 IRQStub:
143         cld                             ; Just to be sure
144         jsr     callirq                 ; Call the functions
145         jmp     IRQInd                  ; Jump to the saved IRQ vector
146
147 ; ------------------------------------------------------------------------
148 ; Data
149
150 .data
151
152 IRQInd: jmp     $0000
153
154 .segment        "ZPSAVE"
155
156 zpsave: .res    zpspace
157
158 .bss
159
160 spsave: .res    1
161 mmusave:.res    1