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