]> git.sur5r.net Git - cc65/blob - libsrc/plus4/crt0.s
User linker generated symbols for stack placement (like most other targets) instead...
[cc65] / libsrc / plus4 / crt0.s
1 ;
2 ; Startup code for cc65 (Plus/4 version)
3 ;
4
5         .export         _exit
6         .export         brk_jmp
7         .export         __STARTUP__ : absolute = 1      ; Mark as startup
8
9         .import         callirq_y, initlib, donelib
10         .import         callmain, zerobss
11         .import         __INTERRUPTOR_COUNT__
12         .import         __RAM_START__, __RAM_SIZE__     ; Linker generated
13         .import         __STACKSIZE__                   ; Linker generated
14
15         .include        "zeropage.inc"
16         .include        "plus4.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; Constants
21
22 IRQInd          = $500  ; JMP $0000 - used as indirect IRQ vector
23
24 ; ------------------------------------------------------------------------
25 ; BASIC header with a SYS call
26
27 .segment        "EXEHDR"
28
29         .word   Head            ; Load address
30 Head:   .word   @Next
31         .word   .version        ; Line number
32         .byte   $9E             ; SYS token
33         .byte   <(((Start / 1000) .mod 10) + '0')
34         .byte   <(((Start /  100) .mod 10) + '0')
35         .byte   <(((Start /   10) .mod 10) + '0')
36         .byte   <(((Start /    1) .mod 10) + '0')
37         .byte   $00             ; End of BASIC line
38 @Next:  .word   0               ; BASIC end marker
39
40 ; ------------------------------------------------------------------------
41 ; Startup code
42
43 .segment        "STARTUP"
44
45 Start:
46
47 ; Save the zero page locations we need
48
49         sei                     ; No interrupts since we're banking out the ROM
50         sta     ENABLE_RAM
51         ldx     #zpspace-1
52 L1:     lda     sp,x
53         sta     zpsave,x
54         dex
55         bpl     L1
56         sta     ENABLE_ROM
57         cli
58
59 ; Switch to second charset
60
61         lda     #14
62         jsr     $FFD2           ; BSOUT
63
64 ; Save system stuff and setup the stack. The stack starts at the top of the
65 ; usable RAM.
66
67         tsx
68         stx     spsave          ; save system stk ptr
69
70         lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
71         sta     sp
72         lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
73         sta     sp+1
74
75 ; Setup the IRQ vector in the banked RAM and switch off the ROM
76
77         ldx     #<IRQ
78         ldy     #>IRQ
79         sei                     ; No ints, handler not yet in place
80         sta     ENABLE_RAM
81         stx     $FFFE           ; Install interrupt handler
82         sty     $FFFF
83         cli                     ; Allow interrupts
84
85 ; Clear the BSS data
86
87         jsr     zerobss
88
89 ; Initialize irqcount, which means that from now own custom linked in IRQ
90 ; handlers (via condes) will be called.
91
92         lda     #.lobyte(__INTERRUPTOR_COUNT__*2)
93         sta     irqcount
94
95 ; Call module constructors
96
97         jsr     initlib
98
99 ; Push arguments and call main()
100
101         jsr     callmain
102
103 ; Back from main (this is also the _exit entry). Run module destructors.
104
105 _exit:  pha                     ; Save the return code
106         jsr     donelib         ; Run module destructors
107
108 ; Disable chained IRQ handlers
109
110         lda     #0
111         sta     irqcount        ; Disable custom IRQ handlers
112
113 ; Copy back the zero page stuff
114
115         ldx     #zpspace-1
116 L2:     lda     zpsave,x
117         sta     sp,x
118         dex
119         bpl     L2
120
121 ; Place the program return code into ST
122
123         pla
124         sta     ST
125
126 ; Restore the stack pointer
127
128         ldx     spsave
129         txs
130
131 ; Enable the ROM, reset changed vectors and return to BASIC
132
133         sta     ENABLE_ROM
134         jmp     $FF8A           ; RESTOR
135
136
137 ; ------------------------------------------------------------------------
138 ; IRQ handler. The handler in the ROM enables the kernal and jumps to
139 ; $CE00, where the ROM code checks for a BRK or IRQ and branches via the
140 ; indirect vectors at $314/$316.
141 ; To make our stub as fast as possible, we skip the whole part of the ROM
142 ; handler and jump to the indirect vectors directly. We do also call our
143 ; own interrupt handlers if we have any, so they need not use $314.
144
145 .segment        "LOWCODE"
146
147 IRQ:    cld                     ; Just to be sure
148         pha
149         txa
150         pha
151         tya
152         pha
153         tsx                     ; Get the stack pointer
154         lda     $0104,x         ; Get the saved status register
155         and     #$10            ; Test for BRK bit
156         bne     dobreak
157
158 ; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
159 ; a flag here instead of loading __INTERRUPTOR_COUNT__ directly, since the
160 ; condes function is not reentrant. The irqcount flag will be set/reset from
161 ; the main code, to avoid races.
162
163         ldy     irqcount
164         beq     @L1
165         jsr     callirq_y       ; Call the IRQ functions
166
167 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
168 ; on stack, so we get control of the CPU after the ROM handler and can switch
169 ; back to RAM.
170
171 @L1:    lda     #>irq_ret       ; Push new return address
172         pha
173         lda     #<irq_ret
174         pha
175         php                     ; Push faked IRQ frame on stack
176         pha                     ; Push faked A register
177         pha                     ; Push faked X register
178         pha                     ; Push faked Y register
179         sta     ENABLE_ROM      ; Switch to ROM
180         jmp     (IRQVec)        ; Jump indirect to kernal irq handler
181
182 irq_ret:
183         sta     ENABLE_RAM      ; Switch back to RAM
184         pla
185         tay
186         pla
187         tax
188         pla
189         rti
190
191 dobreak:
192         lda     brk_jmp+2       ; Check high byte of address
193         beq     nohandler
194         jmp     brk_jmp         ; Jump to the handler
195
196 ; No break handler installed, jump to ROM
197
198 nohandler:
199         sta     ENABLE_ROM
200         jmp     (BRKVec)        ; Jump indirect to the break vector
201
202 ; ------------------------------------------------------------------------
203 ; Data
204
205 .data
206
207 ; BRK handling
208 brk_jmp:        jmp     $0000
209
210 spsave:         .res    1
211
212 irqcount:       .byte   0
213
214 .segment        "ZPSAVE"
215
216 zpsave:         .res    zpspace
217
218