Basic library and cell name parsing.
diff --git a/scripts/.gitignore b/scripts/.gitignore
new file mode 100644
index 0000000..0a764a4
--- /dev/null
+++ b/scripts/.gitignore
@@ -0,0 +1 @@
+env
diff --git a/scripts/Makefile b/scripts/Makefile
new file mode 100644
index 0000000..58134cb
--- /dev/null
+++ b/scripts/Makefile
@@ -0,0 +1,55 @@
+SHELL := /bin/bash
+
+UNAME_S := $(shell uname -s)
+ifneq (, $(findstring Linux, $(UNAME_S)))
+ OSFLAG := Linux
+endif
+ifeq ($(UNAME_S), Darwin)
+ OSFLAG := MacOSX
+endif
+ifneq (, $(findstring Cygwin, $(UNAME_S)))
+ OSFLAG := Linux
+endif
+ifneq (, $(findstring MINGW, $(UNAME_S)))
+ OSFLAG := Linux
+endif
+
+CONDA_DIR := $(PWD)/env/conda
+CONDA_PYTHON := $(CONDA_DIR)/bin/python
+CONDA_ENV_NAME := skywater-pdk-scripts
+CONDA_ENV_PYTHON := $(CONDA_DIR)/envs/$(CONDA_ENV_NAME)/bin/python
+IN_CONDA_ENV_BASE := source $(CONDA_DIR)/bin/activate &&
+IN_CONDA_ENV := $(IN_CONDA_ENV_BASE) conda activate $(CONDA_ENV_NAME) &&
+
+
+DOWNLOADS_DIR = env/downloads
+
+$(DOWNLOADS_DIR):
+ mkdir -p $(DOWNLOADS_DIR)
+
+$(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh: | $(DOWNLOADS_DIR)
+ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-$(OSFLAG)-x86_64.sh -O $(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh
+ chmod a+x $(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh
+
+$(DOWNLOADS_DIR)/pkgs: $(CONDA_PYTHON)
+ $(IN_CONDA_ENV_BASE) conda config --system --add pkgs_dirs $(DOWNLOADS_DIR)/conda-pkgs
+
+$(CONDA_PYTHON): $(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh
+ $(DOWNLOADS_DIR)/Miniconda3-latest-$(OSFLAG)-x86_64.sh -p $(CONDA_DIR) -b -f
+
+$(CONDA_DIR)/envs: | $(CONDA_PYTHON)
+ $(IN_CONDA_ENV_BASE) conda config --system --add envs_dirs $(CONDA_DIR)/envs
+
+$(CONDA_ENV_PYTHON): environment.yml requirements.txt | $(CONDA_PYTHON) $(CONDA_DIR)/envs $(DOWNLOADS_DIR)/pkgs
+ $(IN_CONDA_ENV_BASE) conda env update --name $(CONDA_ENV_NAME) --file ./environment.yml
+
+env: $(CONDA_ENV_PYTHON)
+ $(IN_CONDA_ENV) conda info
+
+enter: $(CONDA_ENV_PYTHON)
+ $(IN_CONDA_ENV) bash
+
+.PHONY: env
+
+clean:
+ rm -rf env
diff --git a/scripts/environment.yml b/scripts/environment.yml
new file mode 100644
index 0000000..df79dc0
--- /dev/null
+++ b/scripts/environment.yml
@@ -0,0 +1,9 @@
+name: skywater-pdk-scripts
+channels:
+- defaults
+dependencies:
+- python=3.7
+- pip
+# Packages installed from PyPI
+- pip:
+ - -r file:requirements.txt
diff --git a/scripts/python-skywater-pdk/.gitignore b/scripts/python-skywater-pdk/.gitignore
new file mode 100644
index 0000000..a81c8ee
--- /dev/null
+++ b/scripts/python-skywater-pdk/.gitignore
@@ -0,0 +1,138 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+# Usually these files are written by a python script from a template
+# before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+cover/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+.pybuilder/
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+# For a library or package, you might want to ignore these files since the code is
+# intended to run in multiple environments; otherwise, check them in:
+# .python-version
+
+# pipenv
+# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+# However, in case of collaboration, if having platform-specific dependencies or dependencies
+# having no cross-platform support, pipenv may install dependencies that don't work, or not
+# install all needed dependencies.
+#Pipfile.lock
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# pytype static type analyzer
+.pytype/
+
+# Cython debug symbols
+cython_debug/
diff --git a/scripts/python-skywater-pdk/skywater_pdk/base.py b/scripts/python-skywater-pdk/skywater_pdk/base.py
new file mode 100644
index 0000000..c86cde2
--- /dev/null
+++ b/scripts/python-skywater-pdk/skywater_pdk/base.py
@@ -0,0 +1,219 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright 2020 The SkyWater PDK Authors.
+#
+# Use of this source code is governed by the Apache 2.0
+# license that can be found in the LICENSE file or at
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# SPDX-License-Identifier: Apache-2.0
+
+import os
+
+from dataclasses import dataclass
+from enum import Enum
+from typing import Optional
+
+
+def parse_filename(pathname):
+ """Extract library and module name from pathname.
+
+ >>> t = list(parse_filename('sky130_fd_io-top_ground_padonlyv2-tt_1p80V_3p30V_3p30V_25C.wrap.lib'))
+ >>> t.pop(0)
+ Cell(name='top_ground_padonlyv2', library=Library(node=LibraryNode.SKY130, source=LibrarySource('fd'), type=LibraryType.io, name=None))
+ >>> t.pop(0)
+ 'tt_1p80V_3p30V_3p30V_25C'
+ >>> t.pop(0)
+ 'wrap.lib'
+
+ """
+ dirname, filename = os.path.split(pathname)
+
+ if '.' in filename:
+ basename, extension = filename.split('.', 1)
+ else:
+ basename = filename
+ extension = ''
+ basename = basename.replace('-', SEPERATOR)
+
+ library, cell, extra = basename.split(SEPERATOR, 3)
+ return (Cell.parse(library+SEPERATOR+cell), extra, extension)
+
+
+SEPERATOR = "__"
+
+
+class LibraryNode(Enum):
+ SKY130 = "SkyWater 130nm"
+
+ @classmethod
+ def parse(cls, s):
+ s = s.upper()
+ if not hasattr(cls, s):
+ raise ValueError("Unknown node: {}".format(s))
+ return getattr(cls, s)
+
+ def __repr__(self):
+ return "LibraryNode."+self.name
+
+
+class LibrarySource(str):
+ """Where a library was created."""
+ Known = []
+
+ @classmethod
+ def parse(cls, s):
+ try:
+ return cls.Known[cls.Known.index(s)]
+ except ValueError:
+ return cls(s)
+
+ @property
+ def fullname(self):
+ if self in self.Known:
+ return self.__doc__
+ else:
+ return 'Unknown source: '+str.__repr__(self)
+
+ def __repr__(self):
+ return 'LibrarySource({})'.format(str.__repr__(self))
+
+
+Foundary = LibrarySource("fd")
+Foundary.__doc__ = "The SkyWater Foundary"
+LibrarySource.Known.append(Foundary)
+
+Efabless = LibrarySource("ef")
+Efabless.__doc__ = "Efabless"
+LibrarySource.Known.append(Efabless)
+
+OSU = LibrarySource("osu")
+OSU.__doc__ = "Oklahoma State University"
+LibrarySource.Known.append(OSU)
+
+
+class LibraryType(Enum):
+ pr = "Primitives"
+ sc = "Standard Cells"
+ sp = "Build Space (Flash, SRAM, etc)"
+ io = "IO and Periphery"
+ xx = "Miscellaneous"
+
+ @classmethod
+ def parse(cls, s):
+ if not hasattr(cls, s):
+ raise ValueError("Unknown library type: {}".format(s))
+ return getattr(cls, s)
+
+ def __repr__(self):
+ return "LibraryType."+self.name
+
+ def __str__(self):
+ return self.value
+
+
+@dataclass
+class Library:
+ """
+
+ >>> l = Library.parse("sky130_fd_sc_hd")
+ >>> l
+ Library(node=LibraryNode.SKY130, source=LibrarySource('fd'), type=LibraryType.sc, name='hd')
+ >>> l.fullname
+ 'sky130_fd_sc_hd'
+ >>> l.source.fullname
+ 'The SkyWater Foundary'
+ >>> print(l.type)
+ Standard Cells
+
+ >>> l = Library.parse("sky130_rrr_sc_hd")
+ >>> l
+ Library(node=LibraryNode.SKY130, source=LibrarySource('rrr'), type=LibraryType.sc, name='hd')
+ >>> l.fullname
+ 'sky130_rrr_sc_hd'
+ >>> l.source.fullname
+ "Unknown source: 'rrr'"
+
+
+ """
+
+ node: LibraryNode
+ source: LibrarySource
+ type: LibraryType
+ name: Optional[str] = None
+
+ @property
+ def fullname(self):
+ output = []
+ output.append(self.node.name.lower())
+ output.append(self.source.lower())
+ output.append(self.type.name)
+ if self.name:
+ output.append(self.name)
+ return "_".join(output)
+
+ @classmethod
+ def parse(cls, s):
+ if SEPERATOR in s:
+ raise ValueError(
+ "Found separator '__' in library name: {!r}".format(s))
+
+ bits = s.split("_")
+ if len(bits) < 3:
+ raise ValueError(
+ "Did not find enough parts in library name: {}".format(bits))
+
+ kw = {}
+ kw['node'] = LibraryNode.parse(bits.pop(0))
+ kw['source'] = LibrarySource.parse(bits.pop(0))
+ kw['type'] = LibraryType.parse(bits.pop(0))
+ if bits:
+ kw['name'] = bits.pop(0)
+ return cls(**kw)
+
+
+
+@dataclass
+class Cell:
+ """
+ >>> c = Cell.parse("sky130_fd_sc_hd__abc")
+ >>> c
+ Cell(name='abc', library=Library(node=LibraryNode.SKY130, source=LibrarySource('fd'), type=LibraryType.sc, name='hd'))
+ >>> c.fullname
+ 'sky130_fd_sc_hd__abc'
+
+ >>> c = Cell.parse("abc")
+ >>> c
+ Cell(name='abc', library=None)
+ >>> c.fullname
+ Traceback (most recent call last):
+ ...
+ ValueError: Can't get fullname for cell without a library! Cell(name='abc', library=None)
+ """
+
+ name: str
+ library: Optional[Library] = None
+
+ @property
+ def fullname(self):
+ if not self.library:
+ raise ValueError(
+ "Can't get fullname for cell without a library! {}".format(
+ self))
+ return "{}__{}".format(self.library.fullname, self.name)
+
+ @classmethod
+ def parse(cls, s):
+ kw = {}
+ if SEPERATOR in s:
+ library, s = s.split(SEPERATOR, 1)
+ kw['library'] = Library.parse(library)
+ kw['name'] = s
+ return cls(**kw)
+
+
+
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod()
diff --git a/scripts/python-skywater-pdk/skywater_pdk/corners.py b/scripts/python-skywater-pdk/skywater_pdk/corners.py
index 7747699..46c78de 100644
--- a/scripts/python-skywater-pdk/skywater_pdk/corners.py
+++ b/scripts/python-skywater-pdk/skywater_pdk/corners.py
@@ -9,22 +9,121 @@
#
# SPDX-License-Identifier: Apache-2.0
-Corner = namedtuple("Corner", "mod volts temps extra")
-class CornerFlag(enum.Flag):
+import re
+import os
+
+from enum import Flag
+from dataclasses import dataclass
+from typing import List, Optional
+
+from .base import parse_filename as base_parse_filename
+
+
+CornerTypeMappings = {}
+# "fs" is sometimes called "wp" meaning worst-case power,
+CornerTypeMappings["wp"] = "fs"
+# "sf" is sometimes called "ws" meaning worst-case speed
+CornerTypeMappings["ws"] = "sf"
+
+
+class CornerType(Flag):
+ """
+ >>> CornerType.parse('t')
+ CornerType.t
+ >>> CornerType.parse('tt')
+ [CornerType.t, CornerType.t]
+ >>> CornerType.parse('wp')
+ [CornerType.f, CornerType.s]
+ """
t = 'Typical' # all nominal (typical) values
f = 'Fast' # fast, that is, values that make transistors run faster
s = 'Slow' # slow
- # "fs" is sometimes called "wp" meaning worst-case power,
- # "sf" is sometimes called "ws" meaning worst-case speed
+ @classmethod
+ def parse(cls, s):
+ if s in CornerTypeMappings:
+ return cls.parse(CornerTypeMappings[s])
+ if len(s) > 1:
+ o = []
+ for c in s:
+ o.append(cls.parse(c))
+ return o
+ return getattr(cls, s)
+ def __repr__(self):
+ return 'CornerType.'+self.name
+
+ def __str__(self):
+ return self.value
+
+
+
+class CornerFlag(Flag):
nointpr = 'No internal power'
lv = 'Low voltage'
ccsnoise = 'Composite Current Source Noise'
+ def __repr__(self):
+ return 'CornerType.'+self.name
+
+ def __str__(self):
+ return self.value
+
+@dataclass
+class Corner:
+ volts: List[float]
+ temps: List[int]
+ flags: List[CornerFlag]
+ types: Optional[List[CornerType]] = None
+
+
+VOLTS_REGEX = re.compile('([0-9]p[0-9]+)V')
+TEMP_REGEX = re.compile('(n?)([0-9][0-9]+)C')
+def parse_filename(pathname):
+ """Extract corner information from a filename.
+
+ >>> parse_filename('sky130_fd_io-top_ground_padonlyv2-tt_1p80V_3p30V_3p30V_25C.wrap.lib')
+ Corner(volts=[1.8, 3.3, 3.3], temps=[25], flags=[], types=[CornerType.t, CornerType.t])
+
+ """
+ cell, extra, extension = base_parse_filename(pathname)
+
+ if extension not in ('', 'lib', 'wrap.lib'):
+ raise ValueError('Not possible to extract corners from: {!r}'.format(extension))
+
+ if not extra:
+ raise ValueError('No corners found in: {!r}'.format(pathname))
+
+ kw = {}
+ kw['flags'] = []
+ kw['volts'] = []
+ kw['temps'] = []
+
+ bits = extra.split("_")
+ while len(bits) > 0:
+ b = bits.pop(0)
+
+ if VOLTS_REGEX.match(b):
+ assert b.endswith('V'), b
+ kw['volts'].append(float(b[:-1].replace('p', '.')))
+ elif TEMP_REGEX.match(b):
+ assert b.endswith('C'), b
+ kw['temps'].append(int(b[:-1].replace('n', '-')))
+ elif hasattr(CornerFlag, b):
+ kw['flags'].append(getattr(CornerFlag, b))
+ else:
+ assert 'types' not in kw, (kw, b)
+ kw['types'] = CornerType.parse(b)
+
+ return Corner(**kw)
+
+
+
# 1p60V 5p50V n40C
-# VOLTS_RE = re.compile('([0-9]p[0-9]+)V')
-# TEMP_RE = re.compile('(n?)([0-9][0-9]+)C')
+
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod()
diff --git a/scripts/requirements.txt b/scripts/requirements.txt
new file mode 100644
index 0000000..8ff2475
--- /dev/null
+++ b/scripts/requirements.txt
@@ -0,0 +1 @@
+-e python-skywater-pdk