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