story · 2020年05月07日

SystemVerilog教程之Data Types Part-II

User defined types

用户定义类型与其他编程语言中的类型相同,用户可以使用typedef定义自己的数据类型。

`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

输出

a = 10
Displaying object
a = 00001010 b = 0 c = 0064


Class Types

Class是一组数据和对该数据进行操作的方法( methods)。类中的数据称为属性( properties)。类中的属性和方法一起定义了类**对象( object)**的内容和功能。

我们可以动态地对类进行创建和销毁,然后通过句柄传递。

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


Casting

与C语言中一样,Casting类型转换)用于将一种类型的数据转换为另一种类型的数据。可以使用强制转换(')操作更改数据类型。

module casting_data();
int a = 0;
shortint b = 0;
initial begin
$monitor ("%g a = %d b = %h", $time, a , b);
#1 a = int'(2.3 * 3.3);
#1 b = shortint'{8'hDE,8'hAD,8'hBE,8'hEF};
#1 $finish;
end
endmodule

输出

a = 0 b = 0000
a = 8 b = 0000
a = 8 b = beef



本文转载自公众号:芯片数字实验室
原文链接:
https://mp.weixin.qq.com/s/Yb88q5m3-C5RY\_0QbQVXcA
未经作者同意,请勿转载!

推荐阅读

想了解更多内容,欢迎关注芯片数字实验室专栏
推荐阅读
关注数
12284
内容数
203
前瞻性的眼光,和持之以恒的学习~
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息