📖 Scenario: You are designing a simple digital system that combines two smaller modules: a counter and a comparator. The top module will integrate these two modules to create a system that counts up and signals when the count reaches a preset value.
🎯 Goal: Build a top-level Verilog module that integrates a counter module and a comparator module. The top module should instantiate both modules, connect their inputs and outputs properly, and output a signal when the counter value matches the preset comparator value.
📋 What You'll Learn
Create a counter module with a 4-bit output count and an input clock clk.
Create a comparator module with inputs value and preset, and output match that is high when value == preset.
Create a top module called top_module that instantiates counter and comparator.
Connect the count output of counter to the value input of comparator.
Use a 4-bit input preset in top_module to connect to the preset input of comparator.
Output the match signal from comparator as top_match from top_module.
💡 Why This Matters
🌍 Real World
Top module integration is used in hardware design to combine smaller functional blocks into a complete system, like combining processors, memory, and peripherals.
💼 Career
Understanding how to integrate modules is essential for FPGA and ASIC design engineers who build complex digital circuits.
Progress0 / 4 steps
1
Create the counter module
Write a Verilog module called counter with inputs clk and output count as a 4-bit register. On every positive edge of clk, increment count by 1. Initialize count to 0.
Verilog
Hint
Use always @(posedge clk) to update the count on clock edges.
2
Create the comparator module
Write a Verilog module called comparator with 4-bit inputs value and preset, and output match. The output match should be high when value equals preset, otherwise low.
Verilog
Hint
Use a continuous assignment with assign to compare value and preset.
3
Create the top module top_module and instantiate submodules
Write a Verilog module called top_module with inputs clk and 4-bit preset, and output top_match. Instantiate the counter and comparator modules inside. Connect counter's count output to comparator's value input. Connect preset input to comparator's preset input. Connect comparator's match output to top_match output.
Verilog
Hint
Use named port connections to connect signals between modules.
4
Output the top_match signal
Add a testbench module to simulate the top_module. Instantiate top_module with a clock signal and preset value 4'b0101. Run the simulation for enough clock cycles to see top_match go high when the count reaches the preset. Use $display to print the count and top_match values on each clock cycle.
Verilog
Hint
Use a clock that toggles every 5 time units and print values at each positive edge.
Practice
(1/5)
1. What is the main purpose of the top module integration pattern in Verilog?
easy
A. To combine smaller modules into one organized system
B. To write testbench code for simulation
C. To declare variables inside a module
D. To define timing constraints for synthesis
Solution
Step 1: Understand the role of the top module
The top module acts as a container that connects smaller modules together.
Step 2: Recognize the benefit of integration
Integrating modules in the top module keeps the design organized and easier to manage.
Final Answer:
To combine smaller modules into one organized system -> Option A
Quick Check:
Top module integration = combining modules [OK]
Hint: Top module connects smaller modules into one system [OK]
Common Mistakes:
Confusing top module with testbench
Thinking top module declares variables only
Mixing synthesis constraints with integration
2. Which of the following is the correct way to instantiate a submodule named alu inside a top module in Verilog?
Hint: Output width matches submodule port width [OK]
Common Mistakes:
Assuming output width equals input width
Ignoring declared wire width in top module
Confusing bit ranges like [4:0] as 4 bits
4. Identify the error in this top module integration code:
module adder(input [3:0] a, input [3:0] b, output [4:0] sum);
assign sum = a + b;
endmodule
module top();
wire [4:0] result;
wire [3:0] x = 4'b0011;
wire [3:0] y = 4'b0101;
adder add_inst(.a(x), .b(y), .sum(sum));
endmodule
medium
A. Incorrect bit width for 'result'
B. Module 'adder' is missing ports
C. Signal 'sum' is not declared in top module
D. Instance name 'add_inst' is invalid
Solution
Step 1: Check port connections in top module
The instance connects .sum(sum), but sum is not declared in top module.
Step 2: Verify declared signals
Only result is declared as 5-bit wire, but not sum.
Final Answer:
Signal 'sum' is not declared in top module -> Option C
Quick Check:
Port connection must match declared signals [OK]
Hint: Check all connected signals are declared in top module [OK]
Common Mistakes:
Using undeclared signals in port connections
Assuming instance names affect correctness
Ignoring port width mismatches
5. You want to integrate two modules, counter and display, in a top module. The counter outputs a 4-bit count signal, and display takes a 4-bit input to show the count. Which of the following top module code snippets correctly connects these modules?