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