1 /* Copyright (c) 1995-2013 Xilinx, Inc. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
7 * 1. Redistributions source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Xilinx nor the names of its contributors may be
15 * used to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
19 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 extern void outbyte (char);
37 /*----------------------------------------------------*/
38 /* Use the following parameter passing structure to */
39 /* make xil_printf re-entrant. */
40 /*----------------------------------------------------*/
41 typedef struct params_s
{
50 /*---------------------------------------------------*/
51 /* The purpose of this routine is to output data the */
52 /* same as the standard printf function without the */
53 /* overhead most run-time libraries involve. Usually */
54 /* the printf brings in many kilobytes of code and */
55 /* that is unacceptable in most embedded systems. */
56 /*---------------------------------------------------*/
58 typedef char* charptr
;
59 typedef int (*func_ptr
)(int c
);
61 /*---------------------------------------------------*/
63 /* This routine puts pad characters into the output */
66 static void padding( const int l_flag
, params_t
*par
)
70 if (par
->do_padding
&& l_flag
&& (par
->len
< par
->num1
))
71 for (i
=par
->len
; i
<par
->num1
; i
++)
72 outbyte( par
->pad_character
);
75 /*---------------------------------------------------*/
77 /* This routine moves a string to the output buffer */
78 /* as directed by the padding and positioning flags. */
80 static void outs( charptr lp
, params_t
*par
)
82 /* pad on left if needed */
83 par
->len
= strlen( lp
);
84 padding( !(par
->left_flag
), par
);
86 /* Move string to the buffer */
87 while (*lp
&& (par
->num2
)--)
90 /* Pad on right if needed */
91 /* CR 439175 - elided next stmt. Seemed bogus. */
92 /* par->len = strlen( lp); */
93 padding( par
->left_flag
, par
);
96 /*---------------------------------------------------*/
98 /* This routine moves a number to the output buffer */
99 /* as directed by the padding and positioning flags. */
102 static void outnum( const long n
, const long base
, params_t
*par
)
107 const char digits
[] = "0123456789ABCDEF";
110 /* Check if number is negative */
111 if (base
== 10 && n
< 0L) {
120 /* Build number (backwards) in outbuf */
123 *cp
++ = digits
[(int)(num
% base
)];
124 } while ((num
/= base
) > 0);
129 /* Move the converted number to the buffer and */
130 /* add in the padding where needed. */
131 par
->len
= strlen(outbuf
);
132 padding( !(par
->left_flag
), par
);
135 padding( par
->left_flag
, par
);
138 /*---------------------------------------------------*/
140 /* This routine gets a number from the format */
143 static int getnum( charptr
* linep
)
151 n
= n
*10 + ((*cp
++) - '0');
156 /*---------------------------------------------------*/
158 /* This routine operates just like a printf/sprintf */
159 /* routine. It outputs a set of data under the */
160 /* control of a formatting string. Not all of the */
161 /* standard C format control are supported. The ones */
162 /* provided are primarily those needed for embedded */
163 /* systems work. Primarily the floaing point */
164 /* routines are omitted. Other formats could be */
165 /* added easily by following the examples shown for */
166 /* the supported formats. */
169 /* void esp_printf( const func_ptr f_ptr,
170 const charptr ctrl1, ...) */
171 void xil_printf( const charptr ctrl1
, ...)
181 charptr ctrl
= ctrl1
;
183 va_start( argp
, ctrl1
);
185 for ( ; *ctrl
; ctrl
++) {
187 /* move format string chars to buffer until a */
188 /* format control is found. */
194 /* initialize all the flags for this format. */
195 dot_flag
= long_flag
= par
.left_flag
= par
.do_padding
= 0;
196 par
.pad_character
= ' ';
204 par
.num2
= getnum(&ctrl
);
207 par
.pad_character
= '0';
209 par
.num1
= getnum(&ctrl
);
216 switch (tolower(ch
)) {
234 if (long_flag
|| ch
== 'D') {
235 outnum( va_arg(argp
, long), 10L, &par
);
239 outnum( va_arg(argp
, int), 10L, &par
);
243 outnum((long)va_arg(argp
, int), 16L, &par
);
247 outs( va_arg( argp
, charptr
), &par
);
251 outbyte( va_arg( argp
, int));
284 /*---------------------------------------------------*/