forked from OSVVM/OSVVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomPkg.vhd
1993 lines (1806 loc) · 87.1 KB
/
RandomPkg.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- File Name : RandomPkg.vhd
-- Design Unit Name : RandomPkg
-- Revision : STANDARD VERSION
--
-- Maintainer : Jim Lewis email : jim@synthworks.com
-- Contributor(s) :
-- Jim Lewis email: jim@synthworks.com
-- Lars Asplund email: lars.anders.asplund@gmail.com - RandBool, RandSl, RandBit, DistBool, DistSl, DistBit
-- *
--
-- * In writing procedures normal, poisson, the following sources were referenced :
-- Wikipedia
-- package rnd2 written by John Breen and Ken Christensen
-- package RNG written by Gnanasekaran Swaminathan
--
--
-- Description :
-- RandomPType, a protected type, defined to hold randomization RandomSeeds and
-- function methods to facilitate randomization with uniform and weighted
-- distributions
--
-- Developed for :
-- SynthWorks Design Inc.
-- VHDL Training Classes
-- 11898 SW 128th Ave. Tigard, Or 97223
-- http ://www.SynthWorks.com
--
-- Revision History :
-- Date Version Description
-- 05/2024 2024.05 Update to address if max < min sometimes multiple errors were generated
-- 03/2024 2024.03 Added RandInt that randomly selects a value in the entire integer range
-- 09/2023 2023.09 Added control of UseNewSeedMethods from OsvvmSettingsPkg.RANDOM_USE_NEW_SEED_METHODS
-- 06/2023 2023.06 Updated InitSeed for type time to do (T mod 2**30*std.env.resolution_limit)
-- 06/2021 2021.06 Updated InitSeed, moved shared stuff to RandomBasePkg
-- 08/2020 2020.08 RandBool, RandSl, RandBit, DistBool, DistSl, DistBit (from Lars)
-- 01/2020 2020.01 Updated Licenses to Apache
-- 11/2016 2016.11 No changes. Updated release numbers to make documentation and
-- package have consistent release identifiers.
-- 5/2015 2015.06 Revised Alerts to Alert(OSVVM_ALERTLOG_ID, ...) ;
-- 1/2015 2015.01 Changed Assert/Report to Alert
-- 1/2014 2014.01 Added RandTime, RandReal(set), RandIntV, RandRealV, RandTimeV
-- Made sort, revsort from SortListPkg_int visible via aliases
-- 5/2013 2013.05 Big vector randomization added overloading RandUnsigned, RandSlv, and RandSigned
-- Added NULL_RANGE_TYPE to minimize null range warnings
-- 5/2013 - Removed extra variable declaration in functions RandInt and RandReal
-- 04/2013 2013.04 Changed DistInt. Return array indices now match input
-- Better Min, Max error handling in Uniform, FavorBig, FavorSmall, Normal, Poisson
-- 06/2012 2.2 Removed '_' in the name of subprograms FavorBig and FavorSmall
-- 07/2011 2.1 Bug fix to convenience functions for slv, unsigned, and signed.
-- 03/2011 2.0 Major clean-up. Moved RandomParamType and control to here
-- 06/2010 1.2 Added Normal and Poisson distributions
-- 02/2009 : 1.0 First Public Released Version
-- 02/25/2009 1.1 Replaced reference to std_2008 with a reference to
-- ieee_proposed.standard_additions.all ;
-- Numerous revisions for SynthWorks' Advanced VHDL Testbenches and Verification
-- 12/2006 : 0.1 Initial revision
--
-- This file is part of OSVVM.
--
-- Copyright (c) 2006 - 2021 by SynthWorks Design Inc.
-- Copyright (C) 2021 by OSVVM Authors
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
use work.OsvvmGlobalPkg.all ;
use work.AlertLogPkg.all ;
use work.RandomBasePkg.all ;
use work.SortListPkg_int.all ;
use work.OsvvmSettingsPkg.all ;
use std.textio.all ;
library ieee ;
use ieee.std_logic_1164.all ;
use ieee.numeric_std.all ;
use ieee.numeric_std_unsigned.all ;
use ieee.math_real.all ;
-- comment out following 3 lines with VHDL-2008. Leave in for VHDL-2002
-- library ieee_proposed ; -- remove with VHDL-2008
-- use ieee_proposed.standard_additions.all ; -- remove with VHDL-2008
-- use ieee_proposed.standard_textio_additions.all ; -- remove with VHDL-2008
package RandomPkg is
-- make things from SortListPkg_int visible
-- alias sort is work.SortListPkg_int.sort [integer_vector return integer_vector] ;
-- alias revsort is work.SortListPkg_int.revsort[integer_vector return integer_vector] ;
-- Supports DistValInt functionality
type DistRecType is record
Value : integer ;
Weight : integer ;
end record ;
type DistType is array (natural range <>) of DistRecType ;
-- Weight vectors not indexed by integers
type NaturalVBoolType is array (boolean range <>) of natural;
type NaturalVSlType is array (std_logic range <>) of natural;
type NaturalVBitType is array (bit range <>) of natural;
--- ///////////////////////////////////////////////////////////////////////////
--- ///////////////////////////////////////////////////////////////////////////
---
--- RandomPType
---
--- ///////////////////////////////////////////////////////////////////////////
--- ///////////////////////////////////////////////////////////////////////////
type RandomPType is protected
--- ///////////////////////////////////////////////////////////////////////////
---
--- Parameter Settings
---
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
-- Seed Manipulation
------------------------------------------------------------
-- Known ambiguity between InitSeed with string and integer_vector
-- Recommendation, use : RV.InitSeed(RV'instance_path) ;
-- For integer_vector use either : RV.InitSeed(IV => (1,5)) ;
-- or : RV.InitSeed(integer_vector'(1,5)) ;
-- Initialize Seeds
procedure InitSeed ( S : string ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) ;
procedure InitSeed ( I : integer ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) ;
procedure InitSeed ( T : time ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) ;
procedure InitSeed ( IV : integer_vector ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) ;
-- Save and restore seed values
procedure SetSeed (RandomSeedIn : RandomSeedType ) ;
impure function GetSeed return RandomSeedType ;
procedure SeedRandom (RandomSeedIn : RandomSeedType ) ;
impure function SeedRandom return RandomSeedType ;
-- alias SeedRandom is SetSeed [RandomSeedType] ;
-- alias SeedRandom is GetSeed [return RandomSeedType] ;
------------------------------------------------------------
-- Setting Randomization Parameters
------------------------------------------------------------
procedure SetRandomParm (RandomParmIn : RandomParamType) ;
procedure SetRandomParm (
Distribution : RandomDistType ;
Mean : Real := 0.0 ;
Deviation : Real := 0.0
) ;
impure function GetRandomParm return RandomParamType ;
impure function GetRandomParm return RandomDistType ;
-- For compatibility with previous version - replace with alias
procedure SetRandomMode (RandomDistIn : RandomDistType) ;
-- alias SetRandomMode is SetRandomParm [RandomDistType, Real, Real] ;
--- ///////////////////////////////////////////////////////////////////////////
---
--- Base Randomization Distributions
---
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
--
-- Uniform
-- Generate a random number with a Uniform distribution
--
------------------------------------------------------------
impure function Uniform (Min, Max : in real) return real ;
impure function Uniform (Min, Max : integer) return integer ;
impure function Uniform (Min, Max : integer ; Exclude : integer_vector) return integer ;
------------------------------------------------------------
--
-- FavorSmall
-- Generate random numbers with a greater number of small
-- values than large values
--
------------------------------------------------------------
impure function FavorSmall (Min, Max : real) return real ;
impure function FavorSmall (Min, Max : integer) return integer ;
impure function FavorSmall (Min, Max : integer ; Exclude : integer_vector) return integer ;
------------------------------------------------------------
--
-- FavorBig
-- Generate random numbers with a greater number of large
-- values than small values
--
------------------------------------------------------------
impure function FavorBig (Min, Max : real) return real ;
impure function FavorBig (Min, Max : integer) return integer ;
impure function FavorBig (Min, Max : integer ; Exclude : integer_vector) return integer ;
-----------------------------------------------------------------
--
-- Normal
-- Generate a random number with a normal distribution
-- Uses Box Muller, per Wikipedia
--
------------------------------------------------------------
impure function Normal (Mean, StdDeviation : real) return real ;
impure function Normal (Mean, StdDeviation, Min, Max : real) return real ;
impure function Normal (
Mean : real ;
StdDeviation : real ;
Min : integer ;
Max : integer ;
Exclude : integer_vector := NULL_INTV
) return integer ;
-----------------------------------------------------------------
-- Poisson
-- Generate a random number with a poisson distribution
-- Discrete distribution = only generates integral values
-- Uses knuth method, per Wikipedia
--
------------------------------------------------------------
impure function Poisson (Mean : real) return real ;
impure function Poisson (Mean, Min, Max : real) return real ;
impure function Poisson (
Mean : real ;
Min : integer ;
Max : integer ;
Exclude : integer_vector := NULL_INTV
) return integer ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Randomization with range.
-- Uses internal settings of RandomParm to deterimine distribution.
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function RandInt (Min, Max : integer) return integer ;
impure function RandReal (Min, Max : Real) return real ;
impure function RandTime (Min, Max : time ; Unit : time := ns) return time ;
impure function RandSlv (Min, Max, Size : natural) return std_logic_vector ;
impure function RandUnsigned (Min, Max, Size : natural) return Unsigned ;
impure function RandSigned (Min, Max : integer ; Size : natural) return Signed ;
impure function RandIntV (Min, Max : integer ; Size : natural) return integer_vector ;
impure function RandIntV (Min, Max : integer ; Unique : natural ; Size : natural) return integer_vector ;
impure function RandRealV (Min, Max : real ; Size : natural) return real_vector ;
impure function RandTimeV (Min, Max : time ; Size : natural ; Unit : time := ns) return time_vector ;
impure function RandTimeV (Min, Max : time ; Unique : natural ; Size : natural ; Unit : time := ns) return time_vector ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Randomization with range and exclude vector.
-- Uses internal settings of RandomParm to deterimine distribution.
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function RandInt (Min, Max : integer ; Exclude : integer_vector ) return integer ;
impure function RandTime (Min, Max : time ; Exclude : time_vector ; Unit : time := ns) return time ;
impure function RandSlv (Min, Max : natural ; Exclude : integer_vector ; Size : natural) return std_logic_vector ;
impure function RandUnsigned (Min, Max : natural ; Exclude : integer_vector ; Size : natural) return Unsigned ;
impure function RandSigned (Min, Max : integer ; Exclude : integer_vector ; Size : natural) return Signed ;
impure function RandIntV (Min, Max : integer ; Exclude : integer_vector ; Size : natural) return integer_vector ;
impure function RandIntV (Min, Max : integer ; Exclude : integer_vector ; Unique : natural ; Size : natural) return integer_vector ;
impure function RandTimeV (Min, Max : time ; Exclude : time_vector ; Size : natural ; Unit : in time := ns) return time_vector ;
impure function RandTimeV (Min, Max : time ; Exclude : time_vector ; Unique : natural ; Size : natural ; Unit : in time := ns) return time_vector ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Randomly select a value within a set of values
-- Uses internal settings of RandomParm to deterimine distribution.
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function RandInt (A : integer_vector ) return integer ;
impure function RandReal (A : real_vector ) return real ;
impure function RandTime (A : time_vector ) return time ;
impure function RandSlv (A : integer_vector ; Size : natural) return std_logic_vector ;
impure function RandUnsigned (A : integer_vector ; Size : natural) return Unsigned ;
impure function RandSigned (A : integer_vector ; Size : natural) return Signed ;
impure function RandIntV (A : integer_vector ; Size : natural) return integer_vector ;
impure function RandIntV (A : integer_vector ; Unique : natural ; Size : natural) return integer_vector ;
impure function RandRealV (A : real_vector ; Size : natural) return real_vector ;
impure function RandRealV (A : real_vector ; Unique : natural ; Size : natural) return real_vector ;
impure function RandTimeV (A : time_vector ; Size : natural) return time_vector ;
impure function RandTimeV (A : time_vector ; Unique : natural ; Size : natural) return time_vector ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Randomly select a value within a set of values with exclude values (so can skip last or last n)
-- Uses internal settings of RandomParm to deterimine distribution.
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function RandInt (A, Exclude : integer_vector ) return integer ;
impure function RandReal (A, Exclude : real_vector ) return real ;
impure function RandTime (A, Exclude : time_vector ) return time ;
impure function RandSlv (A, Exclude : integer_vector ; Size : natural) return std_logic_vector ;
impure function RandUnsigned (A, Exclude : integer_vector ; Size : natural) return Unsigned ;
impure function RandSigned (A, Exclude : integer_vector ; Size : natural ) return Signed ;
impure function RandIntV (A, Exclude : integer_vector ; Size : natural) return integer_vector ;
impure function RandIntV (A, Exclude : integer_vector ; Unique : natural ; Size : natural) return integer_vector ;
impure function RandRealV (A, Exclude : real_vector ; Size : natural) return real_vector ;
impure function RandRealV (A, Exclude : real_vector ; Unique : natural ; Size : natural) return real_vector ;
impure function RandTimeV (A, Exclude : time_vector ; Size : natural) return time_vector ;
impure function RandTimeV (A, Exclude : time_vector ; Unique : natural ; Size : natural) return time_vector ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Basic Discrete Distributions
-- Randomly select between 0 and N-1 based on the specified weight.
-- where N = number values in weight array
-- Always uses Uniform
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function DistInt (Weight : integer_vector ) return integer ;
impure function DistSlv (Weight : integer_vector ; Size : natural ) return std_logic_vector ;
impure function DistUnsigned (Weight : integer_vector ; Size : natural ) return unsigned ;
impure function DistSigned (Weight : integer_vector ; Size : natural ) return signed ;
impure function DistBool (Weight : NaturalVBoolType ) return boolean ;
impure function DistSl (Weight : NaturalVSlType ) return std_logic ;
impure function DistBit (Weight : NaturalVBitType ) return bit ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Basic Distributions with exclude values (so can skip last or last n)
-- Always uses Uniform via DistInt
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function DistInt (Weight : integer_vector ; Exclude : integer_vector ) return integer ;
impure function DistSlv (Weight : integer_vector ; Exclude : integer_vector ; Size : natural ) return std_logic_vector ;
impure function DistUnsigned (Weight : integer_vector ; Exclude : integer_vector ; Size : natural ) return unsigned ;
impure function DistSigned (Weight : integer_vector ; Exclude : integer_vector ; Size : natural ) return signed ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Distribution for sparse values
-- Specify weight and value
-- Always uses Uniform via DistInt
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function DistValInt (A : DistType ) return integer ;
impure function DistValSlv (A : DistType ; Size : natural) return std_logic_vector ;
impure function DistValUnsigned (A : DistType ; Size : natural) return unsigned ;
impure function DistValSigned (A : DistType ; Size : natural) return signed ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Distribution for sparse values with exclude values (so can skip last or last n)
-- Specify weight and value
-- Always uses Uniform via DistInt
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function DistValInt (A : DistType ; Exclude : integer_vector ) return integer ;
impure function DistValSlv (A : DistType ; Exclude : integer_vector ; Size : natural) return std_logic_vector ;
impure function DistValUnsigned (A : DistType ; Exclude : integer_vector ; Size : natural) return unsigned ;
impure function DistValSigned (A : DistType ; Exclude : integer_vector ; Size : natural) return signed ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Large vector handling.
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function RandUnsigned (Size : natural) return unsigned ;
impure function RandSlv (Size : natural) return std_logic_vector ;
impure function RandSigned (Size : natural) return signed ;
impure function RandUnsigned (Max : Unsigned) return unsigned ;
impure function RandSlv (Max : std_logic_vector) return std_logic_vector ;
impure function RandSigned (Max : signed) return signed ;
impure function RandUnsigned (Min, Max : unsigned) return unsigned ;
impure function RandSlv (Min, Max : std_logic_vector) return std_logic_vector ;
impure function RandSigned (Min, Max : signed) return signed ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Convenience Functions. Resolve into calls into the other functions
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function RandReal return real ; -- 0.0 to 1.0
impure function RandReal (Max : Real) return real ; -- 0.0 to Max
impure function RandInt return integer ;
impure function RandInt (Max : integer) return integer ;
impure function RandSlv (Max, Size : natural) return std_logic_vector ;
impure function RandUnsigned (Max, Size : natural) return Unsigned ;
impure function RandSigned (Max : integer ; Size : natural ) return Signed ;
impure function RandBool return boolean;
impure function RandSl return std_logic;
impure function RandBit return bit;
end protected RandomPType ;
end RandomPkg ;
--- ///////////////////////////////////////////////////////////////////////////
--- ///////////////////////////////////////////////////////////////////////////
--- ///////////////////////////////////////////////////////////////////////////
package body RandomPkg is
-----------------------------------------------------------------
-- Local Randomization Support
-----------------------------------------------------------------
constant NULL_SLV : std_logic_vector (NULL_RANGE_TYPE) := (others => '0') ;
constant NULL_UV : unsigned (NULL_RANGE_TYPE) := (others => '0') ;
constant NULL_SV : signed (NULL_RANGE_TYPE) := (others => '0') ;
--- ///////////////////////////////////////////////////////////////////////////
--- RandomPType Body
--- ///////////////////////////////////////////////////////////////////////////
type RandomPType is protected body
variable RandomSeed : RandomSeedType := OldGenRandSeed(integer_vector'(1,7)) ;
--- ///////////////////////////////////////////////////////////////////////////
---
--- Base Call to Uniform. Use this one rather than RandomBasePkg
---
--- ///////////////////////////////////////////////////////////////////////////
-----------------------------------------------------------------
impure function Uniform return real is
-----------------------------------------------------------------
variable rRandom : real ;
begin
ieee.math_real.Uniform (RandomSeed(RandomSeed'left), RandomSeed(RandomSeed'right), rRandom) ;
return rRandom ;
end function Uniform ;
--- ///////////////////////////////////////////////////////////////////////////
---
--- Seed Manipulation
---
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
procedure InitSeed (S : string ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) is
------------------------------------------------------------
begin
if UseNewSeedMethods then
RandomSeed := GenRandSeed(S) ;
else
RandomSeed := OldGenRandSeed(S) ;
end if ;
end procedure InitSeed ;
------------------------------------------------------------
procedure InitSeed (I : integer ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) is
------------------------------------------------------------
begin
if UseNewSeedMethods then
RandomSeed := GenRandSeed(I) ;
else
RandomSeed := OldGenRandSeed(I) ;
end if ;
end procedure InitSeed ;
------------------------------------------------------------
procedure InitSeed (T : time ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) is
------------------------------------------------------------
begin
RandomSeed := GenRandSeed(T) ;
end procedure InitSeed ;
------------------------------------------------------------
procedure InitSeed (IV : integer_vector ; UseNewSeedMethods : boolean := RANDOM_USE_NEW_SEED_METHODS ) is
------------------------------------------------------------
begin
if UseNewSeedMethods then
RandomSeed := GenRandSeed(IV) ;
else
RandomSeed := OldGenRandSeed(IV) ;
end if ;
end procedure InitSeed ;
------------------------------------------------------------
procedure SetSeed (RandomSeedIn : RandomSeedType ) is
------------------------------------------------------------
begin
RandomSeed := RandomSeedIn ;
end procedure SetSeed ;
------------------------------------------------------------
procedure SeedRandom (RandomSeedIn : RandomSeedType ) is
------------------------------------------------------------
begin
RandomSeed := RandomSeedIn ;
end procedure SeedRandom ;
------------------------------------------------------------
impure function GetSeed return RandomSeedType is
------------------------------------------------------------
begin
return RandomSeed ;
end function GetSeed ;
------------------------------------------------------------
impure function SeedRandom return RandomSeedType is
------------------------------------------------------------
begin
return RandomSeed ;
end function SeedRandom ;
--- ///////////////////////////////////////////////////////////////////////////
---
--- Setting Randomization Parameters
---
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
variable RandomParm : RandomParamType ; -- left most values ok for init
------------------------------------------------------------
procedure SetRandomParm (RandomParmIn : RandomParamType) is
------------------------------------------------------------
begin
RandomParm := RandomParmIn ;
end procedure SetRandomParm ;
------------------------------------------------------------
procedure SetRandomParm (
------------------------------------------------------------
Distribution : RandomDistType ;
Mean : Real := 0.0 ;
Deviation : Real := 0.0
) is
begin
RandomParm := RandomParamType'(Distribution, Mean, Deviation) ;
end procedure SetRandomParm ;
------------------------------------------------------------
impure function GetRandomParm return RandomParamType is
------------------------------------------------------------
begin
return RandomParm ;
end function GetRandomParm ;
------------------------------------------------------------
impure function GetRandomParm return RandomDistType is
------------------------------------------------------------
begin
return RandomParm.Distribution ;
end function GetRandomParm ;
------------------------------------------------------------
-- Deprecated. For compatibility with previous version
procedure SetRandomMode (RandomDistIn : RandomDistType) is
------------------------------------------------------------
begin
SetRandomParm(RandomDistIn) ;
end procedure SetRandomMode ;
--- ///////////////////////////////////////////////////////////////////////////
---
--- Check ranges for Randomization and Generate FAILURE
---
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
-- PT Local
impure function CheckMinMax(
------------------------------------------------------------
constant Name : in string ;
constant Min : in real ;
constant Max : in real
) return real is
begin
if Min > Max then
Alert(OSVVM_RANDOM_ALERTLOG_ID,
"RandomPkg." & Name &
": Min: " & to_string(Min, 2) &
" > Max: " & to_string(Max, 2),
FAILURE ) ;
return Min ;
else
return Max ;
end if;
end function CheckMinMax ;
------------------------------------------------------------
-- PT Local
impure function CheckMinMax(
------------------------------------------------------------
constant Name : in string ;
constant Min : in integer ;
constant Max : in integer
) return integer is
begin
if Min > Max then
Alert(OSVVM_RANDOM_ALERTLOG_ID,
"RandomPkg." & Name &
": Min: " & to_string(Min) &
" > Max: " & to_string(Max),
FAILURE ) ;
return Min ;
else
return Max ;
end if;
end function CheckMinMax ;
------------------------------------------------------------
-- PT Local
impure function CheckMinMax(
------------------------------------------------------------
constant Name : in string ;
constant Min : in time ;
constant Max : in time
) return time is
begin
if Min > Max then
Alert(OSVVM_RANDOM_ALERTLOG_ID,
"RandomPkg." & Name &
": Min: " & to_string(Min, ns) &
" > Max: " & to_string(Max, ns),
FAILURE ) ;
return Min ;
else
return Max ;
end if;
end function CheckMinMax ;
--- ///////////////////////////////////////////////////////////////////////////
---
--- Base Randomization Distributions
---
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
--
-- Uniform
-- Generate a random number with a Uniform distribution
--
------------------------------------------------------------
impure function LocalUniform (Min, Max : in real) return real is
------------------------------------------------------------
begin
return scale(Uniform, Min, Max) ;
end function LocalUniform ;
------------------------------------------------------------
impure function Uniform (Min, Max : in real) return real is
------------------------------------------------------------
constant CkMax : real := CheckMinMax("Uniform", Min, Max) ;
begin
return LocalUniform(Min, CkMax) ;
end function Uniform ;
------------------------------------------------------------
impure function LocalUniform (Min, Max : integer) return integer is
------------------------------------------------------------
begin
return scale(Uniform, Min, Max) ;
end function LocalUniform ;
------------------------------------------------------------
impure function Uniform (Min, Max : integer) return integer is
------------------------------------------------------------
constant CkMax : integer := CheckMinMax("Uniform", Min, Max) ;
begin
return LocalUniform(Min, CkMax) ;
end function Uniform ;
------------------------------------------------------------
impure function LocalUniform (Min, Max : integer ; Exclude : integer_vector) return integer is
------------------------------------------------------------
variable iRandomVal : integer ;
variable ExcludeList : SortListPType ;
variable count : integer ;
begin
ExcludeList.add(Exclude, Min, Max) ;
count := ExcludeList.count ;
iRandomVal := Uniform(Min, Max - count) ;
-- adjust count, note iRandomVal changes while checking.
for i in 1 to count loop
exit when iRandomVal < ExcludeList.Get(i) ;
iRandomVal := iRandomVal + 1 ;
end loop ;
ExcludeList.erase ;
return iRandomVal ;
end function LocalUniform ;
------------------------------------------------------------
impure function Uniform (Min, Max : integer ; Exclude : integer_vector) return integer is
------------------------------------------------------------
constant CkMax : integer := CheckMinMax("Uniform", Min, Max) ;
begin
return LocalUniform (Min, CkMax, Exclude) ;
end function Uniform ;
------------------------------------------------------------
--
-- FavorSmall
-- Generate random numbers with a greater number of small
-- values than large values
--
------------------------------------------------------------
impure function FavorSmall (Min, Max : real) return real is
------------------------------------------------------------
constant CkMax : real := CheckMinMax("FavorSmall", Min, Max) ;
begin
return scale(FavorSmall(Uniform), Min, CkMax) ; -- real
end function FavorSmall ;
------------------------------------------------------------
impure function FavorSmall (Min, Max : integer) return integer is
------------------------------------------------------------
constant CkMax : integer := CheckMinMax("FavorSmall", Min, Max) ;
begin
return scale(FavorSmall(Uniform), Min, CkMax) ; -- integer
end function FavorSmall ;
------------------------------------------------------------
impure function FavorSmall (Min, Max : integer ; Exclude : integer_vector) return integer is
------------------------------------------------------------
variable iRandomVal : integer ;
variable ExcludeList : SortListPType ;
variable count : integer ;
constant CkMax : integer := CheckMinMax("FavorSmall", Min, Max) ;
begin
ExcludeList.add(Exclude, Min, CkMax) ;
count := ExcludeList.count ;
iRandomVal := FavorSmall(Min, CkMax - count) ;
-- adjust count, note iRandomVal changes while checking.
for i in 1 to count loop
exit when iRandomVal < ExcludeList.Get(i) ;
iRandomVal := iRandomVal + 1 ;
end loop ;
ExcludeList.erase ;
return iRandomVal ;
end function FavorSmall ;
------------------------------------------------------------
--
-- FavorBig
-- Generate random numbers with a greater number of large
-- values than small values
--
------------------------------------------------------------
impure function FavorBig (Min, Max : real) return real is
------------------------------------------------------------
constant CkMax : real := CheckMinMax("FavorBig", Min, Max) ;
begin
return scale(FavorBig(Uniform), Min, CkMax) ; -- real
end function FavorBig ;
------------------------------------------------------------
impure function FavorBig (Min, Max : integer) return integer is
------------------------------------------------------------
constant CkMax : integer := CheckMinMax("FavorBig", Min, Max) ;
begin
return scale(FavorBig(Uniform), Min, CkMax) ; -- integer
end function FavorBig ;
------------------------------------------------------------
impure function FavorBig (Min, Max : integer ; Exclude : integer_vector) return integer is
------------------------------------------------------------
variable iRandomVal : integer ;
variable ExcludeList : SortListPType ;
variable count : integer ;
constant CkMax : integer := CheckMinMax("FavorBig", Min, Max) ;
begin
ExcludeList.add(Exclude, Min, CkMax) ;
count := ExcludeList.count ;
iRandomVal := FavorBig(Min, CkMax - count) ;
-- adjust count, note iRandomVal changes while checking.
for i in 1 to count loop
exit when iRandomVal < ExcludeList.Get(i) ;
iRandomVal := iRandomVal + 1 ;
end loop ;
ExcludeList.erase ;
return iRandomVal ;
end function FavorBig ;
-----------------------------------------------------------------
--
-- Normal
-- Generate a random number with a normal distribution
--
-- Use Box Muller, per Wikipedia :
-- http ://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
--
------------------------------------------------------------
impure function Normal (Mean, StdDeviation : real) return real is
------------------------------------------------------------
variable x01, y01 : real ;
variable StdNormalDist : real ; -- mean 0, variance 1
begin
-- add this check to set parameters?
if StdDeviation < 0.0 then
Alert(OSVVM_RANDOM_ALERTLOG_ID, "RandomPkg.Normal: Standard deviation must be >= 0.0", FAILURE) ;
return -1.0 ;
end if ;
-- Box Muller
-- Uniform (x01, RandomSeed) ;
-- Uniform (y01, RandomSeed) ;
x01 := Uniform ;
y01 := Uniform ;
StdNormalDist := sqrt(-2.0 * log(x01)) * cos(math_2_pi*y01) ;
-- Polar form rejected due to mean 50.0, std deviation = 5 resulted
-- in a median of 49
-- -- find two Uniform distributed values with range -1 to 1
-- -- that satisify S = X **2 + Y**2 < 1.0
-- loop
-- Uniform (x01, RandomSeed) ;
-- Uniform (y01, RandomSeed) ;
-- x := 2.0 * x01 - 1.0 ; -- scale to -1 to 1
-- y := 2.0 * y01 - 1.0 ;
-- s := x*x + y*y ;
-- exit when s < 1.0 and s > 0.0 ;
-- end loop ;
-- -- Calculate Standard Normal Distribution
-- StdNormalDist := x * sqrt((-2.0 * log(s)) / s) ;
-- Convert to have Mean and StdDeviation
return StdDeviation * StdNormalDist + Mean ;
end function Normal ;
------------------------------------------------------------
-- Normal + RandomVal >= Min and RandomVal <= Max
impure function Normal (Mean, StdDeviation, Min, Max : real) return real is
------------------------------------------------------------
variable rRandomVal : real ;
begin
if Max < Min then
Alert(OSVVM_RANDOM_ALERTLOG_ID,
"RandomPkg.Normal: Min: " & to_string(Min, 2) &
" > Max: " & to_string(Max, 2),
FAILURE) ;
return Mean ;
else
loop
rRandomVal := Normal (Mean, StdDeviation) ;
exit when rRandomVal >= Min and rRandomVal <= Max ;
end loop ;
end if ;
return rRandomVal ;
end function Normal ;
------------------------------------------------------------
-- Normal + RandomVal >= Min and RandomVal <= Max
impure function Normal (
------------------------------------------------------------
Mean : real ;
StdDeviation : real ;
Min : integer ;
Max : integer ;
Exclude : integer_vector := NULL_INTV
) return integer is
variable iRandomVal : integer ;
begin
if Max < Min then
Alert(OSVVM_RANDOM_ALERTLOG_ID,
"RandomPkg.Normal: Min: " & to_string(Min) &
" > Max: " & to_string(Max),
FAILURE) ;
return integer(round(Mean)) ;
else
loop
iRandomVal := integer(round( Normal(Mean, StdDeviation) )) ;
exit when iRandomVal >= Min and iRandomVal <= Max and
not inside(iRandomVal, Exclude) ;
end loop ;
end if ;
return iRandomVal ;
end function Normal ;
-----------------------------------------------------------------
-- Poisson
-- Generate a random number with a poisson distribution
-- Discrete distribution = only generates integral values
--
-- Use knuth method, per Wikipedia :
-- http ://en.wikipedia.org/wiki/Poisson_distribution
--
------------------------------------------------------------
impure function Poisson (Mean : real) return real is
------------------------------------------------------------
variable Product : Real := 1.0 ;
variable Bound : Real := 0.0 ;
variable UniformRand : Real := 0.0 ;
variable PoissonRand : Real := 0.0 ;
begin
Bound := exp(-1.0 * Mean) ;
Product := 1.0 ;
-- add this check to set parameters?
if Mean <= 0.0 or Bound <= 0.0 then
Alert(OSVVM_RANDOM_ALERTLOG_ID, "RandomPkg.Poisson: Mean < 0 or too large. Mean = " & real'image(Mean), FAILURE) ;
return Mean ;
end if ;
while (Product >= Bound) loop
PoissonRand := PoissonRand + 1.0 ;
UniformRand := Uniform ;
Product := Product * UniformRand ;
end loop ;
return PoissonRand ;
end function Poisson ; -- no range
------------------------------------------------------------
-- Poisson + RandomVal >= Min and RandomVal < Max
impure function Poisson (Mean, Min, Max : real) return real is
------------------------------------------------------------
variable rRandomVal : real ;
begin
if Max < Min then
Alert(OSVVM_RANDOM_ALERTLOG_ID,
"RandomPkg.Poisson: Min: " & to_string(Min, 2) &
" > Max: " & to_string(Max, 2),
FAILURE) ;
return Mean ;
else
loop
rRandomVal := Poisson (Mean) ;
exit when rRandomVal >= Min and rRandomVal <= Max ;
end loop ;
end if ;
return rRandomVal ;
end function Poisson ;
------------------------------------------------------------
impure function Poisson (
------------------------------------------------------------
Mean : real ;
Min : integer ;
Max : integer ;
Exclude : integer_vector := NULL_INTV
) return integer is
variable iRandomVal : integer ;
begin
if Max < Min then
Alert(OSVVM_RANDOM_ALERTLOG_ID,
"RandomPkg.Poisson: Min: " & to_string(Min) &
" > Max: " & to_string(Max),
FAILURE) ;
return integer(round(Mean)) ;
else
loop
iRandomVal := integer(round( Poisson (Mean) )) ;
exit when iRandomVal >= Min and iRandomVal <= Max and
not inside(iRandomVal, Exclude) ;
end loop ;
end if ;
return iRandomVal ;
end function Poisson ;
--- ///////////////////////////////////////////////////////////////////////////
--
-- Randomization with range.
-- Uses internal settings of RandomParm to deterimine distribution.
--
--- ///////////////////////////////////////////////////////////////////////////
------------------------------------------------------------
impure function LocalRandInt (Min, Max : integer) return integer is
------------------------------------------------------------
begin
case RandomParm.Distribution is
when UNIFORM => return LocalUniform(Min, Max) ;
when FAVOR_SMALL => return FavorSmall(Min, Max) ;
when FAVOR_BIG => return FavorBig (Min, Max) ;
when NORMAL => return Normal(RandomParm.Mean, RandomParm.StdDeviation, Min, Max) ;
when POISSON => return Poisson(RandomParm.Mean, Min, Max) ;
when others =>
Alert(OSVVM_RANDOM_ALERTLOG_ID, "RandomPkg.RandInt: RandomParm.Distribution not implemented", FAILURE) ;
return integer'low ;
end case ;
end function LocalRandInt ;
------------------------------------------------------------
impure function RandInt (Min, Max : integer) return integer is
------------------------------------------------------------
constant CkMax : integer := CheckMinMax("RandInt", Min, Max) ;
begin
return LocalRandInt(Min, CkMax) ;
end function RandInt ;
------------------------------------------------------------
impure function RandSlv (Min, Max, Size : natural) return std_logic_vector is
------------------------------------------------------------
constant CkMax : integer := CheckMinMax("RandSlv", Min, Max) ;
begin
return std_logic_vector(to_unsigned(LocalRandInt(Min, CkMax), Size)) ;
end function RandSlv ;