Ports let you connect your Verilog module to the outside world. They define how signals come in, go out, or both.
Port declaration (input, output, inout) in Verilog
module module_name (
input wire input_name,
output wire output_name,
inout wire bidir_name
);input means the signal goes into the module.
output means the signal comes out of the module.
inout means the signal can go both ways (bidirectional).
module simple_module (
input wire clk,
output wire led
);module bidir_example (
inout wire data_line
);module mixed_ports (
input wire reset,
output wire done,
inout wire bus
);This module takes two inputs a and b, outputs their XOR as sum, and has a bidirectional data_line which is driven low.
module port_example (
input wire a,
input wire b,
output wire sum,
inout wire data_line
);
assign sum = a ^ b; // sum is XOR of inputs
// data_line is bidirectional, here just driven low
assign data_line = 1'b0;
endmoduleUse wire type for ports unless you need to store values inside the module (then use reg internally).
Bidirectional ports (inout) are often used for data buses or shared lines.
Always clearly define port directions to avoid confusion and errors in hardware design.
Ports connect your module to other parts of your design.
input brings signals into the module.
output sends signals out of the module.
inout allows signals to go both ways.
Practice
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 keywordinputis used to declare ports that receive signals from outside the module.Final Answer:
input -> Option AQuick Check:
input = receives signals [OK]
- Confusing output with input
- Using wire instead of input for ports
- Thinking inout means input only
data_out in Verilog?Solution
Step 1: Check port direction and width syntax
Output ports use the keywordoutput. 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 asoutput [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 CQuick Check:
output [3:0] name; is correct syntax [OK]
- Placing width after port name
- Using input instead of output
- Incorrect bit range like [4:0]
module example(input clk, output reg ready, inout wire data); // module body endmodule
Which statement about the ports is true?
Solution
Step 1: Identify each port direction from declaration
input clkmeans clk is input,output reg readymeans ready is output, andinout wire datameans 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 AQuick Check:
input, output, inout match ports [OK]
- Mixing input and output roles
- Assuming all ports default to input
- Ignoring reg or wire keywords
module test_module(clk, data_out, inout_signal); input wire clk; output data_out; inout inout_signal endmodule
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 declarationinout inout_signallacks the required semicolon.Final Answer:
Missing semicolon after inout_signal declaration -> Option DQuick Check:
Declarations inside module end with semicolon [OK]
- Missing semicolons after port declarations
- Thinking output must be reg always
- Believing inout is invalid keyword
bus that is 8 bits wide. Which is the correct port declaration and internal signal type to allow reading and writing on bus?Solution
Step 1: Understand bidirectional port requirements
Bidirectional ports useinoutand require internal signals to store data for output, typically declared asregfor 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 declaresinout [7:0] busand usesreg [7:0] bus_internalfor 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 BQuick Check:
inout with reg internal and tri-state assign [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
