]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Move the startup code into its own segment
[cc65] / libsrc / c16 / crt0.s
1 ;
2 ; Startup code for cc65 (C16 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6 ; Note: The C16 is actually the Plus/4 with just 16KB of memory. So many
7 ; things are similar here, and we even use the plus4.inc include file.
8 ;
9
10         .export         _exit
11         .import         initlib, donelib
12         .import         push0, _main, zerobss
13         .import         MEMTOP, RESTOR, BSOUT, CLRCH
14
15         .include        "zeropage.inc"
16         .include        "../plus4/plus4.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; Create an empty LOWCODE segment to avoid linker warnings
21
22 .segment        "LOWCODE"
23
24 ; ------------------------------------------------------------------------
25 ; Place the startup code in a special segment.
26
27 .segment        "STARTUP"
28
29 ; BASIC header with a SYS call
30
31         .word   Head            ; Load address
32 Head:   .word   @Next
33         .word   1000            ; Line number
34         .byte   $9E,"4109"      ; SYS 4109
35         .byte   $00             ; End of BASIC line
36 @Next:  .word   0               ; BASIC end marker
37
38 ; ------------------------------------------------------------------------
39 ; Actual code
40
41         ldx     #zpspace-1
42 L1:     lda     sp,x
43         sta     zpsave,x        ; save the zero page locations we need
44         dex
45         bpl     L1
46
47 ; Close open files
48
49         jsr     CLRCH
50
51 ; Switch to second charset
52
53         lda     #14
54         jsr     BSOUT
55
56 ; Clear the BSS data
57
58         jsr     zerobss
59
60 ; Save system stuff and setup the stack
61
62         tsx
63         stx     spsave          ; save system stk ptr
64
65         sec
66         jsr     MEMTOP          ; Get top memory
67         cpy     #$80            ; We can only use the low 32K :-(
68         bcc     MemOk
69         ldy     #$80
70         ldx     #$00
71 MemOk:  stx     sp
72         sty     sp+1            ; set argument stack ptr
73
74 ; Call module constructors
75
76         jsr     initlib
77
78 ; Pass an empty command line
79
80         jsr     push0           ; argc
81         jsr     push0           ; argv
82
83         ldy     #4              ; Argument size
84         jsr     _main           ; call the users code
85
86 ; Call module destructors. This is also the _exit entry.
87
88 _exit:  jsr     donelib         ; Run module destructors
89
90 ; Restore system stuff
91
92         ldx     spsave
93         txs
94
95 ; Copy back the zero page stuff
96
97         ldx     #zpspace-1
98 L2:     lda     zpsave,x
99         sta     sp,x
100         dex
101         bpl     L2
102
103 ; Reset changed vectors
104
105         jmp     RESTOR
106
107
108 .data
109 zpsave: .res    zpspace
110
111 .bss
112 spsave: .res    1
113
114