From f1604e8736300c3ccfb7aeebd9fca3f059f18104 Mon Sep 17 00:00:00 2001 From: Konstantin Pavlov Date: Tue, 25 Feb 2020 15:38:56 +0300 Subject: [PATCH] Added gray code converters --- bin2gray.sv | 34 +++++++++++++++ gray2bin.sv | 37 +++++++++++++++++ gray_tb.sv | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 bin2gray.sv create mode 100644 gray2bin.sv create mode 100644 gray_tb.sv diff --git a/bin2gray.sv b/bin2gray.sv new file mode 100644 index 0000000..f17e671 --- /dev/null +++ b/bin2gray.sv @@ -0,0 +1,34 @@ +//------------------------------------------------------------------------------ +// bin2gray.sv +// Konstantin Pavlov, pavlovconst@gmail.com +//------------------------------------------------------------------------------ + +// INFO ------------------------------------------------------------------------ +// Gray code to binary converter +// Combinational design + + +/* --- INSTANTIATION TEMPLATE BEGIN --- + +bin2gray #( + .WIDTH( 32 ) +) BG1 ( + .bin_in( ), + .gray_out( ) +); + +--- INSTANTIATION TEMPLATE END ---*/ + +module bin2gray #( parameter + WIDTH = 32 +)( + input [WIDTH-1:0] bin_in, + output logic[WIDTH-1:0] gray_out +); + +always_comb begin + gray_out[WIDTH-1:0] = bin_in[WIDTH-1:0]^(bin_in[WIDTH-1:0]>>1); +end + +endmodule + diff --git a/gray2bin.sv b/gray2bin.sv new file mode 100644 index 0000000..6c3fe4a --- /dev/null +++ b/gray2bin.sv @@ -0,0 +1,37 @@ +//------------------------------------------------------------------------------ +// gray2bin.sv +// Konstantin Pavlov, pavlovconst@gmail.com +//------------------------------------------------------------------------------ + +// INFO ------------------------------------------------------------------------ +// Binary to gray code converter +// Combinational design + + +/* --- INSTANTIATION TEMPLATE BEGIN --- + +gray2bin #( + .WIDTH( 32 ) +) GB1 ( + .gray_in( ), + .bin_out( ) +); + +--- INSTANTIATION TEMPLATE END ---*/ + +module gray2bin #( parameter + WIDTH = 32 +)( + input [WIDTH-1:0] gray_in, + output [WIDTH-1:0] bin_out +); + +genvar i; +generate + for( i=0; i