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:37 +02:00
|
|
|
static u8 malloc_pool[100 * 1024];
|
|
|
|
void * malloc_pointer = &malloc_pool[0];
|
|
|
|
|
2008-11-28 12:16:36 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:37 +02:00
|
|
|
char *strncpy(char *dest, const char *src, size_t n)
|
|
|
|
{
|
|
|
|
char * dest_orig = dest;
|
|
|
|
|
|
|
|
while (*src && n--)
|
|
|
|
*dest++ = *src++;
|
|
|
|
|
2008-11-28 12:16:38 +02:00
|
|
|
if (n)
|
|
|
|
*dest = '\0';
|
|
|
|
|
2008-11-28 12:16:37 +02:00
|
|
|
return dest_orig;
|
2008-08-13 02:44:56 +03:00
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:37 +02:00
|
|
|
|
|
|
|
int strcmp(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
if (*s1 != *s2)
|
|
|
|
return *s1 - *s2;
|
|
|
|
if (!*s1)
|
|
|
|
return 0;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
2008-11-28 12:16:37 +02:00
|
|
|
}
|
|
|
|
|
2008-11-28 12:16:37 +02:00
|
|
|
char *strchr(const char *s, int c)
|
|
|
|
{
|
|
|
|
while ((*s) && (*s != c))
|
|
|
|
s++;
|
|
|
|
|
|
|
|
if (*s == c)
|
|
|
|
return (char *)s;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-28 12:16:37 +02:00
|
|
|
|
2008-08-13 02:44:56 +03:00
|
|
|
int puts(const char *string)
|
|
|
|
{
|
|
|
|
while (*string)
|
2008-11-28 12:16:36 +02:00
|
|
|
this_board->putc(*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:36 +02:00
|
|
|
this_board->putc('0' + n);
|
2008-08-13 02:44:56 +03:00
|
|
|
else
|
2008-11-28 12:16:36 +02:00
|
|
|
this_board->putc('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
|
|
|
}
|
|
|
|
|
|
|
|
void hexdump(unsigned char *start, int len)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
print32((int)start);
|
2008-11-28 12:16:36 +02:00
|
|
|
this_board->putc(':');
|
|
|
|
this_board->putc(' ');
|
2008-08-13 02:44:56 +03:00
|
|
|
for (n = 0; n < 16; n++) {
|
2008-11-28 12:16:37 +02:00
|
|
|
print8(*start++);
|
2008-11-28 12:16:36 +02:00
|
|
|
this_board->putc(' ');
|
2008-08-13 02:44:56 +03:00
|
|
|
}
|
2008-11-28 12:16:36 +02:00
|
|
|
this_board->putc('\n');
|
2008-08-13 02:44:56 +03:00
|
|
|
len -= 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
this_board->putc('-');
|
|
|
|
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:37 +02:00
|
|
|
this_board->putc('0' + r);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* improbably simple malloc and free for small and non-intense allocation
|
|
|
|
* just moves the allocation ptr forward each time and ignores free
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *malloc(size_t size)
|
|
|
|
{
|
|
|
|
void *p = malloc_pointer;
|
|
|
|
|
2008-11-28 12:16:38 +02:00
|
|
|
malloc_pointer += (size & ~3) + 4;
|
2008-11-28 12:16:37 +02:00
|
|
|
|
|
|
|
if (((u8 *)malloc_pointer - &malloc_pool[0]) > sizeof(malloc_pool)) {
|
|
|
|
puts("Ran out of malloc pool\n");
|
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free(void *ptr)
|
|
|
|
{
|
|
|
|
}
|
2008-11-28 12:16:40 +02:00
|
|
|
|
|
|
|
int q;
|
|
|
|
|
|
|
|
void udelay(int n)
|
|
|
|
{
|
|
|
|
while (n--)
|
|
|
|
q+=n * q;
|
|
|
|
}
|