Verilog中的BitSet电路

时间:2016-02-03 22:46:50

标签: verilog system-verilog circuit-diagram iverilog

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

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

这是我从课堂上的例子中得到的代码:

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

我甚至不确定这是否符合我的要求,但任何人都可以帮助解决错误或如何修复程序以执行所要求的操作?

1 个答案:

答案 0 :(得分:1)

不,即使语法错误得到修复,这也无法达到您想要的效果。我会帮助你解决语法错误并尝试指出正确的方法来解决问题(我想我们不应该直接解决硬件问题!)

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
endmodule