aboutsummaryrefslogtreecommitdiff
path: root/src/sc_export.cc
blob: 7acca58dd39ced89a8cfbd8814b31f2eb57cf9f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
}