OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [game_of_life/] [main.c] - Blame information for rev 43

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2
// # << NEORV32 - 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 NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37 20 zero_gravi
 * @file game_of_life/main.c
38 2 zero_gravi
 * @author Stephan Nolting
39
 * @brief Simple blinking LED demo program using the lowest 8 bits of the GPIO.output port.
40
 **************************************************************************/
41
 
42
#include <neorv32.h>
43
 
44
 
45
/**********************************************************************//**
46
 * @name User configuration
47
 **************************************************************************/
48
/**@{*/
49
/** UART BAUD rate */
50
#define BAUD_RATE     19200
51
/** Universe x size (has to be a multiple of 8) */
52
#define NUM_CELLS_X   160
53
/** Universe y size */
54
#define NUM_CELLS_Y   40
55
/** Delay between generations in ms */
56
#define GEN_DELAY     500
57 22 zero_gravi
/** Symbol for dead cell */
58
#define CELL_DEAD  (' ')
59
/** Symbol for alive cell */
60
#define CELL_ALIVE ('#')
61 2 zero_gravi
/**@}*/
62
 
63
 
64
 
65
/**********************************************************************//**
66
 * The universe
67
 **************************************************************************/
68
uint8_t universe[2][NUM_CELLS_X/8][NUM_CELLS_Y];
69
 
70
// Prototypes
71
void clear_universe(int u);
72
void set_cell(int u, int x, int y);
73
int get_cell(int u, int x, int y);
74
int get_neighborhood(int u, int x, int y);
75
void print_universe(int u);
76
int pop_count(int u);
77
uint32_t xorshift32(void);
78
 
79
 
80
/**********************************************************************//**
81
 * Conway's Game of Life.
82
 *
83
 * @note This program requires the UART to be synthesized (the TRNG is optional).
84
 *
85
 * @return Irrelevant.
86
 **************************************************************************/
87
int main(void) {
88
 
89
  // check if UART unit is implemented at all
90
  if (neorv32_uart_available() == 0) {
91
    return 0;
92
  }
93
 
94
 
95
  // capture all exceptions and give debug info via UART
96
  // this is not required, but keeps us safe
97 14 zero_gravi
  neorv32_rte_setup();
98 2 zero_gravi
 
99
 
100 42 zero_gravi
  // init UART at default baud rate, no parity bits, no rx interrupt, no tx interrupt
101
  neorv32_uart_setup(BAUD_RATE, 0b00, 0, 0);
102 2 zero_gravi
 
103
 
104
  while (1) {
105
 
106
    int u = 0, cell = 0, n = 0;
107
    int x, y;
108
    int trng_available = 0;
109 23 zero_gravi
    uint8_t trng_data;
110 2 zero_gravi
 
111
 
112
    // initialize universe
113
    uint32_t generation = 0;
114
    clear_universe(0);
115
    clear_universe(1);
116
 
117
    // intro
118
    neorv32_uart_printf("\n\n<<< Conways's Game of Life >>>\n\n");
119
    neorv32_uart_printf("This program requires a terminal resolution of at least %ux%u characters.\n", NUM_CELLS_X+2, NUM_CELLS_Y+3);
120
    neorv32_uart_printf("Press any key to start a random-initialized torus-style universe of %ux%u cells.\n", NUM_CELLS_X, NUM_CELLS_Y);
121
    neorv32_uart_printf("You can pause/restart the simulation by pressing any key.\n");
122
 
123
 
124
    // check if TRNG was synthesized
125
    if (neorv32_trng_available()) {
126 23 zero_gravi
      neorv32_uart_printf("\nTRNG detected. Using TRNG for universe initialization.\n");
127
      neorv32_trng_enable();
128 2 zero_gravi
      trng_available = 1;
129
    }
130
 
131
 
132
    // randomize until key pressed
133
    while (neorv32_uart_char_received() == 0) {
134
      xorshift32();
135
    }
136
 
137
 
138
    // initialize universe using random data
139
    for (x=0; x<NUM_CELLS_X/8; x++) {
140
      for (y=0; y<NUM_CELLS_Y; y++) {
141
        if (trng_available) {
142 23 zero_gravi
          while (1) {
143
            int err = neorv32_trng_get(&trng_data);
144
            if (err) {
145
              neorv32_uart_printf("TRNG error (%i)! Restarting TRNG...\n", err);
146
              continue;
147
            }
148
            else {
149
              break;
150
            }
151 2 zero_gravi
          }
152 23 zero_gravi
          universe[0][x][y] = trng_data; // use data from TRNG
153 2 zero_gravi
        }
154
        else {
155
          universe[0][x][y] = (uint8_t)xorshift32(); // use data from PRNG
156
        }
157
      }
158
    }
159
 
160
 
161
    while(1) {
162
 
163
      // user abort?
164
      if (neorv32_uart_char_received()) {
165
        neorv32_uart_printf("\nRestart (y/n)?");
166
        if (neorv32_uart_getc() == 'y') {
167
          break;
168
        }
169
      }
170
 
171
      // print generation, population count and the current universe
172
      neorv32_uart_printf("\n\nGeneration %u: %u/%u living cells\n", (uint32_t)generation, (uint32_t)pop_count(u), NUM_CELLS_X*NUM_CELLS_Y);
173
      print_universe(u);
174
 
175
      // compute next generation
176
      clear_universe((u + 1) & 1);
177
 
178
      for (x=0; x<NUM_CELLS_X; x++) {
179
        for (y=0; y<NUM_CELLS_Y; y++) {
180
 
181
          cell = get_cell(u, x, y); // state of current cell
182
          n = get_neighborhood(u, x, y); // number of living neighbor cells
183
 
184 22 zero_gravi
          // -- classic rule set --
185
          // if center cell is dead -> cell comes to life when there are exactly 3 living cells around
186
          // if center cell is alive -> stay alive if there are 2 or three living cells around
187
          // else -> cell is/becomes dead
188 2 zero_gravi
          if (((cell == 0) && (n == 3)) || ((cell != 0) && ((n == 2) || (n == 3)))) {
189
            set_cell((u + 1) & 1, x, y);
190
          }
191
 
192
        } // y
193
      } // x
194
      u = (u + 1) & 1; // switch universe
195
      generation++;
196
 
197
      // wait GEN_DELAY ms
198
      neorv32_cpu_delay_ms(GEN_DELAY);
199
    }
200
 
201
  }
202
 
203
  return 0;
204
}
205
 
206
 
207
/**********************************************************************//**
208
 * Print universe via UARt.
209
 *
210
 * @param[in] u Universe select (0 or 1).
211
 **************************************************************************/
212
void print_universe(int u){
213
 
214
  int16_t x, y;
215
 
216
  neorv32_uart_putc('+');
217
  for (x=0; x<NUM_CELLS_X; x++) {
218
    neorv32_uart_putc('-');
219
  }
220
  neorv32_uart_putc('+');
221
  neorv32_uart_putc('\r');
222
  neorv32_uart_putc('\n');
223
 
224
  for (y=0; y<NUM_CELLS_Y; y++) {
225
    neorv32_uart_putc('|');
226
 
227
    for (x=0; x<NUM_CELLS_X; x++) {
228
      if (get_cell(u, x, y))
229 22 zero_gravi
        neorv32_uart_putc((char)CELL_ALIVE);
230 2 zero_gravi
      else
231 22 zero_gravi
        neorv32_uart_putc((char)CELL_DEAD);
232 2 zero_gravi
    }
233
 
234
    // end of line
235
    neorv32_uart_putc('|');
236
    neorv32_uart_putc('\r');
237
    neorv32_uart_putc('\n');
238
  }
239
 
240
  neorv32_uart_putc('+');
241
  for (x=0; x<NUM_CELLS_X; x++) {
242
    neorv32_uart_putc('-');
243
  }
244
  neorv32_uart_putc('+');
245
}
246
 
247
 
248
/**********************************************************************//**
249
 * Kill all cells in universe.
250
 *
251
 * @param[in] u Universe select (0 or 1).
252
 **************************************************************************/
253
void clear_universe(int u){
254
 
255
  uint16_t x, y;
256
 
257
  for (x=0; x<NUM_CELLS_X/8; x++) {
258
    for (y=0; y<NUM_CELLS_Y; y++) {
259
      universe[u][x][y] = 0;
260
    }
261
  }
262
}
263
 
264
 
265
/**********************************************************************//**
266
 * Make cell alive.
267
 *
268
 * @param[in] u Universe select (0 or 1).
269
 * @param[in] x X coordinate of cell.
270
 * @param[in] y Y coordinate of cell.
271
 **************************************************************************/
272
void set_cell(int u, int x, int y){
273
 
274
  if ((x >= NUM_CELLS_X) || (y >= NUM_CELLS_Y))
275
    return; // out of range
276
 
277
  universe[u][x>>3][y] |= (uint8_t)(1 << (7 - (x & 7)));
278
}
279
 
280
 
281
/**********************************************************************//**
282
 * Get state of cell.
283
 *
284
 * @param[in] u Universe select (0 or 1).
285
 * @param[in] x X coordinate of cell.
286
 * @param[in] y Y coordinate of cell.
287
 * @return Cell is dead when 0, cell is alive when 1.
288
 **************************************************************************/
289
int get_cell(int u, int x, int y){
290
 
291
  // range check: wrap around -> torus-style universe
292
  if (x < 0)
293
    x = NUM_CELLS_X-1;
294
 
295
  if (x > NUM_CELLS_X-1)
296
    x = 0;
297
 
298
  if (y < 0)
299
    y = NUM_CELLS_Y-1;
300
 
301
  if (y > NUM_CELLS_Y-1)
302
    y = 0;
303
 
304
  // check bit according to cell
305
  uint8_t tmp = universe[u][x>>3][y];
306
  tmp &= 1 << (7 - (x & 7));
307
 
308
  if (tmp == 0)
309
    return 0; // DEAD
310
  else
311
    return 1; // ALIVE
312
}
313
 
314
 
315
/**********************************************************************//**
316
 * Get number of living cells in neighborhood.
317
 *
318
 * @param[in] u Universe select (0 or 1).
319
 * @param[in] x X coordinate of the neighborhood's center cell.
320
 * @param[in] y Y coordinate of the neighborhood's center cell.
321
 * @return Number of living cells in neighborhood (0..9).
322
 **************************************************************************/
323
int get_neighborhood(int u, int x, int y){
324
 
325
// Cell index layout:
326
// 012
327
// 3#4
328
// 567
329
 
330
  int num = 0;
331
  num += get_cell(u, x-1, y-1); // 0
332
  num += get_cell(u, x,   y-1); // 1
333
  num += get_cell(u, x+1, y-1); // 2
334
  num += get_cell(u, x-1, y);   // 3
335
  num += get_cell(u, x+1, y);   // 4
336
  num += get_cell(u, x-1, y+1); // 5
337
  num += get_cell(u, x,   y+1); // 6
338
  num += get_cell(u, x+1, y+1); // 7
339
 
340
  return num;
341
}
342
 
343
 
344
/**********************************************************************//**
345
 * Count living cells in universe.
346
 *
347
 * @param[in] u Universe select (0 or 1).
348
 * @return Number of living cells.
349
 **************************************************************************/
350
int pop_count(int u) {
351
 
352
  int x, y, cnt;
353
 
354
  cnt = 0;
355
  for (x=0; x<NUM_CELLS_X; x++) {
356
    for (y=0; y<NUM_CELLS_Y; y++) {
357
      cnt += (int)get_cell(u, x, y);
358
    }
359
  }
360
 
361
  return cnt;
362
}
363
 
364
 
365
/**********************************************************************//**
366
 * Simple pseudo random number generator.
367
 *
368
 * @return Random number.
369
 **************************************************************************/
370
uint32_t xorshift32(void) {
371
 
372
  static uint32_t x32 = 314159265;
373
 
374
  x32 ^= x32 << 13;
375
  x32 ^= x32 >> 17;
376
  x32 ^= x32 << 5;
377
 
378
  return x32;
379
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.