1 |
5 |
gedra |
----------------------------------------------------------------------
|
2 |
|
|
---- ----
|
3 |
|
|
---- Testbench for Rand number generator library. ----
|
4 |
|
|
---- ----
|
5 |
|
|
---- This file is part of the Random Number Generator project ----
|
6 |
|
|
---- http://www.opencores.org/cores/rng_lib/ ----
|
7 |
|
|
---- ----
|
8 |
|
|
---- Description ----
|
9 |
|
|
---- The test bench will generate 10000 random numbers from each ----
|
10 |
|
|
---- distribution and do a simple plot of the distributions. ----
|
11 |
|
|
---- ----
|
12 |
|
|
---- To Do: ----
|
13 |
|
|
---- - ----
|
14 |
|
|
---- ----
|
15 |
|
|
---- Author(s): ----
|
16 |
|
|
---- - Geir Drange, gedra@opencores.org ----
|
17 |
|
|
---- ----
|
18 |
|
|
----------------------------------------------------------------------
|
19 |
|
|
---- ----
|
20 |
|
|
---- Copyright (C) 2004 Authors and OPENCORES.ORG ----
|
21 |
|
|
---- ----
|
22 |
|
|
---- This source file may be used and distributed without ----
|
23 |
|
|
---- restriction provided that this copyright statement is not ----
|
24 |
|
|
---- removed from the file and that any derivative work contains ----
|
25 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
26 |
|
|
---- ----
|
27 |
|
|
---- This source file is free software; you can redistribute it ----
|
28 |
|
|
---- and/or modify it under the terms of the GNU General ----
|
29 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
30 |
|
|
---- either version 2.0 of the License, or (at your option) any ----
|
31 |
|
|
---- later version. ----
|
32 |
|
|
---- ----
|
33 |
|
|
---- This source is distributed in the hope that it will be ----
|
34 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
35 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
36 |
|
|
---- PURPOSE. See the GNU General Public License for more details.----
|
37 |
|
|
---- ----
|
38 |
|
|
---- You should have received a copy of the GNU General ----
|
39 |
|
|
---- Public License along with this source; if not, download it ----
|
40 |
|
|
---- from http://www.gnu.org/licenses/gpl.txt ----
|
41 |
|
|
---- ----
|
42 |
|
|
----------------------------------------------------------------------
|
43 |
|
|
--
|
44 |
|
|
-- CVS Revision History
|
45 |
|
|
--
|
46 |
|
|
-- $Log: not supported by cvs2svn $
|
47 |
7 |
gedra |
-- Revision 1.1 2004/09/28 15:12:52 gedra
|
48 |
|
|
-- Test bench for random numbers.
|
49 |
5 |
gedra |
--
|
50 |
|
|
--
|
51 |
7 |
gedra |
--
|
52 |
|
|
|
53 |
5 |
gedra |
library ieee;
|
54 |
|
|
use ieee.std_logic_1164.all;
|
55 |
|
|
use ieee.numeric_std.all;
|
56 |
|
|
use std.textio.all;
|
57 |
|
|
use work.rng_lib.all;
|
58 |
|
|
|
59 |
7 |
gedra |
entity tb_rng is
|
60 |
5 |
gedra |
|
61 |
|
|
end tb_rng;
|
62 |
|
|
|
63 |
|
|
architecture behav of tb_rng is
|
64 |
|
|
|
65 |
7 |
gedra |
constant BIN_COUNT : integer := 15;
|
66 |
|
|
constant PLOT_HEIGHT : real := 40.0;
|
67 |
|
|
constant RAND_COUNT : integer := 10000;
|
68 |
5 |
gedra |
|
69 |
7 |
gedra |
type rand_array is array (0 to RAND_COUNT - 1) of real; -- array used for plot
|
70 |
|
|
type bin_array is array (0 to BIN_COUNT - 1) of integer;
|
71 |
|
|
|
72 |
5 |
gedra |
-- Plot a distribution of the numbers that are between lo,hi values
|
73 |
7 |
gedra |
impure function plot_dist (numbs : rand_array; lo, hi : real) return integer is
|
74 |
|
|
variable bins : bin_array;
|
75 |
|
|
variable bin_size, height : real;
|
76 |
|
|
variable idx, max : integer;
|
77 |
|
|
variable bar : line;
|
78 |
|
|
begin
|
79 |
|
|
-- reset bins
|
80 |
|
|
for i in 0 to BIN_COUNT - 1 loop
|
81 |
|
|
bins(i) := 0;
|
82 |
5 |
gedra |
end loop;
|
83 |
7 |
gedra |
-- sort numbers into bins
|
84 |
|
|
bin_size := (hi - lo) / real(BIN_COUNT);
|
85 |
|
|
for i in 0 to RAND_COUNT - 1 loop
|
86 |
|
|
if numbs(i) > lo and numbs(i) < hi then
|
87 |
|
|
idx := integer(((numbs(i) - lo) / bin_size) - 0.5);
|
88 |
|
|
if idx > BIN_COUNT - 1 then
|
89 |
|
|
idx := BIN_COUNT - 1;
|
90 |
|
|
elsif idx < 0 then
|
91 |
|
|
idx := 0;
|
92 |
|
|
end if;
|
93 |
|
|
bins(idx) := bins(idx) + 1;
|
94 |
|
|
end if;
|
95 |
|
|
end loop;
|
96 |
|
|
-- find largest bin
|
97 |
|
|
max := 0;
|
98 |
|
|
for i in 0 to BIN_COUNT - 1 loop
|
99 |
|
|
if bins(i) > max then
|
100 |
|
|
max := bins(i);
|
101 |
|
|
end if;
|
102 |
|
|
end loop;
|
103 |
|
|
-- plot bins
|
104 |
|
|
for i in 0 to BIN_COUNT - 1 loop
|
105 |
|
|
height := PLOT_HEIGHT * real(bins(i)) / real(max);
|
106 |
|
|
for j in 1 to integer(height) loop
|
107 |
|
|
write(bar, string'("*"));
|
108 |
|
|
end loop;
|
109 |
|
|
writeline(OUTPUT, bar);
|
110 |
|
|
end loop;
|
111 |
|
|
return 0;
|
112 |
|
|
end plot_dist;
|
113 |
|
|
|
114 |
5 |
gedra |
begin
|
115 |
|
|
|
116 |
7 |
gedra |
p1 : process
|
117 |
|
|
variable r_uni, r_gauss, r_exp : rand_var;
|
118 |
|
|
variable r_poisson : rand_var;
|
119 |
|
|
variable txt : line;
|
120 |
|
|
variable a : integer;
|
121 |
|
|
variable numbs : rand_array;
|
122 |
|
|
begin
|
123 |
|
|
-- Test the uniform distribution
|
124 |
|
|
r_uni := init_uniform(0, 0, 0, 0.0, 10.0); -- range 0 to 10
|
125 |
|
|
t1 : for i in 0 to RAND_COUNT - 1 loop
|
126 |
|
|
r_uni := rand(r_uni);
|
127 |
|
|
numbs(i) := r_uni.rnd;
|
128 |
|
|
end loop t1;
|
129 |
|
|
write(txt, string'("Uniform distribution:"));
|
130 |
|
|
writeline(OUTPUT, txt);
|
131 |
|
|
a := plot_dist (numbs, 0.0, 10.0);
|
132 |
|
|
-- Test the gaussian distribution
|
133 |
|
|
r_gauss := init_gaussian(0, 0, 0, 0.0, 10.0); -- mean=0, stdev=10
|
134 |
|
|
t2 : for i in 0 to RAND_COUNT - 1 loop
|
135 |
|
|
r_gauss := rand(r_gauss);
|
136 |
|
|
numbs(i) := r_gauss.rnd;
|
137 |
|
|
end loop t2;
|
138 |
|
|
write(txt, string'("Gaussian distribution:"));
|
139 |
|
|
writeline(OUTPUT, txt);
|
140 |
|
|
a := plot_dist (numbs, -20.0, 20.0);
|
141 |
|
|
-- Test the exponential distribution
|
142 |
|
|
r_exp := init_exponential(0, 0, 0, 10.0); -- mean=10
|
143 |
|
|
t3 : for i in 0 to RAND_COUNT - 1 loop
|
144 |
|
|
r_exp := rand(r_exp);
|
145 |
|
|
numbs(i) := r_exp.rnd;
|
146 |
|
|
end loop t3;
|
147 |
|
|
write(txt, string'("Exponential distribution:"));
|
148 |
|
|
writeline(OUTPUT, txt);
|
149 |
|
|
a := plot_dist (numbs, 0.0, 20.0);
|
150 |
|
|
|
151 |
|
|
wait for 1 ns;
|
152 |
|
|
|
153 |
|
|
report "End of simulation! (ignore this failure)"
|
154 |
|
|
severity failure;
|
155 |
|
|
wait;
|
156 |
|
|
end process p1;
|
157 |
|
|
|
158 |
5 |
gedra |
end behav;
|