reduce versa_ecp5 clock freq to 50 mhz, reduce bit-width of XICS addressing
[ls2.git] / hello_world / hello_world.c
1 #include <stdint.h>
2 #include <stdbool.h>
3
4 #include "console.h"
5
6 static char mw_logo[] =
7
8 "\n"
9 " .oOOo. \n"
10 " .\" \". \n"
11 " ; .mw. ; Microwatt, it works.\n"
12 " . ' ' . \n"
13 " \\ || / \n"
14 " ;..; \n"
15 " ;..; \n"
16 " `ww' \n";
17
18 static inline uint32_t readl(unsigned long addr)
19 {
20 uint32_t val;
21 __asm__ volatile("sync; lwzcix %0,0,%1" : "=r" (val) : "r" (addr) : "memory");
22 return val;
23 }
24
25 static inline void writel(uint32_t val, unsigned long addr)
26 {
27 __asm__ volatile("sync; stwcix %0,0,%1" : : "r" (val), "r" (addr) : "memory");
28 }
29
30 void uart_writeuint32(uint32_t val) {
31 const char lut[] = { '0', '1', '2', '3', '4', '5', '6', '7',
32 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
33 uint8_t *val_arr = (uint8_t*)(&val);
34 size_t i;
35
36 for (i = 0; i < 4; i++) {
37 putchar(lut[(val_arr[3-i] >> 4) & 0xF]);
38 putchar(lut[val_arr[3-i] & 0xF]);
39 }
40 }
41
42
43 int main(void)
44 {
45 console_init();
46
47 puts(mw_logo);
48
49 volatile uint32_t *sram = 0x0;
50 int count = 26;
51 puts("writing\n");
52 for (int i = 0; i < count; i++) {
53 uart_writeuint32(i);
54 puts("\n");
55 writel(0xBEEF0000+i, &(sram[1<<i]));
56 }
57 puts("reading\n");
58 for (int i = 0; i < count; i++) {
59 int val = readl(&(sram[1<<i]));
60 uart_writeuint32(i);
61 puts(" ");
62 uart_writeuint32(val);
63 puts("\n");
64 }
65 while (1) {
66 unsigned char c = getchar();
67 putchar(c);
68 if (c == 13) // if CR send LF
69 putchar(10);
70 }
71 }