格雷码相邻数字间只相差一位,那么这样就可以做到比较低功耗。
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;
always@(*)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;
always@(*)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