Rev 42 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 42 | Rev 61 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <putchar.h> |
29 | #include <putchar.h> |
30 | #include <print.h> |
30 | #include <print.h> |
31 | #include <synch/spinlock.h> |
31 | #include <synch/spinlock.h> |
32 | #include <arch/arg.h> |
32 | #include <arch/arg.h> |
33 | 33 | ||
34 | 34 | ||
35 | static char digits[] = "0123456789abcdef"; |
35 | static char digits[] = "0123456789abcdef"; /**< Hexadecimal characters */ |
36 | static spinlock_t printflock; |
36 | static spinlock_t printflock; /**< printf spinlock */ |
37 | 37 | ||
- | 38 | ||
- | 39 | /** Print NULL terminated string |
|
- | 40 | * |
|
- | 41 | * Print characters from str using putchar() until |
|
- | 42 | * \x00 character is reached. |
|
- | 43 | * |
|
- | 44 | * @param str Characters to print. |
|
- | 45 | * |
|
- | 46 | */ |
|
38 | void print_str(char *str) |
47 | void print_str(char *str) |
39 | { |
48 | { |
40 | int i = 0; |
49 | int i = 0; |
41 | char c; |
50 | char c; |
42 | 51 | ||
43 | while (c = str[i++]) |
52 | while (c = str[i++]) |
44 | putchar(c); |
53 | putchar(c); |
45 | } |
54 | } |
46 | 55 | ||
47 | 56 | ||
- | 57 | /** Print hexadecimal digits |
|
48 | /* |
58 | * |
49 | * This is a universal function for printing hexadecimal numbers of fixed |
59 | * Print fixed count of hexadecimal digits from |
- | 60 | * the number num. The digits are printed in |
|
- | 61 | * natural left-to-right order starting with |
|
50 | * width. |
62 | * the width-th digit. |
- | 63 | * |
|
- | 64 | * @param num Number containing digits. |
|
- | 65 | * @param width Count of digits to print. |
|
- | 66 | * |
|
51 | */ |
67 | */ |
52 | void print_fixed_hex(__native num, int width) |
68 | void print_fixed_hex(__native num, int width) |
53 | { |
69 | { |
54 | int i; |
70 | int i; |
55 | 71 | ||
56 | for (i = width*8 - 4; i >= 0; i -= 4) |
72 | for (i = width*8 - 4; i >= 0; i -= 4) |
57 | putchar(digits[(num>>i) & 0xf]); |
73 | putchar(digits[(num>>i) & 0xf]); |
58 | } |
74 | } |
59 | 75 | ||
- | 76 | ||
- | 77 | /** Print number in given base |
|
60 | /* |
78 | * |
61 | * This is a universal function for printing decimal and hexadecimal numbers. |
79 | * Print significant digits of a number in given |
- | 80 | * base. |
|
- | 81 | * |
|
- | 82 | * @param num Number to print. |
|
- | 83 | * @param base Base to print the number in (should |
|
62 | * It prints only significant digits. |
84 | * be in range 2 .. 16). |
- | 85 | * |
|
63 | */ |
86 | */ |
64 | void print_number(__native num, int base) |
87 | void print_number(__native num, int base) |
65 | { |
88 | { |
66 | char d[sizeof(__native)*8+1]; /* this is good enough even for base == 2 */ |
89 | char d[sizeof(__native)*8+1]; /* this is good enough even for base == 2 */ |
67 | int i = sizeof(__native)*8-1; |
90 | int i = sizeof(__native)*8-1; |
68 | 91 | ||
69 | do { |
92 | do { |
70 | d[i--] = digits[num % base]; |
93 | d[i--] = digits[num % base]; |
71 | } while (num /= base); |
94 | } while (num /= base); |
72 | 95 | ||
73 | d[sizeof(__native)*8] = 0; |
96 | d[sizeof(__native)*8] = 0; |
74 | print_str(&d[i + 1]); |
97 | print_str(&d[i + 1]); |
75 | } |
98 | } |
76 | 99 | ||
- | 100 | ||
- | 101 | /** General formatted text print |
|
77 | /* |
102 | * |
- | 103 | * Print text formatted according the fmt parameter |
|
- | 104 | * and variant arguments. Each formatting directive |
|
- | 105 | * begins with % (percentage) character and one of the |
|
- | 106 | * following character: |
|
- | 107 | * |
|
- | 108 | * % Prints the percentage character. |
|
78 | * This is our function for printing formatted text. |
109 | * s The next variant argument is threated as char* |
79 | * It's much simpler than the user-space one. |
110 | * and printed as a NULL terminated string. |
- | 111 | * c The next variant argument is threated as a single char. |
|
- | 112 | * l The next variant argument is threated as a 32b integer |
|
- | 113 | * and printed in full hexadecimal width. |
|
- | 114 | * L As with 'l', but '0x' is prefixed. |
|
- | 115 | * w The next variant argument is threated as a 16b integer |
|
- | 116 | * and printed in full hexadecimal width. |
|
- | 117 | * W As with 'w', but '0x' is prefixed. |
|
- | 118 | * b The next variant argument is threated as a 8b integer |
|
- | 119 | * and printed in full hexadecimal width. |
|
- | 120 | * N As with 'b', but '0x' is prefixed. |
|
- | 121 | * d The next variant argument is threated as integer |
|
- | 122 | * and printed in standard decimal format (only significant |
|
- | 123 | * digits). |
|
- | 124 | * x The next variant argument is threated as integer |
|
- | 125 | * and printed in standard hexadecimal format (only significant |
|
- | 126 | * digits). |
|
- | 127 | * X As with 'x', but '0x' is prefixed. |
|
- | 128 | * |
|
- | 129 | * All other characters from fmt except the formatting directives |
|
80 | * We are greateful for this function. |
130 | * are printed in verbatim. |
- | 131 | * |
|
- | 132 | * @param fmt Formatting NULL terminated string. |
|
- | 133 | * |
|
81 | */ |
134 | */ |
82 | void printf(char *fmt, ...) |
135 | void printf(char *fmt, ...) |
83 | { |
136 | { |
84 | int irqpri, i = 0; |
137 | int irqpri, i = 0; |
85 | va_list ap; |
138 | va_list ap; |
86 | char c; |
139 | char c; |
87 | 140 | ||
88 | va_start(ap, fmt); |
141 | va_start(ap, fmt); |
89 | 142 | ||
90 | irqpri = cpu_priority_high(); |
143 | irqpri = cpu_priority_high(); |
91 | spinlock_lock(&printflock); |
144 | spinlock_lock(&printflock); |
92 | 145 | ||
93 | while (c = fmt[i++]) { |
146 | while (c = fmt[i++]) { |
94 | switch (c) { |
147 | switch (c) { |
95 | 148 | ||
96 | /* control character */ |
149 | /* control character */ |
97 | case '%': |
150 | case '%': |
98 | switch (c = fmt[i++]) { |
151 | switch (c = fmt[i++]) { |
99 | 152 | ||
100 | /* percentile itself */ |
153 | /* percentile itself */ |
101 | case '%': |
154 | case '%': |
102 | break; |
155 | break; |
103 | 156 | ||
104 | /* |
157 | /* |
105 | * String and character conversions. |
158 | * String and character conversions. |
106 | */ |
159 | */ |
107 | case 's': |
160 | case 's': |
108 | print_str(va_arg(ap, char_ptr)); |
161 | print_str(va_arg(ap, char_ptr)); |
109 | goto loop; |
162 | goto loop; |
110 | 163 | ||
111 | case 'c': |
164 | case 'c': |
112 | c = (char) va_arg(ap, int); |
165 | c = (char) va_arg(ap, int); |
113 | break; |
166 | break; |
114 | 167 | ||
115 | /* |
168 | /* |
116 | * Hexadecimal conversions with fixed width. |
169 | * Hexadecimal conversions with fixed width. |
117 | */ |
170 | */ |
118 | case 'L': |
171 | case 'L': |
119 | print_str("0x"); |
172 | print_str("0x"); |
120 | case 'l': |
173 | case 'l': |
121 | print_fixed_hex(va_arg(ap, __native), INT32); |
174 | print_fixed_hex(va_arg(ap, __native), INT32); |
122 | goto loop; |
175 | goto loop; |
123 | 176 | ||
124 | case 'W': |
177 | case 'W': |
125 | print_str("0x"); |
178 | print_str("0x"); |
126 | case 'w': |
179 | case 'w': |
127 | print_fixed_hex(va_arg(ap, __native), INT16); |
180 | print_fixed_hex(va_arg(ap, __native), INT16); |
128 | goto loop; |
181 | goto loop; |
129 | 182 | ||
130 | case 'B': |
183 | case 'B': |
131 | print_str("0x"); |
184 | print_str("0x"); |
132 | case 'b': |
185 | case 'b': |
133 | print_fixed_hex(va_arg(ap, __native), INT8); |
186 | print_fixed_hex(va_arg(ap, __native), INT8); |
134 | goto loop; |
187 | goto loop; |
135 | 188 | ||
136 | /* |
189 | /* |
137 | * Decimal and hexadecimal conversions. |
190 | * Decimal and hexadecimal conversions. |
138 | */ |
191 | */ |
139 | case 'd': |
192 | case 'd': |
140 | print_number(va_arg(ap, __native), 10); |
193 | print_number(va_arg(ap, __native), 10); |
141 | goto loop; |
194 | goto loop; |
142 | 195 | ||
143 | case 'X': |
196 | case 'X': |
144 | print_str("0x"); |
197 | print_str("0x"); |
145 | case 'x': |
198 | case 'x': |
146 | print_number(va_arg(ap, __native), 16); |
199 | print_number(va_arg(ap, __native), 16); |
147 | goto loop; |
200 | goto loop; |
148 | 201 | ||
149 | /* |
202 | /* |
150 | * Bad formatting. |
203 | * Bad formatting. |
151 | */ |
204 | */ |
152 | default: |
205 | default: |
153 | goto out; |
206 | goto out; |
154 | } |
207 | } |
155 | 208 | ||
156 | default: putchar(c); |
209 | default: putchar(c); |
157 | } |
210 | } |
158 | 211 | ||
159 | loop: |
212 | loop: |
160 | ; |
213 | ; |
161 | } |
214 | } |
162 | 215 | ||
163 | out: |
216 | out: |
164 | spinlock_unlock(&printflock); |
217 | spinlock_unlock(&printflock); |
165 | cpu_priority_restore(irqpri); |
218 | cpu_priority_restore(irqpri); |
166 | 219 | ||
167 | va_end(ap); |
220 | va_end(ap); |
168 | } |
221 | } |
169 | 222 |