112855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES
212855Sgabeblack@google.com#include "systemc"
312855Sgabeblack@google.comusing namespace sc_core;
412855Sgabeblack@google.comusing namespace sc_dt;
512855Sgabeblack@google.comusing namespace std;
612855Sgabeblack@google.com
712855Sgabeblack@google.com#include "tlm.h"
812855Sgabeblack@google.com#include "tlm_utils/peq_with_cb_and_phase.h"
912855Sgabeblack@google.com#include "tlm_utils/peq_with_get.h"
1012855Sgabeblack@google.com
1112855Sgabeblack@google.com
1212855Sgabeblack@google.comSC_MODULE(Test_peq_with_cb)
1312855Sgabeblack@google.com{
1412855Sgabeblack@google.com
1512855Sgabeblack@google.com  SC_CTOR(Test_peq_with_cb)
1612855Sgabeblack@google.com  : m_peq(this, &Test_peq_with_cb::peq_cb), flag1(true), flag2(true)
1712855Sgabeblack@google.com  {
1812855Sgabeblack@google.com    SC_THREAD(thread);
1912855Sgabeblack@google.com  }
2012855Sgabeblack@google.com
2112855Sgabeblack@google.com  void thread()
2212855Sgabeblack@google.com  {
2312855Sgabeblack@google.com    section = 1;
2412855Sgabeblack@google.com
2512855Sgabeblack@google.com    tlm::tlm_generic_payload *trans;
2612855Sgabeblack@google.com    tlm::tlm_phase phase;
2712855Sgabeblack@google.com    for (int i = 0; i < 50; i++)
2812855Sgabeblack@google.com    {
2912855Sgabeblack@google.com      trans = new tlm::tlm_generic_payload;
3012855Sgabeblack@google.com      trans->set_address(i);
3112855Sgabeblack@google.com      m_peq.notify( *trans, phase, sc_time(rand() % 100, SC_NS) );
3212855Sgabeblack@google.com    }
3312855Sgabeblack@google.com    wait(50, SC_NS);
3412855Sgabeblack@google.com
3512855Sgabeblack@google.com    m_peq.cancel_all();
3612855Sgabeblack@google.com    cout << "cancel_all\n";
3712855Sgabeblack@google.com    section = 2;
3812855Sgabeblack@google.com
3912855Sgabeblack@google.com    for (int i = 100; i < 150; i++)
4012855Sgabeblack@google.com    {
4112855Sgabeblack@google.com      trans = new tlm::tlm_generic_payload;
4212855Sgabeblack@google.com      trans->set_address(i);
4312855Sgabeblack@google.com      m_peq.notify( *trans, phase, sc_time(rand() % 100, SC_NS) );
4412855Sgabeblack@google.com    }
4512855Sgabeblack@google.com    wait(50, SC_NS);
4612855Sgabeblack@google.com    m_peq.cancel_all();
4712855Sgabeblack@google.com    cout << "cancel_all\n";
4812855Sgabeblack@google.com
4912855Sgabeblack@google.com    wait(50, SC_NS);
5012855Sgabeblack@google.com  }
5112855Sgabeblack@google.com
5212855Sgabeblack@google.com  void peq_cb(tlm::tlm_generic_payload& trans, const tlm::tlm_phase& phase)
5312855Sgabeblack@google.com  {
5412855Sgabeblack@google.com    sc_time t = sc_time_stamp();
5512855Sgabeblack@google.com    sc_dt::uint64 adr = trans.get_address();
5612855Sgabeblack@google.com    sc_assert( section == 1 || section == 2 );
5712855Sgabeblack@google.com    if (section == 1)
5812855Sgabeblack@google.com    {
5912855Sgabeblack@google.com      if (flag1) cout << "Called peq_cb with section = " << section << "\n";
6012855Sgabeblack@google.com      flag1 = false;
6112855Sgabeblack@google.com      sc_assert( t >= sc_time(0, SC_NS) && t <= sc_time(50, SC_NS) );
6212855Sgabeblack@google.com      sc_assert( adr >= 0 && adr < 50);
6312855Sgabeblack@google.com    }
6412855Sgabeblack@google.com    else if (section == 2)
6512855Sgabeblack@google.com    {
6612855Sgabeblack@google.com      if (flag2) cout << "Called peq_cb with section = " << section << "\n";
6712855Sgabeblack@google.com      flag2 = false;
6812855Sgabeblack@google.com      sc_assert( t >= sc_time(50, SC_NS) && t <= sc_time(100, SC_NS) );
6912855Sgabeblack@google.com      sc_assert( adr >= 100 && adr < 150);
7012855Sgabeblack@google.com    }
7112855Sgabeblack@google.com  }
7212855Sgabeblack@google.com
7312855Sgabeblack@google.com  int section;
7412855Sgabeblack@google.com  tlm_utils::peq_with_cb_and_phase<Test_peq_with_cb> m_peq;
7512855Sgabeblack@google.com  bool flag1, flag2;
7612855Sgabeblack@google.com};
7712855Sgabeblack@google.com
7812855Sgabeblack@google.com
7912855Sgabeblack@google.comSC_MODULE(Test_peq_with_get)
8012855Sgabeblack@google.com{
8112855Sgabeblack@google.com
8212855Sgabeblack@google.com  SC_CTOR(Test_peq_with_get)
8312855Sgabeblack@google.com  : m_peq("foo"), flag3(true), flag4(true)
8412855Sgabeblack@google.com  {
8512855Sgabeblack@google.com    SC_THREAD(thread);
8612855Sgabeblack@google.com    SC_THREAD(ass_end_thread);
8712855Sgabeblack@google.com  }
8812855Sgabeblack@google.com
8912855Sgabeblack@google.com  void thread()
9012855Sgabeblack@google.com  {
9112855Sgabeblack@google.com    wait(1000, SC_NS);
9212855Sgabeblack@google.com
9312855Sgabeblack@google.com    section = 3;
9412855Sgabeblack@google.com
9512855Sgabeblack@google.com    tlm::tlm_generic_payload *trans;
9612855Sgabeblack@google.com    for (int i = 0; i < 50; i++)
9712855Sgabeblack@google.com    {
9812855Sgabeblack@google.com      trans = new tlm::tlm_generic_payload;
9912855Sgabeblack@google.com      trans->set_address(i);
10012855Sgabeblack@google.com      m_peq.notify( *trans, sc_time(rand() % 100, SC_NS) );
10112855Sgabeblack@google.com    }
10212855Sgabeblack@google.com    wait(50, SC_NS);
10312855Sgabeblack@google.com
10412855Sgabeblack@google.com    m_peq.cancel_all();
10512855Sgabeblack@google.com    cout << "cancel_all\n";
10612855Sgabeblack@google.com    section = 4;
10712855Sgabeblack@google.com
10812855Sgabeblack@google.com    for (int i = 100; i < 150; i++)
10912855Sgabeblack@google.com    {
11012855Sgabeblack@google.com      trans = new tlm::tlm_generic_payload;
11112855Sgabeblack@google.com      trans->set_address(i);
11212855Sgabeblack@google.com      m_peq.notify( *trans, sc_time(rand() % 100, SC_NS) );
11312855Sgabeblack@google.com    }
11412855Sgabeblack@google.com    wait(50, SC_NS);
11512855Sgabeblack@google.com    m_peq.cancel_all();
11612855Sgabeblack@google.com    cout << "cancel_all\n";
11712855Sgabeblack@google.com
11812855Sgabeblack@google.com    wait(50, SC_NS);
11912855Sgabeblack@google.com  }
12012855Sgabeblack@google.com
12112855Sgabeblack@google.com  void ass_end_thread()
12212855Sgabeblack@google.com  {
12312855Sgabeblack@google.com    tlm::tlm_generic_payload *trans;
12412855Sgabeblack@google.com    for(;;)
12512855Sgabeblack@google.com    {
12612855Sgabeblack@google.com      wait(m_peq.get_event());
12712855Sgabeblack@google.com      while( (trans = m_peq.get_next_transaction()) )
12812855Sgabeblack@google.com      {
12912855Sgabeblack@google.com        sc_time t = sc_time_stamp();
13012855Sgabeblack@google.com        sc_dt::uint64 adr = trans->get_address();
13112855Sgabeblack@google.com        sc_assert( section == 3 || section == 4 );
13212855Sgabeblack@google.com        if (section == 3)
13312855Sgabeblack@google.com        {
13412855Sgabeblack@google.com          if (flag3) cout << "Called get_next_transaction with section = " << section << "\n";
13512855Sgabeblack@google.com          flag3 = false;
13612855Sgabeblack@google.com          sc_assert( t >= sc_time(1000, SC_NS) && t <= sc_time(1050, SC_NS) );
13712855Sgabeblack@google.com          sc_assert( adr >= 0 && adr < 50);
13812855Sgabeblack@google.com        }
13912855Sgabeblack@google.com        else if (section == 4)
14012855Sgabeblack@google.com        {
14112855Sgabeblack@google.com          if (flag4) cout << "Called get_next_transaction with section = " << section << "\n";
14212855Sgabeblack@google.com          flag4 = false;
14312855Sgabeblack@google.com          sc_assert( t >= sc_time(1050, SC_NS) && t <= sc_time(1100, SC_NS) );
14412855Sgabeblack@google.com          sc_assert( adr >= 100 && adr < 150);
14512855Sgabeblack@google.com        }
14612855Sgabeblack@google.com      }
14712855Sgabeblack@google.com    }
14812855Sgabeblack@google.com  }
14912855Sgabeblack@google.com
15012855Sgabeblack@google.com  int section;
15112855Sgabeblack@google.com  tlm_utils::peq_with_get<tlm::tlm_generic_payload> m_peq;
15212855Sgabeblack@google.com  bool flag3, flag4;
15312855Sgabeblack@google.com};
15412855Sgabeblack@google.com
15512855Sgabeblack@google.com
15612855Sgabeblack@google.comint sc_main(int argc, char* argv[])
15712855Sgabeblack@google.com{
15812855Sgabeblack@google.com  cout << "Unit test for cancel_all()\n";
15912855Sgabeblack@google.com  Test_peq_with_cb  cb("test_peq_with_cb");
16012855Sgabeblack@google.com  Test_peq_with_get get("test_peq_with_get");
16112855Sgabeblack@google.com  sc_start();
16212855Sgabeblack@google.com  return 0;
16312855Sgabeblack@google.com}
16412855Sgabeblack@google.com
165