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