]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Added routines to handle command line params
[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 ; Create an empty LOWCODE segment to avoid linker warnings
21
22 .segment        "LOWCODE"
23
24 ; ------------------------------------------------------------------------
25 ; Place the startup code in a special segment.
26
27 .segment        "STARTUP"
28
29 ; BASIC header with a SYS call
30
31         .word   Head            ; Load address
32 Head:   .word   @Next
33         .word   1000            ; Line number
34         .byte   $9E,"4109"      ; SYS 4109
35         .byte   $00             ; End of BASIC line
36 @Next:  .word   0               ; BASIC end marker
37
38 ; ------------------------------------------------------------------------
39 ; Actual code
40
41         ldx     #zpspace-1
42 L1:     lda     sp,x
43         sta     zpsave,x        ; save the zero page locations we need
44         dex
45         bpl     L1
46
47 ; Close open files
48
49         jsr     CLRCH
50
51 ; Switch to second charset
52
53         lda     #14
54         jsr     BSOUT
55
56 ; Clear the BSS data
57
58         jsr     zerobss
59
60 ; Save system stuff and setup the stack
61
62         tsx
63         stx     spsave          ; save system stk ptr
64
65         sec
66         jsr     MEMTOP          ; Get top memory
67         cpy     #$80            ; We can only use the low 32K :-(
68         bcc     MemOk
69         ldy     #$80
70         ldx     #$00
71 MemOk:  stx     sp
72         sty     sp+1            ; set argument stack ptr
73
74 ; Call module constructors
75
76         jsr     initlib
77
78 ; Push arguments and call main()
79
80         jsr     callmain
81
82 ; Call module destructors. This is also the _exit entry.
83
84 _exit:  jsr     donelib         ; Run module destructors
85
86 ; Restore system stuff
87
88         ldx     spsave
89         txs
90
91 ; Copy back the zero page stuff
92
93         ldx     #zpspace-1
94 L2:     lda     zpsave,x
95         sta     sp,x
96         dex
97         bpl     L2
98
99 ; Reset changed vectors
100
101         jmp     RESTOR
102
103
104 .data
105 zpsave: .res    zpspace
106
107 .bss
108 spsave: .res    1
109
110