【SpinalHDL快速入门】4.1、基本类型之Bool

文章详细介绍了SpinalHDL中的Bool类型,包括其与ScalaBoolean的区别、声明方式以及各种运算符的使用,如逻辑运算、边沿检测、比较、类型转换和MaskedBoolean。此外,还展示了如何进行边沿检测和在不同条件下的布尔值操作。

Tips1:

由于SpinalHDL是基于Scala构建的,Scala本身自带类似变量Boolean,故在此要认准SpinalHDL中采用的是Bool而非Boolean:

  • Bool(大写的True和False):True表示1,False表示0
  • Boolean(小写的true和false):true表示1,false表示0

Tips2:

SpinalHDL在声明时采用“=”,而在改变电路状态时用“:=

在这里插入图片描述

1.1、描述

Bool类型对应于布尔值(True或False)。

1.2、声明

声明布尔值的语法如下:(方括号中的所有内容都是可选项

在这里插入图片描述

val myBool_1 = Bool() // Create a Bool
myBool_1 := False // := is the assignment operator

val myBool_2 = False // Equivalent to the code above
val myBool_3 = Bool(5 > 12) // Use a Scala Boolean to create a Bool

1.3、运算符

以下运算符可用于布尔类型:

1.3.1、逻辑运算(logic)

在这里插入图片描述

val a, b, c = Bool()
val res = (!a & b) ^ c // ((NOT a) AND b) XOR c

val d = False
when(cond) {
	d.set() // equivalent to d := True
}

val e = False
e.setWhen(cond) // equivalent to when(cond) { d := True }

val f = RegInit(False) fallWhen(ack) setWhen(req) //这个理解起来有点难度???
/** equivalent to
* when(f && ack) { f := False }
* when(req) { f := True }
* or
* f := req || (f && !ack)
*/

// mind the order of assignments!
val g = RegInit(False) setWhen(req) fallWhen(ack) //这个理解起来有点难度???
// equivalent to g := ((!g) && req) || (g && !ack)

1.3.2、边沿检测(Edge detection)

在这里插入图片描述

when(myBool_1.rise(False)) { //括号里面表示复位值为低
	// do something when a rising edge is detected
}


val edgeBundle = myBool_2.edges(False)
when(edgeBundle.rise) {
	// do something when a rising edge is detected
}
when(edgeBundle.fall) {
	// do something when a falling edge is detected
}
when(edgeBundle.toggle) {
	// do something at each edge
}

1.3.3、比较(Comparison)

在这里插入图片描述

when(myBool) { // Equivalent to when(myBool === True)
	// do something when myBool is True
}

when(!myBool) { // Equivalent to when(myBool === False)
	// do something when myBool is False
}

1.3.4、类型转换(Type cast)

在这里插入图片描述

// Add the carry to an SInt value
val carry = Bool()
val res = mySInt + carry.asSInt

1.3.5、杂项(Misc)

在这里插入图片描述

就是拼接符,等效Verilog中的{}

val a, b, c = Bool()
// Concatenation of three Bool into a Bits
val myBits = a ## b ## c

1.3.6、MaskedBoolean

MaskedBoolean 允许使用不确定值。它们通常不单独使用,而是通过MaskedLiteral来使用。

MaskedLiteral值是带有“-”的位向量,表示不关心的值。【下面的M就表示MaskedLiteral】

val myBits = B"1101
val test1 = myBits === M"1-01" // True
val test2 = myBits === M"0---" // False
val test3 = myBits === M"1--1" // True
// first argument: boolean value
// second argument: do we care ?
val masked = new MaskedBoolean(true, false)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReCclay

如果觉得不错,不妨请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值