1+ --
2+ -- File Name: Demo_Rand.vhd
3+ -- Design Unit Name: Demo_Rand
4+ -- Revision: STANDARD VERSION, revision 2015.03
5+ --
6+ -- Maintainer: Jim Lewis email: jim@synthworks.com
7+ -- Contributor(s):
8+ -- Jim Lewis email: jim@synthworks.com
9+ --
10+ -- Description:
11+ -- Demonstration program for RandomPkg.vhd
12+ --
13+ -- Developed for:
14+ -- SynthWorks Design Inc.
15+ -- VHDL Training Classes
16+ -- 11898 SW 128th Ave. Tigard, Or 97223
17+ -- http://www.SynthWorks.com
18+ --
19+ -- Revision History:
20+ -- Date Version Description
21+ -- 02/2009: 1.0 Initial revision and First Public Released Version
22+ -- 03/2009 1.1 Minor tweek to printing
23+ -- 03/2015 2015.03 Updated FAVOR_BIG to FavorBig and FAVOR_SMALL to FavorSmall
24+ --
25+ -- Copyright (c) 2009 by SynthWorks Design Inc. All rights reserved.
26+ --
27+ -- Verbatim copies of this source file may be used and
28+ -- distributed without restriction.
29+ --
30+ -- This source file is free software; you can redistribute it
31+ -- and/or modify it under the terms of the ARTISTIC License
32+ -- as published by The Perl Foundation; either version 2.0 of
33+ -- the License, or (at your option) any later version.
34+ --
35+ -- This source is distributed in the hope that it will be
36+ -- useful, but WITHOUT ANY WARRANTY; without even the implied
37+ -- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
38+ -- PURPOSE. See the Artistic License for details.
39+ --
40+ -- You should have received a copy of the license with this source.
41+ -- If not download it from,
42+ -- http://www.perlfoundation.org/artistic_license_2_0
43+ --
44+ library IEEE ;
45+ use ieee.std_logic_1164.all ;
46+ use std.textio.all ;
47+ use ieee.std_logic_textio.all ;
48+
49+ Package TestSupportPkg is
50+ type integer_array is array (integer range <> ) of integer ;
51+
52+ procedure TestInit (TestName : string ; variable Results : inout integer_array ) ;
53+ procedure TestInit (TestName : string ; variable Results : inout integer_array ; variable Count : inout natural ) ;
54+ procedure AccumulateResults (IntVal : integer ; Num : integer ; variable Results : inout integer_array) ;
55+ procedure PrintResults (Results : integer_array) ;
56+
57+ end TestSupportPkg ;
58+ Package body TestSupportPkg is
59+ procedure TestInit (TestName : string ; variable Results : inout integer_array ) is
60+ begin
61+ write (OUTPUT , LF& LF & TestName & LF ) ;
62+ Results := (Results'range => 0 ) ;
63+ write (OUTPUT , " 1st 20 values = " ) ;
64+ end ;
65+
66+ procedure TestInit (TestName : string ; variable Results : inout integer_array ; variable Count : inout natural ) is
67+ begin
68+ Count := Count + 1 ;
69+ write (OUTPUT , LF& LF & " Test " & integer 'image (Count) & " : " & TestName & LF ) ;
70+ Results := (Results'range => 0 ) ;
71+ write (OUTPUT , " 1st 20 values = " ) ;
72+ end ;
73+
74+ procedure AccumulateResults (IntVal : integer ; Num : integer ; variable Results : inout integer_array) is
75+ begin
76+ Results(IntVal) := Results(IntVal) + 1 ;
77+ if Num < 20 then
78+ write (OUTPUT , integer 'image (IntVal) & " " ) ;
79+ end if ;
80+ end ;
81+
82+ procedure PrintResults (Results : integer_array) is
83+ begin
84+ write (OUTPUT , LF & " Accumulated Results. Expecting approximately 1000 of each per weight." & LF) ;
85+ for i in Results'range loop
86+ if Results(i) > 0 then
87+ write (OUTPUT , " ** " ) ;
88+ write (OUTPUT , integer 'image (i) & " : " & integer 'image (Results(i)) & LF) ;
89+ end if ;
90+ end loop ;
91+ end ;
92+ end TestSupportPkg ;
93+
94+ library IEEE ;
95+ use ieee.std_logic_1164.all ;
96+ use ieee.numeric_std.all ;
97+
98+ use std.textio.all ;
99+ use ieee.std_logic_textio.all ;
100+
101+ library SynthWorks ;
102+ use SynthWorks.RandomBasePkg.all ;
103+ use SynthWorks.RandomPkg.all ;
104+
105+ use work.TestSupportPkg.all ;
106+
107+ entity Demo_Rand is
108+ end Demo_Rand ;
109+ architecture test of Demo_Rand is
110+ begin
111+
112+
113+ RandomGenProc : process
114+ variable RV : RandomPType ;
115+
116+ variable DataInt : integer ;
117+ variable DataSlv : std_logic_vector (3 downto 0 ) ;
118+ variable DataUnsigned : unsigned (3 downto 0 ) ;
119+ variable DataSigned : signed (4 downto 0 ) ;
120+
121+ -- Statistics
122+ variable TestNum : integer := 0 ;
123+ variable Results : integer_array (- 100 to 100 ) := (others => 0 ) ;
124+ variable writebuf : line ;
125+
126+ begin
127+
128+ RV.InitSeed(RV'instance_name ) ; -- Initialize Seed. Typically done one time
129+
130+
131+ write (OUTPUT , LF& LF& " Random Range Tests" ) ;
132+ TestInit(" RandInt(0, 7) Range 0-7" , Results, TestNum) ; -- 1
133+ for i in 1 to 8000 loop -- Loop 1000x per value
134+ DataInt := RV.RandInt(0 , 7 );
135+ AccumulateResults(DataInt, i, Results) ;
136+ end loop ;
137+ PrintResults (Results) ;
138+
139+ TestInit(" RandInt(1, 13, (3, 7, 11) Range 1-13, Exclude 3,7,11" , Results, TestNum) ; -- 2
140+ for i in 1 to 10000 loop -- Loop 1000x per value
141+ DataInt := RV.RandInt(1 , 13 , (3 , 7 , 11 ));
142+ AccumulateResults(DataInt, i, Results) ;
143+ end loop ;
144+ PrintResults (Results) ;
145+
146+ TestInit(" RandSlv(0, 4, 4) Range 0-4" , Results, TestNum) ; -- 3
147+ for i in 1 to 5000 loop -- Loop 1000x per value
148+ DataSlv := RV.RandSlv(0 , 4 , 4 );
149+ AccumulateResults(to_integer (unsigned (DataSlv)), i, Results) ;
150+ end loop ;
151+ PrintResults (Results) ;
152+
153+ TestInit(" RandUnsigned(4, 9, (0 => 7), 4) Range 4-9, Exclude 7" , Results, TestNum) ; -- 4
154+ for i in 1 to 5000 loop -- Loop 1000x per value
155+ DataUnsigned := RV.RandUnsigned(4 , 9 , (0 => 7 ), 4 ); -- only 1 exclude element
156+ AccumulateResults(to_integer (DataUnsigned), i, Results) ;
157+ end loop ;
158+ PrintResults (Results) ;
159+
160+ TestInit(" RandSigned(-4, 3, 5)" , Results, TestNum) ; -- 5
161+ for i in 1 to 8000 loop -- Loop 1000x per value
162+ DataSigned := RV.RandSigned(- 4 , 3 , 5 );
163+ AccumulateResults(to_integer (DataSigned), i, Results) ;
164+ end loop ;
165+ PrintResults (Results) ;
166+
167+
168+ write (OUTPUT , LF& LF& " Random Set Tests" ) ;
169+ TestNum := 0 ;
170+ TestInit(" RandInt( (-50, -22, -14, -7, -2, 0, 3, 7, 9, 27, 49, 89, 99)). Set: (-50, -22, -14, -7, -2, 0, 3, 7, 9, 27, 49, 89, 99)" , Results, TestNum) ; -- 1
171+ for i in 1 to 13000 loop -- Loop 1000x per value
172+ DataInt := RV.RandInt( (- 50 , - 22 , - 14 , - 7 , - 2 , 0 , 3 , 7 , 9 , 27 , 49 , 89 , 99 ));
173+ AccumulateResults(DataInt, i, Results) ;
174+ end loop ;
175+ PrintResults (Results) ;
176+
177+ TestInit(" RandInt( (-5, -1, 3, 7, 11), (-1, 7) ) Set (-5, -1, 3, 7, 11), Exclude (-1, 7)" , Results, TestNum) ; -- 2
178+ for i in 1 to 3000 loop -- Loop 1000x per value
179+ DataInt := RV.RandInt( (- 5 , - 1 , 3 , 7 , 11 ), (- 1 , 7 ) );
180+ AccumulateResults(DataInt, i, Results) ;
181+ end loop ;
182+ PrintResults (Results) ;
183+
184+ TestInit(" RandSlv( (1, 2, 3, 7, 11), 4)" , Results, TestNum) ; -- 3
185+ for i in 1 to 5000 loop -- Loop 1000x per value
186+ DataSlv := RV.RandSlv( (1 , 2 , 3 , 7 , 11 ), 4 );
187+ AccumulateResults(to_integer (unsigned (DataSlv)), i, Results) ;
188+ end loop ;
189+ PrintResults (Results) ;
190+
191+ TestInit(" RandUnsigned( (1, 2, 3, 11), (1 => 3), 4)" , Results, TestNum) ; -- 4
192+ for i in 1 to 3000 loop -- Loop 1000x per value
193+ DataUnsigned := RV.RandUnsigned( (1 , 2 , 3 , 11 ), (1 => 3 ), 4 ); -- 1 element middle
194+ AccumulateResults(to_integer (DataUnsigned), i, Results) ;
195+ end loop ;
196+ PrintResults (Results) ;
197+
198+ TestInit(" RandSigned( (-5, -1, 3, 7, 11), 5)" , Results, TestNum) ; -- 5
199+ for i in 1 to 5000 loop -- Loop 1000x per value
200+ DataSigned := RV.RandSigned( (- 5 , - 1 , 3 , 7 , 11 ), 5 );
201+ AccumulateResults(to_integer (DataSigned), i, Results) ;
202+ end loop ;
203+ PrintResults (Results) ;
204+
205+
206+ write (OUTPUT , LF& LF& " Weighted Distribution Tests" ) ;
207+ TestNum := 0 ;
208+ -- There is also DistSlv, DistUnsigned, DistSigned
209+ TestInit(" RV.DistInt( (7, 2, 1) ) " , Results, TestNum) ;
210+ for i in 1 to 10000 loop -- Loop 1000x per distribute weight
211+ DataInt := RV.DistInt( (7 , 2 , 1 ) ) ;
212+ AccumulateResults(DataInt, i, Results) ;
213+ end loop ;
214+ PrintResults (Results) ;
215+
216+ TestInit(" RV.DistInt( (0, 2, 0, 4, 0, 6, 0, 8, 0, 10), (3,9) );" , Results, TestNum) ;
217+ for i in 1 to 16000 loop -- Loop 1000x per distribute weight
218+ DataInt := RV.DistInt( (0 , 2 , 0 , 4 , 0 , 6 , 0 , 8 , 0 , 10 ), (3 ,9 ) ) ;
219+ AccumulateResults(DataInt, i, Results) ;
220+ end loop ;
221+ PrintResults (Results) ;
222+
223+
224+ write (OUTPUT , LF& LF& " Weighted Distribution with Value" ) ;
225+ TestNum := 0 ;
226+ -- There is also DistValSlv, DistValUnsigned, DistValSigned
227+ TestInit(" RV.DistValInt( ((1, 7), (3, 2), (5, 1)) ) " , Results, TestNum) ;
228+ for i in 1 to 10000 loop -- Loop 1000x per distribute weight
229+ DataInt := RV.DistValInt( ((1 , 7 ), (3 , 2 ), (5 , 1 )) ) ;
230+ AccumulateResults(DataInt, i, Results) ;
231+ end loop ;
232+ PrintResults (Results) ;
233+
234+ TestInit(" RV.DistValInt( ((1, 7), (3, 2), (5, 1)), (1=>3) ) Exclude 3" , Results, TestNum) ;
235+ for i in 1 to 8000 loop -- Loop 1000x per distribute weight
236+ DataInt := RV.DistValInt( ((1 , 7 ), (3 , 2 ), (5 , 1 )), (1 => 3 ) ) ;
237+ AccumulateResults(DataInt, i, Results) ;
238+ end loop ;
239+ PrintResults (Results) ;
240+
241+ write (OUTPUT , LF& LF& " Mode Direct Tests" ) ;
242+ -- There are also real return values
243+ TestNum := 0 ;
244+ TestInit(" Integer Uniform: Integer Range (0 to 9)" , Results, TestNum) ;
245+ for i in 1 to 10000 loop -- Loop 1000x per value
246+ DataInt := RV.uniform(0 ,9 );
247+ AccumulateResults(DataInt, i, Results) ;
248+ end loop ;
249+ PrintResults (Results) ;
250+
251+ TestInit(" Integer FavorSmall: Integer Range (0 to 9)" , Results, TestNum) ;
252+ for i in 1 to 10000 loop -- Loop 1000x per value
253+ DataInt := RV.FavorSmall(0 ,9 );
254+ AccumulateResults(DataInt, i, Results) ;
255+ end loop ;
256+ PrintResults (Results) ;
257+
258+ TestInit(" Integer FavorBig: Integer Range (0 to 9)" , Results, TestNum) ;
259+ for i in 1 to 10000 loop -- Loop 1000x per value
260+ DataInt := RV.FavorBig(0 ,9 );
261+ AccumulateResults(DataInt, i, Results) ;
262+ end loop ;
263+ PrintResults (Results) ;
264+
265+ TestInit(" Integer NORMAL, 50.0, 5.0 range -100 to 100" , Results, TestNum) ;
266+ for i in 1 to 100000 loop -- Loop 1000x per value
267+ DataInt := RV.Normal(50.0 , 5.0 , - 100 , 100 );
268+ AccumulateResults(DataInt, i, Results) ;
269+ end loop ;
270+ PrintResults (Results) ;
271+
272+ TestInit(" Integer Poisson, 10.0, -100, 100" , Results, TestNum) ;
273+ for i in 1 to 10000 loop -- Loop 1000x per value
274+ DataInt := RV.Poisson(10.0 , - 100 , 100 ) ;
275+ AccumulateResults(DataInt, i, Results) ;
276+ end loop ;
277+ PrintResults (Results) ;
278+
279+ wait ;
280+ end process RandomGenProc ;
281+
282+ end test ;
0 commit comments