]> git.sur5r.net Git - cc65/blob - libsrc/plus4/crt0.s
Fixed capslock on startup
[cc65] / libsrc / plus4 / crt0.s
1 ;
2 ; Startup code for cc65 (Plus/4 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .export         brk_jmp
9
10         .import         condes, initlib, donelib
11         .import         push0, callmain, zerobss
12         .import         __IRQFUNC_TABLE__, __IRQFUNC_COUNT__
13
14         .include        "zeropage.inc"
15         .include        "plus4.inc"
16
17
18 ; ------------------------------------------------------------------------
19 ; Constants
20
21 IRQInd          = $500  ; JMP $0000 - used as indirect IRQ vector
22
23 ; ------------------------------------------------------------------------
24 ; Place the startup code in a special segment to cope with the quirks of
25 ; plus/4 banking.
26
27 .segment        "STARTUP"
28
29         .word   Head            ; Load address
30 Head:   .word   @Next
31         .word   1000            ; Line number
32         .byte   $9E,"4109"      ; SYS 4109
33         .byte   $00             ; End of BASIC line
34 @Next:  .word   0               ; BASIC end marker
35
36 ; ------------------------------------------------------------------------
37 ; Actual code
38
39         sei                     ; No interrupts since we're banking out the ROM
40         sta     ENABLE_RAM
41         ldx     #zpspace-1
42 L1:     lda     sp,x
43         sta     zpsave,x        ; save the zero page locations we need
44         dex
45         bpl     L1
46         sta     ENABLE_ROM
47         cli
48
49 ; Close open files
50
51         jsr     $FFCC           ; CLRCH
52
53 ; Switch to second charset
54
55         lda     #14
56         jsr     $FFD2           ; BSOUT
57
58 ; Setup the IRQ vector in the banked RAM and switch off the ROM
59
60         sei                     ; No ints, handler not yet in place
61         sta     ENABLE_RAM
62         lda     #<IRQ
63         sta     $FFFE           ; Install interrupt handler
64         lda     #>IRQ
65         sta     $FFFF
66         cli                     ; Allow interrupts
67
68 ; Clear the BSS data
69
70         jsr     zerobss
71
72 ; Save system stuff and setup the stack. The stack starts at the top of the
73 ; usable RAM.
74
75         tsx
76         stx     spsave          ; save system stk ptr
77
78         lda     #<$FD00
79         sta     sp
80         lda     #>$FD00
81         sta     sp+1
82
83 ; Call module constructors
84
85         jsr     initlib
86
87 ; If we have IRQ functions, chain our stub into the IRQ vector
88
89         lda     #<__IRQFUNC_COUNT__
90         beq     NoIRQ1
91         lda     IRQVec
92         ldx     IRQVec+1
93         sta     IRQInd+1
94         stx     IRQInd+2
95         lda     #<IRQStub
96         ldx     #>IRQStub
97         sei
98         sta     IRQVec
99         stx     IRQVec+1
100         cli
101
102 ; Push arguments and call main()
103
104 NoIRQ1: jsr     callmain
105
106 ; Back from main (this is also the _exit entry). Reset the IRQ vector if
107 ; we chained it.
108
109 _exit:  lda     #<__IRQFUNC_COUNT__
110         beq     NoIRQ2
111         lda     IRQInd+1
112         ldx     IRQInd+2
113         sei
114         sta     IRQVec
115         stx     IRQVec+1
116         cli
117
118 ; Run module destructors.
119
120 NoIRQ2: jsr     donelib         ; Run module destructors
121
122 ; Restore system stuff
123
124         ldx     spsave
125         txs
126
127 ; Copy back the zero page stuff
128
129         ldx     #zpspace-1
130 L2:     lda     zpsave,x
131         sta     sp,x
132         dex
133         bpl     L2
134
135 ; Enable the ROM, reset changed vectors and return to BASIC
136
137         sta     ENABLE_ROM
138         jmp     $FF8A           ; RESTOR
139
140
141 ; ------------------------------------------------------------------------
142 ; IRQ handler
143
144 .segment        "LOWCODE"
145
146 IRQ:    pha
147         txa
148         pha
149         tsx                     ; Get the stack pointer
150         lda     $0103,x         ; Get the saved status register
151         tax                     ; Save for later
152         and     #$10            ; Test for BRK bit
153         bne     dobreak
154         lda     #>irq_ret       ; Push new return address
155         pha
156         lda     #<irq_ret
157         pha
158         txa
159         pha
160         sta     ENABLE_ROM      ; Switch to ROM
161         jmp     ($FFFE)         ; Jump to kernal irq handler
162
163 irq_ret:
164         sta     ENABLE_RAM      ; Switch back to RAM
165         pla
166         tax
167         pla
168         rti
169
170 dobreak:
171         lda     brk_jmp+2       ; Check high byte of address
172         beq     nohandler
173         jmp     brk_jmp         ; Jump to the handler
174
175 ; No break handler installed, jump to ROM
176
177 nohandler:
178         tya
179         pha                     ; ROM handler expects Y on stack
180         sta     ENABLE_ROM
181         jmp     (BRKVec)        ; Jump indirect to the break vector
182
183 ; ------------------------------------------------------------------------
184 ; Stub for the IRQ chain. Is used only if there are IRQs defined. Needed in
185 ; low memory because of the banking.
186
187 .segment        "LOWCODE"
188
189 IRQStub:
190         cld                     ; Just to be sure
191         sta     ENABLE_RAM      ; Switch to RAM
192         ldy     #<(__IRQFUNC_COUNT__*2)
193         lda     #<__IRQFUNC_TABLE__
194         ldx     #>__IRQFUNC_TABLE__
195         jsr     condes                  ; Call the IRQ functions
196         sta     ENABLE_ROM
197         jmp     IRQInd                  ; Jump to the saved IRQ vector
198
199 ; ------------------------------------------------------------------------
200 ; Data
201
202 .data
203 zpsave:         .res    zpspace
204
205 ; BRK handling
206 brk_jmp:        jmp     $0000
207
208 .bss
209 spsave:         .res    1
210
211
212