OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [mini-libc/] [libc_asm.S] - Blame information for rev 33

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  libc_asm.S                                                  //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  Assembly routines for the mini-libc library.                //
10
//                                                              //
11
//  Author(s):                                                  //
12
//      - Conor Santifort, csantifort.amber@gmail.com           //
13
//                                                              //
14
//////////////////////////////////////////////////////////////////
15
//                                                              //
16
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
17
//                                                              //
18
// This source file may be used and distributed without         //
19
// restriction provided that this copyright statement is not    //
20
// removed from the file and that any derivative work contains  //
21
// the original copyright notice and the associated disclaimer. //
22
//                                                              //
23
// This source file is free software; you can redistribute it   //
24
// and/or modify it under the terms of the GNU Lesser General   //
25
// Public License as published by the Free Software Foundation; //
26
// either version 2.1 of the License, or (at your option) any   //
27
// later version.                                               //
28
//                                                              //
29
// This source is distributed in the hope that it will be       //
30
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
31
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
32
// PURPOSE.  See the GNU Lesser General Public License for more //
33
// details.                                                     //
34
//                                                              //
35
// You should have received a copy of the GNU Lesser General    //
36
// Public License along with this source; if not, download it   //
37
// from http://www.opencores.org/lgpl.shtml                     //
38
//                                                              //
39
----------------------------------------------------------------*/
40
 
41
#include "amber_registers.h"
42
 
43
 
44
/* _testfail: Used to terminate execution in Verilog simulations */
45
/* On the board just puts the processor into an infinite loop    */
46
        .section .text
47
        .globl _testfail
48
_testfail:
49
        ldr     r11, AdrTestStatus
50
        str     r0, [r11]
51
        b       _testfail
52
 
53
 
54
/* _testpass: Used to terminate execution in Verilog simulations */
55
/* On the board just puts the processor into an infinite loop    */
56
        .globl _testpass
57
_testpass:
58
        ldr     r11, AdrTestStatus
59
        mov     r10, #17
60
        str     r10, [r11]
61
        b       _testpass
62
 
63
/* _outbyte: Output a single character through UART 0 */
64
        @ if the uart tx fifo is stuck full
65
        @ this routine will cycle forever
66
        .globl _outbyte
67
_outbyte:
68
        ldr     r1, AdrUARTDR
69
        ldr     r3, AdrUARTFR
70
        @ Check the tx_full flag
71
1:      ldr     r2, [r3]
72
        and     r2, r2, #0x20
73
        cmp     r2, #0
74
        streqb  r0, [r1]
75
        moveqs  pc, lr          @ return
76
        bne     1b
77
 
78
 
79
/* _inbyte: Input a single character from UART 0 */
80
        @ r0 is the timeout in mS
81
        .globl _inbyte
82
_inbyte:
83
        ldr     r2, AdrUARTDR @ data
84
        ldr     r3, AdrUARTFR @ flags
85
 
86
        @ Multiple delay value by 2560
87
        @ as the delay loop takes about 12 clock cycles running cached
88
        @ so that factor gives 1:1mS @33MHz
89
        mov     r1, r0, lsl #11
90
        add     r1, r1, r0, lsl #9
91
 
92
        @ Check the r2 empty flag
93
2:      ldr     r0, [r3]
94
        ands    r0, r0, #0x10
95
        ldreqb  r0, [r2]
96
        moveq   pc, lr
97
 
98
        @ decrement timeout
99
        subs    r1, r1, #1
100
        bne     2b
101
 
102
        mov     r0, #-1
103
        movs    pc, lr
104
 
105
 
106
/* _div: Integer division function */
107
        @ Divide r0 by r1
108
        @ Answer returned in r1
109
        .globl _div
110 31 csantifort
        .globl __aeabi_idiv
111
__aeabi_idiv:
112 2 csantifort
_div:
113
        stmdb   sp!, {r4, lr}
114
 
115 33 csantifort
        @ set r4 to 1 if one of the two inputs is negative
116
        and     r2, r0, #0x80000000
117
        and     r3, r1, #0x80000000
118
        eor     r4, r2, r3
119
 
120
        @ Invert negative numbers
121
        tst     r0, #0x80000000
122
        mvnne   r0, r0
123
        addne   r0, r0, #1
124
 
125
        tst     r1, #0x80000000
126
        mvnne   r1, r1
127
        addne   r1, r1, #1
128
 
129 2 csantifort
        @ divide r1 by r2, also use registers r0 and r4
130
        mov     r2, r1
131
        mov     r1, r0
132
 
133
        cmp      r2, #0
134
        beq      3f
135
 
136
        @ In order to divide r1 by r2, the first thing we need to do is to shift r2
137
        @ left by the necessary number of places. The easiest method of doing this
138
        @ is simply by trial and error - shift until we discover that r2 has become
139
        @ too big, then stop.
140
        mov      r0,#0     @ clear r0 to accumulate result
141
        mov      r3,#1     @ set bit 0 in r3, which will be
142
                           @ shifted left then right
143
 
144
1:      cmp      r3, #0    @ escape on error
145
        moveq    r3, #0x10000000
146
        beq      2f
147
        cmp      r2,r1
148
        movls    r2,r2,lsl#1
149
        movls    r3,r3,lsl#1
150
        bls      1b
151
        @ shift r2 left until it is about to be bigger than r1
152
        @ shift r3 left in parallel in order to flag how far we have to go
153
 
154
        @ r0 will be used to hold the result. The role of r3 is more complicated.
155
        @ In effect, we are using r3 to mark where the right-hand end of r2 has got to
156
        @ - if we shift r2 three places left, this will be indicated by a value of %1000
157
        @ in r3. However, we also add it to r0 every time we manage a successful subtraction,
158
        @ since it marks the position of the digit currently being calculated in the answer.
159
        @ In the binary example (50 ÷ 10) above, we shifted the '10' two places left,
160
        @ so at the time of the first subtraction, r3 would have been %100, at the time
161
        @ of the second (which failed) it would have been %10, and at the time of the
162
        @ third %1. Adding it to r0 after each successful subtraction would have
163
        @ given us, once again, the answer of %101!
164
 
165
        @ Now for the loop that actually does the work:
166
2:      cmp       r1,r2      @ carry set if r1>r2 (don't ask why)
167
        subcs     r1,r1,r2   @ subtract r2 from r1 if this would
168
                             @ give a positive answer
169
        addcs     r0,r0,r3   @ and add the current bit in r3 to
170
                             @ the accumulating answer in r0
171
 
172
        @ In subtraction (a cmp instruction simulates a subtraction in
173
        @ order to set the flags), if r1 - r2 gives a positive answer and no 'borrow'
174
        @ is required, the carry flag is set. This is required in order to make SBC
175
        @ (Subtract with Carry) work properly when used to carry out a 64-bit subtraction,
176
        @ but it is confusing!
177
 
178
        @ In this case, we are turning it to our advantage. The carry flag is set to
179
        @ indicate that a successful subtraction is possible, i.e. one that doesn't
180
        @ generate a negative result, and the two following instructions are carried
181
        @ out only when the condition Carry Set applies. Note that the 'S' on the end
182
        @ of these instructions is part of the 'CS' condition code and does not mean
183
        @ that they set the flags!
184
 
185
        movs      r3,r3,lsr #1    @ Shift r3 right into carry flag
186
        movcc     r2,r2,lsr #1    @ and if bit 0 of r3 was zero, also
187
                                  @ shift r2 right
188
        bcc       2b              @ If carry not clear, r3 has shifted
189
                                  @ back to where it started, and we
190
                                  @ can end
191 33 csantifort
 
192
        @ if one of the inputs is negetive then return a negative result
193
        tst     r4, #0x80000000
194
        mvnne   r0, r0
195
        addne   r0, r0, #1
196 2 csantifort
3:      ldmia   sp!, {r4, pc}^
197
 
198
 
199 31 csantifort
/* strcpy: String copy function
200
    char * strcpy ( char * destination, const char * source );
201
    destination is returned
202
*/
203 2 csantifort
        @ r0 points to destination
204
        @ r1 points to source string which terminates with a 0
205
        .globl strcpy
206 31 csantifort
strcpy:
207
        stmdb   sp!, {r4-r7, lr}
208 33 csantifort
        @ Use r6 to process the destination pointer.
209
        @ At the end of the function, r0 is returned, so need to preserve it
210 31 csantifort
        mov     r6, r0
211
        @ only if both strings are zero-aligned use the fast 'aligned' algorithm
212 33 csantifort
        orr     r2, r6, r1
213
        ands    r2, r2, #3
214 31 csantifort
        bne     strcpy_slow
215
 
216
strcpy_fast:
217
        @ process strings 12 bytes at a time
218
        ldmia   r1!, {r2-r5}
219
 
220
        @ check for a zero byte
221
        @ only need to examine one of the strings because
222
        @ they are equal up to this point!
223
        ands    r7, r2, #0xff
224
        andnes  r7, r2, #0xff00
225
        andnes  r7, r2, #0xff0000
226
        andnes  r7, r2, #0xff000000
227
        strne   r2, [r6], #4
228
        subeq   r1, r1, #4
229
 
230
        andnes  r7, r3, #0xff
231
        andnes  r7, r3, #0xff00
232
        andnes  r7, r3, #0xff0000
233
        andnes  r7, r3, #0xff000000
234
        strne   r3, [r6], #4
235
        subeq   r1, r1, #4
236
 
237
        andnes  r7, r4, #0xff
238
        andnes  r7, r4, #0xff00
239
        andnes  r7, r4, #0xff0000
240
        andnes  r7, r4, #0xff000000
241
        strne   r4, [r6], #4
242
        subeq   r1, r1, #4
243
 
244
        andnes  r7, r5, #0xff
245
        andnes  r7, r5, #0xff00
246
        andnes  r7, r5, #0xff0000
247
        andnes  r7, r5, #0xff000000
248
        strne   r5, [r6], #4
249
        subeq   r1, r1, #4
250
 
251
        @ loop back to look at next 12 bytes
252
        bne     strcpy_fast
253
 
254
        @ the source string contains a zero character
255
 
256
strcpy_slow:
257
        @ unroll the loop 4 times
258 2 csantifort
        ldrb    r3, [r1], #1
259 31 csantifort
        strb    r3, [r6], #1
260 2 csantifort
        cmp     r3, #0
261 31 csantifort
        ldmeqia sp!, {r4-r7, pc}^
262
 
263
        ldrb    r3, [r1], #1
264
        strb    r3, [r6], #1
265
        cmp     r3, #0
266
        ldmeqia sp!, {r4-r7, pc}^
267
 
268
        ldrb    r3, [r1], #1
269
        strb    r3, [r6], #1
270
        cmp     r3, #0
271
        ldmeqia sp!, {r4-r7, pc}^
272
 
273
        ldrb    r3, [r1], #1
274
        strb    r3, [r6], #1
275
        cmp     r3, #0
276
        ldmeqia sp!, {r4-r7, pc}^
277
 
278
        b       strcpy_slow
279 2 csantifort
 
280
 
281 31 csantifort
/* int strcmp ( const char * str1, const char * str2 );
282
   A value greater than zero indicates that the first character
283
   that does not match has a greater value in str1 than in str2;
284
   And a value less than zero indicates the opposite.
285
*/
286
        .globl strcmp
287
strcmp:
288
        stmdb   sp!, {r4-r8, lr}
289
 
290
        @ only if both strings are zero-aligned use the fast 'aligned' algorithm
291
        orr     r2, r0, r1
292
        ands    r2, r2, #3
293
        bne     strcmp_slow
294
 
295
strcmp_fast:
296
        @ process strings 12 bytes at a time
297
        ldmia   r0!, {r2-r4}
298
        ldmia   r1!, {r5-r7}
299
        cmp     r2, r5
300
        bne     1f
301
        cmpeq   r3, r6
302
        bne     2f
303
        cmpeq   r4, r7
304
        bne     3f
305
 
306
        @ strings are equal - find a zero byte
307
        @ only need to examine one of the strings because
308
        @ they are equal up to this point!
309
        ands    r8, r2, #0xff
310
        andnes  r8, r2, #0xff00
311
        andnes  r8, r2, #0xff0000
312
        andnes  r8, r2, #0xff000000
313
 
314
        andnes  r8, r3, #0xff
315
        andnes  r8, r3, #0xff00
316
        andnes  r8, r3, #0xff0000
317
        andnes  r8, r3, #0xff000000
318
 
319
        andnes  r8, r4, #0xff
320
        andnes  r8, r4, #0xff00
321
        andnes  r8, r4, #0xff0000
322
        andnes  r8, r4, #0xff000000
323
 
324
        @ loop back to look at next 12 bytes
325
        bne     strcmp_fast
326
 
327
        @ the first string contains a zero character
328
        @ the strings are the same, so both strings end
329
        moveq   r0, #0
330
        ldmeqia sp!, {r4-r8, pc}^
331
 
332
 
333
        @ Roll back the string pointers to before the mismatch
334
        @ then handle the remaining part byte by byte
335
1:      sub     r0, r0, #12
336
        sub     r1, r1, #12
337
 
338
strcmp_slow:
339
        ldrb    r2, [r0], #1
340
        ldrb    r3, [r1], #1
341
        eors    r4, r2, r3          @ are the bytes equal ?
342
        bne     bytes_different
343
        ldrb    r5, [r0], #1
344
        ldrb    r6, [r1], #1
345
        cmp     r2, #0              @ are they equal and zero ?
346
        beq     bytes_zero
347
        eors    r7, r5, r6          @ are the bytes equal ?
348
        bne     bytes_different
349
        ldrb    r2, [r0], #1
350
        ldrb    r3, [r1], #1
351
        cmp     r5, #0              @ are they equal and zero ?
352
        beq     bytes_zero
353
        eors    r4, r2, r3          @ are the bytes equal ?
354
        bne     bytes_different
355
        ldrb    r5, [r0], #1
356
        ldrb    r6, [r1], #1
357
        cmp     r2, #0              @ are they equal and zero ?
358
        beq     bytes_zero
359
        eors    r7, r5, r6          @ are the bytes equal ?
360
        bne     bytes_different
361
        cmp     r5, #0              @ are they equal and zero ?
362
        beq     bytes_zero
363
 
364
        bne     strcmp_slow
365
 
366
 
367
 
368
@ Skipping first 4 bytes so just check they
369
@ don't contain an end of string 0 character
370
2:      ands    r8, r2, #0xff
371
        andnes  r8, r2, #0xff00
372
        andnes  r8, r2, #0xff0000
373
        andnes  r8, r2, #0xff000000
374
        beq     bytes_zero
375
 
376
        @ start looking at 5th byte
377
        sub     r0, r0, #8
378
        sub     r1, r1, #8
379
 
380
        ldrb    r2, [r0], #1
381
        ldrb    r3, [r1], #1
382
        eors    r4, r2, r3          @ are the bytes equal ?
383
        bne     bytes_different
384
        ldrb    r5, [r0], #1
385
        ldrb    r6, [r1], #1
386
        cmp     r2, #0              @ are they equal and zero ?
387
        beq     bytes_zero
388
        eors    r7, r5, r6          @ are the bytes equal ?
389
        bne     bytes_different
390
        ldrb    r2, [r0], #1
391
        ldrb    r3, [r1], #1
392
        cmp     r5, #0              @ are they equal and zero ?
393
        beq     bytes_zero
394
        eors    r4, r2, r3          @ are the bytes equal ?
395
        bne     bytes_different
396
        ldrb    r5, [r0], #1
397
        ldrb    r6, [r1], #1
398
        cmp     r2, #0              @ are they equal and zero ?
399
        beq     bytes_zero
400
        eors    r7, r5, r6          @ are the bytes equal ?
401
        bne     bytes_different
402
        cmp     r5, #0              @ are they equal and zero ?
403
        beq     bytes_zero
404
 
405
        bne     strcmp_slow
406
 
407
@ Skipping first 8 bytes so just check they
408
@ don't contain an end of string 0 character
409
3:      ands    r8, r2, #0xff
410
        andnes  r8, r2, #0xff00
411
        andnes  r8, r2, #0xff0000
412
        andnes  r8, r2, #0xff000000
413
 
414
        andnes  r8, r3, #0xff
415
        andnes  r8, r3, #0xff00
416
        andnes  r8, r3, #0xff0000
417
        andnes  r8, r3, #0xff000000
418
        beq     bytes_zero
419
 
420
        sub     r0, r0, #4
421
        sub     r1, r1, #4
422
        ldrb    r2, [r0], #1
423
        ldrb    r3, [r1], #1
424
        eors    r4, r2, r3          @ are the bytes equal ?
425
        bne     bytes_different
426
        ldrb    r5, [r0], #1
427
        ldrb    r6, [r1], #1
428
        cmp     r2, #0              @ are they equal and zero ?
429
        beq     bytes_zero
430
        eors    r7, r5, r6          @ are the bytes equal ?
431
        bne     bytes_different
432
        ldrb    r2, [r0], #1
433
        ldrb    r3, [r1], #1
434
        cmp     r5, #0              @ are they equal and zero ?
435
        beq     bytes_zero
436
        eors    r4, r2, r3          @ are the bytes equal ?
437
        bne     bytes_different
438
        ldrb    r5, [r0], #1
439
        ldrb    r6, [r1], #1
440
        cmp     r2, #0              @ are they equal and zero ?
441
        beq     bytes_zero
442
        eors    r7, r5, r6          @ are the bytes equal ?
443
        bne     bytes_different
444
        cmp     r5, #0              @ are they equal and zero ?
445
        beq     bytes_zero
446
 
447
        bne     strcmp_slow
448
 
449
 
450
bytes_zero:
451
        moveq   r0, #0              @ if equal and zero, return zero
452
        ldmeqia sp!, {r4-r8, pc}^
453
 
454
 
455
bytes_different:
456
        sub     r0, r5, r6
457
        ldmia   sp!, {r4-r8, pc}^
458
 
459
 
460
 
461
        /* void *malloc(size_t size); */
462
        .globl malloc
463
malloc:
464
        ldr     r1, AdrMalloc
465
        ldr     r0, [r1]
466
        add     r0, r0, #0x10000
467
        str     r0, [r1]
468
        mov     pc, lr
469
 
470
 
471
 
472 2 csantifort
/* strncpy: String copy function */
473
        @ r0 points to destination
474
        @ r1 points to source string
475
        @ r2 is the number of bytes to copy
476
        .globl strncpy
477
strncpy:
478
        stmdb   sp!, {r4, lr}
479
        cmp     r2, #0
480
        beq     2f
481 33 csantifort
        add     r4, r0, r2    @ set r4 to the address of the last byte copied
482 2 csantifort
1:      ldrb    r3, [r1], #1
483
        strb    r3, [r0], #1
484
        cmp     r2,  r4
485 33 csantifort
        bne     1b
486 2 csantifort
2:      ldmia   sp!, {r4, pc}^
487
 
488
 
489
/* strncpy: String compare function */
490
        @ return the difference if the strings don't match
491
        .globl strncmp
492
strncmp:
493
        stmdb   sp!, {r4, r5, r6, lr}
494
 
495
        @ check for 0 length
496
        cmp     r2, #0
497
        moveq   r0, #1
498
        beq     2f
499
 
500
        mov     r3, #0
501
 
502
1:      add     r3, r3,   #1
503
        ldrb    r4, [r0], #1
504
        ldrb    r5, [r1], #1
505
 
506
        subs    r6, r4, r5
507
        movne   r0, r6
508
        bne     2f
509
 
510
        cmp     r3, r2
511
        moveq   r0, #0
512
        beq     2f
513
 
514
        b       1b
515
2:      ldmia   sp!, {r4, r5, r6, pc}^
516
 
517
 
518 31 csantifort
AdrMalloc:      .word  0x7000000
519 2 csantifort
AdrTestStatus:  .word  ADR_AMBER_TEST_STATUS
520
AdrUARTDR:      .word  ADR_AMBER_UART0_DR
521
AdrUARTFR:      .word  ADR_AMBER_UART0_FR
522
/* ========================================================================= */
523
/* ========================================================================= */
524
 
525
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.