]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Use "override" when appending to CFLAGS, so this works even when CFLAGS is
[cc65] / libsrc / c16 / crt0.s
1 ;
2 ; Startup code for cc65 (C16 version)
3 ;
4 ; Note: The C16 is actually the Plus/4 with just 16KB of memory. So many
5 ; things are similar here, and we even use the plus4.inc include file.
6 ;
7
8         .export         _exit
9         .export         __STARTUP__ : absolute = 1      ; Mark as startup
10         .import         initlib, donelib, callirq
11         .import         callmain, zerobss
12         .import         MEMTOP, RESTOR, BSOUT, CLRCH
13         .import         __INTERRUPTOR_COUNT__
14
15         .include        "zeropage.inc"
16         .include        "plus4.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,"4109"      ; SYS 4109
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 ; Clear the BSS data
52
53         jsr     zerobss
54
55 ; Save system stuff and setup the stack
56
57         tsx
58         stx     spsave          ; save system stk ptr
59
60         sec
61         jsr     MEMTOP          ; Get top memory
62         cpy     #$80            ; We can only use the low 32K :-(
63         bcc     MemOk
64         ldy     #$80
65         ldx     #$00
66 MemOk:  stx     sp
67         sty     sp+1            ; set argument stack ptr
68
69 ; If we have IRQ functions, chain our stub into the IRQ vector
70
71         lda     #<__INTERRUPTOR_COUNT__
72         beq     NoIRQ1
73         lda     IRQVec
74         ldx     IRQVec+1
75         sta     IRQInd+1
76         stx     IRQInd+2
77         lda     #<IRQStub
78         ldx     #>IRQStub
79         sei
80         sta     IRQVec
81         stx     IRQVec+1
82         cli
83
84 ; Call module constructors
85
86 NoIRQ1: jsr     initlib
87
88 ; Push arguments and call main()
89
90         jsr     callmain
91
92 ; Call module destructors. This is also the _exit entry.
93
94 _exit:  pha                     ; Save the return code on stack
95         jsr     donelib         ; Run module destructors
96
97 ; Reset the IRQ vector if we chained it.
98
99         pha                     ; Save the return code on stack
100         lda     #<__INTERRUPTOR_COUNT__
101         beq     NoIRQ2
102         lda     IRQInd+1
103         ldx     IRQInd+2
104         sei
105         sta     IRQVec
106         stx     IRQVec+1
107         cli
108
109 ; Copy back the zero page stuff
110
111 NoIRQ2: ldx     #zpspace-1
112 L2:     lda     zpsave,x
113         sta     sp,x
114         dex
115         bpl     L2
116
117 ; Store the return code into ST
118
119         pla
120         sta     ST
121
122 ; Restore the stack pointer
123
124         ldx     spsave
125         txs
126
127 ; Reset changed vectors
128
129         jmp     RESTOR
130
131 ; ------------------------------------------------------------------------
132 ; The IRQ vector jumps here, if condes routines are defined with type 2.
133
134 IRQStub:
135         cld                             ; Just to be sure
136         jsr     callirq                 ; Call the functions
137         jmp     IRQInd                  ; Jump to the saved IRQ vector
138
139 ; ------------------------------------------------------------------------
140 ; Data
141
142 .data
143
144 IRQInd: jmp     $0000
145
146 .segment        "ZPSAVE"
147
148 zpsave: .res    zpspace
149
150 .bss
151
152 spsave: .res    1
153
154