]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
f75a5c46b1e11bb8337b9d6b01ae964c762a0f95
[cc65] / libsrc / c64 / 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         callmain
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 ; Place the startup code in a special segment.
20
21 .segment        "STARTUP"
22
23 ; BASIC header with a SYS call
24
25         .word   Head            ; Load address
26 Head:   .word   @Next
27         .word   .version        ; Line number
28         .byte   $9E,"2061"      ; SYS 2061
29         .byte   $00             ; End of BASIC line
30 @Next:  .word   0               ; BASIC end marker
31
32 ; ------------------------------------------------------------------------
33 ; Actual code
34
35         ldx     #zpspace-1
36 L1:     lda     sp,x
37         sta     zpsave,x        ; Save the zero page locations we need
38         dex
39         bpl     L1
40
41 ; Close open files
42
43         jsr     CLRCH
44
45 ; Switch to second charset
46
47         lda     #14
48         jsr     BSOUT
49
50 ; Switch off the BASIC ROM
51
52         lda     $01
53         pha                     ; Remember the value
54         and     #$F8
55         ora     #$06            ; Enable kernal+I/O, disable basic
56         sta     $01
57
58 ; Clear the BSS data
59
60         jsr     zerobss
61
62 ; Save system settings and setup the stack
63
64         pla
65         sta     mmusave         ; Save the memory configuration
66
67         tsx
68         stx     spsave          ; Save the system stack ptr
69
70         lda     #<(__RAM_START__ + __RAM_SIZE__)
71         sta     sp
72         lda     #>(__RAM_START__ + __RAM_SIZE__)
73         sta     sp+1            ; Set argument stack ptr
74
75 ; Call module constructors
76
77         jsr     initlib
78
79 ; Push arguments and call main
80
81         jsr     callmain
82
83 ; Call module destructors. This is also the _exit entry.
84
85 _exit:  pha                     ; Save the return code
86         jsr     donelib         ; Run module destructors
87
88 ; Copy back the zero page stuff
89
90         ldx     #zpspace-1
91 L2:     lda     zpsave,x
92         sta     sp,x
93         dex
94         bpl     L2
95
96 ; Place the program return code into ST
97
98         pla
99         sta     ST
100
101 ; Restore system stuff
102
103         ldx     spsave
104         txs                     ; Restore stack pointer
105         ldx     mmusave
106         stx     $01             ; Restore memory configuration
107
108 ; Reset changed vectors, back to basic
109
110         jmp     RESTOR
111
112
113 ; ------------------------------------------------------------------------
114 ; Data
115
116 .data
117
118 zpsave: .res    zpspace
119
120 .bss
121
122 spsave: .res    1
123 mmusave:.res    1