System Verilog - data type

Posted by teamfox20 on Tue, 15 Feb 2022 12:32:26 +0100

1, Built in data type

Four valued logic typeBinary logic type
logic integer reg net-typebyte int longint shortint bit
Signed typeUnsigned type
byte integer int longint shortintlogic bit reg net-type

Tips:
(1) The default value returned by four value status is x, and the default value returned by two value status is 0.
(2) logic type can only have one driver.
(3) The output of the network is z when there is no driver.

2, Array

1. Fixed width array

Declaration of fixed width array

int array_lo_hi[0:15];//16 integers [0] [15]
int array_c_style[16];//16 integers [0] [15]

Declaration of multidimensional array

int array1 [0:7][0:3];//Complete declaration
int array2 [8][4];//Compact statement
array1[7][3] = 1;//Set last element

Initialization of constant array

int ascend[4] = '{0, 1, 2, 3};//Initialize four elements
int descend[5];

descend = '{4, 3, 2, 1, 0};//Assign values to five elements
descend[0:2] = '{5, 6,  7};//Assign values to the first three elements
ascend = '{4{8}};//Assign all four values to 8
ascend = '{9, 8, default:1};//The assignment is {9,8,1,1,1}

Basic array operations
<1> foreach
Syntax format of multidimensional array traversal

int md[2][3] = '{ '{0, 1, 2}, '{3, 4, 5}};
foreach(md[i, j])
	$display("md[%0d][%0d]=%0d", i, j, md[i, j]);

For array f[0:4], foreach(f[i]) is equivalent to

for(int i=0;i<=4;i++)

For array rev[6:2], foreach(rev[i]) is equivalent to

for(int i=6;i>=2; i--)

<2> Copy and compare

dst == src;//Copy all elements of src to dst

Use the "?:" operator for comparison,
Such as A?B:C, judge that A and A are true, execute B, otherwise execute C.

<3> Use of merged and non merged arrays

Declaration of merged / unmerged mixed arrays

bit[3:0][7:0]barray[3];//A non merged array with three merged elements, 3x32 bits
bit[31:0]lw = 32'h0123_4567;//One word
bit[7:0][3:0]nibbles;//Merge array

barray[0] = lw;//Using a subscript, you can get a word of data
barray[0][3] = 8'h01;//Using two subscripts, you can get one byte of data
barray[0][1][6] = 1'b1;//Using three subscripts, a single bit can be accessed
nibbles = barray[0];//Copy the element values of the merged array

Tips:
(1) When declaring a merged array, the merged bits and array size must be specified before the variable name as part of the data type. The format of array size definition must be [msb:lsb], not [size].
(2) The array declaration specifies the array size barray[3] after the variable name. This dimension is non merged, so there must be at least one subscript when using the array.

2. Dynamic array

(1) The width of a dynamic array is not given at compile time like a fixed width array, but is specified at program run time.
(2) The array is empty at the beginning. You need to call new [] to allocate space, and pass the array width in square brackets.
(3) As long as the basic data types are the same, such as int, fixed width array and dynamic array can be assigned to each other. When the number of elements is the same, the value of dynamic array can be copied to fixed width array.

int d1[],d2[];//Declare dynamic array

initial begin
	d1 = new[5];//Assigned to d1 five elements
	foreach(d1[i])	d1[i] = i;//Initialize element
	d2 = d1;//Copy a dynamic array
	d2[0] = 5;//Modify copy value
	$display(d1[0], d2[0]);//Display values (0 and 5)
	d1 = new[20](d1);//Assign 20 integer values and copy the original d1 array to the first five elements
	d1 = new[100];//Assign 100 new integer values, old values no longer exist
	d1.delete();//Delete all elements
end

3. Associative array

Associative array can be used to store the elements of sparse matrix. When you address a very large address space, the array only allocates space for the actually written elements, so that the occupied space is very small.

initial begin
	bit[63:0]assoc[bit[63:0]], idx = 1;
	//Initialize sparsely distributed elements
	repeat(64) begin
		assoc[idx] = idx;
		idx = idx<<1;
	end
	//Traversing arrays using foreach
	foreach(assoc[i])
		$display("assoc[%h] = %h", i, assoc[i]);
end
	

4. Array method

  1. Array reduction method (reducing an array to a value)
    Sum, sum all elements in the array.
    In addition, there are product, and, or, xor.
  2. Array positioning method
//Array positioning method min, max, unique
int f[6] = '{1, 6, 2, 6, 8, 6};
int d[ ] = '{2, 4, 6, 8, 10};
int q[$] = '{1, 3, 5, 7}, tq[$];

tq = q.min();//{1}
tq = d.max();//{10}
tq = f.unique();//{1, 6, 2, 8}
//Array location method find
int d[ ] = '{9, 1, 8, 3, 4, 4}, tq[$];
//Find all elements greater than 3
tq = d.find with(item >3);//{9, 8, 4, 4}
//Equivalent code
foreach (d[i])
	if (d[i])
		tq.push_back(d[i]);

tq = d.find_index with(item >3);//{0, 2, 4, 5}, (i.e. the index of 9, 8, 4, 4)
tq = d.find_first with(item >99);//{} not found
tq = d.find_first_index with(item == 8);//{2} d[2] = 8


//Array positioning methods count and total
int count, total, d[ ] = '{9, 1, 8, 3, 4, 4};
count = d.sum with(item > 7);//2    {9, 8}
total = d.sum with((item > 7) * item);//17   1*9+1*8
count = d.sum with(item < 8);//4   {1, 3, 4, 4}
total = d.sum with(item < 8 ? item :0);//12   1+3+4+4
count = d.sum with(item == 4);//2     {4, 4}
  1. Sorting of arrays
int d[ ] = '{9, 1, 8, 3, 4, 4};
d.reverse();//Reverse {4, 4, 3, 8, 1, 9}
d.sort();//Positive order {1, 3, 4, 4, 8, 9}
d.rsort();//Reverse order {9, 8, 4, 4, 3, 1}
d.shuffle();//Shuffle {9, 4, 3, 8, 1, 4}

3, Queue

  1. Combining the advantages of linked list and array, elements can be added or deleted anywhere else, and any element can be accessed through index.
  2. The declaration of the queue uses the subscript [$], and the label of the queue element ranges from 0 to $. (subscript concatenation can be used, [$: 2] stands for [0:2], and [1: $] stands for [1:2].)
  3. The queue does not need new [] to create space. It only needs to use its method to increase or decrease elements. At the beginning, its space is 0
  4. Queues can be pushed_ Back () and pop_front() to implement FIFO.
int j = 1;
q2[$] = {3, 4};//The constant of the queue does not need to use ''
q[$] = {0, 2, 5};//{0, 2, 5}
initial begin
	q.insert(1, j);//{0, 1, 2, 5} insert 1 before 2
	q.insert(3, q2);//{0, 1, 2, 3, 4, 5} insert a queue in q
	q.delete(1);//{0, 2, 3, 4, 5} delete the first element
	q.push_front(6);//{6, 0, 2, 3, 4, 5} is inserted in front of the queue
	j = q.pop_back();//{6, 0, 2, 3, 4}  j = 5
	q.push_back(8);//{6, 0, 2, 3, 4, 8} insert at the end of the queue
	j = q.pop_front();//{0, 2, 3 ,4, 8}  j = 6
	foreach(q[i])
		$display(q[i]);//Print entire queue
	q.delete();//Delete entire queue
end

4, String

String method
getc (N): returns the byte at position N.
toupper: returns a string with all uppercase characters.
Tower: returns a string with all characters in lowercase.
{}: used to concatenate strings.
putc (M, C): write byte C to position M of the string.
substr(start, end): extract all characters from the position start to end.

string s;
initial begin
	s = "IEEE ";
	$display(s.getc(0));//73('I')
	$display(s.tolower());//ieee
	s.putc(s.len()-1, "-");//Change space to '-'
	s = {s, "P1800"};//"IEEE-P1800"
	$display(s.substr(2, 5));//EE-P
end

Topics: systemverilog