]> git.sur5r.net Git - cc65/blob - libsrc/pet/crt0.s
* Moved the BASIC stub that calls the compiled program into it's own segment
[cc65] / libsrc / pet / crt0.s
1 ;
2 ; Startup code for cc65 (PET 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         CLRCH, BSOUT
11         .import         __INTERRUPTOR_COUNT__
12
13         .include        "zeropage.inc"
14         .include        "pet.inc"
15         .include        "../cbm/cbm.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. The routine that is called by BSOUT to switch the
53 ; character set will use FNLEN as temporary storage - YUCK! Since the
54 ; initmainargs routine, which parses the command line for arguments needs this
55 ; information, we need to save and restore it here.
56 ; Thanks to Stefan Haubenthal for this information!
57
58         lda     FNLEN
59         pha                     ; Save FNLEN
60         lda     #14
61 ;       sta     $E84C           ; See PET FAQ
62         jsr     BSOUT
63         pla
64         sta     FNLEN           ; Restore FNLEN
65
66 ; Clear the BSS data
67
68         jsr     zerobss
69
70 ; Save system stuff and setup the stack
71
72         tsx
73         stx     spsave          ; Save the system stack ptr
74
75         lda     MEMSIZE
76         sta     sp
77         lda     MEMSIZE+1
78         sta     sp+1            ; Set argument stack ptr
79
80 ; If we have IRQ functions, chain our stub into the IRQ vector
81
82         lda     #<__INTERRUPTOR_COUNT__
83         beq     NoIRQ1
84         lda     IRQVec
85         ldx     IRQVec+1
86         sta     IRQInd+1
87         stx     IRQInd+2
88         lda     #<IRQStub
89         ldx     #>IRQStub
90         sei
91         sta     IRQVec
92         stx     IRQVec+1
93         cli
94
95 ; Call module constructors
96
97 NoIRQ1: jsr     initlib
98
99 ; Push arguments and call main()
100
101         jsr     callmain
102
103 ; Call module destructors. This is also the _exit entry.
104
105 _exit:  pha                     ; Save the return code on stack
106         jsr     donelib
107
108 ; Reset the IRQ vector if we chained it.
109
110         lda     #<__INTERRUPTOR_COUNT__
111         beq     NoIRQ2
112         lda     IRQInd+1
113         ldx     IRQInd+2
114         sei
115         sta     IRQVec
116         stx     IRQVec+1
117         cli
118
119 ; Copy back the zero page stuff
120
121 NoIRQ2: ldx     #zpspace-1
122 L2:     lda     zpsave,x
123         sta     sp,x
124         dex
125         bpl     L2
126
127 ; Store the program return code into ST
128
129         pla
130         sta     ST
131
132 ; Restore the stack pointer
133
134         ldx     spsave
135         txs                     ; Restore stack pointer
136
137 ; Back to basic
138
139         rts
140
141 ; ------------------------------------------------------------------------
142 ; The IRQ vector jumps here, if condes routines are defined with type 2.
143
144 IRQStub:
145         cld                             ; Just to be sure
146         jsr     callirq                 ; Call the functions
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 mmusave:.res    1
164