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 22

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
  // init UART at default baud rate, no rx interrupt, no tx interrupt
101
  neorv32_uart_setup(BAUD_RATE, 0, 0);
102
 
103
 
104
  while (1) {
105
 
106
    int u = 0, cell = 0, n = 0;
107
    int x, y;
108
    int trng_available = 0;
109
    uint16_t trng_data;
110
 
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
      uint16_t trng_tap_config = neorv32_trng_find_tap_mask();
127
      neorv32_trng_setup(trng_tap_config);
128
      neorv32_uart_printf("\nTRNG detected (tap mask 0x%x). Using TRNG for universe initialization.\n", (uint32_t)trng_tap_config);
129
      trng_available = 1;
130
    }
131
 
132
 
133
    // randomize until key pressed
134
    while (neorv32_uart_char_received() == 0) {
135
      xorshift32();
136
    }
137
 
138
 
139
    // initialize universe using random data
140
    for (x=0; x<NUM_CELLS_X/8; x++) {
141
      for (y=0; y<NUM_CELLS_Y; y++) {
142
        if (trng_available) {
143
          if (neorv32_trng_get(&trng_data)) {
144
            neorv32_uart_printf("TRNG error!\n");
145
            return 1;
146
          }
147
          universe[0][x][y] = (uint8_t)trng_data; // use data from TRNG
148
        }
149
        else {
150
          universe[0][x][y] = (uint8_t)xorshift32(); // use data from PRNG
151
        }
152
      }
153
    }
154
 
155
 
156
    while(1) {
157
 
158
      // user abort?
159
      if (neorv32_uart_char_received()) {
160
        neorv32_uart_printf("\nRestart (y/n)?");
161
        if (neorv32_uart_getc() == 'y') {
162
          break;
163
        }
164
      }
165
 
166
      // print generation, population count and the current universe
167
      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);
168
      print_universe(u);
169
 
170
      // compute next generation
171
      clear_universe((u + 1) & 1);
172
 
173
      for (x=0; x<NUM_CELLS_X; x++) {
174
        for (y=0; y<NUM_CELLS_Y; y++) {
175
 
176
          cell = get_cell(u, x, y); // state of current cell
177
          n = get_neighborhood(u, x, y); // number of living neighbor cells
178
 
179 22 zero_gravi
          // -- classic rule set --
180
          // if center cell is dead -> cell comes to life when there are exactly 3 living cells around
181
          // if center cell is alive -> stay alive if there are 2 or three living cells around
182
          // else -> cell is/becomes dead
183 2 zero_gravi
          if (((cell == 0) && (n == 3)) || ((cell != 0) && ((n == 2) || (n == 3)))) {
184
            set_cell((u + 1) & 1, x, y);
185
          }
186
 
187
        } // y
188
      } // x
189
      u = (u + 1) & 1; // switch universe
190
      generation++;
191
 
192
      // wait GEN_DELAY ms
193
      neorv32_cpu_delay_ms(GEN_DELAY);
194
    }
195
 
196
  }
197
 
198
  return 0;
199
}
200
 
201
 
202
/**********************************************************************//**
203
 * Print universe via UARt.
204
 *
205
 * @param[in] u Universe select (0 or 1).
206
 **************************************************************************/
207
void print_universe(int u){
208
 
209
  int16_t x, y;
210
 
211
  neorv32_uart_putc('+');
212
  for (x=0; x<NUM_CELLS_X; x++) {
213
    neorv32_uart_putc('-');
214
  }
215
  neorv32_uart_putc('+');
216
  neorv32_uart_putc('\r');
217
  neorv32_uart_putc('\n');
218
 
219
  for (y=0; y<NUM_CELLS_Y; y++) {
220
    neorv32_uart_putc('|');
221
 
222
    for (x=0; x<NUM_CELLS_X; x++) {
223
      if (get_cell(u, x, y))
224 22 zero_gravi
        neorv32_uart_putc((char)CELL_ALIVE);
225 2 zero_gravi
      else
226 22 zero_gravi
        neorv32_uart_putc((char)CELL_DEAD);
227 2 zero_gravi
    }
228
 
229
    // end of line
230
    neorv32_uart_putc('|');
231
    neorv32_uart_putc('\r');
232
    neorv32_uart_putc('\n');
233
  }
234
 
235
  neorv32_uart_putc('+');
236
  for (x=0; x<NUM_CELLS_X; x++) {
237
    neorv32_uart_putc('-');
238
  }
239
  neorv32_uart_putc('+');
240
}
241
 
242
 
243
/**********************************************************************//**
244
 * Kill all cells in universe.
245
 *
246
 * @param[in] u Universe select (0 or 1).
247
 **************************************************************************/
248
void clear_universe(int u){
249
 
250
  uint16_t x, y;
251
 
252
  for (x=0; x<NUM_CELLS_X/8; x++) {
253
    for (y=0; y<NUM_CELLS_Y; y++) {
254
      universe[u][x][y] = 0;
255
    }
256
  }
257
}
258
 
259
 
260
/**********************************************************************//**
261
 * Make cell alive.
262
 *
263
 * @param[in] u Universe select (0 or 1).
264
 * @param[in] x X coordinate of cell.
265
 * @param[in] y Y coordinate of cell.
266
 **************************************************************************/
267
void set_cell(int u, int x, int y){
268
 
269
  if ((x >= NUM_CELLS_X) || (y >= NUM_CELLS_Y))
270
    return; // out of range
271
 
272
  universe[u][x>>3][y] |= (uint8_t)(1 << (7 - (x & 7)));
273
}
274
 
275
 
276
/**********************************************************************//**
277
 * Get state of cell.
278
 *
279
 * @param[in] u Universe select (0 or 1).
280
 * @param[in] x X coordinate of cell.
281
 * @param[in] y Y coordinate of cell.
282
 * @return Cell is dead when 0, cell is alive when 1.
283
 **************************************************************************/
284
int get_cell(int u, int x, int y){
285
 
286
  // range check: wrap around -> torus-style universe
287
  if (x < 0)
288
    x = NUM_CELLS_X-1;
289
 
290
  if (x > NUM_CELLS_X-1)
291
    x = 0;
292
 
293
  if (y < 0)
294
    y = NUM_CELLS_Y-1;
295
 
296
  if (y > NUM_CELLS_Y-1)
297
    y = 0;
298
 
299
  // check bit according to cell
300
  uint8_t tmp = universe[u][x>>3][y];
301
  tmp &= 1 << (7 - (x & 7));
302
 
303
  if (tmp == 0)
304
    return 0; // DEAD
305
  else
306
    return 1; // ALIVE
307
}
308
 
309
 
310
/**********************************************************************//**
311
 * Get number of living cells in neighborhood.
312
 *
313
 * @param[in] u Universe select (0 or 1).
314
 * @param[in] x X coordinate of the neighborhood's center cell.
315
 * @param[in] y Y coordinate of the neighborhood's center cell.
316
 * @return Number of living cells in neighborhood (0..9).
317
 **************************************************************************/
318
int get_neighborhood(int u, int x, int y){
319
 
320
// Cell index layout:
321
// 012
322
// 3#4
323
// 567
324
 
325
  int num = 0;
326
  num += get_cell(u, x-1, y-1); // 0
327
  num += get_cell(u, x,   y-1); // 1
328
  num += get_cell(u, x+1, y-1); // 2
329
  num += get_cell(u, x-1, y);   // 3
330
  num += get_cell(u, x+1, y);   // 4
331
  num += get_cell(u, x-1, y+1); // 5
332
  num += get_cell(u, x,   y+1); // 6
333
  num += get_cell(u, x+1, y+1); // 7
334
 
335
  return num;
336
}
337
 
338
 
339
/**********************************************************************//**
340
 * Count living cells in universe.
341
 *
342
 * @param[in] u Universe select (0 or 1).
343
 * @return Number of living cells.
344
 **************************************************************************/
345
int pop_count(int u) {
346
 
347
  int x, y, cnt;
348
 
349
  cnt = 0;
350
  for (x=0; x<NUM_CELLS_X; x++) {
351
    for (y=0; y<NUM_CELLS_Y; y++) {
352
      cnt += (int)get_cell(u, x, y);
353
    }
354
  }
355
 
356
  return cnt;
357
}
358
 
359
 
360
/**********************************************************************//**
361
 * Simple pseudo random number generator.
362
 *
363
 * @return Random number.
364
 **************************************************************************/
365
uint32_t xorshift32(void) {
366
 
367
  static uint32_t x32 = 314159265;
368
 
369
  x32 ^= x32 << 13;
370
  x32 ^= x32 >> 17;
371
  x32 ^= x32 << 5;
372
 
373
  return x32;
374
}

powered by: WebSVN 2.1.0

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