-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from harshkhandeparkar/verilog-mako
[GSoC] Common Verilog Generation API and Implementation in `temp-sense-gen`
- Loading branch information
Showing
22 changed files
with
569 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
Common Python API | ||
================= | ||
|
||
.. contents:: Contents | ||
:local: | ||
|
||
Introduction | ||
------------ | ||
|
||
OpenFASoC generators have a Python script that runs the generator flow (See `Generators <generators.html>`_ for more information). Many of the steps in the Python script are common across generators. | ||
|
||
A common Python module is exported in ``openfasoc/generators/common/``to reduce code duplication and simplify the creation of new generators. This module exports various functions and constants which may be used in generators. | ||
Importing | ||
--------- | ||
Since the common Python module is in the ``openfasoc/generators/common/`` directory, which is a parent directory, the path to this directory must be added using ``sys.path.append()``. | ||
|
||
.. code-block:: python | ||
import sys | ||
sys.path.append( | ||
# This is the relative path to the `openfasoc/generators/common/` directory. | ||
os.path.join(os.path.dirname(__file__), '..', '..') | ||
) | ||
Once the directory is added to the path, the exported modules can be imported using the standard Python import syntax. | ||
|
||
.. code-block:: python | ||
from common.verilog_generation import generate_verilog | ||
See `Exported Modules`_ for a list of exported modules and their respective functions and constants. | ||
|
||
Exported Modules | ||
---------------- | ||
The top level module ``common`` exports the following submodules. | ||
|
||
1. Verilog Generation (``common.verilog_generation``) | ||
##################################################### | ||
This module exports functions and constants used in the Verilog generation step. See `Temperature Sensor Generator <flow-tempsense.html#verilog-generation>`_ for more about Verilog generation. | ||
|
||
This module uses the `Mako <https://www.makotemplates.org/>`_ templating library to convert source Verilog templates into final Verilog files used in the OpenROAD flow. | ||
|
||
Functions | ||
^^^^^^^^^ | ||
1. ``generate_verilog(parameters: dict, src_dir: str, out_dir: str) -> None`` | ||
|
||
Reads source Verilog files from ``src_dir`` and generates output Verilog files for synthesis in the OpenROAD flow. The source files are `Mako <https://makotemplates.org>`_ templates. | ||
|
||
The ``parameters`` argument is a dictionary of all the parameters used in the source Verilog Mako templates. Use the ``${parameter}}`` syntax in the source files to insert parameters. For example, the number of inverters in the `Temperature Sensor Generator <flow-tempsense.html>`_ is a parameter. | ||
|
||
This function maintains the source directory (``src_dir``) structure in the output directory (``out_dir``). i.e., source files from a subdirectory of the ``src_dir`` will be generated in a subdirectory in ``out_dir`` with the same name. | ||
|
||
Arguments: | ||
- ``src_dir`` (str): Path to the directory with the source Verilog templates. (default: ``src/``) | ||
- ``out_dir`` (str): Path to the directory in which the output will be generated. (default: ``flow/design/src/``) | ||
- ``parameters`` (dict): Dictionary of all the parameters used in the `Mako templates <https://makotemplates.org>`_. | ||
|
||
Example: | ||
.. code-block:: python | ||
generate_verilog( | ||
# Generates the output in flow/design/src/ | ||
out_dir=os.path.join('flow', 'design', 'src', 'tempsense'), | ||
# Sets the parameters used in the design | ||
parameters={ | ||
"ninv": 6, | ||
"nhead": 3, | ||
"design_name": "tempsenseInst_error", | ||
} | ||
) | ||
See the generators' Python files in ``tools/`` for more examples. | ||
|
||
This function also appends (can be directly used in the source Verilog files) the following Mako `defs <https://docs.makotemplates.org/en/latest/defs.html>`_: | ||
- ``cell(name)`` | ||
|
||
This def returns the name of a standard cell for a given platform. Currently, it only supports the sky130 platform. The naming scheme for sky130 is ``${cell_prefix}${name}${cell_suffix}``. | ||
|
||
Here ``name`` is an argument passed to the ``cell()`` def, and ``cell_prefix`` and ``cell_suffix`` are set in the ``parameters`` argument passed to the ``generate_verilog()`` function. | ||
|
||
For example, an inverter cell can be inserted using the syntax ``${cell('inv')}``. If the prefix is ``sky130_fd_sc_hd__`` (sky130hd) and the suffix is ``_1``, the cell will be replaced with ``sky130_fd_sc_hd__inv_1``. The same statement will be replaced with ``sky130_fd_sc_hs__inv_1`` for the sky130hs platform. | ||
|
||
Use the constant ``COMMON_PLATFORMS_PREFIX_MAP`` for mapping a sky130 platform to its platform. | ||
|
||
Constants | ||
^^^^^^^^^ | ||
1. ``COMMON_PLATFORMS_PREFIX_MAP`` | ||
|
||
This is a dictionary of common platforms (currently sky130) and their cell naming prefixes. See the ``cell()`` def in the ``generate_verilog()`` function for more information on how to use it. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""A common module used in OpenFASOC generators. | ||
This module exports functions used in OpenFASOC generators. The following submodules and functions are exported. | ||
- `common.verilog_generation` | ||
1. `generate_verilog(parameters: dict, src_dir: str, out_dir: str) -> None`: Used to generate synthesizable Verilog files (for OpenROAD flow) from source Mako-based Verilog templates. | ||
2. `COMMON_PLATFORMS_PREFIX_MAP` (dict): This is a dictionary of common platforms (currently sky130) and their cell naming prefixes. | ||
See individual function documentation for more information on a particular function. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from mako.template import Template | ||
from os import path, makedirs, listdir | ||
|
||
# TODO: Find a better way to import common used defs in the future. | ||
__COMMON_MAKO_DEFS = ''' | ||
<%def name="cell(name)">${cell_prefix}${name}${cell_suffix}</%def> | ||
''' | ||
|
||
def _mako_defs_preprocessor(input: str) -> str: | ||
"""A Mako preprocessor that appens commonly used defs to the template. | ||
Mako templates have a preprocessor argument. See https://docs.makotemplates.org/en/latest/usage.html#mako.template.Template.params.preprocessor. | ||
This preprocessor adds defs commonly used in Verilog files to the template. | ||
TODO: Find a better way to import common used defs in the future. | ||
""" | ||
return __COMMON_MAKO_DEFS + input | ||
|
||
def _generate_file(input_path: str, output_path: str, parameters: dict) -> None: | ||
"""Generates a single output Verilog file from its Mako template. | ||
Arguments: | ||
- `input_path` (str): Path to the input file (Mako template) with the extension. | ||
- `output_path` (str): Path to the output file location with the extension. | ||
- `parameters` (dict): Dictionary of all the parameters used in the Mako template. | ||
""" | ||
|
||
# TODO: Find a better way to import common used defs in the future. | ||
template = Template(filename=input_path, preprocessor=_mako_defs_preprocessor) | ||
|
||
out_file = open(output_path, "w") | ||
out_file.write(template.render(**parameters)) | ||
|
||
def _generate_subdirectory(src_dir: str, out_dir: str, parameters: dict) -> None: | ||
"""Generates the output Verilog files of a single subdirectory of Mako templates. | ||
Reads Mako templates from a subdirectory (`src_dir`), generates the output files in the output directory (`out_dir`), and maintains the directory structure. i.e., templates from a subdirectory of the `src_dir` will be generated in a subdirectory in `out_dir` with the same name. | ||
This function recursively calls itself for subdirectories. | ||
Arguments: | ||
- `src_dir` (str): Path to the source directory with Mako templates. | ||
- `out_dir` (str): Path to the output directory. | ||
- `parameters` (dict): Dictionary of all the parameters used in the Mako template. | ||
""" | ||
# generate the output directory if it doesn't exist | ||
if not path.exists(out_dir): | ||
makedirs(out_dir) | ||
|
||
for filename in listdir(src_dir): | ||
input_filepath = path.join(src_dir, filename) | ||
output_filepath = path.join(out_dir, filename) | ||
|
||
if path.isdir(input_filepath): | ||
# if the path is a subdirectory, recursively call the function | ||
_generate_subdirectory( | ||
input_filepath, | ||
output_filepath, | ||
parameters | ||
) | ||
else: | ||
# if the path is a fine, generate the output | ||
_generate_file( | ||
input_filepath, | ||
output_filepath, | ||
parameters | ||
) | ||
|
||
def generate_verilog( | ||
parameters: dict, | ||
src_dir: str = "src", | ||
out_dir: str = path.join("flow", "design", "src") | ||
) -> None: | ||
"""Generates output Verilog files from source Mako templates. | ||
Reads source Verilog files from `src_dir` and generates output Verilog files for synthesis in the OpenROAD flow. | ||
The source files are Mako templates. See https://makotemplates.org for syntax and documentation. | ||
This function maintains the source directory (`src_dir`) structure in the output directory (`out_dir`). i.e., source files from a subdirectory of the `src_dir` will be generated in a subdirectory in `out_dir` with the same name. | ||
Arguments: | ||
- `parameters` (dict): Dictionary of all the parameters used in the Mako templates. See https://makotemplates.org for documentation. | ||
- `src_dir` (str): Path to the directory with the source Verilog templates. | ||
- `out_dir` (str): Path to the directory in which the output will be generated. | ||
""" | ||
_generate_subdirectory(src_dir, out_dir, parameters) | ||
|
||
# A dictionary of commonly used platforms and the prefix used in their cell naming | ||
# Currently includes only sky130 platform prefixes | ||
COMMON_PLATFORMS_PREFIX_MAP = { | ||
"sky130hd": "sky130_fd_sc_hd__", | ||
"sky130hs": "sky130_fd_sc_hs__", | ||
"sky130hvl": "sky130_fd_sc_hvl__", | ||
"sky130osu12Ths": "sky130_osu_sc_12T_hs__", | ||
"sky130osu12Tms": "sky130_osu_sc_12T_ms__", | ||
"sky130osu12Tls": "sky130_osu_sc_12T_ls__", | ||
"sky130osu15Ths": "sky130_osu_sc_15T_hs__", | ||
"sky130osu15Tms": "sky130_osu_sc_15T_ms__", | ||
"sky130osu15Tls": "sky130_osu_sc_15T_ls__", | ||
"sky130osu18Ths": "sky130_osu_sc_18T_hs__", | ||
"sky130osu18Tms": "sky130_osu_sc_18T_ms__", | ||
"sky130osu18Tls": "sky130_osu_sc_18T_ls__" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tempsense/ |
6 changes: 0 additions & 6 deletions
6
openfasoc/generators/temp-sense-gen/flow/design/src/tempsense/.gitignore
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module tempsenseInst_error | ||
module ${design_name} | ||
( | ||
input CLK_REF, | ||
input RESET_COUNTERn, | ||
|
Oops, something went wrong.