]> git.sur5r.net Git - cc65/blob - libsrc/atmos/crt0.s
Added the telldir() function.
[cc65] / libsrc / atmos / crt0.s
1 ;
2 ; Startup code for cc65 (Oric version)
3 ;
4 ; By Debrune Jérôme <jede@oric.org> and Ullrich von Bassewitz <uz@cc65.org>
5 ;
6
7         .export         _exit
8         .export         __STARTUP__ : absolute = 1      ; Mark as startup
9         .import         initlib, donelib
10         .import         callmain, zerobss, callirq
11         .import         __INTERRUPTOR_COUNT__
12         .import         __RAM_START__, __RAM_SIZE__
13         .import         __ZPSAVE_LOAD__, __STACKSIZE__
14
15         .include        "zeropage.inc"
16         .include        "atmos.inc"
17
18
19 ; ------------------------------------------------------------------------
20 ; Oric tape header
21
22 .segment        "TAPEHDR"
23
24         .byte   $16, $16, $16   ; Sync bytes
25         .byte   $24             ; End of header marker
26
27         .byte   $00                             ; $2B0
28         .byte   $00                             ; $2AF
29         .byte   $80                             ; $2AE Machine code flag
30         .byte   $C7                             ; $2AD Autoload flag
31         .dbyt   __ZPSAVE_LOAD__ - 1             ; $2AB
32         .dbyt   __RAM_START__                   ; $2A9
33         .byte   $00                             ; $2A8
34         .byte   ((.VERSION >> 8) & $0F) + '0'
35         .byte   ((.VERSION >> 4) & $0F) + '0'
36         .byte   (.VERSION & $0F) + '0'
37         .byte   $00                             ; Zero terminated compiler version
38
39 ; ------------------------------------------------------------------------
40 ; Place the startup code in a special segment.
41
42 .segment        "STARTUP"
43
44 ; Save the zero page area we're about to use
45
46         ldx     #zpspace-1
47 L1:     lda     sp,x
48         sta     zpsave,x        ; Save the zero page locations we need
49         dex
50         bpl     L1
51
52 ; Clear the BSS data
53
54         jsr     zerobss
55
56 ; Unprotect columns 0 and 1
57
58         lda     STATUS
59         sta     stsave
60         and     #%11011111
61         sta     STATUS
62
63 ; Save system stuff and setup the stack
64
65         tsx
66         stx     spsave          ; Save system stk ptr
67
68         lda     #<(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
69         sta     sp
70         lda     #>(__RAM_START__ + __RAM_SIZE__ + __STACKSIZE__)
71         sta     sp+1            ; Set argument stack ptr
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 ; Call module constructors
89
90 NoIRQ1: jsr     initlib
91
92 ; Push arguments and call main()
93
94         jsr     callmain
95
96 ; Call module destructors. This is also the _exit entry.
97
98 _exit:  jsr     donelib         ; Run module destructors
99
100 ; Reset the IRQ vector if we chained it.
101
102         pha                     ; Save the return code on stack
103         lda     #<__INTERRUPTOR_COUNT__
104         beq     NoIRQ2
105         lda     IRQInd+1
106         ldx     IRQInd+2
107         sei
108         sta     IRQVec
109         stx     IRQVec+1
110         cli
111
112 ; Restore system stuff
113
114 NoIRQ2: ldx     spsave
115         txs
116         lda     stsave
117         sta     STATUS
118
119 ; Copy back the zero page stuff
120
121         ldx     #zpspace-1
122 L2:     lda     zpsave,x
123         sta     sp,x
124         dex
125         bpl     L2
126
127 ; Back to BASIC
128
129         rts
130
131 ; ------------------------------------------------------------------------
132 ; The IRQ vector jumps here, if condes routines are defined with type 2.
133
134 IRQStub:
135         cld                             ; Just to be sure
136         pha
137         txa
138         pha
139         tya
140         pha
141         jsr     callirq                 ; Call the functions
142         pla
143         tay
144         pla
145         tax
146         pla
147         jmp     IRQInd                  ; Jump to the saved IRQ vector
148
149 ; ------------------------------------------------------------------------
150 ; Data
151
152 .data
153
154 IRQInd: jmp     $0000
155
156 .segment        "ZPSAVE"
157
158 zpsave: .res    zpspace
159
160 .bss
161
162 spsave: .res    1
163 stsave: .res    1