格雷碼(Gray Code)轉二進制碼(Binary Code)

    學習verilog generate語句時,偶然看到用generate語句來進行格雷碼到二進制碼轉換的代碼,就從網上找了一些案例來學習。html

    下表爲幾種天然二進制碼與格雷碼的對照表:web

   

十進制數 天然二進制數 格雷碼 十進制數 天然二進制數 格雷碼
0 0000 0000 8 1000 1100
1 0001 0001 9 1001 1101
2 0010 0011 10 1010 1111
3 0011 0010 11 1011 1110
4 0100 0110 12 1100 1010
5 0101 0111 13 1101 1011
6 0110 0101 14 1110 1001
7 0111 0100 15 1111 1000

 

    格雷碼轉換爲二進制碼算法有如下幾種表述形式:算法

表述一:學習

    二進制格雷碼爲Gn-1Gn-2...G2G1G0測試

    對應的天然二進制碼爲Bn-1Bn-2...B2B1B0this

    其中:最高位保留—Bn-1=Gn-1spa

    其餘各位—Bi-1=Gi-1 xor Bi ,i=1,2,...,n-1debug

表述二:3d

    Bi = ˆG[n-1:i]=G[n-1]ˆG[n-2]ˆ..ˆG[i],i=0,1,...,n-1code

表述三:

    Bi = ˆ(G>>i),i=0,1,...,n-1

 

表述一的仿真實例:

    源代碼:

 1 //http://www.cnblogs.com/adamite/archive/2008/10/20/1314949.html
 2 //example2
 3 module GrayToBinary2 (binarycode, graycode); 
 4   parameter n = 4;      // this module is parameterizable 
 5   output reg [n-1:0] binarycode; 
 6   input  [n-1:0] graycode; 
 7   integer i;
 8   always @ (graycode)
 9      begin
10           binarycode[n-1]=graycode[n-1];
11           for(i=1;i<=n-1;i=i+1)
12           binarycode[i-1]=graycode[i-1] ^ binarycode[i];//比較節省空間
13      end
14 endmodule

 

    測試代碼:

 1 `timescale 1ns/1ns
 2 module tb_GrayToBinary2;
 3 
 4 reg [3:0] gray;
 5 wire [3:0] bin;
 6 
 7 GrayToBinary2 dut (bin,gray);
 8 
 9 initial begin
10   gray = 4'h0;
11   #10;
12   gray = 4'h1;
13   #10;
14   gray = 4'h2;
15   #10;
16   gray = 4'h3;
17   #10;  
18   gray = 4'he;
19   #10;  
20   gray = 4'h7;
21   #10; 
22   gray = 4'hf;
23 end
24 endmodule

 

    仿真結果:

    modelsim生成的原理圖(注意要在vsim後面加上-debugDB選項)

    從仿真結果來看,格雷碼轉二進制碼過程當中出現錯誤。開始兩次轉換出現不定態,並且後面的轉換結果也是錯誤的。是什麼緣由呢?從算法上看,格雷碼的最高位給了二進制碼的最高位,這沒問題。可是接下來的for循環,從二進制的最低bit位開始,即B0=G0 xor B1 ,i=1,2,...,n-1。此時G0是肯定的值,可是B1還未計算出來,是不定值x,所以兩者異或結果爲不定值x,其餘bit以此類推。從modelsim生成的原理圖也能看出來,二進制碼的次態輸出除了取決於格雷碼的現態值,還依賴於二進制碼的現態值。

將上述算法進行修改,for循環從高bit爲開始,結果以下。

 

表述一修改後的仿真實例:

    源代碼:

 

 1 //http://www.cnblogs.com/adamite/archive/2008/10/20/1314949.html
 2 //example2
 3 module GrayToBinary2ex (binarycode, graycode); 
 4   parameter n = 4;      // this module is parameterizable 
 5   output reg [n-1:0] binarycode; 
 6   input  [n-1:0] graycode; 
 7   integer i;
 8   always @ (graycode)
 9      begin
10           binarycode[n-1]=graycode[n-1];
11           // for(i=1;i<=n-1;i=i+1)
12           // binarycode[i-1]=graycode[i-1] ^ binarycode[i];//比較節省空間
13           for(i=n-1;i>0;i=i-1)
14           binarycode[i-1]=graycode[i-1] ^ binarycode[i];//比較節省空間
15      end
16 endmodule

 

    測試代碼:

 1 `timescale 1ns/1ns
 2 module tb_GrayToBinary2ex;
 3 
 4 reg [3:0] gray;
 5 wire [3:0] bin;
 6 
 7 GrayToBinary2ex dut (bin,gray);
 8 
 9 initial begin
10   gray = 4'h0;
11   #10;
12   gray = 4'h1;
13   #10;
14   gray = 4'h2;
15   #10;
16   gray = 4'h3;
17   #10;  
18   gray = 4'he;
19   #10;  
20   gray = 4'h7;
21   #10; 
22   gray = 4'hf;
23 end
24 endmodule

 

    仿真結果:

    modelsim生成的原理圖(注意要在vsim後面加上-debugDB選項)

表述二的仿真實例:

    源代碼:

 1 // Verilog LRM 1364-2005.pdf P184
 2 module gray2bin (bin, gray); 
 3   parameter SIZE = 4;      // this module is parameterizable 
 4   output [SIZE-1:0] bin; 
 5   input  [SIZE-1:0] gray; 
 6   
 7   genvar i; 
 8   generate 
 9     for (i=0; i<SIZE; i=i+1) begin:bit
10       assign bin[i] = ^gray[SIZE-1:i]; 
11             // i refers to the implicitly defined localparam whose
12             // value in each instance of the generate block is
13             // the value of the genvar when it was elaborated.
14     end 
15   endgenerate
16 endmodule

 

    測試代碼:

 1 `timescale 1ns/1ns
 2 module tb_gray2bin;
 3 
 4 reg [3:0] gray;
 5 wire [3:0] bin;
 6 
 7 gray2bin dut (bin,gray);
 8 
 9 initial begin
10   gray = 4'h0;
11   #10;
12   gray = 4'h1;
13   #10;
14   gray = 4'h2;
15   #10;
16   gray = 4'h3;
17   #10;  
18   gray = 4'he;
19   #10;  
20   gray = 4'h7;
21   #10; 
22   gray = 4'hf;
23 end
24 endmodule

 

    仿真結果:

    modelsim生成的原理圖(注意要在vsim後面加上-debugDB選項)

 

 

表述三的仿真實例:

    源代碼:

 1 //http://www.cnblogs.com/adamite/archive/2008/10/20/1314949.html
 2 //example1
 3 module GrayToBinary1 (binarycode, graycode); 
 4   parameter n = 4;      // this module is parameterizable 
 5   output reg [n-1:0] binarycode; 
 6   input  [n-1:0] graycode; 
 7   integer i;
 8   always @ (graycode)
 9      begin
10           for(i=0;i<=n-1;i=i+1)
11           binarycode[i]=^(graycode>>i);//比較浪費空間
12      end
13 endmodule

 

    測試代碼:

 1 `timescale 1ns/1ns
 2 module tb_GrayToBinary1;
 3 
 4 reg [3:0] gray;
 5 wire [3:0] bin;
 6 
 7 GrayToBinary1 dut (bin,gray);
 8 
 9 initial begin
10   gray = 4'h0;
11   #10;
12   gray = 4'h1;
13   #10;
14   gray = 4'h2;
15   #10;
16   gray = 4'h3;
17   #10;  
18   gray = 4'he;
19   #10;  
20   gray = 4'h7;
21   #10; 
22   gray = 4'hf;
23 end
24 endmodule

 

    仿真結果:

    modelsim生成的原理圖(注意要在vsim後面加上-debugDB選項)

 

相關文章
相關標籤/搜索