From 2717d451b99c336e87a9e8548969bb57e383cfa5 Mon Sep 17 00:00:00 2001 From: Derek Hensley Date: Thu, 28 Nov 2024 09:36:18 -0800 Subject: [PATCH] Print function mdebug (#90) * xprintf mdebug * xlitob mdebug * xldtob mdebug * sprintf mdebug * syncprintf mdebug * ldouble + remove ultratypes.h from xstdio.h * double only for IDO --- src/libc/sprintf.c | 14 ++--- src/libc/syncprintf.c | 11 ++-- src/libc/xldtob.c | 31 +++++----- src/libc/xlitob.c | 38 ++++++------ src/libc/xprintf.c | 141 +++++++++++++++++++++--------------------- src/libc/xstdio.h | 17 +++-- 6 files changed, 127 insertions(+), 125 deletions(-) diff --git a/src/libc/sprintf.c b/src/libc/sprintf.c index fd95523b..a65fddeb 100644 --- a/src/libc/sprintf.c +++ b/src/libc/sprintf.c @@ -5,18 +5,18 @@ // TODO: this comes from a header #ident "$Revision: 1.23 $" -static char* proutSprintf(char* dst, const char* src, size_t count); +static void* proutSprintf(void* s, const char* buf, size_t n); -int sprintf(char* dst, const char* fmt, ...) { - s32 ans; +int sprintf(char* s, const char* fmt, ...) { + int ans; va_list ap; va_start(ap, fmt); - ans = _Printf(proutSprintf, dst, fmt, ap); + ans = _Printf(proutSprintf, s, fmt, ap); if (ans >= 0) { - dst[ans] = 0; + s[ans] = 0; } return ans; } -static char* proutSprintf(char* dst, const char* src, size_t count) { - return (char*)memcpy((u8*)dst, (u8*)src, count) + count; +static void* proutSprintf(void* s, const char* buf, size_t n) { + return (char*)memcpy(s, buf, n) + n; } diff --git a/src/libc/syncprintf.c b/src/libc/syncprintf.c index 572ac739..75f858ef 100644 --- a/src/libc/syncprintf.c +++ b/src/libc/syncprintf.c @@ -1,5 +1,6 @@ #include "stdarg.h" #include "PR/os.h" +#include "PR/os_internal.h" #include "PR/rdb.h" #include "xstdio.h" #include "PR/rcp.h" @@ -12,7 +13,7 @@ extern void* __printfunc; extern u32 __kmc_pt_mode; static void* proutSyncPrintf(void* str, const char* buf, size_t n) { - size_t sent = 0; + u32 sent = 0; while (sent < n) { sent += __osRdbSend(buf + sent, n - sent, RDB_TYPE_GtoH_PRINT); @@ -25,8 +26,6 @@ static volatile unsigned int* wport = (unsigned*)0xbff08000; static volatile unsigned int* piok = (unsigned*)PHYS_TO_K1(PI_STATUS_REG); static void rmonPutchar(char c) { - u32 data; - while (*piok & (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY)) { } @@ -44,7 +43,7 @@ static void* kmc_proutSyncPrintf(void* str, const char* buf, int n) { char xbuf[128]; static int column = 0; - p = &xbuf; + p = xbuf; for (i = 0; i < n; i++) { c = *buf++; @@ -117,12 +116,12 @@ void rmonPrintf(const char* fmt, ...) { #else -void __osSyncVPrintf(const char* fmt, va_list args) { +void __osSyncVPrintf(const char* fmt, va_list ap) { int ans; #ifndef _FINALROM if (__printfunc != NULL) { - ans = _Printf(__printfunc, NULL, fmt, args); + ans = _Printf(__printfunc, NULL, fmt, ap); } #endif } diff --git a/src/libc/xldtob.c b/src/libc/xldtob.c index 023c8bd4..f291e238 100644 --- a/src/libc/xldtob.c +++ b/src/libc/xldtob.c @@ -9,10 +9,10 @@ #define BUFF_LEN 0x20 -static s16 _Ldunscale(s16* pex, _Pft* px); -static void _Genld(_Pft* px, char code, u8* p, s16 nsig, s16 xexp); +static short _Ldunscale(short* pex, ldouble* px); +static void _Genld(_Pft* px, char code, unsigned char* p, short nsig, short xexp); -static const double pows[] = {10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L, 10e127L, 10e255L}; +static const ldouble pows[] = {10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L, 10e127L, 10e255L}; // float properties #define _D0 0 @@ -41,17 +41,16 @@ static const double pows[] = {10e0L, 10e1L, 10e3L, 10e7L, 10e15L, 10e31L, 10e63L #define _D2 2 #define _D3 3 -#define ALIGN(s, align) (((u32)(s) + ((align)-1)) & ~((align)-1)) +#define ALIGN(s, align) (((unsigned int)(s) + ((align)-1)) & ~((align)-1)) void _Ldtob(_Pft* px, char code) { char buff[BUFF_LEN]; char *p; - f64 ldval; - s16 err; - s16 nsig; - s16 xexp; + ldouble ldval; + short err; + short nsig; + short xexp; - // char unused[0x4]; p = buff; ldval = px->v.ld; @@ -61,7 +60,7 @@ void _Ldtob(_Pft* px, char code) { px->prec = 1; } - err = _Ldunscale(&xexp, px); + err = _Ldunscale(&xexp, &px->v.ld); if (err > 0) { memcpy(px->s, err == 2 ? "NaN" : "Inf", px->n1 = 3); return; @@ -87,7 +86,7 @@ void _Ldtob(_Pft* px, char code) { } } } else if (xexp > 0) { - f64 factor = 1; + ldouble factor = 1; xexp &= ~3; @@ -160,9 +159,9 @@ void _Ldtob(_Pft* px, char code) { _Genld(px, code, p, nsig, xexp); } -s16 _Ldunscale(s16* pex, _Pft* px) { - u16* ps = (u16*)px; - s16 xchar = (ps[_D0] & _DMASK) >> _DOFF; +short _Ldunscale(short* pex, ldouble* px) { + unsigned short* ps = (unsigned short*)px; + short xchar = (ps[_D0] & _DMASK) >> _DOFF; if (xchar == _DMAX) { @@ -181,7 +180,7 @@ s16 _Ldunscale(s16* pex, _Pft* px) { } } -void _Genld(_Pft* px, char code, u8* p, s16 nsig, s16 xexp) { +void _Genld(_Pft* px, char code, unsigned char* p, short nsig, short xexp) { const unsigned char point = '.'; if (nsig <= 0) { @@ -299,7 +298,7 @@ void _Genld(_Pft* px, char code, u8* p, s16 nsig, s16 xexp) { } if ((px->flags & 0x14) == 0x10) { - s32 n = px->n0 + px->n1 + px->nz1 + px->n2 + px->nz2; + int n = px->n0 + px->n1 + px->nz1 + px->n2 + px->nz2; if (n < px->width) { px->nz0 = px->width - n; diff --git a/src/libc/xlitob.c b/src/libc/xlitob.c index 2b27b3a2..608e35f4 100644 --- a/src/libc/xlitob.c +++ b/src/libc/xlitob.c @@ -12,47 +12,47 @@ static char ldigs[] = "0123456789abcdef"; static char udigs[] = "0123456789ABCDEF"; -void _Litob(_Pft *args, char type) { +void _Litob(_Pft *px, char code) { char buff[BUFF_LEN]; const char *digs; - s32 base; - s32 i; + int base; + int i; unsigned long long ullval; - digs = (type == 'X') ? udigs : ldigs; + digs = (code == 'X') ? udigs : ldigs; - base = (type == 'o') ? 8 : ((type != 'x' && type != 'X') ? 10 : 16); + base = (code == 'o') ? 8 : ((code != 'x' && code != 'X') ? 10 : 16); i = BUFF_LEN; - ullval = args->v.ll; + ullval = px->v.ll; - if ((type == 'd' || type == 'i') && args->v.ll < 0) { + if ((code == 'd' || code == 'i') && px->v.ll < 0) { ullval = -ullval; } - if (ullval != 0 || args->prec != 0) { + if (ullval != 0 || px->prec != 0) { buff[--i] = digs[ullval % base]; } - args->v.ll = ullval / base; + px->v.ll = ullval / base; - while (args->v.ll > 0 && i > 0) { - lldiv_t qr = lldiv(args->v.ll, base); + while (px->v.ll > 0 && i > 0) { + lldiv_t qr = lldiv(px->v.ll, base); - args->v.ll = qr.quot; + px->v.ll = qr.quot; buff[--i] = digs[qr.rem]; } - args->n1 = BUFF_LEN - i; + px->n1 = BUFF_LEN - i; - memcpy(args->s, buff + i, args->n1); + memcpy(px->s, buff + i, px->n1); - if (args->n1 < args->prec) { - args->nz0 = args->prec - args->n1; + if (px->n1 < px->prec) { + px->nz0 = px->prec - px->n1; } - if (args->prec < 0 && (args->flags & (FLAGS_ZERO | FLAGS_MINUS)) == FLAGS_ZERO) { - if ((i = args->width - args->n0 - args->nz0 - args->n1) > 0) { - args->nz0 += i; + if (px->prec < 0 && (px->flags & (FLAGS_ZERO | FLAGS_MINUS)) == FLAGS_ZERO) { + if ((i = px->width - px->n0 - px->nz0 - px->n1) > 0) { + px->nz0 += i; } } } diff --git a/src/libc/xprintf.c b/src/libc/xprintf.c index f12d6b35..9191e1ce 100644 --- a/src/libc/xprintf.c +++ b/src/libc/xprintf.c @@ -32,7 +32,7 @@ #define PUT(s, n) \ if (0 < (n)) \ { \ - if ((arg = (*prout)(arg, s, n)) != NULL) \ + if ((arg = (*pfn)(arg, s, n)) != NULL) \ x.nchar += (n); \ else \ return x.nchar; \ @@ -40,19 +40,19 @@ static char spaces[] = " "; static char zeroes[] = "00000000000000000000000000000000"; -static void _Putfld(_Pft *pf, va_list *pap, char code, char *ac); +static void _Putfld(_Pft *px, va_list *pap, char code, char *ac); -int _Printf(outfun prout, char *arg, const char *fmt, va_list args) { +int _Printf(void* pfn(void*,const char*,size_t), void *arg, const char *fmt, va_list ap) { _Pft x; x.nchar = 0; - while (TRUE) { + while (1) { const char *s; char c; const char *t; static const char fchar[] = {' ', '+', '-', '#', '0', '\0'}; - static const int fbit[] = {FLAGS_SPACE, FLAGS_PLUS, FLAGS_MINUS, FLAGS_HASH, FLAGS_ZERO, 0}; + static const unsigned int fbit[] = {FLAGS_SPACE, FLAGS_PLUS, FLAGS_MINUS, FLAGS_HASH, FLAGS_ZERO, 0}; char ac[32]; s = fmt; @@ -73,7 +73,7 @@ int _Printf(outfun prout, char *arg, const char *fmt, va_list args) { } if (*s == '*') { - x.width = va_arg(args, int); + x.width = va_arg(ap, int); if (x.width < 0) { x.width = -x.width; @@ -87,7 +87,7 @@ int _Printf(outfun prout, char *arg, const char *fmt, va_list args) { if (*s != '.') { x.prec = -1; } else if (*++s == '*') { - x.prec = va_arg(args, int); + x.prec = va_arg(ap, int); ++s; } else for (x.prec = 0; isdigit(*s); s++) { @@ -103,7 +103,7 @@ int _Printf(outfun prout, char *arg, const char *fmt, va_list args) { ++s; } - _Putfld(&x, &args, *s, ac); + _Putfld(&x, &ap, *s, ac); x.width -= x.n0 + x.nz0 + x.n1 + x.nz1 + x.n2 + x.nz2; { @@ -139,119 +139,118 @@ int _Printf(outfun prout, char *arg, const char *fmt, va_list args) { return 0; } -static void _Putfld(_Pft *x, va_list *args, char type, char *buff) { - x->n0 = x->nz0 = x->n1 = x->nz1 = x->n2 = - x->nz2 = 0; +static void _Putfld(_Pft *px, va_list *pap, char code, char *ac) { + px->n0 = px->nz0 = px->n1 = px->nz1 = px->n2 = + px->nz2 = 0; - switch (type) { + switch (code) { case 'c': - buff[x->n0++] = va_arg(*args, int); + ac[px->n0++] = va_arg(*pap, int); break; case 'd': case 'i': - if (x->qual == 'l') { - x->v.ll = va_arg(*args, int); - } else if (x->qual == 'L') { - x->v.ll = va_arg(*args, s64); + if (px->qual == 'l') { + px->v.ll = va_arg(*pap, long); + } else if (px->qual == 'L') { + px->v.ll = va_arg(*pap, long long); } else { - x->v.ll = va_arg(*args, int); + px->v.ll = va_arg(*pap, int); } - if (x->qual == 'h') { - x->v.ll = (s16)x->v.ll; + if (px->qual == 'h') { + px->v.ll = (short)px->v.ll; } - if (x->v.ll < 0) { - buff[x->n0++] = '-'; - } else if (x->flags & FLAGS_PLUS) { - buff[x->n0++] = '+'; - } else if (x->flags & FLAGS_SPACE) { - buff[x->n0++] = ' '; + if (px->v.ll < 0) { + ac[px->n0++] = '-'; + } else if (px->flags & FLAGS_PLUS) { + ac[px->n0++] = '+'; + } else if (px->flags & FLAGS_SPACE) { + ac[px->n0++] = ' '; } - x->s = (char *)&buff[x->n0]; + px->s = (char *)&ac[px->n0]; - _Litob(x, type); + _Litob(px, code); break; case 'x': case 'X': case 'u': case 'o': - if (x->qual == 'l') { - x->v.ll = va_arg(*args, int); - } else if (x->qual == 'L') { - x->v.ll = va_arg(*args, s64); + if (px->qual == 'l') { + px->v.ll = va_arg(*pap, long); + } else if (px->qual == 'L') { + px->v.ll = va_arg(*pap, long long); } else { - x->v.ll = va_arg(*args, int); + px->v.ll = va_arg(*pap, int); } - if (x->qual == 'h') { - x->v.ll = (u16)x->v.ll; - } else if (x->qual == 0) { - x->v.ll = (unsigned int)x->v.ll; + if (px->qual == 'h') { + px->v.ll = (unsigned short)px->v.ll; + } else if (px->qual == 0) { + px->v.ll = (unsigned int)px->v.ll; } - if (x->flags & FLAGS_HASH) { - buff[x->n0++] = '0'; + if (px->flags & FLAGS_HASH) { + ac[px->n0++] = '0'; - if (type == 'x' || type == 'X') { - buff[x->n0++] = type; + if (code == 'x' || code == 'X') { + ac[px->n0++] = code; } } - x->s = (char *)&buff[x->n0]; - _Litob(x, type); + px->s = (char *)&ac[px->n0]; + _Litob(px, code); break; case 'e': case 'f': case 'g': case 'E': case 'G': - //... okay? - x->v.ld = x->qual == 'L' ? va_arg(*args, f64) : va_arg(*args, f64); - - if (LDSIGN(x->v.ld)) - buff[x->n0++] = '-'; - else if (x->flags & FLAGS_PLUS) - buff[x->n0++] = '+'; - else if (x->flags & FLAGS_SPACE) - buff[x->n0++] = ' '; - - x->s = (char *)&buff[x->n0]; - _Ldtob(x, type); + px->v.ld = px->qual == 'L' ? va_arg(*pap, ldouble) : va_arg(*pap, double); + + if (LDSIGN(px->v.ld)) + ac[px->n0++] = '-'; + else if (px->flags & FLAGS_PLUS) + ac[px->n0++] = '+'; + else if (px->flags & FLAGS_SPACE) + ac[px->n0++] = ' '; + + px->s = (char *)&ac[px->n0]; + _Ldtob(px, code); break; case 'n': - if (x->qual == 'h') { - *(va_arg(*args, u16 *)) = x->nchar; - } else if (x->qual == 'l') { - *va_arg(*args, unsigned int *) = x->nchar; - } else if (x->qual == 'L') { - *va_arg(*args, u64 *) = x->nchar; + if (px->qual == 'h') { + *va_arg(*pap, unsigned short *) = px->nchar; + } else if (px->qual == 'l') { + *va_arg(*pap, unsigned long *) = px->nchar; + } else if (px->qual == 'L') { + *va_arg(*pap, unsigned long long *) = px->nchar; } else { - *va_arg(*args, unsigned int *) = x->nchar; + *va_arg(*pap, unsigned int *) = px->nchar; } break; case 'p': - x->v.ll = (long)va_arg(*args, void *); - x->s = (char *)&buff[x->n0]; - _Litob(x, 'x'); + px->v.ll = (long)va_arg(*pap, void *); + px->s = (char *)&ac[px->n0]; + _Litob(px, 'x'); break; case 's': - x->s = va_arg(*args, char *); - x->n1 = strlen(x->s); + px->s = va_arg(*pap, char *); + px->n1 = strlen(px->s); - if (x->prec >= 0 && x->prec < x->n1) { - x->n1 = x->prec; + if (px->prec >= 0 && px->prec < px->n1) { + px->n1 = px->prec; } break; case '%': - buff[x->n0++] = '%'; + ac[px->n0++] = '%'; break; default: - buff[x->n0++] = type; + ac[px->n0++] = code; break; } } diff --git a/src/libc/xstdio.h b/src/libc/xstdio.h index bd70de8a..fd551778 100644 --- a/src/libc/xstdio.h +++ b/src/libc/xstdio.h @@ -1,13 +1,19 @@ #ifndef _XSTDIO_H #define _XSTDIO_H -#include "PR/ultratypes.h" + #include "stdlib.h" #include "stdarg.h" +#ifdef __sgi +typedef double ldouble; // IDO does not support long doubles +#else +typedef long double ldouble; +#endif + typedef struct { /* 0x0 */ union { /* 0x0 */ long long ll; - /* 0x0 */ double ld; + /* 0x0 */ ldouble ld; } v; /* 0x8 */ unsigned char* s; /* 0xC */ int n0; @@ -28,10 +34,9 @@ typedef struct { #define FLAGS_MINUS 4 #define FLAGS_HASH 8 #define FLAGS_ZERO 16 -typedef char *outfun(char*,const char*,size_t); -int _Printf(outfun prout, char *arg, const char *fmt, va_list args); -void _Litob(_Pft *args, char type); -void _Ldtob(_Pft* args, char type); +int _Printf(void* pfn(void*,const char*,size_t), void *arg, const char *fmt, va_list ap); +void _Litob(_Pft *px, char code); +void _Ldtob(_Pft* px, char code); #endif