English 中文(简体)
How to NOT use while() loops in verilog (for synthesis)?
原标题:

I ve gotten in the habit of developing a lot testbenches and use for() and while() loops for testing purpose. Thats fine. The problem is that I ve taken this habit over to coding for circuits which should be synthesizable. XST and others refuse to synthesize code (without additional modification to synthesis parameters) such as:

while (num < test_number) 
     begin 
     . 
     . 
     . 
     num = num+1; 
     end

This is bad coding style because to the synthesizer test_num is an int with value 2^32! or it sees it as unbounded parameter. Either way, its a bad coding habit. But I m so used to doing this in C and testbenches. What would be the equivalent synthesizable of code of the above code segment?

Thanks!

问题回答

Synthesis tools vary but generally a loop can be synthesized so long as the number of iterations is known a to the synthesis tool. So,

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

is OK because the tool knows there are 10 loop iterations. But

reg [10:0] r;
for ( i = 0; i < r; i = i + 1 )

is not OK because r is a variable r s value is unknown at synthesis time.

Think of loops in RTL code as creating a known fixed number of copies of a piece of logic.

You need to have a clock to control it to start.

always @(posedge clk or negedge rst_n)
  if (!rst_n)
     num <= 32 b0; // or whatever your width is.
  else
     if (num < test_number)
       num <= num + 1 b1;

If your synthesis tool does not support while or for loops, then don t use a loop. Just expand your code out.

wire [1:0] addr;
reg  [3:0] wren;

always @(posedge clk) begin
    wren[0] <= (addr == 2 d0);
    wren[1] <= (addr == 2 d1);
    wren[2] <= (addr == 2 d2);
    wren[3] <= (addr == 2 d3);
end

I am unfamiliar with XST, but some synthesis tools do support loops (Synopsys, for example).





相关问题
as2 simple for loop not populating textbox

I have got an xml file that brings text into a flash movie in the form of an array, I need to population some textboxes and want to do this using a for loop. My loop look like this: for(var i=...

Undirected (Multi) Graph (Hierholzers Algorithm)

I am very stuck with the looping structure for my graph to work out Eulers tour. This is the graph I am drawing, remember it s undirected, so a journey from N1 to N4 is the same as a journey from N4 ...

Smarty Section Making the selected Link Bold

How can I make the selected link from the List below, bold, that you see you are on Page ex. 3 or 5 With this code, If you click on a Link you don t know on which page you are. The smarty code ...

Back button loop with IFRAMES

In my (school) website we use Iframes to display class blogs (on blogger). This works well EXCEPT if the user then clicks on (say) a photo inside the iframe. Blogger (in this case) then displays the ...

Handling multiple windows WIN32 API

HI I m trying to create an application in the Win32 environment containing more than one window. How do i do that? all the Win32 Tutorials on web i found only showed how to manage one window. How do i ...

Saving output of a for-loop to file

I have opened a file with blast results and printed out the hits in fasta format to the screen. The code looks like this: result_handle = open("/Users/jonbra/Desktop/my_blast.xml") from Bio.Blast ...

热门标签