Port declaration (input, output, inout) in Verilog
📖 Scenario: You are designing a simple digital module that interacts with other parts of a circuit. To do this, you need to declare ports that define how signals enter and leave your module.
🎯 Goal: Create a Verilog module named simple_module with three ports: an input called clk, an output called led, and an inout called data_bus. This will help you understand how to declare different types of ports in Verilog.
📋 What You'll Learn
Declare a module named simple_module
Declare an input port named clk
Declare an output port named led
Declare an inout port named data_bus
💡 Why This Matters
🌍 Real World
Declaring ports correctly is essential when designing hardware modules that connect and communicate with each other in digital circuits.
💼 Career
Hardware engineers and FPGA developers must know how to declare ports to build and test digital designs effectively.
Progress0 / 4 steps
1
Create the module skeleton
Write a Verilog module named simple_module with empty parentheses and an empty body.
Verilog
Hint
Start by writing module simple_module(); and end with endmodule.
2
Add port declarations
Inside the module parentheses, declare three ports exactly as follows: input clk, output led, and inout data_bus.
Verilog
Hint
List the ports inside the parentheses separated by commas, specifying their directions.
3
Declare port types inside the module
Inside the module body, declare the ports again with their directions: input clk;, output led;, and inout data_bus;.
Verilog
Hint
Inside the module, write each port declaration ending with a semicolon.
4
Print the module ports as comments
Add comments inside the module that list the ports and their directions exactly as: // Port: clk (input), // Port: led (output), and // Port: data_bus (inout).
Verilog
Hint
Use comments to clearly label each port and its direction.
Practice
(1/5)
1. Which keyword in Verilog is used to declare a port that receives signals from outside the module?
easy
A. input
B. output
C. inout
D. wire
Solution
Step 1: Understand port directions
In Verilog, ports can be inputs, outputs, or bidirectional (inout).
Step 2: Identify the keyword for receiving signals
The keyword input is used to declare ports that receive signals from outside the module.
Final Answer:
input -> Option A
Quick Check:
input = receives signals [OK]
Hint: Input ports receive signals from outside [OK]
Common Mistakes:
Confusing output with input
Using wire instead of input for ports
Thinking inout means input only
2. Which of the following is the correct way to declare a 4-bit output port named data_out in Verilog?
easy
A. inout [4:0] data_out;
B. input [3:0] data_out;
C. output [3:0] data_out;
D. output data_out[3:0];
Solution
Step 1: Check port direction and width syntax
Output ports use the keyword output. The width is declared as [3:0] for 4 bits.
Step 2: Validate the syntax correctness
output [3:0] data_out; correctly declares a 4-bit output port as output [3:0] data_out;. output data_out[3:0]; has incorrect syntax placing the width after the name.
Final Answer:
output [3:0] data_out; -> Option C
Quick Check:
output [3:0] name; is correct syntax [OK]
Hint: Declare width before port name with brackets [OK]
A. clk is an input port, ready is an output port, data is a bidirectional port
B. clk is an output port, ready is an input port, data is an output port
C. clk is a bidirectional port, ready is an input port, data is an output port
D. All ports are inputs by default
Solution
Step 1: Identify each port direction from declaration
input clk means clk is input, output reg ready means ready is output, and inout wire data means data is bidirectional.
Step 2: Match each port with its direction
clk is an input port, ready is an output port, data is a bidirectional port correctly matches clk as input, ready as output, and data as inout (bidirectional).
Final Answer:
clk is input, ready is output, data is bidirectional -> Option A
Quick Check:
input, output, inout match ports [OK]
Hint: Match keywords input/output/inout to port roles [OK]
Common Mistakes:
Mixing input and output roles
Assuming all ports default to input
Ignoring reg or wire keywords
4. Identify the error in this Verilog port declaration:
D. Missing semicolon after inout_signal declaration
Solution
Step 1: Check syntax of port declarations
In this non-ANSI style Verilog module, port names are listed in the header, and directions/types are declared inside the module body. Each declaration must end with a semicolon.
Step 2: Identify the specific error
The declaration inout inout_signal lacks the required semicolon.
Final Answer:
Missing semicolon after inout_signal declaration -> Option D
Quick Check:
Declarations inside module end with semicolon [OK]
Hint: Port declarations inside module end with semicolon [OK]
Common Mistakes:
Missing semicolons after port declarations
Thinking output must be reg always
Believing inout is invalid keyword
5. You want to design a Verilog module with a bidirectional data bus named bus that is 8 bits wide. Which is the correct port declaration and internal signal type to allow reading and writing on bus?
hard
A. input [7:0] bus; reg [7:0] bus_internal;
B. inout [7:0] bus; reg [7:0] bus_internal; assign bus = write_enable ? data_out : 8'bz;
C. output [7:0] bus; wire [7:0] bus_internal;
D. inout [7:0] bus; wire bus_internal; assign bus = write_enable ? data_out : 8'bz;
Solution
Step 1: Understand bidirectional port requirements
Bidirectional ports use inout and require internal signals to store data for output, typically declared as reg for driven values.
Step 2: Analyze each option for correctness
inout [7:0] bus; reg [7:0] bus_internal; assign bus = write_enable ? data_out : 8'bz; correctly declares inout [7:0] bus and uses reg [7:0] bus_internal for internal storage, with an assignment controlling bus output with high impedance when not writing.
Final Answer:
inout [7:0] bus; reg [7:0] bus_internal; assign bus = write_enable ? data_out : 8'bz; -> Option B
Quick Check:
inout with reg internal and tri-state assign [OK]
Hint: Use inout with reg and tri-state assign for bidirectional bus [OK]
Common Mistakes:
Using wire instead of reg for internal signal
Declaring bus as input or output only
Not using tri-state (8'bz) for bus when not driving
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.