https://mp.weixin.qq.com/s/tDpUe9yhwC-2c1VqisFzMw

 
演示如何使用状态机。
 
参考链接:
https://github.com/ucb-bar/chisel-tutorial/blob/release/src/main/scala/solutions/VendingMachine.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) 使用when判断逻辑嵌套实现状态机;
 
5. 生成Verilog
 
 
可以直接点运行符号运行。
 
也可以使用sbt shell执行:
 
生成Verilog如下:
略(惨不忍睹)
 
6. 测试
 
 
 
7. 附录
 
VendingMachine.scala:
import chisel3._
import chisel3.util._ // Problem:
//
// Implement a vending machine using 'when' states.
// '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 VendingMachine 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)
when (state === sIdle) {
when (io.nickel) { state := s5 }
when (io.dime) { state := s10 }
}
when (state === s5) {
when (io.nickel) { state := s10 }
when (io.dime) { state := s15 }
}
when (state === s10) {
when (io.nickel) { state := s15 }
when (io.dime) { state := sOk }
}
when (state === s15) {
when (io.nickel) { state := sOk }
when (io.dime) { state := sOk }
}
when (state === sOk) {
state := sIdle
}
io.valid := (state === sOk)
} object VendingMachineMain {
def main(args: Array[String]): Unit = {
chisel3.Driver.execute(Array("--target-dir", "generated/VendingMachine"), () => new VendingMachine)
}
}

最新文章

  1. MT4平台经验总结
  2. js实现判断浏览器版本
  3. 优化后的二次测试Miller_Rabin素性测试算法
  4. IOS开发篇UI之重用scrollView
  5. 手把手教你写对拍程序(PASCAL)
  6. 1012: [JSOI2008]最大数maxnumber
  7. JSON数据格式中的引号
  8. React 国际化
  9. 解决VMware虚拟机不能上网的问题
  10. spring整合mybatis错误:Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 62; 文档根元素 "mapper" 必须匹配 DOCTYPE 根 "configuration"。
  11. java中属性,set get 以及如何学习类的一些用法
  12. Nuget发布自己的DLL
  13. 安装nodejs时:The error code is 2503.
  14. Leetcode_48_Rotate Image
  15. OpenCV图像分割1
  16. tar 的–exclude参数,实现不包括某些文件(转)
  17. 音视频处理ffmpeg使用
  18. 题解——洛谷P2613 【模板】有理数取余(扩展欧几里得算法+逆元)
  19. 如何自定义 maven中的archetype
  20. 开工:创建虚拟机,xshell连接虚拟机,复制虚拟机,docker安装,添加加速器

热门文章

  1. dp 20190617
  2. SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器
  3. Code::Blocks20.03 编译报错
  4. Excel开始,Excel结束,R语言居中
  5. 一句话+两张图搞定JDK1.7HashMap,剩下凑字数
  6. React之Antd table表格渲染按钮问题
  7. django 两种创建模型实例的方法
  8. node的events模块
  9. 最短Hamilton路径 数位dp
  10. 使用urllib