SystemVerilog OOP Mastery · All levels
Static Members and Static Methods in Class-Based Testbenches: Exercises
Exercises for Static Members and Static Methods in Class-Based Testbenches.
Practice exercises
Exercise 1
Add a static function snapshot_count() to txn that returns how many objects were created so far.
Solution
diagram
Implement static function int snapshot_count(); return next_id; endfunction. Because next_id increments in new(), it tracks created count.Exercise 2
Write a reset sequence step that avoids stale static counters before traffic starts.
Solution
diagram
Call txn#(my_item_t)::reset_ids() in build/setup before any object creation. Keep this in one deterministic phase so all tests follow the same reset policy.