cancel_all.cpp revision 12855:588919e0e4aa
1#define SC_INCLUDE_DYNAMIC_PROCESSES
2#include "systemc"
3using namespace sc_core;
4using namespace sc_dt;
5using namespace std;
6
7#include "tlm.h"
8#include "tlm_utils/peq_with_cb_and_phase.h"
9#include "tlm_utils/peq_with_get.h"
10
11
12SC_MODULE(Test_peq_with_cb)
13{
14
15  SC_CTOR(Test_peq_with_cb)
16  : m_peq(this, &Test_peq_with_cb::peq_cb), flag1(true), flag2(true)
17  {
18    SC_THREAD(thread);
19  }
20
21  void thread()
22  {
23    section = 1;
24
25    tlm::tlm_generic_payload *trans;
26    tlm::tlm_phase phase;
27    for (int i = 0; i < 50; i++)
28    {
29      trans = new tlm::tlm_generic_payload;
30      trans->set_address(i);
31      m_peq.notify( *trans, phase, sc_time(rand() % 100, SC_NS) );
32    }
33    wait(50, SC_NS);
34
35    m_peq.cancel_all();
36    cout << "cancel_all\n";
37    section = 2;
38
39    for (int i = 100; i < 150; i++)
40    {
41      trans = new tlm::tlm_generic_payload;
42      trans->set_address(i);
43      m_peq.notify( *trans, phase, sc_time(rand() % 100, SC_NS) );
44    }
45    wait(50, SC_NS);
46    m_peq.cancel_all();
47    cout << "cancel_all\n";
48
49    wait(50, SC_NS);
50  }
51
52  void peq_cb(tlm::tlm_generic_payload& trans, const tlm::tlm_phase& phase)
53  {
54    sc_time t = sc_time_stamp();
55    sc_dt::uint64 adr = trans.get_address();
56    sc_assert( section == 1 || section == 2 );
57    if (section == 1)
58    {
59      if (flag1) cout << "Called peq_cb with section = " << section << "\n";
60      flag1 = false;
61      sc_assert( t >= sc_time(0, SC_NS) && t <= sc_time(50, SC_NS) );
62      sc_assert( adr >= 0 && adr < 50);
63    }
64    else if (section == 2)
65    {
66      if (flag2) cout << "Called peq_cb with section = " << section << "\n";
67      flag2 = false;
68      sc_assert( t >= sc_time(50, SC_NS) && t <= sc_time(100, SC_NS) );
69      sc_assert( adr >= 100 && adr < 150);
70    }
71  }
72
73  int section;
74  tlm_utils::peq_with_cb_and_phase<Test_peq_with_cb> m_peq;
75  bool flag1, flag2;
76};
77
78
79SC_MODULE(Test_peq_with_get)
80{
81
82  SC_CTOR(Test_peq_with_get)
83  : m_peq("foo"), flag3(true), flag4(true)
84  {
85    SC_THREAD(thread);
86    SC_THREAD(ass_end_thread);
87  }
88
89  void thread()
90  {
91    wait(1000, SC_NS);
92
93    section = 3;
94
95    tlm::tlm_generic_payload *trans;
96    for (int i = 0; i < 50; i++)
97    {
98      trans = new tlm::tlm_generic_payload;
99      trans->set_address(i);
100      m_peq.notify( *trans, sc_time(rand() % 100, SC_NS) );
101    }
102    wait(50, SC_NS);
103
104    m_peq.cancel_all();
105    cout << "cancel_all\n";
106    section = 4;
107
108    for (int i = 100; i < 150; i++)
109    {
110      trans = new tlm::tlm_generic_payload;
111      trans->set_address(i);
112      m_peq.notify( *trans, sc_time(rand() % 100, SC_NS) );
113    }
114    wait(50, SC_NS);
115    m_peq.cancel_all();
116    cout << "cancel_all\n";
117
118    wait(50, SC_NS);
119  }
120
121  void ass_end_thread()
122  {
123    tlm::tlm_generic_payload *trans;
124    for(;;)
125    {
126      wait(m_peq.get_event());
127      while( (trans = m_peq.get_next_transaction()) )
128      {
129        sc_time t = sc_time_stamp();
130        sc_dt::uint64 adr = trans->get_address();
131        sc_assert( section == 3 || section == 4 );
132        if (section == 3)
133        {
134          if (flag3) cout << "Called get_next_transaction with section = " << section << "\n";
135          flag3 = false;
136          sc_assert( t >= sc_time(1000, SC_NS) && t <= sc_time(1050, SC_NS) );
137          sc_assert( adr >= 0 && adr < 50);
138        }
139        else if (section == 4)
140        {
141          if (flag4) cout << "Called get_next_transaction with section = " << section << "\n";
142          flag4 = false;
143          sc_assert( t >= sc_time(1050, SC_NS) && t <= sc_time(1100, SC_NS) );
144          sc_assert( adr >= 100 && adr < 150);
145        }
146      }
147    }
148  }
149
150  int section;
151  tlm_utils::peq_with_get<tlm::tlm_generic_payload> m_peq;
152  bool flag3, flag4;
153};
154
155
156int sc_main(int argc, char* argv[])
157{
158  cout << "Unit test for cancel_all()\n";
159  Test_peq_with_cb  cb("test_peq_with_cb");
160  Test_peq_with_get get("test_peq_with_get");
161  sc_start();
162  return 0;
163}
164
165