Root/qiboot/src/utils.c

1/*
2 * (C) Copyright 2008 Openmoko, Inc.
3 * Author: Andy Green <andy@openmoko.org>
4 *
5 * Little utils for print and strings
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include <qi.h>
24#include <string.h>
25
26void (*putc_func)(char) = NULL;
27
28
29void set_putc_func(void (*p)(char))
30{
31    putc_func = p;
32}
33
34size_t strlen(const char *s)
35{
36    size_t n = 0;
37
38    while (*s++)
39        n++;
40
41    return n;
42}
43
44char *strcpy(char *dest, const char *src)
45{
46    char * dest_orig = dest;
47
48    while (*src)
49        *dest++ = *src++;
50    *dest = '\0';
51
52    return dest_orig;
53}
54
55int puts(const char *string)
56{
57    while (*string)
58        (putc_func)(*string++);
59
60    return 1;
61}
62
63/* done like this to avoid needing statics in steppingstone */
64void printnybble(unsigned char n)
65{
66    if (n < 10)
67        (putc_func)('0' + n);
68    else
69        (putc_func)('a' + n - 10);
70}
71
72void print8(unsigned char n)
73{
74    printnybble((n >> 4) & 15);
75    printnybble(n & 15);
76}
77
78void print32(unsigned int u)
79{
80    print8(u >> 24);
81    print8(u >> 16);
82    print8(u >> 8);
83    print8(u);
84}
85
86void printdec(int n)
87{
88    int d[] = {
89        1 * 1000 * 1000 * 1000,
90             100 * 1000 * 1000,
91              10 * 1000 * 1000,
92               1 * 1000 * 1000,
93                100 * 1000,
94                 10 * 1000,
95                  1 * 1000,
96                   100,
97                    10,
98                     1,
99                     0
100    };
101    int flag = 0;
102    int div = 0;
103
104    if (n < 0) {
105        (putc_func)('-');
106        n = -n;
107    }
108
109    while (d[div]) {
110        int r = 0;
111        while (n >= d[div]) {
112            r++;
113            n -= d[div];
114        }
115        if (r || flag || (d[div] == 1)) {
116            (putc_func)('0' + r);
117            flag = 1;
118        }
119        div++;
120    }
121}
122
123void *memcpy(void *dest, const void *src, size_t n)
124{
125    u8 const * ps = src;
126    u8 * pd = dest;
127
128    while (n--)
129        *pd++ = *ps++;
130
131    return dest;
132}
133
134void *memset(void *s, int c, size_t n)
135{
136    u8 * p = s;
137
138    while (n--)
139        *p++ = c;
140
141    return s;
142}
143
144int q;
145
146void udelay(int n)
147{
148    while (n--)
149        q+=n * q;
150}
151

Archive Download this file



interactive