+/* Epiphany Host Application */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+
+#include "e-hal.h"
+
+/* common data structures */
+#include "../shared.h"
+
+#define FAIL(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
+#define SHM_OFFSET 0x01000000
+
+#define ROW(X) (X/4)
+#define COL(X) (X%4)
+
+#define NUM_PROGRAMS 2
+
+/* programs to run */
+char programs[NUM_PROGRAMS][64] = {
+ "bin/Square.srec",
+ "bin/Gen.srec",
+};
+
+/* local copy of shared memory */
+shm_t shm = {{ 0 }};
+
+#if 0
+static uint32_t xorshift32(void)
+{
+ static uint32_t x = 314159265;
+ x ^= x << 17;
+ x ^= x >> 13;
+ x ^= x << 5;
+ return(x);
+}
+#endif
+
+void shm_init(void) {
+ /* input data */
+ shm.buf0.size = 8;
+ shm.buf0.rp = 0;
+ shm.buf0.wp = 0;
+
+ /* output data */
+ shm.buf1.size = 32;
+ shm.buf1.rp = 0;
+ shm.buf1.wp = 0;
+}
+
+int main(int argc, char *argv[])
+{
+ char filename[255]; /* filename to load */
+
+ /* core states */
+ int32_t states[NUM_CORES] = { 0 };
+ int32_t laststates[NUM_CORES] = { 0 };
+
+ e_epiphany_t dev;
+ e_mem_t emem;
+
+ e_set_host_verbosity(H_D0);
+ e_set_loader_verbosity(L_D0);
+
+ /* init board, reset epiphany, open workgroup */
+ if(e_init(NULL) != E_OK)
+ FAIL("Can't init!\n");
+ e_reset_system();
+ if(e_open(&dev, 0, 0, 4, 4) != E_OK)
+ FAIL("Can't open!\n");
+
+ /* initialize, allocate and update shared memory buffer */
+ shm_init();
+ if(e_alloc(&emem, SHM_OFFSET, sizeof(shm_t)) != E_OK)
+ FAIL("Can't alloc!\n");
+ if(e_write(&emem, 0, 0, (off_t)0, &shm, sizeof(shm_t)) == E_ERR)
+ FAIL("Can't clear shm!\n");
+ if(e_write(&emem, 0, 0, (off_t)0, &states, sizeof(states)) == E_ERR)
+ FAIL("Can't clear states!\n");
+
+ /* load and start all programs */
+ for(int i = 0; i < NUM_PROGRAMS; i++) {
+ snprintf(filename, 255, "%s", programs[i]);
+ printf("Kicking core %i = (%i, %i) with '%s'\n",
+ i, ROW(i), COL(i), filename);
+ if(e_load(filename, &dev, ROW(i), COL(i), E_TRUE) != E_OK)
+ FAIL("Can't load %i!\n", i);
+ }
+
+ /* =============================================================== */
+ printf("Started %i cores.\n", NUM_PROGRAMS);
+ while(1) {
+ int done = 1;
+
+ /* poll shm states for changes */
+ printf("Polling shared memory.\n");
+
+ while(1) {
+ /* read epiphany core states */
+ if(e_read(&emem, 0, 0, (off_t)0, &states,
+ sizeof(states)) == E_ERR)
+ FAIL("Can't poll!\n");
+
+ /* check for state changes */
+ if(memcmp(laststates, states, sizeof(states)))
+ break;
+ };
+
+ /* save current states */
+ memcpy(laststates, states, sizeof(states));
+
+ /* print core states */
+ for(int i = 0; i < NUM_PROGRAMS; i++) {
+ printf("CORE %i: ", i);
+ switch(states[i]) {
+ case -1: printf("done "); break;
+ default: printf("state %i ", states[i]); done=0; break;
+ }
+ printf("\n");
+ }
+ printf("CORE14: 0x%x\n", states[14]);
+ printf("CORE15: 0x%x\n", states[15]);
+
+ /* stop polling if all cores finished */
+ if(done) break;
+ }
+ /* =============================================================== */
+
+ printf("All cores finished.\n");
+
+ /* read whole shm buffer */
+ if(e_read(&emem, 0, 0, (off_t)0, &shm, sizeof(shm)) == E_ERR)
+ FAIL("Can't read full shm!\n");
+
+ /* print buffer 0 and buffer 1 */
+ for(int i = 0; i < 16; i++) {
+ printf("0x%x ", shm.buf0.buf[i]);
+ } printf("\n");
+ for(int i = 0; i < 16; i++) {
+ printf("0x%x ", shm.buf1.buf[i]);
+ } printf("\n");
+
+
+ for(int i = 0; i < 4; i++) {
+ printf("%.2f\t", ((float*)shm.buf0.buf)[i]);
+ } printf("\n");
+
+ for(int i = 0; i < 8; i++) {
+ printf("%.2f\t", ((float*)shm.buf1.buf)[i]);
+ } printf("\n");
+
+ /* free shm buffer, close workgroup and finalize */
+ if(e_free(&emem) != E_OK)
+ FAIL("Can't free!\n");
+ if(e_close(&dev) != E_OK)
+ FAIL("Can't close!\n");
+ if(e_finalize() != E_OK)
+ FAIL("Can't finalize!\n");
+
+ return(0);
+}
+