Chisel 學習筆記(五)


Chisel 學習筆記(五)

Chisel中的時序邏輯

寄存器reg

val register = Reg(UInt(12.W))

class RegisterModule extends Module {
  val io = IO(new Bundle {
    val in  = Input(UInt(12.W))
    val out = Output(UInt(12.W))
  })
  
  val register = Reg(UInt(12.W))
  register := io.in + 1.U
  io.out := register
}

還可以通過RegNext來實例化出來一個寄存器

class RegNextModule extends Module {
  val io = IO(new Bundle {
    val in  = Input(UInt(12.W))
    val out = Output(UInt(12.W))
  })
  
  // register bitwidth is inferred from io.out
  io.out := RegNext(io.in + 1.U)
}

step(n)可以改變n次時鍾,常用於測試中

class RegisterModuleTester(c: RegisterModule) extends PeekPokeTester(c) {
  for (i <- 0 until 100) {
    poke(c.io.in, i)
    step(1)
    expect(c.io.out, i+1)
  }
}

可以通過RegInit來讓寄存器中初始附帶特定值

val myReg = RegInit(UInt(12.W), 0.U)
val myReg = RegInit(0.U(12.W))

有如下例子

classclass  RegInitModuleRegInit  extends Module {
  val io = IO(new Bundle {
    val in  = Input(UInt(12.W))
    val out = Output(UInt(12.W))
  })
  
  val register = RegInit(0.U(12.W))
  register := io.in + 1.U
  io.out := register
}

必須要使用RegInit,否則寄存器里的初值未知

可以在測試時使用reset(n)來使reset信號有效n個周期

精確時鍾和復位

Chisel中對於寄存器有默認的同步復位reset和時鍾clk,但如果想自己加入額外的復位信號和額外的時鍾信號,就要用到withClock/withReset/withClockAndReset
withClock(a){}意味着在a的上升沿會觸發什么
withReset(a){}意味着在標准時鍾上升沿a有效時復位
withClockAndReset(a,b){}意味着在a的上升沿,b有效時復位

class ClockExamples extends Module {
  val io = IO(new Bundle {
    val in = Input(UInt(10.W))
    val alternateReset    = Input(Bool())
    val alternateClock    = Input(Clock())
    val outImplicit       = Output(UInt())
    val outAlternateReset = Output(UInt())
    val outAlternateClock = Output(UInt())
    val outAlternateBoth  = Output(UInt())
  })

  val imp = RegInit(0.U(10.W))
  imp := io.in
  io.outImplicit := imp

  withReset(io.alternateReset) {
    // everything in this scope with have alternateReset as the reset
    val altRst = RegInit(0.U(10.W))
    altRst := io.in
    io.outAlternateReset := altRst
  }

  withClock(io.alternateClock) {
    val altClk = RegInit(0.U(10.W))
    altClk := io.in
    io.outAlternateClock := altClk
  }

  withClockAndReset(io.alternateClock, io.alternateReset) {
    val alt = RegInit(0.U(10.W))
    alt := io.in
    io.outAlternateBoth := alt
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    println("Generating the Adder hardware")
    chisel3.Driver.execute(Array("--target-dir", "generated"), () => new ClockExamples)
  }
}

生成的verilog中相關部分如下

module ClockExamples( // @[:@3.2]
  input        clock, // @[:@4.4]
  input        reset, // @[:@5.4]
  input  [9:0] io_in, // @[:@6.4]
  input        io_alternateReset, // @[:@6.4]
  input        io_alternateClock, // @[:@6.4]
  output [9:0] io_outImplicit, // @[:@6.4]
  output [9:0] io_outAlternateReset, // @[:@6.4]
  output [9:0] io_outAlternateClock, // @[:@6.4]
  output [9:0] io_outAlternateBoth // @[:@6.4]
);
  reg [9:0] imp; // @[Passthrough.scala 137:20:@8.4]
  reg [31:0] _RAND_0;
  reg [9:0] _T_23; // @[Passthrough.scala 143:25:@11.4]
  reg [31:0] _RAND_1;
  reg [9:0] _T_26; // @[Passthrough.scala 149:25:@14.4]
  reg [31:0] _RAND_2;
  reg [9:0] _T_29; // @[Passthrough.scala 155:22:@17.4]
  reg [31:0] _RAND_3;
  assign io_outImplicit = imp; // @[Passthrough.scala 139:18:@10.4]
  assign io_outAlternateReset = _T_23; // @[Passthrough.scala 145:26:@13.4]
  assign io_outAlternateClock = _T_26; // @[Passthrough.scala 151:26:@16.4]
  assign io_outAlternateBoth = _T_29; // @[Passthrough.scala 157:25:@19.4]

  always @(posedge clock) begin
    if (reset) begin
      imp <= 10'h0;
    end else begin
      imp <= io_in;
    end
    if (io_alternateReset) begin
      _T_23 <= 10'h0;
    end else begin
      _T_23 <= io_in;
    end
  end
  always @(posedge io_alternateClock) begin
    if (reset) begin
      _T_26 <= 10'h0;
    end else begin
      _T_26 <= io_in;
    end
    if (io_alternateReset) begin
      _T_29 <= 10'h0;
    end else begin
      _T_29 <= io_in;
    end
  end
endmodule


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM