Skip to content

Commit f1604e8

Browse files
committed
Added gray code converters
1 parent b8064ec commit f1604e8

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

bin2gray.sv

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//------------------------------------------------------------------------------
2+
// bin2gray.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// Gray code to binary converter
8+
// Combinational design
9+
10+
11+
/* --- INSTANTIATION TEMPLATE BEGIN ---
12+
13+
bin2gray #(
14+
.WIDTH( 32 )
15+
) BG1 (
16+
.bin_in( ),
17+
.gray_out( )
18+
);
19+
20+
--- INSTANTIATION TEMPLATE END ---*/
21+
22+
module bin2gray #( parameter
23+
WIDTH = 32
24+
)(
25+
input [WIDTH-1:0] bin_in,
26+
output logic[WIDTH-1:0] gray_out
27+
);
28+
29+
always_comb begin
30+
gray_out[WIDTH-1:0] = bin_in[WIDTH-1:0]^(bin_in[WIDTH-1:0]>>1);
31+
end
32+
33+
endmodule
34+

gray2bin.sv

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//------------------------------------------------------------------------------
2+
// gray2bin.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// Binary to gray code converter
8+
// Combinational design
9+
10+
11+
/* --- INSTANTIATION TEMPLATE BEGIN ---
12+
13+
gray2bin #(
14+
.WIDTH( 32 )
15+
) GB1 (
16+
.gray_in( ),
17+
.bin_out( )
18+
);
19+
20+
--- INSTANTIATION TEMPLATE END ---*/
21+
22+
module gray2bin #( parameter
23+
WIDTH = 32
24+
)(
25+
input [WIDTH-1:0] gray_in,
26+
output [WIDTH-1:0] bin_out
27+
);
28+
29+
genvar i;
30+
generate
31+
for( i=0; i<WIDTH; i++ ) begin
32+
assign bin_out[i] = ^gray_in[WIDTH-1:i];
33+
end
34+
endgenerate
35+
36+
endmodule
37+

gray_tb.sv

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//------------------------------------------------------------------------------
2+
// gray_tb.sv
3+
// Konstantin Pavlov, [email protected]
4+
//------------------------------------------------------------------------------
5+
6+
// INFO ------------------------------------------------------------------------
7+
// testbench for bin2gray and gray2bin module
8+
9+
10+
`timescale 1ns / 1ps
11+
12+
module gray_tb();
13+
14+
logic clk200;
15+
initial begin
16+
#0 clk200 = 1'b0;
17+
forever
18+
#2.5 clk200 = ~clk200;
19+
end
20+
21+
logic rst;
22+
initial begin
23+
#0 rst = 1'b0;
24+
#10.2 rst = 1'b1;
25+
#5 rst = 1'b0;
26+
//#10000;
27+
forever begin
28+
#9985 rst = ~rst;
29+
#5 rst = ~rst;
30+
end
31+
end
32+
33+
logic nrst;
34+
assign nrst = ~rst;
35+
36+
logic rst_once;
37+
initial begin
38+
#0 rst_once = 1'b0;
39+
#10.2 rst_once = 1'b1;
40+
#5 rst_once = 1'b0;
41+
end
42+
43+
logic nrst_once;
44+
assign nrst_once = ~rst_once;
45+
46+
logic [31:0] DerivedClocks;
47+
clk_divider #(
48+
.WIDTH( 32 )
49+
) cd1 (
50+
.clk( clk200 ),
51+
.nrst( nrst_once ),
52+
.ena( 1'b1 ),
53+
.out( DerivedClocks[31:0] )
54+
);
55+
56+
logic [31:0] E_DerivedClocks;
57+
edge_detect ed1[31:0] (
58+
.clk( {32{clk200}} ),
59+
.nrst( {32{nrst_once}} ),
60+
.in( DerivedClocks[31:0] ),
61+
.rising( E_DerivedClocks[31:0] ),
62+
.falling( ),
63+
.both( )
64+
);
65+
66+
logic [15:0] RandomNumber1;
67+
c_rand rng1 (
68+
.clk( clk200 ),
69+
.rst( rst_once ),
70+
.reseed( 1'b0 ),
71+
.seed_val( DerivedClocks[31:0] ),
72+
.out( RandomNumber1[15:0] )
73+
);
74+
75+
logic start;
76+
initial begin
77+
#0 start = 1'b0;
78+
#100 start = 1'b1;
79+
#20 start = 1'b0;
80+
end
81+
82+
// Module under test ==========================================================
83+
84+
`define WIDTH 32
85+
86+
logic [`WIDTH-1:0] bin = 0;
87+
88+
always_ff @(posedge clk200) begin
89+
if(~nrst_once) begin
90+
bin[`WIDTH-1:0] <= 0;
91+
end else begin
92+
bin[`WIDTH-1:0] <= bin[`WIDTH-1:0] + 1'b1;
93+
end
94+
end
95+
96+
logic [`WIDTH-1:0] gray;
97+
bin2gray #(
98+
.WIDTH( `WIDTH )
99+
) BG1 (
100+
.bin_in( bin[`WIDTH-1:0] ),
101+
.gray_out( gray[`WIDTH-1:0] )
102+
);
103+
104+
logic [`WIDTH-1:0] bin2;
105+
gray2bin #(
106+
.WIDTH( `WIDTH )
107+
) GB1 (
108+
.gray_in( gray[`WIDTH-1:0] ),
109+
.bin_out( bin2[`WIDTH-1:0] )
110+
);
111+
112+
//assert property
113+
// (bin[`WIDTH-1:0] == bin2[`WIDTH-1:0])
114+
//else $error("It's gone wrong");
115+
116+
endmodule

0 commit comments

Comments
 (0)