112855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES
212855Sgabeblack@google.com
312855Sgabeblack@google.com#include <systemc>
412855Sgabeblack@google.com#include <cstring>
512855Sgabeblack@google.comusing namespace sc_core;
612855Sgabeblack@google.comusing namespace sc_dt;
712855Sgabeblack@google.comusing sc_core::wait;
812855Sgabeblack@google.comusing std::cout;
912855Sgabeblack@google.comusing std::endl;
1012855Sgabeblack@google.com
1112855Sgabeblack@google.com// 8) get_parent_object, get_child_objects
1212855Sgabeblack@google.com
1312855Sgabeblack@google.comstruct Object: sc_object
1412855Sgabeblack@google.com{
1512855Sgabeblack@google.com  Object(const char* _n) : sc_object(_n) {}
1612855Sgabeblack@google.com};
1712855Sgabeblack@google.com
1812855Sgabeblack@google.comSC_MODULE(M)
1912855Sgabeblack@google.com{
2012855Sgabeblack@google.com  sc_signal<bool> sig;
2112855Sgabeblack@google.com  sc_object* T_obj;
2212855Sgabeblack@google.com
2312855Sgabeblack@google.com  SC_CTOR(M)
2412855Sgabeblack@google.com  {
2512855Sgabeblack@google.com    std::vector<sc_object*> children = sig.get_child_objects();
2612855Sgabeblack@google.com    sc_assert (children.size() == 0);
2712855Sgabeblack@google.com    SC_THREAD(T);
2812855Sgabeblack@google.com    sc_process_handle h = sc_get_current_process_handle();
2912855Sgabeblack@google.com    sc_assert (h.valid());
3012855Sgabeblack@google.com    T_obj = h.get_process_object();
3112855Sgabeblack@google.com    children = h.get_child_objects();
3212855Sgabeblack@google.com    sc_assert (children.size() == 0);
3312855Sgabeblack@google.com    children = h.get_process_object()->get_child_objects();
3412855Sgabeblack@google.com    sc_assert (children.size() == 0);
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com    Object obj("obj");
3712855Sgabeblack@google.com    children = obj.get_child_objects();
3812855Sgabeblack@google.com    sc_assert (children.size() == 0);
3912855Sgabeblack@google.com    sc_assert (obj.get_parent_object() == this);
4012855Sgabeblack@google.com  }
4112855Sgabeblack@google.com
4212855Sgabeblack@google.com  void T()
4312855Sgabeblack@google.com  {
4412855Sgabeblack@google.com    sc_process_handle h = sc_get_current_process_handle();
4512855Sgabeblack@google.com    sc_assert (h.valid());
4612855Sgabeblack@google.com    sc_assert (h.dynamic() == false);
4712855Sgabeblack@google.com    sc_assert (h.get_process_object() == T_obj);
4812855Sgabeblack@google.com    sc_assert (h.get_parent_object() == this);
4912855Sgabeblack@google.com    std::vector<sc_object*> children = h.get_child_objects();
5012855Sgabeblack@google.com    sc_assert (children.size() == 0);
5112855Sgabeblack@google.com    children = h.get_process_object()->get_child_objects();
5212855Sgabeblack@google.com    sc_assert (children.size() == 0);
5312855Sgabeblack@google.com
5412855Sgabeblack@google.com    Object obj("obj");
5512855Sgabeblack@google.com    children = obj.get_child_objects();
5612855Sgabeblack@google.com    sc_assert (children.size() == 0);
5712855Sgabeblack@google.com    sc_assert (obj.get_parent_object() == h.get_process_object());
5812855Sgabeblack@google.com
5912855Sgabeblack@google.com    children = h.get_child_objects();
6012855Sgabeblack@google.com    sc_assert (children.size() == 1);
6112855Sgabeblack@google.com    sc_assert (children[0] == &obj);
6212855Sgabeblack@google.com    sc_assert (strcmp(children[0]->name(), "top.m.T.obj") == 0);
6312855Sgabeblack@google.com    sc_assert (children[0]->get_parent_object() == h.get_process_object());
6412855Sgabeblack@google.com
6512855Sgabeblack@google.com    Object obj2("obj2");
6612855Sgabeblack@google.com    children = h.get_child_objects();
6712855Sgabeblack@google.com    sc_assert (children.size() == 2);
6812855Sgabeblack@google.com    sc_assert (children[0] == &obj);
6912855Sgabeblack@google.com    sc_assert (children[1] == &obj2);
7012855Sgabeblack@google.com    sc_assert (children[0]->get_parent_object() == h.get_process_object());
7112855Sgabeblack@google.com    sc_assert (children[1]->get_parent_object() == h.get_process_object());
7212855Sgabeblack@google.com
7312855Sgabeblack@google.com    wait (1, SC_NS);
7412855Sgabeblack@google.com    sc_spawn(sc_bind(&M::dynamic_proc, this), "dynamic_proc");
7512855Sgabeblack@google.com    wait (1, SC_NS);
7612855Sgabeblack@google.com
7712855Sgabeblack@google.com    sc_assert (h.valid());
7812855Sgabeblack@google.com  }
7912855Sgabeblack@google.com
8012855Sgabeblack@google.com  sc_object* dynamic_proc_obj;
8112855Sgabeblack@google.com
8212855Sgabeblack@google.com  void dynamic_proc()
8312855Sgabeblack@google.com  {
8412855Sgabeblack@google.com    sc_process_handle h = sc_get_current_process_handle();
8512855Sgabeblack@google.com    sc_assert (h.valid());
8612855Sgabeblack@google.com    dynamic_proc_obj = h.get_process_object();
8712855Sgabeblack@google.com    sc_assert (h.dynamic() == true);
8812855Sgabeblack@google.com    sc_assert (h.get_parent_object() == T_obj);
8912855Sgabeblack@google.com    std::vector<sc_object*> children = h.get_child_objects();
9012855Sgabeblack@google.com    sc_assert (children.size() == 0);
9112855Sgabeblack@google.com    children = h.get_process_object()->get_child_objects();
9212855Sgabeblack@google.com    sc_assert (children.size() == 0);
9312855Sgabeblack@google.com
9412855Sgabeblack@google.com    Object obj("obj");
9512855Sgabeblack@google.com    children = obj.get_child_objects();
9612855Sgabeblack@google.com    sc_assert (children.size() == 0);
9712855Sgabeblack@google.com    sc_assert (obj.get_parent_object() == dynamic_proc_obj);
9812855Sgabeblack@google.com
9912855Sgabeblack@google.com    children = h.get_child_objects();
10012855Sgabeblack@google.com    sc_assert (children.size() == 1);
10112855Sgabeblack@google.com    sc_assert (children[0] == &obj);
10212855Sgabeblack@google.com    sc_assert (strcmp(children[0]->name(), "top.m.T.dynamic_proc.obj") == 0);
10312855Sgabeblack@google.com    sc_assert (children[0]->get_parent_object() == dynamic_proc_obj);
10412855Sgabeblack@google.com
10512855Sgabeblack@google.com    Object obj2("obj2");
10612855Sgabeblack@google.com    children = h.get_child_objects();
10712855Sgabeblack@google.com    sc_assert (children.size() == 2);
10812855Sgabeblack@google.com    sc_assert (children[0] == &obj);
10912855Sgabeblack@google.com    sc_assert (children[1] == &obj2);
11012855Sgabeblack@google.com    sc_assert (children[0]->get_parent_object() == dynamic_proc_obj);
11112855Sgabeblack@google.com    sc_assert (children[1]->get_parent_object() == dynamic_proc_obj);
11212855Sgabeblack@google.com
11312855Sgabeblack@google.com    wait (1, SC_NS);
11412855Sgabeblack@google.com    sc_process_handle h2 = sc_spawn(sc_bind(&M::dynamic_proc2, this), "dynamic_proc2");
11512855Sgabeblack@google.com    wait (1, SC_NS);
11612855Sgabeblack@google.com
11712855Sgabeblack@google.com    children = h.get_child_objects();
11812855Sgabeblack@google.com    sc_assert (children.size() == 3);
11912855Sgabeblack@google.com    sc_assert (children[0] == &obj);
12012855Sgabeblack@google.com    sc_assert (children[1] == &obj2);
12112855Sgabeblack@google.com    sc_assert (children[2] == h2.get_process_object());
12212855Sgabeblack@google.com    sc_assert (children[0]->get_parent_object() == dynamic_proc_obj);
12312855Sgabeblack@google.com    sc_assert (children[1]->get_parent_object() == dynamic_proc_obj);
12412855Sgabeblack@google.com    sc_assert (children[2]->get_parent_object() == dynamic_proc_obj);
12512855Sgabeblack@google.com
12612855Sgabeblack@google.com    sc_assert (h.valid());
12712855Sgabeblack@google.com  }
12812855Sgabeblack@google.com
12912855Sgabeblack@google.com  void dynamic_proc2()
13012855Sgabeblack@google.com  {
13112855Sgabeblack@google.com    sc_process_handle h = sc_get_current_process_handle();
13212855Sgabeblack@google.com    sc_assert (h.valid());
13312855Sgabeblack@google.com    sc_assert (h.dynamic() == true);
13412855Sgabeblack@google.com    sc_assert (h.get_parent_object() == dynamic_proc_obj);
13512855Sgabeblack@google.com    std::vector<sc_object*> children = h.get_child_objects();
13612855Sgabeblack@google.com    sc_assert (children.size() == 0);
13712855Sgabeblack@google.com    children = h.get_process_object()->get_child_objects();
13812855Sgabeblack@google.com    sc_assert (children.size() == 0);
13912855Sgabeblack@google.com
14012855Sgabeblack@google.com    Object obj("obj");
14112855Sgabeblack@google.com    children = obj.get_child_objects();
14212855Sgabeblack@google.com    sc_assert (children.size() == 0);
14312855Sgabeblack@google.com    sc_assert (obj.get_parent_object() == h.get_process_object());
14412855Sgabeblack@google.com
14512855Sgabeblack@google.com    children = h.get_child_objects();
14612855Sgabeblack@google.com    sc_assert (children.size() == 1);
14712855Sgabeblack@google.com    sc_assert (children[0] == &obj);
14812855Sgabeblack@google.com    sc_assert (strcmp(children[0]->name(), "top.m.T.dynamic_proc.dynamic_proc2.obj") == 0);
14912855Sgabeblack@google.com    sc_assert (children[0]->get_parent_object() == h.get_process_object());
15012855Sgabeblack@google.com
15112855Sgabeblack@google.com    Object obj2("obj2");
15212855Sgabeblack@google.com    children = h.get_child_objects();
15312855Sgabeblack@google.com    sc_assert (children.size() == 2);
15412855Sgabeblack@google.com    sc_assert (children[0] == &obj);
15512855Sgabeblack@google.com    sc_assert (children[1] == &obj2);
15612855Sgabeblack@google.com    sc_assert (children[0]->get_parent_object() == h.get_process_object());
15712855Sgabeblack@google.com    sc_assert (children[1]->get_parent_object() == h.get_process_object());
15812855Sgabeblack@google.com
15912855Sgabeblack@google.com    wait(10, SC_NS);
16012855Sgabeblack@google.com
16112855Sgabeblack@google.com    sc_assert (h.valid());
16212855Sgabeblack@google.com  }
16312855Sgabeblack@google.com};
16412855Sgabeblack@google.com
16512855Sgabeblack@google.comvoid g(sc_object* obj)
16612855Sgabeblack@google.com{
16712855Sgabeblack@google.com  sc_process_handle h = sc_get_current_process_handle();
16812855Sgabeblack@google.com  sc_object* this_process = h.get_process_object();
16912855Sgabeblack@google.com  sc_assert(this_process->get_parent_object() == obj);
17012855Sgabeblack@google.com}
17112855Sgabeblack@google.comvoid f()
17212855Sgabeblack@google.com{
17312855Sgabeblack@google.com  sc_object* obj = sc_get_current_process_handle().get_process_object();
17412855Sgabeblack@google.com  sc_process_handle h = sc_spawn(sc_bind(&g, obj));
17512855Sgabeblack@google.com  std::vector<sc_object*> children = obj->get_child_objects();
17612855Sgabeblack@google.com  sc_assert (children.size() == 1);
17712855Sgabeblack@google.com  wait(1, SC_NS);
17812855Sgabeblack@google.com}
17912855Sgabeblack@google.com
18012855Sgabeblack@google.comSC_MODULE(Top)
18112855Sgabeblack@google.com{
18212855Sgabeblack@google.com  sc_in_clk clk;
18312855Sgabeblack@google.com  sc_port<sc_signal_in_if<int>, 1, SC_ZERO_OR_MORE_BOUND> p;
18412855Sgabeblack@google.com  sc_export<sc_signal_in_if<int> > xp;
18512855Sgabeblack@google.com  sc_signal<int> sig;
18612855Sgabeblack@google.com
18712855Sgabeblack@google.com  M *m;
18812855Sgabeblack@google.com  SC_CTOR(Top)
18912855Sgabeblack@google.com    : clk("clk"), p("p"), xp("xp"), sig("sig")
19012855Sgabeblack@google.com  {
19112855Sgabeblack@google.com    m = new M("m");
19212855Sgabeblack@google.com    xp.bind(sig);
19312855Sgabeblack@google.com
19412855Sgabeblack@google.com    SC_THREAD(p1);
19512855Sgabeblack@google.com    SC_CTHREAD(p2,clk);
19612855Sgabeblack@google.com    SC_METHOD(p3);
19712855Sgabeblack@google.com    sc_spawn(&f,"p4");
19812855Sgabeblack@google.com
19912855Sgabeblack@google.com    sc_object* parent = this->get_parent_object();
20012855Sgabeblack@google.com    sc_assert (parent == 0);
20112855Sgabeblack@google.com
20212855Sgabeblack@google.com    std::vector<sc_object*> children = this->get_child_objects();
20312855Sgabeblack@google.com    sc_assert (children.size() == 9);
20412855Sgabeblack@google.com    sc_assert (strcmp(children[0]->name(), "top.clk") == 0);
20512855Sgabeblack@google.com    sc_assert (strcmp(children[1]->name(), "top.p") == 0);
20612855Sgabeblack@google.com    sc_assert (strcmp(children[2]->name(), "top.xp") == 0);
20712855Sgabeblack@google.com    sc_assert (strcmp(children[3]->name(), "top.sig") == 0);
20812855Sgabeblack@google.com    sc_assert (strcmp(children[4]->name(), "top.m") == 0);
20912855Sgabeblack@google.com    sc_assert (strcmp(children[5]->name(), "top.p1") == 0);
21012855Sgabeblack@google.com    sc_assert (strcmp(children[6]->name(), "top.p2") == 0);
21112855Sgabeblack@google.com    sc_assert (strcmp(children[7]->name(), "top.p3") == 0);
21212855Sgabeblack@google.com    sc_assert (strcmp(children[8]->name(), "top.p4") == 0);
21312855Sgabeblack@google.com
21412855Sgabeblack@google.com    for (unsigned i = 0; i < children.size(); i++)
21512855Sgabeblack@google.com      sc_assert (children[i]->get_parent_object() == this);
21612855Sgabeblack@google.com  }
21712855Sgabeblack@google.com  void p1() {}
21812855Sgabeblack@google.com  void p2() {}
21912855Sgabeblack@google.com  void p3() {}
22012855Sgabeblack@google.com};
22112855Sgabeblack@google.com
22212855Sgabeblack@google.comint sc_main(int argc, char* argv[])
22312855Sgabeblack@google.com{
22412855Sgabeblack@google.com  cout << "Should be silent..." << endl;
22512855Sgabeblack@google.com
22612855Sgabeblack@google.com  Object obj("obj");
22712855Sgabeblack@google.com  std::vector<sc_object*> children = obj.get_child_objects();
22812855Sgabeblack@google.com  sc_assert (children.size() == 0);
22912855Sgabeblack@google.com  sc_assert (obj.get_parent_object() == 0);
23012855Sgabeblack@google.com
23112855Sgabeblack@google.com  sc_signal<bool> clk;
23212855Sgabeblack@google.com  Top top("top");
23312855Sgabeblack@google.com  top.clk(clk);
23412855Sgabeblack@google.com  sc_start();
23512855Sgabeblack@google.com
23612855Sgabeblack@google.com  cout << endl << "Success" << endl;
23712855Sgabeblack@google.com  return 0;
23812855Sgabeblack@google.com}
239