RTL Foundations / F06

Reuse and Parameters: What Changes When There Are Three Channels?

Use the same RTL for different channel counts and widths, then check the selection contract with waveforms, six configurations, and generic netlists.

Lesson film

Film chapters

TranscriptCaptionsDownload film

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_iChanneldata_ovalid_oparity_o
00A8'h1210
01B8'hA510
10C8'h8110
11Unassigned8'h0000

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.

The one-channel instance has an eight-bit data input and a one-bit selector. The three-channel instance has a 24-bit data input and a two-bit selector.
The same RTL gives two fixed configurations. At runtime the selector changes where the current result comes from.

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.

Array channels C, B, and A occupy packed ranges 23 to 16, 15 to 8, and 7 to 0. Selector 01 reads eight bits starting at bit 8, giving A5.
The packed bus is written C, B, A from high to low. The selector counts from A at the low end.

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.

The extension calculates parity for all channels. Words 12, A5, and 83 feed XOR reductions and produce 0, 0, and 1. The main design calculates parity for the selected word.
If the receiver needs three parity bits simultaneously, each channel has a result. The extension exposes that wiring.
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.

Captured Icarus VCD. At 15 ns, selector 11 gives 00 and valid 0 in the corrected version. With the bounds check removed, data becomes XX and valid stays 1. The last vector changes C to 83 and parity rises to 1.
At 15 ns, the selector has no corresponding channel. Without the bounds check the receiver gets an incorrect valid flag.
CheckRecorded result
Six configurations, Icarus Verilog 12.0All 98 checks of data, valid, and parity pass.
Directed three-channel waveformThe 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; optAt 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

  1. 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
  2. Icarus Verilog documents language modes and available warnings. Command-line flags
  3. Yosys documents RTL conversion, optimization, and technology mapping. Synthesis starter

Wire a different channel bank

Changing a parameter builds a different set of wires. Changing sel switches between inputs on the existing bank. Try the last legal channel in a three-channel bank, then try 3.

    Download RTL, testbench and results

    Check your understanding

    1. How many selector bits do three eight-bit channels need?
    2. The packed input is {C, B, A} with eight bits per channel. What starting bit and width select B?
    3. Two instances use WIDTH=8, with CHANNELS=1 and CHANNELS=3. What does elaboration determine?
    4. When channel A supplies a true zero, what distinguishes selecting A from sending 11 in the three-channel interface?
    5. If the interface needs parity for A, B, and C simultaneously, what does defining the function once mean?