scc 2025.09
SystemC components library
python4sc.cpp
1/*******************************************************************************
2 * Copyright 2025 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16#include "python4sc.h"
17#include "report.h"
18#include "utilities.h"
19#include <fstream>
20#include <pybind11/embed.h> // Everything needed for embedding#include <sysc/kernel/sc_time.h>
21#include <systemc>
22#ifdef CWR_SYSTEMC
23#include "scml_clock/scml_clock_if.h"
24#endif
25
26namespace scc {
27namespace py = pybind11;
28// C++ class to expose to Python
29class Logger {
30public:
31 Logger(const std::string& name)
32 : name(name) {}
33 void log(std::string const& msg) const { SCCINFO(name) << msg; }
34 std::string get_name() const { return name; }
35
36private:
37 std::string name;
38};
39
40void wait(int value, std::string const& unit) {
41 if(icompare(unit, "fs"))
42 sc_core::wait(value * 1_fs);
43 else if(icompare(unit, "ps"))
44 sc_core::wait(value * 1_ps);
45 else if(icompare(unit, "ns"))
46 sc_core::wait(value * 1_ns);
47 else if(icompare(unit, "us"))
48 sc_core::wait(value * 1_us);
49 else if(icompare(unit, "ms"))
50 sc_core::wait(value * 1_ms);
51 else if(icompare(unit, "s"))
52 sc_core::wait(value * 1_sec);
53 else
54 SCCERR("Python::wait") << "Illegal time unit specification: " << unit;
55}
56
57/* example python script:
58from sysc import Greeter
59import sysc
60
61g = Greeter("Alice")
62g.greet()
63print("Name from C++ class:", g.get_name())
64sysc.wait(10, "ns")
65*/
66void python4sc::run() {
67 wait(sc_core::SC_ZERO_TIME);
68 if(input_file_name.get_value().empty())
69 return;
70 {
71 std::ifstream ifs(input_file_name.get_value());
72 if(!ifs)
73 SCCERR(SCMOD) << "Cannot read " << input_file_name.get_value() << "!";
74 }
75 py::scoped_interpreter guard{};
76 // Create a custom module to expose C++ class
77 py::module_ mod = py::module_::create_extension_module("sysc", nullptr, new py::module_::module_def);
78 // clang-format off
79 // Bind class into the module
80 py::class_<Logger>(mod, "Logger")
81 .def(py::init<std::string>())
82 .def("log", &Logger::log)
83 .def("get_name", &Logger::get_name);
84 // Expose free wait functions
85 mod.def("wait", &::scc::wait);
86 // clang-format on
87 // Register the module so Python can import it
88 py::module_::import("sys").attr("modules")["sysc"] = mod;
89 for(auto& e : mods) {
90 py::module_::import("sys").attr("modules")[e.first.c_str()] = e.second;
91 }
92 try {
93 py::eval_file(input_file_name.get_value());
94 } catch(py::error_already_set& e) {
95 SCCERR(SCMOD) << "Python error: " << e.what();
96 }
97 return;
98}
99} // namespace scc
SCC TLM utilities.
bool icompare(std::string const &a, std::string const &b)
Definition utilities.h:214
log
enum defining the log levels
Definition report.h:86