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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_trng/] [main.c] - Blame information for rev 58

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

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2
// # << NEORV32 - TRNG Demo Program >>                                                             #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6 44 zero_gravi
// # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
7 2 zero_gravi
// #                                                                                               #
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
 * @file demo_trng/main.c
38
 * @author Stephan Nolting
39 47 zero_gravi
 * @brief True random number generator demo program.
40 2 zero_gravi
 **************************************************************************/
41
 
42
#include <neorv32.h>
43
 
44
 
45
/**********************************************************************//**
46
 * @name User configuration
47
 **************************************************************************/
48
/**@{*/
49
/** UART BAUD rate */
50
#define BAUD_RATE 19200
51
/**@}*/
52
 
53
 
54 47 zero_gravi
// prototypes
55
void print_random_data(void);
56
void generate_histogram(void);
57 2 zero_gravi
 
58 47 zero_gravi
 
59 2 zero_gravi
/**********************************************************************//**
60 47 zero_gravi
 * Simple true random number test/demo program.
61 2 zero_gravi
 *
62
 * @note This program requires the UART and the TRNG to be synthesized.
63
 *
64
 * @return Irrelevant.
65
 **************************************************************************/
66
int main(void) {
67
 
68
  // check if UART unit is implemented at all
69
  if (neorv32_uart_available() == 0) {
70
    return 0;
71
  }
72
 
73
  // capture all exceptions and give debug info via UART
74
  // this is not required, but keeps us safe
75 14 zero_gravi
  neorv32_rte_setup();
76 2 zero_gravi
 
77
 
78 51 zero_gravi
  // init UART at default baud rate, no parity bits, ho hw flow control
79
  neorv32_uart_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
80 2 zero_gravi
 
81 44 zero_gravi
  // check available hardware extensions and compare with compiler flags
82
  neorv32_rte_check_isa(0); // silent = 0 -> show message if isa mismatch
83
 
84 2 zero_gravi
  // intro
85
  neorv32_uart_printf("\n--- TRNG Demo ---\n\n");
86
 
87
  // check if TRNG unit is implemented at all
88
  if (neorv32_trng_available() == 0) {
89
    neorv32_uart_printf("No TRNG implemented.");
90
    return 0;
91
  }
92
 
93 23 zero_gravi
  // enable TRNG
94
  neorv32_trng_enable();
95 2 zero_gravi
 
96
  while(1) {
97
 
98
    // main menu
99
    neorv32_uart_printf("\nCommands:\n"
100
                        " n: Print 8-bit random numbers (abort by pressing any key)\n"
101 47 zero_gravi
                        " h: Generate and print histogram\n");
102 2 zero_gravi
 
103
    neorv32_uart_printf("CMD:> ");
104
    char cmd = neorv32_uart_getc();
105
    neorv32_uart_putc(cmd); // echo
106
    neorv32_uart_printf("\n");
107
 
108
    if (cmd == 'n') {
109 47 zero_gravi
      print_random_data();
110 2 zero_gravi
    }
111 47 zero_gravi
    else if (cmd == 'h') {
112
      generate_histogram();
113
    }
114
    else {
115
      neorv32_uart_printf("Invalid command.\n");
116
    }
117
  }
118 2 zero_gravi
 
119 47 zero_gravi
  return 0;
120
}
121 2 zero_gravi
 
122
 
123 47 zero_gravi
/**********************************************************************//**
124
 * Print random numbers until a key is pressed.
125
 **************************************************************************/
126
void print_random_data(void) {
127 2 zero_gravi
 
128 47 zero_gravi
  uint32_t num_samples = 0;
129
  int err = 0;
130
  uint8_t trng_data;
131
 
132
  while(1) {
133
    err = neorv32_trng_get(&trng_data);
134
    if (err) {
135
      neorv32_uart_printf("\nTRNG error!\n");
136
      break;
137 2 zero_gravi
    }
138 47 zero_gravi
    neorv32_uart_printf("%u ", (uint32_t)(trng_data));
139
    num_samples++;
140
    if (neorv32_uart_char_received()) { // abort when key pressed
141
      break;
142
    }
143 2 zero_gravi
  }
144 47 zero_gravi
  neorv32_uart_printf("\nPrinted samples: %u\n", num_samples);
145
}
146 2 zero_gravi
 
147 47 zero_gravi
 
148
/**********************************************************************//**
149
 * Generate and print histogram. Samples random data until a key is pressed.
150
 **************************************************************************/
151
void generate_histogram(void) {
152
 
153
  uint32_t hist[256];
154
  uint32_t i;
155
  uint32_t cnt = 0;
156
  int err = 0;
157
  uint8_t trng_data;
158
 
159
  neorv32_uart_printf("Press any key to start.\n");
160
 
161
  while(neorv32_uart_char_received() == 0);
162
  neorv32_uart_printf("Sampling... Press any key to stop.\n");
163
 
164
  // clear histogram
165
  for (i=0; i<256; i++) {
166
    hist[i] = 0;
167
  }
168
 
169
  // sample random data
170
  while(1) {
171
 
172
    err = neorv32_trng_get(&trng_data);
173
    hist[trng_data & 0xff]++;
174
    cnt++;
175
 
176
    if (err) {
177
      neorv32_uart_printf("\nTRNG error!\n");
178
      break;
179
    }
180
 
181
    if (neorv32_uart_char_received()) { // abort when key pressed
182
      break;
183
    }
184
 
185
    if (cnt & 0x80000000UL) { // to prevent overflow
186
      break;
187
    }
188
  }
189
 
190
  // print histogram
191
  neorv32_uart_printf("Histogram [random data value] : [# occurences]\n");
192
  for (i=0; i<256; i++) {
193
    neorv32_uart_printf("%u: %u\n", (uint32_t)i, hist[i]);
194
  }
195
 
196
  neorv32_uart_printf("\nSamples: %u\n", cnt);
197
 
198
  // average
199
  uint64_t average = 0;
200
  for (i=0; i<256; i++) {
201
    average += (uint64_t)hist[i] * i;
202
  }
203
  average = average / ((uint64_t)cnt);
204
  neorv32_uart_printf("Average value: %u\n", (uint32_t)average);
205 2 zero_gravi
}

powered by: WebSVN 2.1.0

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