]> git.sur5r.net Git - cc65/blob - libsrc/c128/crt0.s
Use external symbols for the CBM kernal jump table functions. This allows
[cc65] / libsrc / c128 / crt0.s
1 ;
2 ; Startup code for cc65 (C128 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .export         BRKStub, BRKOld, BRKInd
9         .import         condes, initlib, donelib
10         .import         initconio, doneconio, zerobss
11         .import         push0, _main
12         .import         RESTOR, BSOUT, CLRCH
13         .import         __IRQFUNC_TABLE__, __IRQFUNC_COUNT__
14         .import         __RAM_START__, __RAM_SIZE__
15
16         .include        "zeropage.inc"
17         .include        "c128.inc"
18
19
20 ; ------------------------------------------------------------------------
21 ; Constants
22
23 IRQInd          = $2FD  ; JMP $0000 - used as indirect IRQ vector
24
25 ; ------------------------------------------------------------------------
26 ; Place the startup code in a special segment to cope with the quirks of
27 ; c128 banking. Do also create an empty segment named "NMI" to avoid
28 ; warnings if the rs232 routines are not used.
29
30 .segment        "NMI"
31 ; empty
32
33 .segment        "STARTUP"
34
35 ; ------------------------------------------------------------------------
36 ; BASIC header with a SYS call
37
38         .org    $1BFF
39         .word   Head            ; Load address
40 Head:   .word   @Next
41         .word   1000            ; Line number
42         .byte   $9E,"7181"      ; SYS 7181
43         .byte   $00             ; End of BASIC line
44 @Next:  .word   0               ; BASIC end marker
45         .reloc
46
47 ; ------------------------------------------------------------------------
48 ; Actual code
49
50 ; Close open files
51
52         jsr     CLRCH
53
54 ; Switch to the second charset
55
56         lda     #14
57         jsr     BSOUT
58
59 ; Set the bank for the file name our execution bank
60
61         lda     #0
62         sta     FNAM_BANK
63
64 ; Before doing anything else, we have to setup our banking configuration.
65 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
66 ; to the underlying RAM works, but it is bad style.
67
68         lda     MMU_CR          ; Get current memory configuration...
69         pha                     ; ...and save it for later
70         lda     #CC65_MMU_CFG   ; Bank0 with kernal ROM
71         sta     MMU_CR
72
73 ; Save the zero page locations we need
74
75         ldx     #zpspace-1
76 L1:     lda     sp,x
77         sta     zpsave,x
78         dex
79         bpl     L1
80
81 ; Clear the BSS data
82
83         jsr     zerobss
84
85 ; Save system stuff and setup the stack
86
87         pla                     ; Get MMU setting
88         sta     mmusave
89
90         tsx
91         stx     spsave          ; Save the system stack pointer
92
93         lda     #<(__RAM_START__ + __RAM_SIZE__)
94         sta     sp
95         lda     #>(__RAM_START__ + __RAM_SIZE__)
96         sta     sp+1            ; Set argument stack ptr
97
98 ; Call module constructors
99
100         jsr     initlib
101
102 ; Initialize conio stuff
103
104         jsr     initconio
105
106 ; If we have IRQ functions, chain our stub into the IRQ vector
107
108         lda     #<__IRQFUNC_COUNT__
109         beq     NoIRQ1
110         lda     IRQVec
111         ldx     IRQVec+1
112         sta     IRQInd+1
113         stx     IRQInd+2
114         lda     #<IRQStub
115         ldx     #>IRQStub
116         sei
117         sta     IRQVec
118         stx     IRQVec+1
119         cli
120
121 ; Pass an empty command line
122
123 NoIRQ1: jsr     push0           ; argc
124         jsr     push0           ; argv
125
126         ldy     #4              ; Argument size
127         jsr     _main           ; call the users code
128
129 ; This is also the _exit entry. Reset the IRQ vector if we chained it.
130
131 _exit:  lda     #<__IRQFUNC_COUNT__
132         beq     NoIRQ2
133         lda     IRQInd+1
134         ldx     IRQInd+2
135         sei
136         sta     IRQVec
137         stx     IRQVec+1
138         cli
139
140 ; Run module destructors
141
142 NoIRQ2: jsr     donelib
143
144 ; Reset the conio stuff
145
146         jsr     doneconio
147
148 ; Reset the stack
149
150         ldx     spsave
151         txs
152
153 ; Copy back the zero page stuff
154
155         ldx     #zpspace-1
156 L2:     lda     zpsave,x
157         sta     sp,x
158         dex
159         bpl     L2
160
161 ; Reset the memory configuration
162
163         lda     mmusave
164         sta     MMU_CR
165
166 ; Done, restore kernal vectors in an attempt to cleanup
167
168         jmp     RESTOR
169
170 ; ------------------------------------------------------------------------
171 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
172 ; above will change this setting so that we have RAM from $0000-$BFFF. This
173 ; works quite well with the exception of interrupts: The interrupt handler
174 ; is in ROM, and the ROM switches back to the ROM configuration, which means
175 ; that parts of our program may not be accessible. Since the crt0 module is
176 ; the first module in the program, it will always be below $4000 and always
177 ; in RAM. So we place several short stubs here that switch back our ROM
178 ; config before calling our user defined handlers. These stubs are only
179 ; used if any other code uses the interrupt or break vectors. They are dead
180 ; code otherwise, but since there is no other way to keep them in low memory,
181 ; they have to go here.
182
183 IRQStub:
184         cld                             ; Just to be sure
185         lda     MMU_CR                  ; Get old register value
186         pha                             ; And save on stack
187         lda     #CC65_MMU_CFG           ; Bank 0 with kernal ROM
188         sta     MMU_CR
189         ldy     #<(__IRQFUNC_COUNT__*2)
190         lda     #<__IRQFUNC_TABLE__
191         ldx     #>__IRQFUNC_TABLE__
192         jsr     condes                  ; Call the functions
193         pla                             ; Get old register value
194         sta     MMU_CR
195         jmp     IRQInd                  ; Jump to the save IRQ vector
196
197
198 BRKStub:
199         pla                             ; Get original MMU_CR value
200         sta     MMU_CR                  ; And set it
201         jmp     BRKInd                  ; Jump indirect to break
202
203
204 ; ------------------------------------------------------------------------
205 ; Data
206
207 .data
208 zpsave: .res    zpspace
209
210 ; Old break vector preceeded by a jump opcode
211 BRKOld: jmp     $0000
212
213 ; Indirect vectors preceeded by a jump opcode
214 BRKInd: jmp     $0000
215
216 .bss
217 spsave: .res    1
218 mmusave:.res    1
219
220
221