SpinalDoc-RTD icon indicating copy to clipboard operation
SpinalDoc-RTD copied to clipboard

Add section for idioms

Open numero-744 opened this issue 3 years ago • 7 comments

Spinal enables representing the code in a way which is not possible in RTL.

This section would show idioms which are useful in Spinal. For each idiom, it would explain it, how to use it, when and why it is good to use it, and maybe how it works.

Example of idiom:

val doSomethingFlag = False
def doSomething(): Unit = doSomethingFlag := True

I find it useful in fsm.State.whenActive to express the output logic.

  val fsm = new StateMachine {
    val IDLE, Rnd0, RndI, RndN, DONE = new State
    setEntry(IDLE)

    // Transition logic
    IDLE.whenIsActive { when(io.start) { goto(Rnd0) } }
    Rnd0.whenIsActive { goto(RndI) }
    RndI.whenIsActive { when(round.isLast) { goto(RndN) } }
    RndN.whenIsActive { goto(DONE) }
    DONE.whenIsActive { when(!io.start) { goto(IDLE) } }

    // Output logic, using only this idiom
    IDLE.whenIsActive { round.resetCount }
    Rnd0.whenIsActive { run; useInputs }
    RndI.whenIsActive { run }
    RndN.whenIsActive { run; withoutMixColumns }
    DONE.whenIsActive { enableOutput }
  }

numero-744 avatar Oct 24 '22 13:10 numero-744

Yes, such section would be usefull, showing all kind of funny / creative thing

Dolu1990 avatar Oct 24 '22 13:10 Dolu1990

Nice Work! This will be a shining pool of sparks for SpinalHDL.

Readon avatar Oct 24 '22 15:10 Readon

Maybe we can do a call to discuss about patterns ?

Dolu1990 avatar Oct 25 '22 17:10 Dolu1990

I was thinking that each pattern would be introduced by a PR into the which we could discuss but a call might be good too.

numero-744 avatar Oct 25 '22 22:10 numero-744

sure.

Dolu1990 avatar Oct 26 '22 15:10 Dolu1990

Note for when I will have created this section:

Function-like components: a companion object with an apply.

  object MyComponent {
    def apply(i: MyEnum.C): Bits = {
      val myComponent = new MyComponent
      myComponent.io.i := i
      myComponent.io.o
    }
  }

numero-744 avatar Nov 15 '22 12:11 numero-744

Once idioms are defined we can provide snippets (for instance providing them in the template) for IDEs. For instance:

	"Doing flag": {
		"scope": "scala",
		"prefix": "doing",
		"body": ["val ${2:${1}ing} = False", "def $1(): Unit = $2 := True", ""]
	}

We can also provide such snippets for Component etc. Providing this kind of ready-to-use tools can improve the Scala UX XD

With this example snippet in VSCodium with 8 keystrokes:

do<Tab>run<Tab><Tab>

I get:

val runing = False
def run(): Unit = runing := True

(It can require more than the 2 keystrokes do to reach the doing snippet depending on the coding context)

numero-744 avatar Nov 17 '22 17:11 numero-744