]> git.sur5r.net Git - cc65/blob - libsrc/pet/crt0.s
Added constructor support (contributed 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
46
47         lda     #14
48 ;       sta     $E84C           ; See PET FAQ
49         jsr     BSOUT
50
51 ; Clear the BSS data
52
53         jsr     zerobss
54
55 ; Save system stuff and setup the stack
56
57         tsx
58         stx     spsave          ; Save the system stack ptr
59
60         lda     MEMSIZE
61         sta     sp
62         lda     MEMSIZE+1
63         sta     sp+1            ; Set argument stack ptr
64
65 ; Call module constructors
66
67         jsr     initlib
68
69 ; If we have IRQ functions, chain our stub into the IRQ vector
70
71         lda     #<__INTERRUPTOR_COUNT__
72         beq     NoIRQ1
73         lda     IRQVec
74         ldx     IRQVec+1
75         sta     IRQInd+1
76         stx     IRQInd+2
77         lda     #<IRQStub
78         ldx     #>IRQStub
79         sei
80         sta     IRQVec
81         stx     IRQVec+1
82         cli
83
84 ; Push arguments and call main()
85
86 NoIRQ1: jsr     callmain
87
88 ; Call module destructors. This is also the _exit entry.
89
90 _exit:  pha                     ; Save the return code on stack
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 ; Run module destructors
101
102 NoIRQ2: jsr     donelib         ; Run module destructors
103
104 ; Copy back the zero page stuff
105
106         ldx     #zpspace-1
107 L2:     lda     zpsave,x
108         sta     sp,x
109         dex
110         bpl     L2
111
112 ; Store the program return code into ST
113
114         pla
115         sta     ST
116
117 ; Restore the stack pointer
118
119         ldx     spsave
120         txs                     ; Restore stack pointer
121
122 ; Back to basic
123
124         rts
125
126 ; ------------------------------------------------------------------------
127 ; The IRQ vector jumps here, if condes routines are defined with type 2.
128
129 IRQStub:
130         cld                             ; Just to be sure
131         jsr     callirq                 ; Call the functions
132         jmp     IRQInd                  ; Jump to the saved IRQ vector
133
134 ; ------------------------------------------------------------------------
135 ; Data
136
137 .data
138
139 zpsave: .res    zpspace
140 IRQInd: jmp     $0000
141
142 .bss
143
144 spsave: .res    1
145 mmusave:.res    1
146