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

Subversion Repositories amber

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

Go to most recent revision | 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
 
52
int print(char** dst, const char *format, unsigned long *varg)
53
{
54
    register int width, pad;
55
    register int pc = 0;
56
    char scr[2];
57
 
58
    for (; *format != 0; ++format) {
59
       if (*format == '%') {
60
          ++format;
61
          width = pad = 0;
62
          if (*format == '\0') break;
63
          if (*format == '%') goto out;
64
          if (*format == '-') {
65
             ++format;
66
             pad = PAD_RIGHT;
67
          }
68
          while (*format == '0') {
69
             ++format;
70
             pad |= PAD_ZERO;
71
          }
72
          for ( ; *format >= '0' && *format <= '9'; ++format) {
73
             width *= 10;
74
             width += *format - '0';
75
          }
76
          if( *format == 's' ) {
77
             register char *s = *((char **)varg++);
78
             pc += prints (dst, s?s:"(null)", width, pad);
79
             continue;
80
          }
81
          if( *format == 'd' ) {
82
             pc += printi (dst, *varg++, 10, 1, width, pad, 'a');
83
             continue;
84
          }
85
          if( *format == 'x' ) {
86
             pc += printi (dst, *varg++, 16, 0, width, pad, 'a');
87
             continue;
88
          }
89
          if( *format == 'X' ) {
90
             pc += printi (dst, *varg++, 16, 0, width, pad, 'A');
91
             continue;
92
          }
93
          if( *format == 'u' ) {
94
             pc += printi (dst, *varg++, 10, 0, width, pad, 'a');
95
             continue;
96
          }
97
          if( *format == 'c' ) {
98
             /* char are converted to int then pushed on the stack */
99
             scr[0] = *varg++;
100
             scr[1] = '\0';
101
             pc += prints (dst, scr, width, pad);
102
             continue;
103
          }
104
       }
105
       else {
106
       out:
107
          //outbyte (dst, *format);
108
          *(*dst)++ = *format;
109
          ++pc;
110
       }
111
    }
112
 
113
    return pc;
114
}
115
 
116
 
117
/* Print a string - no formatting characters will be interpreted here */
118
int prints(char** dst, const char *string, int width, int pad)
119
{
120
    register int pc = 0, padchar = ' ';
121
 
122
    if (width > 0) {
123
       register int len = 0;
124
       register const char *ptr;
125
       for (ptr = string; *ptr; ++ptr) ++len;
126
       if (len >= width) width = 0;
127
       else width -= len;
128
       if (pad & PAD_ZERO) padchar = '0';
129
    }
130
    if (!(pad & PAD_RIGHT)) {
131
       for ( ; width > 0; --width) {
132
          //outbyte(dst, padchar);              
133
          *(*dst)++ = padchar;
134
          ++pc;
135
       }
136
    }
137
    for ( ; *string ; ++string) {
138
       //outbyte(dst, *string);                 
139
       *(*dst)++ = *string;
140
       ++pc;
141
    }
142
    for ( ; width > 0; --width) {
143
       //outbyte(dst, padchar);                 
144
       *(*dst)++ = padchar;
145
       ++pc;
146
    }
147
 
148
    return pc;
149
}
150
 
151
 
152
/* Printf an integer */
153
int printi(char** dst, int i, int b, int sg, int width, int pad, int letbase)
154
{
155
    char print_buf[PRINT_BUF_LEN];
156
    char *s;
157
    int t, neg = 0, pc = 0;
158
    unsigned int u = i;
159
 
160
    if (i == 0) {
161
       print_buf[0] = '0';
162
       print_buf[1] = '\0';
163
       return prints (dst, print_buf, width, pad);
164
    }
165
 
166
    if (sg && b == 10 && i < 0) {
167
       neg = 1;
168
       u = -i;
169
    }
170
 
171
    s = print_buf + PRINT_BUF_LEN-1;
172
    *s = '\0';
173
 
174
    while (u) {
175
       if ( b == 16 )    t = u & 0xf;                  /* hex modulous */
176
       else              t = u - ( _div (u, b) * b );  /* Modulous */
177
 
178
       if( t >= 10 )
179
          t += letbase - '0' - 10;
180
       *--s = t + '0';
181
 
182
    /*   u /= b;  */
183
       if ( b == 16)  u = u >> 4;    /* divide by 16 */
184
       else           u = _div(u, b);
185
    }
186
 
187
    if (neg) {
188
       if( width && (pad & PAD_ZERO) ) {
189
          //outbyte(dst,'-'); 
190
          *(*dst)++ = '-';
191
          ++pc;
192
          --width;
193
       }
194
       else {
195
          *--s = '-';
196
       }
197
    }
198
 
199
    return pc + prints (dst, s, width, pad);
200
}
201
 

powered by: WebSVN 2.1.0

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