]> git.sur5r.net Git - cc65/blob - libsrc/c16/crt0.s
Added dummy classification macros for the remaining targets - even for those that...
[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 ; Startup code
21
22 .segment        "STARTUP"
23
24 Start:
25
26 ; Save the zero page locations 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 second charset
35
36         lda     #14
37         jsr     BSOUT
38
39 ; Clear the BSS data
40
41         jsr     zerobss
42
43 ; Save system stuff and setup the stack
44
45         tsx
46         stx     spsave          ; save system stk ptr
47
48         sec
49         jsr     MEMTOP          ; Get top memory
50         cpy     #$80            ; We can only use the low 32K :-(
51         bcc     MemOk
52         ldy     #$80
53         ldx     #$00
54 MemOk:  stx     sp
55         sty     sp+1            ; set argument stack ptr
56
57 ; If we have IRQ functions, chain our stub into the IRQ vector
58
59         lda     #<__INTERRUPTOR_COUNT__
60         beq     NoIRQ1
61         lda     IRQVec
62         ldx     IRQVec+1
63         sta     IRQInd+1
64         stx     IRQInd+2
65         lda     #<IRQStub
66         ldx     #>IRQStub
67         sei
68         sta     IRQVec
69         stx     IRQVec+1
70         cli
71
72 ; Call module constructors
73
74 NoIRQ1: jsr     initlib
75
76 ; Push arguments and call main()
77
78         jsr     callmain
79
80 ; Call module destructors. This is also the _exit entry.
81
82 _exit:  pha                     ; Save the return code on stack
83         jsr     donelib         ; Run module destructors
84
85 ; Reset the IRQ vector if we chained it.
86
87         pha                     ; Save the return code on stack
88         lda     #<__INTERRUPTOR_COUNT__
89         beq     NoIRQ2
90         lda     IRQInd+1
91         ldx     IRQInd+2
92         sei
93         sta     IRQVec
94         stx     IRQVec+1
95         cli
96
97 ; Copy back the zero page stuff
98
99 NoIRQ2: ldx     #zpspace-1
100 L2:     lda     zpsave,x
101         sta     sp,x
102         dex
103         bpl     L2
104
105 ; Store the return code into ST
106
107         pla
108         sta     ST
109
110 ; Restore the stack pointer
111
112         ldx     spsave
113         txs
114
115 ; Back to BASIC
116
117         rts
118
119 ; ------------------------------------------------------------------------
120 ; The IRQ vector jumps here, if condes routines are defined with type 2.
121
122 IRQStub:
123         cld                             ; Just to be sure
124         jsr     callirq                 ; Call the functions
125         jmp     IRQInd                  ; Jump to the saved IRQ vector
126
127 ; ------------------------------------------------------------------------
128 ; Data
129
130 .data
131
132 IRQInd: jmp     $0000
133
134 .segment        "ZPSAVE"
135
136 zpsave: .res    zpspace
137
138 .bss
139
140 spsave: .res    1
141
142