]> git.sur5r.net Git - cc65/blob - libsrc/pet/crt0.s
remove superfluous ".code" line
[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 ; 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,"1037"      ; SYS 1037
28         .byte   $00             ; End of BASIC line
29 @Next:  .word   0               ; BASIC end marker
30
31 ; ------------------------------------------------------------------------
32 ; Actual code
33
34         ldx     #zpspace-1
35 L1:     lda     sp,x
36         sta     zpsave,x        ; Save the zero page locations we need
37         dex
38         bpl     L1
39
40 ; Close open files
41
42         jsr     CLRCH
43
44 ; Switch to second charset. The routine that is called by BSOUT to switch the
45 ; character set will use FNLEN as temporary storage - YUCK! Since the
46 ; initmainargs routine, which parses the command line for arguments needs this
47 ; information, we need to save and restore it here.
48 ; Thanks to Stefan Haubenthal for this information!
49
50         lda     FNLEN
51         pha                     ; Save FNLEN
52         lda     #14
53 ;       sta     $E84C           ; See PET FAQ
54         jsr     BSOUT
55         pla
56         sta     FNLEN           ; Restore FNLEN
57
58 ; Clear the BSS data
59
60         jsr     zerobss
61
62 ; Save system stuff and setup the stack
63
64         tsx
65         stx     spsave          ; Save the system stack ptr
66
67         lda     MEMSIZE
68         sta     sp
69         lda     MEMSIZE+1
70         sta     sp+1            ; Set argument stack ptr
71
72 ; If we have IRQ functions, chain our stub into the IRQ vector
73
74         lda     #<__INTERRUPTOR_COUNT__
75         beq     NoIRQ1
76         lda     IRQVec
77         ldx     IRQVec+1
78         sta     IRQInd+1
79         stx     IRQInd+2
80         lda     #<IRQStub
81         ldx     #>IRQStub
82         sei
83         sta     IRQVec
84         stx     IRQVec+1
85         cli
86
87 ; Call module constructors
88
89 NoIRQ1: jsr     initlib
90
91 ; Push arguments and call main()
92
93         jsr     callmain
94
95 ; Call module destructors. This is also the _exit entry.
96
97 _exit:  pha                     ; Save the return code on stack
98         jsr     donelib
99
100 ; Reset the IRQ vector if we chained it.
101
102         lda     #<__INTERRUPTOR_COUNT__
103         beq     NoIRQ2
104         lda     IRQInd+1
105         ldx     IRQInd+2
106         sei
107         sta     IRQVec
108         stx     IRQVec+1
109         cli
110
111 ; Copy back the zero page stuff
112
113 NoIRQ2: ldx     #zpspace-1
114 L2:     lda     zpsave,x
115         sta     sp,x
116         dex
117         bpl     L2
118
119 ; Store the program return code into ST
120
121         pla
122         sta     ST
123
124 ; Restore the stack pointer
125
126         ldx     spsave
127         txs                     ; Restore stack pointer
128
129 ; Back to basic
130
131         rts
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
155 mmusave:.res    1
156