A module is like a small machine that does a specific job in a digital circuit. It helps organize and build bigger circuits by connecting these small machines together.
Module as the basic building block in Verilog
module module_name (port_list); // declarations // statements endmodule
The module keyword starts the module definition.
The endmodule keyword ends the module.
module and_gate (input a, input b, output y); assign y = a & b; endmodule
module inverter (input in, output out); assign out = ~in; endmodule
module simple_counter (input clk, input reset, output reg [3:0] count); always @(posedge clk or posedge reset) begin if (reset) count <= 0; else count <= count + 1; end endmodule
This program defines an AND gate module and a testbench to check its output for all input combinations. The testbench changes inputs every 10 time units and prints the result.
module and_gate (input a, input b, output y); assign y = a & b; endmodule module testbench; reg a, b; wire y; and_gate uut (.a(a), .b(b), .y(y)); initial begin a = 0; b = 0; #10 a = 0; b = 1; #10 a = 1; b = 0; #10 a = 1; b = 1; #10 $finish; end initial begin $monitor("At time %0t: a=%b b=%b y=%b", $time, a, b, y); end endmodule
Modules can have inputs, outputs, and internal signals.
Use meaningful names for modules and ports to keep your design clear.
You can connect modules together to build complex circuits.
A module is the basic building block in Verilog to create parts of a digital circuit.
Modules help organize, reuse, and test your designs easily.
Every module starts with module and ends with endmodule.
Practice
What is the main purpose of a module in Verilog?
Solution
Step 1: Understand what a module represents
A module in Verilog is a block that describes hardware behavior or structure.Step 2: Identify the purpose of modules
Modules help organize and reuse hardware design parts, not software or power control.Final Answer:
It defines a reusable block of hardware design. -> Option AQuick Check:
Module = reusable hardware block [OK]
- Thinking modules run software code
- Confusing modules with memory storage
- Assuming modules control power
Which of the following is the correct way to start and end a Verilog module named my_module?
?
Solution
Step 1: Recall Verilog module syntax
A module starts with the keywordmodulefollowed by the module name and ends withendmodule.Step 2: Check each option
module my_module(); endmodule uses correct keywords and syntax. Others use invalid keywords or punctuation.Final Answer:
module my_module(); endmodule -> Option BQuick Check:
module ... endmodule = correct syntax [OK]
- Using wrong keywords like start or finish
- Missing parentheses after module name
- Adding colons or semicolons incorrectly
What will be the output of the following Verilog module when input_a = 1 and input_b = 0?
module simple_and(input input_a, input input_b, output reg out);
always @(*) begin
out = input_a & input_b;
end
endmoduleSolution
Step 1: Understand the operation inside the module
The module assignsoutas the AND ofinput_aandinput_b.Step 2: Calculate the AND of inputs
Giveninput_a = 1andinput_b = 0, 1 & 0 = 0.Final Answer:
out = 0 -> Option DQuick Check:
1 AND 0 = 0 [OK]
- Assuming output equals one input directly
- Confusing AND with OR operation
- Ignoring that output is reg type
Identify the error in this Verilog module code:
module faulty_module(input a, input b, output reg c);
always @(*) begin
c = a | b
end
endmoduleSolution
Step 1: Check syntax inside always block
The assignmentc = a | bis missing a semicolon at the end.Step 2: Verify other parts
Usingregfor output is correct; sensitivity list@(*)is valid; module names can be lowercase.Final Answer:
Missing semicolon after assignment inside always block. -> Option CQuick Check:
Statements inside always need semicolons [OK]
- Forgetting semicolon after assignment
- Thinking output cannot be reg
- Believing sensitivity list must list signals explicitly
You want to create a Verilog module named counter that counts from 0 to 3 repeatedly using a 2-bit output count. Which module header and port declaration is correct?
Solution
Step 1: Identify correct port order and types
The clock inputclkshould be input, andcountis a 2-bit register output, declared asoutput reg [1:0] count.Step 2: Check each option
module counter(input clk, output reg [1:0] count); endmodule correctly declares ports and types. module counter(output reg [2:0] count, input clk); endmodule has wrong bit width. module counter(input clk, output [1:0] count); endmodule missesregfor output assigned inside always block. module counter(input clk, output reg count[1:0]); endmodule uses incorrect syntax for bus declaration.Final Answer:
module counter(input clk, output reg [1:0] count); endmodule -> Option AQuick Check:
Output reg [1:0] count with input clk = correct [OK]
- Forgetting 'reg' for output assigned in always block
- Incorrect bus declaration syntax
- Swapping input and output order
Ready to prove it?
Pass the test and this pattern moves unknown
