1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < Conway's Game of Life > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
7 |
|
|
// # #
|
8 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
9 |
|
|
// # permitted provided that the following conditions are met: #
|
10 |
|
|
// # #
|
11 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
12 |
|
|
// # conditions and the following disclaimer. #
|
13 |
|
|
// # #
|
14 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
15 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
16 |
|
|
// # provided with the distribution. #
|
17 |
|
|
// # #
|
18 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
19 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
20 |
|
|
// # permission. #
|
21 |
|
|
// # #
|
22 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
23 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
24 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
25 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
26 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
27 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
28 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
29 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
30 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
31 |
|
|
// # ********************************************************************************************* #
|
32 |
|
|
// # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
// Libraries
|
37 |
|
|
#include <stdint.h>
|
38 |
|
|
#include <neo430.h>
|
39 |
|
|
|
40 |
|
|
// Configuration
|
41 |
|
|
#define NUM_CELLS_X 160 // must be a multiple of 8
|
42 |
|
|
#define NUM_CELLS_Y 40
|
43 |
|
|
#define BAUD_RATE 19200
|
44 |
|
|
#define GEN_DELAY 500 // delay between iterations in ms
|
45 |
|
|
#define TRNG_TAP_MASK 0b01010001000000 // highly experimental!
|
46 |
|
|
|
47 |
|
|
// Global variables
|
48 |
|
|
uint8_t universe[2][NUM_CELLS_X/8][NUM_CELLS_Y];
|
49 |
|
|
|
50 |
|
|
// Prototypes
|
51 |
|
|
void clear_universe(uint8_t u);
|
52 |
|
|
void set_cell(uint8_t u, int16_t x, int16_t y);
|
53 |
|
|
uint8_t get_cell(uint8_t u, int16_t x, int16_t y);
|
54 |
|
|
uint8_t get_neighborhood(uint8_t u, int16_t x, int16_t y);
|
55 |
|
|
void print_universe(uint8_t u);
|
56 |
|
|
uint16_t pop_count(uint8_t u);
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/* ------------------------------------------------------------
|
60 |
|
|
* INFO Main function
|
61 |
|
|
* ------------------------------------------------------------ */
|
62 |
|
|
int main(void) {
|
63 |
|
|
|
64 |
|
|
uint8_t u = 0, cell = 0, n = 0;
|
65 |
|
|
int16_t x, y;
|
66 |
|
|
uint16_t trng_available = 0;
|
67 |
|
|
uint8_t trng_data = 0;
|
68 |
|
|
|
69 |
|
|
// setup UART
|
70 |
|
|
neo430_uart_setup(BAUD_RATE);
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
// initialize universe
|
74 |
|
|
uint32_t generation = 0;
|
75 |
|
|
clear_universe(0);
|
76 |
|
|
clear_universe(1);
|
77 |
|
|
|
78 |
|
|
neo430_printf("\n\n<<< Conways's Game of Life >>>\n\n");
|
79 |
|
|
neo430_printf("This program requires a terminal resolution of at least %ux%u characters.\n", NUM_CELLS_X+2, NUM_CELLS_Y+3);
|
80 |
|
|
neo430_printf("Press any key to start a random-initialized torus-style universe of %ux%u cells.\n", NUM_CELLS_X, NUM_CELLS_Y);
|
81 |
|
|
neo430_printf("You can pause/restart the simulation by pressing any key.\n");
|
82 |
|
|
|
83 |
|
|
// check if TRNG was synthesized
|
84 |
|
|
if ((SYS_FEATURES & (1<<SYS_TRNG_EN))) {
|
85 |
|
|
neo430_printf("TRNG detected. Using TRNG for universe initialization.\n");
|
86 |
|
|
neo430_trng_enable(TRNG_TAP_MASK);
|
87 |
|
|
trng_available = 1;
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
if (trng_available) {
|
91 |
|
|
neo430_uart_getc(); // wait for pressed key
|
92 |
|
|
}
|
93 |
|
|
else {
|
94 |
|
|
// randomize until key pressed
|
95 |
|
|
while (neo430_uart_char_received() == 0) {
|
96 |
|
|
neo430_xorshift32();
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
// initialize universe using random data
|
101 |
|
|
for (x=0; x<NUM_CELLS_X/8; x++) {
|
102 |
|
|
for (y=0; y<NUM_CELLS_Y; y++) {
|
103 |
|
|
if (trng_available) {
|
104 |
|
|
if (neo430_trng_get(&trng_data)) {
|
105 |
|
|
neo430_printf("TRNG error!\n");
|
106 |
|
|
return 1;
|
107 |
|
|
}
|
108 |
|
|
universe[0][x][y] = (uint8_t)trng_data; // use data from TRNG
|
109 |
|
|
}
|
110 |
|
|
else {
|
111 |
|
|
universe[0][x][y] = (uint8_t)neo430_xorshift32(); // use data von PRNG
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
while(1) {
|
117 |
|
|
|
118 |
|
|
// user abort?
|
119 |
|
|
if (neo430_uart_char_received()) {
|
120 |
|
|
neo430_printf("\nRestart (y/n)?");
|
121 |
|
|
if (neo430_uart_getc() == 'y') {
|
122 |
|
|
neo430_soft_reset();
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
// print generation, population count and the current universe
|
127 |
|
|
neo430_printf("\n\nGeneration %l: %u/%u living cells\n", generation, pop_count(u), NUM_CELLS_X*NUM_CELLS_Y);
|
128 |
|
|
print_universe(u);
|
129 |
|
|
|
130 |
|
|
// compute next generation
|
131 |
|
|
clear_universe((u + 1) & 1);
|
132 |
|
|
|
133 |
|
|
for (x=0; x<NUM_CELLS_X; x++) {
|
134 |
|
|
for (y=0; y<NUM_CELLS_Y; y++) {
|
135 |
|
|
|
136 |
|
|
cell = get_cell(u, x, y); // state of current cell
|
137 |
|
|
n = get_neighborhood(u, x, y); // number of living neighbor cells
|
138 |
|
|
|
139 |
|
|
// classic rule set
|
140 |
|
|
if (((cell == 0) && (n == 3)) || ((cell != 0) && ((n == 2) || (n == 3)))) {
|
141 |
|
|
set_cell((u + 1) & 1, x, y);
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
} // y
|
145 |
|
|
} // x
|
146 |
|
|
u = (u + 1) & 1; // switch universe
|
147 |
|
|
generation++;
|
148 |
|
|
|
149 |
|
|
// wait 500ms
|
150 |
|
|
neo430_cpu_delay_ms(GEN_DELAY);
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
return 0;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
/* ------------------------------------------------------------
|
158 |
|
|
* INFO Print universe u to console
|
159 |
|
|
* ------------------------------------------------------------ */
|
160 |
|
|
void print_universe(uint8_t u){
|
161 |
|
|
|
162 |
|
|
int16_t x, y;
|
163 |
|
|
|
164 |
|
|
neo430_uart_putc('+');
|
165 |
|
|
for (x=0; x<NUM_CELLS_X; x++) {
|
166 |
|
|
neo430_uart_putc('-');
|
167 |
|
|
}
|
168 |
|
|
neo430_uart_putc('+');
|
169 |
|
|
neo430_uart_putc('\r');
|
170 |
|
|
neo430_uart_putc('\n');
|
171 |
|
|
|
172 |
|
|
for (y=0; y<NUM_CELLS_Y; y++) {
|
173 |
|
|
neo430_uart_putc('|');
|
174 |
|
|
|
175 |
|
|
for (x=0; x<NUM_CELLS_X; x++) {
|
176 |
|
|
if (get_cell(u, x, y))
|
177 |
|
|
neo430_uart_putc('#');
|
178 |
|
|
else
|
179 |
|
|
neo430_uart_putc(' ');
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
// end of line
|
183 |
|
|
neo430_uart_putc('|');
|
184 |
|
|
neo430_uart_putc('\r');
|
185 |
|
|
neo430_uart_putc('\n');
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
neo430_uart_putc('+');
|
189 |
|
|
for (x=0; x<NUM_CELLS_X; x++) {
|
190 |
|
|
neo430_uart_putc('-');
|
191 |
|
|
}
|
192 |
|
|
neo430_uart_putc('+');
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
/* ------------------------------------------------------------
|
197 |
|
|
* INFO Clear universe u
|
198 |
|
|
* ------------------------------------------------------------ */
|
199 |
|
|
void clear_universe(uint8_t u){
|
200 |
|
|
|
201 |
|
|
uint16_t x, y;
|
202 |
|
|
|
203 |
|
|
for (x=0; x<NUM_CELLS_X/8; x++) {
|
204 |
|
|
for (y=0; y<NUM_CELLS_Y; y++) {
|
205 |
|
|
universe[u][x][y] = 0;
|
206 |
|
|
}
|
207 |
|
|
}
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
|
211 |
|
|
/* ------------------------------------------------------------
|
212 |
|
|
* INFO Set single cell (make ALIVE) in uinverse u
|
213 |
|
|
* ------------------------------------------------------------ */
|
214 |
|
|
void set_cell(uint8_t u, int16_t x, int16_t y){
|
215 |
|
|
|
216 |
|
|
if ((x >= NUM_CELLS_X) || (y >= NUM_CELLS_Y))
|
217 |
|
|
return; // out of range
|
218 |
|
|
|
219 |
|
|
universe[u][x>>3][y] |= 1 << (7 - (x & 7));
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
/* ------------------------------------------------------------
|
224 |
|
|
* INFO Get state of cell
|
225 |
|
|
* RETURN Cell state (DEAD or ALIVE)
|
226 |
|
|
* ------------------------------------------------------------ */
|
227 |
|
|
uint8_t get_cell(uint8_t u, int16_t x, int16_t y){
|
228 |
|
|
|
229 |
|
|
// range check: wrap around -> torus-style universe
|
230 |
|
|
if (x < 0)
|
231 |
|
|
x = NUM_CELLS_X-1;
|
232 |
|
|
|
233 |
|
|
if (x > NUM_CELLS_X-1)
|
234 |
|
|
x = 0;
|
235 |
|
|
|
236 |
|
|
if (y < 0)
|
237 |
|
|
y = NUM_CELLS_Y-1;
|
238 |
|
|
|
239 |
|
|
if (y > NUM_CELLS_Y-1)
|
240 |
|
|
y = 0;
|
241 |
|
|
|
242 |
|
|
// check bit according to cell
|
243 |
|
|
uint8_t tmp = universe[u][x>>3][y];
|
244 |
|
|
tmp &= 1 << (7 - (x & 7));
|
245 |
|
|
|
246 |
|
|
if (tmp == 0)
|
247 |
|
|
return 0; // DEAD
|
248 |
|
|
else
|
249 |
|
|
return 1; // ALIVE
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
/* ------------------------------------------------------------
|
254 |
|
|
* INFO Get number of alive cells in direct neigborhood
|
255 |
|
|
* RETURN Number of set cells in neigborhood
|
256 |
|
|
* ------------------------------------------------------------ */
|
257 |
|
|
uint8_t get_neighborhood(uint8_t u, int16_t x, int16_t y){
|
258 |
|
|
|
259 |
|
|
// Cell index layout:
|
260 |
|
|
// 012
|
261 |
|
|
// 3#4
|
262 |
|
|
// 567
|
263 |
|
|
|
264 |
|
|
uint8_t num = 0;
|
265 |
|
|
num += get_cell(u, x-1, y-1); // 0
|
266 |
|
|
num += get_cell(u, x, y-1); // 1
|
267 |
|
|
num += get_cell(u, x+1, y-1); // 2
|
268 |
|
|
num += get_cell(u, x-1, y); // 3
|
269 |
|
|
num += get_cell(u, x+1, y); // 4
|
270 |
|
|
num += get_cell(u, x-1, y+1); // 5
|
271 |
|
|
num += get_cell(u, x, y+1); // 6
|
272 |
|
|
num += get_cell(u, x+1, y+1); // 7
|
273 |
|
|
|
274 |
|
|
return num;
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
|
278 |
|
|
/* ------------------------------------------------------------
|
279 |
|
|
* INFO Population count
|
280 |
|
|
* RETURN 16-bit number of living cells in universe u
|
281 |
|
|
* ------------------------------------------------------------ */
|
282 |
|
|
uint16_t pop_count(uint8_t u) {
|
283 |
|
|
|
284 |
|
|
uint16_t x, y, cnt;
|
285 |
|
|
|
286 |
|
|
cnt = 0;
|
287 |
|
|
for (x=0; x<NUM_CELLS_X; x++) {
|
288 |
|
|
for (y=0; y<NUM_CELLS_Y; y++) {
|
289 |
|
|
cnt += (uint16_t)get_cell(u, x, y);
|
290 |
|
|
}
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
return cnt;
|
294 |
|
|
}
|