• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Chisel三

武飞扬头像
薄荷茶哈哈哈
帮助1

参考boot-camp2.1

一、导入库和当中包含的类

根据Chisel(一)配置好环境后建立scala文件,导入需要的chisel库中的类

  1.  
    import chisel3._
  2.  
    import chisel3.util._
  3.  
    import chisel3.tester._
  4.  
    import chisel3.tester.RawTester.test
  5.  
    import dotvisualizer._

二、建立第一个模块 

  1.  
    / Chisel Code: Declare a new module definition
  2.  
    class Passthrough extends Module {
  3.  
    val io = IO(new Bundle {
  4.  
    val in = Input(UInt(4.W))
  5.  
    val out = Output(UInt(4.W))
  6.  
    })
  7.  
    io.out := io.in
  8.  
    }

class Passthrough extends Module {  --声明模块名称为Passthrough,Module是Chisel的内置类,所以所有的硬件模块都必须extends

val io = IO(...)  -- 声明可输入输出端口,表现形式为IO(_instantiated_bundle_)

new Bundle { val in = Input(...) val out = Output(...) }  --声明新的硬件结构类型Bundle,包含两个val型变量in,out,分别为输入、输出类型

UInt(4.W) -- 声明一个硬件类型,这是无符号整数,宽度为4

io.out := io.in  --连接输入和输出端口,io.in驱动了io.out,:=是Chisel的操作符,意思是右边的信号驱动了左边的信号,这是有方向的操作符。

三、细化(elaborati)

HCLs中我们可以使用底层编程语言作为脚本语言,比如我们可以使用Scala来调用Chisel编译器编译Chisel的Passthrough模块为verilog类型,这个过程叫做细化(elaboration)

  1.  
    package mypack
  2.  
    //定义包
  3.  
    import chisel3._
  4.  
    import chisel3.util._
  5.  
    import chisel3.tester._
  6.  
    import chisel3.tester.RawTester.test
  7.  
    //调用chisel包
  8.  
     
  9.  
    class PassthroughGenerator(width: Int) extends Module {
  10.  
    val io = IO(new Bundle {
  11.  
    val in = Input(UInt(width.W))
  12.  
    val out = Output(UInt(width.W))
  13.  
    })
  14.  
    io.out := io.in
  15.  
    }
  16.  
    object PassthroughGenerator extends App {
  17.  
    println(getVerilogString(new PassthroughGenerator(10)))
  18.  
    }
学新通

因为可以通过PassthroughGenerator生成定义位宽的模块,所以PassthroughGenerator被称为生成器,效果如下:

学新通

四、测试硬件模块

 Chisel内置了测试模块,下面的测试模块

以下示例是一个 Chisel 测试工具,它将值传递给 Passthrough 的输入端口 in 的实例,并检查在输出端口 out 上是否看到相同的值。

  1.  
    import chisel3._
  2.  
    import chiseltest._
  3.  
    import org.scalatest.flatspec.AnyFlatSpec
  4.  
    import mypack._
  5.  
     
  6.  
     
  7.  
    class PassthroughTest extends AnyFlatSpec with ChiselScalatestTester {
  8.  
    behavior of "PassthroughGenerator"
  9.  
    it should "pass through bits" in {
  10.  
    test(new PassthroughGenerator(20)) { c =>
  11.  
    c.io.in.poke(0.U) // Set our input to value 0
  12.  
    c.io.out.expect(0.U) // Assert that the output correctly has 0
  13.  
    c.io.in.poke(10.U) // Set our input to value 10
  14.  
    c.io.out.expect(10.U) // Assert that the output correctly has 10
  15.  
    c.io.in.poke(20.U) // Set our input to value 20
  16.  
    c.io.out.expect(20.U) // Assert that the output correctly has 20
  17.  
    }
  18.  
    println("SUCCESS!!") // Scala Code: if we get here, our tests passed!
  19.  
    }
  20.  
    }
学新通

测试输出:

学新通

使用sbt test会将文件夹中所有的test编译 

五、printf与println的区别

println 是一个内置的 Scala 函数,可以打印到控制台。它不能在电路仿真期间用于打印,因为生成的电路是 FIRRTL 或 Verilog,而不是 Scala。

  1.  
    package mypack
  2.  
    //定义包
  3.  
    import chisel3._
  4.  
    import chisel3.util._
  5.  
    import chisel3.tester._
  6.  
    import chisel3.tester.RawTester.test
  7.  
    //调用chisel包
  8.  
     
  9.  
    class PassthroughGenerator(width: Int) extends Module {
  10.  
    val io = IO(new Bundle {
  11.  
    val in = Input(UInt(width.W))
  12.  
    val out = Output(UInt(width.W))
  13.  
    })
  14.  
    io.out := io.in
  15.  
    printf("Print during simulation: Input is %d\n", io.in)
  16.  
    // chisel printf has its own string interpolator too
  17.  
    printf(p"Print during simulation: IO is $io\n")
  18.  
     
  19.  
    println(s"Print during generation: Input is ${io.in}")
  20.  
    }
  21.  
    object PassthroughGenerator extends App {
  22.  
    println(getVerilogString(new PassthroughGenerator(10)))
  23.  
    }
学新通

 测试结果如下:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgkgijh
系列文章
更多 icon
同类精品
更多 icon
继续加载