如何在Verilog中合成While循环?

时间:2013-09-27 18:03:23

标签: verilog system-verilog

我试图设计一个Booth乘数,它在所有编译器中运行良好,包括:

Modelsim,Verilogger Extreame,Aldec Active Hdl& Xilinx的Isim。 .....

我知道模拟和合成是两个不同的过程,只有少数具有各种限制的 Verilog 构造用于合成。但我不知道我的程序中While loop发生了什么, Synopsys Synplify 9.6 以及 Xilinx ise 14.2

当我尝试合成 Synopsys时"loop iteration limit 2000 exceeded" Xilinx'XST 说“This Xilinx application has run out of memory or has encountered a memory conflict"

我已在下面附上我的代码。         我还写了这个< -------“由于这个while循环而产生的错误”,其中Synthesizer因 while循环而产生错误.....

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////Author/Coder-Shrikant Vaishnav///////////////////////////////////////////////
/////////////////////////////////////////Design-Booth Algorithm Demonstration////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


module booth_synt(input wire [4:0]a,input wire [4:0]b,output reg signed[9:0] g);
reg signed[10:0]c;// One extra bit for sign bit.....I mean 11th bit.
//We used Signed Reg bcoz ASR fill vacant bit with MSB and then shift other for unsigned reg they fill it with zeros and then shift.. 
reg[4:0]d;
reg [4:0]e;
reg [2:0]count1; 
reg [2:0]count2; 
reg [2:0]count3; 
reg [2:0]count4; 

//Always start whenever any changes happens

always@(a,b)
 begin :close



//If's for sign bit check...
//Then 2's Complement...

count1=3'b000; //Initialize Counter
count2=3'b000;
count3=3'b000;
count4=3'b000;

//For negative
if(a[4]==1'b1) //Internal checking
  begin
  if(a==5'b10000)
    begin
     g[9:0]=10'b0000000000;
    end
  else
  begin
  d=~{1'b0,a[3],a[2],a[1],a[0]};  //we place 1'b0 because its inversion is 1   
  d=d+5'b00001;  //2's Complement we use additional register d for data holding.....bcoz wire not hold data
     if(d[4]==1'b0)//This "if" is used bcoz if due to calculation if accidently d[5]==1'b0 then this changs sign bit and thus ans
    begin
    d[4]=1'b1;
    end
    c[5:1]=d;
  end
  end

 if(b[4]==1'b1)
  begin 

   if(b==5'b10000)
    begin
     g[9:0]=10'b0000000000;
     disable close;
    end
  else
   begin
   e=~{1'b0,b[3],b[2],b[1],b[0]}; //we place 1'b0 because its inversion is 1
   e=e+5'b00001;
   if(e[4]==1'b0)//This "if" is used bcoz if due to calculation if accidently e[4]==1'b0 then this changs sign bit and thus ans
    begin
   e[4]=1'b1;
    end
   end
  end


//For positive 
if(b[4]==1'b0)
begin
e[4:0]=b[4:0];

end

if(a[4]==1'b0)
begin

c[1]=a[0];
c[2]=a[1]; //"a" is multiplier while "b" is multiplicand...
c[3]=a[2];
c[4]=a[3];
c[5]=a[4];

end



//Initialization of Output ........
c[0]=1'b0;

//All MSB's are Initially set to Zeros
c[6]=1'b0;
c[7]=1'b0;
c[8]=1'b0;
c[9]=1'b0;
c[10]=1'b0;



//Four Different Conditions Checking......
case({c[1],c[0]})

2'b00:begin   //Case 1

       while(count1<3'b101)  **<-------"Error Generated Here due to this while loop"**
      begin


         if({c[1],c[0]}==2'b10) //cond1 for 10
            begin
             c[10:6]=(c[10:6]-e[4:0]);
              c=c>>>1;
              count1=count1+1'b1;


            if(count1==3'b101)// Counter value check
             begin
               if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

                 end
            end //end if==4
          end
          if(({c[1],c[0]}==2'b00) || ({c[1],c[0]}==2'b11))      //cond 2 in it we describe both 00 and11.........Arithemetic Right Shift operation
             begin
              c=c>>>1;     
              count1=count1+1'b1;                    


             if(count1==3'b101) // Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

                 end

              end
              end   

           if({c[1],c[0]}==2'b01) //cond3 for 01
            begin
             c[10:6]=(c[10:6]+e[4:0]);
              c=c>>>1;
              count1=count1+1'b1;


             if(count1==3'b101) // Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

             end
              end
              end
     end  

   end//while's end


 //Case2
 2'b11:begin
      while(count2<3'b101) **<-------"Error Generated Here due to this while loop"**
      begin



         if({c[1],c[0]}==2'b10) //cond1 for 10
            begin
             c[10:6]=(c[10:6]-e[4:0]);
              c=c>>>1;
              count2=count2+1'b1;


             if(count2==3'b101) // Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

              end
              end
              end

          if(({c[1],c[0]}==2'b00)||({c[1],c[0]}==2'b11))//cond 2 in it we describe both 00 and11.........Arithemetic Right Shift operation
             begin
              c=c>>>1;     
              count2=count2+1'b1;                    


             if(count2==3'b101)// Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity  
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                g[9:0]=c[10:1];

             end
              end
              end   

           if({c[1],c[0]}==2'b01) //cond3 for 01
            begin
            c[10:6]=(c[10:6]+e[4:0]);
              c=c>>>1;
              count2=count2+1'b1;


             if(count2==3'b101)// Counter value check
               begin
                 if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity 
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

              end
              end
             end
           end 
          end //while's end



  //Case 3
  2'b10:begin
     while(count3<3'b101) **<-------"Error Generated Here due to this while loop"**
      begin

     if({c[1],c[0]}==2'b10) //Cond1 for 10
            begin
             c[10:6]=(c[10:6]-e[4:0]);

              c=c>>>1;
              count3=count3+1'b1;


             if(count3==3'b101)// Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity 
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

              end
              end
              end

          if(({c[1],c[0]}==2'b00)||({c[1],c[0]}==2'b11))//cond 2 in it we describe both 00 and11.........Arithemetic Right Shift operation
             begin
              c=c>>>1;     

              count3=count3+1'b1;                    


             if(count3==3'b101)// Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity 
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

               end
              end
              end   

           if({c[1],c[0]}==2'b01) //cond3 for 01
            begin
             c[10:6]=(c[10:6]+e[4:0]); 

              c=c>>>1;
              count3=count3+1'd1;

             if(count3==3'b101)// Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity 
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

                 end

               end
             end
           end 
          end //while's end



  //Case 4
  2'b01:begin
         while(count4<3'b101) **<-------"Error Generated Here due to this while loop"**
          begin

         if({c[1],c[0]}==2'b10) //cond1 for 10
            begin
             c[10:6]=(c[10:6]-e[4:0]);
              c=c>>>1;
              count4=count4+1'b1;


             if(count4==3'b101)// Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity 
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

              end
              end
              end

          if(({c[1],c[0]}==2'b00)||({c[1],c[0]}==2'b11))//cond 2 in it we describe both 00 and11.........Arithemetic Right Shift operation
             begin
              c=c>>>1;     
              count4=count4+1'b1;                    


             if(count4==3'b101)// Counter value check
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity 
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

              end
              end
              end   

           if({c[1],c[0]}==2'b01) //cond3 for 01
            begin
             c[10:6]=(c[10:6]+e[4:0]);
              c=c>>>1;
              count4<=count4+1'b1;


             if(count4==3'b101)
               begin
                if(c[10]==1)
                 begin
                 c=~{1'b0,c[9],c[8],c[7],c[6],c[5],c[4],c[3],c[2],c[1],c[0]};//we place 1'b0 because its inversion is 1
                 c=c+10'b0000000010;
                 c[10]=1'b1; //Again giving 1 for surity 
                 g[9:0]=c[10:1];

                 end

                 if(c[10]==0)
                 begin
                 c[10]=1'b0;
                 g[9:0]=c[10:1];

                  end
                end
               end
              end  //while's end

          end//01's end  

   endcase //case end


end       //always end
endmodule

3 个答案:

答案 0 :(得分:1)

这是编写得很糟糕的代码。你把它写成计算机程序。 Verilog是一种硬件描述语言 - 不是编程语言。在你的情况下,合成器试图在case语句中的while循环内复制逻辑。

  • 在将纸张翻译成HDL之前在一张纸上设计硬件
  • 在编码之前识别设计中的组合和顺序逻辑。
  • 想想合成器使用什么逻辑来实现你所写的逻辑。

答案 1 :(得分:0)

以下是一些提示:

  • 取下闩锁:
    • c[5:1]是一个锁存器,因为它未在if(a==5'b10000)分支中初始化。
    • {li> d未在if(a==5'b10000)中定义,可能是潜在的锁存器,具体取决于合成器优化功能。
    • e未在if(b==5'b10000)中定义,可能是d之类的潜在锁定。
    • 在always块的开头,确保所有组合逻辑都有初始值。
  • 尽量避免使用disable来合成代码。请改用if-else语句。
    • 注意:这是一条指导原则。
  • 有很多冗余且组织不良的代码:
    • case语句中的所有分支都是相同的。应该合并清理。
    • while循环中的代码更适合在循环外部(例如if(count*==3'b101)块)
  • 如果while循环出现问题,请尝试for循环。

答案 2 :(得分:0)

While循环往往暗示某些动态,比如检查一个条件。这不是用于合成的verilog的良好用途。可以静态展开的For循环更常用于缩短编写的代码。

如果你需要一些更动态的东西,应该写一个专用的状态机。

回答评论中提出的一些问题:

组合逻辑使用assign或包含在always @*中,这是连续评估的,并且所有并行运行都认为是AND,OR,NOR门。

顺序逻辑将包含在always @( posedge clk )中,这将在时钟的每个正边沿执行。其中使用的寄存器或存储器元件通常表示触发器。