SpinalHDL icon indicating copy to clipboard operation
SpinalHDL copied to clipboard

Question: Is there a way to get the SpinalConfig object during elaboration phase?

Open parthkalgaonkar opened this issue 7 months ago • 3 comments

I'm new to this so the wording might be a bit confusing. Also, there is a chance that this might be an instance of the XY problem as well.

Essentially what I want to do is call an external tool (a couple of python scripts) that does some RTL generation. I have already created Blackbox sub-classes corresponding to the generated code. Now, instead of having to call the external tool in a separate step, I thought it might be a good idea if I run python using scala.sys.process or something similar.

I've configured my SpinalConfig to generate files in a gen/ folder in the workspace root. I'd like to make it so my external tool also dumps files in the same path. But I don't want to hard-code the path when I'm calling my script. I'd like it if I can just set the targetDirectory once in SpinalConfig and access it at the place where I'm caling scala.sys.process. Is there a way to achieve something like this?

Best Regards,

parthkalgaonkar avatar Apr 16 '26 02:04 parthkalgaonkar

My structure looks like this

# my_script.py
# This generates some rtl (sv)
# One of the arguments here tells the folder in which to generate
// TopLevel.scala
object TopLevel extends App {
  config = SpinalConfig(
    targetDirectory = "gen",    // I want to be able to change this and have it reflect in <out-path> below
    ...
  )
  config.generateSystemVerilog(new TopLevel())
}
// SubModule.scala
import scala.sys.process._

class SubModule {
  def apply(): Unit = {
    "python my_script.py -o <out-path>".!   // I want <out-path> to track targetDirectory above
  }
}

I hope I've made my question clear.

parthkalgaonkar avatar Apr 16 '26 02:04 parthkalgaonkar

Hello,

I'm a bit new as well to scala and spinal but I think I can answer your question.

If you add a def in front of config = ... It become a method and is accessible outside of TopLevel. On my side it seems that println(TopLevel.config.targetDirectory) compile fine.

You can also have the a constant outside the TopLevel and initialize targetDirectory with it.

Also I don't know your workflow, but perhaps you should handle the generation of your files with the build system (like with sbt). Because invoking executables to build stuffs that are requered by other stuffs look much like a build system problem. The drawbacks is you have to dig into how to do it with the build system instead of only running process in scala. But depending on your use case, in the long run, it's more efficient (think sbt clean, unit tests and bitstream needing to invoke you script, etc.). In this case you can get the gen dir as command line argument and pass it with the build system when invoking TopLevel.

Hope this help.

mrcmry avatar Apr 16 '26 19:04 mrcmry

Hi,

You can do :

object BrawChakalaka42 extends App {
  val sc = SpinalConfig(device = Device.ASIC)
  sc.generateVerilog(new Component {
    println(GlobalData.get.config.device.vendor)
    println(GlobalData.get.config.targetDirectory)
  })
}

GlobalData.get.config is a the SpinalConfig used by the elaboration.

You can mutate the content of the GlobalData.get.config, but not the GlobalData.get.config reference itself. Note that not all attribute of SpinalConfig are var

Dolu1990 avatar May 09 '26 10:05 Dolu1990