In F04, a two-bit selector chose channel A, B, or C. Now the same panel design must work on different boards. Some boards have one sensor. Others have three or four. Each channel can offer a one-bit flag or an eight-bit reading. Turning both dimensions into parameters lets one RTL description cover those boards. Start with the three-channel contract, then check what changes when the tools build a different instance.
Which selector values do three channels need?
Suppose A, B, and C each provide eight bits. The controller needs at least two bits to send index 0, 1, or 2. Two bits also allow 11, but there is no fourth channel. As in F04, the receiver uses the reading only when valid_o=1. Selecting A, B, or C also returns that word’s XOR parity. If the two-bit sel_i is the remaining code 11, then data_o, valid_o, and parity_o are all zero.
sel_i | Channel | data_o | valid_o | parity_o |
|---|---|---|---|---|
00 | A | 8'h12 | 1 | 0 |
01 | B | 8'hA5 | 1 | 0 |
10 | C | 8'h81 | 1 | 0 |
11 | Unassigned | 8'h00 | 0 | 0 |
A real channel can send zero. In that case, data_o is zero and valid_o must be 1. The receiver needs valid to tell the two cases apart. We can now give the tools the dimensions they need to build the wires.
When does the tool set the dimensions?
At instantiation, WIDTH gives the number of bits in each channel and CHANNELS gives the number of channels. The tool substitutes the parameter values, establishes port widths, and builds the hierarchy. This phase is called elaboration. A localparam gives a name to an internal constant derived from those dimensions. The parent instance cannot override it.
channel_bank #(.WIDTH(8), .CHANNELS(1)) one_channel (/* ports */);
channel_bank #(.WIDTH(8), .CHANNELS(3)) three_channels (/* ports */);
The lab provides a helper for the selector width, SEL_W. For legal parameters, it is (CHANNELS > 1) ? $clog2(CHANNELS) : 1. A one-channel instance uses one bit. Only 0 selects a channel. A three-channel instance uses two bits and follows the table above. Four channels also need two bits, and every binary pattern then corresponds to a channel.
Each parameter remains constant for the life of its instance. If the selector moves from 01 to 10 during operation, the three-channel instance still has all three sets of wires. A product that needs to disable a channel at runtime can add an enable signal and a policy for disabled channels. The wiring still comes from the elaborated dimensions.
Use a channel index and a bit index
In wire [WIDTH-1:0] data [0:CHANNELS-1];, the range before the name describes the packed bits of each word. The range after the name describes the unpacked array of channels. With data[channel][bit_index], first choose a word, then choose a bit inside it. For example, data[1][0] is the least-significant bit of B.
The main module in the download connects all the words through one packed input, data_i. With three eight-bit channels, connect it as {c_i, b_i, a_i}. A occupies the eight low positions. The fixed slice data_i[15:8] gives B. To vary the start position with the selector, use an indexed part-select. In data_i[sel_i * DATA_W +: DATA_W], the start position changes with sel_i, but the width is the constant DATA_W, equal to WIDTH for legal dimensions. With sel_i=01, the slice begins at bit 8 and takes eight bits toward the high end, again giving B.
Only indices from 0 through CHANNELS-1 can select a word. A three-channel input ends at bit 23, and 11 would start the slice at bit 24. The RTL first compares {1'b0, sel_i} with an equally wide unsigned constant, CHANNEL_LIMIT. The extra bit lets the constant represent 4 when there are four channels. A two-bit constant would truncate 4 to 0. A successful comparison selects a word. Otherwise the module drives the specified invalid outputs. In the recorded four-state RTL simulation, a selector containing X or Z also takes the invalid branch.
Use the same function to describe parity
After selecting a legal channel, the main module calculates parity once for data_o. Use the reduction XOR from F03, ^word, to XOR the bits of the entire word. The teaching example wraps it in a function with no timing control. It takes one word and returns one bit.
function automatic logic word_parity(input logic [DATA_W-1:0] word);
word_parity = ^word;
endfunction
The call word_parity(data_o) returns the selected channel’s parity. If the interface needs all the parity values at once, it can pass A, B, and C through three calls to the same function. The inputs and outputs of those calls, together with the synthesized result, tell you what logic the design uses. A single definition in the source says little about the number of physical copies.
Further detail: what do procedural for and generate build?
The procedural for in parity_reuse.sv assigns an output to each word in a fixed number of iterations. The genvar loop creates named by_channel instances of parity_leaf at elaboration. Icarus checks that both produce 100. In the elaborated hierarchy before flattening, Yosys 0.33 retains three procedural $reduce_xor cells and three parity_leaf instances. Synthesis settings can allow identical logic to merge later. This example has one unpacked channel dimension. Track the meaning of each index if you add dimensions or nested generate loops. Memory inference also depends on read and write ports, their timing, and the target tools. F12 uses timed testbench tasks, and F08 contrasts parallel instances with registers in series.
Check six configurations and one out-of-range counterexample
Download the F06 RTL, testbenches, VCDs and generic netlists. Extract the archive on Linux or WSL and run bash run.sh. The tests combine WIDTH∈{1,8} with CHANNELS∈{1,3,4}. Each configuration checks the highest legal channel, every binary selector, valid zero data, and X and Z selectors. Another test drives the three-channel selector to 11 after a legal 10 and examines what the receiver sees.
| Check | Recorded result |
|---|---|
| Six configurations, Icarus Verilog 12.0 | All 98 checks of data, valid, and parity pass. |
| Directed three-channel waveform | The corrected RTL passes all six vectors. Removing the bounds check gives data XX, valid 1, and parity X for 11, failing one vector. |
Six generic netlists, Yosys 0.33 after proc; opt | At width eight, one, three, and four channels give 8-, 24-, and 32-bit data ports. The variable slice produces a generic $shiftx. The design has no latch or flip-flop. |
Further detail: zero channels and two out-of-range accesses
$clog2(1) is 0. Writing [SEL_W-1:0] with that value gives [-1:0], which declares two bits. The helper keeps at least one selector bit. The supported range is WIDTH 1 to 32 and CHANNELS 1 to 16. Outside that range, safe fallback port dimensions allow a named generate branch to select a deliberately undefined module. The recorded Icarus and Yosys hierarchy checks both reject CHANNELS=0. In a separate Icarus 12.0 test, reading bit 4 of a packed four-bit vector returns X. Reading word 3 of the three-word unpacked four-state array returns XX. The out-of-range writes leave both objects unchanged. The channel bank enforces its policy explicitly so the interface does not depend on a raw out-of-range read or subsequent tool optimization.
The tool transcript and evidence with SHA-256 document the run. The interactive example uses the same contract to show the wiring and legal selectors. If the panel needs to display the previous reading after the input changes, it needs the storage element introduced in F07.
Transfer exercise
Keep the two-bit selector and the three inputs. What should 11 return for data, valid, and parity? Design a test to distinguish a genuine zero reading from an unassigned selection.
Solution
Under this contract, 11 returns data 00, valid 0, and parity 0. First make A equal to 00 and select it with 00. Expect data 00, valid 1, and parity 0. Keep the same inputs and switch the selector to 11. Only valid should change, from 1 to 0. The receiver can now distinguish the two cases.
References
- IEEE Std 1800-2017, §§6.20, 7.4, 11.5.1, 13.4, 23.10, and 27.4, covers parameters, arrays, vector selects, functions, overrides and loop generate. Standard PDF
- Icarus Verilog documents language modes and available warnings. Command-line flags
- Yosys documents RTL conversion, optimization, and technology mapping. Synthesis starter