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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_trng/] [main.c] - Diff between revs 14 and 23

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

Rev 14 Rev 23
Line 62... Line 62...
int main(void) {
int main(void) {
 
 
  uint8_t lucky_numbers_5of50[5];
  uint8_t lucky_numbers_5of50[5];
  uint8_t lucky_numbers_2of10[2];
  uint8_t lucky_numbers_2of10[2];
 
 
  uint8_t i, j, tmp;
  int err;
  uint16_t trng_data;
  uint8_t i, j, probe;
 
  uint8_t trng_data;
  unsigned int num_samples;
  unsigned int num_samples;
 
 
  // check if UART unit is implemented at all
  // check if UART unit is implemented at all
  if (neorv32_uart_available() == 0) {
  if (neorv32_uart_available() == 0) {
    return 0;
    return 0;
  }
  }
 
 
 
 
  // 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();
 
 
 
 
Line 89... Line 89...
  if (neorv32_trng_available() == 0) {
  if (neorv32_trng_available() == 0) {
    neorv32_uart_printf("No TRNG implemented.");
    neorv32_uart_printf("No TRNG implemented.");
    return 0;
    return 0;
  }
  }
 
 
  // configure TRNG with defaul GARO tap mask
  // enable TRNG
  neorv32_uart_printf("Configuring TRNG...\n");
  neorv32_trng_enable();
  uint16_t trng_tap_config = neorv32_trng_find_tap_mask();
 
  neorv32_uart_printf("TRNG: Using tap mask: 0x%x ", (uint32_t)trng_tap_config);
 
  neorv32_trng_setup(trng_tap_config);
 
 
 
  while(1) {
  while(1) {
 
 
    // main menu
    // main menu
    neorv32_uart_printf("\nCommands:\n"
    neorv32_uart_printf("\nCommands:\n"
Line 111... Line 108...
 
 
    // output RND data
    // output RND data
    if (cmd == 'n') {
    if (cmd == 'n') {
      num_samples = 0;
      num_samples = 0;
      while(1) {
      while(1) {
        if (neorv32_trng_get(&trng_data)) {
        err = neorv32_trng_get(&trng_data);
          neorv32_uart_printf("\nTRNG error!\n");
        if (err) {
 
          neorv32_uart_printf("\nTRNG error (%i)!\n", err);
          break;
          break;
        }
        }
        neorv32_uart_printf("%u ", (uint32_t)(trng_data & 0xFF));
        neorv32_uart_printf("%u ", (uint32_t)(trng_data));
        num_samples++;
        num_samples++;
        if (neorv32_uart_char_received()) { // abort when key pressed
        if (neorv32_uart_char_received()) { // abort when key pressed
          neorv32_uart_printf("\nPrinted samples: %u", num_samples);
          neorv32_uart_printf("\nPrinted samples: %u", num_samples);
          break;
          break;
        }
        }
      }
      }
    }
    }
 
 
    // print lucky numbers
    // print lucky numbers
    if (cmd == 'l') {
    if (cmd == 'l') {
      // reset array
      // reset arrays
      for (i=0; i<5; i++) {
      for (i=0; i<5; i++) {
        lucky_numbers_5of50[i] = 0;
        lucky_numbers_5of50[i] = 0;
      }
      }
      lucky_numbers_2of10[0] = 0;
      lucky_numbers_2of10[0] = 0;
      lucky_numbers_2of10[1] = 0;
      lucky_numbers_2of10[1] = 0;
 
 
      // get numbers
      // get numbers
      i = 0;
      i = 0;
      while (i<5) {
      while (i<5) {
        if (neorv32_trng_get(&trng_data)) {
        err = neorv32_trng_get(&trng_data);
          neorv32_uart_printf("\nTRNG error!\n");
        if (err) {
 
          neorv32_uart_printf("\nTRNG error (%i)!\n", err);
          break;
          break;
        }
        }
        // valid range?
        // valid range?
        tmp = (uint8_t)(trng_data & 0xff);
        if ((trng_data == 0) || (trng_data > 50)) {
        if ((tmp == 0) || (tmp > 50)) {
 
          continue;
          continue;
        }
        }
        // already sampled?
        // already sampled?
 
        probe = 0;
        for (j=0; j<5; j++) {
        for (j=0; j<5; j++) {
          if (lucky_numbers_5of50[j] == tmp) {
          if (lucky_numbers_5of50[j] == trng_data) {
            continue;
            probe++;
          }
          }
        }
        }
        lucky_numbers_5of50[i] = tmp;
        if (probe) {
 
          continue;
 
        }
 
        else {
 
          lucky_numbers_5of50[i] = trng_data;
        i++;
        i++;
      }
      }
 
      }
 
 
      // get numbers part 2
      // get numbers part 2
      i = 0;
      i = 0;
      while (i<2) {
      while (i<2) {
        if (neorv32_trng_get(&trng_data)) {
        err = neorv32_trng_get(&trng_data);
          neorv32_uart_printf("\nTRNG error!\n");
        if (err) {
 
          neorv32_uart_printf("\nTRNG error (%i)!\n", err);
          break;
          break;
        }
        }
        // valid range?
        // valid range?
        tmp = (uint8_t)(trng_data & 0xff);
        if ((trng_data == 0) || (trng_data > 10)) {
        if ((tmp == 0) || (tmp > 10)) {
 
          continue;
          continue;
        }
        }
        // already sampled?
        // already sampled?
 
        probe = 0;
        for (j=0; j<2; j++) {
        for (j=0; j<2; j++) {
          if (lucky_numbers_2of10[j] == tmp) {
          if (lucky_numbers_2of10[j] == trng_data) {
            continue;
            probe++;
 
          }
          }
          }
 
        if (probe) {
 
          continue;
        }
        }
        lucky_numbers_2of10[i] = tmp;
        else {
 
          lucky_numbers_2of10[i] = trng_data;
        i++;
        i++;
      }
      }
 
      }
 
 
      // output
      // output
      neorv32_uart_printf("\n");
      neorv32_uart_printf("\n");
      for (j=0; j<5; j++) {
      for (j=0; j<5; j++) {
        if (i==4) {
        if (i==4) {

powered by: WebSVN 2.1.0

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