【问题标题】:BitSet Circuit in VerilogVerilog中的BitSet电路
【发布时间】:2016-02-03 22:46:49
【问题描述】:

一种特定类型的位级操作包括在给定索引和新值的情况下设置或清除多位值中的单个位。这个操作可以通过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

我什至不确定这是否能满足我的要求,但谁能帮助解决错误或如何修复程序以执行要求的操作?

【问题讨论】:

  • always@(x,index,value); always @*,除非你需要遵循1995年的语法。

标签: verilog system-verilog circuit-diagram iverilog


【解决方案1】:

不,即使语法错误已修复,这也不会满足您的要求。我将帮助您解决语法错误,并尝试为您指出解决问题的正确路径(我认为在 SO 上我们不应该彻底解决硬件问题!)

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

【讨论】:

  • 嗯,显然这是一个重复的问题,在我回复之前我没有看到。在 always 块中执行此操作没有任何问题,但不必再这样做了。
  • @Cox 除了索引位之外,y 的其他输出位的值是多少?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多