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());
27 while (SER_OUTBUSY());
29 while (SER_OUTBUSY());
35 static int _column
= 0;
36 int putchar(unsigned ch
)
41 } else if (ch
== '\t') {
59 unsigned char tag
, ch
;
63 while (_serial_buffer_wp
== _serial_buffer_rp
);
65 ch
= _serial_buffer
[_serial_buffer_rp
++];
66 _serial_buffer_rp
&= BUF_SIZE
-1;
89 int printf(char *fmt
, ...);
93 asm("li $2, 0x87654321");
95 asm(".word 0x48000000");
105 /* Division-free integer printing */
106 static void put_unsigned(unsigned n
)
108 // (unsigned)-1 == 4294967295
109 static unsigned powers10
[] = {
123 unsigned leading_zero
= 1;
130 for (p
= powers10
; *p
; ++p
) {
134 for (i
= 0; n
>= scale
; ++i
, n
-= scale
)
136 if (i
!= 0 || !leading_zero
) {
143 /* Hokey printf substitute */
144 int printf(char *fmt
, ...)
153 return 1; // XXX return val incorrect
157 int value
= va_arg(ap
, int);
159 putchar('-'), value
= -value
;
160 put_unsigned((unsigned) value
);
164 char c
= va_arg(ap
, int);
169 char *s
= va_arg(ap
, char *);
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' ||
202 static _skip_line(void)
204 while (_lookahead
!= '\n')
208 int scanf_hack(char *fmt
, int *v
)
217 int scanf(char *fmt
, ...)
236 if (_lookahead
== '-')
239 while ('0' <= _lookahead
&& _lookahead
<= '9') {
240 value
= value
* 10 + _lookahead
- '0';
246 *va_arg(ap
, int *) = value
;
261 if (*fmt
== _lookahead
)
274 for (n
= 0; *s
; ++n
, ++s
)
280 int abs(int v
) { return v
< 0 ? -v
: v
; }
287 while (*s
&& (*s
== ' ' || *s
== '\t' || *s
== '\n' || *s
== '\r'))
293 while ('0' <= *s
&& *s
<= '9') {
294 value
= value
* 10 + _lookahead
- '0';
306 FILE *fopen(char *name
, char *mode
)
309 "#include <stdio.h>\n"
310 "#define X(s) (!(s&3)-((s&3)==2))\n"
312 "char Z[82][82],A,B,f,g=26;z(q){return atoi(q);}m(d,l){return\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"
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"
345 #define NULL ((void *) 0)
347 int fclose(FILE *f
) {}
348 char *fgets(char *str
, int size
, FILE *f
)
356 while (1 < size
&& *fp
) {
363 if (s
!= str
&& size
)
370 unsigned __pre_main_t0
;
372 void __pre_main(void)
377 void __post_main(void)
379 printf("Program spent %d cycles in main\n", TSC
- __pre_main_t0
);
385 void *__sbrk_p
= _end
;
387 void *malloc(unsigned size
)
389 void *res
= __sbrk_p
;
394 void memset(void *d
, char v
, unsigned size
)
396 char *cp
= (char *) d
;
398 while ((unsigned) cp
& 3 && size
)
401 unsigned *up
= (unsigned *) cp
;
402 unsigned vvvv
= v
* 0x1010101;
421 // Modern GCCs provide these builtin, but ours is a bit stale
424 static inline int __builtin_rdhwr(int k
)
431 ".set pop" : "=r" (r
) : "i" (k
));
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));
450 ".set pop" :: "m" (* (char *) location
));