StefanSchippers / xschem

A schematic editor for VLSI/Asic/Analog custom designs, netlist backends for VHDL, Spice and Verilog. The tool is focused on hierarchy and parametric designs, to maximize circuit reuse.
Other
300 stars 22 forks source link

[RFC] Make filename (without extension) available as environment variable to ngspice #168

Closed qpwo closed 5 months ago

qpwo commented 5 months ago

Problem: I copy or rename circuit1.sch to circuit2.sch and make some changes and do my sim but the plots aren't working because I was doing write circuit1.raw!

Solution: Xschem could set $base and your ngspice code can say write {$base}.raw. Then schematics are fully copyable/renamable!

I have my simulator in xschem configured to

gnome-terminal -- bash -c "base=`basename $N .spice` ngspice -i $N -a"

But if $base was enabled by default then the skywater example schematics could use it and it would be a lot easier for newbs to copy them and make changes.

StefanSchippers commented 5 months ago

This is available as a simple tcl command: in the xschem terminal: xschem get current_name returns the schematic name, like solar_panel.sch file rootname [xschem get current_name] returns solar_panel

So in your code block you can use this command to write the raw file with the same name as the schematic:

3

Note the whole command is wrapped inside a tcleval(....) construct. This will invoke a round of tcl substitution to resolve $tcl_variables and [tcl commands] I will also add a sch_basename global TCL variable so the expression [file rootname [xschem get current_name]].raw can be simplified as $sch_basename. Escaping can be used if you want a literal $something that must not be substituted, like \\$something.raw

(for the very nerd: double escaping is needed because the whole string is included in double quotes (value="...") and \" can be used to enter a literal quote, so a \\ will enter a literal \ that will escape the $ to the TCL interpreter. If you don't get this quirk no problem, just ignore this detail :-) )

qpwo commented 5 months ago

Great seems to work:

image

qpwo commented 5 months ago

Oh great I don't even need the [return ...].

image

StefanSchippers commented 5 months ago

Correct, the whole expression inside tcleval( ..... ) is passed to a tcl subst command. The subst command takes the input string and returns the string with [ ....] replaced with the return value of the associated tcl command and $xxx as the value for xxx variable. sotcleval(test [return tcl] $x)will return test tcl substitution, assuming x variable is set to substitution