forked from parallella/parallella-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemain.c
161 lines (151 loc) · 6.57 KB
/
emain.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Author: Noémien Kocher
* Date: january 2016
* Licence: MIT
* Purpose:
* This file is run by each eCore, it represents the life of a cell. Until it
* is stopped, loops over and check its neighbor so as to update its status.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <e-lib.h> // Epiphany cores library
#define READY 0x1
#define DEAD 'O'
#define ALIVE 'X'
/*
* [1] status, O = dead X = alive
* [0] ready, 0 = not 1 = ready
*/
char swap[8] SECTION(".text_bank2");
volatile char *result;
volatile uint32_t *status;
volatile uint32_t *state;
int main(void) {
unsigned core_row, core_col,
group_rows, group_cols,
core_num;
char neighbor_status;
uint32_t iterations = 0;
uint32_t iof = 0; // Sticky Integer Overflow Flag
core_row = e_group_config.core_row;
core_col = e_group_config.core_col;
group_rows = e_group_config.group_rows;
group_cols = e_group_config.group_cols;
core_num = core_row * group_cols + core_col;
// our swap, could (should) put it in a structure
swap[1] = DEAD;
swap[0] = READY;
// starts at the beginning of sdram
result = (volatile char *) (0x8f000000 + 0x1*core_num); // writing to external memory, writing 4bytes
// we add offset of 0x10 = 16 = ncores * sizeof(char)
status = (volatile uint32_t*) (0x8f000010 + 0x4*core_num);
// we add offset of 0x50 = 16 + 16 * sizeof(uint32_t)
state = (volatile uint32_t*) (0x8f000050 + 0x4*core_num);
unsigned alive_neighbor;
while(1) {
iterations++; // increment number of iteration
unsigned tmp_iof = e_reg_read(E_REG_STATUS);
tmp_iof = tmp_iof & (4096); // use the sticky overflow integer flag
iof = iof | tmp_iof;
alive_neighbor = 0;
// top left
if(core_row == 0 && core_col == 0) {
alive_neighbor += 5; // dead anyway!
}
// top right
else if(core_row == 0 && core_col == group_cols-1) {
alive_neighbor += 5; // dead anyway!
}
// bottom left
else if(core_row == group_rows-1 && core_col == 0) {
alive_neighbor += 5; // dead anyway!
}
// bottom right
else if(core_row == group_rows-1 && core_col == group_cols-1) {
alive_neighbor += 5; // dead anyway!
}
// top
else if(core_row == 0) {
alive_neighbor += 3;
e_read(&e_group_config,&neighbor_status,core_row,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
}
// bottom
else if(core_row == group_rows-1) {
alive_neighbor += 3;
e_read(&e_group_config,&neighbor_status,core_row,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
}
// left
else if(core_col == 0) {
alive_neighbor += 3;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row+1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row+1,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
}
// right
else if(core_col == group_cols-1) {
alive_neighbor += 3;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row+1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row+1,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
}
// middle
else {
e_read(&e_group_config,&neighbor_status,core_row-1,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row-1,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row+1,core_col-1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row+1,core_col,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
e_read(&e_group_config,&neighbor_status,core_row+1,core_col+1,(char*)0x4001,1);
if(neighbor_status == ALIVE) alive_neighbor++;
}
if(alive_neighbor == 3) swap[1] = ALIVE;
else if(alive_neighbor < 2) swap[1] = DEAD;
else if(alive_neighbor > 3) swap[1] = DEAD;
*result = swap[1]; // store result
*status = iterations; // store number of iterations so far
*state = iof;
}
}