Write two Gray code functions in a package, and use each one on a 5-bit port and on an 8-bit port.
Hardware context: A package of two Gray code functions used on a 5-bit and an 8-bit port; and a temperature grader whose function reads two thresholds from its module. Write the two Gray code functions and use each at 5 and at 8 bits.
A block sends a 5-bit pointer and an 8-bit event count to another clock domain in Gray code. It receives a pointer and a count back. Gray code changes one bit per step, so a value caught while it changes is the old one or the new one. The team wants one pair of conversion functions for both widths, kept in a package.
This is an Implement problem. Write bin2gray and gray2bin in gray_pkg, each 8 bits wide. Then write gray_xing, which calls each function on a 5-bit port and on an 8-bit port.
The tests read the four outputs. They also call your two functions by name from the bench, twice in one expression. A function that remembers anything from one call to the next fails there.
Brief: all four paths are combinational. A 5-bit value is extended with zeros to 8 bits for the call. The low 5 bits of the result are used. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
set | 26 | input of each check |
Full specification
package gray_pkg;
function automatic logic [7:0] bin2gray(input logic [7:0] b);
function automatic logic [7:0] gray2bin(input logic [7:0] g);
endpackage
module gray_xing
import gray_pkg::*;
(
input logic [4:0] wbin, // a 5-bit write pointer, binary
output logic [4:0] wgray, // the same pointer in Gray code
input logic [4:0] rgray, // a 5-bit read pointer, Gray code
output logic [4:0] rbin, // the same pointer in binary
input logic [7:0] cbin, // an 8-bit event count, binary
output logic [7:0] cgray,
input logic [7:0] sgray, // an 8-bit count from the other side, Gray code
output logic [7:0] sbin
);
// your design
endmodule
| Function | Rule |
|---|---|
| bin2gray | Gray bit i is binary bit i XOR binary bit i+1. The top Gray bit equals the top binary bit. |
| gray2bin | Binary bit i is the XOR of Gray bits i and above. |
| Binary | Gray |
|---|---|
| 000 | 000 |
| 001 | 001 |
| 010 | 011 |
| 011 | 010 |
| 100 | 110 |
| 101 | 111 |
| 110 | 101 |
| 111 | 100 |
| Output | Value |
|---|---|
| wgray[4:0] | the Gray code of wbin |
| rbin[4:0] | the binary value of rgray |
| cgray[7:0] | the Gray code of cbin |
| sbin[7:0] | the binary value of sgray |
Result fields: the four outputs come first. Then come bin2gray(cbin) and gray2bin(sgray), called from the bench. The last field is pair = gray2bin(sgray) XOR gray2bin(rgray extended with zeros).
Examples
Example 1
Small Values- Input
wbin=3 rgray=00010 cbin=5 sgray=00000111
- Output
wgray = 'b00010, rbin = 3, cgray = 'b00000111, sbin = 5, bin2gray_of_cbin = 'b00000111, gray2bin_of_sgray = 5, pair = 0x6
Binary 3 is Gray 00010, and Gray 00010 is binary 3. Binary 5 is Gray 00000111, and Gray 00000111 is binary 5.
Example 2
The Top Bit of Each Port- Input
wbin=16 rgray=10000 cbin=128 sgray=10000000
- Output
wgray = 'b11000, rbin = 31, cgray = 'b11000000, sbin = 255, bin2gray_of_cbin = 'b11000000, gray2bin_of_sgray = 255, pair = 0xe0
Binary 16 is Gray 11000. Gray 10000 is binary 31, because every lower bit takes the top bit. The 8-bit ports show the same at bit 7.
Example 3
All Ones- Input
wbin=31 rgray=11111 cbin=255 sgray=11111111
- Output
wgray = 'b10000, rbin = 21, cgray = 'b10000000, sbin = 170, bin2gray_of_cbin = 'b10000000, gray2bin_of_sgray = 170, pair = 0xbf
Binary 31 is Gray 10000. Gray 11111 is binary 21, which is 10101: the bits alternate.
Constraints and Contract
- Inputs are known 0/1 values. Each case applies one set of inputs and reads the outputs 1 ns later.
- Keep the package name, the function names and widths, and the module ports. No clock, no storage.
Hints
Hint 1
bin2gray is one XOR of the value with itself shifted right by one.
Hint 2
gray2bin needs every higher bit. A loop with a constant bound that XORs in the value shifted right by 0, 1, 2 and so on does it.
Hint 3
Check what a zero-extended 5-bit value gives in an 8-bit function. The upper bits are 0, and XOR with 0 changes nothing.
What the grader checks: Eight directed and twelve seeded input sets are applied one at a time. The four outputs and three direct calls of the package functions are compared with an independent model. Passing shows both functions and all four uses agree with the code tables on these values. The synthesis check adds the latch count.
The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.
20 checks on Icarus Verilog + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 steps and the final capstone