]> git.sur5r.net Git - cc65/blob - libsrc/cbm510/crt0.s
Cosmetic change
[cc65] / libsrc / cbm510 / crt0.s
1 ;
2 ; Startup code for cc65 (CBM 500 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .import         _clrscr, initlib, donelib
9         .import         push0, _main
10         .import         __VIDRAM_START__
11         .import         __BSS_RUN__, __BSS_SIZE__
12         .import         irq, nmi
13         .import         k_irq, k_nmi, k_plot, k_udtim, k_scnkey
14
15         .include        "zeropage.inc"
16         .include        "io.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; Define and export the ZP variables for the CBM510 runtime
21
22         .exportzp       sp, sreg, regsave
23         .exportzp       ptr1, ptr2, ptr3, ptr4
24         .exportzp       tmp1, tmp2, tmp3, tmp4
25         .exportzp       regbank, zpspace
26         .exportzp       vic, sid, cia1, cia2, acia, tpi1, tpi2
27         .exportzp       ktab1, ktab2, ktab3, ktab4, time, RecvBuf, SendBuf
28
29 .zeropage
30
31 zpstart = *
32 sp:             .res    2       ; Stack pointer
33 sreg:           .res    2       ; Secondary register/high 16 bit for longs
34 regsave:        .res    2       ; slot to save/restore (E)AX into
35 ptr1:           .res    2
36 ptr2:           .res    2
37 ptr3:           .res    2
38 ptr4:           .res    2
39 tmp1:           .res    1
40 tmp2:           .res    1
41 tmp3:           .res    1
42 tmp4:           .res    1
43 regbank:        .res    6       ; 6 byte register bank
44
45 zpspace = * - zpstart           ; Zero page space allocated
46
47 .code
48
49 ; ------------------------------------------------------------------------
50 ; BASIC header and a small BASIC program. Since it is not possible to start
51 ; programs in other banks using SYS, the BASIC program will write a small
52 ; machine code program into memory at $100 and start that machine code
53 ; program. The machine code program will then start the machine language
54 ; code in bank 0, which will initialize the system by copying stuff from
55 ; the system bank, and start the application.
56 ;
57 ; Here's the basic program that's in the following lines:
58 ;
59 ; 10 for i=0 to 4
60 ; 20 read j
61 ; 30 poke 256+i,j
62 ; 40 next i
63 ; 50 sys 256
64 ; 60 data 120,169,0,133,0
65 ;
66 ; The machine program in the data lines is:
67 ;
68 ; sei
69 ; lda     #$00
70 ; sta     $00           <-- Switch to bank 0 after this command
71 ;
72 ; Initialization is not only complex because of the jumping from one bank
73 ; into another. but also because we want to save memory, and because of
74 ; this, we will use the system memory ($00-$3FF) for initialization stuff
75 ; that is overwritten later.
76 ;
77
78 ; To make things more simple, make the code of this module absolute.
79
80         .org    $0001
81 Head:   .byte   $03,$00,$11,$00,$0a,$00,$81,$20,$49,$b2,$30,$20,$a4,$20,$34,$00
82         .byte   $19,$00,$14,$00,$87,$20,$4a,$00,$27,$00,$1e,$00,$97,$20,$32,$35
83         .byte   $36,$aa,$49,$2c,$4a,$00,$2f,$00,$28,$00,$82,$20,$49,$00,$39,$00
84         .byte   $32,$00,$9e,$20,$32,$35,$36,$00,$4f,$00,$3c,$00,$83,$20,$31,$32
85         .byte   $30,$2c,$31,$36,$39,$2c,$30,$2c,$31,$33,$33,$2c,$30,$00,$00,$00
86
87 ; Since we need some vectors to access stuff in the system bank for our own,
88 ; we will include them here, starting from $60:
89
90         .res    $60-*
91
92 vic:            .word   $d800
93 sid:            .word   $da00
94 cia1:           .word   $db00
95 cia2:           .word   $dc00
96 acia:           .word   $dd00
97 tpi1:           .word   $de00
98 tpi2:           .word   $df00
99 ktab1:          .word   $eab1
100 ktab2:          .word   $eb11
101 ktab3:          .word   $eb71
102 ktab4:          .word   $ebd1
103 time:           .dword  $0000
104 RecvBuf:        .word   $0100           ; RS232 received buffer
105 SendBuf:        .word   $0200           ; RS232 send buffer
106
107
108 ; The code in the target bank when switching back will be put at the bottom
109 ; of the stack. We will jump here to switch segments. The range $F2..$FF is
110 ; not used by any kernal routine.
111
112         .res    $F8-*
113 Back:   ldx     spsave
114         txs
115         lda     IndReg
116         sta     ExecReg
117
118 ; The following code is a copy of the code that is poked in the system bank
119 ; memory by the basic header program, it's only for documentation and not
120 ; actually used here:
121
122         sei
123         lda     #$00
124         sta     ExecReg
125
126 ; This is the actual starting point of our code after switching banks for
127 ; startup. Beware: The following code will get overwritten as soon as we
128 ; use the stack (since it's in page 1)!
129
130         tsx
131         stx     spsave          ; Save the system stackpointer
132         ldx     #$FF
133         txs                     ; Set up our own stack
134
135 ; Set the interrupt, NMI and other vectors
136
137         ldy     #vectable_size
138 L0:     lda     vectable-1,y
139         sta     $FF80,y
140         dey
141         bne     L0
142
143 ; Switch the indirect segment to the system bank
144
145         lda     #$0F
146         sta     IndReg
147
148 ; Copy the kernal zero page ($90-$F2) from the system bank
149
150         lda     #$90
151         sta     ptr1
152         lda     #$00
153         sta     ptr1+1
154         ldy     #$62-1
155 L1:     lda     (ptr1),y
156         sta     $90,y
157         dey
158         bpl     L1
159
160 ; Copy the page 3 vectors in place
161
162         ldy     #$00
163 L2:     lda     p3vectable,y
164         sta     $300,y
165         iny
166         cpy     #p3vectable_size
167         bne     L2
168
169 ; Copy the rest of page 3 from the system bank
170
171         lda     #$00
172         sta     ptr1
173         lda     #$03
174         sta     ptr1+1
175 L3:     lda     (ptr1),y
176         sta     $300,y
177         iny
178         bne     L3
179
180 ; Set the indirect segment to bank we're executing in
181
182         lda     ExecReg
183         sta     IndReg
184
185 ; Zero the BSS segment. We will do that here instead calling the routine
186 ; in the common library, since we have the memory anyway, and this way,
187 ; it's reused later.
188
189         lda     #<__BSS_RUN__
190         sta     ptr1
191         lda     #>__BSS_RUN__
192         sta     ptr1+1
193         lda     #0
194         tay
195
196 ; Clear full pages
197
198         ldx     #>__BSS_SIZE__
199         beq     Z2
200 Z1:     sta     (ptr1),y
201         iny
202         bne     Z1
203         inc     ptr1+1                  ; Next page
204         dex
205         bne     Z1
206
207 ; Clear the remaining page
208
209 Z2:     ldx     #<__BSS_SIZE__
210         beq     Z4
211 Z3:     sta     (ptr1),y
212         iny
213         dex
214         bne     Z3
215 Z4:
216
217 ; Setup the C stack
218
219         lda     #<$FF81
220         sta     sp
221         lda     #>$FF81
222         sta     sp+1
223
224 ; We expect to be in page 2 now
225
226 .if     (* < $1FD)
227         jmp     $200
228         .res    $200-*
229 .endif
230 .if     (* < $200)
231         .res    $200-*,$EA
232 .endif
233 .if     (* >= $2F0)
234 .error  "Code range invalid"
235 .endif
236
237 ; This code is in page 2, so we may now start calling subroutines safely,
238 ; since the code we execute is no longer in the stack page.
239
240 ; Clear the video memory. We will do this before switching the video to bank 0
241 ; to avoid garbage when doing so.
242
243         jsr     _clrscr
244
245 ; Reprogram the VIC so that the text screen is at $F800 in the execution bank
246 ; This is done in three steps:
247
248         lda     #$0F                    ; We need access to the system bank
249         sta     IndReg
250
251 ; Place the VIC video RAM into bank 0
252 ; CA (STATVID) = 0
253
254         ldy     #tpiCtrlReg
255         lda     (tpi1),y
256         sta     vidsave+0
257         and     #$CF
258         ora     #$20
259         sta     (tpi1),y
260
261 ; Set bit 14/15 of the VIC address range to the high bits of __VIDRAM_START__
262 ; PC6/PC7 (VICBANKSEL 0/1) = 11
263
264         ldy     #tpiPortC
265         lda     (tpi2),y
266         sta     vidsave+1
267         and     #$3F
268         ora     #<((>__VIDRAM_START__) & $C0)
269         sta     (tpi2),y
270
271 ; Set bits 10-13 of the VIC address range to address F800
272
273         ldy     #VIC_VIDEO_ADR
274         lda     (vic),y
275         sta     vidsave+2
276         and     #$0F
277         ora     #<(((>__VIDRAM_START__) << 2) & $F0)
278         sta     (vic),y
279
280 ; Switch back to the execution bank
281
282         lda     ExecReg
283         sta     IndReg
284
285 ; Call module constructors
286
287         jsr     initlib
288
289 ; Create the (empty) command line for the program
290
291         jsr     push0           ; argc
292         jsr     push0           ; argv
293
294 ; Execute the program code
295
296         jmp     Start
297
298 ; ------------------------------------------------------------------------
299 ; Additional data that we need for initialization and that's overwritten
300 ; later
301
302 vectable:
303         jmp     $0000           ; CINT
304         jmp     $0000           ; IOINIT
305         jmp     $0000           ; RAMTAS
306         jmp     $0000           ; RESTOR
307         jmp     $0000           ; VECTOR
308         jmp     $0000           ; SETMSG
309         jmp     $0000           ; SECOND
310         jmp     $0000           ; TKSA
311         jmp     $0000           ; MEMTOP
312         jmp     $0000           ; MEMBOT
313         jmp     k_scnkey        ; SCNKEY
314         jmp     $0000           ; SETTMO
315         jmp     $0000           ; ACPTR
316         jmp     $0000           ; CIOUT
317         jmp     $0000           ; UNTLK
318         jmp     $0000           ; UNLSN
319         jmp     $0000           ; LISTEN
320         jmp     $0000           ; TALK
321         jmp     $0000           ; READST
322         jmp     k_setlfs        ; SETLFS
323         jmp     k_setnam        ; SETNAM
324         jmp     $0000           ; OPEN
325         jmp     $0000           ; CLOSE
326         jmp     $0000           ; CHKIN
327         jmp     $0000           ; CKOUT
328         jmp     $0000           ; CLRCH
329         jmp     $0000           ; BASIN
330         jmp     $0000           ; BSOUT
331         jmp     $0000           ; LOAD
332         jmp     $0000           ; SAVE
333         jmp     k_settim        ; SETTIM
334         jmp     k_rdtim         ; RDTIM
335         jmp     $0000           ; STOP
336         jmp     $0000           ; GETIN
337         jmp     $0000           ; CLALL
338         jmp     k_udtim         ; UDTIM
339         jmp     k_screen        ; SCREEN
340         jmp     k_plot          ; PLOT
341         jmp     k_iobase        ; IOBASE
342         sta     ExecReg
343         rts
344         .byte   $01             ; Filler
345         .word   nmi
346         .word   0               ; Reset - not used
347         .word   irq
348 vectable_size   = * - vectable
349
350 p3vectable:
351         .word   k_irq           ; IRQ user vector
352         .word   k_brk           ; BRK user vector
353         .word   k_nmi           ; NMI user vector
354 p3vectable_size = * - p3vectable
355
356
357 ; ------------------------------------------------------------------------
358 ; This is the program code after setup. It starts at $400
359
360         .res    $400-*
361
362 Start:
363
364 ; Enable interrupts
365
366         cli
367
368 ; Call the user code
369
370         ldy     #4              ; Argument size
371         jsr     _main           ; call the users code
372
373 ; Call module destructors. This is also the _exit entry.
374
375 _exit:  jsr     donelib         ; Run module destructors
376
377 ; We need access to the system bank now
378
379         lda     #$0F
380         sta     IndReg
381
382 ; Switch back the video to the system bank
383
384         ldy     #tpiCtrlReg
385         lda     vidsave+0
386         sta     (tpi1),y
387
388         ldy     #tpiPortC
389         lda     vidsave+1
390         sta     (tpi2),y
391
392         ldy     #VIC_VIDEO_ADR
393         lda     vidsave+2
394         sta     (vic),y
395
396 ; Clear the start of the zero page, since it will be interpreted as a
397 ; (garbage) BASIC program otherwise. This is also the default entry for
398 ; the break vector.
399
400 k_brk:  sei
401         lda     #$00
402         ldx     #$3E
403 Clear:  sta     $02,x
404         dex
405         bne     Clear
406
407 ; Setup the welcome code at the stack bottom in the system bank. Use
408 ; the F4/F5 vector to access the system bank
409
410         ldy     #$00
411         sty     $F4
412         iny
413         sty     $F5
414         ldy     #reset_size-1
415 @L1:    lda     reset,y
416         sta     ($F4),y
417         dey
418         bne     @L1
419         jmp     Back
420
421 ; ------------------------------------------------------------------------
422 ; Code that is copied into the system bank at $100 when switching back
423
424 reset:  cli
425         jmp     $8000                   ; BASIC cold start
426 reset_size = * - reset
427
428 ; ------------------------------------------------------------------------
429 ; Code for a few simpler kernal calls goes here
430
431 k_iobase:
432         ldx     cia2
433         ldy     cia2+1
434         rts
435
436 k_screen:
437         ldx     #40             ; Columns
438         ldy     #25             ; Lines
439         rts
440
441 k_setlfs:
442         sta     LogicalAdr
443         stx     FirstAdr
444         sty     SecondAdr
445         rts
446
447 k_setnam:
448         sta     FileNameLen
449         lda     $00,x
450         sta     FileNameAdrLo
451         lda     $01,x
452         sta     FileNameAdrHi
453         lda     $02,x
454         sta     FileNameAdrSeg
455         rts
456
457 k_rdtim:
458         sei
459         lda     time+0
460         ldx     time+1
461         ldy     time+2
462         cli
463         rts
464
465 k_settim:
466         sei
467         sta     time+0
468         stx     time+1
469         sty     time+2
470         cli
471         rts
472
473 ; -------------------------------------------------------------------------
474 ; Data area - switch back to relocatable mode
475
476         .reloc
477
478 .data
479 spsave: .res    1
480 vidsave:.res    3
481
482