📖 Scenario: Imagine you are designing a big digital system like a simple calculator. Instead of writing all the logic in one place, you break it into smaller parts like adders, subtractors, and controllers. This way, your design is easier to understand and manage.
🎯 Goal: You will create a small Verilog project that shows how hierarchy helps organize a large design by building a simple calculator using smaller modules.
📋 What You'll Learn
Create a top module called calculator
Create smaller modules called adder and subtractor
Use hierarchy by instantiating adder and subtractor inside calculator
Show how inputs flow through the hierarchy and produce outputs
💡 Why This Matters
🌍 Real World
Large digital systems like CPUs, GPUs, and communication devices use hierarchy to organize millions of components efficiently.
💼 Career
Understanding hierarchy is essential for hardware engineers to design, test, and maintain complex digital circuits professionally.
Progress0 / 4 steps
1
Create the adder module
Write a Verilog module called adder that has two 4-bit inputs called a and b, and one 4-bit output called sum. The module should add a and b and assign the result to sum.
Verilog
Hint
Use module keyword to define the adder. Use assign to add inputs.
2
Create the subtractor module
Write a Verilog module called subtractor that has two 4-bit inputs called a and b, and one 4-bit output called diff. The module should subtract b from a and assign the result to diff.
Verilog
Hint
Similar to adder, but use subtraction.
3
Create the top-level calculator module with hierarchy
Write a Verilog module called calculator that has two 4-bit inputs called in1 and in2, and two 4-bit outputs called sum_out and diff_out. Inside this module, instantiate the adder and subtractor modules. Connect in1 and in2 to both modules and connect their outputs to sum_out and diff_out respectively.
Verilog
Hint
Use module instantiation syntax with dot connections to connect signals.
4
Test the calculator module output
Write a testbench module called testbench that declares two 4-bit registers a and b, and two 4-bit wires sum and diff. Instantiate the calculator module connecting a, b, sum, and diff. Assign a = 4 and b = 3. Use $display to print the values of a, b, sum, and diff. Run the simulation for one time unit.
Verilog
Hint
Use reg for inputs and wire for outputs. Use $display to print values.
Practice
(1/5)
1. Why is hierarchy important in large Verilog designs?
easy
A. It allows using only one module for everything.
B. It makes the code run faster automatically.
C. It breaks the design into smaller, manageable modules.
D. It removes the need for testbenches.
Solution
Step 1: Understand the role of hierarchy
Hierarchy divides a large design into smaller modules, making it easier to manage.
Step 2: Compare options
Only breaking into smaller modules helps organize and debug large designs effectively.
Final Answer:
It breaks the design into smaller, manageable modules. -> Option C
Quick Check:
Hierarchy = Smaller modules [OK]
Hint: Think: big design = many small parts [OK]
Common Mistakes:
Assuming hierarchy speeds up code automatically
Believing hierarchy removes need for testbenches
Thinking one module can handle everything
2. Which of the following is the correct way to instantiate a module named alu inside another module in Verilog?
easy
A. call alu();
B. module alu();
C. inst alu();
D. alu u1();
Solution
Step 1: Recall module instantiation syntax
In Verilog, to use a module inside another, write: module_name instance_name();
A. Incorrect port connection syntax in cpu instantiation
B. Missing semicolon after module declaration
C. Output port 'done' should be declared as reg in system
D. Always block missing sensitivity list
Solution
Step 1: Recall module instantiation syntax
Correct syntax is <module_name> <instance_name>(<connections for all ports in order>);
Step 2: Analyze the instantiation
cpu has two ports (input clk, output done), but cpu cpu_inst(clk); provides only one connection, causing port mismatch and incorrect port connection syntax.
Final Answer:
Incorrect port connection syntax in cpu instantiation -> Option A
Quick Check:
Port arguments != port declarations [OK]
Hint: Check module instantiation parentheses and port connections [OK]
Common Mistakes:
Confusing reg declaration in top module
Omitting port connections in instantiation
Misunderstanding sensitivity list in always block
5. You have a large Verilog design with multiple modules: cpu, memory, and io. To improve readability and reuse, which hierarchical approach is best?
hard
A. Create a top module that instantiates cpu, memory, and io modules separately.
B. Combine all code into one big module to avoid confusion.
C. Write separate modules but never instantiate them; use only one module.
D. Use only behavioral code without modules for flexibility.
Solution
Step 1: Understand hierarchy benefits
Separating design into modules and instantiating them in a top module improves organization and reuse.
Step 2: Evaluate options
Creating a top module that instantiates cpu, memory, and io modules separately is best practice for large designs.
Final Answer:
Create a top module that instantiates cpu, memory, and io modules separately. -> Option A
Quick Check:
Top module with submodules = Create a top module that instantiates cpu, memory, and io modules separately. [OK]
Hint: Use a top module to organize submodules [OK]
Common Mistakes:
Putting all code in one module
Not instantiating modules at all
Avoiding modules for behavioral code only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.