Core: remove use of blocking assignments (XST can't deal)
[yari.git] / testcases / stdio.h
blobe2fffacc82af467ef0f3fec4bf4e96869962a3ed
1 #ifdef STANDALONE
3 #define RS232IN_DATA (*(volatile unsigned *) 0xFF000004)
4 #define RS232IN_TAG (*(volatile unsigned *) 0xFF000008)
5 #define TSC (*(volatile unsigned *) 0xFF00000C)
6 #define SER_OUTBUSY() (*(volatile unsigned *)0xFF000000 != 0)
7 #define SER_OUT(data) (*(volatile unsigned *)0xFF000000 = (data))
9 #define BUF_SIZE 1024 // must be a power of two
11 char _serial_buffer[BUF_SIZE];
12 unsigned _serial_buffer_wp;
13 unsigned _serial_buffer_rp;
15 void check_serial_input(void)
17 static unsigned last_serial_tag;
19 unsigned char tag = RS232IN_TAG;
20 if (tag != last_serial_tag) {
21 _serial_buffer[_serial_buffer_wp++] = RS232IN_DATA;
22 _serial_buffer_wp &= BUF_SIZE-1;
23 last_serial_tag = tag;
24 if (_serial_buffer_wp == _serial_buffer_rp) {
25 while (SER_OUTBUSY());
26 SER_OUT('<');
27 while (SER_OUTBUSY());
28 SER_OUT('#');
29 while (SER_OUTBUSY());
30 SER_OUT('>');
35 static int _column = 0;
36 int putchar(unsigned ch)
38 if (ch == '\n') {
39 putchar('\r');
40 _column = -1;
41 } else if (ch == '\t') {
43 putchar(' ');
44 while (_column & 7);
45 return 1;
48 check_serial_input();
49 while (SER_OUTBUSY())
50 check_serial_input();
51 SER_OUT(ch);
52 ++_column;
54 return 1;
57 int getchar(void)
59 unsigned char tag, ch;
62 check_serial_input();
63 while (_serial_buffer_wp == _serial_buffer_rp);
65 ch = _serial_buffer[_serial_buffer_rp++];
66 _serial_buffer_rp &= BUF_SIZE-1;
68 // Local echo
69 putchar(ch);
71 if (ch == '\r')
72 ch = '\n';
74 return ch;
77 int puts(char *s)
79 int n = 0;
80 while (*s)
81 putchar(*s++), ++n;
83 putchar('\n');
85 return n;
89 int printf(char *fmt, ...);
91 int exit(int v)
93 asm("li $2, 0x87654321");
94 asm("mtlo $2");
95 asm(".word 0x48000000");
97 printf("THE END\n");
98 for (;;);
101 #endif
103 #include <stdarg.h>
105 /* Division-free integer printing */
106 static void put_unsigned(unsigned n)
108 // (unsigned)-1 == 4294967295
109 static unsigned powers10[] = {
110 1000000000,
111 100000000,
112 10000000,
113 1000000,
114 100000,
115 10000,
116 1000,
117 100,
122 unsigned *p;
123 unsigned leading_zero = 1;
125 if (!n) {
126 putchar('0');
127 return;
130 for (p = powers10; *p; ++p) {
131 unsigned scale = *p;
132 unsigned i, t;
134 for (i = 0; n >= scale; ++i, n -= scale)
136 if (i != 0 || !leading_zero) {
137 putchar('0' + i);
138 leading_zero = 0;
143 /* Hokey printf substitute */
144 int printf(char *fmt, ...)
146 va_list ap;
147 va_start(ap, fmt);
149 for (;; ++fmt)
150 switch (*fmt) {
151 case '\0':
152 va_end(ap);
153 return 1; // XXX return val incorrect
154 case '%':
155 switch (*++fmt) {
156 case 'd': {
157 int value = va_arg(ap, int);
158 if (value < 0)
159 putchar('-'), value = -value;
160 put_unsigned((unsigned) value);
161 break;
163 case 'c': {
164 char c = va_arg(ap, int);
165 putchar(c);
166 break;
168 case 's': {
169 char *s = va_arg(ap, char *);
170 while (*s)
171 putchar(*s++);
172 break;
174 default:
175 putchar(*fmt);
176 break;
178 break;
180 default:
181 putchar(*fmt);
182 break;
186 static char _lookahead = ' ';
188 static void _next(void)
190 _lookahead = getchar();
193 static _skip_ws(void)
195 while (_lookahead == ' ' ||
196 _lookahead == '\t' ||
197 _lookahead == '\r' ||
198 _lookahead == '\n')
199 _next();
202 static _skip_line(void)
204 while (_lookahead != '\n')
205 _next();
208 int scanf_hack(char *fmt, int *v)
210 static int next = 2;
212 *v = next;
214 next = 34;
217 int scanf(char *fmt, ...)
219 int n = 0;
220 va_list ap;
221 va_start(ap, fmt);
223 for (;; ++fmt)
224 switch (*fmt) {
225 case '\0':
226 va_end(ap);
227 return n;
228 case '%':
229 switch (*++fmt) {
230 case 'd': {
232 int neg = 0;
233 int value = 0;
235 _skip_ws();
236 if (_lookahead == '-')
237 neg = 1, _next();
239 while ('0' <= _lookahead && _lookahead <= '9') {
240 value = value * 10 + _lookahead - '0';
241 _next();
244 if (neg)
245 value = -value;
246 *va_arg(ap, int *) = value;
247 ++n;
248 break;
251 default:
252 continue;
254 break;
256 case ' ':
257 _skip_ws();
258 break;
260 default:
261 if (*fmt == _lookahead)
262 break;
263 else {
264 _skip_line();
265 return n;
270 int strlen(char *s)
272 int n;
274 for (n = 0; *s; ++n, ++s)
277 return n;
280 int abs(int v) { return v < 0 ? -v : v; }
282 int atoi(char *s)
284 int neg = 0;
285 int value = 0;
287 while (*s && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'))
288 ++s;
290 if (*s == '-')
291 neg = 1, ++s;
293 while ('0' <= *s && *s <= '9') {
294 value = value * 10 + _lookahead - '0';
295 ++s;
298 if (neg)
299 value = -value;
301 return value;
304 typedef char *FILE;
306 FILE *fopen(char *name, char *mode)
308 static FILE f =
309 "#include <stdio.h>\n"
310 "#define X(s) (!(s&3)-((s&3)==2))\n"
311 "#define W while\n"
312 "char Z[82][82],A,B,f,g=26;z(q){return atoi(q);}m(d,l){return\n"
313 "Z[ B + X ( f +\n"
314 "3) * d+l *X(f+ 2 )][ A+X ( f ) * d +\n"
315 "l* X ( f + 3 ) ] ;}int\n"
316 "h= 0;D(p,s)char*s; {W(h>>3<p>> 3 ) {putchar('\\t'\n"
317 "); h = ( h +8\n"
318 ")&~7 ;}W(h < p ){putchar(' ');++h; }(void)printf(\n"
319 "\"%s\" , s ) ;h+=strlen(s);}main(x,a)char **a; {\n"
320 "# define P(x) (x?(5-(x))*(6-(x ))/2:11)\n"
321 "int b; { char b[256],i, j=0; FILE*F;F=fopen(x-1?a[1]:\"buzzard.c\",\"r\");W(\n"
322 "fgets( b ,256 ,F)){for(i=0;b[ i];++ i)\n"
323 "Z[j][i ] =( b [ i ] ==' '?1:2*(b[i]==(x>2?*a[2]:'\\\\')));++j;}fclose\n"
324 "(F);}A =4 ; B = 3 ; f = 1;x >3? A=z(a[3]),B=z(a[4]):0;b='\\n';do{if(b=='\\n'\n"
325 "){int y , s , d , p , q ,i;for\n"
326 "(y=-11; y<= 11;++ y){ for(s = 1 ,d=0;s+3;s-=2){for\n"
327 "(;d!=2 + 3 * s ; d+=s){\n"
328 "if(m(d,0) !=1 ){p=P (d) ;if (abs( y )\n"
329 " <p&& ! m ( d , 0 )||abs(y)>p)break;for\n"
330 "(i =-p;i<p;++i)D(g+i*2,\"--\");D(0,\"-\");break;}if(d==5)continue;\n"
331 "p=P(d+1);q=P(d);if\n"
332 "(abs(y) >q)continue;if \n"
333 "(abs(y) <p)D(g-s*(2*p+1),\"|\");else if(m(d,s)){if\n"
334 "(abs(y) <=p)for(i=(s==1?-q:p);i!=(s==1?-p:q);\n"
335 "(abs(y) ),++i)D(g+2*i+(s==-1),\"--\");}else if\n"
336 "(abs(y) ==p)D(g-s*(2*p+1),\"|\");else D(g-\n"
337 "(abs(y) *s*2),(s==1)^(y>0)?\"\\\\\":\"/\");}d-=s;}puts(\n"
338 "\"\");h=0;}}f+=(b=='r')-(b=='l');f&=3;if(b=='f'){if(!m(1,0))continue;\n"
339 "A+=X(f);B+=X(f-1);}}W((b=getchar())!=-1&&m(0,0)==1);return 0;}\n"
342 return &f;
345 #define NULL ((void *) 0)
347 int fclose(FILE *f) {}
348 char *fgets(char *str, int size, FILE *f)
350 char *s = str;
351 char *fp = *f;
353 if (!*fp)
354 return NULL;
356 while (1 < size && *fp) {
357 *s++ = *fp++;
358 --size;
359 if (fp[-1] == '\n')
360 break;
363 if (s != str && size)
364 *s = '\0';
366 *f = fp;
367 return str;
370 unsigned __pre_main_t0;
372 void __pre_main(void)
374 __pre_main_t0 = TSC;
377 void __post_main(void)
379 printf("Program spent %d cycles in main\n", TSC - __pre_main_t0);
382 extern char _end[];
385 void *__sbrk_p = _end;
387 void *malloc(unsigned size)
389 void *res = __sbrk_p;
391 __sbrk_p += size;
394 void memset(void *d, char v, unsigned size)
396 char *cp = (char *) d;
398 while ((unsigned) cp & 3 && size)
399 *cp++ = v, --size;
401 unsigned *up = (unsigned *) cp;
402 unsigned vvvv = v * 0x1010101;
403 while (size > 32) {
404 up[0] = vvvv;
405 up[1] = vvvv;
406 up[2] = vvvv;
407 up[3] = vvvv;
408 up[4] = vvvv;
409 up[5] = vvvv;
410 up[6] = vvvv;
411 up[7] = vvvv;
412 up += 8;
413 size -= 32;
416 cp = (char *) up;
417 while (size)
418 *cp++ = v, --size;
421 // Modern GCCs provide these builtin, but ours is a bit stale
424 static inline int __builtin_rdhwr(int k)
426 int r;
428 asm(".set push;"
429 ".set mips32r2;"
430 "rdhwr %0,$%1;"
431 ".set pop" : "=r" (r) : "i" (k));
433 return r;
436 typedef unsigned size_t;
438 static inline void __builtin_flush_icache(void *location, size_t len)
440 const int SYNCI_Step = 1;
441 unsigned inc = __builtin_rdhwr(SYNCI_Step);
442 void *end = location + len;
444 for (location = (void *) ((unsigned) location & ~(inc - 1));
445 location < end;
446 location += inc) {
447 asm(".set push;"
448 ".set mips32r2;"
449 "synci %0;"
450 ".set pop" :: "m" (* (char *) location));