EE5530 Lecture6 Data Types
EE5530 Lecture6 Data Types
Systemverilog 2
Data Types
Memory Read
byte b8; // 2-state, 8-bit signed integer Explicit 2-state variables allow compiler
optimizations to improve performance
shortint s; // 2-state, 16-bit signed integer
• logic has equivalent functionality to reg and can be used anywhere that a reg is traditionally
used.
module counter (output [3:0] dout, input clk, rst, cnt);
logic [3:0] dout;
always_ff @(posedge clk or posedge rst)
if (rst)
dout <= ’0;
else if (cnt)
dout <= dout + 1;
endmodule
Signed/Unsigned
byte, shortint, int, integer and longint defaults to signed
Use unsigned to represent unsigned integer value
Example: int unsigned ui;
• In packed arrays [range is on the left side of the identifier] all elements
are glued together
• Ex: logic [3:0] m;
m = 4’b1100;
• In unpacked arrays [range is on the right side of the identifier] each
individual element is considered by itself without any relation to other
elements.
• Ex: logic m [5:0];
// each element is only 1-bit deep
• Arrays can have packed and unpacked dimensions.
• Ex: logic [3:0] m [5:0]
Packed Array Example
bit [1:0] [2:0] [3:0] barray;
barray = '{'{4’h6, 4’h5, 4’h4}, '{4’h3, 4’h2, 4’h1}};
$displayh(barray,, barray[1],, barray[1][2],, barray[1][2][3]);
# 654321 654 6 0
For more control, consider the dimensions of the array and use { } to match those
dimensions exactly.
bit [31:0] src[5], dst[5]; The SystemVerilog function $size returns the
size of the array.
for (int i=0; i<$size(src); i++)
src[i] = i;
In the foreach-loop, you specify the array name
foreach (dst[j]) and an index in square brackets, and SV
dst[j] = src[j] * 2; // dst doubles src values automatically steps through all the elements of
the array.
end
The index variable is automatically declared for
you and is local to the loop.
Printing a multidimensional array
initial begin
byte twoD[4][6];
foreach(twoD[i,j])
0: 0 1 2 3 4 5
twoD[i][j] = i*10+j;
1: 10 11 12 13 14 15
2: 20 21 22 23 24 25
foreach (twoD[i]) begin // Step through first dim.
3: 30 31 32 33 34 35
$write("%2d:", i);
foreach(twoD[,j]) // Step through second
$write("%3d", twoD[i][j]);
$display;
end
end
Dynamic Arrays
Dynamic declaration of one index of an unpacked array
Syntax:
data_type array_name[] ;
Declares a dynamic array array_name of type data_type
20
Queues and Lists
SV has a built-in list mechanism which is ideal for queues, stacks, etc.
A list is basically a variable size array of any SV data type.
for (int i=0; i < q1.size; i++) // step through a list using integers (NO POINTERS)
begin … end
insert() Inserts the given item at the specified index position. Prototype:
function void insert (int index, queue_type item);
Q.insert (i, e) => Q = ‘{Q[0:i-1], e, Q[i,$]}
pop_front() Removes and returns the first element of the queue. Prototype:
function queue_type pop_front();
e = Q.pop_front () => e = Q[0]; Q = Q[1,$]
pop_back() Removes and returns the last element of the queue. Prototype:
function queue_type pop_back();
e = Q.pop_back () => e = Q[$]; Q = Q[0,$-1]
push_front() Inserts the given element at the front of the queue. Prototype:
function void push_front (queue_type item);
Q.push_front (e) => Q = ‘{e, Q}
push_back() Inserts the given element at the end of the queue. Prototype:
function void push_back (queue_type item);
Q.push_back (e) => Q = ‘{Q, e}
Queue Example
module queues ();
int q [$]; // declare the q
initial
begin: store_disp
// Push elements into the queue
q.push_back(1);
q.push_back(0);
// Display its contents
$display("Size of queue = %0d", q.size());
// Delete the element of queue at index 1
q.delete(1);
// Push to front of the queue
q.push_front (0);
// Display all the contents in the queue
for (int i = 0; i < q.size(); i++)
$display("q[%0d] = %0d", i, q[i]);
end: store_disp
endmodule: queues 23
Associative Arrays
• Associative arrays are used when the size of the array is not known
or the data is sparse.
• Syntax:
data_type array_name [index_type];
In other words
value_type array_name [key_type];
• It implements a lookup table of the elements of its declared type.
• Data type used as an index serves as lookup key and imposes an
order.
• Associative array do not have their storage allocated until it is used.
Associative Arrays
• Suppose a processor you are modeling has a 4GB memory space.
• A fixed/packed/dynamic array will take up too much memory
• Use an associative array to model sparse memories
Function Use
num() Returns number of entries
Queues work well for creating scoreboards for which you constantly need to add
and remove data.
Dynamic arrays allow you to choose the array size at run-time for maximum
testbench flexibility.
Associative arrays are used for sparse memories and some scoreboards with a
single index.
Enumerated types make your code easier to read and write by creating groups of
named constants.
Systemverilog summary so far…
– C/C++ type language constructs for efficient programming
– Interfaces to encapsulate communication between design blocks
– Assertions and coverage for new verification techniques
– System-level testbench features
– Lightweight interface to C/C++ programs