2023-04-10 10:16:55 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "9quLG8jhYqwq"
},
"source": [
2023-04-11 23:22:54 +02:00
"# Fullcustom analog design demo using Magic\n",
"\n",
2023-04-15 18:24:34 +02:00
"The environment path must be retrieved in order to provide _Magic_ with the technology files.\n",
"\n",
"**Please check them. If they are wrong, you can hardcode the correct ones. If you don't know, let it as it is and check for strange behaviours afterwards 😉**"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ghmpRJhL3ac3"
},
"outputs": [],
"source": [
"import os\n",
2023-04-15 18:24:34 +02:00
"from pathlib import Path\n",
"CONDA_DIR = os.environ['CONDA_PREFIX']\n",
"HOME_DIR = Path.home()\n",
"FULLCUSTOM_DIR = os.path.abspath(\"\")\n",
"\n",
"print(f\"Anaconda environment absolute path = '{CONDA_DIR}'\")\n",
"print(f\"Home directory absolute path = '{HOME_DIR}'\")\n",
"print(f\"Fullcustom absolute path = '{FULLCUSTOM_DIR}'\")"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-11 23:22:54 +02:00
"## Source files\n",
2023-04-10 10:16:55 +02:00
"\n",
2023-04-11 23:22:54 +02:00
"### Using the _Magic_ VLSI layout tool\n",
"\n",
"[_Magic_](http://opencircuitdesign.com/magic/) cells (`.mag`) are used to store and manage the layouts. They can be edited using _Magic_ commands or using _Magic_'s GUI.\n",
"\n",
"An existing _inverter_ design as well as its dependencies is imported and converted to _Graphic Design System_ (`.gds`) file for viewing and manufacturing and a _SPICE_ (`.spice`) file for simulation:"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hhpUJrMBmlfj",
2023-04-11 23:22:54 +02:00
"outputId": "bb1109ea-5d45-4a35-e81e-5f0fca3bea9f",
"scrolled": true
2023-04-10 10:16:55 +02:00
},
"outputs": [],
"source": [
2023-04-15 18:24:34 +02:00
"%%script magic -dnull -noconsole -rcfile {CONDA_DIR}/share/pdk/sky130A/libs.tech/magic/sky130A.magicrc\n",
2023-04-10 10:16:55 +02:00
"\n",
2023-04-15 18:24:34 +02:00
"load cad/inv/mag/inv.mag\n",
2023-04-10 10:16:55 +02:00
"\n",
2023-04-15 18:24:34 +02:00
"extract all\n",
2023-04-10 10:16:55 +02:00
"ext2spice lvs\n",
2023-04-15 18:24:34 +02:00
"#ext2spice cthresh 0\n",
"#ext2spice rthresh 0\n",
"ext2spice -o cad/inv/spice/inv.spice\n",
2023-04-10 10:16:55 +02:00
"\n",
"gds labels no\n",
2023-04-15 18:24:34 +02:00
"gds write cad/inv/gds/inv.gds"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-11 23:22:54 +02:00
"### Display the inverter layout\n",
2023-04-10 10:16:55 +02:00
"\n",
2023-04-11 23:22:54 +02:00
"The `.gds` file can be converted to vector graphics for displaying:"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 611
},
"id": "xjz3IYz55vu2",
"outputId": "8b2b0d37-abc2-4446-a9b9-8dd4b8d7bfe7"
},
"outputs": [],
"source": [
"import gdstk\n",
"import IPython.display\n",
"\n",
2023-04-15 18:24:34 +02:00
"library = gdstk.read_gds('cad/inv/gds/inv.gds')\n",
2023-04-10 10:16:55 +02:00
"top_cells = library.top_level()\n",
2023-04-15 18:24:34 +02:00
"top_cells[0].write_svg('cad/inv/svg/inv.svg', scaling=100)\n",
"IPython.display.SVG('cad/inv/svg/inv.svg')"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-11 23:22:54 +02:00
"## Simulation\n",
"\n",
"### Using _SPICE_ simulation engine\n",
"\n",
2023-04-15 18:24:34 +02:00
"[_PySPICE_](https://pyspice.fabrice-salvaire.fr/releases/v1.4/overview.html) is a _Python_ interface for several _SPICE_ simulation engines. The _SPICE_ engines are used to simulate analog circuits. A top-level test bench circuit must be built:"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 927
},
"id": "9scIFl1w7Lk6",
"outputId": "47f12782-4e69-471f-8101-6abfdc041a28"
},
"outputs": [],
"source": [
"from PySpice.Spice.Netlist import Circuit, SubCircuit, SubCircuitFactory\n",
"from PySpice.Unit import *\n",
"\n",
2023-04-11 23:22:54 +02:00
"# Declare the top-level circuit\n",
2023-04-15 18:24:34 +02:00
"circuit = Circuit('INV')\n",
2023-04-11 23:22:54 +02:00
"# Link to technology libraries\n",
2023-04-15 18:24:34 +02:00
"circuit.lib(f'{CONDA_DIR}/share/pdk/sky130A/libs.tech/ngspice/sky130.lib.spice', 'tt')\n",
2023-04-11 23:22:54 +02:00
"\n",
"# Import and instantiate the CUT (Circuit Under Test)\n",
2023-04-15 18:24:34 +02:00
"circuit.include('cad/inv/spice/inv.spice')\n",
"circuit.X('INV', 'inv', 'VDD','VSS','A','X');\n",
"circuit.C('','X','VSS','10f')\n",
2023-04-11 23:22:54 +02:00
"\n",
"# Add voltage sources\n",
2023-04-15 18:24:34 +02:00
"circuit.V('V_SS', 'VSS', 0, 0)\n",
"circuit.V('V_DD', 'VDD', 'VSS', 1.8)\n",
"\n",
"# Add pulse sources\n",
"circuit.PulseVoltageSource('A', 'A', 0,\n",
" initial_value=0@u_V, pulsed_value=1.8@u_V,\n",
" rise_time=10@u_ps, fall_time=10@u_ps,\n",
" pulse_width=0.35@u_ns, period=0.8@u_ns, delay_time=0.2@u_ns)\n",
2023-04-11 23:22:54 +02:00
"\n",
"# Create the simulation and simulate\n",
2023-04-10 10:16:55 +02:00
"simulator = circuit.simulator()\n",
2023-04-15 18:24:34 +02:00
"analysis = simulator.transient(step_time=10@u_ps, end_time=1@u_ns)"
2023-04-10 10:16:55 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-11 23:22:54 +02:00
"### Plot the waveform\n",
"\n",
2023-04-10 10:16:55 +02:00
"The output waveform can be plotted:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
2023-04-11 23:22:54 +02:00
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"fig, ax = plt.subplots(figsize=(8,5))\n",
"ax.grid(visible=True, which='major', axis='both');\n",
"ax.grid(visible=True, which='minor', axis='both', linestyle=\"--\");\n",
"ax.set_yticks(np.linspace(-1.0,2.5,8,endpoint=True));\n",
"ax.set_yticks(np.linspace(-1.0,2.5,36,endpoint=True),minor=True);\n",
2023-04-15 18:24:34 +02:00
"ax.set_xticks(np.linspace(0e-12,1e-9,11,endpoint=True));\n",
"ax.set_xticks(np.linspace(0e-12,1e-9,21,endpoint=True),minor=True);\n",
"ax.set_title('INV Gate - Transient Simulation')\n",
2023-04-11 23:22:54 +02:00
"ax.set_xlabel('Time [s]')\n",
"ax.set_ylabel('Signal [V]')\n",
2023-04-15 18:24:34 +02:00
"ax.plot(analysis.time,analysis.A)\n",
"ax.plot(analysis.time,analysis.X)\n",
"ax.legend(('Input (A)', 'Output (X)'))\n",
2023-04-10 10:16:55 +02:00
"plt.tight_layout()\n",
"plt.show()"
]
},
2023-04-11 23:22:54 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-15 18:24:34 +02:00
"# To have fun going further...\n",
"\n",
2023-04-11 23:22:54 +02:00
"## Graphical editor\n",
"\n",
"_Magic_ can be started specifying only one file (`.magicrc`) that sets everything up (layers, macros, DRC rules, etc.)! Both a console and a GUI are started. Some functions are only accessible using the command-line interface or using keybindings."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2023-04-15 18:24:34 +02:00
"!magic -rcfile {CONDA_DIR}/share/pdk/sky130A/libs.tech/magic/sky130A.magicrc"
2023-04-11 23:22:54 +02:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2023-04-15 18:24:34 +02:00
"## Schematic editor\n",
"\n",
"Of course, the workflow would not be complete without a schematic editor... and an LVS tool. For instance, one can use _Xscheme_:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!export PDK_ROOT={CONDA_DIR}/share/pdk/; \\\n",
"xschem --rcfile {CONDA_DIR}/share/pdk/sky130A/libs.tech/xschem/xschemrc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The inverter schematic is available under:\n",
" \n",
"```\n",
"fullcustom/cad/inv/sch/inv.sch\n",
"```\n",
"\n",
"Open it with _xschem_ and generate the netlist by:\n",
"\n",
" * Selecting _Spice netlist_ in the _Options_ toolbar menu. (This will produce a netlist in a format accepted by the _LVS_ tool.)\n",
" * Selecting _LVS netlist: Top level is a .subckt_ in the _Simulation_ toolbar menu. (This will encapsulate the circuit in a subcircuit. The _LVS_ tool fails otherwose.)\n",
" \n",
"Click on _Netlist_ (top right) to generate the netlist. It is generated by default under the user repository:\n",
"\n",
"```\n",
"~/.xschem/simulations/<circuit>.spice\n",
"```\n",
"\n",
"This location is assumed in the following cells."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Layout Versus Schematic\n",
"\n",
"The layout netlists takes into account the parasitics. They are modeled as lumped resistances and capacitances but they are reported on the schematic. They must be therefore removed from the schematic's netlist. Comment out the two following lines of the corresponding cell above:\n",
"\n",
"```diff\n",
"- ext2spice cthresh 0\n",
"- ext2spice rthresh 0\n",
"+ #ext2spice cthresh 0\n",
"+ #ext2spice rthresh 0\n",
"```\n",
"\n",
"Now that both the schematic's and the layout's netlists are written, they can be compare by _netgen_, the LVS tool:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!netgen lvs \\\n",
"\"{FULLCUSTOM_DIR}/cad/inv/spice/inv.spice inv\" \\\n",
"\"{HOME_DIR}/.xschem/simulations/inv.spice inv\" \\\n",
"\"{CONDA_DIR}/share/pdk/sky130A/libs.tech/netgen/setup.tcl\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some ideas...\n",
2023-04-11 23:22:54 +02:00
"\n",
"Here are a couple ideas to spend a good time exploring those beautiful pieces of software:\n",
"\n",
" * Characterize an NMOS device or a PMOS device by applying voltage pulses on the different pads.\n",
2023-04-15 18:24:34 +02:00
" * Start _Magic_ on another PDK and design an inverter.\n",
" * Vary the devices' geometry and compare the pull-down and pull-up curves.\n",
" * Copy this notebook to explore the NAND gate cell available in the repository.\n",
2023-04-11 23:22:54 +02:00
" \n",
" **Do not hesitate to open the `.mag` files to see how they are built and to adapt from them!**\n",
" \n",
2023-04-15 18:24:34 +02:00
" > Good luck and read the docs. 😉\n",
" \n",
"## More food for the brain\n",
"\n",
" * http://opencircuitdesign.com/\n",
" * https://skywater-pdk.readthedocs.io"
2023-04-11 23:22:54 +02:00
]
},
2023-04-10 10:16:55 +02:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# References\n",
2023-04-11 23:22:54 +02:00
"\n",
2023-04-10 10:16:55 +02:00
"Inspired from:\n",
"“Silicon Notebooks.” CHIPS Alliance, Apr. 08, 2023. Accessed: Apr. 10, 2023. [Online]. Available: https://github.com/chipsalliance/silicon-notebooks/blob/b65134a43b01ae31423f7ee87110740b2257ac42/analog-inverter-magic.ipynb (Apache License 2.0)"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"include_colab_link": true,
"name": "analog-inverter-magic.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}