]> git.sur5r.net Git - cc65/blob - libsrc/vic20/crt0.s
Moved the data that keeps a copy of the used zero page locations in its own
[cc65] / libsrc / vic20 / crt0.s
1 ;
2 ; Startup code for cc65 (Vic20 version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .import         initlib, donelib, callirq
9         .import         zerobss, push0
10         .import         callmain
11         .import         RESTOR, BSOUT, CLRCH
12         .import         __INTERRUPTOR_COUNT__
13         .import         __RAM_START__, __RAM_SIZE__     ; Linker generated
14
15         .include        "zeropage.inc"
16         .include        "vic20.inc"
17
18 ; ------------------------------------------------------------------------
19 ; Place the startup code in a special segment.
20
21 .segment        "STARTUP"
22
23 ; BASIC header with a SYS call
24
25         .word   Head            ; Load address
26 Head:   .word   @Next
27         .word   .version        ; Line number
28         .byte   $9E             ; SYS token
29         .byte   <(((@Start / 1000) .mod 10) + $30)
30         .byte   <(((@Start /  100) .mod 10) + $30)
31         .byte   <(((@Start /   10) .mod 10) + $30)
32         .byte   <(((@Start /    1) .mod 10) + $30)
33         .byte   $00             ; End of BASIC line
34 @Next:  .word   0               ; BASIC end marker
35 @Start:
36
37 ; ------------------------------------------------------------------------
38 ; Actual code
39
40         ldx     #zpspace-1
41 L1:     lda     sp,x
42         sta     zpsave,x        ; Save the zero page locations we need
43         dex
44         bpl     L1
45
46 ; Close open files
47
48         jsr     CLRCH
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 the system stack ptr
63
64         lda     #<(__RAM_START__ + __RAM_SIZE__)
65         sta     sp
66         lda     #>(__RAM_START__ + __RAM_SIZE__)
67         sta     sp+1            ; Set argument stack ptr
68
69 ; Call module constructors
70
71         jsr     initlib
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 ; Push arguments and call main()
89
90 NoIRQ1: jsr     callmain
91
92 ; Back from main (This is also the _exit entry). Reset the IRQ vector if we
93 ; chained it.
94
95 _exit:  pha                     ; Save the return code on stack
96         lda     #<__INTERRUPTOR_COUNT__
97         beq     NoIRQ2
98         lda     IRQInd+1
99         ldx     IRQInd+2
100         sei
101         sta     IRQVec
102         stx     IRQVec+1
103         cli
104
105 ; Run module destructors
106
107 NoIRQ2: jsr     donelib
108
109 ; Copy back the zero page stuff
110
111         ldx     #zpspace-1
112 L2:     lda     zpsave,x
113         sta     sp,x
114         dex
115         bpl     L2
116
117 ; Place the program return code into ST
118
119         pla
120         sta     ST
121
122 ; Restore the stack pointer
123
124         ldx     spsave
125         txs
126
127 ; Reset changed vectors, back to basic
128
129         jmp     RESTOR
130
131
132 ; ------------------------------------------------------------------------
133 ; The IRQ vector jumps here, if condes routines are defined with type 2.
134
135 IRQStub:
136         cld                             ; Just to be sure
137         jsr     callirq                 ; Call the functions
138         jmp     IRQInd                  ; Jump to the saved IRQ vector
139
140 ; ------------------------------------------------------------------------
141 ; Data
142
143 .data
144
145 IRQInd: jmp     $0000
146
147 .segment        "ZPSAVE"
148
149 zpsave: .res    zpspace
150
151 .bss
152
153 spsave: .res    1