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