Gray Code is quite popular theme in educational problems - so let's have it bit differently. The task is to build logic function (or schematic) to convert Gray Code to normal binary.
Look at the picture above - what may be this curiously marked disc? It is a typical way of constructing various
positional and angualar sensors, with electro-mechanic or optical elements. Suppose this disc is printed on
the transparent film and installed on some rotating axle. We put 4
light-sensitive elements (e.g. photo-diodes)
behind the disc, in "radial" direction - and some lamp (or lamps) in front of it - so that elements are lit
when not covered by darkened area of the disc.
All right, but why so strange order of dark areas? If we represent it as 4
binary digits, assigning 0
to light areas and 1
to dark ones (least significant bit closer to center),
then, starting from the top, clockwise we get 0000
, 0001
, 0011
- i.e. 0
, 1
and then suddenly 3
if regarded as normal binary!
Ok, it is not the normal binary. It is Gray Code
with the magic property that every two consecutive
values differ only by single bit.
Why is it important? Suppose we have used normal binary instead. Suppose disc is turning from position 5
to
position 6
, for example. If light sensors are aligned ideally (and disc also is ideal) then all bits
shall change at single instant, from 0101
to 0110
. However world is not ideal and especially when much more
than 4
bits are used, zones become small and so the bits may change not simultaneously.
For example if the bit meaning 2
switches from 0
to 1
slightly before the bit meaning 1
switches from
1
to 0
we shall observe curious sequence 0101
, changing to 0111
(shortly) and only then 0110
comes.
Control system, unless it is too slow to react, will see it as if the axle made fast turn 2 positions forward and then returned one position back! Surely this may lead to unwanted behavior.
Gray Code is free from such errors - it never changes the state of more than single element!
Suppose we are building some machine with Gray Code disc described above - and want to display its position. Of course modern student may tell "let's wire arduino and write a program for it". But no! Let's do it as pure logic function which can be implemented in few discrete chips (i.e. we still need wiring or soldering, but skip programming step - which may be important if we make thousands of devices).
Recollect the seven-segment decoder task. It takes binary value and represents it as a human-readable digit. So if we create some logic to convert Gray Code to Binary, we can directly connect it to such decoder-and-display module!
Solution should be provided as a set of logic functions, in syntax much similar to one used in the problem of seven-segment decoder:
OUT = FUNC(INPUTS)
OUT
is a single variable, named from a
to z
INPUTS
are several variables, comma-separatedNAND
, NOR
and XOR
(note that XOR
works on exactly 2
arguments)Input bits of Gray Code are stored in variables from a
to d
- and output bits of binary value should be
stored in variables from w
to z
. In both cases it is least significant bit first.
Example:
; comments start with semicolon
o = xor(a, a) ; a way to represent 0
e = nand(o) ; a way to represent 1
y = nor(b, o)