演示如何使用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. G - Pictures with Kittens (easy version) dp

    https://codeforces.com/problemset/problem/1077/F1 这个其实是一个比较简单的dp了 题目大意: 给你n个数,让你从n个数里选出x个数,并且每隔k个至少选 ...

  2. LeetCode--Unique Email Addresses & Hamming Distance (Easy)

    929. Unique Email Addresses (Easy)# Every email consists of a local name and a domain name, separate ...

  3. Day_09【常用API】扩展案例4_将字符串变为字符数组,并将大写字母改为小写,首尾内容不同互换,并将索引为偶数的元素替换

    分析以下需求,并用代码实现 1.键盘录入一个字符串 2.将该字符串变成字符数组(不能使用toCharArray()方法) 3.将字符数组中的所有大写字母变成小写字母(不能使用toLowerCase() ...

  4. FOC中电流环调试的宝贵经验总结(有理有据+全盘拖出)

    你是否经历过一个人独自摸索前进磕磕碰碰最终体无完肤,然后将胜利的旗帜插到山顶的时刻,如果有,本文也许能帮你在调试FOC电流环的时候给你带来一些帮助和思路. 如果本文帮到了您,请帮忙点个赞

  5. LabVIEW(数据库连接)

    Driver={Microsoft Access Driver (*.mdb)}; Dbq=路径; Uid=Admin; Pwd=密码; 插入: INSERT INTO xs(学号,姓名,专业名,性别 ...

  6. [c++ IO加速]快速输入输出

    自己封装的FastIO类,效率虽有所损失,不过实用性提高很多. 测试,写10000000个整数(86M): printf 2.7s cout 27s FastIO 1s 测试,读10000000个整数 ...

  7. 神奇的'license': 'AGPL 3.0'标签报错

    在__minifest__.py中,放了license标签,然后整个模块就报错了. 注释掉这个标签就好了.

  8. unittest详解 跳过用例的执行(skip)

    在执行测试用例时,有时候有些用例是不需要执行的,那我们怎么办呢?难道删除这些用例?那下次执行时如果又需要执行这些用例时,又把它补回来?这样操作就太麻烦了. unittest提供了一些跳过指定用例的方法 ...

  9. 黑马程序员_毕向东_Java基础视频教程——java语言组成部分(随笔)

    java语言组成部分 Java是一种强类型语言,所谓强类型语言就是对具体的数据进行不同的定义.对类型的划分的十分细致,对内存中分配了不同大小的内u你空间 关键字 标识符 注释 变量和常量 运算符 语句 ...

  10. Antd 表格数据分页展示

    分页组件代码 render(){ const {total,size,currenPage} = this.state // 参数分别为数据总条数.每页数据条数.当前页页码 return ( // 渲 ...