Académique Documents
Professionnel Documents
Culture Documents
signed:
reg ,bit
unsigned:
integer, time ,byte, int, short_int,long_int
Data types:
Data types in Verilog are divided in to nets and registers. wire, tri, wor, trior,
wand, triand, tri0, tri1, supply0, supply1 and trireg are net data types. reg, integer,
time and real are register data types.
wire:
wire is the most widely used net data type. It represents 1 bit interconnecting
wire. If you use a signal to connect two modules in verilog code without declaring, it
will be considered as wire by default. Default value of an un-initialized wire is 'z'.
reg:
If a value is assigned to reg type of signal, value will retain until a new value is
assigned, It just means that a signal that holds the value. Default value of an un-
initialized reg is 'x'.
integer, time and real are other register type of datatypes. integer is a signed
variable of 32 bits, time is unsigned integer, 64 bits and real is double precision
floating point variable.
logic:
A logic signal can be used any where a net and reg is used, except that a logic
variable can not be driven by multiple structural drivers, such as when you are
modeling a bidirectional bus. In this case, the variable needs to be a net-type such as
wire.
Example:
module data_types();
bit data_1bit;
byte data_8bit;
shortint data_16bit;
int data_32bit;
longint data_64bit;
integer data_integer;
initial
begin
data_1bit = 1'b0;
data_8bit = 8'h54;
data_16bit = 16'h5467;
data_32bit = 32'h67ab_9854;
data_64bit = 64'haaaa_aaaa_bbbb_4353;
data_integer = 32'habcd ;
end
endmodule
Arrays:
Fixed_sized arrays:
SystemVerilog uses the term packed array to refer to the dimensions declared
before the object name (what Verilog-2001 refers to as the vector width). The
term unpacked array is used to refer to the dimensions declared after the object
name.
Packed_unpacked array:
module packed_unpacked_data();
// packed array
bit [7:0] packed_array = 8'hAA;
// unpacked array
reg unpacked_array [7:0] = '{0,0,0,0,0,0,0,1};
initial begin
$display ("packed array[0] = %b", packed_array[0]);
$display ("unpacked array[0] = %b", unpacked_array[0]);
$display ("packed array = %b", packed_array);
// Below one is wrong syntax
//$display("unpacked array[0] = %b",unpacked_array);
#1 $finish;
end
Multi-dimension array
module arrays_data();
// 2 dimension array of Verilog 2001
reg [7:0] mem [0:3] = '{8'h0,8'h1,8'h2,8'h3};
initial begin
$display ("mem[0] = %b", mem[0]);
$display ("mem[1][0] = %b", mem[1][0]);
$display ("mem1[0][1] = %b", mem1[0][1]);
$display ("mem1[1][1] = %b", mem1[1][1]);
#1 $finish;
end
initial
begin
foreach(mem1[i,j])
$display(" mem1 [%0d] [%0d] %d",i,j,mem1[i][j]);
foreach(mem2[i,j])
$display(" mem2 [%0h] [%0h] %h",i,j,mem2[i][j]);
end
endmodule
Dynamic array:
Dynamic arrays are useful for dealing with contiguous collections of variables
whose number changes dynamically
module dynamic_array_data();
// Declare dynamic array
reg [7:0] mem [];
initial begin
// Allocate array for 4 locations
$display ("Setting array size to 4");
mem = new[4];
$display("Initial the array with default values");
for (int i = 0; i < 4; i ++) begin
mem[i] = i;
end
// Doubling the size of array, with old content still valid
mem = new[8] (mem);
// Print current size
$display ("Current array size is %d",mem.size());
for (int i = 0; i < 4; i ++) begin
$display ("Value at location %g is %d ", i, mem[i]);
end
// Delete array
$display ("Deleting the array");
mem.delete();
$display ("Current array size is %d",mem.size());
#1 $finish;
end
endmodule
Associative array:
When the size of the collection is unknown or the data space is sparse, an
associative array is a better option. Associative arrays do not have any storage
allocated until it is used, and the index expression is not restricted to integral
expressions, but can be of any type. diffrerent methods are allowing by associative
array num(), size(), delete(), exists(), first(), last(), next(), prev().
queues:
Queues and dynamic arrays have the same assignment and argument passing
semantics. A Queue is analogous to one dimensional unpacked array that grows and
shrinks automatically. Queues can be used to model a last in, first out buffer or first
in, first out buffer. Queues support insertion and deletion of elements from random
locations using an index.
In addition to the array operators, queues provide several built-in methods. They
are: size(), insert(), delete(), pop_front, pop_back, push_front(), push_back.
module queue_data();
// Queue is declated with $ in array size
integer queue[$] = { 0, 1, 2, 3, 4 };
integer i;
initial begin
$display ("Initial value of queue");
print_queue;
// Insert new element at begin of queue
queue = {5, queue};
$display ("new element added using concate");
print_queue;
// Insert using method at begining
queue.push_front(6);
$display ("new element added using push_front");
print_queue;
// Insert using method at end
queue.push_back(7);
$display ("new element added using push_back");
print_queue;
// Using insert to insert, here 4 is index
// and 8 is value
queue.insert(4,8);
$display ("new element added using insert(index,value)");
print_queue;
// get first queue element method at begining
i = queue.pop_front();
$display ("element poped using pop_front");
print_queue;
// get last queue element method at end
i = queue.pop_back();
$display ("element poped using pop_end");
print_queue;
// Use delete method to delete element at index 4 in queue
queue.delete(4);
$display ("deleted element at index 4");
print_queue;
#1 $finish;
end
task print_queue;
integer i;
$write("Queue contains ");
for (i = 0; i < queue.size(); i ++) begin
$write (" %g", queue[i]);
end
$write("\n");
endtask
endmodule
Array methods:
SystemVerilog provides following build in methods to help with usage of
arrays. This methods can be used with fixed, dynamic, queues, and associative
arrays.
arry reduction method: sum ,size
Array Locator Methods: min,max,unique
They are sum, product, and, or, xor, min, max, unique.
module array_methods();
int data [0:9] = '{1,2,3,6,5,7,8,9,9,2};
int queue [$];
initial
begin
queue = data.min;
$display("Min size element is %0d",queue.pop_front());
queue = data.max;
$display("Max size element is %0d",queue.pop_front());
$display("Sum of array %0d",data.sum);
$display("Product of array %0d",data.product);
$display("XOR of array %0d",data.xor);
$display("AND of array %0d",data.and);
$display("OR of array %0d",data.or);
end
endmodule
strings:
module string_ex ();
initial begin
$display ("My String = %s",my_string);
// Assign new string of different size
my_string = "This is new string of different length";
$display ("My String = %s",my_string);
// Change to uppercase and assign to new string
my_new_string = my_string.toupper();
$display ("My New String = %s",my_new_string);
// Get the length of sting
$display ("Length of new string %0d",my_new_string.len());
// Compare variable to another variable
if (my_string.tolower() == my_new_string.tolower()) begin
$display("String Compare matches");
end
// Compare variable to variable
if (my_string.toupper() == my_new_string) begin
$display("String Variable Compare matches");
end
#1 $finish;
end
endmodule
struct:
which is nothing but a collection of other data type
enum:
enumerated type automatically gives a unique value
to every name in the list.
Their global scope is too
broad,
make your code easier to write and maintain