dol: initial epiphany code generator (untested)
[jump.git] / dol / src / dol / visitor / epiphany / template / main.c
1 /* Epiphany Host Application */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <time.h>
8
9 #include "e-hal.h"
10
11 /* common data structures */
12 #include "../shared.h"
13
14 #define FAIL(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
15 #define SHM_OFFSET 0x01000000
16
17 #define ROW(X) (X/CORES_PER_ROW)
18 #define COL(X) (X%CORES_PER_ROW)
19
20 #define NUM_PROGRAMS    @@SREC_NUM@@
21
22 #if NUM_PROGRAMS > NUM_CORES
23   #error "Please limit the number of processes to the number of cores."
24 #endif
25
26 /* programs to run */
27 char programs[NUM_PROGRAMS][64] = {
28 @@SREC_FILES@@
29 };
30
31 /* local copy of shared memory */
32 shm_t   shm = {{ 0 }};
33
34 void shm_init(void) {
35 @@SHM_BUF_INIT@@
36 }
37
38 int main(int argc, char *argv[])
39 {
40         char filename[255];     /* filename to load */
41
42         /* core states */
43         int32_t states[NUM_CORES]     = { 0 };
44         int32_t laststates[NUM_CORES] = { 0 };
45
46         e_epiphany_t dev;
47         e_mem_t      emem;
48
49         e_set_host_verbosity(H_D0);
50         e_set_loader_verbosity(L_D0);
51
52         /* init board, reset epiphany, open workgroup */
53         if(e_init(NULL) != E_OK)
54                 FAIL("Can't init!\n");
55         e_reset_system();
56         if(e_open(&dev, 0, 0, 4, 4) != E_OK)
57                 FAIL("Can't open!\n");
58
59         /* initialize, allocate and update shared memory buffer */
60         shm_init();
61         if(e_alloc(&emem, SHM_OFFSET, sizeof(shm_t)) != E_OK)
62                 FAIL("Can't alloc!\n");
63         if(e_write(&emem, 0, 0, (off_t)0, &shm, sizeof(shm_t)) == E_ERR)
64                 FAIL("Can't clear shm!\n");
65         if(e_write(&emem, 0, 0, (off_t)0, &states, sizeof(states)) == E_ERR)
66                 FAIL("Can't clear states!\n");
67
68         /* load and start all programs */
69         for(int i = 0; i < NUM_PROGRAMS; i++) {
70                 snprintf(filename, 255, "%s", programs[i]);
71                 printf("Kicking core %i = (%i, %i) with '%s'\n",
72                         i, ROW(i), COL(i), filename);
73                 if(e_load(filename, &dev, ROW(i), COL(i), E_TRUE) != E_OK)
74                         FAIL("Can't load %i!\n", i);
75         }
76
77         /* =============================================================== */
78         printf("Started %i cores.\n", NUM_PROGRAMS);
79         while(1) {
80                 int done = 1;
81
82                 /* poll shm states for changes */
83                 printf("Polling shared memory.\n");
84
85                 while(1) {
86                         /* read epiphany core states */
87                         if(e_read(&emem, 0, 0, (off_t)0, &states,
88                                 sizeof(states)) == E_ERR)
89                                         FAIL("Can't poll!\n");
90
91                         /* check for state changes */
92                         if(memcmp(laststates, states, sizeof(states)))
93                                 break;
94                 };
95
96                 /* save current states */
97                 memcpy(laststates, states, sizeof(states));
98
99                 /* print core states */
100                 for(int i = 0; i < NUM_PROGRAMS; i++) {
101                         printf("CORE %i: ", i);
102                         switch(states[i]) {
103                         case -1: printf("done "); break;
104                         default: printf("state %i ", states[i]); done=0; break;
105                         }
106                         printf("\n");
107                 }
108                 printf("CORE14: 0x%x\n", states[14]);
109                 printf("CORE15: 0x%x\n", states[15]);
110
111                 /* stop polling if all cores finished */
112                 if(done) break;
113         }
114         /* =============================================================== */
115
116         printf("All cores finished.\n");
117
118         /* read whole shm buffer */
119         if(e_read(&emem, 0, 0, (off_t)0, &shm, sizeof(shm)) == E_ERR)
120                 FAIL("Can't read full shm!\n");
121
122         /* print buffer 0 and buffer 1 */
123         for(int i = 0; i < 16; i++) {
124                 printf("0x%x ", shm.buf0.buf[i]);
125         } printf("\n");
126         for(int i = 0; i < 16; i++) {
127                 printf("0x%x ", shm.buf1.buf[i]);
128         } printf("\n");
129
130
131         for(int i = 0; i < 4; i++) {
132                 printf("%.2f\t", ((float*)shm.buf0.buf)[i]);
133         } printf("\n");
134
135         for(int i = 0; i < 8; i++) {
136                 printf("%.2f\t", ((float*)shm.buf1.buf)[i]);
137         } printf("\n");
138
139         /* free shm buffer, close workgroup and finalize */
140         if(e_free(&emem) != E_OK)
141                 FAIL("Can't free!\n");
142         if(e_close(&dev) != E_OK)
143                 FAIL("Can't close!\n");
144         if(e_finalize() != E_OK)
145                 FAIL("Can't finalize!\n");
146
147         return(0);
148 }
149