Skip to content

Commit 63a2f2e

Browse files
committed
initial commit
0 parents  commit 63a2f2e

13 files changed

+293
-0
lines changed

.gitignore

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.vscode
2+
.venv
3+
*.pin
4+
*.pof
5+
*.sof
6+
*.qpf
7+
*.qsf
8+
*.sid
9+
*.map.*
10+
*.sta.*
11+
*.fit.*
12+
bitstream.tcl
13+
bitstream
14+
incremental_db
15+
*.hdb
16+
*.cdb
17+
*.ddb
18+
*.idb
19+
*.rdb
20+
*.logdb
21+
*.qmsg
22+
*.hsd
23+
*.ammdb
24+
db
25+
*.rpt
26+
*.sld
27+
*.jdi
28+
*.done
29+
*.pow.*
30+
Makefile
31+
project
32+
project.tcl
33+
files.tcl
34+
*.vcd
35+
*.vvp
36+
run.command
37+
modelsim.ini
38+
transcript
39+
work/
40+
*.wlf
41+
ip_cores/

.travis.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
sudo: required
2+
language: python
3+
os: linux
4+
addons:
5+
apt:
6+
update: false
7+
packages:
8+
- lib32z1
9+
- lib32stdc++6
10+
- libexpat1:i386
11+
- libc6:i386
12+
- libsm6:i386
13+
- libncurses5:i386
14+
- libx11-6:i386
15+
- zlib1g:i386
16+
- libxext6:i386
17+
- libxft2:i386
18+
19+
install:
20+
- pip install -r requirements.txt
21+
- stat /home/travis/intelFPGA/19.1/modelsim_ase || (curl 'http://download.altera.com/akdlm/software/acdsinst/19.1std/670/ib_installers/ModelSimSetup-19.1.0.670-linux.run' -o ModelSimSetup.run && chmod +x ModelSimSetup.run && travis_wait 30 ./ModelSimSetup.run --mode unattended --accept_eula 1 && sed -i 's/linux_rh60/linux/g' /home/travis/intelFPGA/19.1/modelsim_ase/vco )
22+
script:
23+
- export PATH=$PATH:/home/travis/intelFPGA/19.1/modelsim_ase/bin
24+
- cd ./sim/xorshift_tb/ && hdlmake fetch && hdlmake && make
25+
- cd -
26+
27+
cache:
28+
directories:
29+
- /home/travis/intelFPGA/

LICENSE-APACHE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2020 Sameer Puri
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

LICENSE-MIT

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Sameer Puri
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Manifest.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
modules = {
2+
"local": "./src/"
3+
}

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# rand
2+
3+
[![Build Status](https://travis-ci.com/hdl-util/rand.svg?branch=master)](https://travis-ci.com/hdl-util/rand)
4+
5+
SystemVerilog code for arbitrary width random number utilities.
6+
7+
## Why?
8+
9+
I needed an efficient, easy way to generate gray codes for dual clock FIFOs. It's a pain to manually write out a gray code. Why not let a module do the heavy lifting for you?
10+
11+
## Usage
12+
13+
### LFSR
14+
15+
1. Take files from `src/` and add them to your own project. If you use [hdlmake](https://hdlmake.readthedocs.io/en/master/), you can add this repository itself as a remote module.
16+
1. Other helpful modules are also available in this GitHub organization.
17+
1. Consult the testbench in `test/xorshift_tb.sv` for example usage.
18+
1. Read through the parameter descriptions in `xorshift.sv` and tailor any instantiations to your situation.
19+
1. Please create an issue if you run into a problem or have any questions.
20+
21+
## To-do List
22+
23+
* [x] [Xorshift](https://en.wikipedia.org/wiki/Xorshift) (LFSR)
24+
* [ ] [CBRNG](https://en.wikipedia.org/wiki/Counter-based_random_number_generator_(CBRNG)#Squares_RNG)
25+
* Making Squares RNG arbitrary width seems non-trivial, unfortunately
26+
* More upon request

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hdlmake==3.3

sim/vsim.do

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run -all;

sim/xorshift_tb/Manifest.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
action = "simulation"
2+
sim_tool = "modelsim"
3+
sim_top = "xorshift_tb"
4+
5+
sim_post_cmd = "vsim -novopt -do ../vsim.do -c xorshift_tb"
6+
7+
modules = {
8+
"local" : [ "../../test/" ],
9+
}

src/Manifest.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
files = [
2+
"xorshift.sv"
3+
]

src/xorshift.sv

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
module xorshift #(
2+
parameter int WIDTH,
3+
// By default, a polynomial from a table of known polynomials is picked.
4+
// You may want to specify your own if your polynomial is beyond the supported range
5+
parameter bit [WIDTH-1:0] POLYNOMIAL = WIDTH'(0)
6+
) (
7+
input logic clk,
8+
input logic reset,
9+
input logic [WIDTH-1:0] seed,
10+
output logic [WIDTH-1:0] state
11+
);
12+
13+
logic [WIDTH-1:0] polynomial;
14+
logic lfsr_xnor;
15+
16+
integer i;
17+
always_comb
18+
begin
19+
lfsr_xnor = 1'b0;
20+
for (i = 0; i < WIDTH; i++)
21+
begin
22+
if (polynomial[i])
23+
lfsr_xnor = lfsr_xnor ^~ state[i];
24+
end
25+
end
26+
27+
always_ff @(posedge clk)
28+
begin
29+
if (reset)
30+
state <= seed;
31+
else
32+
state <= {state[WIDTH-2:0], lfsr_xnor};
33+
end
34+
35+
generate
36+
if (POLYNOMIAL != WIDTH'(0))
37+
assign polynomial = POLYNOMIAL;
38+
else
39+
case (WIDTH)
40+
// Special thanks to Philip Koopman for these maximal length LFSR feedback polynomials
41+
// for i in {4..64}; do echo -n $i': assign polynomial = '$i"'"'h'; curl 'http://users.ece.cmu.edu/~koopman/lfsr/'$i'.txt' 2>/dev/null | head -n 1 | tr -d '\n'; echo ';'; done
42+
4: assign polynomial = 4'h9;
43+
5: assign polynomial = 5'h12;
44+
6: assign polynomial = 6'h21;
45+
7: assign polynomial = 7'h41;
46+
8: assign polynomial = 8'h8E;
47+
9: assign polynomial = 9'h108;
48+
10: assign polynomial = 10'h204;
49+
11: assign polynomial = 11'h402;
50+
12: assign polynomial = 12'h829;
51+
13: assign polynomial = 13'h100D;
52+
14: assign polynomial = 14'h2015;
53+
15: assign polynomial = 15'h4001;
54+
16: assign polynomial = 16'h8016;
55+
17: assign polynomial = 17'h10004;
56+
18: assign polynomial = 18'h20013;
57+
19: assign polynomial = 19'h40013;
58+
20: assign polynomial = 20'h80004;
59+
21: assign polynomial = 21'h100002;
60+
22: assign polynomial = 22'h200001;
61+
23: assign polynomial = 23'h400010;
62+
24: assign polynomial = 24'h80000D;
63+
25: assign polynomial = 25'h1000004;
64+
26: assign polynomial = 26'h2000023;
65+
27: assign polynomial = 27'h4000013;
66+
28: assign polynomial = 28'h8000004;
67+
29: assign polynomial = 29'h10000002;
68+
30: assign polynomial = 30'h20000029;
69+
31: assign polynomial = 31'h40000004;
70+
32: assign polynomial = 32'h80000057;
71+
33: assign polynomial = 33'h100000029;
72+
34: assign polynomial = 34'h200000073;
73+
35: assign polynomial = 35'h400000002;
74+
36: assign polynomial = 36'h80000003B;
75+
37: assign polynomial = 37'h100000001F;
76+
38: assign polynomial = 38'h2000000031;
77+
39: assign polynomial = 39'h4000000008;
78+
40: assign polynomial = 40'h800000001C;
79+
41: assign polynomial = 41'h10000000004;
80+
42: assign polynomial = 42'h2000000001F;
81+
43: assign polynomial = 43'h4000000002C;
82+
44: assign polynomial = 44'h80000000032;
83+
45: assign polynomial = 45'h10000000000D;
84+
46: assign polynomial = 46'h200000000097;
85+
47: assign polynomial = 47'h400000000010;
86+
48: assign polynomial = 48'h80000000005B;
87+
49: assign polynomial = 49'h1000000000038;
88+
50: assign polynomial = 50'h200000000000E;
89+
51: assign polynomial = 51'h4000000000025;
90+
52: assign polynomial = 52'h8000000000004;
91+
53: assign polynomial = 53'h10000000000023;
92+
54: assign polynomial = 54'h2000000000003E;
93+
55: assign polynomial = 55'h40000000000023;
94+
56: assign polynomial = 56'h8000000000004A;
95+
57: assign polynomial = 57'h100000000000016;
96+
58: assign polynomial = 58'h200000000000031;
97+
59: assign polynomial = 59'h40000000000003D;
98+
60: assign polynomial = 60'h800000000000001;
99+
61: assign polynomial = 61'h1000000000000013;
100+
62: assign polynomial = 62'h2000000000000034;
101+
63: assign polynomial = 63'h4000000000000001;
102+
64: assign polynomial = 64'h800000000000000D;
103+
endcase
104+
endgenerate
105+
106+
endmodule

test/Manifest.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
files = [
2+
"xorshift_tb.sv"
3+
]
4+
5+
modules = {
6+
"local" : [ "../src/" ],
7+
}

test/xorshift_tb.sv

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module xorshift_tb;
2+
timeunit 1ns;
3+
timeprecision 1ns;
4+
logic clk = 1'b0;
5+
always
6+
begin
7+
#1;
8+
clk <= ~clk;
9+
end
10+
localparam int WIDTH = 12;
11+
logic reset = 1'b1;
12+
logic [WIDTH-1:0] seed = WIDTH'(1), state;
13+
xorshift #(.WIDTH(WIDTH)) xorshift(.clk(clk), .reset(reset), .seed(seed), .state(state));
14+
15+
logic [WIDTH-1:0] seen [0:2**WIDTH-1];
16+
integer i, current = -1;
17+
always_ff @(posedge clk)
18+
begin
19+
if (current == -1)
20+
reset <= 1'b0;
21+
else if (current == 2**WIDTH - 1)
22+
$finish;
23+
else
24+
begin
25+
seen[current] <= state;
26+
for (i = 0; i < current; i++)
27+
begin
28+
assert(seen[i] != state) else $fatal(1, "Encountered a repeat of %d at %d %d", state, i, current);
29+
end
30+
end
31+
current <= current + 1'd1;
32+
end
33+
endmodule

0 commit comments

Comments
 (0)