]> git.sur5r.net Git - cc65/blob - libsrc/c64/crt0.s
Refined the comments in the target start-up files.
[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         .import         initlib, donelib
8         .import         zerobss
9         .import         callmain
10         .import         RESTOR, BSOUT, CLRCH
11         .import         __RAM_START__, __RAM_SIZE__     ; Linker generated
12         .import         __STACKSIZE__                   ; Linker generated
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 ; Save the zero-page locations that we need.
27
28         ldx     #zpspace-1
29 L1:     lda     sp,x
30         sta     zpsave,x
31         dex
32         bpl     L1
33
34 ; Switch to the second charset.
35
36         lda     #14
37         jsr     BSOUT
38
39 ; Switch off the BASIC ROM.
40
41         lda     $01
42         pha                     ; Remember the value
43         and     #$F8
44         ora     #$06            ; Enable Kernal+I/O, disable BASIC
45         sta     $01
46
47 ; Clear the BSS data.
48
49         jsr     zerobss
50
51 ; Save some system settings; and, set up the stack.
52
53         pla
54         sta     mmusave         ; Save the memory configuration
55
56         tsx
57         stx     spsave          ; Save the system stack ptr
58
59         lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
60         sta     sp
61         lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
62         sta     sp+1            ; Set argument stack ptr
63
64 ; Call the module constructors.
65
66         jsr     initlib
67
68 ; Push the command-line arguments; and, call main().
69
70         jsr     callmain
71
72 ; Back from main() [this is also the exit() entry]. Run the module destructors.
73
74 _exit:  pha                     ; Save the return code on stack
75         jsr     donelib
76
77 ; Copy back the zero-page stuff.
78
79         ldx     #zpspace-1
80 L2:     lda     zpsave,x
81         sta     sp,x
82         dex
83         bpl     L2
84
85 ; Place the program return code into BASIC's status variable.
86
87         pla
88         sta     ST
89
90 ; Restore the system stuff.
91
92         ldx     spsave
93         txs                     ; Restore stack pointer
94         ldx     mmusave
95         stx     $01             ; Restore memory configuration
96
97 ; Back to BASIC.
98
99         rts
100
101 ; ------------------------------------------------------------------------
102 ; Data
103
104 .segment        "ZPSAVE"
105
106 zpsave: .res    zpspace
107
108 .bss
109
110 spsave: .res    1
111 mmusave:.res    1