diff options
author | Johannes Stoelp <johannes.stoelp@gmail.com> | 2023-07-26 00:05:14 +0200 |
---|---|---|
committer | Johannes Stoelp <johannes.stoelp@gmail.com> | 2023-07-26 00:05:14 +0200 |
commit | 173622e9e82b7c2b1c6f8146056661b2a6577735 (patch) | |
tree | 4d14127163d9621e7462a8fc25444552b5a80137 /sc_export.cc | |
download | sysc-playground-173622e9e82b7c2b1c6f8146056661b2a6577735.tar.gz sysc-playground-173622e9e82b7c2b1c6f8146056661b2a6577735.zip |
add some initial playground examples + scripts to build
Diffstat (limited to 'sc_export.cc')
-rw-r--r-- | sc_export.cc | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/sc_export.cc b/sc_export.cc new file mode 100644 index 0000000..7acca58 --- /dev/null +++ b/sc_export.cc @@ -0,0 +1,76 @@ +#include <systemc> +#include "utils.h" + +using sc_core::sc_export; +using sc_core::sc_in; +using sc_core::sc_module; +using sc_core::sc_module_name; +using sc_core::SC_NS; +using sc_core::sc_signal; +using sc_core::sc_signal_inout_if; + +// -- CLOCK_GEN ---------------------------------------------------------------- + +struct clock_gen : public sc_module { + SC_HAS_PROCESS(clock_gen); + + explicit clock_gen(sc_module_name nm, unsigned period_ns = 20) + : sc_module(nm), m_half_period(period_ns / 2) { + assert(period_ns == m_half_period * 2); + + // Bind sc_signal (clk) against export. + p_clk(m_clk); + + SC_METHOD(tick); + } + + // Export to expose sc_signal (clk) outside this module. + sc_export<sc_signal_inout_if<bool>> p_clk; + + private: + // Implementation of sc_signal_inout_if. + sc_signal<bool> m_clk; + + const unsigned m_half_period; + + void tick() { + m_clk.write(!m_clk.read()); + next_trigger(m_half_period, SC_NS); + } +}; + +// -- TICKTOCK ----------------------------------------------------------------- + +struct ticktock : public sc_module { + SC_HAS_PROCESS(ticktock); + + explicit ticktock(sc_module_name nm) : sc_module(nm) { + SC_METHOD(handle_clk); + sensitive << p_clk; + dont_initialize(); + } + + // In port, where sc_in is just a sc_port<sc_signal_in_if<bool>>. + sc_in<bool> p_clk; + + private: + void handle_clk() { + if (p_clk.read()) { + LOGM("TICK"); + } else { + LOGM("TOCK"); + } + } +}; + +// -- SC_MAIN ------------------------------------------------------------------ + +extern "C" int sc_main(int, char*[]) { + clock_gen clk("clk"); + ticktock tiktok{"listen"}; + + tiktok.p_clk(clk.p_clk); + + sc_start(200, SC_NS); + return 0; +} |