Menu
Coddy logo textTech

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
endfunction

Calling 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 icon

Challenge

Medium

Implement 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;
endmodule
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in RTL Design & Verification

Practice on your own: Online Verilog compiler