-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc_test.c
71 lines (62 loc) · 1.24 KB
/
c_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
int fib(int n);
void uart_hex(int d);
void echo();
void uart_send(char c);
void uart_send_string(char *str);
int main()
{
// echo();
int ans = fib(20);
uart_send_string("ans:");
uart_hex(ans);
uart_send('\n');
return 0;
}
int fib(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
return (fib(n - 1) + fib(n - 2));
}
void uart_hex(int d)
{
volatile char *uart = (volatile char *) 0x10000000;
unsigned int n;
int c;
for (c = 28; c >= 0; c -= 4) {
// get highest tetrad
n = (d >> c) & 0xF;
// 0-9 => '0'-'9', 10-15 => 'A'-'F'
n += n > 9 ? 0x37 : 0x30;
uart[0] = n;
}
}
void uart_send(char c)
{
volatile char *uart = (volatile char *) 0x10000000;
uart[0] = c;
}
void uart_send_string(char *str)
{
volatile char *uart = (volatile char *) 0x10000000;
for (int i = 0; str[i] != '\0'; i++) {
uart[0] = str[i];
}
}
void echo()
{
while (1) {
volatile char *uart = (volatile char *) 0x10000000;
while ((uart[5] & 0x01) == 0)
;
char c = uart[0];
if ('a' <= c && c <= 'z') {
c = c + 'A' - 'a';
}
uart[0] = c;
if (c == 'X')
break;
}
}