Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - Gray counter

 
Thread Tools Search this Thread
Old 09-13-2007, 02:00 PM   #1
Default Gray counter


Hi,

Do you know how to design a gray counter in VHDL ?

Better without a lookup table

The difficulty is to find a mathematical relation between the next state
and the current state.

thx


ast
  Reply With Quote
Old 09-13-2007, 02:04 PM   #2
Pieter Hulshoff
 
Posts: n/a
Default Re: Gray counter
ast wrote:
> Do you know how to design a gray counter in VHDL ?
> Better without a lookup table
> The difficulty is to find a mathematical relation between the next state
> and the current state.


Why not use a normal counter, and then do a binary to Gray conversion? Just
Googling on binary to gray conversion should give you all the information you
need. Alternatively, you could convert from Gray to binary, add 1, and then
convert back to binary.

Regards,

Pieter


Pieter Hulshoff
  Reply With Quote
Old 09-13-2007, 02:17 PM   #3
Jonathan Bromley
 
Posts: n/a
Default Re: Gray counter
On Thu, 13 Sep 2007 15:00:38 +0200, "ast" <> wrote:

>Do you know how to design a gray counter in VHDL ?


Just a thought: If you have an N-bit Gray counter, you
can make an (N+1)-bit Gray counter by induction:

* write out the states of the N-bit counter as a linear
sequence
* set the N+1'th bit to zero
* count up the linear sequence
* when you're in the last state of the sequence, and
the N+1'th bit is zero, don't wrap around to the
first state but instead toggle the N+1'th bit to 1,
keeping the other bits' value frozen...
* ... and flip the count direction so that you then
start to count down towards the first state
* when you're in the first state and the N+1'th bit
is 1, don't count down but instead toggle the N+1#th
bit to zero, keeping the other bits frozen

So you need only detect the start and finish values
of the N-bit counter's linear sequence, and the
value of the N+1'th bit. And if you make the
low and high limits of the N-bit counter be
1000....000 and 0000...000 respectively, it all
becomes rather easy.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK

http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.


Jonathan Bromley
  Reply With Quote
Old 09-13-2007, 02:27 PM   #4
Andy
 
Posts: n/a
Default Re: Gray counter
On Sep 13, 8:04 am, Pieter Hulshoff <phuls...@xs4all.nl> wrote:
> ast wrote:
> > Do you know how to design a gray counter in VHDL ?
> > Better without a lookup table
> > The difficulty is to find a mathematical relation between the next state
> > and the current state.

>
> Why not use a normal counter, and then do a binary to Gray conversion?


Because the binary to gray conversion would be "glitchy". For many
applications of a gray counter, this would not be acceptable. If a
latency of one clock cycle is acceptable, registering the output of
the conversion would work.

> Just
> Googling on binary to gray conversion should give you all the information you
> need. Alternatively, you could convert from Gray to binary, add 1, and then
> convert back to binary.


Err... "then convert back to gray". Yes, that would work. Not sure it
is optimal, since one of the directions is expensive (can't remember
which one, but I think it is gray->binary), especially for long
counters.

>
> Regards,
>
> Pieter





Andy
  Reply With Quote
Old 09-13-2007, 02:36 PM   #5
ast
 
Posts: n/a
Default Re: Gray counter

"ast" <> a écrit dans le message de news: 46e93478$0$5074$...
> Hi,
>
> Do you know how to design a gray counter in VHDL ?
>
> Better without a lookup table
>
> The difficulty is to find a mathematical relation between the next state
> and the current state.
>
> thx


I found a very interesting article in wikipedia about Gray code.

http://en.wikipedia.org/wiki/Gray_code

As Pieter Hulshoff, they say:

"Probably the most obvious way to increment a Gray code number is to convert it
into ordinary binary code, add one to it with a standard binary adder, and then convert
the result back to Gray code."

They provide algoritms for the conversion

Here is an algorithm in pseudocode to convert natural binary codes to Gray code (encode):

Let B[n:0] be the input array of bits in the usual binary representation, [0] being LSB
Let G[n:0] be the output array of bits in Gray code

G[n] = B[n]
for i = n-1 downto 0
G[i] = B[i+1] XOR B[i]

This algorithm can be rewritten in terms of words instead of arrays of bits:
G = B XOR (SHR(B))

Here is an algorithm to convert Gray code to natural binary codes (decode):

Let G[n:0] be the input array of bits in Gray code
Let B[n:0] be the output array of bits in the usual binary representation

B[n] = G[n]
for i = n-1 downto 0
B[i] = B[i+1] XOR G[i]

This can be easily translated in VHDL.




ast
  Reply With Quote
Old 09-13-2007, 02:50 PM   #6
Pieter Hulshoff
 
Posts: n/a
Default Re: Gray counter
Andy wrote:
> On Sep 13, 8:04 am, Pieter Hulshoff <phuls...@xs4all.nl> wrote:
>> ast wrote:
>>> Do you know how to design a gray counter in VHDL ?
>>> Better without a lookup table
>>> The difficulty is to find a mathematical relation between the next state
>>> and the current state.

>> Why not use a normal counter, and then do a binary to Gray conversion?

>
> Because the binary to gray conversion would be "glitchy". For many
> applications of a gray counter, this would not be acceptable. If a
> latency of one clock cycle is acceptable, registering the output of
> the conversion would work.


I would expect the need for a Gray counter to already indicate that an extra
clock cycle should not be a huge issue, but I guess the OP will have to answer
that one. Signal transfer between clock areas should always be FF2FF IMHO,
and of course an extra FF to take care of meta stability issues.

Regards,

Pieter


Pieter Hulshoff
  Reply With Quote
Old 09-13-2007, 08:49 PM   #7
Petter Gustad
 
Posts: n/a
Default Re: Gray counter
"ast" <> writes:

> Do you know how to design a gray counter in VHDL ?
>
> Better without a lookup table


Why? If you have excess ROM (or even LUT's) available, why not use it.

> The difficulty is to find a mathematical relation between the next
> state and the current state.


Your synthesis tool should be good at that. I usually feed it a table
so it can figure out the next states itself.

Usually I have some other requirements like it has to be circular or
that certain bit toggle in some specific order etc.


Petter
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Petter Gustad
  Reply With Quote
Old 09-13-2007, 09:00 PM   #8
KJ
 
Posts: n/a
Default Re: Gray counter
On Sep 13, 9:00 am, "ast" <a...@ast.com> wrote:
> Hi,
>
> Do you know how to design a gray counter in VHDL ?
>
> Better without a lookup table
>
> The difficulty is to find a mathematical relation between the next state
> and the current state.
>
> thx


Besides the usual 'Google', you might want to read the following
couple of articles for inspiration
http://www.pldesignline.com/196604078
http://www.pldesignline.com/showArti...leID=201002340

KJ



KJ
  Reply With Quote
Old 09-13-2007, 09:47 PM   #9
Kai Harrekilde-Petersen
 
Posts: n/a
Default Re: Gray counter
"ast" <> writes:

> "ast" <> a écrit dans le message de news: 46e93478$0$5074$...
>> Hi,
>>
>> Do you know how to design a gray counter in VHDL ?
>>
>> Better without a lookup table
>>
>> The difficulty is to find a mathematical relation between the next state
>> and the current state.

>
> I found a very interesting article in wikipedia about Gray code.
>
> http://en.wikipedia.org/wiki/Gray_code
>
> As Pieter Hulshoff, they say:
>
> "Probably the most obvious way to increment a Gray code number is to convert it
> into ordinary binary code, add one to it with a standard binary adder, and then convert
> the result back to Gray code."
>
> They provide algoritms for the conversion


[snip]

A few years back, I spent a weekend figuring out how to do this
directly, and generically.

The result was smaller, faster, and synthesized quicker than the
normal way of computing
next <= bin2gray(gray2bin(current)+1)


Kai
--
Kai Harrekilde-Petersen <khp(at)harrekilde(dot)dk>


Kai Harrekilde-Petersen
  Reply With Quote
Old 09-14-2007, 06:41 AM   #10
Zara
 
Posts: n/a
Default Re: Gray counter
On Thu, 13 Sep 2007 15:00:38 +0200, "ast" <> wrote:

>Hi,
>
>Do you know how to design a gray counter in VHDL ?
>
>Better without a lookup table
>
>The difficulty is to find a mathematical relation between the next state
>and the current state.
>



the relation is simple to express

Every bit changes when:

a) the parity of the higher bits equals the bit
AND
b) Next lower bit is 1
c) Next lower bits all 0

Try it!


Best regards,

Zara


Zara
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
VHDL 12 bit BCD counter dv09 Hardware 0 10-07-2009 05:57 PM
The Counter Strikes... aihockey44 Gaming 0 04-29-2009 08:35 PM
VHDL problem - Signal counter cannot be synthesized, bad synchronous description. shipacpoloy Software 0 08-14-2007 07:26 AM
Resetting The Counter. Patrick D. Rockwell DVD Video 3 07-09-2004 10:32 PM
Spalding Gray ...Swimming to Cambodia? Nomen Nescio DVD Video 2 06-28-2003 06:30 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46