格雷碼相鄰數字間隻相差一位,那麼這樣就可以做到比較低功耗。
FIFO中一般使用Gray code去表示位址。Gray碼有反射特性和自補特性,它的循環和單步特性消除了随機數出現重大錯誤的可能性。
轉換關系:
二進制轉格雷碼:
G[ n-1 ] = b[ n - 1 ] (最高位的轉化)
G[ i ] = b[i] & b[ i+1 ]
格雷碼轉二進制:
b[ n-1 ] = G[ n-1 ]
b[ i ] = G[ i ] ^ G[ i+1 ] ^ .....^G[ n -1 ]
= G[ i ] ^ b[ i + 1 ]
代碼:
Gray->Binary
module Gray_to_Binary(
iBin,
oGray
);
input wire[8:0] iBin;
output reg[8:0] Gray;
[email protected](*)begin
oGray[8] <= iBin[8];
oGray[7] <= iBin[7]^iBin[8];
oGray[6] <= iBin[6]^iBin[7];
oGray[5] <= iBin[5]^iBin[6];
oGray[4] <= iBin[4]^iBin[5];
oGray[3] <= iBin[3]^iBin[4];
oGray[2] <= iBin[2]^iBin[3];
oGray[1] <= iBin[1]^iBin[2];
oGray[0] <= iBin[0]^iBin[1];
end
endmodule
Binary->Gray
module Gray_to_Binary(
iBin,
oGray
);
input wire[8:0] oGray;
output reg[8:0] Gray;
[email protected](*)begin
iBin[8] <= oGray[8];
iBin[7] <= oGray[7]^iBin[8];
iBin[6] <= oGray[6]^iBin[7];
iBin[5] <= oGray[5]^iBin[6];
iBin[4] <= oGray[4]^iBin[5];
iBin[3] <= oGray[3]^iBin[4];
iBin[2] <= oGray[2]^iBin[3];
iBin[1] <= oGray[1]^iBin[2];
iBin[0] <= oGray[0]^iBin[1];
end
endmodule
參考資料:
http://blog.csdn.net/xiangyuqxq/article/details/7312121