/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/fps2bios/kernel/iopload/iopdebug.c
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/fps2bios/kernel/iopload/iopdebug.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 32 - (show annotations) (download)
Tue Sep 7 03:29:01 2010 UTC (10 years, 10 months ago) by william
File MIME type: text/plain
File size: 5253 byte(s)
branching from upstream revision (http://pcsx2.googlecode.com/svn/trunk
): r3113 to
https://svn.netsolutions.dnsalias.com/websvn/ps2/pcsx2/pcsx2_0.9.7/branch/r3113_0.9.7_beta
1 /* Debugging printf, for debugging the library itself.
2
3 We don't assume stdio is working.
4 We do assume _write_r is working.
5 */
6
7 #include "iopload.h"
8 #include "iopdebug.h"
9
10 #ifdef __STDC__
11 #include "stdarg.h"
12 #else
13 #include "varargs.h"
14 #endif
15
16 static char *parse_number ();
17 static long get_number ();
18 static void print_number ();
19 static void write_char (char c);
20 static void write_string (char* s);
21
22 /* Non-zero for big-endian systems. */
23 static int big_endian_p;
24
25 /* For now hardcode 2 (stderr) as the console file descriptor.
26 May wish to let the caller pass in a file descriptor or some such but
27 this is only for debugging purposes anyway. */
28 #define CONSOLE_FD 2
29
30 /* Standalone printf routine.
31
32 The format string has been enhanced so that multiple values can be dumped
33 without having to have a %-field for each one (say if you want to dump
34 20 words at a certain address). A modifier of `N' says the next argument
35 is a count, and the one after that is a pointer.
36
37 Example: __dprintf (stderr, "%Nx\n", 20, p); /-* print 20 ints at `p' *-/
38
39 Supported formats are: c d u x s p.
40
41 All ints are retrieved a byte at a time so alignment issues are not
42 a problem.
43
44 This routine is used in situations where the only debugging capability
45 is console output and was written to aid debugging newlib itself. We don't
46 use printf ourselves as we may be debugging it. We do assume _write_r is
47 working.
48 */
49
50 void
51 #ifdef __STDC__
52 __printf (char *fmt, ...)
53 #else
54 __printf (fmt, va_alist)
55 char *fmt;
56 va_dcl
57 #endif
58 {
59 va_list args;
60
61 /* Which endian are we? */
62 {
63 short tmp = 1;
64 big_endian_p = *(char *) &tmp == 0;
65 }
66
67 #ifdef __STDC__
68 va_start (args, fmt);
69 #else
70 va_start (args);
71 #endif
72
73 while (*fmt)
74 {
75 char c, *p;
76 int count;
77 long l;
78
79 if (*fmt != '%' || *++fmt == '%')
80 {
81 write_char (*fmt++);
82 continue;
83 }
84
85 if (*fmt == 'N')
86 {
87 count = va_arg (args, int);
88 p = va_arg (args, char *);
89 ++fmt;
90 c = *fmt++;
91
92 while (--count >= 0)
93 {
94 switch (c)
95 {
96 // case 'c' :
97 // write_string (unctrl (*p++));
98 // break;
99 case 'p' :
100 print_number (16, 1, get_number (p, sizeof (char *), 1));
101 p += sizeof (char *);
102 break;
103 case 'd' :
104 case 'u' :
105 case 'x' :
106 print_number (c == 'x' ? 16 : 10, c != 'd',
107 get_number (p, sizeof (int), c != 'd'));
108 p += sizeof (int);
109 break;
110 case 's' :
111 write_string (*(char **) p);
112 p += sizeof (char *);
113 break;
114 }
115 if (count > 0)
116 write_char (' ');
117 }
118 }
119 else
120 {
121 switch (c = *fmt++)
122 {
123 // case 'c' :
124 // c = va_arg (args, int);
125 // write_string (unctrl (c));
126 // break;
127 case 'p' :
128 l = (void *) va_arg (args, char *);
129 print_number (16, 1, l);
130 break;
131 case 'd' :
132 case 'u' :
133 case 'x' :
134 l = va_arg (args, int);
135 print_number (c == 'x' ? 16 : 10, c != 'd', l);
136 break;
137 case 's' :
138 p = va_arg (args, char *);
139 write_string (p);
140 break;
141 }
142 }
143 }
144
145 va_end (args);
146 }
147
148 static int isdigit(int c) {
149 if (c >= '0' && c <= '9') return 1;
150 return 0;
151 }
152
153 /* Parse a positive decimal integer at S.
154 FIXME: Was used in earlier version, but not currently used.
155 Keep for now. */
156
157 static char *
158 parse_number (s, p)
159 char *s;
160 long *p;
161 {
162 long x = 0;
163
164 while (isdigit (*s))
165 {
166 x = (x * 10) + (*s - '0');
167 ++s;
168 }
169
170 *p = x;
171 return s;
172 }
173
174 /* Fetch the number at S of SIZE bytes. */
175
176 static long
177 get_number (s, size, unsigned_p)
178 char *s;
179 long size;
180 int unsigned_p;
181 {
182 long x;
183 unsigned char *p = (unsigned char *) s;
184
185 switch (size)
186 {
187 case 1 :
188 x = *p;
189 if (!unsigned_p)
190 x = (x ^ 0x80) - 0x80;
191 return x;
192 case 2 :
193 if (big_endian_p)
194 x = (p[0] << 8) | p[1];
195 else
196 x = (p[1] << 8) | p[0];
197 if (!unsigned_p)
198 x = (x ^ 0x8000) - 0x8000;
199 return x;
200 case 4 :
201 if (big_endian_p)
202 x = ((long)p[0] << 24) | ((long)p[1] << 16) | (p[2] << 8) | p[3];
203 else
204 x = ((long)p[3] << 24) | ((long)p[2] << 16) | (p[1] << 8) | p[0];
205 if (!unsigned_p)
206 x = (x ^ 0x80000000L) - 0x80000000L;
207 return x;
208 #if 0 /* FIXME: Is there a standard mechanism for knowing if
209 long longs exist? */
210 case 8 :
211 #endif
212 default :
213 return 0;
214 }
215 }
216
217 /* Print X in base BASE. */
218
219 static void
220 print_number (base, unsigned_p, n)
221 int base;
222 int unsigned_p;
223 long n;
224 {
225 static char chars[16] = "0123456789abcdef";
226 char *p, buf[32];
227 unsigned long x;
228
229 if (!unsigned_p && n < 0)
230 {
231 write_char ('-');
232 x = -n;
233 }
234 else
235 x = n;
236
237 p = buf + sizeof (buf);
238 *--p = '\0';
239 do
240 {
241 *--p = chars[x % base];
242 x /= base;
243 }
244 while (x != 0);
245
246 write_string (p);
247 }
248
249 /* Write C to the console.
250 We go through the file descriptor directly because we can't assume
251 stdio is working. */
252
253 static void
254 write_char (char c)
255 {
256 __putc(c);
257 }
258
259 /* Write S to the console.
260 We go through the file descriptor directly because we can't assume
261 stdio is working. */
262
263 static void
264 write_string (char *s)
265 {
266 __puts(s);
267 }
268
269
270 void __putc(u8 c) {
271 *((u8*)0x1f80380c) = c;
272 }
273
274 void __puts(u8 *s) {
275 while (*s != 0) {
276 __putc(*s++);
277 }
278 }
279

  ViewVC Help
Powered by ViewVC 1.1.22