学习chisel(2): chisel基础

1. 第一个模块

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 。Module是一个内嵌的chisel类,所有的硬件模块都必须扩展它。

第二行 val io = IO(...) 我们会在io常量中声明我们的输入输出端口,需要这种类型 IO(_instantiated_bundle_).

new Bundle {

    val in = Input(...)

    val out = Output(...)
}

声明一个新的硬件结构体 Bundle(集束),包含一些被命名的信号 in 和 out ,它们的方向是 Input 和 Output

UInt(4.W)

我们声明一个信号的硬件类型。在这个例子中,这是一个无符号整数,宽度为4。

io.out := io.in

在这个模块中,我们把 io.out 和 io.in 连接。

Note that the `:=` operator is a ***Chisel*** operator that indicates that the right-hand signal drives the left-hand signal. It is a directioned operator.

hardware construction languages (HCLs)

接着我们可以使用scala把chisel模块翻译成verilog模块,这个过程叫elaboration(细化)

println(getVerilog(new Passthrough))

 

 1 // Chisel Code, but pass in a parameter to set widths of ports
 2 class PassthroughGenerator(width: Int) extends Module { 
 3   val io = IO(new Bundle {
 4     val in = Input(UInt(width.W))
 5     val out = Output(UInt(width.W))
 6   })
 7   io.out := io.in
 8 }
 9 
10 // Let's now generate modules with different widths
11 println(getVerilog(new PassthroughGenerator(10)))
12 println(getVerilog(new PassthroughGenerator(20)))

可以看到, chisel模块实际上就是用scala类实现的,可以通过参数来动态修改端口的大小

 

2. 组合逻辑

1 是scala整数,而1.U 是一根通电值为“1”的线(wire),这两者是不一样的。

 

posted @ 2022-04-22 12:29  yinhuachen  阅读(90)  评论(0编辑  收藏  举报