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