]> git.sur5r.net Git - cc65/blob - libsrc/vic20/crt0.s
Use "override" when appending to CFLAGS, so this works even when CFLAGS is
[cc65] / libsrc / vic20 / crt0.s
1 ;
2 ; Startup code for cc65 (Vic20 version)
3 ;
4
5         .export         _exit
6         .export         __STARTUP__ : absolute = 1      ; Mark as startup
7         .import         initlib, donelib, callirq
8         .import         zerobss, push0
9         .import         callmain
10         .import         RESTOR, BSOUT, CLRCH
11         .import         __INTERRUPTOR_COUNT__
12         .import         __RAM_START__, __RAM_SIZE__     ; Linker generated
13
14         .include        "zeropage.inc"
15         .include        "vic20.inc"
16
17 ; ------------------------------------------------------------------------
18 ; Place the startup code in a special segment.
19
20 .segment        "STARTUP"
21
22 ; BASIC header with a SYS call
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) + $30)
29         .byte   <(((@Start /  100) .mod 10) + $30)
30         .byte   <(((@Start /   10) .mod 10) + $30)
31         .byte   <(((@Start /    1) .mod 10) + $30)
32         .byte   $00             ; End of BASIC line
33 @Next:  .word   0               ; BASIC end marker
34 @Start:
35
36 ; ------------------------------------------------------------------------
37 ; Actual code
38
39         ldx     #zpspace-1
40 L1:     lda     sp,x
41         sta     zpsave,x        ; Save the zero page locations we need
42         dex
43         bpl     L1
44
45 ; Close open files
46
47         jsr     CLRCH
48
49 ; Switch to second charset
50
51         lda     #14
52         jsr     BSOUT
53
54 ; Clear the BSS data
55
56         jsr     zerobss
57
58 ; Save system stuff and setup the stack
59
60         tsx
61         stx     spsave          ; Save the system stack ptr
62
63         lda     #<(__RAM_START__ + __RAM_SIZE__)
64         sta     sp
65         lda     #>(__RAM_START__ + __RAM_SIZE__)
66         sta     sp+1            ; Set argument stack ptr
67
68 ; If we have IRQ functions, chain our stub into the IRQ vector
69
70         lda     #<__INTERRUPTOR_COUNT__
71         beq     NoIRQ1
72         lda     IRQVec
73         ldx     IRQVec+1
74         sta     IRQInd+1
75         stx     IRQInd+2
76         lda     #<IRQStub
77         ldx     #>IRQStub
78         sei
79         sta     IRQVec
80         stx     IRQVec+1
81         cli
82
83 ; Call module constructors
84
85 NoIRQ1: jsr     initlib
86
87 ; Push arguments and call main()
88
89         jsr     callmain
90
91 ; Back from main (This is also the _exit entry). Run module destructors
92
93 _exit:  jsr     donelib
94
95 ; Reset the IRQ vector if we chained it.
96
97         pha                     ; Save the return code on stack
98         lda     #<__INTERRUPTOR_COUNT__
99         beq     NoIRQ2
100         lda     IRQInd+1
101         ldx     IRQInd+2
102         sei
103         sta     IRQVec
104         stx     IRQVec+1
105         cli
106
107 ; Copy back the zero page stuff
108
109 NoIRQ2: ldx     #zpspace-1
110 L2:     lda     zpsave,x
111         sta     sp,x
112         dex
113         bpl     L2
114
115 ; Place the program return code into ST
116
117         pla
118         sta     ST
119
120 ; Restore the stack pointer
121
122         ldx     spsave
123         txs
124
125 ; Reset changed vectors, back to basic
126
127         jmp     RESTOR
128
129
130 ; ------------------------------------------------------------------------
131 ; The IRQ vector jumps here, if condes routines are defined with type 2.
132
133 IRQStub:
134         cld                             ; Just to be sure
135         jsr     callirq                 ; Call the functions
136         jmp     IRQInd                  ; Jump to the saved IRQ vector
137
138 ; ------------------------------------------------------------------------
139 ; Data
140
141 .data
142
143 IRQInd: jmp     $0000
144
145 .segment        "ZPSAVE"
146
147 zpsave: .res    zpspace
148
149 .bss
150
151 spsave: .res    1