URL
https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk
Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc
Compare Revisions
- This comparison shows the changes necessary to convert path
/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk/mpsoc/src_processor/src_lib/simple-printf
- from Rev 38 to Rev 48
- ↔ Reverse comparison
Rev 38 → Rev 48
/prinf_long.c
0,0 → 1,422
/* |
* Copyright (c) 2016, Matt Redfearn |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* 1. Redistributions of source code must retain the above copyright notice, |
* this list of conditions and the following disclaimer. |
* |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
* this list of conditions and the following disclaimer in the documentation |
* and/or other materials provided with the distribution. |
* |
* 3. Neither the name of the copyright holder nor the names of its |
* contributors may be used to endorse or promote products derived from this |
* software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
* POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
#include <stddef.h> |
#include <stdarg.h> |
|
#ifdef TEST |
#include <stdio.h> |
#endif /* TEST */ |
|
static void simple_outputchar(char **str, char c) |
{ |
if (str) { |
**str = c; |
++(*str); |
} else { |
outbyte(c); |
} |
} |
|
enum flags { |
PAD_ZERO = 1, |
PAD_RIGHT = 2, |
}; |
|
static int prints(char **out, const char *string, int width, int flags) |
{ |
int pc = 0, padchar = ' '; |
|
if (width > 0) { |
int len = 0; |
const char *ptr; |
for (ptr = string; *ptr; ++ptr) ++len; |
if (len >= width) width = 0; |
else width -= len; |
if (flags & PAD_ZERO) |
padchar = '0'; |
} |
if (!(flags & PAD_RIGHT)) { |
for ( ; width > 0; --width) { |
simple_outputchar(out, padchar); |
++pc; |
} |
} |
for ( ; *string ; ++string) { |
simple_outputchar(out, *string); |
++pc; |
} |
for ( ; width > 0; --width) { |
simple_outputchar(out, padchar); |
++pc; |
} |
|
return pc; |
} |
|
#define PRINT_BUF_LEN 64 |
|
static int simple_outputi(char **out, long long i, int base, int sign, int width, int flags, int letbase) |
{ |
char print_buf[PRINT_BUF_LEN]; |
char *s; |
int t, neg = 0, pc = 0; |
unsigned long long u = i; |
|
if (i == 0) { |
print_buf[0] = '0'; |
print_buf[1] = '\0'; |
return prints(out, print_buf, width, flags); |
} |
|
if (sign && base == 10 && i < 0) { |
neg = 1; |
u = -i; |
} |
|
s = print_buf + PRINT_BUF_LEN-1; |
*s = '\0'; |
|
while (u) { |
t = u % base; |
if( t >= 10 ) |
t += letbase - '0' - 10; |
*--s = t + '0'; |
u /= base; |
} |
|
if (neg) { |
if( width && (flags & PAD_ZERO) ) { |
simple_outputchar (out, '-'); |
++pc; |
--width; |
} |
else { |
*--s = '-'; |
} |
} |
|
return pc + prints (out, s, width, flags); |
} |
|
|
static int simple_vsprintf(char **out, char *format, va_list ap) |
{ |
int width, flags; |
int pc = 0; |
char scr[2]; |
union { |
char c; |
char *s; |
int i; |
unsigned int u; |
long li; |
unsigned long lu; |
long long lli; |
unsigned long long llu; |
short hi; |
unsigned short hu; |
signed char hhi; |
unsigned char hhu; |
void *p; |
} u; |
|
for (; *format != 0; ++format) { |
if (*format == '%') { |
++format; |
width = flags = 0; |
if (*format == '\0') |
break; |
if (*format == '%') |
goto out; |
if (*format == '-') { |
++format; |
flags = PAD_RIGHT; |
} |
while (*format == '0') { |
++format; |
flags |= PAD_ZERO; |
} |
if (*format == '*') { |
width = va_arg(ap, int); |
format++; |
} else { |
for ( ; *format >= '0' && *format <= '9'; ++format) { |
width *= 10; |
width += *format - '0'; |
} |
} |
switch (*format) { |
case('i'): |
case('d'): |
u.i = va_arg(ap, int); |
pc += simple_outputi(out, u.i, 10, 1, width, flags, 'a'); |
break; |
|
case('u'): |
u.u = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.u, 10, 0, width, flags, 'a'); |
break; |
|
case('x'): |
u.u = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.u, 16, 0, width, flags, 'a'); |
break; |
|
case('X'): |
u.u = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.u, 16, 0, width, flags, 'A'); |
break; |
|
case('c'): |
u.c = va_arg(ap, int); |
scr[0] = u.c; |
scr[1] = '\0'; |
pc += prints(out, scr, width, flags); |
break; |
|
case('s'): |
u.s = va_arg(ap, char *); |
pc += prints(out, u.s ? u.s : "(null)", width, flags); |
break; |
case('l'): |
++format; |
switch (*format) { |
case('i'): |
case('d'): |
u.li = va_arg(ap, long); |
pc += simple_outputi(out, u.li, 10, 1, width, flags, 'a'); |
break; |
|
case('u'): |
u.lu = va_arg(ap, unsigned long); |
pc += simple_outputi(out, u.lu, 10, 0, width, flags, 'a'); |
break; |
|
case('x'): |
u.lu = va_arg(ap, unsigned long); |
pc += simple_outputi(out, u.lu, 16, 0, width, flags, 'a'); |
break; |
|
case('X'): |
u.lu = va_arg(ap, unsigned long); |
pc += simple_outputi(out, u.lu, 16, 0, width, flags, 'A'); |
break; |
|
case('l'): |
++format; |
switch (*format) { |
case('i'): |
case('d'): |
u.lli = va_arg(ap, long long); |
pc += simple_outputi(out, u.lli, 10, 1, width, flags, 'a'); |
break; |
|
case('u'): |
u.llu = va_arg(ap, unsigned long long); |
pc += simple_outputi(out, u.llu, 10, 0, width, flags, 'a'); |
break; |
|
case('x'): |
u.llu = va_arg(ap, unsigned long long); |
pc += simple_outputi(out, u.llu, 16, 0, width, flags, 'a'); |
break; |
|
case('X'): |
u.llu = va_arg(ap, unsigned long long); |
pc += simple_outputi(out, u.llu, 16, 0, width, flags, 'A'); |
break; |
|
default: |
break; |
} |
break; |
default: |
break; |
} |
break; |
case('h'): |
++format; |
switch (*format) { |
case('i'): |
case('d'): |
u.hi = va_arg(ap, int); |
pc += simple_outputi(out, u.hi, 10, 1, width, flags, 'a'); |
break; |
|
case('u'): |
u.hu = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.lli, 10, 0, width, flags, 'a'); |
break; |
|
case('x'): |
u.hu = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'a'); |
break; |
|
case('X'): |
u.hu = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'A'); |
break; |
|
case('h'): |
++format; |
switch (*format) { |
case('i'): |
case('d'): |
u.hhi = va_arg(ap, int); |
pc += simple_outputi(out, u.hhi, 10, 1, width, flags, 'a'); |
break; |
|
case('u'): |
u.hhu = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.lli, 10, 0, width, flags, 'a'); |
break; |
|
case('x'): |
u.hhu = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'a'); |
break; |
|
case('X'): |
u.hhu = va_arg(ap, unsigned int); |
pc += simple_outputi(out, u.lli, 16, 0, width, flags, 'A'); |
break; |
|
default: |
break; |
} |
break; |
default: |
break; |
} |
break; |
default: |
break; |
} |
} |
else { |
out: |
simple_outputchar (out, *format); |
++pc; |
} |
} |
if (out) **out = '\0'; |
return pc; |
} |
|
int simple_printf(char *fmt, ...) |
{ |
va_list ap; |
int r; |
|
va_start(ap, fmt); |
r = simple_vsprintf(NULL, fmt, ap); |
va_end(ap); |
|
return r; |
} |
|
int simple_sprintf(char *buf, char *fmt, ...) |
{ |
va_list ap; |
int r; |
|
va_start(ap, fmt); |
r = simple_vsprintf(&buf, fmt, ap); |
va_end(ap); |
|
return r; |
} |
|
|
#ifdef TEST |
|
#define printf simple_printf |
#define sprintf simple_sprintf |
|
int main(int argc, char *argv[]) |
{ |
static char shortstr[] = "Test"; |
char buf[256]; |
|
printf("percent: \"%%\"\n"); |
printf("bad format: \"%z\"\n"); |
printf("\n"); |
printf("decimal: \"%d\"\n", 12345); |
printf("decimal negative: \"%d\"\n", -2345); |
printf("\n"); |
printf("unsigned: \"%u\"\n", 12345); |
printf("unsigned negative: \"%u\"\n", -2345); |
printf("\n"); |
printf("hex: \"%x\"\n", 0x12345); |
printf("hex negative: \"%x\"\n", -0x12345); |
printf("\n"); |
printf("long decimal: \"%ld\"\n", 123456L); |
printf("long decimal negative: \"%ld\"\n", -23456L); |
printf("\n"); |
printf("long unsigned: \"%lu\"\n", 123456L); |
printf("long unsigned negative: \"%lu\"\n", -123456L); |
printf("\n"); |
printf("long hex: \"%lx\"\n", 0x12345L); |
printf("long hex negative: \"%lx\"\n", -0x12345L); |
printf("\n"); |
printf("long long decimal: \"%lld\"\n", 123456LL); |
printf("long long decimal negative: \"%lld\"\n", -23456LL); |
printf("\n"); |
printf("long long unsigned: \"%llu\"\n", 123456LL); |
printf("long long unsigned negative: \"%llu\"\n", -123456LL); |
printf("\n"); |
printf("long long hex: \"%llx\"\n", 0x12345LL); |
printf("long long hex negative: \"%llx\"\n", -0x12345LL); |
printf("\n"); |
printf("zero-padded LD: \"%010ld\"\n", 123456); |
printf("zero-padded LDN: \"%010ld\"\n", -123456); |
printf("left-adjusted ZLDN: \"%-010ld\"\n", -123456); |
printf("space-padded LDN: \"%10ld\"\n", -123456); |
printf("left-adjusted SLDN: \"%-10ld\"\n", -123456); |
printf("\n"); |
printf("variable pad width: \"%0*d\"\n", 15, -2345); |
printf("\n"); |
printf("char: \"%c%c%c%c\"\n", 'T', 'e', 's', 't'); |
printf("\n"); |
printf("zero-padded string: \"%010s\"\n", shortstr); |
printf("left-adjusted Z string: \"%-010s\"\n", shortstr); |
printf("space-padded string: \"%10s\"\n", shortstr); |
printf("left-adjusted S string: \"%-10s\"\n", shortstr); |
printf("null string: \"%s\"\n", (char *)NULL); |
|
sprintf(buf, "decimal:\t\"%d\"\n", -2345); |
printf("sprintf: %s", buf); |
} |
#endif /* TEST */ |
|
|
/printf.c
168,6 → 168,7
} |
switch (*format) { |
case('d'): |
case('i'): |
u.i = va_arg(ap, int); |
pc += simple_outputi(out, u.i, 10, 1, width, flags, 'a'); |
break; |
/printf.h
60,8 → 60,13
/* Platform must supply this function to output a character */ |
void outbyte(char c); |
|
#include "printf.c" |
//#include "printf.c" |
|
//int simple_printf (__const char *__restrict __format, ...); |
int simple_printf(char *fmt, ...); |
|
int sprintf (char *str, __const char *__restrict __format, ...); |
|
#define printf simple_printf |
#define sprintf simple_sprintf |
|
/sim_fprintf.c
0,0 → 1,230
/* |
* Copyright (c) 2016, Matt Redfearn |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* 1. Redistributions of source code must retain the above copyright notice, |
* this list of conditions and the following disclaimer. |
* |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
* this list of conditions and the following disclaimer in the documentation |
* and/or other materials provided with the distribution. |
* |
* 3. Neither the name of the copyright holder nor the names of its |
* contributors may be used to endorse or promote products derived from this |
* software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
* POSSIBILITY OF SUCH DAMAGE. |
*/ |
|
#include <stddef.h> |
#include <stdarg.h> |
|
#ifdef TEST |
#include <stdio.h> |
#endif /* TEST */ |
|
|
|
static void fsimple_outputchar(char *file_ptr, char c) |
{ |
|
foutbyte(c); |
|
} |
|
enum fflags { |
fPAD_ZERO = 1, |
fPAD_RIGHT = 2, |
}; |
|
static int fprints(char *out, const char *string, int width, int flags) |
{ |
int pc = 0, padchar = ' '; |
|
if (width > 0) { |
int len = 0; |
const char *ptr; |
for (ptr = string; *ptr; ++ptr) ++len; |
if (len >= width) width = 0; |
else width -= len; |
if (flags & fPAD_ZERO) |
padchar = '0'; |
} |
if (!(flags & fPAD_RIGHT)) { |
for ( ; width > 0; --width) { |
fsimple_outputchar(out, padchar); |
++pc; |
} |
} |
for ( ; *string ; ++string) { |
fsimple_outputchar(out, *string); |
++pc; |
} |
for ( ; width > 0; --width) { |
fsimple_outputchar(out, padchar); |
++pc; |
} |
|
return pc; |
} |
|
#define PRINT_BUF_LEN 64 |
|
static int fsimple_outputi(char *out, int i, int base, int sign, int width, int flags, int letbase) |
{ |
char print_buf[PRINT_BUF_LEN]; |
char *s; |
int t, neg = 0, pc = 0; |
unsigned int u = i; |
|
if (i == 0) { |
print_buf[0] = '0'; |
print_buf[1] = '\0'; |
return fprints(out, print_buf, width, flags); |
} |
|
if (sign && base == 10 && i < 0) { |
neg = 1; |
u = -i; |
} |
|
s = print_buf + PRINT_BUF_LEN-1; |
*s = '\0'; |
|
while (u) { |
t = u % base; |
if( t >= 10 ) |
t += letbase - '0' - 10; |
*--s = t + '0'; |
u /= base; |
} |
|
if (neg) { |
if( width && (flags & fPAD_ZERO) ) { |
fsimple_outputchar (out, '-'); |
++pc; |
--width; |
} |
else { |
*--s = '-'; |
} |
} |
|
return pc + fprints (out, s, width, flags); |
} |
|
|
static int fsimple_vsprintf(char *out, char *format, va_list ap) |
{ |
int width, flags; |
int pc = 0; |
char scr[2]; |
union { |
char c; |
char *s; |
int i; |
unsigned int u; |
void *p; |
} u; |
|
for (; *format != 0; ++format) { |
if (*format == '%') { |
++format; |
width = flags = 0; |
if (*format == '\0') |
break; |
if (*format == '%') |
goto out; |
if (*format == '-') { |
++format; |
flags = fPAD_RIGHT; |
} |
while (*format == '0') { |
++format; |
flags |= fPAD_ZERO; |
} |
if (*format == '*') { |
width = va_arg(ap, int); |
format++; |
} else { |
for ( ; *format >= '0' && *format <= '9'; ++format) { |
width *= 10; |
width += *format - '0'; |
} |
} |
switch (*format) { |
case('d'): |
case('i'): |
u.i = va_arg(ap, int); |
pc += fsimple_outputi(out, u.i, 10, 1, width, flags, 'a'); |
break; |
|
case('u'): |
u.u = va_arg(ap, unsigned int); |
pc += fsimple_outputi(out, u.u, 10, 0, width, flags, 'a'); |
break; |
|
case('x'): |
u.u = va_arg(ap, unsigned int); |
pc += fsimple_outputi(out, u.u, 16, 0, width, flags, 'a'); |
break; |
|
case('X'): |
u.u = va_arg(ap, unsigned int); |
pc += fsimple_outputi(out, u.u, 16, 0, width, flags, 'A'); |
break; |
|
case('c'): |
u.c = va_arg(ap, int); |
scr[0] = u.c; |
scr[1] = '\0'; |
pc += fprints(out, scr, width, flags); |
break; |
|
case('s'): |
u.s = va_arg(ap, char *); |
pc += fprints(out, u.s ? u.s : "(null)", width, flags); |
break; |
default: |
break; |
} |
} |
else { |
out: |
fsimple_outputchar (out, *format); |
++pc; |
} |
} |
//if (out) **out = '\0'; |
return pc; |
} |
|
|
|
int simple_fprintf(char *buf, char *fmt, ...) |
{ |
va_list ap; |
int r; |
fout_select(buf); |
va_start(ap, fmt); |
r = fsimple_vsprintf(buf, fmt, ap); |
va_end(ap); |
|
return r; |
} |
|
|
|
/sim_fprintf.h
0,0 → 1,74
/* |
* Copyright (c) 2016, Matt Redfearn |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* 1. Redistributions of source code must retain the above copyright notice, |
* this list of conditions and the following disclaimer. |
* |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
* this list of conditions and the following disclaimer in the documentation |
* and/or other materials provided with the distribution. |
* |
* 3. Neither the name of the copyright holder nor the names of its |
* contributors may be used to endorse or promote products derived from this |
* software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
* POSSIBILITY OF SUCH DAMAGE. |
* |
* |
* This library provides a simple implementation of printf and sprintf. I wrote |
* it because I could not find a good BSD licensed printf that may be built in |
* to proprietary software. |
* |
* The formats specifiers supported by this implementation are: |
* 'd' 'u' 'x' 'X' 'c' 's'. |
* |
* Zero padding and field width are also supported, as well as pad right and |
* variable pad width. |
* |
* To use printf() your program should provide an implementation of putchar() |
* to output a character, for example: |
* |
* void putchar(char c) |
* { |
* while(!SERIAL_PORT_EMPTY); |
* SERIAL_PORT_TX_REGISTER = c; |
* } |
* |
* The code is re-entrant, so it is safe to call from any context, though you |
* will probably get interleaved output. This means that your putchar() |
* function should also be reentrant. |
*/ |
|
|
#ifndef __SIMPLE_FPRINTF__ |
#define __SIMPLE_FPRINTF__ |
|
/* Platform must supply this function to output a character */ |
void foutbyte(char c); |
void fout_select(char * f); |
|
//#include "printf.c" |
|
//int simple_printf (__const char *__restrict __format, ...); |
int simple_fprintf(char *str,char *fmt, ...); |
|
//int fsprintf (char *str, __const char *__restrict __format, ...); |
|
#define fprintf simple_fprintf |
|
|
#endif /* __SIMPLE_FPRINTF__ */ |