00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include <string.h>
00046 #include <stdarg.h>
00047 #include "dsp.h"
00048 #include "dsp_debug.h"
00049 #include "dsp_debug_shared.h"
00050
00051 static int print(int size, char **out, const char *format, va_list args);
00052 static int prints(char **out, const char *string, int width, int pad, int printlimit, Bool IsNumber);
00053 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase, int printlimit);
00054 static void printchar(char **str, int c);
00055
00056
00057 static void printchar(char **str, int c)
00058 {
00059 if (str)
00060 {
00061 **str = c;
00062 ++(*str);
00063 }
00064 }
00065
00066 #define PAD_RIGHT 1
00067 #define PAD_ZERO 2
00068
00069
00070 static int prints(char **out, const char *string, int width, int pad, int printlimit, Bool IsNumber)
00071 {
00072 register int pc = 0, padchar = ' ';
00073 int i, len;
00074 register const char *ptr;
00075
00076 if (width > 0)
00077 {
00078 register int len = 0;
00079 for (ptr = string; *ptr; ++ptr)
00080 ++len;
00081 if (len >= width)
00082 width = 0;
00083 else
00084 width -= len;
00085 if (pad & PAD_ZERO)
00086 padchar = '0';
00087 }
00088 if (!(pad & PAD_RIGHT))
00089 {
00090 for(; width > 0; --width)
00091 {
00092 printchar (out, padchar);
00093 ++pc;
00094 }
00095 }
00096 if (FALSE == IsNumber)
00097 {
00098
00099
00100 for ( ; printlimit && *string ; ++string, --printlimit)
00101 {
00102 printchar (out, *string);
00103 ++pc;
00104 }
00105 }
00106 if (TRUE == IsNumber)
00107 {
00108
00109
00110
00111
00112 len = strlen(string);
00113 if (len < printlimit)
00114 {
00115 i = printlimit - len;
00116 for(; i; i--)
00117 {
00118 printchar (out, '0');
00119 ++pc;
00120 }
00121 }
00122 }
00123
00124
00125 for(; printlimit && *string ; ++string, --printlimit)
00126 {
00127 printchar (out, *string);
00128 ++pc;
00129 }
00130
00131 for(; width > 0; --width)
00132 {
00133 printchar (out, padchar);
00134 ++pc;
00135 }
00136
00137 return pc;
00138 }
00139
00140
00141 #define PRINT_BUF_LEN 12
00142
00143
00144 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase, int printlimit)
00145 {
00146 char print_buf[PRINT_BUF_LEN];
00147 register char *s;
00148 register int t, neg = 0, pc = 0;
00149 register unsigned int u = i;
00150
00151
00152 if (i == 0)
00153 {
00154 print_buf[0] = '0';
00155 print_buf[1] = '\0';
00156 return prints(out, print_buf, width, pad, printlimit, TRUE);
00157 }
00158
00159
00160 if (sg && b == 10 && i < 0)
00161 {
00162 neg = 1;
00163 u = -i;
00164 }
00165
00166 s = print_buf + PRINT_BUF_LEN-1;
00167 *s = '\0';
00168
00169
00170 while (u)
00171 {
00172 t = u % b;
00173 if( t >= 10 )
00174 t += letbase - '0' - 10;
00175 *--s = t + '0';
00176 u /= b;
00177 }
00178
00179
00180 if (neg)
00181 {
00182 if(width && (pad & PAD_ZERO))
00183 {
00184 printchar (out, '-');
00185 ++pc;
00186 --width;
00187 }
00188 else
00189 *--s = '-';
00190 }
00191
00192 return pc + prints(out, s, width, pad, printlimit, TRUE);
00193 }
00194
00195
00196 static int print(int size, char **out, const char *format, va_list args)
00197 {
00198 register int width, pad, printlimit;
00199 register int pc = 0;
00200 char scr[2];
00201
00202 for(; *format != 0; ++format)
00203 {
00204 if (*format == '%')
00205 {
00206 ++format;
00207 width = pad = printlimit = 0;
00208
00209 if (*format == '\0')
00210 break;
00211
00212 if (*format == '%')
00213 goto out;
00214
00215 if (*format == '-')
00216 {
00217 ++format;
00218 pad = PAD_RIGHT;
00219 }
00220
00221 while(*format == '0')
00222 {
00223 ++format;
00224 pad |= PAD_ZERO;
00225 }
00226
00227 for(; *format >= '0' && *format <= '9'; ++format)
00228 {
00229 width *= 10;
00230 width += *format - '0';
00231 }
00232
00233 if (*format == '.')
00234 {
00235 ++format;
00236 for(; *format >= '0' && *format <= '9'; ++format)
00237 {
00238 printlimit *= 10;
00239 printlimit += *format - '0';
00240 }
00241 }
00242 if (0 == printlimit)
00243 printlimit--;
00244
00245 if (*format == 's')
00246 {
00247 register char *s = (char *) va_arg( args, int );
00248 pc += prints(out, s?s:"(null)", width, pad, printlimit, FALSE);
00249 continue;
00250 }
00251
00252 if (*format == 'd' || *format == 'i')
00253 {
00254 pc += printi(out, va_arg( args, int ), 10, 1, width, pad, 'a', printlimit);
00255 continue;
00256 }
00257
00258 if (*format == 'o')
00259 {
00260 pc += printi(out, va_arg( args, int ), 8, 0, width, pad, 'a', printlimit);
00261 continue;
00262 }
00263
00264 if (*format == 'x' )
00265 {
00266 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a', printlimit);
00267 continue;
00268 }
00269
00270 if (*format == 'X')
00271 {
00272 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A', printlimit);
00273 continue;
00274 }
00275
00276 if (*format == 'u')
00277 {
00278 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a', printlimit);
00279 continue;
00280 }
00281
00282 if (*format == 'c')
00283 {
00284
00285 scr[0] = (char) va_arg( args, int );
00286 scr[1] = '\0';
00287 pc += prints (out, scr, width, pad, printlimit, FALSE);
00288 continue;
00289 }
00290
00291 if (*format == 'f')
00292 {
00293 switch(size)
00294 {
00295
00296 case 16:
00297 pc += dsp_debug_sprint(out, 16, DSP16_QA, va_arg(args, int));
00298 continue;
00299
00300 case 32:
00301 pc += dsp_debug_sprint(out, 32, DSP32_QA, va_arg(args, int));
00302 continue;
00303 }
00304 }
00305 }
00306 else
00307 {
00308
00309 out:
00310 printchar (out, *format);
00311 ++pc;
00312 }
00313 }
00314
00315 if (out)
00316 **out = '\0';
00317 va_end( args );
00318 return pc;
00319 }
00320
00321
00322 int dsp16_debug_sprintf(char *out, const char *format, ...)
00323 {
00324 va_list args;
00325 int n;
00326 char *pdsp_debug_buffer;
00327
00328
00329 pdsp_debug_buffer = out;
00330
00331
00332 va_start(args, format);
00333 n = print(16, &pdsp_debug_buffer, format, args);
00334 va_end(args);
00335
00336 return n;
00337 }
00338
00339
00340 int dsp32_debug_sprintf(char *out, const char *format, ...)
00341 {
00342 va_list args;
00343 int n;
00344 char *pdsp_debug_buffer;
00345
00346
00347 pdsp_debug_buffer = out;
00348
00349
00350 va_start(args, format);
00351 n = print(32, &pdsp_debug_buffer, format, args);
00352 va_end(args);
00353
00354 return n;
00355 }
00356
00357
00358 int dsp16_debug_printf(const char *format, ...)
00359 {
00360 va_list args;
00361 int n;
00362 char *pdsp_debug_buffer;
00363
00364
00365 pdsp_debug_buffer = dsp_debug_buffer;
00366
00367
00368 va_start(args, format);
00369 n = print(16, &pdsp_debug_buffer, format, args);
00370 va_end(args);
00371
00372
00373 dsp_debug_print_fct(dsp_debug_buffer);
00374
00375 return n;
00376 }
00377
00378
00379 int dsp32_debug_printf(const char *format, ...)
00380 {
00381 va_list args;
00382 int n;
00383 char *pdsp_debug_buffer;
00384
00385 pdsp_debug_buffer = dsp_debug_buffer;
00386
00387
00388 va_start(args, format);
00389 n = print(32, &pdsp_debug_buffer, format, args);
00390 va_end(args);
00391
00392
00393 dsp_debug_print_fct(dsp_debug_buffer);
00394
00395 return n;
00396 }
00397