[render_text] Move all glyphs to the left...
[wikipediardware.git] / bootloader / misc.c
blob78436ca673a91be3e9dfbe7e387c5f0c02c00a09
1 /*
2 e07 bootloader suite
3 Copyright (c) 2008 Daniel Mack <daniel@caiaq.de>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "regs.h"
20 #include "types.h"
21 #include "wikireader.h"
22 #include "misc.h"
25 int serial_input_available(void) {
26 return (0 != (REG_EFSIF0_STATUS & RDBFx));
30 int serial_input_char(void)
32 while (!serial_input_available()) {
35 return(REG_EFSIF0_RXD);
39 #define WAIT_FOR_EFSIF0_RDY() \
40 do { \
41 } while (0 == (REG_EFSIF0_STATUS & TDBEx))
45 void print_char(const char c)
47 if (c == '\n') {
48 WAIT_FOR_EFSIF0_RDY();
49 REG_EFSIF0_TXD = '\r';
51 WAIT_FOR_EFSIF0_RDY();
52 REG_EFSIF0_TXD = c;
56 void print(const char *txt)
58 while (txt && *txt) {
59 print_char(*txt++);
63 static void print_nibble(u8 nib)
65 nib &= 0x0f;
66 if (nib >= 10)
67 print_char(nib - 10 + 'a');
68 else
69 print_char(nib + '0');
72 void print_byte(u8 byte)
74 print_nibble(byte >> 4);
75 print_nibble(byte);
78 void hex_dump(const u8 *buf, u32 size)
80 int i, l;
81 char a[2] = "X";
83 for (l = 0; l < size; l += 16) {
84 print_byte(l >> 24);
85 print_byte(l >> 16);
86 print_byte(l >> 8);
87 print_byte(l);
88 print(" ");
90 for (i = 0; i < 16; i++) {
91 if (l + i < size) {
92 print_byte(buf[l + i]);
93 print(" ");
94 } else
95 print(" ");
98 print(" |");
99 for (i = 0; i < 16; i++) {
100 if (l + i < size) {
101 if (buf[l + i] >= ' ' && buf[l + i] <= '~')
102 a[0] = buf[l + i];
103 else
104 a[0] = '.';
105 } else
106 a[0] = ' ';
108 print(a);
111 print("|\n");
115 void print_u32(u32 val)
117 print("0x");
118 print_byte(val >> 24);
119 print_byte(val >> 16);
120 print_byte(val >> 8);
121 print_byte(val);
124 void delay(u32 nops)
126 while (nops--)
127 asm volatile ("nop");
130 void delay_us(unsigned int microsec)
132 while (microsec--) {
133 //asm volatile("nop");
134 // at 48 MHz this should take 1 micro second
135 asm volatile (
136 "\tld.w\t%r4,12\n"
137 "delay_loop:\n"
138 "\tnop\n"
139 "\tsub\t%r4,1\n"
140 "\tjrne\tdelay_loop"
145 #if 0
146 void printf(const char *fmt, ...) /* format to be printed */
148 int c; /* next character in fmt */
149 int d;
150 unsigned long u; /* hold number argument */
151 int base; /* base of number arg */
152 int negative; /* print minus sign */
153 static char x2c[] = "0123456789ABCDEF"; /* nr conversion table */
154 char ascii[8 * sizeof(long) / 3 + 2]; /* string for ascii number */
155 char *s; /* string to be printed */
156 va_list argp; /* optional arguments */
158 va_start(argp, fmt); /* init variable arguments */
160 while((c=*fmt++) != 0) {
162 if (c == '%') { /* expect format '%key' */
163 negative = 0; /* (re)initialize */
164 s = NULL; /* (re)initialize */
165 switch(c = *fmt++) { /* determine what to do */
167 /* Known keys are %d, %u, %x, %s, and %%. This is easily extended
168 * with number types like %b and %o by providing a different base.
169 * Number type keys don't set a string to 's', but use the general
170 * conversion after the switch statement.
172 case 'd': /* output decimal */
173 d = va_arg(argp, signed int);
174 if (d < 0) { negative = 1; u = -d; } else { u = d; }
175 base = 10;
176 break;
177 case 'u': /* output unsigned long */
178 u = va_arg(argp, unsigned long);
179 base = 10;
180 break;
181 case 'x': /* output hexadecimal */
182 u = va_arg(argp, unsigned long);
183 base = 0x10;
184 break;
185 case 's': /* output string */
186 s = va_arg(argp, char *);
187 if (s == NULL) s = "(null)";
188 break;
189 case '%': /* output percent */
190 s = "%";
191 break;
193 /* Unrecognized key. */
194 default: /* echo back %key */
195 s = "%?";
196 s[1] = c; /* set unknown key */
199 /* Assume a number if no string is set. Convert to ascii. */
200 if (s == NULL) {
201 s = ascii + sizeof(ascii)-1;
202 *s = 0;
203 do { *--s = x2c[(u % base)]; } /* work backwards */
204 while ((u /= base) > 0);
207 /* This is where the actual output for format "%key" is done. */
208 if (negative) kputc('-'); /* print sign if negative */
209 while(*s != 0) { kputc(*s++); } /* print string/ number */
211 else {
212 kputc(c); /* print and continue */
215 va_end(argp); /* end variable arguments */
217 #endif