1. 程式人生 > >Chisel3 - model - DefWire, Reg, Memory, Prim

Chisel3 - model - DefWire, Reg, Memory, Prim

https://mp.weixin.qq.com/s/KkkqvekWHG6yrqwHUECRIw   介紹如何定義Wire/Reg/Memory/Prim。   1. DefWire   Wire()表明內括的Data的容器為線,用法為:   Wire()定義如下: a. 獲取一個t的克隆x; b. 定義一個Definition, 即為DefWire:DefWire(sourceInfo, x) c. DefWire同時也是一個Command,將其存下:pushCommand(DefWire(sourceInfo, x))
  pushCommand()定義如下: 把命令c即DefWire,加入到forcedUserModule中。     2. DefReg   Reg()定義如下: 同樣呼叫pushCommand()把定義暫存器的命令(DefReg),新增到forcedUserModule中。     3. DefMemory   Mem()定義如下: 呼叫pushCommand()把定義記憶體的命令(DefMemory),新增到forcedUserModule中。
  4. DefPrim   以加法為例。   作為抽象方法,定義在Num中:   在子類UInt中實現,       5. 附錄   Wire(): trait WireFactory { def apply[T <: Data](t: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = {
if (compileOptions.declaredTypeMustBeUnbound) { requireIsChiselType(t, "wire type") } val x = t.cloneTypeFull   // Bind each element of x to being a Wire x.bind(WireBinding(Builder.forcedUserModule))   pushCommand(DefWire(sourceInfo, x)) if (!compileOptions.explicitInvalidate) { pushCommand(DefInvalid(sourceInfo, x.ref)) }   x } }   Reg(): object Reg { /** Creates a register without initialization (reset is ignored). Value does * not change unless assigned to (using the := operator). * * @param t: data type for the register */ def apply[T <: Data](t: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): T = { if (compileOptions.declaredTypeMustBeUnbound) { requireIsChiselType(t, "reg type") } val reg = t.cloneTypeFull val clock = Node(Builder.forcedClock)   reg.bind(RegBinding(Builder.forcedUserModule)) pushCommand(DefReg(sourceInfo, reg, clock)) reg } }   Mem(): object Mem { @chiselRuntimeDeprecated @deprecated("Mem argument order should be size, t; this will be removed by the official release", "chisel3") def apply[T <: Data](t: T, size: Int)(implicit compileOptions: CompileOptions): Mem[T] = do_apply(size, t)(UnlocatableSourceInfo, compileOptions)   /** Creates a combinational/asynchronous-read, sequential/synchronous-write [[Mem]]. * * @param size number of elements in the memory * @param t data type of memory element */ def apply[T <: Data](size: Int, t: T): Mem[T] = macro MemTransform.apply[T] def do_apply[T <: Data](size: Int, t: T)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Mem[T] = { if (compileOptions.declaredTypeMustBeUnbound) { requireIsChiselType(t, "memory type") } val mt = t.cloneTypeFull val mem = new Mem(mt, size) pushCommand(DefMemory(sourceInfo, mem, mt, size)) mem } }