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