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
297 stars 21 forks source link

Check simulation status via TCP/IP socket #200

Closed georgtree closed 1 month ago

georgtree commented 2 months ago

Hello! I have a question: how to check if simulation finished via TCP/IP socket from TCL script? I was able to connect to xschem in non-gui, detached from terminal regime, send commands to open file, netlist, simulate, etc. As I understand, I have to check some status variable to understand that simulation is finished and I can load .raw file. Maybe it is more wise to use xschem only for netlist generation and reading raw files, while simulation is run with ngspice directly... Could you please point in right direction? Thank you in advance, George.

georgtree commented 2 months ago

And the same question about other commands - how to make sure that particular operation is finished?

georgtree commented 2 months ago

Update - there is an optional callback, but it doesn't run when simulation finished - just at the moment it is started. I think it depends on how the simulation command is built...

georgtree commented 2 months ago

Ok, I found the way, the simulation should be set background set sim(spice,2,fg) 0, and the call back works just fine :)

georgtree commented 2 months ago

Maybe there is a more elegant way than using socket on local machine? I think about sending commands directly to xschem tcl console, but not sure how do this :(

StefanSchippers commented 2 months ago

Hi, George, this is an example of using xschem as a sub process, the script sends commands to xschem stdin and waits until xschem has completed, printing the output. The script is done in tcl, I guess the same function can be implemented in python or other languages. You need to set asynchronous read mode (fconfigure $fd -buffering line -blocking 0) and whenever data is available call a procedure (fileevent $fd readable "readproc $fd") The script sends commands to xschem (using senddata procedure) to load a schematic (rom8k.sch) and prints some informations about the schematic. You can use any schematic you have (either give only the file name if it is in one of the xschem search path directories or give full path name).

#### called whenever $fd becomes readable
proc readproc {fd} {
  global xschem_data
  set data {}
  set first 1
  while {1} {
    set ret [gets $fd a]
    if {$ret > 0} {
      if {$first == 0} {
        append data \n
      }
      append data $a
      set first 0
    } elseif {$ret == -1} {
      #### finished reading
      break
    }
  }
  set xschem_data $data ;#### unlocks vwait in senddata
}

proc senddata {fd data} {
  global xschem_data
  #### send the string "puts [$data]" (without quotes) to xschem
  puts $fd "puts \[$data\]"
  #### wait until output has been read
  vwait xschem_data
}

#### run subprocess, read/write pipe
set fd [open "|xschem -r -x" r+]

#### line buffering,  non blocking
fconfigure $fd -buffering line -blocking 0

#### asynchronous read procedure
fileevent $fd readable "readproc $fd"

#### senddata $fd {xschem load poweramp.sch}
# senddata $fd {xschem load test_bigschematic.sch}
senddata $fd {xschem load rom8k.sch}
senddata $fd {xschem instance_list}
puts "List of instances:"
puts $xschem_data
senddata $fd {xschem list_nets}
puts "\nList of nets:"
puts $xschem_data
senddata $fd {xschem get current_name}
puts "\nSchematic name: --- $xschem_data ---"

#### gracefully quit subprocess
senddata $fd exit
#### close pipe
close $fd
StefanSchippers commented 2 months ago

Here a simple way (in the xschem tcl console) to check if simulation is running or if it has finished. Any time you call simulate an id variable is returned. This id can be used to query simulation status. xschem sets various variables when running a simulation, one of these is array variable execute(cmd,$id) , it returns the ngspice command line being executed. When simulation is finished the array variable is deleted. you can use info exists execute(cmd,$id) to check if simulation is running. The use of the id variable allows you to run multiple simulations in parallel.

schippes@asus:~$ xschem poweramp.sch
xschem [~] xschem netlist
0
xschem [~] set id [simulate]
Simulation started: execution ID: 0
0
xschem [~] puts $execute(cmd,$id)
xterm -e {ngspice -i "/home/schippes/.xschem/simulations/poweramp.spice" -a || sh}
...
...
xschem [~] puts $execute(cmd,$id)
can't read "execute(cmd,0)": no such element in array
while evaluating puts $execute(cmd,$id)
xschem [~] info exists execute(cmd,$id)
0

The following example script runs 3 simulations in parallel on a schematic poweramp.sch and waits until all 3 sims are completed.

xschem load poweramp.sch
set id_list {}

xschem netlist
set id [simulate]
# some delay to allow ngspice to start and read the netlist before changing it
delay 1000
lappend id_list $id

# change VPP, output file name and input signal amplitude
set commands [xschem getprop instance NGSPICE value]
regsub {\.param VPP=50} $commands {.param VPP=30} commands
regsub -all {poweramp\.raw} $commands {poweramp1.raw} commands
xschem setprop instance NGSPICE value $commands
xschem setprop instance V3 value {dc 0 sin 0 0.5 {frequ} 1m}

xschem netlist
set id [simulate]
# some delay to allow ngspice to start and read the netlist before changing it
delay 1000
lappend id_list $id

# change again VPP and output file name
regsub {\.param VPP=30} $commands {.param VPP=25} commands
regsub -all {poweramp1\.raw} $commands {poweramp2.raw} commands
xschem setprop instance NGSPICE value $commands

xschem netlist
set id [simulate]
# some delay to allow ngspice to start and read the netlist before changing it
delay 1000
lappend id_list $id

# poll loop
while {1} {
  delay 1000
  set busy 0
  foreach id $id_list {
    set var "execute(cmd,$id)"
    if {[info exists $var]} { set busy 1}
  }
  if {!$busy} { break }
}
puts Done
georgtree commented 1 month ago

Wow, thank you, it is much more than I expected, thank you, I will use this example for control of xschem from tcl script!

StefanSchippers commented 1 month ago

If there is something you can't get working let me know. All these things need some 3rd party testing :-)

georgtree commented 1 month ago

I tried both things, all works, thank you!