risc-v中文社区

 找回密码
 立即注册
查看: 1178|回复: 1

[原创] 用chisel做一个2-4译码器电路

[复制链接]

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
发表于 2021-8-17 23:04:09 | 显示全部楼层 |阅读模式
import chisel3._
import chisel3.util._
class DeCoder extends RawModule{
  val io = IO(new Bundle{
    val a = Input(UInt(1.W))
    val b = Input(UInt(1.W))
    val c = Output(UInt(4.W))
  })
  val sel = Cat(io.a,io.b)
  io.c := 0.U //必须要有默认值 否则:firrtl.passes.CheckInitialization$RefNotInitializedException
  switch(sel) {
    is(0.U) { io.c := 1.U}
    is(1.U) { io.c := 2.U}
    is(2.U) { io.c := 3.U}
    is(3.U) { io.c := 4.U}
  }
}
对应的verilog文件内容如下:
module DeCoder(
  input        io_a,
  input        io_b,
  output [3:0] io_c
);
  wire [1:0] sel = {io_a,io_b}; // @[Cat.scala 30:58]
  wire  _T = 2'h0 == sel; // @[Conditional.scala 37:30]
  wire  _T_1 = 2'h1 == sel; // @[Conditional.scala 37:30]
  wire  _T_2 = 2'h2 == sel; // @[Conditional.scala 37:30]
  wire  _T_3 = 2'h3 == sel; // @[Conditional.scala 37:30]
  wire [2:0] _GEN_0 = _T_3 ? 3'h4 : 3'h0; // @[Conditional.scala 39:67]
  wire [2:0] _GEN_1 = _T_2 ? 3'h3 : _GEN_0; // @[Conditional.scala 39:67]
  wire [2:0] _GEN_2 = _T_1 ? 3'h2 : _GEN_1; // @[Conditional.scala 39:67]
  wire [2:0] _GEN_3 = _T ? 3'h1 : _GEN_2; // @[Conditional.scala 40:58]
  assign io_c = {{1'd0}, _GEN_3}; // @[DeCoder.scala 12:8 DeCoder.scala 14:20 DeCoder.scala 15:20 DeCoder.scala 16:20 DeCoder.scala 17:20]
endmodule

回复

使用道具 举报

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
 楼主| 发表于 2021-8-17 23:05:00 | 显示全部楼层
可以看到用RawModule则生成的verilog中就没有clock和reset信号了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-4-27 19:01 , Processed in 0.020854 second(s), 26 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表