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.

One Simple Verilog Design Question

Status
Not open for further replies.

javatea

Newbie level 4
Joined
Dec 6, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,326
Hi Folks,
I will like to design a counter (from 0~4)
and only for odd state (counter=1,3), output = 1. otherwise =0
This module gets two input clk and rst

I wrote two different code for it.
Which one is better? Thank you so much


counter 01234 01234
output 01010 01010

////////////////////////////////////////////////////
// Code Style 1
////////////////////////////////////////////////////

module counter(input rst, clk, output reg out);

reg [2:0] tmp; // fixed

always@(posedge clk) begin
if (!rst) begin
out<=0;
tmp<=0;
end
else if (tmp==2'd4) begin
tmp<=0;
out<=0;
end
else if (tmp==2'd1 || tmp==2'd3) begin
tmp<=tmp+1;
out<=1;
end
else begin
tmp<=tmp+1'b1;
out<=0;
end
endmodule


////////////////////////////
// Code Style two
////////////////////////////

module counter(input rst, clk, output reg out);

reg [2:0]tmp; // fixed

always@(posedge clk) begin
if (!rst) begin
tmp<=0;
end
else if(tmp==4)
tmp<=0;
else
tmp<=tmp+1;
end

always@(tmp)
if (tmp==1 || tmp==3)
out=1;
else
out=0;
end

endmodule
 

Both suffer from the fact, that tmp can't never reach a value of four. tmp needs three bits. Apart from this problem,
with code style 2, the output can have glitches. This may be a problem depending on the usage of signal "out".
 
  • Like
Reactions: sam33r

    javatea

    Points: 2
    Helpful Answer Positive Rating

    sam33r

    Points: 2
    Helpful Answer Positive Rating
Oh yeah ~ for tmp variable, it need 3 bits, => [2:0] tmp
Beside this, Is there any bug ?
I remember we should better separate combinational and sequential part in our design. It could save more gate counts for us?
So, in practice, style 2 is better them style 1? correct?
Thank you
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top