joe 发表于 2021-9-5 22:19:34

RegFifo

import chisel3._
import chisel3.util._
class FifoIO(private val gen:T) extends Bundle {
val enq = Flipped(new DecoupledIO(gen))   //DecoupledIO-->>ReadyValidIO-->>Bundle是带有valid和ready的复杂信号Flipped则将bits,ready和valid信号切换方向
val deq = new DecoupledIO(gen) //deq是正常的bits,ready,valid方向
}
abstract class Fifo(gen:T,depth:Int) extends Module {//带有valid和ready的FIFO,并且有二个“group”,一个是enq,一个是deq
val io = IO(new FifoIO(gen))
assert(depth > 0,"Number of buffer elements needs to be larger than 0")
}

import chisel3._
import chisel3.util._
class RegFifo(gen:T,depth:Int) extends Fifo(gen,depth) {
def counter(depth:Int,incr:Bool): (UInt,UInt) = {
    val cntReg = RegInit(0.U(log2Ceil(depth).W)) //log2Ceil(n:BigInt)求log2的值且向上取整 log2Ceil(1)->0,log2Ceil(2)->1,log2Ceil(3)->2,log2Ceil(4)->2,log2Ceil(5)->3
    //要想获取n值所对应的bit数量则log2Ceil(n+1)4-->0100->3bitlog2Ceil(4+1)=3
    //cntReg表示深度depth-1值所需要的bit位数
    val nextVal = Mux(cntReg === (depth - 1).U,0.U,cntReg + 1.U) //达到最大深度则复位,否则下次值为cntReg加1
    when (incr) {
      cntReg := nextVal//只要没有达到最大深度,每次加1
    }
    (cntReg,nextVal) //以元组类型返回计数器值以下一次值
}
val memReg = Reg(Vec(depth,gen)) //深度为depth的向量
val incrRead = WireInit(false.B)
val incrWrite = WireInit(false.B)
val (readPtr,nextRead) = counter(depth,incrRead) //返回的元组中的readPtr和nextRead都是Reg硬件类型???
val (writePtr,nextWrite) = counter(depth,incrWrite)

val emptyReg = RegInit(true.B) //FIFO空标志
val fullReg = RegInit(false.B) //FIFO满标志

when(io.enq.valid && !fullReg) { //收到MASTER发来的valid,且FIFO不满
    memReg(writePtr) := io.enq.bits //writePtr指针所指的FIFO缓冲区的数据被设置为MASTER发来的数据
    emptyReg := false.B//需要清空 空标志位
    fullReg := nextWrite === readPtr //写指针和读指针相同时则置 满标志
    incrWrite := true.B//启动写计数
}
when(io.deq.ready && !emptyReg) { //返回数据给MASTER的控制相关逻辑
    fullReg := false.B //清满标志位
    emptyReg := nextRead === writePtr //读写指针相等时则为空
    incrRead := true.B //启动读计数
}
io.deq.bits := memReg(readPtr) //根据读指针将缓冲区对应位置的数据返回
io.enq.ready := !fullReg//output出准备好信号 只要不满
io.deq.valid := !emptyReg //只要不空 output类型的deq.valid就发出指标
}

joe 发表于 2021-9-5 22:20:53

Driver.execute(Array("--target-dir","generated"),()=>new RegFifo(UInt(4.W),5))
产生的verilog文件内容如下:
module RegFifo(
input      clock,
input      reset,
output       io_enq_ready,
input      io_enq_valid,
input io_enq_bits,
input      io_deq_ready,
output       io_deq_valid,
output io_deq_bits
);
reg memReg_0; // @
reg memReg_1; // @
reg memReg_2; // @
reg memReg_3; // @
reg memReg_4; // @
reg readPtr; // @
wire_T = readPtr == 3'h4; // @
wire _T_2 = readPtr + 3'h1; // @
wire nextRead = _T ? 3'h0 : _T_2; // @
regemptyReg; // @
reg _RAND_6;
wire_T_9 = ~emptyReg; // @
wireincrRead = io_deq_ready & _T_9; // @
reg writePtr; // @
reg _RAND_7;
wire_T_3 = writePtr == 3'h4; // @
wire _T_5 = writePtr + 3'h1; // @
wire nextWrite = _T_3 ? 3'h0 : _T_5; // @
regfullReg; // @
reg _RAND_8;
wire_T_6 = ~fullReg; // @
wireincrWrite = io_enq_valid & _T_6; // @
wire_T_8 = nextWrite == readPtr; // @
wire_GEN_12 = incrWrite ? 1'h0 : emptyReg; // @
wire_T_11 = nextRead == writePtr; // @
wire_GEN_16 = incrRead ? _T_11 : _GEN_12; // @
wire _GEN_19 = 3'h1 == readPtr ? memReg_1 : memReg_0; // @
wire _GEN_20 = 3'h2 == readPtr ? memReg_2 : _GEN_19; // @
wire _GEN_21 = 3'h3 == readPtr ? memReg_3 : _GEN_20; // @
assign io_enq_ready = ~fullReg; // @
assign io_deq_valid = ~emptyReg; // @
assign io_deq_bits = 3'h4 == readPtr ? memReg_4 : _GEN_21; // @

always @(posedge clock) begin
    if (incrWrite) begin
      if (3'h0 == writePtr) begin
      memReg_0 <= io_enq_bits;
      end
    end
    if (incrWrite) begin
      if (3'h1 == writePtr) begin
      memReg_1 <= io_enq_bits;
      end
    end
    if (incrWrite) begin
      if (3'h2 == writePtr) begin
      memReg_2 <= io_enq_bits;
      end
    end
    if (incrWrite) begin
      if (3'h3 == writePtr) begin
      memReg_3 <= io_enq_bits;
      end
    end
    if (incrWrite) begin
      if (3'h4 == writePtr) begin
      memReg_4 <= io_enq_bits;
      end
    end
    if (reset) begin
      readPtr <= 3'h0;
    end else if (incrRead) begin
      if (_T) begin
      readPtr <= 3'h0;
      end else begin
      readPtr <= _T_2;
      end
    end
    emptyReg <= reset | _GEN_16;
    if (reset) begin
      writePtr <= 3'h0;
    end else if (incrWrite) begin
      if (_T_3) begin
      writePtr <= 3'h0;
      end else begin
      writePtr <= _T_5;
      end
    end
    if (reset) begin
      fullReg <= 1'h0;
    end else if (incrRead) begin
      fullReg <= 1'h0;
    end else if (incrWrite) begin
      fullReg <= _T_8;
    end
end
endmodule
页: [1]
查看完整版本: RegFifo