This post describes how to write a Verilog testbench for bidirectional or inout ports. This happens in special designs which contain bidirectional or inout ports such as I2C core, IO pads, memories, etc.

In this post, I will give an example how to write testbench code for a digital IO pad. Basically, the IO pad has logic inputs DS, OEN, IE, PE to configure the IO pad as an input or output. When DS = OEN = IE = PE = 1, the IO pad operates as an input pad. Thus, the data from the bidirectional port PAD are written into the output C. When DS = PE =1 and OEN = IE = 0, the IO pad operates as an output pad. Therefore, the signal from the input I is passed to the bidirectional port PAD.

Block diagram of the Verilog testbench

As shown in the block diagram of the Verilog testbench, the IO pad under test is the DUT block. Block 0 controls the inout port PAD as follows:

assign PAD = wr==1 ? din:1'bz;

When wr==1, the inout port PAD operates as an input, so PAD = din. When wr==0, the inout port PAD is assigned to a high impedance 1'bz. Thus, it becomes an output to get data from the input I of the IO pad.

Block 1 is the main testbench to generate test cases to check the input/ output functionality of the IO pad.

Below is the full Verilog testbench for the IO pad.