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