Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / [signed][input]Verilog的有符号数输入测试

git://github.com/adream307/signedTest.git一直错误得以为Verilog中的数据是无符号的。测试脚本,在QuartusII中成功编译,且下载在硬件上运行。
  1. //SIG.v 
  2. module SIG( 
  3.     iCLK, 
  4.     oSY, 
  5.     oUY 
  6. ); 
  7.  
  8. input iCLK; 
  9. output [7:0] oSY; 
  10. output [7:0] oUY; 
  11.  
  12. reg [7:0] x; 
  13. wire [3:0] x1 = x[3:0]; 
  14. wire [3:0] x2 = x[7:4]; 
  15. wire [7:0] sy; 
  16. wire [7:0] uy; 
  17.  
  18. assign oSY = sy; 
  19. assign oUY = uy; 
  20.  
  21. always@(negedge iCLK) begin 
  22.     x<=x+8"d1; 
  23. end 
  24.  
  25. SIGNED SIG_1( 
  26.     .iX1(x1), 
  27.     .iX2(x2), 
  28.     .oY(sy) 
  29. ); 
  30.  
  31. UNSIGNED USIG_1( 
  32.     .iX1(x1), 
  33.     .iX2(x2), 
  34.     .oY(uy) 
  35. ); 
  36.  
  37. endmodule 
//SIG.vmodule SIG(iCLK,oSY,oUY);input iCLK;output [7:0] oSY;output [7:0] oUY;reg [7:0] x;wire [3:0] x1 = x[3:0];wire [3:0] x2 = x[7:4];wire [7:0] sy;wire [7:0] uy;assign oSY = sy;assign oUY = uy;always@(negedge iCLK) beginx<=x+8"d1;endSIGNED SIG_1(.iX1(x1),.iX2(x2),.oY(sy));UNSIGNED USIG_1(.iX1(x1),.iX2(x2),.oY(uy));endmodule 
  1. //SIGNED.v 
  2. module SIGNED( 
  3.     iX1, 
  4.     iX2, 
  5.     oY 
  6. ); 
  7.  
  8. input signed [3:0] iX1; 
  9. input signed [3:0] iX2; 
  10. output signed [7:0] oY; 
  11.  
  12. assign oY = iX1*iX2; 
  13.  
  14. endmodule 
//SIGNED.vmodule SIGNED(iX1,iX2,oY);input signed [3:0] iX1;input signed [3:0] iX2;output signed [7:0] oY;assign oY = iX1*iX2;endmodule   
  1. //UNSIGNED.v 
  2. module UNSIGNED( 
  3.     iX1, 
  4.     iX2, 
  5.     oY 
  6. ); 
  7.  
  8. input [3:0] iX1; 
  9. input [3:0] iX2; 
  10. output [7:0] oY; 
  11.  
  12. assign oY = iX1*iX2; 
  13.  
  14. endmodule 
//UNSIGNED.vmodule UNSIGNED(iX1,iX2,oY);input [3:0] iX1;input [3:0] iX2;output [7:0] oY;assign oY = iX1*iX2;endmodule
 上图为SignalTapII的运行截图,可以发现当x=0xFF时,此时x1=0xF,x2=0xF。对于SIGNED,有符号运算,x1=-1,x2=-1,所以结果为1。而对于UNSIGNED,无符号运算,x1=15,x2=15,所以结果为225,即0xE1。