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