整型
2状态整型(0,1)
类型 | 字长 |
---|---|
shortint | 16-bit signed |
int | 32-bit signed |
longint | 64-bit signed |
byte | 8-bit signed |
bit | 用户自定义向量 |
4状态整型(0,1,x,z)
类型 | 字长 |
---|---|
logic | 用户自定义向量 |
reg | 用户自定义向量 |
wire | 用户自定义向量 |
integer | 32-bit signed |
time | 64-bit unsigned |
void、none
chandle(储存指针)
字符串(string)
事件(event)
module events();
// Declare a new event called ack
event ack;
// Declare done as alias to ack
event done = ack;
// Event variable with no synchronization object
event empty = null;
initial begin
#1 -> ack;
#1 -> empty;
#1 -> done;
#1 $finish;
end
always @ (ack)
begin
$display("ack event emitted");
end
always @ (done)
begin
$display("done event emitted");
end
/*
always @ (empty)
begin
$display("empty event emitted");
end
*/
endmodule
输出
ack event emitted
done event emitted
ack event emitted
done event emitted
用户自定义类型(User defined types)
`timescale 1ns/10ps
// Type define a struct
typedef struct {
byte a;
reg b;
shortint unsigned c;
} myStruct;
module typedef_data ();
// Full typedef here
typedef integer myinteger;
// Typedef declaration without type
typedef myinteger;
// Typedef used here
myinteger a = 10;
myStruct object = '{10,0,100};
initial begin
$display ("a = %d", a);
$display ("Displaying object");
$display ("a = %b b = %b c = %h", object.a, object.b, object.c);
#1 $finish;
end
endmodule
枚举(enum)
module enum_data();
enum integer {IDLE=0, GNT0=1, GNT1=2} state;
enum {RED,GREEN,ORANGE} color;
enum {BRONZE=4, SILVER, GOLD} medal;
// a=0, b=7, c=8
enum {a, b=7, c} alphabet;
// Width declaration
enum bit [3:0] {bronze='h1, silver, gold='h5} newMedal;
// Using enum in typedef
typedef enum { red, green, blue, yellow, white, black } Colors;
Colors Lcolors;
initial begin
state = IDLE;
color = RED;
medal = BRONZE;
alphabet = c;
newMedal = silver;
Lcolors = yellow;
$display (" state = %0d", state);
$display (" color = %s", color.name());
$display (" medal = %s", medal.name());
$display (" alphabet = %s", alphabet.name());
$display (" newMedal = %s", newMedal.name());
$display (" Lcolors = %s", Lcolors.name());
end
endmodule
输出
state = 0
color = RED
medal = BRONZE
alphabet = c
newMedal = silver
Lcolors = yellow
结构体(structure)和共用体(union)
类(class)
module class_data();
// Class with local fields
class Packet;
int address;
bit [63:0] data;
shortint crc;
endclass:Packet
// Class with task
class print;
task print_io (input string msg);
$display("%s",msg);
endtask:print_io
endclass:print
// Create instance
Packet p;
print prn;
initial begin
// Allocate memory
p = new();
prn = new();
// Assign values
p.address = 32'hDEAD_BEAF;
p.data = {4{16'h55AA}};
p.crc = 0;
// Print all the assigned values
$display("p.address = %d p.data = %h p.crc = %d",
p.address, p.data, p.crc);
prn.print_io("Test calling task inside class");
$finish;
end
endmodule
输出
p.address = -559038801 p.data = 55aa55aa55aa55aa p.crc = 0
Test calling task inside class
类型转换(cast)
数组(Array)
pack与unpack数组
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
endmodule
动态数组
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 arrays)
module integer_associative_array ();
integer as_mem [integer];
integer i;
initial begin
// Add element array
as_mem[100] = 101;
$display ("value stored in 100 is %d", 101);
as_mem[1] = 100;
$display ("value stored in 1 is %d", 100);
as_mem[50] = 99;
$display ("value stored in 50 is %d", 99);
as_mem[256] = 77;
$display ("value stored in 256 is %d", 77);
// Print the size of array
$display ("size of array is %d", as_mem.num());
// Check if index 2 exists
$display ("index 2 exists %d", as_mem.exists(2));
// Check if index 100 exists
$display ("index 100 exists %d", as_mem.exists(100));
// Value stored in first index
if (as_mem.first(i)) begin
$display ("value at first index %d value %d", i, as_mem[i]);
end
// Value stored in last index
if (as_mem.last(i)) begin
$display ("value at last index %d value %d", i, as_mem[i]);
end
// Delete the first index
as_mem.delete(100);
$display ("Deleted index 100");
// Value stored in first index
if (as_mem.first(i)) begin
$display ("value at first index %d value %d", i, as_mem[i]);
end
#1 $finish;
end
endmodule
队列(Queues)
dule 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