算法中常常会到浮点数运算,而浮点数的处理常常是Verilog初学中常常遇到的问题。以下将就一个简单的例子说明Verilog中浮点数运算处理。
在JPEG图像压缩时遇到色彩空间变换的问题,将YCbCr转换到RGB会遇到浮点数的运算,这个实现复杂,以摄氏温度转换为华氏温度为例 : F = C x 1.8 + 32
R = 1.164(Y-16) + 1.596(Cr-128)
G = 1.164(Y-16) - 0.391(Cb-128) - 0.813(Cr-128)
B = 1.164(Y-16) + 2.018(Cb-128)
module C2F( iclk,irstn,ic,of);
input iclk;
input irstn;
input[7:0] ic;
output[10:0] of;
reg[7:0] c;
reg[10:0] of;
always@(posedge iclk or negedge irstn)begin
if(!irstn) begin
c <= 0;
of <= 0;
end
else begin
c <= ic;
of <= c * 1.8 + 32; // 直接处理,在ISE中综合时会报出错
end //ERROR:Xst:850 - "C2F.v" line 31: Unsupported real constant.
end
endmodule
以下为改正后的程序
module C2F( iclk,irstn,ic,of);
input iclk;
input irstn;
input[7:0] ic;
output[10:0] of;
reg[7:0] c;
reg[10:0] of;
reg[10:0] sum;
always@(posedge iclk or negedge irstn)begin
if(!irstn) begin
//c <= 0;
of <= 0;
sum <= 0;
end
else begin
// c <= ic;
sum <= ic * 7+ 128;
of <= (sum >>2);
end
end
endmodule |