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