Exhaustive Stimulus Generator for a Priority Encoder
A small combinational block can be tested on every possible input. Here you build the hardware that generates all those inputs and the expected outputs.
Design a hardware stimulus and expected-response generator for an 8-to-3 priority encoder. The module contains an 8-bit counter representing the test vector. When enabled, it increments this test vector to sweep through all 256 possible values. For the current test vector, it continuously outputs the expected 3-bit encoded value (the index of the highest set bit) and a valid flag (1 if any bit is set, 0 otherwise). Once all 256 values have been generated, it flags completion and halts.
Timing and Reset Rules:
- Clock edge:
posedge clk - Reset: Asynchronous, active-low (
rst_n) - On reset,
dut_ingoes to 8'h00 anddonegoes to 1'b0. The combinational outputsexpected_outandexpected_validmust immediately reflect the reset value ofdut_in. - When
enis 1 anddoneis 0,dut_inincrements by 1 on each clock edge. - When
enis 0,dut_inholds its current value. - When
dut_inreaches 8'hFF (255) andenis 1, on the next clock edgedut_inwraps to 8'h00 anddoneis set to 1. - When
doneis 1, the generator halts.dut_instays at 8'h00 anddonestays 1, regardless ofen, until a reset occurs.
Worked Trace:
Cycle 1: rst_n=0 → dut_in=0, expected_out=0, expected_valid=0, done=0
Cycle 2: rst_n=1, en=1 → dut_in=1, expected_out=0, expected_valid=1, done=0
Cycle 3: en=1 → dut_in=2, expected_out=1, expected_valid=1, done=0
Cycle 4: en=0 → dut_in=2, expected_out=1, expected_valid=1, done=0 (hold)
...
Cycle 257: en=1 → dut_in=255, expected_out=7, expected_valid=1, done=0
Cycle 258: en=1 → dut_in=0, expected_out=0, expected_valid=0, done=1 (wrap and halt)
Cycle 259: en=1 → dut_in=0, expected_out=0, expected_valid=0, done=1 (halted)
| Signal | Direction | Width | Description |
|---|---|---|---|
clk |
input | 1 | Positive-edge triggered clock |
rst_n |
input | 1 | Asynchronous active-low reset |
en |
input | 1 | Enable signal; increments test vector when high |
dut_in |
output | 8 | Current test vector; resets to 8'h00 |
expected_out |
output | 3 | Expected priority encoder output for dut_in |
expected_valid |
output | 1 | Expected valid flag for dut_in |
done |
output | 1 | Asserts to 1 when all 256 vectors have been tested |
Constraints
- The reset must be asynchronous and active-low.
expected_outandexpected_validmust be purely combinational functions ofdut_in.- The priority encoder logic must identify the most significant bit (MSB) that is set to 1.
- Once
doneis asserted,dut_inmust remain at 8'h00 anddonemust remain 1 until a reset occurs.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.
This problem is part of Codiode Pro. The statement above is free to read.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.
Part of Debug and Verification
Problem 13 of 19 in the Debug and Verification track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks