]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
Removed the call to CLRCH in the startup code.
[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 ; BASIC header with a SYS call
20
21 .segment        "EXEHDR"
22
23         .word   Head            ; Load address
24 Head:   .word   @Next
25         .word   .version        ; Line number
26         .byte   $9E             ; SYS token
27         .byte   <(((Start / 1000) .mod 10) + $30)
28         .byte   <(((Start /  100) .mod 10) + $30)
29         .byte   <(((Start /   10) .mod 10) + $30)
30         .byte   <(((Start /    1) .mod 10) + $30)
31         .byte   $00             ; End of BASIC line
32 @Next:  .word   0               ; BASIC end marker
33
34 ; ------------------------------------------------------------------------
35 ; Startup code
36
37 .segment        "STARTUP"
38
39 Start:
40
41 ; Save the zero page locations we need
42
43         ldx     #zpspace-1
44 L1:     lda     sp,x
45         sta     zpsave,x
46         dex
47         bpl     L1
48
49 ; Switch to second charset
50
51         lda     #14
52         jsr     BSOUT
53
54 ; Switch off the BASIC ROM
55
56         lda     $01
57         pha                     ; Remember the value
58         and     #$F8
59         ora     #$06            ; Enable kernal+I/O, disable basic
60         sta     $01
61
62 ; Clear the BSS data
63
64         jsr     zerobss
65
66 ; Save system settings and setup the stack
67
68         pla
69         sta     mmusave         ; Save the memory configuration
70
71         tsx
72         stx     spsave          ; Save the system stack ptr
73
74         lda     #<(__RAM_START__ + __RAM_SIZE__)
75         sta     sp
76         lda     #>(__RAM_START__ + __RAM_SIZE__)
77         sta     sp+1            ; Set argument stack ptr
78
79 ; If we have IRQ functions, chain our stub into the IRQ vector
80
81         lda     #<__INTERRUPTOR_COUNT__
82         beq     NoIRQ1
83         lda     IRQVec
84         ldx     IRQVec+1
85         sta     IRQInd+1
86         stx     IRQInd+2
87         lda     #<IRQStub
88         ldx     #>IRQStub
89         sei
90         sta     IRQVec
91         stx     IRQVec+1
92         cli
93
94 ; Call module constructors
95
96 NoIRQ1: jsr     initlib
97
98 ; Push arguments and call main
99
100         jsr     callmain
101
102 ; Back from main (This is also the _exit entry). Run module destructors
103
104 _exit:  jsr     donelib
105
106
107 ; Reset the IRQ vector if we chained it.
108
109         pha                     ; Save the return code on stack
110         lda     #<__INTERRUPTOR_COUNT__
111         beq     NoIRQ2
112         lda     IRQInd+1
113         ldx     IRQInd+2
114         sei
115         sta     IRQVec
116         stx     IRQVec+1
117         cli
118
119 ; Copy back the zero page stuff
120
121 NoIRQ2: ldx     #zpspace-1
122 L2:     lda     zpsave,x
123         sta     sp,x
124         dex
125         bpl     L2
126
127 ; Place the program return code into ST
128
129         pla
130         sta     ST
131
132 ; Restore system stuff
133
134         ldx     spsave
135         txs                     ; Restore stack pointer
136         ldx     mmusave
137         stx     $01             ; Restore memory configuration
138
139 ; Reset changed vectors, back to basic
140
141         jmp     RESTOR
142
143 ; ------------------------------------------------------------------------
144 ; The IRQ vector jumps here, if condes routines are defined with type 2.
145
146 IRQStub:
147         cld                             ; Just to be sure
148         jsr     callirq                 ; Call the functions
149         jmp     IRQInd                  ; Jump to the saved IRQ vector
150
151 ; ------------------------------------------------------------------------
152 ; Data
153
154 .data
155
156 IRQInd: jmp     $0000
157
158 .segment        "ZPSAVE"
159
160 zpsave: .res    zpspace
161
162 .bss
163
164 spsave: .res    1
165 mmusave:.res    1