port-work; won't compile or even work
[ppcskel.git] / crt0.S
1 # crt0.s file for the GameCube V1.0 by Costis (costis@gbaemu.com)!
2
3
4         .globl _start
5 _start:
6
7         # Clear all GPRs
8         .irp i, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
9         li \i,0
10         .endr
11
12         lis 1,_stack_bot@h ; ori 1,1,_stack_bot@l
13         stwu 0,-64(1)
14
15         lis      2,0x8000
16         stw      1,0x34(2) # write sp
17 #       lis      13,_SDA_BASE_@h
18 #       ori      13,13,_SDA_BASE_@l # Set the Small Data (Read\Write) base register.    
19
20         bl       InitHardware # Initialize the GameCube Hardware (Floating Point Registers, Caches, etc.) 
21         bl       SystemInit # Initialize more cache aspects, clear a few SPR's, and disable interrupts.
22         
23         # clear BSS
24         lis 3, __bss_start@h
25         ori 3, 3, __bss_start@l
26         li 4, 0
27         lis 5, __bss_end@h
28         ori 5, 5, __bss_end@l
29         sub 5, 5, 3
30         
31         bl memset
32         
33         bl       main # Branch to the user code!
34         b .     # If the main function returns, then just loop endlessly.
35
36 InitHardware:
37         mflr     31 # Store the link register in r31
38         
39         bl               PSInit         # Initialize Paired Singles
40         bl       FPRInit        # Initialize the FPR's
41         bl       CacheInit      # Initialize the system caches
42
43         mtlr     31 # Retreive the link register from r31
44         blr
45
46 PSInit:
47         mfspr    3, 920 # (HID2)
48         oris     3, 3, 0xA000
49         mtspr    920, 3 # (HID2)
50
51         # Set the Instruction Cache invalidation bit in HID0
52         mfspr    3,1008
53         ori      3,3,0x0800
54         mtspr    1008,3
55
56         sync
57
58         # Clear various Special Purpose Registers
59         li       3,0
60         mtspr    912,3
61         mtspr    913,3
62         mtspr    914,3
63         mtspr    915,3
64         mtspr    916,3
65         mtspr    917,3
66         mtspr    918,3
67         mtspr    919,3
68
69         # Return 
70         blr
71
72 FPRInit:
73         # Enable the Floating Point Registers
74         mfmsr    3
75         ori      3,3,0x2000
76         mtmsr    3
77         
78         # Clear all of the FPR's to 0
79         lis              3, zfloat@h
80         ori              3, 3, zfloat@l
81         lfd              0, 0(3)
82         fmr      1,0
83         fmr      2,0
84         fmr      3,0
85         fmr      4,0
86         fmr      5,0
87         fmr      6,0
88         fmr      7,0
89         fmr      8,0
90         fmr      9,0
91         fmr      10,0
92         fmr      11,0
93         fmr      12,0
94         fmr      13,0
95         fmr      14,0
96         fmr      15,0
97         fmr      16,0
98         fmr      17,0
99         fmr      18,0
100         fmr      19,0
101         fmr      20,0
102         fmr      21,0
103         fmr      22,0
104         fmr      23,0
105         fmr      24,0
106         fmr      25,0
107         fmr      26,0
108         fmr      27,0
109         fmr      28,0
110         fmr      29,0
111         fmr      30,0
112         fmr      31,0
113         mtfsf    255,0
114
115         # Return
116         blr
117
118 CacheInit:
119         mflr     0
120         stw      0, 4(1)
121         stwu     1, -16(1)
122         stw      31, 12(1)
123         stw      30, 8(1)
124
125         mfspr    3,1008 # (HID0)
126         rlwinm   0, 3, 0, 16, 16
127         cmplwi   0, 0x0000 # Check if the Instruction Cache has been enabled or not.
128         bne      ICEnabled
129
130         # If not, then enable it.
131         isync
132         mfspr    3, 1008
133         ori      3, 3, 0x8000
134         mtspr    1008, 3
135
136 ICEnabled:
137         mfspr    3, 1008 # bl       PPCMfhid0
138         rlwinm   0, 3, 0, 17, 17
139         cmplwi   0, 0x0000 # Check if the Data Cache has been enabled or not.
140         bne      DCEnabled
141         
142         # If not, then enable it.
143         sync
144         mfspr    3, 1008
145         ori      3, 3, 0x4000
146         mtspr    1008, 3
147         
148 DCEnabled:
149         
150         mfspr    3, 1017 # (L2CR)
151         clrrwi   0, 3, 31 # Clear all of the bits except 31
152         cmplwi   0, 0x0000
153         bne      L2GISkip # Skip the L2 Global Cache Invalidation process if it has already been done befor.
154
155         # Store the current state of the MSR in r30
156         mfmsr    3
157         mr       30,3
158         
159         sync
160         
161         # Enable Instruction and Data Address Translation
162         li       3, 48
163         mtmsr    3
164
165         sync
166         sync
167
168         # Disable the L2 Global Cache.
169         mfspr    3, 1017 # (L2CR
170         clrlwi   3, 3, 1
171         mtspr    1017, 3 # (L2CR)
172         sync
173
174         # Invalidate the L2 Global Cache.
175         bl       L2GlobalInvalidate
176
177         # Restore the previous state of the MSR from r30
178         mr       3, 30
179         mtmsr    3
180
181         # Enable the L2 Global Cache and disable the L2 Data Only bit and the L2 Global Invalidate Bit.
182         mfspr    3, 1017 # (L2CR)
183         oris     0, 3, 0x8000
184         rlwinm   3, 0, 0, 11, 9
185         mtspr    1017, 3 # (L2CR)
186
187
188 L2GISkip:
189         # Restore the non-volatile registers to their previous values and return.
190         lwz      0, 20(1)
191         lwz      31, 12(1)
192         lwz      30, 8(1)
193         addi     1, 1, 16
194         mtlr     0
195         blr
196
197 L2GlobalInvalidate:
198         mflr     0
199         stw      0, 4(1)
200         stwu     1, -16(1)
201         stw      31, 12(1)
202         sync
203
204         # Disable the L2 Cache.
205         mfspr    3, 1017  # bl       PPCMf1017
206         clrlwi   3, 3, 1
207         mtspr    1017, 3 # bl       PPCMt1017
208
209         sync
210
211         # Initiate the L2 Cache Global Invalidation process.
212         mfspr    3, 1017  # (L2CR)
213         oris     3, 3, 0x0020
214         mtspr    1017, 3 # (L2CR)
215
216         # Wait until the L2 Cache Global Invalidation has been completed.
217 L2GICheckComplete:
218         mfspr    3, 1017 # (L2CR)
219         clrlwi   0, 3, 31
220         cmplwi   0, 0x0000
221         bne      L2GICheckComplete
222         
223         # Clear the L2 Data Only bit and the L2 Global Invalidate Bit.
224         mfspr    3, 1017  # (L2CR)
225         rlwinm   3, 3, 0, 11, 9
226         mtspr    1017, 3 # (L2CR)
227
228         # Wait until the L2 Cache Global Invalidation status bit signifies that it is ready.
229 L2GDICheckComplete:
230         mfspr    3, 1017  # (L2CR)
231         clrlwi   0, 3, 31
232         cmplwi   0, 0x0000
233         bne      L2GDICheckComplete
234
235         # Restore the non-volatile registers to their previous values and return.
236         lwz      0, 20(1)
237         lwz      31, 12(1)
238         addi     1, 1, 16
239         mtlr     0
240         blr
241
242 SystemInit:
243         mflr    0
244         stw     0, 4(1)
245         stwu    1, -0x18(1)
246         stw     31, 0x14(1)
247         stw     30, 0x10(1)
248         stw     29, 0xC(1)
249
250         # Disable interrupts!
251         mfmsr    3
252         rlwinm   4,3,0,17,15
253         rlwinm   4,4,0,26,24
254         mtmsr    4
255
256         # Clear various SPR's
257         li       3,0
258         mtspr    952, 3
259         mtspr    956, 3
260         mtspr    953, 3
261         mtspr    954, 3
262         mtspr    957, 3
263         mtspr    958, 3
264
265         # Disable Speculative Bus Accesses to non-guarded space from both caches.
266         mfspr    3, 1008 # (HID0)
267         ori      3, 3, 0x0200
268         mtspr    1008, 3
269
270         # Set the Non-IEEE mode in the FPSCR
271         mtfsb1   29
272         
273         mfspr    3,920 # (HID2)
274         rlwinm   3, 3, 0, 2, 0
275         mtspr    920,3 # (HID2)         
276
277         # Restore the non-volatile registers to their previous values and return.
278         lwz     0, 0x1C(1)
279         lwz     31, 0x14(1)
280         lwz     30, 0x10(1)
281         lwz     29, 0xC(1)
282         addi    1, 1, 0x18
283         mtlr    0
284         blr
285
286 zfloat:
287         .float  0
288         .align  4
289