算法中常常会到浮点数运算,而浮点数的处理常常是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

最新文章

  1. 设计模式(六):控制台中的“命令模式”(Command Pattern)
  2. 【原】让H5页面适配移动设备全家 - 前端篇 - PPT
  3. Sql Server函数全解(一)字符串函数
  4. c# 实现 java 的 System.currentTimeMillis() 值
  5. subroutines of perl
  6. 浅析C#深拷贝与浅拷贝
  7. LintCode Validate Binary Search Tree
  8. Access“输入的表达式中含有一个无效日期值”
  9. Windows7下出现“不支持此接口”的解决方案
  10. bootstrap tab切换如何让鼠标移动自动切换内容
  11. ORACLE 日期函数
  12. the specified child alread has a parent
  13. 【BZOJ [1878】[SDOI2009]HH的项链
  14. Linux的文件属性
  15. 使用ownCloud在Linux安装你的个人云服务
  16. Everything You Wanted to Know About Machine Learning
  17. (中等) HDU 1542 Atlantis,扫描线。
  18. linux下访问windows的共享
  19. 无法连接MySQL
  20. kerbose常用操作

热门文章

  1. CF978E Bus Video System【数学/前缀和/思维】
  2. KMP【UVA1328】 Period
  3. Xcode_9_beta.xip 更新下载
  4. 每天一个liunx命令4之 ps -ef ,ps -aux ,ps aux
  5. 纯CSS实现网站常用的五角星评分和分数展示交互效果
  6. UIWebView to view self signed websites (No private api, not NSURLConnection) - is it possible?
  7. httpanalyzer 抓包时会更换证书
  8. Java:网络编程值TCP的使用
  9. 鸟哥的linux私房菜学习记录之计算机概论
  10. 新人补钙系列教程之:AS 与 JS 相互通信