diff --git a/.pydevproject b/.pydevproject index 240bc60..b5a435a 100644 --- a/.pydevproject +++ b/.pydevproject @@ -1,16 +1,30 @@ + + + + pysysc-env + - Default python interpreter + + 3.6 + + + + /${PROJECT_DIR_NAME} - - + + + + + + diff --git a/MANIFEST.in b/MANIFEST.in index a3f93b8..937d05d 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -2,4 +2,3 @@ recursive-include projectname *.py include README.md include LICENSE include requirements.txt - diff --git a/README.md b/README.md index 8bbaa55..f8937de 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,26 @@ # PySysC -A Python package to use SystemC from Python +A Python 3 package to use SystemC from Python ## How to setup the environment +The installation for PySysC is as follows (using bash shell): ``` -pyenv install 3.6.3 -pyenv virtualenv 3.6.3 pysysc -pyenv activate pysysc -STDCXX=11 MAKE_NPROCS=32 pip install --verbose cppyy --no-binary=cppyy-cling -pip install conan +# create virtual environment +python3 -m venv pysysc-env +# and enable it +. pysysc-env/bin/activate +# install needed packages +python3 -m pip install wheel +# install cppyy, C++ std version needs to match the version used to build the SystemC library +STDCXX=11 python3 -m pip install cppyy +# clone of PySysC +git clone https://git.minres.com/SystemC/PySysC.git +# install PySysC, for development PySysC use 'python3 -m pip install -e` +python3 -m pip install -e PySysC ``` ## TODO -* pythonize `sc_module` with iteration protocol (`__next__` and `StopIteration` exception) \ No newline at end of file +* pythonize `sc_module` with iteration protocol (`__next__` and `StopIteration` exception) diff --git a/pysysc/sccppyy.py b/pysysc/sccppyy.py index 4c401b7..ea0d79e 100644 --- a/pysysc/sccppyy.py +++ b/pysysc/sccppyy.py @@ -2,7 +2,9 @@ import json import cppyy import os.path from pathlib import Path +from sysconfig import get_paths import sys +import re import tempfile import conans.client.conan_api as conan from contextlib import (redirect_stdout, redirect_stderr) @@ -122,12 +124,13 @@ def _load_systemc_cci(): return False def _load_pythonization_lib(): - import pysysc - path = os.path.dirname(os.path.dirname(pysysc.__file__)) - for file in os.listdir(path): - if file.endswith(".so"): - cppyy.load_library(os.path.join(path, file)) - cppyy.include(os.path.join(path, "PyScModule.h")) + info = get_paths() + for file in os.listdir(info['platlib']): + if re.match(r'pysyscsc.*\.so', file): + cppyy.load_library(os.path.join(info['platlib'], file)) + full_path = os.path.join(info['data'], 'include/site/python%d.%d/PySysC/PyScModule.h' % sys.version_info[:2]) + if os.path.isfile(full_path): + cppyy.include(full_path) return diff --git a/setup.py b/setup.py index ec3c5df..54bbcb8 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,8 @@ pysyscsc = Extension('pysyscsc', include_dirs = [sysc_home+'/include'], libraries = ['systemc'], library_dirs = [sysc_home+'/lib'], - sources = ['PyScModule.cpp']) + sources = ['PyScModule.cpp'], + depends = ['PyScModule.h']) setup(name='PySysC', @@ -33,7 +34,11 @@ setup(name='PySysC', author_email='info@minres.com', license='Apache-2.0', packages=['pysysc'], - data_files=[('.',['PyScModule.h'])], + package_data={ + 'pysyscsc': ['PyScModule.h'], + }, + #data_files=[('include',['PyScModule.h'])], + headers=['PyScModule.h'], include_package_data=True, install_requires=[ 'cppyy', diff --git a/tests/modules.py b/tests/modules.py new file mode 100644 index 0000000..25c2dd5 --- /dev/null +++ b/tests/modules.py @@ -0,0 +1,79 @@ +import os.path +import logging +import cppyy +from cppyy import gbl as cpp +import pysysc +from pysysc.structural import Connection, Module, Signal, Simulation + +num_of_mem = 100 + +class TopModule(cpp.scc.PyScModule): + + def __init__(self, name): + super().__init__(self, name) + ############################################################################### + # instantiate + ############################################################################### + self.clk_gen = Module(cpp.ClkGen).create("clk_gen") + self.rst_gen = Module(cpp.ResetGen).create("rst_gen") + self.initiator = Module(cpp.Initiator).create("initiator") + self.memories = [Module(cpp.Memory).create("mem%d"%idx) for idx in range(0,num_of_mem)] + self.router = Module(cpp.Router[num_of_mem]).create("router") + ############################################################################### + # connect them + ############################################################################### + self.clk = Signal("clk").src(self.clk_gen.clk_o).sink(self.initiator.clk_i).sink(self.router.clk_i) + [self.clk.sink(m.clk_i) for m in self.memories] + self.rst = Signal("rst").src(self.rst_gen.reset_o).sink(self.initiator.reset_i).sink(self.router.reset_i) + [self.rst.sink(m.reset_i) for m in self.memories] + Connection().src(self.initiator.socket).sink(self.router.target_socket) + [Connection().src(self.router.initiator_socket.at(idx)).sink(m.socket) for idx,m in enumerate(self.memories)] + super().method("TickMethod", [self.clk.signal.pos_edge()]) + + + def EndOfElaboration(self): + print("Elaboration finished") + + def StartOfSimulation(self): + print("Simulation started") + + def EndOfSimulation(self): + print("Simulation finished") + + def TickMethod(self): + print("Simulation tick") + +############################################################################### +# setup and load +############################################################################### +logging.basicConfig(level=logging.INFO) +build_type='Debug' +############################################################################### +#myDir = os.path.dirname( os.path.realpath(__file__)) +myDir = os.path.dirname( os.path.realpath(__file__)+'/../../PySysC-SC') +pysysc.read_config_from_conan(os.path.join(myDir, 'conanfile.txt'), build_type) +pysysc.load_systemc() +############################################################################### +logging.debug("Loading SC-Components lib") +pysysc.add_include_path(os.path.join(myDir, 'sc-components/incl')) +pysysc.add_library('scc.h', os.path.join(myDir, 'build/%s/lib/libsc-components.so'%build_type)) +############################################################################### +logging.debug("Loading Components lib") +pysysc.add_include_path(os.path.join(myDir, 'components')) +pysysc.add_library('components.h', os.path.join(myDir, 'build/%s/lib/libcomponents.so'%build_type)) +############################################################################### +# configure +############################################################################### +Simulation.setup(logging.root.level) +############################################################################### +# instantiate +############################################################################### +#from modules import TopModule +dut = Module(TopModule).create("dut") +############################################################################### +# run if it is standalone +############################################################################### +if __name__ == "__main__": + Simulation.configure(enable_vcd=False) + Simulation.run() + logging.debug("Done") diff --git a/tests/router_example.py b/tests/router_example.py index 24ed012..5324c68 100644 --- a/tests/router_example.py +++ b/tests/router_example.py @@ -7,6 +7,8 @@ from cppyy import gbl as cpp proj_home='../../PySysC-SC' conan_res = scpy.read_config_from_conan(os.path.join(proj_home, 'conanfile.txt')) scpy.load_systemc() +scpy.add_include_path(os.path.join(proj_home, 'sc-components/incl')) +scpy.add_library('scc.h', os.path.join(proj_home, 'build/Debug/lib/libscc.so')) scpy.add_include_path(os.path.join(proj_home, 'components')) scpy.add_library('components.h', os.path.join(proj_home, 'build/Debug/lib/libcomponents.so')) diff --git a/tests/test_pysysc.py b/tests/test_pysysc.py index 9550411..cc42acd 100644 --- a/tests/test_pysysc.py +++ b/tests/test_pysysc.py @@ -12,22 +12,50 @@ from cppyy import gbl as cpp class Test(unittest.TestCase): - initialized=False + + not_initialized=True def setUp(self): - proj_home='../PySysC-SC' - conan_res = scpy.read_config_from_conan(os.path.join(proj_home, 'conanfile.txt')) - scpy.load_systemc() - scpy.add_include_path(os.path.join(proj_home, 'components')) - scpy.add_library('components.h', os.path.join(proj_home, 'build/Debug/lib/libcomponents.so')) - - self.initiator = cpp.Initiator(cpp.sc_core.sc_module_name("initiator")) - self.memories = [cpp.Memory(cpp.sc_core.sc_module_name(name)) for name in ["mem0", "mem1", "mem2", "mem3"]] - self.router = cpp.Router[4](cpp.sc_core.sc_module_name("router")) - self.initiator.socket.bind(self.router.target_socket) - for idx,m in enumerate(self.memories): - self.router.initiator_socket.at(idx).bind(m.socket) - print("Initialized") + if Test.not_initialized: + proj_home='../../PySysC-SC' + conan_path=os.path.join(proj_home, 'conanfile.txt') + conan_res = scpy.read_config_from_conan(conan_path) + scpy.load_systemc() + scpy.add_include_path(os.path.join(proj_home, 'sc-components/incl')) + scpy.add_library('scc.h', os.path.join(proj_home, 'build/Debug/lib/libscc.so')) + scpy.add_include_path(os.path.join(proj_home, 'components')) + scpy.add_library('components.h', os.path.join(proj_home, 'build/Debug/lib/libcomponents.so')) + + + ############################################################################### + # instantiate + ############################################################################### + Test.clkgen = cpp.ClkGen(cpp.sc_core.sc_module_name("clk_gen")) + Test.rstgen = cpp.ResetGen(cpp.sc_core.sc_module_name("rst_gen")) + Test.initiator = cpp.Initiator(cpp.sc_core.sc_module_name("initiator")) + Test.memories = [cpp.Memory(cpp.sc_core.sc_module_name(name)) for name in ["mem0", "mem1", "mem2", "mem3"]] + Test.router = cpp.Router[4](cpp.sc_core.sc_module_name("router")) + ############################################################################### + # signals + ############################################################################### + Test.sig_clk = cpp.sc_core.sc_signal[cpp.sc_core.sc_time]("clk") + Test.sig_rst = cpp.sc_core.sc_signal[cpp.sc_dt.sc_logic]("rst") + ############################################################################### + # connect it + ############################################################################### + Test.clkgen.clk_o(Test.sig_clk) + Test.rstgen.reset_o(Test.sig_rst) + Test.initiator.socket.bind(Test.router.target_socket) + Test.initiator.clk_i(Test.sig_clk) + Test.initiator.reset_i(Test.sig_rst) + Test.router.clk_i(Test.sig_clk) + Test.router.reset_i(Test.sig_rst) + for idx,m in enumerate(Test.memories): + Test.router.initiator_socket.at(idx).bind(m.socket) + m.clk_i(Test.sig_clk) + m.reset_i(Test.sig_rst) + + Test.not_initialized=False def tearDown(self): #cpp.sc_core.sc_stop() @@ -35,26 +63,28 @@ class Test(unittest.TestCase): def testSCTimeRepr(self): - self.assertEqual(str(cpp.sc_core.sc_time_stamp()), '0 s') + cur_time=cpp.sc_core.sc_time_stamp() + cur_time_str=cur_time.to_string() + self.assertEqual(cur_time_str, '0 s') def testMembers(self): - members = scpy.get_members(initiator) + members = scpy.get_members(Test.initiator) print("members") - methods = scpy.get_methods(initiator) + methods = scpy.get_methods(Test.initiator) print("methods") - - ports = scpy.get_ports(initiator) + ports = scpy.get_ports(Test.initiator) print("ports") - intors = scpy.get_inititator_sockets(initiator) + intors = scpy.get_inititator_sockets(Test.initiator) print("intors") - tgts = scpy.get_target_sockets(initiator) + tgts = scpy.get_target_sockets(Test.initiator) print("tgts") - childs = scpy.get_submodules(initiator) + childs = scpy.get_submodules(Test.initiator) print("childs") self.assertTrue(len(intors)==1, "Wrong numbers of initiator sockets") def testConnection(self): - self.assertFalse(isinstance(self.intors[0][0], cpp.sc_core.sc_object), " intor[0] connects to sc_object", ) + intors = scpy.get_inititator_sockets(Test.initiator) + self.assertFalse(isinstance(intors[0][0], cpp.sc_core.sc_object), " intor[0] connects to sc_object", ) # def testSim(self): # cpp.sc_core.sc_in_action=True