https://mp.weixin.qq.com/s/RQg2ca1rwfVHx_QG-IOV-w

 
字节选择器。
 
参考链接:
https://github.com/ucb-bar/chisel-tutorial/blob/release/src/main/scala/examples/ByteSelector.scala
 
1. 引入Chisel3
 
 
2. 继承自Module类
 
 
3. 定义输入输出接口
 
创建各项输入输出接口。
 
这些接口都是无符号整型数:val in = Input(UInt(32.W))
a. 使用32.W表示位宽为32位;
b. 使用UInt创建无符号整型数;
c. 使用Input/Output表示接口方向;
d. val 关键字表明定义的变量是所属匿名Bundle子类的数据成员;
 
4. 内部连接
 
 
使用when/elsewhen/otherwise表达判断逻辑。
 
1) when()()可以理解为一个有三个参数列表的函数
 
函数签名如下,返回一个WhenContext对象:
 
Scala中,允许使用{}代替()来包围参数列表。所以when()()可以改成when(){}的形式。
 
最后一个参数列表忽略。
 
2) elsewhen()()
 
 
elsewhen/otherwise都是WhenContext的方法。所以可以直接调用。elsewhen如同when,接收两个参数列表。
 
3) otherwise()
 
最后一个判断分支。
 
5. 生成Verilog
 
 
可以直接点运行符号运行。
 
也可以使用sbt shell执行:
 
生成Verilog如下:
 
6. 测试
 
 
 
7. 附录
 
ByteSelector.scala:
import chisel3._

class ByteSelector extends Module {
val io = IO(new Bundle {
val in = Input(UInt(32.W))
val offset = Input(UInt(2.W))
val out = Output(UInt(8.W))
})
io.out := 0.U(8.W)
when (io.offset === 0.U(2.W)) {
io.out := io.in(7, 0)
}.elsewhen(io.offset === 1.U) {
io.out := io.in(15, 8)
}.elsewhen(io.offset === 2.U) {
io.out := io.in(23, 16)
}.otherwise {
io.out := io.in(31, 24)
}
} object ByteSelectorMain {
def main(args: Array[String]): Unit = {
chisel3.Driver.execute(Array("--target-dir", "generated/ByteSelector"), () => new ByteSelector)
}
}

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

  1. Chisel3 - Tutorial - VendingMachine

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

  2. Chisel3 - Tutorial - VendingMachineSwitch

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

  3. Chisel3 - Tutorial - Tbl

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

  4. Chisel3 - Tutorial - Stack

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

  5. Chisel3 - Tutorial - Functionality

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

  6. Chisel3 - Tutorial - Parity

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

  7. Chisel3 - Tutorial - ShiftRegister

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

  8. Chisel3 - Tutorial - Adder

    https://mp.weixin.qq.com/s/SEcVjGRL1YloGlEPSoHr3A   位数为参数的加法器.通过FullAdder级联实现.   参考链接: https://githu ...

  9. Chisel3 - Tutorial - Adder4

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

随机推荐

  1. Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)

    文章目录 AbstractLoadBalancer: BaseLoadBalancer: DynamicServerListLoadBalancer: ServerList: ServerListUp ...

  2. Day_13【IO流】扩展案例2_统计指定项目文件中字符出现的次数

    需求分析 统计当前项目下info2.txt文件中, 每个字符出现的个数 文件内容如下: welcome to itheima!!! 最终效果如下: w(1) (2)!(3)t(2)e(3)c(1)a( ...

  3. STM32 TIM1高级定时器配置快速入门

    layout: post tags: [STM32] comments: true 文章目录 layout: post tags: [STM32] comments: true 重点内容 时基单元 计 ...

  4. 使用IR2104S搭建的H桥-机器人队比赛经典版(原作者答疑)

    原理图地址:http://bbs.ednchina.com/BLOG_ARTICLE_3020313.HTM?click_from=8800020962,4950449047,2014-05-01,E ...

  5. [hdu4627 The Unsolvable Problem]数论

    题意:给一个数n,找一个正整数x<n,使得x和n-x的最小公倍数最大. 思路:显然x和n-x越接近越好,gcd必须为1(贪心).从大到小考虑x,如果n为奇数,则答案就是x=n/2,如果n为偶数, ...

  6. csu1617]强连通分量

    题意:定义域属于一个集合S={0,1,...,n-1},求S的子集个数,满足以子集的元素为定义域的函数P(x)的值域等于子集本身. 思路:以元素为点,x到P(x)连一条有向边,不难发现,如果有一个有向 ...

  7. Apache Poi实现excel解析

    一.说明 1.本文通过使用 poi 工具解析 excel 表格数据,实现导入导出 2.excel目前有两种格式 2003版本的 excel.xls 与 2007版本的 excel.xlsx ,注意两种 ...

  8. python-经典类和新式类区别

    经典类和新式类区别 Eg: class A(object):    def x(self):        print('A')class B(A):    def x(self):        p ...

  9. webpack指南(五)TypeScript

    将webpack与TS进行集成. 1. 安装TypeScript 编译器和 loader npm install --save-dev typescript ts-loader 2. 在package ...

  10. 【PHP+MySQL学习笔记】php操作MySQL数据库中语句

    1.连接 MYSQL 服务器的函数 mysql_connect();<?php $con = mysql_connect("localhost","root&quo ...