我正在尝试理解基于组合逻辑的电线分配的基本原理。
我的问题如下:
wire val;
wire x;
wire a;
wire b;
always @*
begin
if(val == 00)
//I want to assign x = a
if(val == 01)
//I want to assign x = b
end
其中a
和b
是值为-的电线,而x
是进入寄存器的电线。
如果您能指导我需要更改什么,我将不胜感激。
wire
只能通过assign
语句赋值,不能与if语句一起使用。
如果将x
更改为reg
类型,则可以在always块中对其进行赋值。
这样综合后的结果完全相同,一个常见误解是reg
类型变量意味着寄存器,但它实际上只是改变了赋值方式。
另外,如果您希望保持wire类型,可以使用三元运算符?:
来编写assign语句:
assign x = (val==0) ? a :
(val==1) ? b :
'bx ;
首先要问的是:你是想将这些导线用作输入,还是用作连接? 其次:你需要可综合代码吗? 在always块内不能分配导线,必须使用reg。 因此,我的建议是:
//**************************************************************************************
module(a, b, out); //You have to define an interface, and all Verilog modules starts with module
input val[1:0]; //you have to use [] in order to indicate the length. In this case 2 bits, since you want to ask for 00;
input a;
input b;
output out;
reg x;
always @* //NOTE: You are describing combo logic, since there is no clock signal
begin
if(val == 2'b00)
x = a;
else if(val == 2'b01)
x = b;
else
x = 0; //If you are describing combo logic, you have to cover ALL val cases, in order to evade latches
end
assign out = x; //This is how you assign values in Verilog
endmodule
module myfirstmodule (
input a,
input b,
input [1:0] val,
output x
);
// Assign a or b to x based upon val
assign x = (val == 2'b00) ? a :
(val == 2'b01) ? b :
0; // Since val can take on values 00,01,10, and 11, we define all possible cases
endmodule
赋值语句类似于您在流行编程语言(如C或C ++)中使用的if语句,它作为条件块使用。
赋值运算符的工作方式如下: