]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
atari5200.sgml: small fix for last change
[cc65] / libsrc / c64 / crt0.s
1 ;
2 ; Startup code for cc65 (C64 version)
3 ;
4
5         .export         _exit
6         .export         __STARTUP__ : absolute = 1      ; Mark as startup
7
8         .import         initlib, donelib
9         .import         zerobss, callmain
10         .import         BSOUT
11         .import         __MAIN_START__, __MAIN_SIZE__   ; Linker generated
12         .import         __STACKSIZE__                   ; from configure file
13         .importzp       ST
14
15         .include        "zeropage.inc"
16         .include        "c64.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; Startup code
21
22 .segment        "STARTUP"
23
24 Start:
25
26 ; Switch off the BASIC ROM.
27
28         lda     $01
29         sta     mmusave         ; Save the memory configuration
30         and     #$F8
31         ora     #$06            ; Enable Kernal+I/O, disable BASIC
32         sta     $01
33
34         tsx
35         stx     spsave          ; Save the system stack ptr
36
37 ; Save space by putting some of the start-up code in the ONCE segment,
38 ; which can be re-used by the BSS segment, the heap and the C stack.
39
40         jsr     init
41
42 ; Clear the BSS data.
43
44         jsr     zerobss
45
46 ; Push the command-line arguments; and, call main().
47
48         jsr     callmain
49
50 ; Back from main() [this is also the exit() entry]. Run the module destructors.
51
52 _exit:  pha                     ; Save the return code on stack
53         jsr     donelib
54
55 ; Copy back the zero-page stuff.
56
57         ldx     #zpspace-1
58 L2:     lda     zpsave,x
59         sta     sp,x
60         dex
61         bpl     L2
62
63 ; Place the program return code into BASIC's status variable.
64
65         pla
66         sta     ST
67
68 ; Restore the system stuff.
69
70         ldx     spsave
71         txs                     ; Restore stack pointer
72         ldx     mmusave
73         stx     $01             ; Restore memory configuration
74
75 ; Back to BASIC.
76
77         rts
78
79
80 ; ------------------------------------------------------------------------
81
82 .segment        "ONCE"
83
84 init:
85
86 ; Save the zero-page locations that we need.
87
88         ldx     #zpspace-1
89 L1:     lda     sp,x
90         sta     zpsave,x
91         dex
92         bpl     L1
93
94 ; Set up the stack.
95
96         lda     #<(__MAIN_START__ + __MAIN_SIZE__)
97         ldx     #>(__MAIN_START__ + __MAIN_SIZE__)
98         sta     sp
99         stx     sp+1            ; Set argument stack ptr
100
101 ; Switch to the second charset.
102
103         lda     #14
104         jsr     BSOUT
105
106 ; Call the module constructors.
107
108         jmp     initlib
109
110
111 ; ------------------------------------------------------------------------
112 ; Data
113
114 .segment        "INIT"
115
116 mmusave:.res    1
117 spsave: .res    1
118 zpsave: .res    zpspace