]> git.sur5r.net Git - cc65/blob - libsrc/pet/crt0.s
initmainargs routine by Stefan Haubenthal.
[cc65] / libsrc / pet / crt0.s
1 ;
2 ; Startup code for cc65 (PET version)
3 ;
4 ; This must be the *first* file on the linker command line
5 ;
6
7         .export         _exit
8         .import         initlib, donelib, callirq
9         .import         zerobss, push0
10         .import         callmain
11         .import         CLRCH, BSOUT
12         .import         __INTERRUPTOR_COUNT__
13
14         .include        "zeropage.inc"
15         .include        "pet.inc"
16         .include        "../cbm/cbm.inc"
17
18 ; ------------------------------------------------------------------------
19 ; Place the startup code in a special segment.
20
21 .segment        "STARTUP"
22
23 ; BASIC header with a SYS call
24
25         .word   Head            ; Load address
26 Head:   .word   @Next
27         .word   .version        ; Line number
28         .byte   $9E,"1037"      ; SYS 1037
29         .byte   $00             ; End of BASIC line
30 @Next:  .word   0               ; BASIC end marker
31
32 ; ------------------------------------------------------------------------
33 ; Actual code
34
35         ldx     #zpspace-1
36 L1:     lda     sp,x
37         sta     zpsave,x        ; Save the zero page locations we need
38         dex
39         bpl     L1
40
41 ; Close open files
42
43         jsr     CLRCH
44
45 ; Switch to second charset. The routine that is called by BSOUT to switch the
46 ; character set will use FNLEN as temporary storage - YUCK! Since the
47 ; initmainargs routine, which parses the command line for arguments needs this
48 ; information, we need to save and restore it here.
49 ; Thanks to Stefan Haubenthal for this information!
50
51         lda     FNLEN
52         pha                     ; Save FNLEN
53         lda     #14
54 ;       sta     $E84C           ; See PET FAQ
55         jsr     BSOUT
56         pla
57         sta     FNLEN           ; Restore FNLEN
58
59 ; Clear the BSS data
60
61         jsr     zerobss
62
63 ; Save system stuff and setup the stack
64
65         tsx
66         stx     spsave          ; Save the system stack ptr
67
68         lda     MEMSIZE
69         sta     sp
70         lda     MEMSIZE+1
71         sta     sp+1            ; Set argument stack ptr
72
73 ; Call module constructors
74
75         jsr     initlib
76
77 ; If we have IRQ functions, chain our stub into the IRQ vector
78
79         lda     #<__INTERRUPTOR_COUNT__
80         beq     NoIRQ1
81         lda     IRQVec
82         ldx     IRQVec+1
83         sta     IRQInd+1
84         stx     IRQInd+2
85         lda     #<IRQStub
86         ldx     #>IRQStub
87         sei
88         sta     IRQVec
89         stx     IRQVec+1
90         cli
91
92 ; Push arguments and call main()
93
94 NoIRQ1: jsr     callmain
95
96 ; Call module destructors. This is also the _exit entry.
97
98 _exit:  pha                     ; Save the return code on stack
99         lda     #<__INTERRUPTOR_COUNT__
100         beq     NoIRQ2
101         lda     IRQInd+1
102         ldx     IRQInd+2
103         sei
104         sta     IRQVec
105         stx     IRQVec+1
106         cli
107
108 ; Run module destructors
109
110 NoIRQ2: jsr     donelib         ; Run module destructors
111
112 ; Copy back the zero page stuff
113
114         ldx     #zpspace-1
115 L2:     lda     zpsave,x
116         sta     sp,x
117         dex
118         bpl     L2
119
120 ; Store the program return code into ST
121
122         pla
123         sta     ST
124
125 ; Restore the stack pointer
126
127         ldx     spsave
128         txs                     ; Restore stack pointer
129
130 ; Back to basic
131
132         rts
133
134 ; ------------------------------------------------------------------------
135 ; The IRQ vector jumps here, if condes routines are defined with type 2.
136
137 IRQStub:
138         cld                             ; Just to be sure
139         jsr     callirq                 ; Call the functions
140         jmp     IRQInd                  ; Jump to the saved IRQ vector
141
142 ; ------------------------------------------------------------------------
143 ; Data
144
145 .data
146
147 IRQInd: jmp     $0000
148
149 .segment        "ZPSAVE"
150
151 zpsave: .res    zpspace
152
153 .bss
154
155 spsave: .res    1
156 mmusave:.res    1
157