在 Verilog 中将整数分配给 reg

发布于 2024-09-27 23:28:54 字数 311 浏览 5 评论 0原文

我对这个 Verilog 代码有疑问。基本上,它不会让我执行 Y = 3'di 语句。基本上,我希望 Y 等于 i。我很确定问题出在 i 上。那么,有没有办法在 Verilog 中做到这一点?此外,W 是 8 位的输入(换句话说,W[7:0])。

for (i = 7; i >= 0; i = i - 1)
begin
    if(W[i]) Y=3'di;
end

谢谢。

I have problems with this Verilog code. Basically, it won't let me do the Y = 3'di statement. Basically, I want Y to equal i. I am pretty sure the problem is the i. So, is there a way to do this in Verilog? Also, W is an input with 8 bits (in other words, W[7:0]).

for (i = 7; i >= 0; i = i - 1)
begin
    if(W[i]) Y=3'di;
end

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哭泣的笑容 2024-10-04 23:28:54

您可以使用括号选择位。

for (i = 7; i >= 0; i = i - 1)
begin
    if(W[i]) Y = i[2:0];
end

但如果 i 被声明为整数,则甚至没有必要。无论多少位都会自动放入 Y 中,而您只需要 LSB。

You can select bits using brackets .

for (i = 7; i >= 0; i = i - 1)
begin
    if(W[i]) Y = i[2:0];
end

But it isn't even necessary if i was declared to be an integer. It will take however many bits fit in Y automatically and you only wanted the LSBs.

分開簡單 2024-10-04 23:28:54

您可能希望在此处使用 case 语句:

case (1'b1)
  W[0]: Y=3'd0;
  W[1]: Y=3'd1;
  W[2]: Y=3'd2;
  W[3]: Y=3'd3;
  W[4]: Y=3'd4;
  W[5]: Y=3'd5;
  W[6]: Y=3'd6;
  W[7]: Y=3'd7;
  default: Y=3'd0; // to avoid inferring a latch when W==8'd0
endcase

这使得优先级对于代码的读者来说更加明显。

You might wish to use a case statement here:

case (1'b1)
  W[0]: Y=3'd0;
  W[1]: Y=3'd1;
  W[2]: Y=3'd2;
  W[3]: Y=3'd3;
  W[4]: Y=3'd4;
  W[5]: Y=3'd5;
  W[6]: Y=3'd6;
  W[7]: Y=3'd7;
  default: Y=3'd0; // to avoid inferring a latch when W==8'd0
endcase

This makes the priority more obvious to readers of your code.

憧憬巴黎街头的黎明 2024-10-04 23:28:54

我发现最好使用状态机来执行“for-loop”例程。
像这样的事情:

module yourthing(clk, W, i, Y)
input clk;
input [7:0] W;
output [2:0] Y;
reg [2:0] i;  

always@(posedge clk) begin  
  if(reset) begin  
   i = 3'd7;
   Y = 3'd0;
  end

  else begin
    case(i)
      3'd7 : begin
               if(W[i]) Y = i;
               i = 3'd6;
             end
      3'd6 : begin
               if(W[i]) Y = i;
               i = 3'd5;
             end
      3'd5 : begin
               if(W[i]) Y = i;
               i = 3'd4;
             end
      3'd4 : begin
               if(W[i]) Y = i;
               i = 3'd3;
             end
      3'd3 : begin
               if(W[i]) Y = i;
               i = 3'd2;
             end
      3'd2 : begin
               if(W[i]) Y = i;
               i = 3'd1;
             end
      3'd1 : begin
               if(W[i]) Y = i;
               i = 3'd0;
             end
      3'd0 : begin
               if(W[i]) Y = i;
               i = 3'd7;
             end
    endcase
  end
endmodule

希望这有帮助......

I have found that its better to use state machines to do "for-loop" routine.
Something like this:

module yourthing(clk, W, i, Y)
input clk;
input [7:0] W;
output [2:0] Y;
reg [2:0] i;  

always@(posedge clk) begin  
  if(reset) begin  
   i = 3'd7;
   Y = 3'd0;
  end

  else begin
    case(i)
      3'd7 : begin
               if(W[i]) Y = i;
               i = 3'd6;
             end
      3'd6 : begin
               if(W[i]) Y = i;
               i = 3'd5;
             end
      3'd5 : begin
               if(W[i]) Y = i;
               i = 3'd4;
             end
      3'd4 : begin
               if(W[i]) Y = i;
               i = 3'd3;
             end
      3'd3 : begin
               if(W[i]) Y = i;
               i = 3'd2;
             end
      3'd2 : begin
               if(W[i]) Y = i;
               i = 3'd1;
             end
      3'd1 : begin
               if(W[i]) Y = i;
               i = 3'd0;
             end
      3'd0 : begin
               if(W[i]) Y = i;
               i = 3'd7;
             end
    endcase
  end
endmodule

Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文