【问题标题】:Verilog: Converted Integer Not Working for For loopVerilog:转换后的整数不适用于 For 循环
【发布时间】:2014-03-18 10:33:30
【问题描述】:

我正在用 Verilog 编写一个程序,它接受一个二进制值,将其转换为整数,然后打印“Hello, World!”那个次数。

但是,每当我运行程序时,都没有显示 Hello, World。代码如下:

// Internal Variable
wire [0:4] two_times_rotate;

// Multiply the rotate_imm by two (left shift)
assign {two_times_rotate} = rotate_imm << 1;
integer i, times_rotated;

// Convert two_times_rotate into an integer
always @( two_times_rotate )
begin
  times_rotated = two_times_rotate;
end

initial 
begin

for (i = 0; i < times_rotated; i = i + 1)
begin
  $display("Hello, World");
end

end

assign {out} = in;
 endmodule

我尝试使用以下 for 循环:

for (i = 0; i < 7; i = i + 1)

这个打印 Hello, World 七次,就像它应该的那样。

*更新*

到目前为止,这就是我的代码中的内容。我正在尝试做一个比较位的 for 。它仍然无法正常工作。如何以一种可以在 for 循环的比较中使用的方式将二进制完全转换为整数?

 module immShifter(input[0:31] in, input[0:3] rotate_imm, 
              output[0:31] out);

 // Internal Variable
 wire [0:4] two_times_rotate;
 reg [0:4] i;

 // Multiply the rotate_imm by two (left shift)
 assign {two_times_rotate} = rotate_imm << 1;
 integer times_rotated, for_var;

 // Convert two_times_rotate into an integer
 always @( two_times_rotate )
 begin
    assign{times_rotated} = two_times_rotate;
 end

 initial 
 begin

 assign{i} = 5'b00000;
 assign{for_var} = times_rotated;
 for (i = 5'b00000; i < times_rotated; i = i + 5'b00001)
 begin
   $display("Hello, World");
 end

 end

 assign {out} = in;
endmodule 

【问题讨论】:

  • 您的代码的前几行似乎丢失了
  • @jean28,我建议不要将assign 放在always 块或initial 块中。大多数设计者声明打包数组采用(小端)[en.wikipedia.org/wiki/Endianness] 格式(即[31:0])。

标签: for-loop binary verilog


【解决方案1】:

times_rotated32'bX 在评估 for 循环时的时间 0。 for 循环正在检查i &lt; 32'bX,这是错误的。


根据您的更新,这可能是您想要的:

module immShifter( input [31:0] in, input [3:0] rotate_imm, output [31:0] out );
  integer i, times_rotated;
  always @*
  begin
    times_rotated = rotate_imm << 1;
    for (i = 0; i < times_rotated; i++)
      $display("Hello, World");
    $display(""); // empty line as separate between changes to rotate_imm
  end
  assign out = in;
endmodule

【讨论】:

  • 那么如何把它变成一个整数,以便它检查正确的条件?
  • @jean28,您希望正确的条件是什么?此时一切都在时间 0 运行。一切都在 Verilog 中并行运行,因此执行顺序取决于模拟器的调度程序。
猜你喜欢
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
  • 2019-09-10
  • 1970-01-01
相关资源
最近更新 更多