1 /*---------------------------------------------------*/
3 /* Public Domain version of printf */
4 /* Rud Merriam, Compsult, Inc. Houston, Tx. */
5 /* For Embedded Systems Programming, 1991 */
7 /*---------------------------------------------------*/
8 #include "xil_printf.h"
10 #include "xil_assert.h"
15 static void padding( const s32 l_flag,const struct params_s *par);
16 static void outs(const charptr lp, struct params_s *par);
17 static s32 getnum( charptr* linep);
19 typedef struct params_s {
30 /*---------------------------------------------------*/
31 /* The purpose of this routine is to output data the */
32 /* same as the standard printf function without the */
33 /* overhead most run-time libraries involve. Usually */
34 /* the printf brings in many kilobytes of code and */
35 /* that is unacceptable in most embedded systems. */
36 /*---------------------------------------------------*/
39 /*---------------------------------------------------*/
41 /* This routine puts pad characters into the output */
44 static void padding( const s32 l_flag, const struct params_s *par)
48 if ((par->do_padding != 0) && (l_flag != 0) && (par->len < par->num1)) {
50 for (; i<(par->num1); i++) {
51 #ifdef STDOUT_BASEADDRESS
52 outbyte( par->pad_character);
58 /*---------------------------------------------------*/
60 /* This routine moves a string to the output buffer */
61 /* as directed by the padding and positioning flags. */
63 static void outs(const charptr lp, struct params_s *par)
67 /* pad on left if needed */
68 if(LocalPtr != NULL) {
69 par->len = (s32)strlen( LocalPtr);
71 padding( !(par->left_flag), par);
73 /* Move string to the buffer */
74 while (((*LocalPtr) != (char8)0) && ((par->num2) != 0)) {
76 #ifdef STDOUT_BASEADDRESS
82 /* Pad on right if needed */
83 /* CR 439175 - elided next stmt. Seemed bogus. */
84 /* par->len = strlen( lp) */
85 padding( par->left_flag, par);
88 /*---------------------------------------------------*/
90 /* This routine moves a number to the output buffer */
91 /* as directed by the padding and positioning flags. */
94 static void outnum( const s32 n, const s32 base, struct params_s *par)
100 const char8 digits[] = "0123456789ABCDEF";
102 for(i = 0; i<32; i++) {
106 /* Check if number is negative */
107 if ((par->unsigned_flag == 0) && (base == 10) && (n < 0L)) {
116 /* Build number (backwards) in outbuf */
119 outbuf[i] = digits[(num % base)];
132 /* Move the converted number to the buffer and */
133 /* add in the padding where needed. */
134 par->len = (s32)strlen(outbuf);
135 padding( !(par->left_flag), par);
136 while (&outbuf[i] >= outbuf) {
137 #ifdef STDOUT_BASEADDRESS
138 outbyte( outbuf[i] );
142 padding( par->left_flag, par);
145 /*---------------------------------------------------*/
147 /* This routine gets a number from the format */
150 static s32 getnum( charptr* linep)
153 s32 ResultIsDigit = 0;
158 ResultIsDigit = isdigit(((s32)*cptr));
160 while (ResultIsDigit != 0) {
162 n = ((n*10) + (((s32)*cptr) - (s32)'0'));
165 ResultIsDigit = isdigit(((s32)*cptr));
168 ResultIsDigit = isdigit(((s32)*cptr));
170 *linep = ((charptr )(cptr));
174 /*---------------------------------------------------*/
176 /* This routine operates just like a printf/sprintf */
177 /* routine. It outputs a set of data under the */
178 /* control of a formatting string. Not all of the */
179 /* standard C format control are supported. The ones */
180 /* provided are primarily those needed for embedded */
181 /* systems work. Primarily the floating point */
182 /* routines are omitted. Other formats could be */
183 /* added easily by following the examples shown for */
184 /* the supported formats. */
187 /* void esp_printf( const func_ptr f_ptr,
188 const charptr ctrl1, ...) */
189 void xil_printf( const char8 *ctrl1, ...)
199 char8 *ctrl = (char8 *)ctrl1;
201 va_start( argp, ctrl1);
203 while ((ctrl != NULL) && (*ctrl != (char8)0)) {
205 /* move format string chars to buffer until a */
206 /* format control is found. */
208 #ifdef STDOUT_BASEADDRESS
215 /* initialize all the flags for this format. */
218 par.unsigned_flag = 0;
221 par.pad_character = ' ';
237 if (isdigit((s32)ch) != 0) {
239 par.num2 = getnum(&ctrl);
243 par.pad_character = '0';
246 par.num1 = getnum(&ctrl);
256 switch (tolower((s32)ch)) {
258 #ifdef STDOUT_BASEADDRESS
280 par.unsigned_flag = 1;
284 if ((long_flag != 0) || (ch == 'D')) {
285 outnum( va_arg(argp, s32), 10L, &par);
288 outnum( va_arg(argp, s32), 10L, &par);
295 par.unsigned_flag = 1;
296 outnum((s32)va_arg(argp, s32), 16L, &par);
301 outs( va_arg( argp, char *), &par);
306 #ifdef STDOUT_BASEADDRESS
307 outbyte( va_arg( argp, s32));
315 #ifdef STDOUT_BASEADDRESS
316 outbyte( ((char8)0x07));
320 #ifdef STDOUT_BASEADDRESS
321 outbyte( ((char8)0x08));
325 #ifdef STDOUT_BASEADDRESS
326 outbyte( ((char8)0x0D));
330 #ifdef STDOUT_BASEADDRESS
331 outbyte( ((char8)0x0D));
332 outbyte( ((char8)0x0A));
336 #ifdef STDOUT_BASEADDRESS
360 /*---------------------------------------------------*/