首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Verilog中的BitSet电路

Verilog中的BitSet电路
EN

Stack Overflow用户
提问于 2016-02-03 22:46:49
回答 1查看 1.1K关注 0票数 0

一种特定类型的位级操作是在多位值中设置或清除一位,给出它的索引和新值。该操作可以通过具有以下接口的BitSet电路在硬件中实现:

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

下面是我从课堂上的例子中学到的代码:

代码语言:javascript
复制
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

这是测试台:

代码语言:javascript
复制
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

在编译时,我会得到以下错误:

代码语言:javascript
复制
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

我甚至不确定这是否能做我想做的事情,但是谁能帮我解决错误或者如何修复程序来做我想做的事情呢?

EN

回答 1

Stack Overflow用户

发布于 2016-02-04 05:24:25

不,即使语法错误是固定的,这也做不到你想做的事情。我将帮助您处理语法错误,并试图为您指出解决问题的正确途径(因此我们不应该直接解决HW问题!)

代码语言:javascript
复制
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语法的版本:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35189267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档