]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Moved the data that keeps a copy of the used zero page locations in its own
[cc65] / libsrc / c16 / crt0.s
1 ;
2 ; Startup code for cc65 (C16 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6 ; Note: The C16 is actually the Plus/4 with just 16KB of memory. So many
7 ; things are similar here, and we even use the plus4.inc include file.
8 ;
9
10         .export         _exit
11         .import         initlib, donelib
12         .import         push0, callmain, zerobss
13         .import         MEMTOP, RESTOR, BSOUT, CLRCH
14
15         .include        "zeropage.inc"
16         .include        "../plus4/plus4.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; Place the startup code in a special segment.
21
22 .segment        "STARTUP"
23
24 ; BASIC header with a SYS call
25
26         .word   Head            ; Load address
27 Head:   .word   @Next
28         .word   .version        ; Line number
29         .byte   $9E,"4109"      ; SYS 4109
30         .byte   $00             ; End of BASIC line
31 @Next:  .word   0               ; BASIC end marker
32
33 ; ------------------------------------------------------------------------
34 ; Actual code
35
36         ldx     #zpspace-1
37 L1:     lda     sp,x
38         sta     zpsave,x        ; save the zero page locations we need
39         dex
40         bpl     L1
41
42 ; Close open files
43
44         jsr     CLRCH
45
46 ; Switch to second charset
47
48         lda     #14
49         jsr     BSOUT
50
51 ; Clear the BSS data
52
53         jsr     zerobss
54
55 ; Save system stuff and setup the stack
56
57         tsx
58         stx     spsave          ; save system stk ptr
59
60         sec
61         jsr     MEMTOP          ; Get top memory
62         cpy     #$80            ; We can only use the low 32K :-(
63         bcc     MemOk
64         ldy     #$80
65         ldx     #$00
66 MemOk:  stx     sp
67         sty     sp+1            ; set argument stack ptr
68
69 ; Call module constructors
70
71         jsr     initlib
72
73 ; Push arguments and call main()
74
75         jsr     callmain
76
77 ; Call module destructors. This is also the _exit entry.
78
79 _exit:  pha                     ; Save the return code on stack
80         jsr     donelib         ; Run module destructors
81
82 ; Copy back the zero page stuff
83
84         ldx     #zpspace-1
85 L2:     lda     zpsave,x
86         sta     sp,x
87         dex
88         bpl     L2
89
90 ; Store the return code into ST
91
92         pla
93         sta     ST
94
95 ; Restore the stack pointer
96
97         ldx     spsave
98         txs
99
100 ; Reset changed vectors
101
102         jmp     RESTOR
103
104
105 .segment        "ZPSAVE"
106
107 zpsave: .res    zpspace
108
109 .bss
110
111 spsave: .res    1
112
113