作为SpinalHDL语法篇的第一节,我们也从最简单的开始。
Bool类型定义
Bool类型就是Verilog中的单bit类型,定义方式如下:
| Syntax | Description | Return |
|---|---|---|
| Bool() | 创建Bool类型变量 | Bool |
| True | 创建Bool类型变量,并赋值true | Bool |
| False | 创建Bool类型变量,并赋值false | Bool |
| Bool(value: Boolean) | 创建Bool类型变量,并使用Scala表达式赋值 | Bool |
Example:
vala=Bool() valb=True valc=False vald=Bool(1>2)
生成的Verilog代码如下:
wirea; wireb; wirec; wired; assignb=1'b1; assignc=1'b0; assignd=1'b0;
逻辑运算
下图为官方的逻辑运算解释,也不翻译了,很容易理解。
| Operator | Description | Return type |
|---|---|---|
| !x | Logical NOT | Bool |
| x && y | Logical And | Bool |
| x & y | Logical And | Bool |
| x || y | Logical OR | Bool |
| x | y | Logical OR | Bool |
| x ^ y | Logical XOR | Bool |
| x.set[()] | Set x to True | Bool |
| x.clear[()] | Set x to False | Bool |
| x.setWhen(cond) | Set x when cond is True | Bool |
| x.clearWhen(cond) | Clear x when cond is True | Bool |
| x.riseWhen(cond) | Set x when x is False and cond is True | Bool |
| x.fallWhen(cond) | Clear x when x is True and cond is True | Bool |
vale=a&b
valf=a|b
valg=a^b
valh=!a
vali=Bool()
i.set()
valj=Bool()
j.clear()
valk=True#这里必须有初值,否则下一句会报错
k.clearWhen(b)
vall=True
when(b){
l:=False
}
valm=RegInit(False)#关于寄存器类型,这里先熟悉一下,后面章节会讲到
m.riseWhen(b)
边缘检测
| Operator | Description | Return type |
|---|---|---|
| x.edge[()] | Return True when x changes state | Bool |
| x.edge(initAt: bool) | Same as x.edge but with a reset value | Bool |
| x.rise[()] | Return True when x was low at the last cycle and is now high | Bool |
| x.rise(initAt: Bool) | Same as x.rise but with a reset value | Bool |
| x.fall[()] | Return True when x was high at the last cycle and is now low | Bool |
| x.fall(initAt: Bool) | Same as x.fall but with a reset value | Bool |
| x.edges[()] | Return a bundle (rise, fall, toggle) | BoolEdges |
| x.edges(initAt: Bool) | Same as x.edges but with a reset value | BoolEdges |
vala=Bool()
valb=False
when(a.edge()){
b:=True
}
valc=a.edge(False)
转换后的代码为:
moduleDemoBool( inputclk, inputreset ); wirea; regb; rega_regNext; wirewhen_DemoBool_l35; rega_regNext_1; wirec; always@(*)begin b=1'b0; if(when_DemoBool_l35)begin b=1'b1; end end assignwhen_DemoBool_l35=(a^a_regNext); assignc=(a^a_regNext_1); always@(posedgeclk)begin a_regNext<= a; end always @(posedge clk or posedge reset) begin if(reset) begin a_regNext_1 <= 1'b0; end else begin a_regNext_1 <= a; end end endmodule
valedgeBundle=myBool_2.edges(False)
when(edgeBundle.rise){
//dosomethingwhenarisingedgeisdetected
}
when(edgeBundle.fall){
//dosomethingwhenafallingedgeisdetected
}
when(edgeBundle.toggle){
//dosomethingateachedge
}
数值比对
| Operator | Description | Return type |
|---|---|---|
| x === y | Equality | Bool |
| x =/= y | Inequality | Bool |
审核编辑:汤梓红
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
Verilog
+关注
关注
28文章
1351浏览量
110077 -
HDL
+关注
关注
8文章
327浏览量
47378
原文标题:SpinalHDL语法篇之Bool类型
文章出处:【微信号:傅里叶的猫,微信公众号:傅里叶的猫】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
STM32如何去使用bool类型
Q:STM32 如何使用bool类型?通常进行stm32相关编程的时候,bool类型表现更加直白,但编程过程中又不能直接使用,就可以参照工程中的头文件进行添加定义。如:stm32f10
发表于 08-04 08:10
bool定义的类型
[]bool定义的类型只有真和假两种值。[]Static申明的局部变量,存储在静态存储区。静态局部变量的初始化语句块第一次执行起作用。在随后的运行过程中,变量将保持上一次执行的值。[]枚举
发表于 08-12 07:11
在SpinalHDL里switch方法有何用处呢
可以更快速高效的实现tkeep到byteCnt的转换:SpinalHDL在生成RTL时,仅SpinalHDL提供的语法会生成RTL电路,而其他代码则是起指导生成电路的作用,在上面代码里
发表于 06-22 14:25
在SpinalHDL中定义各种各样的复合数据类型
通过继承Bundle,在SpinalHDL中我们可以定义各种各样的复合数据类型。今天,关于Bundle的几个容易被忽略的点,一同来看下。》Bundle个人在使用SpinalHDL来描述电路时,凡是
发表于 06-28 15:21
在SpinalHDL中关于casez的使用
我们常常采用?表示我们不关心的位,而在SpinalHDL中,也存在这么一种表示方式。SpinalHDL整体的数据结构如下图所示:针对BitVector及其子类,SpinalHDL定义了一种特殊的
发表于 07-06 10:59
SpinalHDL的UInt与SInt数据类型能够进行有符号/无符号数操作
在Bits的基础上,SpinalHDL提供了UInt、SInt数据类型,从而能够进行有符号/无符号数操作。变量定义/初始化UInt/SInt的初始化与Bits类型相似:逻辑操作符UInt/SInt
发表于 07-14 14:45
SpinalHDL中Bundle与普通数据类型之间的连接赋值转换
SpinalHDL中Bundle与SystemVerilog中的packed struct很像,在某些场景下,与普通数据类型之间的连接赋值可以通过asBits,assignFromBits来实现
发表于 10-18 14:22
SpinalHDL中Bundle数据类型的转换
SpinalHDL中Bundle与SystemVerilog中的packed struct很像,在某些场景下,与普通数据类型之间的连接赋值可以通过asBits,assignFromBits来实现。

SpinalHDL语法篇之Bool类型
评论