]> git.sur5r.net Git - cc65/blob - libsrc/c128/crt0.s
Use new callirq function
[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 ; Set the bank for the file name our execution bank
54
55         lda     #0
56         sta     FNAM_BANK
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     #MMU_CFG_CC65   ; 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 ; If we have IRQ functions, chain our stub into the IRQ vector
97
98         lda     #<__IRQFUNC_COUNT__
99         beq     NoIRQ1
100         lda     IRQVec
101         ldx     IRQVec+1
102         sta     IRQInd+1
103         stx     IRQInd+2
104         lda     #<IRQStub
105         ldx     #>IRQStub
106         sei
107         sta     IRQVec
108         stx     IRQVec+1
109         cli
110
111 ; Push arguments and call main()
112
113 NoIRQ1: jsr     callmain
114
115 ; Back from main (this is also the _exit entry). Reset the IRQ vector if we
116 ; chained it.
117
118 _exit:  pha                             ; Save the return code on stack
119         lda     #<__IRQFUNC_COUNT__
120         beq     NoIRQ2
121         lda     IRQInd+1
122         ldx     IRQInd+2
123         sei
124         sta     IRQVec
125         stx     IRQVec+1
126         cli
127
128 ; Run module destructors
129
130 NoIRQ2: jsr     donelib
131
132 ; Copy back the zero page stuff
133
134         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 .data
184 zpsave: .res    zpspace
185
186 .bss
187 spsave: .res    1
188 mmusave:.res    1
189
190
191