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/] [sim_fprintf.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 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
 
40
 
41
static void fsimple_outputchar(char  *file_ptr, char c)
42
{
43
 
44
                foutbyte(c);
45
 
46
}
47
 
48
enum fflags {
49
        fPAD_ZERO       = 1,
50
        fPAD_RIGHT      = 2,
51
};
52
 
53
static int fprints(char *out, const char *string, int width, int flags)
54
{
55
        int pc = 0, padchar = ' ';
56
 
57
        if (width > 0) {
58
                int len = 0;
59
                const char *ptr;
60
                for (ptr = string; *ptr; ++ptr) ++len;
61
                if (len >= width) width = 0;
62
                else width -= len;
63
                if (flags & fPAD_ZERO)
64
                        padchar = '0';
65
        }
66
        if (!(flags & fPAD_RIGHT)) {
67
                for ( ; width > 0; --width) {
68
                        fsimple_outputchar(out, padchar);
69
                        ++pc;
70
                }
71
        }
72
        for ( ; *string ; ++string) {
73
                fsimple_outputchar(out, *string);
74
                ++pc;
75
        }
76
        for ( ; width > 0; --width) {
77
                fsimple_outputchar(out, padchar);
78
                ++pc;
79
        }
80
 
81
        return pc;
82
}
83
 
84
#define PRINT_BUF_LEN 64
85
 
86
static int fsimple_outputi(char *out, int i, int base, int sign, int width, int flags, int letbase)
87
{
88
        char print_buf[PRINT_BUF_LEN];
89
        char *s;
90
        int t, neg = 0, pc = 0;
91
        unsigned int u = i;
92
 
93
        if (i == 0) {
94
                print_buf[0] = '0';
95
                print_buf[1] = '\0';
96
                return fprints(out, print_buf, width, flags);
97
        }
98
 
99
        if (sign && base == 10 && i < 0) {
100
                neg = 1;
101
                u = -i;
102
        }
103
 
104
        s = print_buf + PRINT_BUF_LEN-1;
105
        *s = '\0';
106
 
107
        while (u) {
108
                t = u % base;
109
                if( t >= 10 )
110
                        t += letbase - '0' - 10;
111
                *--s = t + '0';
112
                u /= base;
113
        }
114
 
115
        if (neg) {
116
                if( width && (flags & fPAD_ZERO) ) {
117
                        fsimple_outputchar (out, '-');
118
                        ++pc;
119
                        --width;
120
                }
121
                else {
122
                        *--s = '-';
123
                }
124
        }
125
 
126
        return pc + fprints (out, s, width, flags);
127
}
128
 
129
 
130
static int fsimple_vsprintf(char *out, char *format, va_list ap)
131
{
132
        int width, flags;
133
        int pc = 0;
134
        char scr[2];
135
        union {
136
                char c;
137
                char *s;
138
                int i;
139
                unsigned int u;
140
                void *p;
141
        } u;
142
 
143
        for (; *format != 0; ++format) {
144
                if (*format == '%') {
145
                        ++format;
146
                        width = flags = 0;
147
                        if (*format == '\0')
148
                                break;
149
                        if (*format == '%')
150
                                goto out;
151
                        if (*format == '-') {
152
                                ++format;
153
                                flags = fPAD_RIGHT;
154
                        }
155
                        while (*format == '0') {
156
                                ++format;
157
                                flags |= fPAD_ZERO;
158
                        }
159
                        if (*format == '*') {
160
                                width = va_arg(ap, int);
161
                                format++;
162
                        } else {
163
                                for ( ; *format >= '0' && *format <= '9'; ++format) {
164
                                        width *= 10;
165
                                        width += *format - '0';
166
                                }
167
                        }
168
                        switch (*format) {
169
                                case('d'):
170
                                case('i'):
171
                                        u.i = va_arg(ap, int);
172
                                        pc += fsimple_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 += fsimple_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 += fsimple_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 += fsimple_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 += fprints(out, scr, width, flags);
195
                                        break;
196
 
197
                                case('s'):
198
                                        u.s = va_arg(ap, char *);
199
                                        pc += fprints(out, u.s ? u.s : "(null)", width, flags);
200
                                        break;
201
                                default:
202
                                        break;
203
                        }
204
                }
205
                else {
206
out:
207
                        fsimple_outputchar (out, *format);
208
                        ++pc;
209
                }
210
        }
211
        //if (out) **out = '\0';
212
        return pc;
213
}
214
 
215
 
216
 
217
int simple_fprintf(char *buf, char *fmt, ...)
218
{
219
        va_list ap;
220
        int r;
221
        fout_select(buf);
222
        va_start(ap, fmt);
223
        r = fsimple_vsprintf(buf, fmt, ap);
224
        va_end(ap);
225
 
226
        return r;
227
}
228
 
229
 
230
 

powered by: WebSVN 2.1.0

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