SV_4_Data Type1
摘要:System Verilog 增加了新的数据类型,也对已有的数据类型进行了优化,以提高模拟器运行时的内存使用率;
- shortint and longint data types;
- shortreal (real was already defined in verilog) data type;
- string, handle and class data type;
- logic ,bit, and byte data type;
- user defined types typedef;
- struct, union, and class types;
- void data type;
- null data type;
- arrays, queue, associative, dynamatic array;
1. integer data type
2 值的integer类型
- bit : User defined vector types.
- byte : 8-bit signed integer, can be used for storing ASCII charater.
- shortint : 16-bit signed integer.
- int : 32-bit signed integer.
- longint : 64-bit signed integer.
4 值的integer类型
- logic : User defined vector types.
- reg : User defined vector types.
- wire : User defined vector types.
- integer : 32-bit signed integer.
- time : 64-bit unsigned integer.
数值符号性可以声明为有符号或者无符号;
默认情况下:
signed:byte, shortint, int, longint,
unsigned:bit, reg, logic, wire
一个关于integer type的简单例子
1 module data_types(); 2 3 bit data_1bit; 4 byte data_8bit; 5 shortint data_16bit; 6 int data_32bit; 7 longint data_64bit; 8 integer data_integer; 9 10 bit unsigned data_1bit_unsigned; 11 byte unsigned data_8bit_unsigned; 12 shortint unsigned data_16bit_unsigned; 13 int unsigned data_32bit_unsigned; 14 longint unsigned data_64bit_unsigned; 15 integer unsigned data_integer_unsigned; 16 17 initial begin 18 data_1bit = {32{4'b1111}}; 19 data_8bit = {32{4'b1111}}; 20 data_16bit = {32{4'b1111}}; 21 data_32bit = {32{4'b1111}}; 22 data_64bit = {32{4'b1111}}; 23 data_integer= {32{4'b1111}}; 24 $display("data_1bit = %0d",data_1bit); 25 $display("data_8bit = %0d",data_8bit); 26 $display("data_16bit = %0d",data_16bit); 27 $display("data_32bit = %0d",data_32bit); 28 $display("data_64bit = %0d",data_64bit); 29 $display("data_integer = %0d",data_integer); 30 data_1bit = {32{4'bzx01}}; 31 data_8bit = {32{4'bzx01}}; 32 data_16bit = {32{4'bzx01}}; 33 data_32bit = {32{4'bzx01}}; 34 data_64bit = {32{4'bzx01}}; 35 data_integer= {32{4'bzx01}}; 36 $display("data_1bit = %b",data_1bit); 37 $display("data_8bit = %b",data_8bit); 38 $display("data_16bit = %b",data_16bit); 39 $display("data_32bit = %b",data_32bit); 40 $display("data_64bit = %b",data_64bit); 41 $display("data_integer = %b",data_integer); 42 data_1bit_unsigned = {32{4'b1111}}; 43 data_8bit_unsigned = {32{4'b1111}}; 44 data_16bit_unsigned = {32{4'b1111}}; 45 data_32bit_unsigned = {32{4'b1111}}; 46 data_64bit_unsigned = {32{4'b1111}}; 47 data_integer_unsigned = {32{4'b1111}}; 48 $display("data_1bit_unsigned = %d",data_1bit_unsigned); 49 $display("data_8bit_unsigned = %d",data_8bit_unsigned); 50 $display("data_16bit_unsigned = %d",data_16bit_unsigned); 51 $display("data_32bit_unsigned = %d",data_32bit_unsigned); 52 $display("data_64bit_unsigned = %d",data_64bit_unsigned); 53 $display("data_integer_unsigned = %d",data_integer_unsigned); 54 data_1bit_unsigned = {32{4'bzx01}}; 55 data_8bit_unsigned = {32{4'bzx01}}; 56 data_16bit_unsigned = {32{4'bzx01}}; 57 data_32bit_unsigned = {32{4'bzx01}}; 58 data_64bit_unsigned = {32{4'bzx01}}; 59 data_integer_unsigned = {32{4'bzx01}}; 60 $display("data_1bit_unsigned = %b",data_1bit_unsigned); 61 $display("data_8bit_unsigned = %b",data_8bit_unsigned); 62 $display("data_16bit_unsigned = %b",data_16bit_unsigned); 63 $display("data_32bit_unsigned = %b",data_32bit_unsigned); 64 $display("data_64bit_unsigned = %b",data_64bit_unsigned); 65 $display("data_integer_unsigned = %b",data_integer_unsigned); 66 #1 $finish; 67 end 68 69 endmodule 70 71 //compile result 72 data_1bit = 1 73 data_8bit = -1 74 data_16bit = -1 75 data_32bit = -1 76 data_64bit = -1 77 data_integer = -1 78 data_1bit = 1 79 data_8bit = 00010001 80 data_16bit = 0001000100010001 81 data_32bit = 00010001000100010001000100010001 82 data_64bit = 0001000100010001000100010001000 83 100010001000100010001000100010001 84 data_integer = zx01zx01zx01zx01zx01zx01zx01zx01 85 data_1bit_unsigned = 1 86 data_8bit_unsigned = 255 87 data_16bit_unsigned = 65535 88 data_32bit_unsigned = 4294967295 89 data_64bit_unsigned = 18446744073709551615 90 data_integer_unsigned = 4294967295 91 data_1bit_unsigned = 1 92 data_8bit_unsigned = 00010001 93 data_16bit_unsigned = 0001000100010001 94 data_32bit_unsigned = 00010001000100010001000100010001 95 data_64bit_unsigned = 0001000100010001000100010001000 96 100010001000100010001000100010001 97 data_integer_unsigned = zx01zx01zx01zx01zx01zx01zx01zx01
2. void and null data value
- void 定义一个返回值为空的函数;
- null 用于将一个变量与空值进行比较,详细将在class 部分讨论;
3. handle data type
用于存储指针,在OOP(object orientation programming)和DPI(direct programming interface)中使用。
4. string data type
string operations
|
String |
Description |
|
Str1 == Str2 |
Equality. 检查两个字符串是否相等。如果它们相等,则结果为1,否则为0。两个字符串都可以是string类型的。或者其中一个可以是字符串文字。如果两个操作对象都是字符串文字,则等同于verilog中integer类型的相等操作符。允许使用特殊值“”。 |
|
Str1 != Str2 |
Inequality. == 的逻辑否定 |
|
<,>,<=,=> |
Comparison. 如果对应的条件为真,且判断相等,则输出为1,条件支持嵌入式操作。 |
|
{Str1,Str2,...,Strn} |
Concatenation. 每个被操作元素都可以是字符串类型或者字符文字类型;当有一个元素是字符串类型时,所有其他都会被转换为字符串类型,当所有元素都是字符串文字类型时,其也会被转换为字符串类型;总之该操作带有一个隐式的转换,输出都是字符串类型。 |
|
{multiplier{Str}} |
Replication. Str 每个被操作元素都可以是字符串类型或者字符文字类型,该操作带有一个隐式的转换,输出都是字符串类型。 |
|
Str.method(...) |
The dot (.) operator is used to invoke a specified method on strings. |
|
Str[index] |
Indexing. 返回一个给定索引处的ASCII码,索引范围为0-(N-1),如果索引越界,则返回0; |
string tasks
|
Function |
Description |
|
str.len() |
返回字符串的长度。 |
|
putc(int char, FILE *stream) |
把一个无符号的字符写入到指定的流中,位置标识符向前移动; |
|
getc(int char,FILE *stream) |
从指定的流中获取一个无符号的字符,位置标识符向后移动; |
|
str.toupper() |
返回字符串的大写形式 |
|
str.tolower() |
返回字符串的小写形式 |
|
compare(str1,str2) |
返回字符串对比结果 |
|
str1.icompare(str2) |
返回不关心大小写的字符串对比结果 |
|
str.substr(start,length) |
返回一个指定开始指定长度的字符串 |
|
str.atoi() |
返回字符串的32进制 |
|
stratohex() |
返回字符串的16进制 |
|
stratooct() |
返回字符串的8进制 |
|
stratobin() |
返回字符串的2进制 |
|
stratoreal() |
返回字符串表示的实数 |
|
itoa(i) |
返回32进制i的字符串(inverse of atohex) |
|
hextoa() |
返回16进制i的字符串 (inverse of atohex). |
|
octtoa() |
返回8进制i的字符串 (inverse of atooct). |
|
bintoa() |
返回2进制i的字符串 (inverse of atobin). |
|
realtoa() |
返回实数i的字符串 (inverse of atoreal). |
一个例子
1 module string_ex (); 2 3 string my_string = "This is a orginal string"; 4 string my_new_string; 5 6 initial begin 7 $display ("My String = %s",my_string); 8 // Assign new string of different size 9 my_string = "This is new string of different length"; 10 $display ("My String = %s",my_string); 11 // Change to uppercase and assign to new string 12 my_new_string = my_string.toupper(); 13 $display ("My New String = %s",my_new_string); 14 // Get the length of sting 15 $display ("Length of new string %0d",my_new_string.len()); 16 // Compare variable to another variable 17 if (my_string.tolower() == my_new_string.tolower()) begin 18 $display("String Compare matches"); 19 end 20 // Compare variable to variable 21 if (my_string.toupper() == my_new_string) begin 22 $display("String Variable Compare matches"); 23 end 24 #1 $finish; 25 end 26 27 endmodule 28 29 //compile result 30 My String = This is a orginal string 31 My String = This is new string of different length 32 My New String = THIS IS NEW STRING OF DIFFERENT LENGTH 33 Length of new string 38 34 String Compare matches 35 String Variable Compare matches
5. event data type
- 基本上与verilog中的event相同,在SV中event变量之间可以赋值,相当于取别名;
- 可以被always块触发,
一个例子
1 module events(); 2 // Declare a new event called ack 3 event ack; 4 // Declare done as alias to ack 5 event done = ack; 6 // Event variable with no synchronization object 7 event empty = null; 8 9 initial begin 10 #1 -> ack; 11 #1 -> empty; 12 #1 -> done; 13 #1 $finish; 14 end 15 16 always @ (ack) 17 begin 18 $display("ack event emitted"); 19 end 20 21 always @ (done) 22 begin 23 $display("done event emitted"); 24 end 25 //no synchronization object 26 always @ (empty) 27 begin 28 $display("empty event emitted"); 29 end 30 31 endmodule 32 33 //compile result 34 ack event emitted 35 done event emitted 36 ack event emitted 37 done event emitted event_code
6. user defined type
用户定义的类型可以在定义之前使用,前提是先被定义为空的typedef。
相关语法:
- typedef data_type type_identifier variable_dimension;
- typedef interface_instance_identifier . type_identifier;
- typedef [ enum | struct | union | class ] type_identifier;
例子:
1 `timescale 1ns/10ps 2 3 // Type define a struct 4 typedef struct { 5 byte a; 6 reg b; 7 shortint unsigned c; 8 } myStruct; 9 10 module typedef_data (); 11 12 // Full typedef here 13 typedef integer myinteger; 14 15 // Typedef declaration without type 16 typedef myinteger; 17 // Typedef used here 18 myinteger a = 10; 19 myStruct object = '{10,0,100}; 20 21 initial begin 22 $display ("a = %d", a); 23 $display ("Displaying object"); 24 $display ("a = %b b = %b c = %h", object.a, object.b, object.c); 25 #1 $finish; 26 end 27 28 endmodule 29 30 //compile result 31 a = 10 32 Displaying object 33 a = 00001010 b = 0 c = 0064
7. enumerations types
- 枚举类型对没有数据类型或数据值的强类型变量提供了一个抽象定义;
- 还可以使用枚举变量的名字引用枚举类型的数据;
- 默认的数据类型为int。
语法:
- enum [ enum_base_type ] { enum_name_declaration { , enum_name_declaration } }
- enum_base_type ::= integer_atom_type [ signing ] | integer_vector_type [ signing ] [ packed_dimension ] | type_identifier [ packed_dimension ]
- enum_name_declaration ::= enum_identifier [ [ integral_number [ : integral_number ] ] ] [ = constant_expression ]
例子:
1 module enum_data(); 2 3 enum integer {IDLE=0, GNT0=1, GNT1=2} state; 4 enum {RED,GREEN,ORANGE} color; 5 enum {BRONZE=4, SILVER, GOLD} medal; 6 7 // a=0, b=7, c=8 8 enum {a, b=7, c} alphabet; 9 // Width declaration 10 enum bit [3:0] {bronze='h1, silver, gold='h5} newMedal; 11 // Using enum in typedef 12 typedef enum { red, green, blue, yellow, white, black } Colors; 13 14 Colors Lcolors; 15 16 initial begin 17 state = IDLE; 18 color = RED; 19 medal = BRONZE; 20 alphabet = c; 21 newMedal = silver; 22 Lcolors = yellow; 23 $display (" state = %0d", state); 24 $display (" color = %s", color.name()); 25 $display (" medal = %s", medal.name()); 26 $display (" alphabet = %s", alphabet.name()); 27 $display (" newMedal = %s", newMedal.name()); 28 $display (" Lcolors = %s", Lcolors.name()); 29 end 30 31 endmodule 32 33 //compile result 34 state = 0 35 color = RED 36 medal = BRONZE 37 alphabet = c 38 newMedal = silver 39 Lcolors = yellow
1 module enum_data(); 2 3 enum integer {IDLE=0, GNT0=1, GNT1=2} state; 4 enum {RED,GREEN,ORANGE} color; 5 enum {BRONZE=4, SILVER, GOLD} medal; 6 7 // a=0, b=7, c=8 8 enum {a, b=7, c} alphabet; 9 // Width declaration 10 enum bit [3:0] {bronze='h1, silver, gold='h5} newMedal; 11 // Using enum in typedef 12 typedef enum { red, green, blue, yellow, white, black } Colors; 13 14 Colors Lcolors; 15 16 initial begin 17 state = IDLE; 18 color = RED; 19 medal = BRONZE; 20 alphabet = c; 21 newMedal = silver; 22 Lcolors = yellow; 23 $display (" state = %0d", state); 24 $display (" color = %s", color.name()); 25 $display (" medal = %s", medal.name()); 26 $display (" alphabet = %s", alphabet.name()); 27 $display (" newMedal = %s", newMedal.name()); 28 $display (" Lcolors = %s", Lcolors.name()); 29 end 30 31 endmodule 32 33 //compile result 34 state = 0 35 color = RED 36 medal = BRONZE 37 alphabet = c 38 newMedal = silver 39 Lcolors = yellow
8. class type
- 类是一个包含数据以及对数据进行操作的方法的子例程集合;
- 数据(data)被称为类的属性;
- 子例程(subroutines)被称为类的方法;
- 类属性和类方法共同定义类实例或对象的内容和功能;
- 面向对象的类扩展允许动态的创建和销毁对象;
- 类实例或对象用句柄来进行传递参数;
一个例子:
1 module class_data(); 2 3 // Class with local fields 4 class Packet; 5 int address; 6 bit [63:0] data; 7 shortint crc; 8 endclass:Packet 9 10 // Class with task 11 class print; 12 task print_io (input string msg); 13 $display("%s",msg); 14 endtask:print_io 15 endclass:print 16 17 // Create instance 18 Packet p; 19 print prn; 20 21 initial begin 22 // Allocate memory 23 p = new(); 24 prn = new(); 25 // Assign values 26 p.address = 32'hDEAD_BEAF; 27 p.data = {4{16'h55AA}}; 28 p.crc = 0; 29 // Print all the assigned values 30 $display("p.address = %d p.data = %h p.crc = %d", 31 p.address, p.data, p.crc); 32 prn.print_io("Test calling task inside class"); 33 $finish; 34 end 35 36 endmodule 37 38 //compile result 39 40 p.address = -559038801 p.data = 55aa55aa55aa55aa p.crc = 0 41 Test calling task inside class
9.Casting(类型转换)
- 数据转换时使用 ' 操作符;
- 要转换的表达式必须在()、[]、{}、内;
例子:
1 module casting_data(); 2 3 int a = 0; 4 shortint b = 0; 5 6 initial begin 7 $monitor ("%g a = %d b = %h", $time, a , b); 8 #1 a = int'(2.3 * 3.3); 9 #1 b = shortint'{8'hDE,8'hAD,8'hBE,8'hEF}; 10 #1 $finish; 11 end 12 13 endmodule 14 15 //compile result 16 0 a = 0 b = 0000 17 1 a = 8 b = 0000 18 2 a = 8 b = beef
浙公网安备 33010602011771号