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"
14 typedef struct params_s {
24 /*---------------------------------------------------*/
25 /* The purpose of this routine is to output data the */
26 /* same as the standard printf function without the */
27 /* overhead most run-time libraries involve. Usually */
28 /* the printf brings in many kilobytes of code and */
29 /* that is unacceptable in most embedded systems. */
30 /*---------------------------------------------------*/
33 /*---------------------------------------------------*/
35 /* This routine puts pad characters into the output */
38 void padding( const int l_flag, params_t *par)
42 if (par->do_padding && l_flag && (par->len < par->num1))
43 for (i=par->len; i<par->num1; i++) {
44 #ifdef STDOUT_BASEADDRESS
45 outbyte( par->pad_character);
50 /*---------------------------------------------------*/
52 /* This routine moves a string to the output buffer */
53 /* as directed by the padding and positioning flags. */
55 void outs( charptr lp, params_t *par)
57 /* pad on left if needed */
58 par->len = strlen( lp);
59 padding( !(par->left_flag), par);
61 /* Move string to the buffer */
62 while (*lp && (par->num2)--) {
63 #ifdef STDOUT_BASEADDRESS
68 /* Pad on right if needed */
69 /* CR 439175 - elided next stmt. Seemed bogus. */
70 /* par->len = strlen( lp); */
71 padding( par->left_flag, par);
74 /*---------------------------------------------------*/
76 /* This routine moves a number to the output buffer */
77 /* as directed by the padding and positioning flags. */
80 void outnum( const long n, const long base, params_t *par)
85 const char digits[] = "0123456789ABCDEF";
88 /* Check if number is negative */
89 if (base == 10 && n < 0L) {
98 /* Build number (backwards) in outbuf */
101 *cp++ = digits[(int)(num % base)];
102 } while ((num /= base) > 0);
107 /* Move the converted number to the buffer and */
108 /* add in the padding where needed. */
109 par->len = strlen(outbuf);
110 padding( !(par->left_flag), par);
111 while (cp >= outbuf) {
112 #ifdef STDOUT_BASEADDRESS
116 padding( par->left_flag, par);
119 /*---------------------------------------------------*/
121 /* This routine gets a number from the format */
124 int getnum( charptr* linep)
131 while (isdigit(((int)*cp)))
132 n = n*10 + ((*cp++) - '0');
137 /*---------------------------------------------------*/
139 /* This routine operates just like a printf/sprintf */
140 /* routine. It outputs a set of data under the */
141 /* control of a formatting string. Not all of the */
142 /* standard C format control are supported. The ones */
143 /* provided are primarily those needed for embedded */
144 /* systems work. Primarily the floaing point */
145 /* routines are omitted. Other formats could be */
146 /* added easily by following the examples shown for */
147 /* the supported formats. */
150 /* void esp_printf( const func_ptr f_ptr,
151 const charptr ctrl1, ...) */
152 void xil_printf( const char *ctrl1, ...)
162 char *ctrl = (char *)ctrl1;
164 va_start( argp, ctrl1);
166 for ( ; *ctrl; ctrl++) {
168 /* move format string chars to buffer until a */
169 /* format control is found. */
171 #ifdef STDOUT_BASEADDRESS
177 /* initialize all the flags for this format. */
178 dot_flag = long_flag = par.left_flag = par.do_padding = 0;
179 par.pad_character = ' ';
185 if (isdigit((int)ch)) {
187 par.num2 = getnum(&ctrl);
190 par.pad_character = '0';
192 par.num1 = getnum(&ctrl);
199 switch (tolower((int)ch)) {
201 #ifdef STDOUT_BASEADDRESS
219 if (long_flag || ch == 'D') {
220 outnum( va_arg(argp, long), 10L, &par);
224 outnum( va_arg(argp, int), 10L, &par);
228 outnum((long)va_arg(argp, int), 16L, &par);
232 outs( va_arg( argp, char *), &par);
236 #ifdef STDOUT_BASEADDRESS
237 outbyte( va_arg( argp, int));
244 #ifdef STDOUT_BASEADDRESS
249 #ifdef STDOUT_BASEADDRESS
254 #ifdef STDOUT_BASEADDRESS
259 #ifdef STDOUT_BASEADDRESS
265 #ifdef STDOUT_BASEADDRESS
281 /*---------------------------------------------------*/