2008-08-13 02:44:56 +03:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2008 Openmoko, Inc.
|
|
|
|
* Author: Andy Green <andy@openmoko.org>
|
|
|
|
*
|
|
|
|
* Little utils for print and strings
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-11-28 12:16:35 +02:00
|
|
|
#include <qi.h>
|
2008-08-13 02:44:56 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-11-28 12:16:42 +02:00
|
|
|
void (*putc_func)(char) = NULL;
|
2008-11-28 12:16:37 +02:00
|
|
|
|
2008-11-28 12:16:36 +02:00
|
|
|
|
2008-11-28 12:16:41 +02:00
|
|
|
void set_putc_func(void (*p)(char))
|
|
|
|
{
|
|
|
|
putc_func = p;
|
|
|
|
}
|
|
|
|
|
2008-08-13 02:44:56 +03:00
|
|
|
size_t strlen(const char *s)
|
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
|
|
|
|
while (*s++)
|
|
|
|
n++;
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strcpy(char *dest, const char *src)
|
|
|
|
{
|
|
|
|
char * dest_orig = dest;
|
|
|
|
|
|
|
|
while (*src)
|
|
|
|
*dest++ = *src++;
|
2008-11-28 12:16:36 +02:00
|
|
|
*dest = '\0';
|
2008-08-13 02:44:56 +03:00
|
|
|
|
|
|
|
return dest_orig;
|
|
|
|
}
|
|
|
|
|
|
|
|
int puts(const char *string)
|
|
|
|
{
|
|
|
|
while (*string)
|
2008-11-28 12:16:41 +02:00
|
|
|
(putc_func)(*string++);
|
2008-08-13 02:44:56 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* done like this to avoid needing statics in steppingstone */
|
|
|
|
void printnybble(unsigned char n)
|
|
|
|
{
|
|
|
|
if (n < 10)
|
2008-11-28 12:16:41 +02:00
|
|
|
(putc_func)('0' + n);
|
2008-08-13 02:44:56 +03:00
|
|
|
else
|
2008-11-28 12:16:41 +02:00
|
|
|
(putc_func)('a' + n - 10);
|
2008-08-13 02:44:56 +03:00
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:37 +02:00
|
|
|
void print8(unsigned char n)
|
2008-08-13 02:44:56 +03:00
|
|
|
{
|
|
|
|
printnybble((n >> 4) & 15);
|
|
|
|
printnybble(n & 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print32(unsigned int u)
|
|
|
|
{
|
2008-11-28 12:16:37 +02:00
|
|
|
print8(u >> 24);
|
|
|
|
print8(u >> 16);
|
|
|
|
print8(u >> 8);
|
|
|
|
print8(u);
|
2008-08-13 02:44:56 +03:00
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:37 +02:00
|
|
|
void printdec(int n)
|
|
|
|
{
|
2008-11-28 12:16:40 +02:00
|
|
|
int d[] = {
|
|
|
|
1 * 1000 * 1000 * 1000,
|
|
|
|
100 * 1000 * 1000,
|
|
|
|
10 * 1000 * 1000,
|
|
|
|
1 * 1000 * 1000,
|
|
|
|
100 * 1000,
|
|
|
|
10 * 1000,
|
|
|
|
1 * 1000,
|
|
|
|
100,
|
|
|
|
10,
|
|
|
|
1,
|
|
|
|
0
|
|
|
|
};
|
2008-11-28 12:16:37 +02:00
|
|
|
int flag = 0;
|
2008-11-28 12:16:40 +02:00
|
|
|
int div = 0;
|
2008-11-28 12:16:37 +02:00
|
|
|
|
|
|
|
if (n < 0) {
|
2008-11-28 12:16:41 +02:00
|
|
|
(putc_func)('-');
|
2008-11-28 12:16:37 +02:00
|
|
|
n = -n;
|
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:40 +02:00
|
|
|
while (d[div]) {
|
|
|
|
int r = 0;
|
|
|
|
while (n >= d[div]) {
|
|
|
|
r++;
|
|
|
|
n -= d[div];
|
|
|
|
}
|
|
|
|
if (r || flag || (d[div] == 1)) {
|
2008-11-28 12:16:41 +02:00
|
|
|
(putc_func)('0' + r);
|
2008-11-28 12:16:37 +02:00
|
|
|
flag = 1;
|
|
|
|
}
|
2008-11-28 12:16:40 +02:00
|
|
|
div++;
|
2008-11-28 12:16:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:37 +02:00
|
|
|
void *memcpy(void *dest, const void *src, size_t n)
|
|
|
|
{
|
|
|
|
u8 const * ps = src;
|
|
|
|
u8 * pd = dest;
|
|
|
|
|
|
|
|
while (n--)
|
|
|
|
*pd++ = *ps++;
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *memset(void *s, int c, size_t n)
|
|
|
|
{
|
|
|
|
u8 * p = s;
|
|
|
|
|
|
|
|
while (n--)
|
|
|
|
*p++ = c;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:40 +02:00
|
|
|
int q;
|
|
|
|
|
|
|
|
void udelay(int n)
|
|
|
|
{
|
|
|
|
while (n--)
|
|
|
|
q+=n * q;
|
|
|
|
}
|