Problems

1. Binary to Gray and back

EasyWrite the moduleAbout 10 minFree
combinationalgray codexor

Build a purely combinational module with two independent 4-bit converters.

The first turns a binary number into its reflected Gray code, the code in which neighbouring values differ in exactly one bit. The second does the reverse: it takes a 4-bit Gray code and returns the binary number it stands for.

There is no clock and no reset. Both outputs follow their inputs immediately.

Ports

Module gray_codec

PortDirBitsWhat it is
binin4Binary number to encode.
gray_inin4Gray code to decode.
gray_outout4The Gray code of bin.
bin_outout4The binary value of gray_in.

Example

bin = 0110 (6) gives gray_out = 0101. gray_in = 0101 gives bin_out = 0110. Counting 0, 1, 2, 3 in Gray code reads 0000, 0001, 0011, 0010.

Stuck? . Sign in to save your progress. You can solve the free problems without an account.

Veriloggray_codec.v
14px
Loading the editor…

Submit your answer to have it graded. The hidden test runs on the server; you see whether it passes and, if not, the first case it gets wrong.

Binary to Gray and back: Verilog interview question | breadboardhub