]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Use the assembler version as BASIC line number, so the version of the
[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:  jsr     donelib         ; Run module destructors
80
81 ; Restore system stuff
82
83         ldx     spsave
84         txs
85
86 ; Copy back the zero page stuff
87
88         ldx     #zpspace-1
89 L2:     lda     zpsave,x
90         sta     sp,x
91         dex
92         bpl     L2
93
94 ; Reset changed vectors
95
96         jmp     RESTOR
97
98
99 .data
100 zpsave: .res    zpspace
101
102 .bss
103 spsave: .res    1
104
105