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.

which verilog code is better for counter?

Status
Not open for further replies.

digi001

Full Member level 5
Full Member level 5
Joined
Apr 5, 2011
Messages
244
Helped
22
Reputation
44
Reaction score
21
Trophy points
1,298
Activity points
2,904
here is 2 different textbooks show a counter. is one any better than the other, or do both end up being synthesized the same way?



Code:
	// Reset if needed, or increment if counting is enabled
always @ (posedge clk or posedge reset)
begin
	if (reset)
		count <= 0;
	else if (enable == 1'b1)
		count <= count + 1;
end

vs.

Code:
	// Reset if needed, or increment if counting is enabled
always @ (posedge clk or posedge reset)
begin
	if (reset)
		r_reg <= 0;
	else if (enable == 1'b1)
		r_reg <= r_next;
end

assign r_next = r_reg + 1;
 

I would expect that they would both synth the same way if you've got a good synth tool. But if they do synth differently I would expect the second version to generate some additional logic or possibly not be able to optimize as much. The first option is the most straight forward approach and how I always create a counter. The r_next in the second version is a redundant assignment - there is no need to have it, it would be more clear just to say r_reg <= r_reg + 1; which would then be equivalent to the first option. :)
 
  • Like
Reactions: digi001

    digi001

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top