diff options
Diffstat (limited to 'src/sc_export.cc')
-rw-r--r-- | src/sc_export.cc | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/sc_export.cc b/src/sc_export.cc new file mode 100644 index 0000000..7acca58 --- /dev/null +++ b/src/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; +} |