]> git.sur5r.net Git - cc65/blob - libsrc/c128/crt0.s
Set the file name bank to bank zero on startup to make our file routines
[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 ; Set the bank for the file name our execution bank 
59
60         ldx     #0
61         jsr     SETBNK
62
63 ; Before doing anything else, we have to setup our banking configuration.
64 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
65 ; to the underlying RAM works, but it is bad style.
66
67         lda     MMU_CR          ; Get current memory configuration...
68         pha                     ; ...and save it for later
69         lda     #CC65_MMU_CFG   ; Bank0 with kernal ROM
70         sta     MMU_CR
71
72 ; Save the zero page locations we need
73
74         ldx     #zpspace-1
75 L1:     lda     sp,x
76         sta     zpsave,x
77         dex
78         bpl     L1
79
80 ; Clear the BSS data
81
82         jsr     zerobss
83
84 ; Save system stuff and setup the stack
85
86         pla                     ; Get MMU setting
87         sta     mmusave
88
89         tsx
90         stx     spsave          ; Save the system stack pointer
91
92         lda     #<(__RAM_START__ + __RAM_SIZE__)
93         sta     sp
94         lda     #>(__RAM_START__ + __RAM_SIZE__)
95         sta     sp+1            ; Set argument stack ptr
96
97 ; Call module constructors
98
99         jsr     initlib
100
101 ; Initialize conio stuff
102
103         jsr     initconio
104
105 ; If we have IRQ functions, chain our stub into the IRQ vector
106
107         lda     #<__IRQFUNC_COUNT__
108         beq     NoIRQ1
109         lda     IRQVec
110         ldx     IRQVec+1
111         sta     IRQInd+1
112         stx     IRQInd+2
113         lda     #<IRQStub
114         ldx     #>IRQStub
115         sei
116         sta     IRQVec
117         stx     IRQVec+1
118         cli
119
120 ; Pass an empty command line
121
122 NoIRQ1: jsr     push0           ; argc
123         jsr     push0           ; argv
124
125         ldy     #4              ; Argument size
126         jsr     _main           ; call the users code
127
128 ; This is also the _exit entry. Reset the IRQ vector if we chained it.
129
130 _exit:  lda     #<__IRQFUNC_COUNT__
131         beq     NoIRQ2
132         lda     IRQInd+1
133         ldx     IRQInd+2
134         sei
135         sta     IRQVec
136         stx     IRQVec+1
137         cli
138
139 ; Run module destructors
140
141 NoIRQ2: jsr     donelib
142
143 ; Reset the conio stuff
144
145         jsr     doneconio
146
147 ; Reset the stack
148
149         ldx     spsave
150         txs
151
152 ; Copy back the zero page stuff
153
154         ldx     #zpspace-1
155 L2:     lda     zpsave,x
156         sta     sp,x
157         dex
158         bpl     L2
159
160 ; Reset the memory configuration
161
162         lda     mmusave
163         sta     MMU_CR
164
165 ; Done, restore kernal vectors in an attempt to cleanup
166
167         jmp     RESTOR
168
169 ; ------------------------------------------------------------------------
170 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
171 ; above will change this setting so that we have RAM from $0000-$BFFF. This
172 ; works quite well with the exception of interrupts: The interrupt handler
173 ; is in ROM, and the ROM switches back to the ROM configuration, which means
174 ; that parts of our program may not be accessible. Since the crt0 module is
175 ; the first module in the program, it will always be below $4000 and always
176 ; in RAM. So we place several short stubs here that switch back our ROM
177 ; config before calling our user defined handlers. These stubs are only
178 ; used if any other code uses the interrupt or break vectors. They are dead
179 ; code otherwise, but since there is no other way to keep them in low memory,
180 ; they have to go here.
181
182 IRQStub:
183         cld                             ; Just to be sure
184         lda     MMU_CR                  ; Get old register value
185         pha                             ; And save on stack
186         lda     #CC65_MMU_CFG           ; Bank 0 with kernal ROM
187         sta     MMU_CR
188         ldy     #<(__IRQFUNC_COUNT__*2)
189         lda     #<__IRQFUNC_TABLE__
190         ldx     #>__IRQFUNC_TABLE__
191         jsr     condes                  ; Call the functions
192         pla                             ; Get old register value
193         sta     MMU_CR
194         jmp     IRQInd                  ; Jump to the save IRQ vector
195
196
197 BRKStub:
198         pla                             ; Get original MMU_CR value
199         sta     MMU_CR                  ; And set it
200         jmp     BRKInd                  ; Jump indirect to break
201
202
203 ; ------------------------------------------------------------------------
204 ; Data
205
206 .data
207 zpsave: .res    zpspace
208
209 ; Old break vector preceeded by a jump opcode
210 BRKOld: jmp     $0000
211
212 ; Indirect vectors preceeded by a jump opcode
213 BRKInd: jmp     $0000
214
215 .bss
216 spsave: .res    1
217 mmusave:.res    1
218
219
220