Verilog 浮点数运算模块

算法中常常会到浮点数运算,而浮点数的处理常常是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);      //实际是 近似计算:of=(ic*7+128)/4=ic*1.75+32,
                           end 
            end
endmodule 
 
http://blog.sina.com.cn/s/blog_6840802c0100ir5g.html
功能仿真:
 



在t1时刻,输入ic=0x0B=11摄氏度,在iclk上升沿产生0x33=51华氏度[ of=(11*7+128)/4=51.25华氏度 的近似 ,精确实际应为:11*1.8+32=51.8华氏度
其中t6时刻,输入ic=16(0x10)(摄氏温度16度), 在iclk上升沿计算:of=(16*7+128)/4=60(0x3C), 与精确计算 F = C x 1.8  + 32=16*1.8+32=60.8,即摄氏16度对应华氏60.8,存在计算误差。
转载自:http://www.cnblogs.com/waimen/p/5777093.html
原文地址:https://www.cnblogs.com/chengqi521/p/7802787.html