Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | // ========== Copyright Header Begin ========================================== |
2 | // | |
3 | // OpenSPARC T2 Processor File: FNXRandomUtil.vr | |
4 | // Copyright (C) 1995-2007 Sun Microsystems, Inc. All Rights Reserved | |
5 | // 4150 Network Circle, Santa Clara, California 95054, U.S.A. | |
6 | // | |
7 | // * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
8 | // | |
9 | // This program is free software; you can redistribute it and/or modify | |
10 | // it under the terms of the GNU General Public License as published by | |
11 | // the Free Software Foundation; version 2 of the License. | |
12 | // | |
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | // | |
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | // | |
22 | // For the avoidance of doubt, and except that if any non-GPL license | |
23 | // choice is available it will apply instead, Sun elects to use only | |
24 | // the General Public License version 2 (GPLv2) at this time for any | |
25 | // software where a choice of GPL license versions is made | |
26 | // available with the language indicating that GPLv2 or any later version | |
27 | // may be used, or where a choice of which version of the GPL is applied is | |
28 | // otherwise unspecified. | |
29 | // | |
30 | // Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
31 | // CA 95054 USA or visit www.sun.com if you need additional information or | |
32 | // have any questions. | |
33 | // | |
34 | // ========== Copyright Header End ============================================ | |
35 | #include "FNXRandomUtilDefines.vri" | |
36 | #include "report_macros.vri" | |
37 | #include "cReport.vrh" | |
38 | ||
39 | class FNXRandomUtil { | |
40 | ||
41 | ReportClass Report; | |
42 | ||
43 | task new( ReportClass Report ); | |
44 | ||
45 | // Generates a random 32-bit unsigned number between min and max | |
46 | function bit [31:0] Rand32 ( bit [31:0] min = 0, | |
47 | bit [31:0] max = FNX_UTIL_MAX_32_BIT_UNSIGNED_VAL ); | |
48 | ||
49 | // Generates a random 64-bit unsigned number between min and max | |
50 | function bit [63:0] Rand64 ( bit [63:0] min = 0, | |
51 | bit [63:0] max = FNX_UTIL_MAX_64_BIT_UNSIGNED_VAL ); | |
52 | ||
53 | // Generates a random 96-bit unsigned number between min and max | |
54 | function bit [95:0] Rand96 ( bit [95:0] min = 0, | |
55 | bit [95:0] max = FNX_UTIL_MAX_96_BIT_UNSIGNED_VAL ); | |
56 | ||
57 | // Generates a random 128-bit unsigned number between min and max | |
58 | function bit [127:0] Rand128 ( bit [127:0] min = 0, | |
59 | bit [127:0] max = FNX_UTIL_MAX_128_BIT_UNSIGNED_VAL ); | |
60 | ||
61 | // Set The Random Seed. Default will be set with Time of Day | |
62 | task SetSeed( bit [31:0] RandSeed = get_systime() ); | |
63 | ||
64 | // Set Random Seed with the Time of Day | |
65 | task SeedWithTOD(); | |
66 | } | |
67 | ||
68 | task FNXRandomUtil::new ( ReportClass Report ) { | |
69 | this.Report = Report; | |
70 | } | |
71 | ||
72 | function bit [31:0] FNXRandomUtil::Rand32 ( bit [31:0] min = 0, | |
73 | bit [31:0] max = FNX_UTIL_MAX_32_BIT_UNSIGNED_VAL ) | |
74 | { | |
75 | bit [31:0] divisor; | |
76 | ||
77 | if (max >= min) { | |
78 | if ((max - min) == FNX_UTIL_MAX_32_BIT_UNSIGNED_VAL) { | |
79 | Rand32 = urandom(); | |
80 | } | |
81 | else { | |
82 | Rand32 = (urandom() % (max-min+1)) + min; | |
83 | } | |
84 | } else { | |
85 | Rand32 = min; | |
86 | } | |
87 | ||
88 | } | |
89 | ||
90 | function bit [63:0] FNXRandomUtil::Rand64 ( bit [63:0] min = 0, | |
91 | bit [63:0] max = FNX_UTIL_MAX_64_BIT_UNSIGNED_VAL) | |
92 | { | |
93 | if (max >= min) { | |
94 | if ((max - min) == FNX_UTIL_MAX_64_BIT_UNSIGNED_VAL) { | |
95 | Rand64 = {urandom(), urandom()}; | |
96 | } | |
97 | else { | |
98 | Rand64 = ({urandom(), urandom()} % (max-min+1)) + min; | |
99 | } | |
100 | } else { | |
101 | Rand64 = min; | |
102 | } | |
103 | } | |
104 | ||
105 | function bit [95:0] FNXRandomUtil::Rand96 ( bit [95:0] min = 0, | |
106 | bit [95:0] max = FNX_UTIL_MAX_96_BIT_UNSIGNED_VAL ) | |
107 | { | |
108 | if (max >= min) { | |
109 | if ((max - min) == FNX_UTIL_MAX_96_BIT_UNSIGNED_VAL) { | |
110 | Rand96 = {urandom(), urandom(), urandom()}; | |
111 | } | |
112 | else { | |
113 | Rand96 = ({urandom(), urandom(), urandom()} % (max-min+1)) + min; | |
114 | } | |
115 | } else { | |
116 | Rand96 = min; | |
117 | } | |
118 | } | |
119 | ||
120 | function bit [127:0] FNXRandomUtil::Rand128 ( bit [127:0] min = 0, | |
121 | bit [127:0] max = FNX_UTIL_MAX_128_BIT_UNSIGNED_VAL ) | |
122 | { | |
123 | if (max >= min) { | |
124 | if ((max - min) == FNX_UTIL_MAX_128_BIT_UNSIGNED_VAL) { | |
125 | Rand128 = {urandom(), urandom(), urandom(), urandom()}; | |
126 | } | |
127 | else { | |
128 | Rand128 = ({urandom(), urandom(), urandom(), urandom()} % (max-min+1)) + min; | |
129 | } | |
130 | } else { | |
131 | Rand128 = min; | |
132 | } | |
133 | } | |
134 | ||
135 | task FNXRandomUtil::SetSeed( bit [31:0] RandSeed = get_systime() ) | |
136 | { | |
137 | urandom(RandSeed); | |
138 | QuickReport( Report, | |
139 | RTYP_INFO, | |
140 | "FNXRandomUtil::SetSeed(): urandom() seed set to %0h", | |
141 | RandSeed ); | |
142 | } | |
143 | ||
144 | task FNXRandomUtil::SeedWithTOD() | |
145 | { | |
146 | bit [31:0] time_0, urand_seed; | |
147 | ||
148 | time_0 = get_systime(); | |
149 | urandom(time_0); | |
150 | urand_seed = time_0 + urandom(); | |
151 | urandom(urand_seed); | |
152 | ||
153 | QuickReport( Report, | |
154 | RTYP_INFO, | |
155 | "FNXRandomUtil::SeedWithTOD(): urandom() seeded with time of day, seed=%h", | |
156 | urand_seed ); | |
157 | } |