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