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