summaryrefslogtreecommitdiff
path: root/src/systemc/tests/tlm/cancel_all/cancel_all.cpp
blob: df97d89629fbd557230b55002892d08ae4592b30 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#define SC_INCLUDE_DYNAMIC_PROCESSES
#include "systemc"
using namespace sc_core;
using namespace sc_dt;
using namespace std;

#include "tlm.h"
#include "tlm_utils/peq_with_cb_and_phase.h"
#include "tlm_utils/peq_with_get.h"


SC_MODULE(Test_peq_with_cb)
{

  SC_CTOR(Test_peq_with_cb)
  : m_peq(this, &Test_peq_with_cb::peq_cb), flag1(true), flag2(true)
  {
    SC_THREAD(thread);
  }

  void thread()
  {
    section = 1;

    tlm::tlm_generic_payload *trans;
    tlm::tlm_phase phase;
    for (int i = 0; i < 50; i++)
    {
      trans = new tlm::tlm_generic_payload;
      trans->set_address(i);
      m_peq.notify( *trans, phase, sc_time(rand() % 100, SC_NS) );
    }
    wait(50, SC_NS);

    m_peq.cancel_all();
    cout << "cancel_all\n";
    section = 2;

    for (int i = 100; i < 150; i++)
    {
      trans = new tlm::tlm_generic_payload;
      trans->set_address(i);
      m_peq.notify( *trans, phase, sc_time(rand() % 100, SC_NS) );
    }
    wait(50, SC_NS);
    m_peq.cancel_all();
    cout << "cancel_all\n";

    wait(50, SC_NS);
  }

  void peq_cb(tlm::tlm_generic_payload& trans, const tlm::tlm_phase& phase)
  {
    sc_time t = sc_time_stamp();
    sc_dt::uint64 adr = trans.get_address();
    sc_assert( section == 1 || section == 2 );
    if (section == 1)
    {
      if (flag1) cout << "Called peq_cb with section = " << section << "\n";
      flag1 = false;
      sc_assert( t >= sc_time(0, SC_NS) && t <= sc_time(50, SC_NS) );
      sc_assert( adr >= 0 && adr < 50);
    }
    else if (section == 2)
    {
      if (flag2) cout << "Called peq_cb with section = " << section << "\n";
      flag2 = false;
      sc_assert( t >= sc_time(50, SC_NS) && t <= sc_time(100, SC_NS) );
      sc_assert( adr >= 100 && adr < 150);
    }
  }

  int section;
  tlm_utils::peq_with_cb_and_phase<Test_peq_with_cb> m_peq;
  bool flag1, flag2;
};


SC_MODULE(Test_peq_with_get)
{

  SC_CTOR(Test_peq_with_get)
  : m_peq("foo"), flag3(true), flag4(true)
  {
    SC_THREAD(thread);
    SC_THREAD(ass_end_thread);
  }

  void thread()
  {
    wait(1000, SC_NS);

    section = 3;

    tlm::tlm_generic_payload *trans;
    for (int i = 0; i < 50; i++)
    {
      trans = new tlm::tlm_generic_payload;
      trans->set_address(i);
      m_peq.notify( *trans, sc_time(rand() % 100, SC_NS) );
    }
    wait(50, SC_NS);

    m_peq.cancel_all();
    cout << "cancel_all\n";
    section = 4;

    for (int i = 100; i < 150; i++)
    {
      trans = new tlm::tlm_generic_payload;
      trans->set_address(i);
      m_peq.notify( *trans, sc_time(rand() % 100, SC_NS) );
    }
    wait(50, SC_NS);
    m_peq.cancel_all();
    cout << "cancel_all\n";

    wait(50, SC_NS);
  }

  void ass_end_thread()
  {
    tlm::tlm_generic_payload *trans;
    for(;;)
    {
      wait(m_peq.get_event());
      while( (trans = m_peq.get_next_transaction()) )
      {
        sc_time t = sc_time_stamp();
        sc_dt::uint64 adr = trans->get_address();
        sc_assert( section == 3 || section == 4 );
        if (section == 3)
        {
          if (flag3) cout << "Called get_next_transaction with section = " << section << "\n";
          flag3 = false;
          sc_assert( t >= sc_time(1000, SC_NS) && t <= sc_time(1050, SC_NS) );
          sc_assert( adr >= 0 && adr < 50);
        }
        else if (section == 4)
        {
          if (flag4) cout << "Called get_next_transaction with section = " << section << "\n";
          flag4 = false;
          sc_assert( t >= sc_time(1050, SC_NS) && t <= sc_time(1100, SC_NS) );
          sc_assert( adr >= 100 && adr < 150);
        }
      }
    }
  }

  int section;
  tlm_utils::peq_with_get<tlm::tlm_generic_payload> m_peq;
  bool flag3, flag4;
};


int sc_main(int argc, char* argv[])
{
  cout << "Unit test for cancel_all()\n";
  Test_peq_with_cb  cb("test_peq_with_cb");
  Test_peq_with_get get("test_peq_with_get");
  sc_start();
  return 0;
}