]> git.sur5r.net Git - cc65/blob - crt0.s
4c441d513b288fe3766d7b48c528c6d272063f61
[cc65] / crt0.s
1 ;
2 ; Startup code for cc65 (C64 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .import         initlib, donelib
9         .import         zerobss, push0
10         .import         _main
11         .import         RESTOR, BSOUT, CLRCH
12         .import         __RAM_START__, __RAM_SIZE__     ; Linker generated
13
14         .include        "zeropage.inc"
15         .include        "c64.inc"
16
17
18 ; ------------------------------------------------------------------------
19 ; Create an empty LOWCODE segment to avoid linker warnings
20
21 .segment        "LOWCODE"
22
23 ; ------------------------------------------------------------------------
24 ; Place the startup code in a special segment.
25
26 .segment        "STARTUP"
27
28 ; BASIC header with a SYS call
29
30         .word   Head            ; Load address
31 Head:   .word   @Next
32         .word   1000            ; Line number
33         .byte   $9E,"2061"      ; SYS 2061
34         .byte   $00             ; End of BASIC line
35 @Next:  .word   0               ; BASIC end marker
36
37 ; ------------------------------------------------------------------------
38 ; Actual code
39
40         ldx     #zpspace-1
41 L1:     lda     sp,x
42         sta     zpsave,x        ; Save the zero page locations we need
43         dex
44         bpl     L1
45
46 ; Close open files
47
48         jsr     CLRCH
49
50 ; Switch to second charset
51
52         lda     #14
53         jsr     BSOUT
54
55 ; Switch off the BASIC ROM
56
57         lda     $01
58         pha                     ; Remember the value
59         and     #$F8
60         ora     #$06            ; Enable kernal+I/O, disable basic
61         sta     $01
62
63 ; Clear the BSS data
64
65         jsr     zerobss
66
67 ; Save system settings and setup the stack
68
69         pla
70         sta     mmusave         ; Save the memory configuration
71
72         tsx
73         stx     spsave          ; Save the system stack ptr
74
75         lda     #<(__RAM_START__ + __RAM_SIZE__)
76         sta     sp
77         lda     #>(__RAM_START__ + __RAM_SIZE__)
78         sta     sp+1            ; Set argument stack ptr
79
80 ; Call module constructors
81
82         jsr     initlib
83
84 ; Pass an empty command line
85
86         jsr     push0           ; argc
87         jsr     push0           ; argv
88
89         ldy     #4              ; Argument size
90         jsr     _main           ; call the users code
91
92 ; Call module destructors. This is also the _exit entry.
93
94 _exit:  jsr     donelib         ; Run module destructors
95
96 ; Restore system stuff
97
98         ldx     spsave
99         txs                     ; Restore stack pointer
100         lda     mmusave
101         sta     $01             ; Restore memory configuration
102
103 ; Copy back the zero page stuff
104
105         ldx     #zpspace-1
106 L2:     lda     zpsave,x
107         sta     sp,x
108         dex
109         bpl     L2
110
111 ; Reset changed vectors, back to basic
112
113         jmp     RESTOR
114
115
116 ; ------------------------------------------------------------------------
117 ; Data
118
119 .data
120
121 zpsave: .res    zpspace
122
123 .bss
124
125 spsave: .res    1
126 mmusave:.res    1