]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
Added o65 symbol export capability
[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        "c64.inc"
14         .include        "../cbm/cbm.inc"
15
16 ; ------------------------------------------------------------------------
17 ; Define and export the ZP variables for the C64 runtime
18
19         .exportzp       sp, sreg, regsave
20         .exportzp       ptr1, ptr2, ptr3, ptr4
21         .exportzp       tmp1, tmp2, tmp3, tmp4
22         .exportzp       regbank, zpspace
23
24 .zeropage
25
26 zpstart = *
27 sp:             .res    2       ; Stack pointer
28 sreg:           .res    2       ; Secondary register/high 16 bit for longs
29 regsave:        .res    2       ; slot to save/restore (E)AX into
30 ptr1:           .res    2
31 ptr2:           .res    2
32 ptr3:           .res    2
33 ptr4:           .res    2
34 tmp1:           .res    1
35 tmp2:           .res    1
36 tmp3:           .res    1
37 tmp4:           .res    1
38 regbank:        .res    6       ; 6 byte register bank
39
40 zpspace = * - zpstart           ; Zero page space allocated
41
42 .code
43
44 ; ------------------------------------------------------------------------
45 ; BASIC header with a SYS call
46
47         .org    $7FF
48         .word   Head            ; Load address
49 Head:   .word   @Next
50         .word   1000            ; Line number
51         .byte   $9E,"2061"      ; SYS 2061
52         .byte   $00             ; End of BASIC line
53 @Next:  .word   0               ; BASIC end marker
54         .reloc
55
56 ; ------------------------------------------------------------------------
57 ; Actual code
58
59         ldx     #zpspace-1
60 L1:     lda     sp,x
61         sta     zpsave,x        ; Save the zero page locations we need
62         dex
63         bpl     L1
64
65 ; Close open files
66
67         jsr     CLRCH
68
69 ; Switch to second charset
70
71         lda     #14
72         jsr     BSOUT
73
74 ; Clear the BSS data
75
76         jsr     zerobss
77
78 ; Save system stuff and setup the stack
79
80         tsx
81         stx     spsave          ; Save the system stack ptr
82
83         lda     $01
84         sta     mmusave         ; Save the memory configuration
85         and     #$F8
86         ora     #$06            ; Enable kernal+I/O, disable basic
87         sta     $01
88
89         lda     #<(__RAM_START__ + __RAM_SIZE__)
90         sta     sp
91         lda     #>(__RAM_START__ + __RAM_SIZE__)
92         sta     sp+1            ; Set argument stack ptr
93
94 ; Call module constructors
95
96         jsr     initlib
97
98 ; Pass an empty command line
99
100         jsr     push0           ; argc
101         jsr     push0           ; argv
102
103         ldy     #4              ; Argument size
104         jsr     _main           ; call the users code
105
106 ; Call module destructors. This is also the _exit entry.
107
108 _exit:  jsr     donelib         ; Run module destructors
109
110 ; Restore system stuff
111
112         ldx     spsave
113         txs                     ; Restore stack pointer
114         lda     mmusave
115         sta     $01             ; Restore memory configuration
116
117 ; Copy back the zero page stuff
118
119         ldx     #zpspace-1
120 L2:     lda     zpsave,x
121         sta     sp,x
122         dex
123         bpl     L2
124
125 ; Reset changed vectors, back to basic
126
127         jmp     RESTOR
128
129
130 .data
131
132 zpsave: .res    zpspace
133
134 .bss
135
136 spsave: .res    1
137 mmusave:.res    1