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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [game_of_life/] [main.c] - Diff between revs 60 and 65

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 60 Rev 65
Line 85... Line 85...
 * @return 0 if execution was successful
 * @return 0 if execution was successful
 **************************************************************************/
 **************************************************************************/
int main(void) {
int main(void) {
 
 
  // check if UART unit is implemented at all
  // check if UART unit is implemented at all
  if (neorv32_uart_available() == 0) {
  if (neorv32_uart0_available() == 0) {
    return 1;
    return 1;
  }
  }
 
 
 
 
  // capture all exceptions and give debug info via UART
  // capture all exceptions and give debug info via UART
  // this is not required, but keeps us safe
  // this is not required, but keeps us safe
  neorv32_rte_setup();
  neorv32_rte_setup();
 
 
 
 
  // init UART at default baud rate, no parity bits, ho hw flow control
  // init UART at default baud rate, no parity bits, ho hw flow control
  neorv32_uart_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
  neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
 
 
  // check available hardware extensions and compare with compiler flags
  // check available hardware extensions and compare with compiler flags
  neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
  neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
 
 
 
 
Line 116... Line 116...
    uint32_t generation = 0;
    uint32_t generation = 0;
    clear_universe(0);
    clear_universe(0);
    clear_universe(1);
    clear_universe(1);
 
 
    // intro
    // intro
    neorv32_uart_printf("\n\n<<< Conways's Game of Life >>>\n\n");
    neorv32_uart0_printf("\n\n<<< Conways's Game of Life >>>\n\n");
    neorv32_uart_printf("This program requires a terminal resolution of at least %ux%u characters.\n", NUM_CELLS_X+2, NUM_CELLS_Y+3);
    neorv32_uart0_printf("This program requires a terminal resolution of at least %ux%u characters.\n", NUM_CELLS_X+2, NUM_CELLS_Y+3);
    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);
    neorv32_uart0_printf("Press any key to start a random-initialized torus-style universe of %ux%u cells.\n", NUM_CELLS_X, NUM_CELLS_Y);
    neorv32_uart_printf("You can pause/restart the simulation by pressing any key.\n");
    neorv32_uart0_printf("You can pause/restart the simulation by pressing any key.\n");
 
 
 
 
    // check if TRNG was synthesized
    // check if TRNG was synthesized
    if (neorv32_trng_available()) {
    if (neorv32_trng_available()) {
      neorv32_uart_printf("\nTRNG detected. Using TRNG for universe initialization.\n");
      neorv32_uart0_printf("\nTRNG detected. Using TRNG for universe initialization.\n");
      neorv32_trng_enable();
      neorv32_trng_enable();
      trng_available = 1;
      trng_available = 1;
    }
    }
 
 
 
 
    // randomize until key pressed
    // randomize until key pressed
    while (neorv32_uart_char_received() == 0) {
    while (neorv32_uart0_char_received() == 0) {
      xorshift32();
      xorshift32();
    }
    }
 
 
 
 
    // initialize universe using random data
    // initialize universe using random data
Line 143... Line 143...
      for (y=0; y<NUM_CELLS_Y; y++) {
      for (y=0; y<NUM_CELLS_Y; y++) {
        if (trng_available) {
        if (trng_available) {
          while (1) {
          while (1) {
            int err = neorv32_trng_get(&trng_data);
            int err = neorv32_trng_get(&trng_data);
            if (err) {
            if (err) {
              neorv32_uart_printf("TRNG error (%i)! Restarting TRNG...\n", err);
              neorv32_uart0_printf("TRNG error (%i)! Restarting TRNG...\n", err);
              continue;
              continue;
            }
            }
            else {
            else {
              break;
              break;
            }
            }
Line 162... Line 162...
 
 
 
 
    while(1) {
    while(1) {
 
 
      // user abort?
      // user abort?
      if (neorv32_uart_char_received()) {
      if (neorv32_uart0_char_received()) {
        neorv32_uart_printf("\nRestart (y/n)?");
        neorv32_uart0_printf("\nRestart (y/n)?");
        if (neorv32_uart_getc() == 'y') {
        if (neorv32_uart0_getc() == 'y') {
          break;
          break;
        }
        }
      }
      }
 
 
      // print generation, population count and the current universe
      // print generation, population count and the current universe
      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);
      neorv32_uart0_printf("\n\nGeneration %u: %u/%u living cells\n", (uint32_t)generation, (uint32_t)pop_count(u), NUM_CELLS_X*NUM_CELLS_Y);
      print_universe(u);
      print_universe(u);
 
 
      // compute next generation
      // compute next generation
      clear_universe((u + 1) & 1);
      clear_universe((u + 1) & 1);
 
 
Line 214... Line 214...
 **************************************************************************/
 **************************************************************************/
void print_universe(int u){
void print_universe(int u){
 
 
  int16_t x, y;
  int16_t x, y;
 
 
  neorv32_uart_putc('+');
  neorv32_uart0_putc('+');
  for (x=0; x<NUM_CELLS_X; x++) {
  for (x=0; x<NUM_CELLS_X; x++) {
    neorv32_uart_putc('-');
    neorv32_uart0_putc('-');
  }
  }
  neorv32_uart_putc('+');
  neorv32_uart0_putc('+');
  neorv32_uart_putc('\r');
  neorv32_uart0_putc('\r');
  neorv32_uart_putc('\n');
  neorv32_uart0_putc('\n');
 
 
  for (y=0; y<NUM_CELLS_Y; y++) {
  for (y=0; y<NUM_CELLS_Y; y++) {
    neorv32_uart_putc('|');
    neorv32_uart0_putc('|');
 
 
    for (x=0; x<NUM_CELLS_X; x++) {
    for (x=0; x<NUM_CELLS_X; x++) {
      if (get_cell(u, x, y))
      if (get_cell(u, x, y))
        neorv32_uart_putc((char)CELL_ALIVE);
        neorv32_uart0_putc((char)CELL_ALIVE);
      else
      else
        neorv32_uart_putc((char)CELL_DEAD);
        neorv32_uart0_putc((char)CELL_DEAD);
    }
    }
 
 
    // end of line
    // end of line
    neorv32_uart_putc('|');
    neorv32_uart0_putc('|');
    neorv32_uart_putc('\r');
    neorv32_uart0_putc('\r');
    neorv32_uart_putc('\n');
    neorv32_uart0_putc('\n');
  }
  }
 
 
  neorv32_uart_putc('+');
  neorv32_uart0_putc('+');
  for (x=0; x<NUM_CELLS_X; x++) {
  for (x=0; x<NUM_CELLS_X; x++) {
    neorv32_uart_putc('-');
    neorv32_uart0_putc('-');
  }
  }
  neorv32_uart_putc('+');
  neorv32_uart0_putc('+');
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Kill all cells in universe.
 * Kill all cells in universe.

powered by: WebSVN 2.1.0

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