dol: create target object folders when generating
[jump.git] / minidol / hsrc / 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/4)
18 #define COL(X) (X%4)
19
20 #define NUM_PROGRAMS    2
21
22 /* programs to run */
23 char programs[NUM_PROGRAMS][64] = {
24         "bin/Square.srec",
25         "bin/Gen.srec",
26 };
27
28 /* local copy of shared memory */
29 shm_t   shm = {{ 0 }};
30
31 #if 0
32 static uint32_t xorshift32(void)
33 {
34         static uint32_t x = 314159265;
35         x ^= x << 17;
36         x ^= x >> 13;
37         x ^= x << 5;
38         return(x);
39 }
40 #endif
41
42 void shm_init(void) {
43         /* input data */
44         shm.buf0.size = 8;
45         shm.buf0.rp = 0;
46         shm.buf0.wp = 0;
47
48         /* output data */
49         shm.buf1.size = 32;
50         shm.buf1.rp   = 0;
51         shm.buf1.wp   = 0;
52 }
53
54 int main(int argc, char *argv[])
55 {
56         char filename[255];     /* filename to load */
57
58         /* core states */
59         int32_t states[NUM_CORES]     = { 0 };
60         int32_t laststates[NUM_CORES] = { 0 };
61
62         e_epiphany_t dev;
63         e_mem_t      emem;
64
65         e_set_host_verbosity(H_D0);
66         e_set_loader_verbosity(L_D0);
67
68         /* init board, reset epiphany, open workgroup */
69         if(e_init(NULL) != E_OK)
70                 FAIL("Can't init!\n");
71         e_reset_system();
72         if(e_open(&dev, 0, 0, 4, 4) != E_OK)
73                 FAIL("Can't open!\n");
74
75         /* initialize, allocate and update shared memory buffer */
76         shm_init();
77         if(e_alloc(&emem, SHM_OFFSET, sizeof(shm_t)) != E_OK)
78                 FAIL("Can't alloc!\n");
79         if(e_write(&emem, 0, 0, (off_t)0, &shm, sizeof(shm_t)) == E_ERR)
80                 FAIL("Can't clear shm!\n");
81         if(e_write(&emem, 0, 0, (off_t)0, &states, sizeof(states)) == E_ERR)
82                 FAIL("Can't clear states!\n");
83
84         /* load and start all programs */
85         for(int i = 0; i < NUM_PROGRAMS; i++) {
86                 snprintf(filename, 255, "%s", programs[i]);
87                 printf("Kicking core %i = (%i, %i) with '%s'\n",
88                         i, ROW(i), COL(i), filename);
89                 if(e_load(filename, &dev, ROW(i), COL(i), E_TRUE) != E_OK)
90                         FAIL("Can't load %i!\n", i);
91         }
92
93         /* =============================================================== */
94         printf("Started %i cores.\n", NUM_PROGRAMS);
95         while(1) {
96                 int done = 1;
97
98                 /* poll shm states for changes */
99                 printf("Polling shared memory.\n");
100
101                 while(1) {
102                         /* read epiphany core states */
103                         if(e_read(&emem, 0, 0, (off_t)0, &states,
104                                 sizeof(states)) == E_ERR)
105                                         FAIL("Can't poll!\n");
106
107                         /* check for state changes */
108                         if(memcmp(laststates, states, sizeof(states)))
109                                 break;
110                 };
111
112                 /* save current states */
113                 memcpy(laststates, states, sizeof(states));
114
115                 /* print core states */
116                 for(int i = 0; i < NUM_PROGRAMS; i++) {
117                         printf("CORE %i: ", i);
118                         switch(states[i]) {
119                         case -1: printf("done "); break;
120                         default: printf("state %i ", states[i]); done=0; break;
121                         }
122                         printf("\n");
123                 }
124                 printf("CORE14: 0x%x\n", states[14]);
125                 printf("CORE15: 0x%x\n", states[15]);
126
127                 /* stop polling if all cores finished */
128                 if(done) break;
129         }
130         /* =============================================================== */
131
132         printf("All cores finished.\n");
133
134         /* read whole shm buffer */
135         if(e_read(&emem, 0, 0, (off_t)0, &shm, sizeof(shm)) == E_ERR)
136                 FAIL("Can't read full shm!\n");
137
138         /* print buffer 0 and buffer 1 */
139         for(int i = 0; i < 16; i++) {
140                 printf("0x%x ", shm.buf0.buf[i]);
141         } printf("\n");
142         for(int i = 0; i < 16; i++) {
143                 printf("0x%x ", shm.buf1.buf[i]);
144         } printf("\n");
145
146
147         for(int i = 0; i < 4; i++) {
148                 printf("%.2f\t", ((float*)shm.buf0.buf)[i]);
149         } printf("\n");
150
151         for(int i = 0; i < 8; i++) {
152                 printf("%.2f\t", ((float*)shm.buf1.buf)[i]);
153         } printf("\n");
154
155         /* free shm buffer, close workgroup and finalize */
156         if(e_free(&emem) != E_OK)
157                 FAIL("Can't free!\n");
158         if(e_close(&dev) != E_OK)
159                 FAIL("Can't close!\n");
160         if(e_finalize() != E_OK)
161                 FAIL("Can't finalize!\n");
162
163         return(0);
164 }
165