OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [boot-loader-ethmac/] [print.c] - Blame information for rev 78

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 61 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  sprintf.c                                                   //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  printf functions for the mini-libc library.                 //
10
//                                                              //
11
//  Author(s):                                                  //
12
//      - Conor Santifort, csantifort.amber@gmail.com           //
13
//                                                              //
14
//////////////////////////////////////////////////////////////////
15
//                                                              //
16
// Copyright (C) 2011 Authors and OPENCORES.ORG                 //
17
//                                                              //
18
// This source file may be used and distributed without         //
19
// restriction provided that this copyright statement is not    //
20
// removed from the file and that any derivative work contains  //
21
// the original copyright notice and the associated disclaimer. //
22
//                                                              //
23
// This source file is free software; you can redistribute it   //
24
// and/or modify it under the terms of the GNU Lesser General   //
25
// Public License as published by the Free Software Foundation; //
26
// either version 2.1 of the License, or (at your option) any   //
27
// later version.                                               //
28
//                                                              //
29
// This source is distributed in the hope that it will be       //
30
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
31
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
32
// PURPOSE.  See the GNU Lesser General Public License for more //
33
// details.                                                     //
34
//                                                              //
35
// You should have received a copy of the GNU Lesser General    //
36
// Public License along with this source; if not, download it   //
37
// from http://www.opencores.org/lgpl.shtml                     //
38
//                                                              //
39
----------------------------------------------------------------*/
40
 
41
#include "stdio.h"
42
 
43
/* Defines */
44
#define PAD_RIGHT 1
45
#define PAD_ZERO  2
46
 
47
/* the following should be enough for 32 bit int */
48
#define PRINT_BUF_LEN 16
49
 
50
 
51 78 csantifort
/* format and print a string, inserting variables, to dst char buffer,
52
   return the number of characters printed. If dst is 0, the string is
53
   sent to the serial output
54
*/
55 61 csantifort
int print(char** dst, const char *format, unsigned long *varg)
56
{
57
    register int width, pad;
58
    register int pc = 0;
59
    char scr[2];
60 78 csantifort
 
61 61 csantifort
    for (; *format != 0; ++format) {
62
       if (*format == '%') {
63
          ++format;
64
          width = pad = 0;
65
          if (*format == '\0') break;
66
          if (*format == '%') goto out;
67
          if (*format == '-') {
68
             ++format;
69
             pad = PAD_RIGHT;
70
          }
71
          while (*format == '0') {
72
             ++format;
73
             pad |= PAD_ZERO;
74
          }
75
          for ( ; *format >= '0' && *format <= '9'; ++format) {
76
             width *= 10;
77
             width += *format - '0';
78
          }
79
          if( *format == 's' ) {
80
             register char *s = *((char **)varg++);
81
             pc += prints (dst, s?s:"(null)", width, pad);
82
             continue;
83
          }
84
          if( *format == 'd' ) {
85
             pc += printi (dst, *varg++, 10, 1, width, pad, 'a');
86
             continue;
87
          }
88
          if( *format == 'x' ) {
89
             pc += printi (dst, *varg++, 16, 0, width, pad, 'a');
90
             continue;
91
          }
92
          if( *format == 'X' ) {
93
             pc += printi (dst, *varg++, 16, 0, width, pad, 'A');
94
             continue;
95
          }
96
          if( *format == 'u' ) {
97
             pc += printi (dst, *varg++, 10, 0, width, pad, 'a');
98
             continue;
99
          }
100
          if( *format == 'c' ) {
101
             /* char are converted to int then pushed on the stack */
102
             scr[0] = *varg++;
103
             scr[1] = '\0';
104
             pc += prints (dst, scr, width, pad);
105
             continue;
106
          }
107
       }
108
       else {
109
       out:
110 78 csantifort
          if (dst)
111
              *(*dst)++ = *format;
112
          else
113
              serial_putchar_(*format);
114 61 csantifort
          ++pc;
115
       }
116
    }
117
 
118
    return pc;
119
}
120
 
121
 
122
/* Print a string - no formatting characters will be interpreted here */
123
int prints(char** dst, const char *string, int width, int pad)
124
{
125
    register int pc = 0, padchar = ' ';
126
 
127 78 csantifort
    if (width > 0) {
128
       register int len = 0;
129
       register const char *ptr;
130
       for (ptr = string; *ptr; ++ptr) ++len;
131
       if (len >= width) width = 0;
132
       else width -= len;
133
       if (pad & PAD_ZERO) padchar = '0';
134
    }
135
    if (!(pad & PAD_RIGHT)) {
136
       for ( ; width > 0; --width) {
137
          if (dst)
138
              *(*dst)++ = padchar;
139
          else
140
              serial_putchar_(padchar);
141
          ++pc;
142
       }
143
    }
144
    for ( ; *string ; ++string) {
145
       if (dst)
146
          *(*dst)++ = *string;
147
       else
148
          serial_putchar_(*string);
149
       ++pc;
150
    }
151
    for ( ; width > 0; --width) {
152
       if (dst)
153 61 csantifort
          *(*dst)++ = padchar;
154 78 csantifort
       else
155
          serial_putchar_(padchar);
156
       ++pc;
157
    }
158 61 csantifort
 
159 78 csantifort
    return pc;
160 61 csantifort
}
161
 
162
 
163
/* Printf an integer */
164
int printi(char** dst, int i, int b, int sg, int width, int pad, int letbase)
165
{
166
    char print_buf[PRINT_BUF_LEN];
167
    char *s;
168
    int t, neg = 0, pc = 0;
169
    unsigned int u = i;
170
 
171
    if (i == 0) {
172
       print_buf[0] = '0';
173
       print_buf[1] = '\0';
174
       return prints (dst, print_buf, width, pad);
175
    }
176
 
177
    if (sg && b == 10 && i < 0) {
178
       neg = 1;
179
       u = -i;
180
    }
181
 
182
    s = print_buf + PRINT_BUF_LEN-1;
183
    *s = '\0';
184
 
185
    while (u) {
186
       if ( b == 16 )    t = u & 0xf;                  /* hex modulous */
187
       else              t = u - ( _div (u, b) * b );  /* Modulous */
188 78 csantifort
 
189 61 csantifort
       if( t >= 10 )
190
          t += letbase - '0' - 10;
191
       *--s = t + '0';
192 78 csantifort
 
193 61 csantifort
    /*   u /= b;  */
194
       if ( b == 16)  u = u >> 4;    /* divide by 16 */
195
       else           u = _div(u, b);
196
    }
197
 
198
    if (neg) {
199
       if( width && (pad & PAD_ZERO) ) {
200 78 csantifort
          if (dst)
201
              *(*dst)++ = '-';
202
          else
203
              serial_putchar_('-');
204 61 csantifort
          ++pc;
205
          --width;
206
       }
207
       else {
208
          *--s = '-';
209
       }
210
    }
211
 
212
    return pc + prints (dst, s, width, pad);
213
}
214
 

powered by: WebSVN 2.1.0

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