SV——类型转换$cast
0. 介绍
在SV中类型转换有很多,在这里先将类型转换分成两种,静态类型转换和动态转换。
静态转换就是用cast operator——单引号(‘)。
动态转换用$cast。
1. 静态转换 static cast(’)
语法如下:
constant_cast ::= // from A.8.4(来自1800) casting_type ' ( constant_expression )
如果casting_type和表达式类型相同,那么会返回casting_type类型的值给到左值。如果类型不匹配,也会强行转换(string类型也会转换成int类型),但会出现问题:
-
casting_type是枚举类型,转换可能出界。
typedef enum {RED,BLUE,GREEN} color_e; color_e color; int c; initial begin c=10; color=color_e'(c); end
上面的转换会成功,color的值变成10,但这除了color_e这个枚举类型的界限了。
typedef enum {RED,BLUE,GREEN} COLOR_E; COLOR_E color; int c; string str; initial begin str="hh"; c=int'(str); $display("c is %0d",c); c=1; color=COLOR_E'(c); $display("Color is %d / %s",color,color.name); c=3; color=COLOR_E'(c); $display("Color is %d / %s",color,color.name); end // 输出 c is 26728 Color is 1 / BLUE Color is 3 /
从上面输出可以看出string类型转换成了int值;第二个color越界了。
-
casting_type是bit_stream type。
比特流类型先不整理了,没遇到过,在1800 6.24.3节,如果以后遇到再看吧。
2. 动态转换($cast)
上面static cast不会检查表达式的类型,可能会发生string转换成int,枚举越界等问题。
动态转换$cast可以作为function,也可以作为task(这个问题地平线一面的时候被问过,$cast是函数还是任务),语法如下:
function int $cast( singular dest_var, singular source_exp ); or task $cast( singular dest_var, singular source_exp );
Use of $cast as either a task or a function determines how invalid assignments are handled.
When called as a task, $cast attempts to assign the source expression to the destination variable. If the
assignment is invalid, a run-time error occurs, and the destination variable is left unchanged.
When called as a function, $cast attempts to assign the source expression to the destination variable and returns 1 if the cast is legal. If the cast fails, the function does not make the assignment and returns 0. When called as a function, no run-time error occurs, and the destination variable is left unchanged.
It is important to note that $cast performs a run-time check. No type checking is done by the compiler, except to check that the destination variable and source expression are singulars.
至于$cast是作为function还是task,主要看$cast使用的语境是否需要返回值。
$cast作为function
typedef enum {RED,BLUE,GREEN} COLOR_E; COLOR_E color; int c; initial begin c=1; if(!$cast(color,c)) $display("cast failed for c=%0d",c); $display("Color is %d / %s",color,color.name); c=3; if(!$cast(color,c)) $display("cast failed for c=%0d",c); $display("Color is %d / %s",color,color.name); end // 输出 Color is 1 / BLUE cast failed for c=3 Color is 1 / BLUE
第二次$cast失败之后,color的值不变。
$cast作为task
typedef enum {RED,BLUE,GREEN} COLOR_E; COLOR_E color; int c; initial begin c=1; if(!$cast(color,c)) $display("cast failed for c=%0d",c); $display("Color is %d / %s",color,color.name); c=3; $cast(color,c); // 作为任务,没有返回值的语境 $display("Color is %d / %s",color,color.name); end // 会报错 Error-[STASKE_DCF] Dynamic cast failed ./svt.sv, 108 Dynamic cast using '$cast' failed. The source expression is not yielding a valid value for the destination variable.
3. 其他类型转换
在IEEE 1800的20.5节中,还介绍了一些integer2string或者signed2unsigned的函数
$itor converts integer values to real values (for example, 123 becomes 123.0).
$realtobits converts values from a real type to a 64-bit vector representation of the real number.
$bitstoreal converts a bit pattern created by $realtobits to a value of the real type.
$shortrealtobits converts values from a shortreal type to the 32-bit vector representation of the real number.
$bitstoshortreal converts a bit pattern created by $shortrealtobits to a value of the shortreal type
$signed—returned value is signed
$unsigned—returned value is unsigned
module driver (net_r); output [64:1] net_r; real r; wire [64:1] net_r = $realtobits(r); endmodule module receiver (net_r); wire [64:1] net_r; input [64:1] net_r; real r; initial assign r = $bitstoreal(net_r); endmodule
原文地址:https://www.cnblogs.com/east1203/p/11595813.html
- 企业安全漏洞通告引擎
- 通过httprouter和redis框架搭建restful api服务
- .NET Core 实现定时抓取博客园首页文章信息并发送到邮箱
- 基于 R 语言和 SPSS 的决策树算法介绍及应用
- Android 8.0 中的安全增强功能
- python2 群发 html 或文本邮件
- windows 安装 storm 及 eclipse 调试 TopN 实例
- shell 学习笔记(16)
- 根据ip查找ISP运营商和归属地的几种方法
- windows 安装 spark 及 pycharm 调试 TopN 实例
- storm kafka 编程指南
- 基于Session的身份窃取
- 使用 django-blog-zinnia 搭建个人博客
- 针对提权小神器Sherlock的分析与利用
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 在不被spring容器管理的类中使用ApplicationContext应用上下文bean
- Spring boot 在静态类中注入spring组件
- Spring boot 自定义配置文件
- 数据分析 常见技巧和经验总结
- Go by Example 中文版: Base64 编码
- Django3.0+supervisor+uvicorn+nginx进行线上部署
- 前端杂货铺上新
- 短视频系统源代码,实现前置摄像头水平翻转
- linux配置SOCK5代理
- 前端踩坑系列《五》
- linux上安装mitmproxy
- Jmeter(二十三) - 从入门到精通 - JMeter函数 - 上篇(详解教程)
- Elasticsearch学习笔记 -- 1
- Jmeter(二十四) - 从入门到精通 - JMeter函数 - 中篇(详解教程)
- [javascript] elementui和vue下复制粘贴上传图片