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