加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

verilog中有符号数运算的注意事项

(2014-03-10 21:49:50)
标签:

fpga

it

分类: FPGA
从verilog2001开始对于有符号数的运算变得简单了,只需要在定义的时候加上关键字signed。运算结果输出负数以补码的形式给出,比如9位二进制-13,则以补码1 1111 0011表示。
module add(input clk,
           input reset,
   input  signed [7:0]  a,
   input  signed [7:0] b,
   output  signed [15:0] out,
   output reg  flg
          );
reg signed [15:0] out_buf;
always @(posedge clk , negedge reset)
if(!reset)
out_buf <= 9'd0;
else
out_buf <= a * b;


always @(out_buf)
begin
  if(out_buf >= 0) flg <= 1;
else flg <= 0;
end
assign out = out_buf; 
endmodule

如果去掉signed关键字
module add(input clk,
           input reset,
   input   [7:0]  a,
   input   [7:0] b,
   output   [15:0] out,
   output reg  flg
          );
reg  [15:0] out_buf;
always @(posedge clk , negedge reset)
if(!reset)
out_buf <= 9'd0;
else
out_buf <= a * b;


always @(out_buf)
begin
  if(out_buf >= 0) flg <= 1;
else flg <= 0;
end
assign out = out_buf; 
endmodule
仿真结果是
http://s10/mw690/003pe0FOzy6HcOJakRjf9&690
对比发现,负数的部分运算混乱,原因是verilog默认数是无符号的。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有