简体   繁体   English

如何在多个时钟周期内执行for循环?

[英]How to execute a for loop over multiple clock cycles?

The for loop is working properly , but everything is happening in a single clock cycle. for循环工作正常,但一切都在一个时钟周期内发生。 How I make it run a single iteration per cycle? 我如何让它在每个周期运行一次迭代?

   `timescale 1ns/10ps
    module multiplier (clock,multiplier,multiplicand,start,done,product);

    input   [7:0]   multiplier ,multiplicand;
    input           start;
    input           clock;
    output  [15:0]  product;
    output          done;

    reg     [15:0]  multiplierF ,multiplicandF;
    reg     [15:0]  productF;
    reg             doneF;

    integer i;

    assign product  =   productF;
    assign done     =   doneF;

    task rpa_16;

    input   [15:0]      multiplierF;
    inout   [15:0]      productF;

    assign productF = multiplierF + productF;

    endtask

    always @ (posedge clock or posedge start)
    begin
    if(start)
        begin
            multiplierF     =   0 ;
            multiplicandF   =   0 ;
            productF        =   0 ;
            doneF           =   0 ;
        end
    else
        begin
            multiplierF     =   {8'b0,multiplier};
            multiplicandF    =   {8'b0,multiplicand};  
        end        
    end


    always @(posedge clk)
    begin
            if(!doneF)
                begin
                    for (i=0;i<7;i=i+1)
                        begin
                            if(multiplicand[i])
                                begin
                                rpa_16(multiplierF,productF); 
                                multiplierF =  multiplierF << 1;
                                productF     = productF;                       
                                end
                            else
                                begin
                                multiplierF = multiplierF << 1;
                                end
                        end
                    doneF = 1;
                end
    end

I cant paste a picture of the waveform , but I want i to increment after each positive edge . 我不能粘贴波形的图片,但我希望我在每个正边缘后增加。 But whats happening is , for loop executes in a single clock cycle and I get the output. 但是发生的事情是,for循环在一个时钟周期内执行,我得到了输出。

For loops do not imply anything sequential in verilog. for循环并不意味着verilog中的任何顺序。 If you want a loop that takes 8 clock cycles, then you'll have to rewrite it with an explicit counter variable, perhaps something like this: 如果你想要一个需要8个时钟周期的循环,那么你将不得不用一个显式的计数器变量重写它,可能是这样的:

always @(posedge clk or negedge reset_)
if(!reset_) begin
   multiplierF <= 0;
   loopcount   <= 0;
   doneF       <= 0;
end else begin
  if(!doneF) begin
     loopcount <= loopcount + 1;
     if(multiplicand[loopcount]) begin
       rpa_16(multiplierF,productF); 
       multiplierF =  multiplierF << 1;
       productF    = productF;                       
     end else begin
       multiplierF = multiplierF << 1;
     end
  end
  if(loopcount == 7) doneF <= 1;
end

Also, you should not be assigning variables like multiplierF in multiple always blocks, you will get non-deterministic behavior and will likely fail to synthesize. 此外,您不应该在多个always块中分配multiplierF变量,您将获得非确定性行为并且可能无法合成。 On posedge clk both blocks will execute and you cannot know which one will execute last, so it may give different results on different simulators. 在posedge clk上,两个块都将执行,你不知道哪个块最后会执行,所以它可能会在不同的模拟器上给出不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM