]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
5a69f158488f9a3ff12494da4622dcfc8bed9d94
[cc65] / libsrc / c64 / crt0.s
1 ;
2 ; Startup code for cc65 (C64 version)
3 ;
4
5         .export         _exit
6         .exportzp       init_load_, init_run_
7         .export         __STARTUP__ : absolute = 1      ; Mark as startup
8
9         .import         initlib, donelib
10         .import         move_init, zerobss, callmain
11         .import         RESTOR, BSOUT, CLRCH
12         .import         __HIMEM__                       ; from configure file
13         .importzp       ST
14
15         .include        "zeropage.inc"
16         .include        "c64.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; Startup code
21
22 ; Two zero-page pointers are needed before any zero-page stuff is saved.
23 ; Choose locations that are not used by anything.
24
25 init_load_      :=      FREKZP
26 init_run_       :=      FREKZP+2
27
28
29 .segment        "STARTUP"
30
31 Start:
32
33 ; Switch to the second charset.
34
35         lda     #14
36         jsr     BSOUT
37
38 ; Switch off the BASIC ROM.
39
40         lda     $01
41         sta     mmusave         ; Save the memory configuration
42         and     #$F8
43         ora     #$06            ; Enable Kernal+I/O, disable BASIC
44         sta     $01
45
46         tsx
47         stx     spsave          ; Save the system stack ptr
48
49 ; Allow some re-entrancy by skipping the next task if it already was done.
50 ; This often can let us rerun the program without reloading it.
51
52         ldx     moveinit
53         beq     L0
54
55 ; Move the INIT segment from where it was loaded (over ZPSAVE and BSS)
56 ; into where it must be run (in the heap).
57
58         jsr     move_init
59         dec     moveinit        ; set to false
60
61 ; Save space by putting the rest of the start-up code in the INIT segment,
62 ; which can be re-used by the heap.
63
64 L0:     jsr     initstart
65
66 ; Back from main() [this is also the exit() entry]. Run the module destructors.
67
68 _exit:  pha                     ; Save the return code on stack
69         jsr     donelib
70
71 ; Copy back the zero-page stuff.
72
73         ldx     #zpspace-1
74 L2:     lda     zpsave,x
75         sta     sp,x
76         dex
77         bpl     L2
78
79 ; Place the program return code into BASIC's status variable.
80
81         pla
82         sta     ST
83
84 ; Restore the system stuff.
85
86         ldx     spsave
87         txs                     ; Restore stack pointer
88         ldx     mmusave
89         stx     $01             ; Restore memory configuration
90
91 ; Back to BASIC.
92
93         rts
94
95
96 ; ------------------------------------------------------------------------
97
98 .segment        "INIT"
99
100 initstart:
101
102 ; Save the zero-page locations that we need.
103
104         ldx     #zpspace-1
105 L1:     lda     sp,x
106         sta     zpsave,x
107         dex
108         bpl     L1
109
110 ; Clear the BSS data.
111
112         jsr     zerobss
113
114 ; Set up the stack.
115
116         lda     #<__HIMEM__
117         ldx     #>__HIMEM__
118         sta     sp
119         stx     sp+1            ; Set argument stack ptr
120
121 ; Call the module constructors.
122
123         jsr     initlib
124
125 ; Push the command-line arguments; and, call main().
126
127         jmp     callmain
128
129
130 ; ------------------------------------------------------------------------
131 ; Data
132
133 .data
134
135 mmusave:.res    1
136 spsave: .res    1
137 moveinit:
138         .byte   1
139
140 .segment        "ZPSAVE"
141
142 zpsave: .res    zpspace