演示如何使用switch/is来实现状态机。
 
参考链接:
https://github.com/ucb-bar/chisel-tutorial/blob/release/src/main/scala/solutions/VendingMachineSwitch.scala
 
1. 引入Chisel3
 
 
2. 继承自Module类
 
 
3. 定义输入输出接口
 
创建各项输入输出接口。
 
val nickel = Input(Bool())
a. 使用Bool()创建布尔型数,位宽为1;
b. 使用UInt创建无符号整型数;
c. 使用Input/Output表示接口方向;
d. val 关键字表明定义的变量是所属匿名Bundle子类的数据成员;
 
4. 内部连接
 
 
1) 创建5个状态:val sIdle :: s5 :: s10 :: s15 :: sOk :: Nil = Enum(5)
 
2) 使用switch/is判断逻辑嵌套实现状态机;
 
如同when/elsewhen/otherwise判断结构的实现方式,switch/is分别接收两个参数列表,分别是判断条件和要执行的call-by-name代码块。
 
5. 生成Verilog
 
 
可以直接点运行符号运行。
 
也可以使用sbt shell执行:
 
生成Verilog如下:
 
6. 测试
 
 
 
7. 附录
 
VendingMachineSwitch.scala:
 
import chisel3._
import chisel3.util._ // Problem:
//
// Implement a vending machine using a 'switch' statement.
// 'nickel' is a 5 cent coin
// 'dime' is 10 cent coin
// 'sOk' is reached when there are coins totalling 20 cents or more in the machine.
// The vending machine should return to the 'sIdle' state from the 'sOk' state.
//
class VendingMachineSwitch extends Module {
val io = IO(new Bundle {
val nickel = Input(Bool())
val dime = Input(Bool())
val valid = Output(Bool())
})
val sIdle :: s5 :: s10 :: s15 :: sOk :: Nil = Enum(5)
val state = RegInit(sIdle) switch (state) {
is (sIdle) {
when (io.nickel) { state := s5 }
when (io.dime) { state := s10 }
}
is (s5) {
when (io.nickel) { state := s10 }
when (io.dime) { state := s15 }
}
is (s10) {
when (io.nickel) { state := s15 }
when (io.dime) { state := sOk }
}
is (s15) {
when (io.nickel) { state := sOk }
when (io.dime) { state := sOk }
}
is (sOk) {
state := sIdle
}
}
io.valid := (state === sOk)
} object VendingMachineSwitchMain {
def main(args: Array[String]): Unit = {
chisel3.Driver.execute(Array("--target-dir", "generated/VendingMachineSwitch"), () => new VendingMachineSwitch)
}
}

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

  1. Chisel3 - Tutorial - VendingMachine

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

  2. Chisel3 - Tutorial - Tbl

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

  3. Chisel3 - Tutorial - Stack

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

  4. Chisel3 - Tutorial - Functionality

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

  5. Chisel3 - Tutorial - Parity

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

  6. Chisel3 - Tutorial - ByteSelector

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

  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. 使用 if elseif else 指定条件

    nrows = 4; ncols = 6; A = ones(nrows,ncols); 遍历矩阵并为每个元素指定一个新值.对主对角线赋值 2,对相邻对角线赋值 -1,对其他位置赋值 0. for c ...

  2. SpringCloudStream学习(一)RabbitMQ基础

    应公司大佬要求,学习一下SpringCloudStream,作为技术储备.这几天也看了这方面的资料,现在写一篇笔记,以做总结.文章会从RabbitMQ基础讲起,到SpringCloudStream结束 ...

  3. Mahout聚类和kafaka相关知识

    1.说几种距离测度Mahout: 欧式距离测度:平方欧式距离测度:曼哈顿距离测度:余弦距离测度:加权距离测度 2.K-means算法参数:

  4. salesforce零基础学习(九十七)Event / Task 针对WhoId的浅谈

    我们在Sales Cloud中经常会创建顾客,如果针对TO C业务,会启用个人顾客,比如针对车企行业,有一些场景是需要卖给个人的,而不只是企业采购.当通过打电话或者其他的场景有潜在客户并且转换成客户以 ...

  5. 【Spark】通过Spark实现点击流日志分析

    文章目录 数据大致内容及格式 统计PV(PageViews) 统计UV(Unique Visitor) 求取TopN 数据大致内容及格式 194.237.142.21 - - [18/Sep/2013 ...

  6. 【Kafka】Kafka集群环境搭建

    目录 一.初始环境准备 二.下载安装包并上传解压 三.修改配置文件 四.启动ZooKeeper 五.启动Kafka集群 一.初始环境准备 必须安装了JDK和ZooKeeper,并保证Zookeeper ...

  7. Redis学习笔记(二) 链表

    链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地调整链表的长度. redis中链表应用广泛,如list中就使用了链表. 每一个链表节点使用listNode结构标识( ...

  8. [hdu5340]二分,枚举,二进制压位加速

    题意:判断一个字符串能否划成三段非空回文串. 思路:先用二分+hash在nlogn的时间内求出以每条对称轴为中心的回文串的最大半径r[i](可以用对称的两个下标之和来表示 ),然后利用r[i]求出pr ...

  9. Java多线程带返回值的Callable接口

    Java多线程带返回值的Callable接口 在面试的时候,有时候是不是会遇到面试会问你,Java中实现多线程的方式有几种?你知道吗?你知道Java中有可以返回值的线程吗?在具体的用法你知道吗?如果两 ...

  10. docker安装之后的配置各种坑

    1.docker官网下载安装之后 2.点击 Docker quick start快捷方式 错误提示: windows正在查找bash.exe,也可以手动查找,但是一直找不到. 3.执行 docker- ...