Skip to content

PARSA Chisel Coding Style

Shanqing Lin edited this page Oct 20, 2020 · 4 revisions

PARSA Chisel Coding Style (Proposal)

Motivation

See #21.

Naming Convention

Mostly we follow the Scala naming rules that use camel case for variable, function, and member names like backendAddress, and only capitalized the first letter when defining a class like TreeLRU or IOBundle. Some exceptions are listed as follows.

  1. Port definition. Each port including composite port must have a suffix to indicate the dataflow direction.
class SampleModule extends Module{
  val io = IO(new Bundle{
    // For primitive Chisel type, use _i/_o as its suffix.
    val data_i = Input(UInt(8.W)) 
    val addr_o = Output(UInt(10.W))
    // For bundle, suffix is the same as primitive type 
    // if all directions are the same.
    val dataGroup_o = Output(new Bundle{
      val addr = UInt(12.W)
      val data = UInt(512.W)
    })
    // This is also suitable for ValidIO / Valid
    val addrGroup_i = Flipped(Valid(new Bundle{
      val vAddr = UInt(64.W)
      val pAddr = UInt(32.W)
    }))
    // For DecoupledIO, keep the suffix with the direction of the bits.
    val controlGroup_o = DecoupledIO(new Bundle{
      val lock = UInt(8.W)
      val threadID = UInt(4.W)
    })
    // If you need another level of bundle which may contain ports with diverse directions,
    // please only add suffix to the port instead of the group.
    val backendPort = new Bundle{ // ignore `backendPort`
      val addr_o = UInt(8.W) // add suffix for the underlying port.
      val data_i = UInt(16.W)
    }
  })
}
  1. Register. Same as the port, a suffix _r is necessary to add to the end of the variable if it's supposed to be a register.
class SampleModuleB extends Module{
  val state_r = RegInit(0.U(8.W)) 
  val anotherState_r = Reg(UInt(16.W)) // stay another part to camel case while adding suffix.
}
  1. Enumeration. Use prefix s plus capitalized name to denote an enumeration value. You can add any _ in the name for your convenience.
val sREADING :: sWRITING_BACK :: sWAITING :: Nil = Enum(3)
  1. Variance. Same as scala convention, we encourage to use a single upper case letter like T and A to represent a Variance. If you really want to give a meaningful name, please keep it the same as the rule of naming classes. We suggest you add Type in the end to make it identified.
class SelfQueue[
  T <: Data, // use singe upper letter is good.
  CounterType <: Counter  // For details, keep upper camel case.
](t: T, counterType: CounterType) extends Module{
  
}