]> git.sur5r.net Git - cc65/blob - libsrc/plus4/crt0.s
Use new callirq function
[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         callirq_y, initlib, donelib
11         .import         push0, callmain, zerobss
12         .import         __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   .version        ; 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 ; Save system stuff and setup the stack. The stack starts at the top of the
59 ; usable RAM.
60
61         tsx
62         stx     spsave          ; save system stk ptr
63
64         lda     #<$FD00
65         sta     sp
66         lda     #>$FD00
67         sta     sp+1
68
69 ; Setup the IRQ vector in the banked RAM and switch off the ROM
70
71         ldx     #<IRQ
72         ldy     #>IRQ
73         sei                     ; No ints, handler not yet in place
74         sta     ENABLE_RAM
75         stx     $FFFE           ; Install interrupt handler
76         sty     $FFFF
77         cli                     ; Allow interrupts
78
79 ; Clear the BSS data
80
81         jsr     zerobss
82
83 ; Call module constructors
84
85         jsr     initlib
86
87 ; Initialize irqcount, which means that from now own custom linked in IRQ
88 ; handlers (via condes) will be called.
89
90         lda     #.lobyte(__IRQFUNC_COUNT__*2)
91         sta     irqcount
92
93 ; Push arguments and call main()
94
95         jsr     callmain
96
97 ; Back from main (this is also the _exit entry). Run module destructors.
98
99 _exit:  pha                     ; Save the return code
100         lda     #0
101         sta     irqcount        ; Disable custom IRQ handlers
102         jsr     donelib         ; Run module destructors
103
104 ; Copy back the zero page stuff
105
106         ldx     #zpspace-1
107 L2:     lda     zpsave,x
108         sta     sp,x
109         dex
110         bpl     L2
111
112 ; Place the program return code into ST
113
114         pla
115         sta     ST
116
117 ; Restore the stack pointer
118
119         ldx     spsave
120         txs
121
122 ; Enable the ROM, reset changed vectors and return to BASIC
123
124         sta     ENABLE_ROM
125         jmp     $FF8A           ; RESTOR
126
127
128 ; ------------------------------------------------------------------------
129 ; IRQ handler. The handler in the ROM enables the kernal and jumps to
130 ; $CE00, where the ROM code checks for a BRK or IRQ and branches via the
131 ; indirect vectors at $314/$316.
132 ; To make our stub as fast as possible, we skip the whole part of the ROM
133 ; handler and jump to the indirect vectors directly. We do also call our
134 ; own interrupt handlers if we have any, so they need not use $314.
135
136 .segment        "LOWCODE"
137
138 IRQ:    cld                     ; Just to be sure
139         pha
140         txa
141         pha
142         tya
143         pha
144         tsx                     ; Get the stack pointer
145         lda     $0104,x         ; Get the saved status register
146         and     #$10            ; Test for BRK bit
147         bne     dobreak
148
149 ; It's an IRQ and RAM is enabled. If we have handlers, call them. We will use
150 ; a flag here instead of loading __IRQFUNC_COUNT__ directly, since the condes
151 ; function is not reentrant. The irqcount flag will be set/reset from the main
152 ; code, to avoid races.
153
154         ldy     irqcount
155         beq     @L1
156         jsr     callirq_y       ; Call the IRQ functions
157
158 ; Since the ROM handler will end with an RTI, we have to fake an IRQ return
159 ; on stack, so we get control of the CPU after the ROM handler and can switch
160 ; back to RAM.
161
162 @L1:    lda     #>irq_ret       ; Push new return address
163         pha
164         lda     #<irq_ret
165         pha
166         php                     ; Push faked IRQ frame on stack
167         pha                     ; Push faked A register
168         pha                     ; Push faked X register
169         pha                     ; Push faked Y register
170         sta     ENABLE_ROM      ; Switch to ROM
171         jmp     (IRQVec)        ; Jump indirect to kernal irq handler
172
173 irq_ret:
174         sta     ENABLE_RAM      ; Switch back to RAM
175         pla
176         tay
177         pla
178         tax
179         pla
180         rti
181
182 dobreak:
183         lda     brk_jmp+2       ; Check high byte of address
184         beq     nohandler
185         jmp     brk_jmp         ; Jump to the handler
186
187 ; No break handler installed, jump to ROM
188
189 nohandler:
190         sta     ENABLE_ROM
191         jmp     (BRKVec)        ; Jump indirect to the break vector
192
193 ; ------------------------------------------------------------------------
194 ; Data
195
196 .data
197 zpsave:         .res    zpspace
198
199 ; BRK handling
200 brk_jmp:        jmp     $0000
201
202 spsave:         .res    1
203
204 .bss
205 irqcount:       .byte   0
206