SystemVerilog OOP Mastery · All levels

Polymorphism and Dispatch Semantics: Exercises

Exercises for Polymorphism and Dispatch Semantics.

Practice exercises

Exercise 1

Build a base class protocol_monitor with virtual task sample(); create i2c_monitor and spi_monitor and run both from a single queue.

Solution

diagram
Declare sample as virtual in protocol_monitor; push new i2c_monitor and spi_monitor into a queue of protocol_monitor handles; iterate and call sample for dynamic dispatch.

Exercise 2

Write a function that accepts txn_base and prints write data only when the object is write_txn.

Solution

diagram
Inside the function, declare write_txn w; use if ($cast(w, t)) to gate access to w.data; otherwise print a generic base summary.

Related topics