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