MODULE 3 B
MODULE 3 B
• Nets come in many flavors, but most designers use scalar and
vector wires to connect together the ports of design blocks.
• System Verilog adds many new data types to help both hardware
designers and verification engineers.
The Logic Type
A logic signal can be used anywhere a net is used, except
that a logic variable cannot be driven by multiple structural
drivers, such as when you are modeling a bidirectional bus.
• The System Verilog function $size returns the size of the array.
• In the for each-Ioop, you specify the array name and an index in
square brackets, and System Verilog automatically steps through all
the elements of the array.
• This means that you do not specify the array size at compile
time; instead, you give it at run-time.
• The array is initially empty, and so you must call the new [ ]
constructor to allocate space, passing in the number of entries
in the square brackets.
Like an array, you can directly access any element with an index,
without linked list's overhead of stepping through the preceding
elements.
A queue is declared with word subscripts containing a dollar
sign: [$].
Sample shows how you can add and remove values from a
queue using methods.
Note that queue literals only have curly braces, and are
missing the initial apostrophe of array literals.
The System Verilog queue is similar to the Standard Template
Library's deque data type.
• This takes a fixed amount of time no matter how large the queue.
• The time to do this grows linearly with the size of the queue.
• You can copy the contents ofa fixed or dynamic array into a
queue.
Associative Arrays
• SystemVerilog offers associative arrays that store entries in a
sparse matrix.
• This means that while you can address a very large address
space, SystemVerilog only allocates memory for an element
when you write to it.
Here are some guidelines for choosing the right storage type based on
flexibility,
memory usage,
These are just rules of thumb, and results may vary between
simulators.
Flexibility
• Use a fixed-size or dynamic array if it is accessed with consecutive
positive integer indices: 0, 1, 2, 3…
• For only a few reads and writes, you could use any type, as the overhead is
minor compared with the DUT.
• When reading and writing associative arrays, the simulator must search
for the element in memory.
• Popular ways are hash tables and trees, these requires more computation
than other arrays, and therefore associative arrays are the slowest.
Sorting
• System Verilog can sort any single-dimension array (fixed- size, dynamic, and
associative arrays plus queues), you should pick based on how often the data
is added to the array.
• If the data slowly dribbles in, choose a queue, as adding new elements to the
head or tail is very efficient.
• Using the routines first, next, and prev, you can search an associative array
for a value and find successive values.
Choosing the best data structure
Here are some suggestions on choosing a data structure
• Network packets.
Properties: fixed size, accessed sequentially.
Use a fixed-size or dynamic array for fixed- or variable-size packets.
Unions are useful when you frequently need to read and write a
register in several different formats.
Type Conversion
• The proliferation of data types in Systemverilog means that you
will need to convert between them.
• If the bit layouts differ, such as an array of bytes and words, use
the streaming operators to rearrange the bits.
The Static Cast
The static cast operation converts between two types with no
checking of values.
You specify the destination type, an apostrophe, and the
expression to be converted as shown
The Dynamic Cast
The dynamic cast, $cast, allows you to check for out-of-bounds values.
Streaming Operators :
• When used on the right side of an assignment, the streaming
operators « and» take an expression, structure, or array, and packs
it into a stream of bits.
For example, the names ADD, MOVE, or ROTW make your code
easier to write and maintain than if you had used literals such as
8'h01 or macros.
This creates an anonymous enumerated type, but it cannot be used for any
other variables than the ones in this declaration.
You first create the enumerated type, and then the variables of this type.
Constants
• There are several types of constants in System verilog.
•
• The classic verilog way to create a constant is with a text macro.
• On the plus side, macros have global scope and can be used for bit
field definitions and type definitions.
• On the negative side, macros are global, so that they can cause
conflicts if you just need a local constant.
initial begin
const byte colon = “ : ” ;
…
end
• Addition A uses two 1-bit variables, and so with this precision 1 + 1 =0.
• Addition Buses 8-bit precision because there is an 8-bit variable on the left
side of the assignment. In this case, 1 + 1 =2.
• Addition C uses a dummy constant to force System Veri log to use 2-bit
precision.
• Lastly, in addition D, the first value is cast to be a 2-bit value with the cast
operator, and so 1 + 1 =2.
There are several tricks you can use to avoid this problem.