FPGA Central - World's 1st FPGA / CPLD Portal

FPGA Central

World's 1st FPGA Portal

 

Go Back   FPGA Groups > NewsGroup > Verilog

Verilog comp.lang.verilog newsgroup / usenet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-14-2006, 01:35 AM
Guest
 
Posts: n/a
Default How to Divide a clock by 2

Im trying to learn an HDL, and just trying to do a simple clock divide,
but not sure when to use 'reg' or when to use 'wire'. The xilinx ise
8.1 synthesizer complains about dclk below:


module test(clk);
input clk;

reg [1:0] CounterZ;
reg dclk; //input clk divided by 2
wire dclk;

always @(posedge clk)
CounterZ <= CounterZ + 1;

dclk = CounterZ(0);

Reply With Quote
  #2 (permalink)  
Old 02-14-2006, 04:39 AM
Rob
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2

Procedural statements must be acted on by variables defined as 'reg'. In
your case you have dclk getting assigned with a procedural statement and
defined as both wire and reg.

Define dclk as reg only and put begin and end around the last two
statements. Also change the dclk assignment to a non-blocking assignment by
using the syntax <= instead of just =.

You also need an output defined for the module, see below.

module divide_by_two (clk, dclk);

input clk;
output reg dclk;

reg [1:0] CounterZ;

always @(posedge clk)
begin
CounterZ <= CounterZ + 1;
dclk <= CounterZ[0];
end
endmodule


<[email protected]> wrote in message
news:[email protected] oups.com...
> Im trying to learn an HDL, and just trying to do a simple clock divide,
> but not sure when to use 'reg' or when to use 'wire'. The xilinx ise
> 8.1 synthesizer complains about dclk below:
>
>
> module test(clk);
> input clk;
>
> reg [1:0] CounterZ;
> reg dclk; //input clk divided by 2
> wire dclk;
>
> always @(posedge clk)
> CounterZ <= CounterZ + 1;
>
> dclk = CounterZ(0);
>



Reply With Quote
  #3 (permalink)  
Old 02-14-2006, 12:15 PM
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2

Classic divider:
module divide_by_two (clk, rst, dclk);

input clk, rst;
output reg dclk;

always @(posedge clk or posedge rst)
begin
if(rst) dclk <= 1'b0;
else dclk <= ~dclk;
end
endmodule

Reply With Quote
  #4 (permalink)  
Old 02-16-2006, 01:48 AM
Art Stamness
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2

If you want a clock divider in the Xilinx ISE tool, please use the
built in ones for your FPGA. It is in the manual, and they have very
sophisticated clock dividers that can divide down to any reasonable
range.

When I took the FPGA class, it was an automatic F if you tried to build
your own clock divider. You don't have to write your own.

If this is just a toy project and you want to see what the verilog
looks like michaelst's answer is the best given so far.

-Art

Reply With Quote
  #5 (permalink)  
Old 02-16-2006, 02:09 AM
Rob
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2

Of course there are better ways to implement the divider; I was just trying
to build off of what he already started.


"Art Stamness" <[email protected]ail.com> wrote in message
news:[email protected] ups.com...
> If you want a clock divider in the Xilinx ISE tool, please use the
> built in ones for your FPGA. It is in the manual, and they have very
> sophisticated clock dividers that can divide down to any reasonable
> range.
>
> When I took the FPGA class, it was an automatic F if you tried to build
> your own clock divider. You don't have to write your own.
>
> If this is just a toy project and you want to see what the verilog
> looks like michaelst's answer is the best given so far.
>
> -Art
>



Reply With Quote
  #6 (permalink)  
Old 02-16-2006, 08:40 AM
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2

Thanks Rob!

Actually your answer was very helpful, not just in how to do the clock
divide, but also in helping me with my confusion of reg vs wire.

So if I understand correctly, a variable should be declared a reg if
its value is dependent on a condition (i.e. 'always @...'), and it
should be declared a wire otherwise? Also, if the signal is to drive
a pin or a signal in another module, then it should also be declared a
reg, correct?

TIA!


Rob wrote:
> Of course there are better ways to implement the divider; I was just trying
> to build off of what he already started.
>
>
> "Art Stamness" <[email protected]> wrote in message
> news:[email protected] ups.com...
> > If you want a clock divider in the Xilinx ISE tool, please use the
> > built in ones for your FPGA. It is in the manual, and they have very
> > sophisticated clock dividers that can divide down to any reasonable
> > range.
> >
> > When I took the FPGA class, it was an automatic F if you tried to build
> > your own clock divider. You don't have to write your own.
> >
> > If this is just a toy project and you want to see what the verilog
> > looks like michaelst's answer is the best given so far.
> >
> > -Art
> >


Reply With Quote
  #7 (permalink)  
Old 02-16-2006, 07:42 PM
Art Stamness
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2

The above statements are not accurate. You should try getting a good
verilog book, most of this would be layed out better there.

Corrections :

1. A reg or a wire can drive an "output".
2. Wires are assigned using continuous assignments ( done outside of
event triggered blocks : always, initial . . ) . They look like this :

....
wire foo ;
assign foo = a & b ;
....

The expression "a&b" is evaluated whenever a or b change, and the value
of foo is updated with that result. This continuous assignment is used
to model combinational logic, and, or, xor . . . etc.

4. Regs are different. They are procedurally assigned during event
control. So they can exhibit the same behavior as a wire like :

reg foo ;
always @(a or b)
begin
foo = a & b ;
end

The value of foo is caculated when either a or b are changed. The above
code, matches exactly the behavior of the wire above. This is the case
where reg is used to model combinational logic.

But Regs can also be used to model sequential logic, like Flip Flops,
latches, memories :

always @(posedge clk)
begin
foo <= a & b ;
end

This is the modeling of a basic Positive Edge Triggered flip flop. Only
when the positive edge changes is the expression a & b calculated. If a
& b change at any time after that, the value of foo will not be updated
until the next positive edge of the clock, when the expression is
evaluated again.

So when you are talking about modeling a Clock divider, you need to
have a flop, and you need to model it as a reg, whose values change on
the edge of another clock.

I hope this helps.

-Art

Reply With Quote
  #8 (permalink)  
Old 02-17-2006, 02:46 AM
Rob
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2


"Art Stamness" <[email protected]> wrote in message
news:[email protected] ups.com...
> The above statements are not accurate. You should try getting a good
> verilog book, most of this would be layed out better there.


I have a good book!

>
> Corrections :
>
> 1. A reg or a wire can drive an "output".


Did I say it couldn't?!

> 2. Wires are assigned using continuous assignments ( done outside of
> event triggered blocks : always, initial . . ) . They look like this :


I'm very familiar on how to drive wires. I don't think I mentioned how to
drive a wire in my response.

>
> ...
> wire foo ;
> assign foo = a & b ;
> ...
>
> The expression "a&b" is evaluated whenever a or b change, and the value
> of foo is updated with that result. This continuous assignment is used
> to model combinational logic, and, or, xor . . . etc.
>
> 4. Regs are different. They are procedurally assigned during event
> control. So they can exhibit the same behavior as a wire like :
>
> reg foo ;
> always @(a or b)
> begin
> foo = a & b ;
> end
>
> The value of foo is caculated when either a or b are changed. The above
> code, matches exactly the behavior of the wire above. This is the case
> where reg is used to model combinational logic.
>
> But Regs can also be used to model sequential logic, like Flip Flops,
> latches, memories :
>
> always @(posedge clk)
> begin
> foo <= a & b ;
> end
>
> This is the modeling of a basic Positive Edge Triggered flip flop. Only
> when the positive edge changes is the expression a & b calculated. If a
> & b change at any time after that, the value of foo will not be updated
> until the next positive edge of the clock, when the expression is
> evaluated again.
>
> So when you are talking about modeling a Clock divider, you need to
> have a flop, and you need to model it as a reg, whose values change on
> the edge of another clock.
>
> I hope this helps.
>
> -Art
>



Reply With Quote
  #9 (permalink)  
Old 02-17-2006, 06:10 AM
Art Stamness
Guest
 
Posts: n/a
Default Re: How to Divide a clock by 2

Rob,

My post was in response to benn's statement :

> So if I understand correctly, a variable should be declared a reg if
> its value is dependent on a condition (i.e. 'always @...'), and it
> should be declared a wire otherwise? Also, if the signal is to drive
> a pin or a signal in another module, then it should also be declared a
> reg, correct?


For some reason, when I use the "reply" tab on groups.google.com, it
seems to attach the reply to the last, not to the one I clicked on.

-Art

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiply/divide algorithm skyworld Verilog 13 09-01-2005 07:38 AM
Divide by 3/2 clock generation with 50% duty cycle? rsk Verilog 7 07-25-2005 09:36 PM
how to divide the clock by 2 using only xor gates? eva Verilog 1 03-29-2005 11:48 PM
one clock late Paul Verilog 4 02-16-2004 10:08 AM
generate a 20 MHZ clock(pulse) with duty cycle other than 50 % from master clock 40 MHZ having 50 % duty cycle MegaPowerStar Verilog 2 08-19-2003 02:29 AM


All times are GMT +1. The time now is 09:51 PM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Copyright 2008 @ FPGA Central. All rights reserved