Combinational Functions
Part of the RTL Design & Verification section of Coddy's Verilog journey. Lesson 11 of 38.
A Verilog function packages a zero-time calculation for reuse within a module. Declare its return width, input arguments and local variables, then assign the result to the function name. Combinational functions contain no delays or event controls; a loop with fixed bounds can describe repeated logic.
Relevant excerpt; surrounding declarations and connections are supplied in the challenge.
function [7:0] gray;
input [7:0] value;
begin gray = value ^ (value >> 1); end
endfunctionCalling the function computes an expression without adding simulation time.
Assign the function result on every path and keep combinational functions free of timing controls.
Challenge
MediumImplement a Verilog function gray that converts an unsigned 8-bit binary value to Gray code using value XOR (value shifted right by one). Drive y by calling the function with a. Output columns: y. All stimulus values are known binary values.
Complete design.v and preserve its module names and ports. The locked testbench.v supplies input changes and prints the outputs after they settle. It chooses a scenario using a simulator argument such as +CASE=1; no standard input is required. Do not add printing, delays or simulation termination to the design. Expected output is one row of decimal values per observation, separated by one space and ending with a newline.
Try it yourself
module dut (input [7:0] a, output [7:0] y);
// Replace this placeholder with your design.
assign y = 0;
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in RTL Design & Verification
1Reliable Combinational RTL
Defaults Prevent LatchesPriority EncodersOne-Hot ValidationSafe Case DecodingRecap - Request Router2Widths and Signed Arithmetic
Preserving Carry BitsSigned ComparisonsArithmetic Right ShiftsOverflow and SaturationRecap - Signed Difference3Reusable RTL
Combinational FunctionsWidth-Safe ParametersGenerate LoopsConditional GenerateRecap - Reusable Bit MaskPractice on your own: Online Verilog compiler