📖 Scenario: You are working on a simple digital design and want to check which parts of your code are tested during simulation. This helps you find untested parts and improve your tests.
🎯 Goal: Build a small Verilog module with some conditions and add code coverage points to see which parts run during simulation.
📋 What You'll Learn
Create a Verilog module with inputs and outputs
Add a configuration variable to control a threshold
Write conditional logic using if statements
Add code coverage points using covergroup and coverpoint
Simulate and print coverage results
💡 Why This Matters
🌍 Real World
Code coverage helps hardware engineers find parts of their design that are not tested well. This improves test quality and reduces bugs in chips.
💼 Career
Understanding code coverage is important for verification engineers and anyone working on hardware design validation.
Progress0 / 4 steps
1
Create a Verilog module with inputs and outputs
Write a Verilog module called threshold_checker with an input value (4 bits) and an output flag (1 bit).
Verilog
Hint
Define the module with the exact name and ports as described.
2
Add a configuration variable for threshold
Inside the threshold_checker module, declare a parameter called THRESHOLD and set it to 8.
Verilog
Hint
Use parameter THRESHOLD = 8; inside the module.
3
Add conditional logic to set the flag
Add an always block that sets flag to 1 if value is greater than THRESHOLD, else sets flag to 0.
Verilog
Hint
Use an always @(*) block with an if statement comparing value and THRESHOLD.
4
Add code coverage points and print coverage
Add a covergroup named cg inside the module that covers the value input with a coverpoint. Instantiate and sample it inside the always block. Then add a initial block to print "Coverage collected".
Verilog
Hint
Define a covergroup with a coverpoint for value. Create an instance and call sample() inside the always block. Use $display in an initial block.
Practice
(1/5)
1. What is the main purpose of code coverage in Verilog simulations?
easy
A. To generate synthesis reports
B. To speed up the simulation runtime
C. To check which parts of the design have been tested
D. To optimize the hardware layout
Solution
Step 1: Understand code coverage concept
Code coverage tracks which parts of the Verilog design are exercised during simulation.
Step 2: Identify the main goal
The goal is to ensure tests cover all design parts, improving test quality.
Final Answer:
To check which parts of the design have been tested -> Option C
Quick Check:
Code coverage = tested parts check [OK]
Hint: Code coverage shows tested design parts, not speed or layout [OK]
Common Mistakes:
Confusing coverage with simulation speed
Thinking coverage generates synthesis reports
Assuming coverage optimizes hardware layout
2. Which SystemVerilog construct is used to define a coverage point in code coverage?
easy
A. coverpoint
B. assertion
C. covergroup
D. property
Solution
Step 1: Recall coverage constructs
SystemVerilog uses covergroup to group coverage points and coverpoint to define individual coverage points.
Step 2: Identify the coverage point
The coverage point is specifically defined by coverpoint, inside a covergroup.