fbpx
The Wayback Machine - https://web.archive.org/web/20220129122352/https://www.eetimes.com/how-to-generate-gray-codes-for-non-power-of-2-sequences/
News
EEtimes
News the global electronics community can trust
eetimes.com
power electronics news
The trusted news source for power-conscious design engineers
powerelectronicsnews.com
ebn
Supply chain news for the electronics industry
ebnonline.com
elektroda
The can't-miss forum engineers and hobbyists
elektroda.pl
Products
Electronics Products
Product news that empowers design decisions
electronicproducts.com
Datasheets.com
Design engineer' search engine for electronic components
datasheets.com
eem
The electronic components resource for engineers and purchasers
eem.com
Design
embedded.com
The design site for hardware software, and firmware engineers
embedded.com
Elector Schematics
Where makers and hobbyists share projects
electroschematics.com
edn Network
The design site for electronics engineers and engineering managers
edn.com
electronic tutorials
The learning center for future and novice engineers
electronics-tutorials.ws
TechOnline
The educational resource for the global engineering community
techonline.com
Tools
eeweb.com
Where electronics engineers discover the latest toolsThe design site for hardware software, and firmware engineers
eeweb.com
Part Sim
Circuit simulation made easy
partsim.com
schematics.com
Brings you all the tools to tackle projects big and small - combining real-world components with online collaboration
schematics.com
PCB Web
Hardware design made easy
pcbweb.com
schematics.io
A free online environment where users can create, edit, and share electrical schematics, or convert between popular file formats like Eagle, Altium, and OrCAD.
schematics.io
Product Advisor
Find the IoT board you’ve been searching for using this interactive solution space to help you visualize the product selection process and showcase important trade-off decisions.
transim.com/iot
Transim Engage
Transform your product pages with embeddable schematic, simulation, and 3D content modules while providing interactive user experiences for your customers.
transim.com/Products/Engage
About
AspenCore
A worldwide innovation hub servicing component manufacturers and distributors with unique marketing solutions
aspencore.com
Silicon Expert
SiliconExpert provides engineers with the data and insight they need to remove risk from the supply chain.
siliconexpert.com
Transim
Transim powers many of the tools engineers use every day on manufacturers' websites and can develop solutions for any company.
transim.com

How to generate Gray Codes for non-power-of-2 sequences

By   06.29.2007 1

Introduction
In the not-so-distant past, I published a mini-series of four “Logic 101” articles encompassing Assertion-Level Logic, Positive-vs-Negative Logic, Reed-Muller Logic, and Gray Codes.

More recently, a reader posed an interesting question with regard to using Gray codes for FIFO counters. This question was: “Is it possible to generate a Gray code counter/sequence for any non-power-of-2 number (so long as it is an even number)? (Click Here to see the original question in more detail.)

Being up to my ears in alligators fighting fires (I never metaphor I didn't like), I posed this query to the readers of Programmable Logic DesignLine (www.pldesignline.com) and was soon deluged with responses. The thing that amazed me the most was that folks attacked this problem from so many angles and came up with so many different solutions.

What was the question again?
Just to make sure we're all tap-dancing to the same drum beat, let's quickly remind ourselves as to the original problem. Suppose we have a FIFO (First-In First-Out) memory for which we will require a read pointer and a write pointer.

As a starting point, let's assume that the size of the FIFO (the number of words it contains) is a power of 2 – let's say 2^4 = 16 words. One way to implement our read and write pointers would be as binary counters, which means they are each going to be 4 bits wide. The problem here is that multiple bits may change when transitioning from one value to another. For example, four bits change when one of the pointers transitions from 7 to 8 (0111 to 1000 in binary). As an alternative, we can use a Gray code counter, in which only one bit changes as we transition from one value to another.


1. Binary code versus 4-bit Gray code.

Observe that when we reach the final (maximum) Gray code value of 1000, the next “count” will return us to our initial value of 0000, which means that – as we expect – only a single bit changes for this transition also.

But now suppose that – instead of having 16 words, we wish our FIFO to contain only 10 words. If we use our original Gray code as shown above, the sequence will now be as follows: 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101. The problem is that three bits will change value on the next transition, which will return us to our starting value of 0000 from our current value of 1101.

So, is it possible to create a Gray code sequence for any non-power-of-2 number (so long as it is an even number)? Let us see. . .

A sledgehammer approach
Actually, before we leap into the fray, I did receive one email that read as follows:

I'm sure someone will have a more formal proof, but logically you can think of the grey code counter as walking though a Karnaugh map of the counter bits. Given this, you can always extend the “walk” by two. For example, one step right becomes down, right, up. So, I'm confident you can conclude that the grey code sequence always exists.

Well, so long as we're all confident (grin). But I was hoping for a little more than this. Now, several readers took a “sledgehammer” approach whereby they hard-coded a solution without looking for an underlying structure. For example, one reader sent the following sequence with an accompanying one-line message saying: “Not an algorithm, but it appears to work.”


2. A hard-coded sledgehammer approach.

As we shall see, this solution is unknowingly based on the “adjacent pairs” technique presented in the next topic. The snag here is that I simplified the problem for the purposes of my blog. In reality, the guy who posed the original question is looking to create a FIFO with a memory size of 1128, which means we really require some algorithmic way to do this rather than hammering it out by hand.

Throwing away adjacent pairs
Quite a few readers noted that – using our original gray code as the base code – wherever you have a pair of adjacent states where the least-significant bit does not change, then the states before and after such a pair always differ by exactly one bit.


3. Throwing away adjacent pairs.

For example, consider the first four codes in the left-hand table: 0000, 0001, 0011, 0010. If we remove the two shaded codes (0001 and 0011) we are left with 0000 and 0010, which differ by only one bit. So by removing one pair from the left-hand table we have a 14-count sequence, removing any two pairs gives us a 12-count sequence, and removing any three pairs gives us a 10-count sequence (it would be pointless to remove four pairs to give us an 8-count sequence, because we could achieve the same effect by dropping down to a 3-bit Gray code).

In fact, we can mix-and-match to some extent, because we could remove one pair of codes whose least-significant bit was 1 and another pair whose least-significant bit was 0 so long as these pairs are not themselves adjacent to each other.

Pruning the middle
Now, the previous solution is easy to use by hand, but it's not great as the basis for an algorithmic approach, because it would require us to keep track as to which pairs of codes we've removed.

In fact, the solution is rather simple. Remember that we generated our original 4-bit Gray code using what I call the “mirroring method” (see my Original Article for more details) – a more correct name might be to call this a “recursive reverse-and-prefix” approach.

Well, several readers noted that is possible to simply remove pairs of entries from the center of the table around the “mirror line”, although everyone expressed themselves in different ways. For example, one message simply said:

It's quite easy to build gray codes which other sequences numbers then 2,4,8,16 etc. Just use the mirror technique and stop the right position.

Other folks went into a little more detail, such as the contributor who noted:

Here's a method that works for any number that can be divided by two. Let's say your even number is 58, in which case the next highest power of two is 64. So, we begin by generating a Gray code sequence for 64, BUT we stop at 29 (halfway to 58). We then forget about the rest of the “64” Gray codes, replace the MSB with a 1 (just like normal Gray codes), and mirror our original 29 codes (down to 0).

Let's look at this in a little more detail. As shown in the following figure, if we desire a 14-count sequence, we simply remove two entries from the middle – the one immediately above the “mirror” line and one immediately below. Similarly, if we are looking for a 12-count sequence, we remove two entries above the “mirror” line and two below, and so forth.


4. Pruning the middle of the sequence.

Pruning the ends
There's always another way of doing something. For example, quite a few folks came up with a logical counterpart to the previous solution. A typical suggestion was as follows:

I just read the article in which you asked how to create a non-power-of-2 modulo Gray code counter. I believe there are a lot of ways to achieve this, but I think that probably simplest one is to remove the same numbers of entries from the top and from the bottom of a traditional power-of-2 Gray code counter.

And of course, this is also perfectly correct. As shown in the following figure, if we desire a 14-count sequence, we simply remove one entry from the top of the table and one from the bottom; if we are looking for a 12-count sequence, we remove two entries from the top and two from the bottom, and so forth.


5. Pruning the ends of the sequence.

And what about ring and twisted-ring counters?
As discussed in the relevant Wikipedia Entry a Ring Counter (or Overbeck Counter ) is formed from a circular shift register where the output from the last register element is fed back into the input to the first (this is classified as a “counter” because it cycles through a specific sequence of states). Such a counter is used to circulate a single one (or zero) around the ring. So if we wanted a 10-state count, we would require a 10-element shift register as illustrated below:


6. A 10-bit ring counter sequence.

Of course, this does us absolutely no good at all because this isn't a Gray code sequence (two bits change for every transition). But suppose we use a Twisted Ring Counter (or Johnson Counter ) in which the output from the last stage is inverted before being fed back to the input to the first stage. In this case, a 5-bit counter will provide us with the following 10-count sequence:


7. A 5-bit twisted ring counter sequence.

In conclusion
Good Grief Charlie Brown. There's a lot more to this than meets the eye. Actually, I haven't even touched on the really interesting stuff yet, because I have some cunning ideas I'm mulling over in the back of my mind (there's just too much to do and too little time to do it all in).

But before we close, there are a couple of things you might be interested in reading and/or playing with. First of all, you may wish to check the fairly wide-ranging Wikipedia Entry on Gray codes. In addition to the Gray code sequence upon which we've based our discussions, there are also Beckett”Gray codes , Snake-in-the-Box codes , Single-Track Gray codes and n-ary Gray codes (there are also known as non-Boolean Gray codes ).

Last but certainly not least, Principal Engineer Mike Robins emailed me to say:


For a classical Gray code of size a (2 to the power of b ), for each c between 0 and a – 1 , the gray code for c is the same as the gray code for a – c – 1 , except that the most significant bit is flipped.

So, if you want a unit distance code (not sure if it can still be called a Gray code) of size 2 * d , where a / 2 < 2 * d <= a , then write out the full Gray code for a table of size a (starting at 00. . .0 and ending at 10. . . 0, and then remove the middle a – 2 * d entries. The resulting table is what you want.

[Editor's Note: Mike is talking about the “Pruning the middle” algorithm we discussed earlier in this paper.]

A very long time ago, I wrote an algorithm for auto-generating Palasm-style state machines for unit distance codes like this for even numbers. I managed to dig out the original file. I've zipped up the .c source file, .exe windows executable (should run on anything from DOS to XP to Vista, I think) and the .pds output for a 1128-count sequence for you.

As regards the actual state machine, you will note that I make use of a parity bit (QP). This is used to ease logic minimization problems if you use D-type flip-flops to hold the state. Unit distance codes are notorious for generating sparse Karnaugh maps. Depending on your architecture, you may find that it is better to generate QP asynchronously. If you use T-type flip-flops, you may not need QP at all.

Wow, how cool. Click Here to access a compressed ZIP file containing all of mike's tempting treats. Mike notes that he's happy for us [me and you, the readers] to play with the source and/or executable, modify it if we want into a more convenient output format or whatever. However, we may neither claim to have written the original, nor may we attempt to sell the source code.

Clive “Max” Maxfield is president of TechBites Interactive, a marketing consultancy firm specializing in high technology. Max is the author and co-author of a number of books, including Bebop to the Boolean Boogie (An Unconventional Guide to Electronics), The Design Warrior's Guide to FPGAs (Devices, Tools, and Flows), and How Computers Do Math featuring the pedagogical and phantasmagorical virtual DIY Calculator.

Widely regarded as being an expert in all aspects of computing and electronics (at least by his mother), Max was once referred to as “an industry notable” and a “semiconductor design expert” by someone famous who wasn't prompted, coerced, or remunerated in any way. Max can be reached at

.

1 comments
Post Comment
traneus   2018-05-17 17:01:02

Max: Thanks for this article.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.