Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Clone in Chisel

Tags:

scala

chisel

I am a new learner of Chisel. What is the purpose of Cloning in Chisel? I saw somewhere written, "it creates a shallow copy". Why do we need it? Here are examples. Could you please elaborate it.

1)

  class Valid[+T <: Data](gen: T) extends Bundle
    {
    val valid = Output(Bool())
    val bits = Output(gen.chiselCloneType)//?????
    def fire(): Bool = valid
    override def cloneType: this.type = Valid(gen).asInstanceOf[this.type]
    }
    /** Adds a valid protocol to any interface */
    object Valid {
    def apply[T <: Data](gen: T): Valid[T] = new Valid(gen)
    }

2)

class Packet(n: Int, w: Int) extends Bundle {
val address = UInt(Log2Up(n).W)
val payload = UInt(w.W)
override def cloneType: this.type =
new Packet(n, w).asInstanceOf[this.type]
}

Why cloneType is Override. Is it like an apply method in Scala or it just only updates the cloneType method in Bundle.

Thanks

like image 954
ARK91 Avatar asked Jan 04 '18 23:01

ARK91


1 Answers

A typical use case for bundles in Chisel is to create an instance with a particular set of parameters of a bundle then use that instance as a template. Using it as a template means creating a new instance that is the same type. In many cases Chisel can do the cloning automatically and the user does not need implement cloneType but currently limitations of scala and chisel (usually when the bundle has multiple parameters) chisel cannot figure out how to implement the copy and the developer must implement the clonetype manually. Recent developments in chisel will nearly eliminate the need to implement cloneType. This is part of release of 3.1.0 schedule for release this month. See the autoclonetype issue for details.

like image 51
Chick Markley Avatar answered Sep 25 '22 03:09

Chick Markley