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