https://mp.weixin.qq.com/s/SEcVjGRL1YloGlEPSoHr3A

 
位数为参数的加法器。通过FullAdder级联实现。
 
参考链接:
https://github.com/ucb-bar/chisel-tutorial/blob/release/src/main/scala/examples/Adder.scala
 
1. 引入Chisel3
 
 
2. 继承自Module类
 
这里的n是构造方法的参数,表示加法器的位数。
 
3. 定义输入输出接口
 
根据位数n,创建各项输入输出接口。
 
这些接口都是无符号整型数:val A = Input(UInt(n.W))
a. 使用n.W表示位宽为n位;
b. 使用UInt创建无符号整型数;
c. 使用Input/Output表示接口方向;
d. val 关键字表明定义的变量是所属匿名Bundle子类的数据成员;
 
4. 内部连接
 
 
创建n个全加器,并与输入和输出接口相连。
 
1) 创建多个全加器:val FAs = Array.fill(n)(Module(new FullAdder()).io)
 
a. 这个写法实际上是用了一点trick,以缩短后续代码。实际上FA是指FullAdder,但这里创建的却不是全加器,而是全加器的io。
 
如果是全加器,写法应该如下:
val FAs = Array.fill(n)(Module(new FullAdder()))
但这样写,后续使用时,需要加上io:
FAs(i).io.a := io.A(i)
 
因为后续使用时都是使用全加器的io,而不使用全加器。所以直接使用FAs指代FAs的io,也无可厚非。
 
b. 这里使用了Scala的call-by-name机制
Array.fill()方法的签名为:
def fill[T: ClassTag](n: Int)(elem: => T): Array[T]
包含两个参数列表,第一个为数组元素的个数;第二个参数使用“=> T”表明该参数为call-by-name参数,使用时如同这个参数是一个函数,这个函数返回类型为T的返回值。
 
所以Array.fill(n)(Module(new FullAdder()).io)的意思:
i. 创建一个数组;
i. 数组元素的个数为n;
i. 创建数组元素的方法为:Module(new FullAdder()).io。即每次创建数组元素时,都调用这段代码;
 
2) 创建进位线:val carry = Wire(Vec(n+1, UInt(1.W)))
 
a. 创建一个Vec[UInt]
 
 
b. 使用Wire()绑定Vec[UInt]
 
 
WireBinding不是只读绑定,可以作为“:=”的左值。
 
c. carry(i)返回元素UInt
 
carry(i)可以作为左值使用:
carry(0) := io.Cin
 
3) 创建和线:val sum = Wire(Vec(n, Bool()))
 
 
5. 生成Verilog
 
 
可以直接点运行符号运行。
 
也可以使用sbt shell执行:
 
生成Verilog如下:
 
6. 测试
 
 
 
7. 附录
 
Adder.scala:
 
import chisel3._

//A n-bit adder with carry in and carry out
class Adder(val n:Int) extends Module {
val io = IO(new Bundle {
val A = Input(UInt(n.W))
val B = Input(UInt(n.W))
val Cin = Input(UInt(1.W))
val Sum = Output(UInt(n.W))
val Cout = Output(UInt(1.W))
})
//create an Array of FullAdders
// NOTE: Since we do all the wiring during elaboration and not at run-time,
// i.e., we don't need to dynamically index into the data structure at run-time,
// we use an Array instead of a Vec.
val FAs = Array.fill(n)(Module(new FullAdder()).io)
// val FAs = Array.fill(n)(Module(new FullAdder()))
val carry = Wire(Vec(n+1, UInt(1.W)))
val sum = Wire(Vec(n, Bool())) //first carry is the top level carry in
carry(0) := io.Cin //wire up the ports of the full adders
for (i <- 0 until n) {
FAs(i).a := io.A(i)
FAs(i).a := io.A(i)
FAs(i).b := io.B(i)
FAs(i).cin := carry(i)
carry(i+1) := FAs(i).cout
sum(i) := FAs(i).sum.toBool()
} // for (i <- 0 until n) {
// FAs(i).io.a := io.A(i)
// FAs(i).io.a := io.A(i)
// FAs(i).io.b := io.B(i)
// FAs(i).io.cin := carry(i)
// carry(i+1) := FAs(i).io.cout
// sum(i) := FAs(i).io.sum.toBool()
// } io.Sum := sum.asUInt
io.Cout := carry(n)
} object AdderMain {
def main(args: Array[String]): Unit = {
chisel3.Driver.execute(Array("--target-dir", "generated/Adder"), () => new Adder(4))
}
}

Chisel3 - Tutorial - Adder的更多相关文章

  1. Chisel3 - Tutorial - Adder4

    https://mp.weixin.qq.com/s/X5EStKor2DU0-vS_wIO-fg   四位加法器.通过FullAdder级联实现.   参考链接: https://github.co ...

  2. Chisel3 - Tutorial - VendingMachine

    https://mp.weixin.qq.com/s/tDpUe9yhwC-2c1VqisFzMw   演示如何使用状态机.   参考链接: https://github.com/ucb-bar/ch ...

  3. Chisel3 - Tutorial - VendingMachineSwitch

    https://mp.weixin.qq.com/s/5lcMkenM2zTy-pYOXfRjyA   演示如何使用switch/is来实现状态机.   参考链接: https://github.co ...

  4. Chisel3 - Tutorial - Tbl

    https://mp.weixin.qq.com/s/e8vJ8claauBtiuedxYYaJw   实现可以动态索引的表.   参考链接: https://github.com/ucb-bar/c ...

  5. Chisel3 - Tutorial - Stack

    https://mp.weixin.qq.com/s/-AVJD1IfvNIJhmZM40DemA   实现后入先出(last in, first out)的栈.   参考链接: https://gi ...

  6. Chisel3 - Tutorial - Functionality

    https://mp.weixin.qq.com/s/3hDzpJiANdwp07hO03psyA   演示使用函数进行代码复用的方法.   参考链接: https://github.com/ucb- ...

  7. Chisel3 - Tutorial - Parity

    https://mp.weixin.qq.com/s/OtiQnE52PwdCpvmzJ6VFnA   奇偶发生器.统计输入中1的个数,如果为偶数则输出0,奇数则输出1.   参考链接: https: ...

  8. Chisel3 - Tutorial - ByteSelector

    https://mp.weixin.qq.com/s/RQg2ca1rwfVHx_QG-IOV-w   字节选择器.   参考链接: https://github.com/ucb-bar/chisel ...

  9. Chisel3 - Tutorial - ShiftRegister

    https://mp.weixin.qq.com/s/LKiXUgSnt3DzgFLa9zLCmQ   简单的寄存器在时钟的驱动下,逐个往下传值.   参考链接: https://github.com ...

随机推荐

  1. dp 20190617

    A. Alternative Thinking 这个标的是dp,但是我感觉就只能算思维题,不是特别难, 你仔细想想就知道,你先求出01这样子满足条件的个数,如果要进行改变,最多只可以增加两个,也可以增 ...

  2. A. A Twisty Movement dp

    https://codeforces.com/problemset/problem/933/A 这个是一个dp,但是我并没有看出来,然后也不太会写, 这种题一般应该要想到先预处理前缀和后缀,然后再进行 ...

  3. Spring cloud系列教程第二篇:支付项目父工程图文搭建

    Spring cloud系列教程第二篇:支付项目父工程图文搭建 在讲解spring cloud相关的技术的时候,咱们就模拟订单支付这个流程来讲讲 在这个支付模块微服务搭建过程中,上面的这些技术,都会融 ...

  4. DotNet:Socket Server 异步套接字服务端实现

    异步服务器套接字示例 From https://msdn.microsoft.com/zh-cn/library/fx6588te(v=vs.110).aspx 下面的示例程序创建接收来自客户端的连接 ...

  5. JDBC06 其他操作及批处理Batch

    灵活指定SQL语句中的变量 -PreparedStatement 对存储过程进行调用 -CallableStatement 运用事务处理 -Transaction 批处理 -Batch -对于大量的批 ...

  6. C#操作时区转换时遇到的一些问题和解决方法分享

    先上一下自己弄出来的库,.NETCore 2.2环境,以前的老库不在适用了TimeZoneInfo对象要熟悉 /// <summary> /// 获取当前时间戳 /// </summ ...

  7. HTTP Strict Transport Security (通常简称为HSTS)

    HTTP Strict Transport Security (通常简称为HSTS) 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源, 禁止HTTP方式. Freebuf百科:什么是Str ...

  8. iview input 禁止输入特殊字符 ,解决中文输入法中input把拼音输入

    tips:解决了e.target中输入中文 会把拼音也输入的情况 1 html <FormItem label="角色名称" prop="roleName" ...

  9. 简述SpringCloud底层原理

    目录 一.业务场景介绍 二.Spring Cloud核心组件:Eureka 三.Spring Cloud核心组件:Feign 四.Spring Cloud核心组件:Ribbon 五.Spring Cl ...

  10. Rabbitmq 整合Spring,SpringBoot与Docker

    SpringBootLearning是对Springboot与其他框架学习与研究项目,是根据实际项目的形式对进行配置与处理,欢迎star与fork. [oschina 地址] http://git.o ...