]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Minor comment adjustment.
[cc65] / libsrc / c16 / crt0.s
1 ;
2 ; Startup code for cc65 (C16 version)
3 ;
4 ; Note: The C16 is actually the Plus/4 with just 16KB of memory. So many
5 ; things are similar here, and we even use the plus4.inc include file.
6 ;
7
8         .export         _exit
9         .export         __STARTUP__ : absolute = 1      ; Mark as startup
10         .import         initlib, donelib, callirq
11         .import         callmain, zerobss
12         .import         MEMTOP, RESTOR, BSOUT, CLRCH
13         .import         __INTERRUPTOR_COUNT__
14
15         .include        "zeropage.inc"
16         .include        "plus4.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; BASIC header with a SYS call
21
22 .segment        "EXEHDR"
23
24         .word   Head            ; Load address
25 Head:   .word   @Next
26         .word   .version        ; Line number
27         .byte   $9E             ; SYS token
28         .byte   <(((Start / 1000) .mod 10) + '0')
29         .byte   <(((Start /  100) .mod 10) + '0')
30         .byte   <(((Start /   10) .mod 10) + '0')
31         .byte   <(((Start /    1) .mod 10) + '0')
32         .byte   $00             ; End of BASIC line
33 @Next:  .word   0               ; BASIC end marker
34
35 ; ------------------------------------------------------------------------
36 ; Startup code
37
38 .segment        "STARTUP"
39
40 Start:
41
42 ; Save the zero page locations we need
43
44         ldx     #zpspace-1
45 L1:     lda     sp,x
46         sta     zpsave,x
47         dex
48         bpl     L1
49
50 ; Switch to second charset
51
52         lda     #14
53         jsr     BSOUT
54
55 ; Clear the BSS data
56
57         jsr     zerobss
58
59 ; Save system stuff and setup the stack
60
61         tsx
62         stx     spsave          ; save system stk ptr
63
64         sec
65         jsr     MEMTOP          ; Get top memory
66         cpy     #$80            ; We can only use the low 32K :-(
67         bcc     MemOk
68         ldy     #$80
69         ldx     #$00
70 MemOk:  stx     sp
71         sty     sp+1            ; set argument stack ptr
72
73 ; If we have IRQ functions, chain our stub into the IRQ vector
74
75         lda     #<__INTERRUPTOR_COUNT__
76         beq     NoIRQ1
77         lda     IRQVec
78         ldx     IRQVec+1
79         sta     IRQInd+1
80         stx     IRQInd+2
81         lda     #<IRQStub
82         ldx     #>IRQStub
83         sei
84         sta     IRQVec
85         stx     IRQVec+1
86         cli
87
88 ; Call module constructors
89
90 NoIRQ1: jsr     initlib
91
92 ; Push arguments and call main()
93
94         jsr     callmain
95
96 ; Call module destructors. This is also the _exit entry.
97
98 _exit:  pha                     ; Save the return code on stack
99         jsr     donelib         ; Run module destructors
100
101 ; Reset the IRQ vector if we chained it.
102
103         pha                     ; Save the return code on stack
104         lda     #<__INTERRUPTOR_COUNT__
105         beq     NoIRQ2
106         lda     IRQInd+1
107         ldx     IRQInd+2
108         sei
109         sta     IRQVec
110         stx     IRQVec+1
111         cli
112
113 ; Copy back the zero page stuff
114
115 NoIRQ2: ldx     #zpspace-1
116 L2:     lda     zpsave,x
117         sta     sp,x
118         dex
119         bpl     L2
120
121 ; Store the return code into ST
122
123         pla
124         sta     ST
125
126 ; Restore the stack pointer
127
128         ldx     spsave
129         txs
130
131 ; Reset changed vectors
132
133         jmp     RESTOR
134
135 ; ------------------------------------------------------------------------
136 ; The IRQ vector jumps here, if condes routines are defined with type 2.
137
138 IRQStub:
139         cld                             ; Just to be sure
140         jsr     callirq                 ; Call the functions
141         jmp     IRQInd                  ; Jump to the saved IRQ vector
142
143 ; ------------------------------------------------------------------------
144 ; Data
145
146 .data
147
148 IRQInd: jmp     $0000
149
150 .segment        "ZPSAVE"
151
152 zpsave: .res    zpspace
153
154 .bss
155
156 spsave: .res    1
157
158