FIR example on the Chisel 3 website with more idiomatic Scala functional programming concepts

48 views
Skip to first unread message

Øyvind Harboe

unread,
Nov 27, 2021, 1:21:01 PM11/27/21
to chisel-users
I wondered why the example on the Chisel website isn't using more idomatic functional programming concepts...



So rather than:

  zs(0) := io.in
  for (i <- 1 until coeffs.length) { zs(i) := zs(i-1) } 

  // Do the multiplies
  val products = VecInit.tabulate(coeffs.length)(i => zs(i) * coeffs(i))
  
Use: 

  zs := Seq(io.in) ++ zs.tail

  // Do the multiplies
  val products = (zs zip coeffs).map{case (a, b) => a * b}

michael etzkorn

unread,
Nov 27, 2021, 4:43:18 PM11/27/21
to chisel-users
There's an example of this module doing exactly that in some tutorial slides somewhere (I know cause my coworker found it and asked me about map). I think the motivation for not doing so (and probably why the version with map was difficult for me to find on my own) is that the example needs to be accessible to novices wanting to pick up Chisel. We need to have examples targeted to beginners and people who've never seen software techniques on hardware data structures.

Jack Koenig

unread,
Nov 30, 2021, 3:27:50 PM11/30/21
to chisel...@googlegroups.com
Michael is right that the main motivation there is trying to appeal to a wider (less experienced in functional programming audience). That being said, I wouldn't be opposed to having a tutorial taking that original example and turning it into the more functional version.

--
You received this message because you are subscribed to the Google Groups "chisel-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chisel-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chisel-users/acdc4b1e-b1d6-4b83-9454-9743e4148690n%40googlegroups.com.

Øyvind Harboe

unread,
Nov 30, 2021, 4:33:16 PM11/30/21
to chisel...@googlegroups.com
Where in the source code would such a pull request go?

You received this message because you are subscribed to a topic in the Google Groups "chisel-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/chisel-users/0AznTGToRlk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to chisel-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/chisel-users/CAFooY_SimRxa_coNfGAGW6vHf9LzDisnDspCOyCKK4Wd7cxiMw%40mail.gmail.com.


--
Øyvind Harboe
+4791786146

Jack Koenig

unread,
Dec 1, 2021, 6:49:37 PM12/1/21
to chisel...@googlegroups.com
All docs for the chisel3 section on the Chisel website (https://www.chisel-lang.org/chisel3) is located in the chisel3 repo in the docs directory: https://github.com/chipsalliance/chisel3/tree/master/docs.

If you wanted to add a new file it also needs to be mentioned in the menu.yml in the website repo: https://github.com/freechipsproject/www.chisel-lang.org/blob/c6651c5d4f35fd8c033f0a654513c646fc53ed19/docs/src/main/resources/microsite/data/menu.yml

Michael Etzkorn

unread,
Dec 30, 2021, 3:15:47 PM12/30/21
to chisel-users
I did find one of the original versions of the FIR example in the Berkeley Chisel Tutorial paper from 2017. 

def Delays[T <: Data](x: T, n: Int): List[T] = if (n <= 1) List(x) else x :: Delays(RegNext(x), n-1) 

def FIR[T <: Data with Num[T]](ws: Seq[T], x: T): T = (ws, Delays(x, ws.length)).zipped. map( _ * _ ).reduce( _ + _ )  

Definitely like this style of using functions to instantiate the logic since it's like building a library of hardware that can then be worked into the modules. 

Paper can be found here: 
Reply all
Reply to author
Forward
0 new messages