2011-06-01 44 views
0

我不能在这个代码我无法理解下面的Verilog代码

input [15:0] offset ; 
output [31:0] pc; 
output [31:0] pc_plus_4; 
reg [31:0] pc; 
wire [31:0] pcinc ; 

assign pcinc = pc +4 ; 
assign pc_plus_4 = {pc[31],pcinc}; 

assign branch_aadr = {0,pcinc + {{13{offset[15]}},offset[15:0],2'b00}}; 
+2

你究竟在哪里陷入困境? – Marty 2011-06-01 12:38:24

回答

7

年底了解两行。如果你不熟悉的大括号{},它们是连接操作。您可以在IEEE Std for Verilog中阅读它们(例如,1800-2009,第11.4.12节)。

assign pc_plus_4 = {pc[31],pcinc}; 

该串接的pc的MSB与pcinc所有比特来组装pc_plus_4信号。但是,在这种情况下,因为pcincpc_plus_4都是32位宽,所以pc[31]被忽略。一个好的消息工具会告诉你RHS是33位,LHS是32位,最重要的位会丢失。该行可以更简单地编码为:

assign pc_plus_4 = pcinc; 

最后一行是我正在使用的一个模拟器的编译错误。您没有明确声明branch_aadr信号的宽度,并且0常量的宽度未指定。

5

最后一行还包含复制操作符,该操作符使用两组花括号。

{13{offset[15]}} 

这复制了位offset[15]十三次。看起来作者在offset上做了一个符号扩展,然后将其添加到pcinc。更好的方法可能是将offset声明为已签名。

//Three ways to replicate bits 
wire [3:0] repeated; 
wire  value; 

//These two assignments have the same effect 
assign repeated = {4{value}};     //Replication operator 
assign repeated = {value,value,value,value}; //Concatenation operator 

//These four taken together have the same effect as the above two 
assign repeated[3] = value; //Bit selects 
assign repeated[2] = value; 
assign repeated[1] = value; 
assign repeated[0] = value; 
+0

感谢您的回答!这是非常有帮助的 – Rojin 2011-06-03 06:32:12