4.13.1 How big or small should my class be? Just as you may want to split up classes that are too big, you should also have a lower limit on how small a class should be. A class with just one or two members makes the code harder to understand as it adds an extra layer of hierarchy and forces you to constantly jump back and forth between the parent class and all the children to understand what it does. In addition, look at how often it is
Chapter 4: Basic OOP 87 used. If a small class is only instantiated once, you might want to merge it into the parent class. One Synopsys customer put each transaction variable into its own class for fine control of randomization. The transaction had a separate object for the address, CRC, data, etc. In the end, this approach only made the class hierar- chy more complex. On the next project they flattened the hierarchy. See section 8.5 for more ideas on partitioning classes. 4.13.2 Compilation order issue Sometimes you need to compile a class that includes another class that is not yet defined. The declaration of the included class’s handle causes an error, as the compiler does not recognize the new type. Declare the class name with a typedef statement, as shown below. Example 4-20 Using a typedef class statement typedef class Statistics; // Define lower level class class BusTran; Statistics stats; // Use Statistics class ... endclass class Statistics; // Define Statistics class ... endclass 4.14 Understanding Dynamic Objects In a statically allocated language such as Verilog, every piece of data usu- ally has a variable associated with it. For example, there may be a wire called grant , the integer count , and a module instance i1 . In OOP, there is not the same one-to-one correspondence. There can be many objects, but only a few named handles. A testbench may allocate a thousand transaction objects dur- ing a simulation, but may only have a few handles to manipulate them. This situation takes some getting used to if you have only written Verilog code. In reality, there is a handle pointing to every object. Some handles may be stored in arrays or queues, or in another object, like a linked list. For objects stored in a mailbox, the handle is in an internal SystemVerilog structure. See section 7.6 for more information on mailboxes.
SystemVerilog for Verification 88 4.14.1 Passing objects to routines What happens when you pass an object into a routine? Perhaps the routine only needs to read the values in the object, such as transmit above. Or, your routine may modify the object, like a routine to create a packet. Either way, when you call the routine, you pass a handle to the object, not the object itself. Figure 4-4 Handles and objects across routines In Figure 4-4, the generator task has just called transmit . There are two handles, generator.b and transmit.btrans , that both refer to the same object.
You've reached the end of your free preview.
Want to read all 326 pages?
- Spring '14
- Object-Oriented Programming, SystemVerilog, Hardware verification languages