From 993aecd4c11b250f2d1f4ce73b5f7c6e65eaa82e Mon Sep 17 00:00:00 2001
From: Yaman Umuroglu <yamanu@xilinx.com>
Date: Thu, 24 Mar 2022 23:31:19 +0100
Subject: [PATCH] [Stitch] add proposed mdd fix for xparameters.h from Thomas

---
 src/finn/qnn-data/mdd-data/finn_design.mdd    | 36 ++++++++++++
 src/finn/qnn-data/mdd-data/finn_design.tcl    | 58 +++++++++++++++++++
 .../fpgadataflow/create_stitched_ip.py        | 18 ++++++
 3 files changed, 112 insertions(+)
 create mode 100644 src/finn/qnn-data/mdd-data/finn_design.mdd
 create mode 100644 src/finn/qnn-data/mdd-data/finn_design.tcl

diff --git a/src/finn/qnn-data/mdd-data/finn_design.mdd b/src/finn/qnn-data/mdd-data/finn_design.mdd
new file mode 100644
index 000000000..517180fa9
--- /dev/null
+++ b/src/finn/qnn-data/mdd-data/finn_design.mdd
@@ -0,0 +1,36 @@
+# Copyright (c) 2022  Advanced Micro Devices, Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+#   list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+#   this list of conditions and the following disclaimer in the documentation
+#   and/or other materials provided with the distribution.
+#
+# * Neither the name of  Advanced Micro Devices nor the names of its
+#   contributors may be used to endorse or promote products derived from
+#   this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+OPTION psf_version = 2.1;
+
+BEGIN driver finn_design
+        OPTION supported_peripherals = (finn_design);
+        OPTION driver_state = ACTIVE;
+        OPTION VERSION = 1.0;
+        OPTION NAME = finn_design;
+END driver
diff --git a/src/finn/qnn-data/mdd-data/finn_design.tcl b/src/finn/qnn-data/mdd-data/finn_design.tcl
new file mode 100644
index 000000000..b8c55e12b
--- /dev/null
+++ b/src/finn/qnn-data/mdd-data/finn_design.tcl
@@ -0,0 +1,58 @@
+# Copyright (c) 2022  Advanced Micro Devices, Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice, this
+#   list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+#   this list of conditions and the following disclaimer in the documentation
+#   and/or other materials provided with the distribution.
+#
+# * Neither the name of  Advanced Micro Devices nor the names of its
+#   contributors may be used to endorse or promote products derived from
+#   this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+# @brief        Address range defines for FINN IP.
+# @author       Thomas B. Preußer <thomas.preusser@amd.com>
+##
+
+proc generate {drv_handle} {
+        # Bounds of all exposed slave address ranges to xparameters.h
+        set file_handle [hsi::utils::open_include_file "xparameters.h"]
+        generate_memrange_parameters $drv_handle $file_handle
+        close $file_handle
+}
+
+proc generate_memrange_parameters {drv_handle file_handle} {
+        # Collect unique slave interfaces to custom module
+        array unset ranges
+        foreach mem_range [hsi::get_mem_ranges -of_object [hsi::get_cells -hier [hsi::get_sw_processor]] $drv_handle] {
+                set ranges([common::get_property SLAVE_INTERFACE $mem_range]) [list \
+                        [common::get_property BASE_NAME  $mem_range] \
+                        [common::get_property BASE_VALUE $mem_range] \
+                        [common::get_property HIGH_NAME  $mem_range] \
+                        [common::get_property HIGH_VALUE $mem_range] \
+                ]
+        }
+
+        # Produce defines for the address range bounds
+        set prefix "XPAR_[string toupper $drv_handle]"
+        foreach {key val} [array get ranges] {
+                puts $file_handle "#define [format "%s_%s_%s" $prefix $key [lindex $val 0]] [lindex $val 1]"
+                puts $file_handle "#define [format "%s_%s_%s" $prefix $key [lindex $val 2]] [lindex $val 3]"
+        }
+        puts $file_handle ""
+}
diff --git a/src/finn/transformation/fpgadataflow/create_stitched_ip.py b/src/finn/transformation/fpgadataflow/create_stitched_ip.py
index 8ef780e46..59e424547 100644
--- a/src/finn/transformation/fpgadataflow/create_stitched_ip.py
+++ b/src/finn/transformation/fpgadataflow/create_stitched_ip.py
@@ -26,11 +26,14 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+import pkg_resources as pk
+
 import json
 import multiprocessing as mp
 import os
 import subprocess
 import warnings
+from shutil import copytree
 
 from finn.custom_op.registry import getCustomOp
 from finn.transformation.base import Transformation
@@ -442,6 +445,21 @@ class CreateStitchedIP(Transformation):
                 "ipx::add_file dcp/%s.dcp "
                 "[ipx::get_file_groups xilinx_simulationcheckpoint]" % block_name
             )
+        # add a rudimentary driver mdd to get correct ranges in xparameters.h later on
+        example_data_dir = pk.resource_filename("finn.qnn-data", "mdd-data/")
+        copytree(example_data_dir, vivado_stitch_proj_dir + "/mdd-data")
+        tcl.append("file copy -force mdd-data ip/")
+        tcl.append("ipx::add_file_group -type software_driver {} [ipx::current_core]")
+        tcl.append(
+            "set_property type mdd [ipx::add_file mdd-data/finn_design.mdd"
+            "[ipx::get_file_groups xilinx_softwaredriver -of_objects "
+            "[ipx::current_core]]]"
+        )
+        tcl.append(
+            "set_property type tclSource [ipx::add_file mdd-data/finn_design.tcl "
+            "[ipx::get_file_groups xilinx_softwaredriver -of_objects "
+            "[ipx::current_core]]]"
+        )
         tcl.append("ipx::update_checksums [ipx::find_open_core %s]" % block_vlnv)
         tcl.append("ipx::save_core [ipx::find_open_core %s]" % block_vlnv)
         # export list of used Verilog files (for rtlsim later on)
-- 
GitLab