网站首页

人工智能P2P分享搜索全网发布信息网站地图标签大全

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

SpinalHDL语法之Bool类型

时间:2023-05-05 16:01

人气:

作者:admin

标签: HDL  语法 

导读:作为SpinalHDL语法篇的第一节,我们也从最简单的开始。 Bool类型定义...

作为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:

val a = Bool()
val b = True
val c = False
val d = Bool( 1 > 2)

生成的Verilog代码如下:

wire                a;
wire                b;
wire                c;
wire                d;

assign b = 1'b1;
assign c = 1'b0;
assign d = 1'b0;

逻辑运算

下图为官方的逻辑运算解释,也不翻译了,很容易理解。

Operator Description Return type
!x Logical NOT Bool
x && y Logical And Bool
x & y Logical And Bool
x y
x y Logical OR
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
val e = a & b
  val f = a | b
  val g = a ^ b
  val h = !a

  val i = Bool()
  i.set()
  val j = Bool()
  j.clear()

  val k = True #这里必须有初值,否则下一句会报错
  k.clearWhen(b)

  val l = True
  when(b){
    l := False
  }

  val m = 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
val a = Bool()
val b = False
when(a.edge()){
b := True
}
val c = a.edge(False)

转换后的代码为:

module DemoBool (
  input               clk,
  input               reset
);

  wire                a;
  reg                 b;
  reg                 a_regNext;
  wire                when_DemoBool_l35;
  reg                 a_regNext_1;
  wire                c;

  always @(*) begin
    b = 1'b0;
    if(when_DemoBool_l35) begin
      b = 1'b1;
    end
  end

  assign when_DemoBool_l35 = (a ^ a_regNext);
  assign c = (a ^ a_regNext_1);
  always @(posedge clk) 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
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
}

数值比对

Operator Description Return type
x === y Equality Bool
x =/= y Inequality Bool
温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信