Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help verilog hdl program

Status
Not open for further replies.

hareshcooleng

Member level 1
Joined
Aug 6, 2010
Messages
36
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
ahmedabad
Activity points
1,480
error-For statement is only supported when the stop test condition is a comparison between the loop variable and a constant.
any one know how i solve that.


module top(n, result);
input [3:0] n;
output [31:0] result;
reg [31:0] result;
always @(n)
begin
result=factorial(n);
end


function [31:0] factorial;
input [3:0] num;
reg [3:0]index;
begin
factorial=num ?1:0;
for(index=2; index<=num; index=index+1 )
factorial=index * factorial;
end
endfunction
endmodule
 

There are two issues with your code. The first one is a formal syntax problem, that can be easily overcome by rewriting the loop:
Code:
for(index=2; index<=15; index=index+1) // index > 12 causes result overflow
  if (index<=num)
    factorial=index * factorial;
The second and more basic problem reveals, if you try to synthesize the suggested code. I guess, that you didn't think yet about the nature of a for loop iteration in Verilog. It's not the same as a loop in a sequential programming language, it's a direction to build parallel logic. Unfortunately the parallelization can easily break up all reasonable resource limits. You should limit index to 12 to keep the 32 bit result range.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top