OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_processor/] [src_lib/] [simple-printf/] [printf.c] - Blame information for rev 38

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 alirezamon
/*
2
 * Copyright (c) 2016, Matt Redfearn
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 * this list of conditions and the following disclaimer.
10
 *
11
 * 2. Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 *
15
 * 3. Neither the name of the copyright holder nor the names of its
16
 * contributors may be used to endorse or promote products derived from this
17
 * software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
 * POSSIBILITY OF SUCH DAMAGE.
30
 */
31
 
32
#include <stddef.h>
33
#include <stdarg.h>
34
 
35
#ifdef TEST
36
#include <stdio.h>
37
#endif /* TEST */
38
 
39
static void simple_outputchar(char **str, char c)
40
{
41
        if (str) {
42
                **str = c;
43
                ++(*str);
44
        } else {
45
                outbyte(c);
46
        }
47
}
48
 
49
enum flags {
50
        PAD_ZERO        = 1,
51
        PAD_RIGHT       = 2,
52
};
53
 
54
static int prints(char **out, const char *string, int width, int flags)
55
{
56
        int pc = 0, padchar = ' ';
57
 
58
        if (width > 0) {
59
                int len = 0;
60
                const char *ptr;
61
                for (ptr = string; *ptr; ++ptr) ++len;
62
                if (len >= width) width = 0;
63
                else width -= len;
64
                if (flags & PAD_ZERO)
65
                        padchar = '0';
66
        }
67
        if (!(flags & PAD_RIGHT)) {
68
                for ( ; width > 0; --width) {
69
                        simple_outputchar(out, padchar);
70
                        ++pc;
71
                }
72
        }
73
        for ( ; *string ; ++string) {
74
                simple_outputchar(out, *string);
75
                ++pc;
76
        }
77
        for ( ; width > 0; --width) {
78
                simple_outputchar(out, padchar);
79
                ++pc;
80
        }
81
 
82
        return pc;
83
}
84
 
85
#define PRINT_BUF_LEN 64
86
 
87
static int simple_outputi(char **out, int i, int base, int sign, int width, int flags, int letbase)
88
{
89
        char print_buf[PRINT_BUF_LEN];
90
        char *s;
91
        int t, neg = 0, pc = 0;
92
        unsigned int u = i;
93
 
94
        if (i == 0) {
95
                print_buf[0] = '0';
96
                print_buf[1] = '\0';
97
                return prints(out, print_buf, width, flags);
98
        }
99
 
100
        if (sign && base == 10 && i < 0) {
101
                neg = 1;
102
                u = -i;
103
        }
104
 
105
        s = print_buf + PRINT_BUF_LEN-1;
106
        *s = '\0';
107
 
108
        while (u) {
109
                t = u % base;
110
                if( t >= 10 )
111
                        t += letbase - '0' - 10;
112
                *--s = t + '0';
113
                u /= base;
114
        }
115
 
116
        if (neg) {
117
                if( width && (flags & PAD_ZERO) ) {
118
                        simple_outputchar (out, '-');
119
                        ++pc;
120
                        --width;
121
                }
122
                else {
123
                        *--s = '-';
124
                }
125
        }
126
 
127
        return pc + prints (out, s, width, flags);
128
}
129
 
130
 
131
static int simple_vsprintf(char **out, char *format, va_list ap)
132
{
133
        int width, flags;
134
        int pc = 0;
135
        char scr[2];
136
        union {
137
                char c;
138
                char *s;
139
                int i;
140
                unsigned int u;
141
                void *p;
142
        } u;
143
 
144
        for (; *format != 0; ++format) {
145
                if (*format == '%') {
146
                        ++format;
147
                        width = flags = 0;
148
                        if (*format == '\0')
149
                                break;
150
                        if (*format == '%')
151
                                goto out;
152
                        if (*format == '-') {
153
                                ++format;
154
                                flags = PAD_RIGHT;
155
                        }
156
                        while (*format == '0') {
157
                                ++format;
158
                                flags |= PAD_ZERO;
159
                        }
160
                        if (*format == '*') {
161
                                width = va_arg(ap, int);
162
                                format++;
163
                        } else {
164
                                for ( ; *format >= '0' && *format <= '9'; ++format) {
165
                                        width *= 10;
166
                                        width += *format - '0';
167
                                }
168
                        }
169
                        switch (*format) {
170
                                case('d'):
171
                                        u.i = va_arg(ap, int);
172
                                        pc += simple_outputi(out, u.i, 10, 1, width, flags, 'a');
173
                                        break;
174
 
175
                                case('u'):
176
                                        u.u = va_arg(ap, unsigned int);
177
                                        pc += simple_outputi(out, u.u, 10, 0, width, flags, 'a');
178
                                        break;
179
 
180
                                case('x'):
181
                                        u.u = va_arg(ap, unsigned int);
182
                                        pc += simple_outputi(out, u.u, 16, 0, width, flags, 'a');
183
                                        break;
184
 
185
                                case('X'):
186
                                        u.u = va_arg(ap, unsigned int);
187
                                        pc += simple_outputi(out, u.u, 16, 0, width, flags, 'A');
188
                                        break;
189
 
190
                                case('c'):
191
                                        u.c = va_arg(ap, int);
192
                                        scr[0] = u.c;
193
                                        scr[1] = '\0';
194
                                        pc += prints(out, scr, width, flags);
195
                                        break;
196
 
197
                                case('s'):
198
                                        u.s = va_arg(ap, char *);
199
                                        pc += prints(out, u.s ? u.s : "(null)", width, flags);
200
                                        break;
201
                                default:
202
                                        break;
203
                        }
204
                }
205
                else {
206
out:
207
                        simple_outputchar (out, *format);
208
                        ++pc;
209
                }
210
        }
211
        if (out) **out = '\0';
212
        return pc;
213
}
214
 
215
int simple_printf(char *fmt, ...)
216
{
217
        va_list ap;
218
        int r;
219
 
220
        va_start(ap, fmt);
221
        r = simple_vsprintf(NULL, fmt, ap);
222
        va_end(ap);
223
 
224
        return r;
225
}
226
 
227
int simple_sprintf(char *buf, char *fmt, ...)
228
{
229
        va_list ap;
230
        int r;
231
 
232
        va_start(ap, fmt);
233
        r = simple_vsprintf(&buf, fmt, ap);
234
        va_end(ap);
235
 
236
        return r;
237
}
238
 
239
 
240
#ifdef TEST
241
 
242
#define printf simple_printf
243
#define sprintf simple_sprintf
244
 
245
int main(int argc, char *argv[])
246
{
247
        static char shortstr[] = "Test";
248
        char buf[256];
249
 
250
        printf("percent:                \"%%\"\n");
251
        printf("bad format:             \"%z\"\n");
252
        printf("\n");
253
        printf("decimal:                \"%d\"\n", 12345);
254
        printf("decimal negative:       \"%d\"\n", -2345);
255
        printf("\n");
256
        printf("unsigned:               \"%u\"\n", 12345);
257
        printf("unsigned negative:      \"%u\"\n", -2345);
258
        printf("\n");
259
        printf("hex:                    \"%x\"\n", 0x12345);
260
        printf("hex negative:           \"%x\"\n", -0x12345);
261
        printf("\n");
262
        printf("long decimal:           \"%ld\"\n", 123456L);
263
        printf("long decimal negative:  \"%ld\"\n", -23456L);
264
        printf("\n");
265
        printf("long unsigned:          \"%lu\"\n", 123456L);
266
        printf("long unsigned negative: \"%lu\"\n", -123456L);
267
        printf("\n");
268
        printf("long hex:               \"%x\"\n", 0x12345L);
269
        printf("long hex negative:      \"%x\"\n", -0x12345L);
270
        printf("\n");
271
        printf("zero-padded LD:         \"%010ld\"\n", 123456);
272
        printf("zero-padded LDN:        \"%010ld\"\n", -123456);
273
        printf("left-adjusted ZLDN:     \"%-010ld\"\n", -123456);
274
        printf("space-padded LDN:       \"%10ld\"\n", -123456);
275
        printf("left-adjusted SLDN:     \"%-10ld\"\n", -123456);
276
        printf("\n");
277
        printf("variable pad width:     \"%0*d\"\n", 15, -2345);
278
        printf("\n");
279
        printf("char:                   \"%c%c%c%c\"\n", 'T', 'e', 's', 't');
280
        printf("\n");
281
        printf("zero-padded string:     \"%010s\"\n", shortstr);
282
        printf("left-adjusted Z string: \"%-010s\"\n", shortstr);
283
        printf("space-padded string:    \"%10s\"\n", shortstr);
284
        printf("left-adjusted S string: \"%-10s\"\n", shortstr);
285
        printf("null string:            \"%s\"\n", (char *)NULL);
286
 
287
        sprintf(buf, "decimal:\t\"%d\"\n", -2345);
288
        printf("sprintf: %s", buf);
289
}
290
#endif /* TEST */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.