summaryrefslogtreecommitdiffstats
path: root/qemu/roms/openhackware/src/start.S
blob: 471e56fef7f8eafc19e4c7d73da2b050b8f6a62c (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* 
 *	<start.S>
 *	
 *     BIOS start code for Open Hack'Ware.
 *   
 *   Copyright (C) 2004-2005 Jocelyn Mayer (l_indien@magic.fr)
 *   
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License V2
 *   as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define ASSEMBLY_CODE
#include "bios.h"

.section .start, "ax"
.align 2

.globl _start
_start:
        /* Save our stack pointer */
        lis    r11, saved_params@h                           ;
        ori    r11, r11, saved_params@l                      ;
        stw    r1, 0(r11)                                    ;
        /* Fill space from _bss_start to _ram_start with zeroes */
        lis    r11, _bss_start@h                             ;
        ori    r11, r11, _bss_start@l                        ;
        lis    r12, _ram_start@h                             ;
        ori    r12, r12, _ram_start@l                        ;
        subf   r12, r11, r12                                 ;
        srawi  r12, r12, 2                                   ;
        cmpi   0, r12, 0                                     ;
        beq    _bss_done                                     ;
        mtctr  r12                                           ;
        subi   r11, r11, 4                                   ;
        li     r12, 0                                        ;
_bss_loop:
        stwu   r12, 4(r11)                                   ;
        bdnz   _bss_loop                                     ;
_bss_done:
        /* Now, we have a real C environment: call main */
        bl     main                                          ;
        /* If we return, stop */
.globl bug
bug:
        li     r0, 0x80                                      ;
        mtlr   r0                                            ;
        blr                                                  ;
_return_loop:
        b      _return_loop                                  ;

.section .data
.align 2
saved_params:
        .long 0x00000000 /* OF stack     */
        .long 0x00000000 /* client stack */
        .long 0x00000000 /* client link  */
        
.section .text
.align 2

.globl transfer_handler
transfer_handler:
        /* Build a new stack room and launch loaded image
         * void transfer_handler (void *residual, void *load_addr,
         *                        void *OF_entry, void *bootinfos,
         *                        void *cmdline, void *unused,
         *                        void *nip, void *stack_base);
         */
        mfmsr  r0                                            ;
        mtspr  SRR1, r0                                      ;
        mtspr  SRR0, r9                                      ;
        li     r0, 0                                         ;
        mr     r1, r10                                       ;
        stw    r1, -16(r1)                                   ;
        stwu   r0, -4(r1)                                    ;
        stwu   r0, -4(r1)                                    ;
        stwu   r0, -4(r1)                                    ;
        stwu   r0, -4(r1)                                    ;
        /* Skip frame pointer */        
        stwu   r0, -8(r1)                                    ;
        stwu   r0, -4(r1)                                    ;
        stwu   r0, -4(r1)                                    ;
        rfi                                                  ;
        /* Should never return, but who knows... */
        bl     bug                                           ;

.globl  OF_entry
OF_entry:
        /* Save the stack pointer and get our own one */
        lis    r11, saved_params@h                           ;
        ori    r11, r11, saved_params@l                      ;
        mflr   r12                                           ;
        stw    r12, 8(r11)                                   ;
        stw    r1, 4(r11)                                    ;
        lwz    r1, 0(r11)                                    ;
        bl     OF_client_entry                               ;
        lis    r11, saved_params@h                           ;
        ori    r11, r11, saved_params@l                      ;
        lwz    r12, 8(r11)                                   ;
        mtlr   r12                                           ;
        lwz    r1, 4(r11)                                    ;
        blr                                                  ;
        
        /* PPC helpers */
.globl mfmsr
mfmsr:
        /* uint32_t mfmsr (void); */
        mfmsr  r3                                            ;
        blr                                                  ;
.globl mtmsr
mtmsr:
        /* void mtmsr (uint32_t msr); */
        lis    r0, _mtmsr_rfi@h                              ;
        ori    r0, r0, _mtmsr_rfi@l                          ;
        mtspr  26, r0                                        ;
        mtspr  27, r3                                        ;
        rfi                                                  ;
_mtmsr_rfi:
        blr                                                  ;
.globl MMU_on
MMU_on:
        /* void MMU_on (void); */
        stwu   r1, -16(r1)                                   ;
        mflr   r0                                            ;
        stw    r0, 20(r1)                                    ;
        mfmsr  r3                                            ;
        ori    r3, r3, 0x30                                  ;
        bl     mtmsr                                         ;
        lwz    r0, 20(r1)                                    ;
        mtlr   r0                                            ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;
        
.globl MMU_off
MMU_off:
        /* void MMU_off (void); */
        stwu   r1, -16(r1)                                   ;
        mflr   r0                                            ;
        stw    r0, 20(r1)                                    ;
        mfmsr  r3                                            ;
        andi.  r3, r3, 0xFFCF                                ;
        bl     mtmsr                                         ;
        lwz    r0, 20(r1)                                    ;
        mtlr   r0                                            ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;
        
.globl mfpvr
mfpvr:
        /* uint32_t mfpvr (void); */
        mfpvr  r3                                            ;
        blr                                                  ;

.globl mftb
mftb:
        /* void mftb (uint32_t *tb); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        stw    r12,  8(r1)                                   ;
        /* No need to save lr */
_tb_loop:
        mftbu  r11                                           ;
        mftb   r12                                           ;
        mftbu  r0                                            ;
        cmpw   r0, r11                                       ;
        bne    _tb_loop                                      ;
        stw    r11,  0(r3)                                   ;
        stw    r12,  4(r3)                                   ;
        lwz    r12,  8(r1)                                   ;
        lwz    r11, 12(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;      

        /* IO helpers */
.globl inb
inb:
        /* uint32_t inb (uint16_t port); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        lis    r11, isa_io_base@h                            ;
        ori    r11, r11, isa_io_base@l                       ;
        lwz    r11, 0(r11)                                   ;
        add    r3, r3, r11                                   ;
        lbz    r3, 0(r3)                                     ;
        eieio                                                ;
        lwz    r11, 12(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;

.globl outb
outb:
        /* void outb (uint16_t port, uint32_t val); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        lis    r11, isa_io_base@h                            ;
        ori    r11, r11, isa_io_base@l                       ;
        lwz    r11, 0(r11)                                   ;
        add    r3, r3, r11                                   ;
        eieio                                                ;
        stb    r4, 0(r3)                                     ;
        lwz    r11, 12(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;

.globl inw
inw:
        /* uint32_t inw (uint16_t port); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        lis    r11, isa_io_base@h                            ;
        ori    r11, r11, isa_io_base@l                       ;
        lwz    r11, 0(r11)                                   ;
        add    r3, r3, r11                                   ;
        lhbrx  r3, 0, r3                                     ;
        eieio                                                ;
        lwz    r11, 12(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;

.globl outw
outw:
        /* void outw (uint16_t port, uint32_t val); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        lis    r11, isa_io_base@h                            ;
        ori    r11, r11, isa_io_base@l                       ;
        lwz    r11, 0(r11)                                   ;
        add    r3, r3, r11                                   ;
        eieio                                                ;
        sthbrx r4, 0, r3                                     ;
        lwz    r11, 12(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;

.globl inl
inl:
        /* uint32_t inl (uint16_t port); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        lis    r11, isa_io_base@h                            ;
        ori    r11, r11, isa_io_base@l                       ;
        lwz    r11, 0(r11)                                   ;
        add    r3, r3, r11                                   ;
        lwbrx  r3, 0, r3                                     ;
        eieio                                                ;
        lwz    r11, 12(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;

.globl outl
outl:
        /* void outl (uint16_t port, uint32_t val); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        lis    r11, isa_io_base@h                            ;
        ori    r11, r11, isa_io_base@l                       ;
        lwz    r11, 0(r11)                                   ;
        add    r3, r3, r11                                   ;
        eieio                                                ;
        stwbrx r4, 0, r3                                     ;
        lwz    r11, 12(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;

.globl eieio
eieio:
        eieio                                                ;
        blr                                                  ;
        
        /* Misc helpers */
.globl ldswap16
ldswap16:
        /* uint16_t ldswap16 (uint16_t *addr); */
        lhbrx  r3, 0, r3                                     ;
        blr                                                  ;

.globl stswap16
stswap16:
        /* void stswap16 (void *addr, uint16_t val); */
        sthbrx r4, 0, r3                                     ;
        blr                                                  ;

.globl ldswap32
ldswap32:
        /* uint32_t ldswap32 (uint32_t *addr); */
        lwbrx  r3, 0, r3                                     ;
        blr                                                  ;

.globl stswap32
stswap32:
        /* void stswap32 (void *addr, uint32_t val); */
        stwbrx r4, 0, r3                                     ;
        blr                                                  ;

.globl mul64
mul64:
        /* void mul64 (uint32_t *ret, uint32_t a, uint32_t b); */
        mulhwu r0, r4, r5                                    ;
        stw    r0, 0(r3)                                     ;
        mullw  r0, r4, r5                                    ;
        stw    r0, 4(r3)                                     ;
        blr                                                  ;

.globl add64
add64:
        /* void add64 (uint32_t *ret, uint32_t *a, uint32_t *b); */
        stwu   r1, -16(r1)                                   ;
        stw    r11, 12(r1)                                   ;
        stw    r12,  8(r1)                                   ;
        lwz    r11,  4(r4)                                   ;
        lwz    r12,  4(r5)                                   ;
        addc   r0, r11, r12                                  ;
        stw    r0,   4(r3)                                   ;
        lwz    r11,  0(r4)                                   ;
        lwz    r12,  0(r5)                                   ;
        adde   r0, r11, r12                                  ;
        stw    r0,   0(r3)                                   ;
        lwz    r12,  8(r1)                                   ;
        lwz    r11,  4(r1)                                   ;
        addi   r1, r1, 16                                    ;
        blr                                                  ;

.globl setjmp
setjmp:
        /* int setjmp (jmp_buf env); */
        /* save gprs */
        stmw   r0, 0(r3)                                     ;
        /* save lr, ctr, xer and ccr */
        mflr   r0                                            ;
        stw    r0, 0x80(r3)                                  ;
        mfctr  r0                                            ;
        stw    r0, 0x84(r3)                                  ;
        mfxer  r0                                            ;
        stw    r0, 0x88(r3)                                  ;
        mfcr   r0                                            ;
        stw    r0, 0x8C(r3)                                  ;
        /* return 0 */
        li     r3, 0                                         ;
        blr                                                  ;

.globl longjmp
longjmp:
        /* void longjmp (jmp_buf env, int val); */
        /* Let's pretend env is our stack */
        mr     r1, r3                                        ;
        /* Be sure we won't return 0 */
        cmpi   0, r4, 0                                      ;
        bne    _longjmp_cont                                 ;
        addi   r4, r4, 1                                     ;
_longjmp_cont:
        /* Store return value in jmp_buf */
        stw    r4, 0x0C(r1)                                  ;
        /* restore lr, ctr, xer and ccr */
        lwz    r0, 0x80(r1)                                  ;
        mtlr   r0                                            ;
        lwz    r0, 0x84(r1)                                  ;
        mtctr  r0                                            ;
        lwz    r0, 0x88(r1)                                  ;
        mtxer  r0                                            ;
        lwz    r0, 0x8C(r1)                                  ;
        mtcr   r0                                            ;
        /* Restore r2 to r31 */
        lmw    r2, 0x08(r1)                                  ;
        /* Restore r0 (could forget it...) */
        lwz    r0, 0x00(r1)                                  ;
        /* Restore stack */
        lwz    r1, 0x04(r1)                                  ;
        /* Return */
        blr                                                  ;