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

support RF simulators #171

Open joamatab opened 5 months ago

joamatab commented 5 months ago

How could we enable sax and scikit-rf for RF circuit simulations from xschem?

@flaport @proppy

StefanSchippers commented 5 months ago

Looking at scikit-rf it uses a python description of the RF circuit, so to use a library of RF elements a different "netlist" backend must be created. A Spice netlist is a list of text lines each representing a component, its attributes (what type of element, its value(s) ) and the list of nodes it is connected to.

scikit-rf as far as I understand separates circuit topology (the cnx object) from the list of elements.

I will take a look, but I am not a RF expert, plus I don't know the potential impact of such an addition to the community.

joamatab commented 5 months ago

I mostly use sax and we could easily write a command line script to simulate the netlist, how could we call that and retrieve the results?

StefanSchippers commented 5 months ago

So what you want to do is to have some Opto/RF symbols in xschem, create a circuit (by placing and connecting these elements) and get the circuit description that SAX can read and simulate? Should the definition of symbols be in some external file? or embedded in some way in the xschem symbols?

It is possible to generate only the structural netlist (how things are connected in the circuit), like in:

mzi, _ = sax.circuit(
    netlist={
        "instances": {
            "lft": coupler,
            "top": waveguide,
            "rgt": coupler,
        },
        "connections": {
            "lft,out0": "rgt,in0",
            "lft,out1": "top,in0",
            "top,out0": "rgt,in1",
        },
        "ports": {
            "in0": "lft,in0",
            "in1": "lft,in1",
            "out0": "rgt,out0",
            "out1": "rgt,out1",
        },
    }
)

or provide also the definitions, like:

...
...
 def coupler(coupling=0.5):
    kappa = coupling**0.5
    tau = (1-coupling)**0.5
    sdict = sax.reciprocal({
        ("in0", "out0"): tau,
        ("in0", "out1"): 1j*kappa,
        ("in1", "out0"): 1j*kappa,
        ("in1", "out1"): tau,
    })
    return sdict
...
...

In the mean time I will learn a bit about Sax.

joamatab commented 5 months ago

we use a YAML syntax to describe the layout (netlist + placement), we also can use YAML as a schematic representation in our Schematic driven layout in jupyter notebooks or using Ansys Interconnect

ideally from xschem we can extract the SPICE netlist, then convert it to YAML format which can be simulated by SAX, then use gdsfactory to generate the layout

it would also be able to programmatically create a symbol library fo the gdsfactory PDKs and also allow to create complex schematics using python

StefanSchippers commented 5 months ago

I have set up a test circuit:

1

and using the following tcl procedure:

proc sax_netlist {} {
  puts "\"instances\": \{"
  foreach {i s t} [xschem instance_list] {
    if { $t == {primitive} } {
      puts "  \"$i\": [file rootname $s],"
    }
  }
  puts "\},\n\"ports\": \{"
  set ports 1
  foreach i [xschem list_nets] {
    set net [lindex $i 0]
    set type [lindex $i 1]
    if {$type == {net} && $ports == 1} {
       set ports 0
       puts "\},\n\"connections\": \{"
    }
    if {$ports } {puts -nonewline "\"$net\":"}
    set first 1
    foreach c [xschem instances_to_net $net] {
      set inst [lindex $c 0]
      set pin [lindex $c 1]
      if {!$first} { puts -nonewline : }
      puts -nonewline " \"$inst,$pin\""
      set first 0
    }
    puts ","
  }
  puts "\},"
}

yields the following output:

"instances": {
  "lft": coupler,
  "top": waveguide,
  "rgt": coupler,
},
"ports": {
"out1": "rgt,out1",
"out0": "rgt,out0",
"in1": "lft,in0",
"in0": "lft,in1",
},
"connections": {
 "lft,out0": "rgt,in0",
 "lft,out1": "top,in0",
 "top,out0": "rgt,in1",
},

I think creating a YAML netlist is not diffcult. Of course more examples are to be tested and syntax must be checked for YAML format compliance. Just a proof of concept.

joamatab commented 5 months ago

amazing!

would it be possible to use python instead of tcl?

I've used python for the last 14 years but never used tcl before

StefanSchippers commented 5 months ago

xschem is written in C and uses tcl-tk mainly for the GUI and scripting interface. (Python did not exist when i started xschem).

Anyway the idea is to have the small exporter procedure in TCL that xschem can load at startup, then you can call xschem from python to get the netlist.

something like: xschem test.sch --command 'source test.tcl; sax_netlist' or just: xschem test.sch --command 'sax_netlist' if the procedure is sourced on startup.

StefanSchippers commented 5 months ago

You can also call xschem from python as a sub-process, send plain text commands to xschem's stdin and get responses from xschem's stdout.

The above script can be entirely written in python, sending these 3 commands to xschem:

All commands are documented here. Example of xschem's output based on the sample circuit above.

xschem instance_list returns a list of {instname} {symbol_name} {symbol_type} ...

xschem [~/.xschem/xschem_library/sax_rf_examples] xschem instance_list
{lft} {coupler.sym} {primitive}
{top} {waveguide.sym} {primitive}
{rgt} {coupler.sym} {primitive}
{p4} {opin.sym} {opin}
{p5} {opin.sym} {opin}
{p2} {ipin.sym} {ipin}
{p6} {ipin.sym} {ipin}

xschem list_nets returns a list of {net_name type}, where type is either ipin, iopin, opin or net depending if the net is attached to a circuit port or not.

xschem [~/.xschem/xschem_library/sax_rf_examples] xschem list_nets
{out1 opin}
{out0 opin}
{in1 ipin}
{in0 ipin}
{#net1 net}
{#net2 net}
{#net3 net}

xschem instances_to_net <net> returns as a list all instance pins attached to given net: { {instance name} {pin_name} {X-coord} {Y-coord} } ... {..}

xschem [~/.xschem/xschem_library/sax_rf_examples] xschem instances_to_net {#net2}
{ {lft} {out1} {320} {-250} } { {top} {in0} {320} {-250} }