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