在一个始终@(posedge clk)块内,有一个嵌套的for循环,如何输出每个i和j的值?

huangapple go评论88阅读模式
英文:

Inside an always@(posedge clk) block, having a nested for loop, how do we output for each and every values of i and j?

问题

To achieve the desired output, you can add a condition to check for specific values of i and j before displaying the result. Here's the modified code:

  1. integer i, j;
  2. reg [15:0] Y_Planar_Prediction_reg; // Register to store Y_Planar_Prediction
  3. always @(posedge clk) begin
  4. for (j = 0; j < 16; j = j + 1) begin
  5. for (i = 0; i < 16; i = i + 1) begin
  6. Y_pred_h = (16 - 1 - i) * Y_ref_buffer_left[j] + (i + 1) * Y_ref_buffer_top_right;
  7. Y_pred_v = (16 - 1 - j) * Y_ref_buffer_top[i] + (j + 1) * Y_ref_buffer_left_bottom;
  8. Y_Planar_Prediction = (Y_pred_h + Y_pred_v + 16) >> (4 + 1);
  9. // Check for specific i and j values
  10. if (i == 0 && j == 0) begin
  11. Y_Planar_Prediction_reg <= Y_Planar_Prediction;
  12. $display("@%dns Y_Planar_Prediction for i=0, j=0: %d", $time, Y_Planar_Prediction_reg);
  13. end
  14. else if (i == 1 && j == 0) begin
  15. Y_Planar_Prediction_reg <= Y_Planar_Prediction;
  16. $display("@%dns Y_Planar_Prediction for i=1, j=0: %d", $time, Y_Planar_Prediction_reg);
  17. end
  18. else if (i == 2 && j == 0) begin
  19. Y_Planar_Prediction_reg <= Y_Planar_Prediction;
  20. $display("@%dns Y_Planar_Prediction for i=2, j=0: %d", $time, Y_Planar_Prediction_reg);
  21. end
  22. // Add more conditions for other values of i and j as needed
  23. end
  24. end
  25. end

This modified code checks for specific values of i and j and displays the Y_Planar_Prediction result with the corresponding time and values of i and j. You can extend this pattern for other values of i and j as required.

英文:

Code

  1. integer i,j;
  2. //Luma
  3. always @(posedge clk)
  4. begin
  5. for (j=0; j&lt;16; j=j+1)
  6. begin
  7. for (i=0; i&lt;16; i=i+1)
  8. begin
  9. Y_pred_h = (16-1-i) * Y_ref_buffer_left[j] + (i+1) * Y_ref_buffer_top_right;
  10. Y_pred_v = (16-1-j) * Y_ref_buffer_top[i] + (j+1) * Y_ref_buffer_left_bottom;
  11. Y_Planar_Prediction = (Y_pred_h + Y_pred_v + 16 ) &gt;&gt; (4+1);
  12. end
  13. end
  14. end

Here Y_Planar_Prediction is my module output, and I need to display its result on screen for each and every value of i and j in every posedge of clk (i.e for i=0,j=0 at time=15; i=1,j=0 at time=25; i=2,j=0 at time=35;.....; i=5,j=10 at time=something; ..........; i=15,j=15 at time=something)

But, when I simulate observing the output for only i=15,j=15 (i.e 158) in every posedge of clk.

Attached screenshot for reference:

在一个始终@(posedge clk)块内,有一个嵌套的for循环,如何输出每个i和j的值?

How can i modify the code to get:

  1. @15ns Y_Planar_Prediction for i=0,j=0;
  2. @25ns Y_Planar_Prediction for i=1,j=0;
  3. @35ns Y_Planar_Prediction for i=2,j=0;
  4. and so on...

答案1

得分: 1

Verilog中的for循环只是一种编写简洁代码的便捷方式。ij变量只是循环索引,其值在一个模拟时间步骤内更改。模拟的行为符合预期。这不是for循环的正确用法。

看起来你确实希望ij推断计数器的逻辑,而不是使用for循环。在这种情况下,你需要添加新的always块。例如:

  1. reg [3:0] icnt;
  2. always @(posedge clk) begin
  3. if (icnt_en) icnt <= icnt + 1;
  4. end

这样,icnt将在每个时钟周期上具有新的值。

英文:

for loops in Verilog are merely a convenient way to write concise code. The i and j variables are just loop indexes whose values change in a single simulation time step. The simulation is behaving as expected. This is not a proper use of for loops.

It seems like you really want i and j to infer logic of counters instead of using for loops. In that case you would need to add new always blocks. For example:

  1. reg [3:0] icnt;
  2. always @(posedge clk) begin
  3. if (icnt_en) icnt &lt;= icnt + 1;
  4. end

In this way, icnt will have a new value on each clock cycle.

huangapple
  • 本文由 发表于 2023年6月1日 00:21:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定