5

I have some similar register definition, and I want to write under the regmap construct. My code currently looks like this:

val regs = RegInit(Vec(Seq.fill(5)(0.U(32.W))))

regmap (
...
0x30 -> Seq(RegField(32,regs(0),RegFieldDesc("reg0",""),
0x34 -> Seq(RegField(32,regs(1),RegFieldDesc("reg1",""),
0x38 -> Seq(RegField(32,regs(2),RegFieldDesc("reg2",""),
0x3C -> Seq(RegField(32,regs(3),RegFieldDesc("reg3",""),
0x40 -> Seq(RegField(32,regs(4),RegFieldDesc("reg4",""),
...

) 

My question, is there a way to write the above in more concise way using one of the Scala Iterators? Another requirement I have is that I still need to be able to add register before and after this iterator (3 dots lines).

I believe ,using iterators is good against copy/paste mistakes and looks better.
Thanks in advance for any help.

Jack Koenig
  • 5,840
  • 15
  • 21
user3567895
  • 618
  • 3
  • 14

1 Answers1

3

I think the pattern for this would probably be something like

val regs = RegInit(Vec(Seq.fill(5)(0.U(32.W))))

val tuples = regs.zipWithIndex.map { case (reg, i) =>
  (0x30 + (i * 4)) -> Seq(RegField(32,regs,RegFieldDesc(s"reg$i","")))
}
regmap(tuples :_*)

The only bit of magic there is the :_* which converts a sequence into a list of parameters.You don't need the multiple steps that I used either, I just wanted to make it easy to see what is going on.

Chick Markley
  • 4,023
  • 16
  • 17
  • Thank @Chick Markley for your answer, I am a little bit puzzled with the :_* . can you elaborate on that or point me to a web resource to learn about it ? – user3567895 Dec 14 '18 at 00:48
  • Checkout https://stackoverflow.com/questions/6051302/what-does-colon-underscore-star-do-in-scala. Basically in converts a Seq/Collection into a form where it can fill out the parameters of a function with a varargs signature, something like f(varArgs: Int*). It's cool, but doesn't come up too often – Chick Markley Dec 14 '18 at 19:06
  • What about the commas @Chick Markley , do I need to add them to the tuples creating loop body ? – user3567895 Apr 10 '19 at 06:52
  • I also get the next errors, when trying that : "no `: _*' annotation allowed here [error] (such annotations are only allowed in arguments to *-parameters) [error] iqRegs :_*" – user3567895 Apr 10 '19 at 07:12
  • Can you provide a code snippet, I'm not sure what commas you are referring to, or what line is giving the annotations error – Chick Markley Apr 10 '19 at 16:24