]> git.sur5r.net Git - cc65/blob - libsrc/c128/crt0.s
d940e121c2c86242664487f0e8e88a28a7bc0f56
[cc65] / libsrc / c128 / crt0.s
1 ;
2 ; Startup code for cc65 (C128 version)
3 ;
4
5         .export         _exit
6         .export         __STARTUP__ : absolute = 1      ; Mark as startup
7         .import         callirq, initlib, donelib
8         .import         zerobss
9         .import         push0, callmain
10         .import         RESTOR, BSOUT, CLRCH
11         .import         __INTERRUPTOR_COUNT__
12         .import         __RAM_START__, __RAM_SIZE__
13
14         .include        "zeropage.inc"
15         .include        "c128.inc"
16
17
18 ; ------------------------------------------------------------------------
19 ; Constants
20
21 IRQInd          = $2FD  ; JMP $0000 - used as indirect IRQ vector
22
23 ; ------------------------------------------------------------------------
24 ; BASIC header with a SYS call
25
26 .segment        "EXEHDR"
27
28         .word   Head            ; Load address
29 Head:   .word   @Next
30         .word   .version        ; Line number
31         .byte   $9E             ; SYS token
32         .byte   <(((Start / 1000) .mod 10) + $30)
33         .byte   <(((Start /  100) .mod 10) + $30)
34         .byte   <(((Start /   10) .mod 10) + $30)
35         .byte   <(((Start /    1) .mod 10) + $30)
36         .byte   $00             ; End of BASIC line
37 @Next:  .word   0               ; BASIC end marker
38
39 ; ------------------------------------------------------------------------
40 ; Startup code
41
42 .segment        "STARTUP"
43
44 Start:
45
46 ; Close open files
47
48         jsr     CLRCH
49
50 ; Switch to the second charset
51
52         lda     #14
53         jsr     BSOUT
54
55 ; Before doing anything else, we have to setup our banking configuration.
56 ; Otherwise just the lowest 16K are actually RAM. Writing through the ROM
57 ; to the underlying RAM works, but it is bad style.
58
59         lda     MMU_CR          ; Get current memory configuration...
60         pha                     ; ...and save it for later
61         lda     #MMU_CFG_CC65   ; Bank0 with kernal ROM
62         sta     MMU_CR
63
64 ; Save the zero page locations we need
65
66         ldx     #zpspace-1
67 L1:     lda     sp,x
68         sta     zpsave,x
69         dex
70         bpl     L1
71
72 ; Clear the BSS data
73
74         jsr     zerobss
75
76 ; Save system stuff and setup the stack
77
78         pla                     ; Get MMU setting
79         sta     mmusave
80
81         tsx
82         stx     spsave          ; Save the system stack pointer
83
84         lda     #<(__RAM_START__ + __RAM_SIZE__)
85         sta     sp
86         lda     #>(__RAM_START__ + __RAM_SIZE__)
87         sta     sp+1            ; Set argument stack ptr
88
89 ; If we have IRQ functions, chain our stub into the IRQ vector
90
91         lda     #<__INTERRUPTOR_COUNT__
92         beq     NoIRQ1
93         lda     IRQVec
94         ldx     IRQVec+1
95         sta     IRQInd+1
96         stx     IRQInd+2
97         lda     #<IRQStub
98         ldx     #>IRQStub
99         sei
100         sta     IRQVec
101         stx     IRQVec+1
102         cli
103
104 ; Call module constructors
105
106 NoIRQ1: jsr     initlib
107
108 ; Set the bank for the file name to our execution bank. We must do this,
109 ; *after* calling constructors, because some of them may depend on the
110 ; original value of this register.
111
112         lda     #0
113         sta     FNAM_BANK
114
115 ; Push arguments and call main()
116
117         jsr     callmain
118
119 ; Back from main (this is also the _exit entry). Run module destructors
120
121 _exit:  jsr     donelib
122
123 ; Reset the IRQ vector if we chained it.
124
125         pha                             ; Save the return code on stack
126         lda     #<__INTERRUPTOR_COUNT__
127         beq     NoIRQ2
128         lda     IRQInd+1
129         ldx     IRQInd+2
130         sei
131         sta     IRQVec
132         stx     IRQVec+1
133         cli
134
135 ; Copy back the zero page stuff
136
137 NoIRQ2: ldx     #zpspace-1
138 L2:     lda     zpsave,x
139         sta     sp,x
140         dex
141         bpl     L2
142
143 ; Place the program return code into ST
144
145         pla
146         sta     ST
147
148 ; Reset the stack and the memory configuration
149
150         ldx     spsave
151         txs
152         ldx     mmusave
153         stx     MMU_CR
154
155 ; Done, restore kernal vectors in an attempt to cleanup
156
157         jmp     RESTOR
158
159 ; ------------------------------------------------------------------------
160 ; The C128 has ROM parallel to the RAM starting from $4000. The startup code
161 ; above will change this setting so that we have RAM from $0000-$BFFF. This
162 ; works quite well with the exception of interrupts: The interrupt handler
163 ; is in ROM, and the ROM switches back to the ROM configuration, which means
164 ; that parts of our program may not be accessible. To solve this, we place
165 ; the following code into a special segment called "LOWCODE" which will be
166 ; placed just above the startup code, so it goes into a RAM area that is
167 ; not banked.
168
169 .segment        "LOWCODE"
170
171 IRQStub:
172         cld                             ; Just to be sure
173         lda     MMU_CR                  ; Get old register value
174         pha                             ; And save on stack
175         lda     #MMU_CFG_CC65           ; Bank 0 with kernal ROM
176         sta     MMU_CR
177         jsr     callirq                 ; Call the functions
178         pla                             ; Get old register value
179         sta     MMU_CR
180         jmp     IRQInd                  ; Jump to the saved IRQ vector
181
182
183 ; ------------------------------------------------------------------------
184 ; Data
185
186 .segment        "ZPSAVE"
187
188 zpsave: .res    zpspace
189
190 .bss
191 spsave: .res    1
192 mmusave:.res    1
193
194
195