一种特定类型的位级操作是在多位值中设置或清除一位,给出它的索引和新值。该操作可以通过具有以下接口的BitSet电路在硬件中实现:
下面是我从课堂上的例子中学到的代码:
module BitSet(input [3:0]x,
input [1:0]index,
input value,
output [3:0]y);
always@(x,index,value);
begin
if (index = 2'b00)
y[0] = value;
if(index = 2'b01)
y[1]=value;
if(index = 2'b10)
y[2]=value;
if(index=2'b11)
y[3]=value;
end
endmodule这是测试台:
module BitSet_tb();
reg [3:0]x;
reg [1:0]index;
reg value;
wire [3:0]y;
BitSet uut(
.x(x),
.index(index),
.value(value),
.y(y)
);
initial begin
$monitor ("%d %b %b %b %b", $time, x, index, value, y);
x=4'b0000;
index=2'b00;
value=1'b0;
#10 x=4'b0001;
index=2'b01;
value=1'b0;
#10 x=4'b1111;
index=2'b10;
value=1'b0;
#10 x=4'b1111;
index=2'b11;
value=1'b0;
#10 $finish;
end
endmodule在编译时,我会得到以下错误:
bitset.v:10: syntax error
bitset.v:12: error: invalid module item.
bitset.v:13: syntax error
bitset.v:14: error: invalid module item.
bitset.v:15: syntax error
bitset.v:16: error: invalid module item.
bitset.v:17: syntax error
bitset.v:18: error: invalid module item.
bitset.v:19: syntax error我甚至不确定这是否能做我想做的事情,但是谁能帮我解决错误或者如何修复程序来做我想做的事情呢?
发布于 2016-02-04 05:24:25
不,即使语法错误是固定的,这也做不到你想做的事情。我将帮助您处理语法错误,并试图为您指出解决问题的正确途径(因此我们不应该直接解决HW问题!)
module BitSet(input [3:0] x,
input [1:0] index,
input value,
output reg [3:0] y); // y needs to be of type reg if you're
// going to use it in a procedural block
// (like an always block)
// The * infers the sensitivity list, no need to list out each signal.
// Also, you can't have a semicolon here.
always@(*)
begin
// Like in C, a single "=" is an assignment, while a compare is "==".
// You want to compare index to 2'b00, not assign it that value,
if (index == 2'b00)
y[0] = value;
if (index == 2'b01)
y[1] = value;
if (index == 2'b10)
y[2] = value;
if (index == 2'b11)
y[3] = value;
end
endmodule为了获得正确的功能..。你就快到了,其实可以用一个额外的行来完成。我的问题是:在您的代码中,y中的位发生了什么变化,这些位应该取x的直接值而不被更改?你的代码能准确地处理这个问题吗?如果没有,需要做些什么来处理这个问题?
这里有一个带有更新的SystemVerilog语法的版本:
module BitSet(input [3:0] x,
input [1:0] index,
input value,
output logic [3:0] y); // "logic" type can be used as a wire or reg
// Just like always @(*), but directly indicates that this
// is combinational logic. Tools will throw an error if you
// accidentally encode a latch (which you have done!)
always_comb
begin
if (index == 2'b00)
y[0] = value;
if (index == 2'b01)
y[1] = value;
if (index == 2'b10)
y[2] = value;
if (index == 2'b11)
y[3] = value;
end
endmodulehttps://stackoverflow.com/questions/35189267
复制相似问题