2016-02-03 45 views
0

特定类型的位级操作包括设置或清零多位值中的一个位,给定其索引及其新值。该操作可以通过具有以下接口的BitSet电路以硬件实现:Verilog中的BitSet电路

  • 输入x是代表原始值的4位值。
  • 输出y是一个4位值,代表修改后的值,位操作后设为 。
  • 输入索引是一个2位值,范围从0到3,表示要修改的位的索引。
  • 输入值是一个1位的值,设置为0或1,表示位索引在输出y中应该使用的值。 y中的每个其他位应该与x中的对应位相匹配。

下面的代码我有我的例子都在类:

module BitSet(input [3:0]x, 
     input [1:0]index, 
     input value, 
     output [3:0]y); 
    [email protected](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 

我甚至不确定这是否会做我想要的,但任何人都可以帮助解决这些错误,或者如何修复程序来完成问题?

+0

http://electronics.stackexchange.com/questions/215186/verilog-bitset-circuit – toolic

+0

'总是@(X,指数,value);'< - 除去分号,你应该使用'always @ *',除非你需要遵循1995年的语法。 – Greg

回答

1

不,这不会做你想要的东西,即使语法错误是固定的。我会帮你的语法错误,并试图指出你在正确的道路上,以获得问题解决了(我想对,所以我们不应该彻底解决问题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. 
    [email protected](*) 
    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 
endmodule 
+0

很明显,这是一个重复的问题,在我回答之前我没有看到。在永远的块中做这件事没有任何问题,它不一定就是这样。 –

+0

@Cox除了索引位之外,输出“y”的其他位的值是多少? –