wait.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  wait.cpp --
23
24  Original Author: Daniel Aarno, Intel, Corp 2015-07-23
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38#include <systemc>
39
40using sc_core::sc_time;
41using sc_core::SC_US;
42
43namespace {
44int first_line = 0;
45
46bool endsWith(const std::string &str, const std::string &ending) {
47    if (ending.size() > str.size()) {
48        return false;
49    }
50    return std::equal(ending.rbegin(), ending.rend(), str.rbegin());
51}
52
53bool waitFunIsTrue() {
54  if (sc_core::sc_time_stamp() < sc_time(2000, SC_US)) {
55    return false;
56  }
57
58  if (sc_core::sc_time_stamp() < sc_time(3000, SC_US)) {
59    sc_time delay(10, SC_US);
60    SC_WAITN(delay);
61    return false;
62  }
63
64  if (sc_core::sc_time_stamp() < sc_time(4000, SC_US)) {
65    return false;
66  }
67
68  return true;
69}
70
71SC_MODULE(Wait) {
72  SC_CTOR(Wait) : clk( "clk", 10, SC_US, 0.5, 100, SC_US) {
73    SC_THREAD(thread);
74    sensitive << clk;
75  }
76
77  void thread() {
78    sc_time delay(10, SC_US);
79    wait(delay);  // 1st
80
81    first_line = __LINE__ + 1;
82    SC_WAITN(delay);  // Wait some time
83    wait(delay);  // 2nd
84
85    SC_WAIT();  // Wait for clk
86    wait(delay);  // 3rd
87
88    SC_WAIT_UNTIL(sc_core::sc_time_stamp() > sc_time(1000, SC_US));
89    wait(delay);  // 4th
90
91    SC_WAIT_UNTIL(waitFunIsTrue());
92  }
93
94  sc_core::sc_clock clk;
95};
96
97}  // namespace
98
99int sc_main(int argc, char ** argv) {
100  Wait w("dut");
101
102  sc_core::sc_process_handle hnd( sc_core::sc_find_object("dut.thread") );
103  const sc_core::sc_process_b *thread
104    = dynamic_cast<sc_core::sc_process_b*>(hnd.get_process_object());
105  sc_assert(hnd.valid() && thread);
106
107  sc_assert(thread->file == NULL);  // 1st wait(delay)
108  sc_assert(thread->lineno == 0);
109  sc_core::sc_start(sc_time(15, SC_US));
110
111  int lineno = first_line;
112  sc_assert(endsWith(thread->file, "wait.cpp"));  // SC_WAITN
113  sc_assert(thread->lineno == lineno);
114  sc_core::sc_start(sc_time(10, SC_US));
115  sc_assert(thread->file == NULL);  // 2nd wait(delay)
116  sc_assert(thread->lineno == 0);
117  sc_core::sc_start(sc_time(10, SC_US));
118
119  lineno += 3;
120  sc_assert(endsWith(thread->file, "wait.cpp"));  // SC_WAIT
121  sc_assert(thread->lineno == lineno);
122  sc_core::sc_start(sc_time(70, SC_US));
123  sc_assert(thread->file == NULL);  // 3rd wait(delay)
124  sc_assert(thread->lineno == 0);
125  sc_core::sc_start(sc_time(10, SC_US));
126
127  lineno += 3;
128  sc_assert(endsWith(thread->file, "wait.cpp"));  // SC_WAIT_UNTIL
129  sc_assert(thread->lineno == lineno);
130  sc_core::sc_start(sc_time(900, SC_US));
131  sc_assert(thread->file == NULL);  // 4th wait(delay)
132  sc_assert(thread->lineno == 0);
133
134  // Ensure that SC_WAIT_UNTIL can handle nested wait calls
135  sc_core::sc_start(sc_time(10, SC_US));
136  lineno += 3;
137  sc_assert(endsWith(thread->file, "wait.cpp"));  // 2nd SC_WAIT_UNTIL
138  sc_assert(thread->lineno == lineno);
139  sc_core::sc_start(sc_time(980, SC_US));  // time should be 2005
140  sc_assert(endsWith(thread->file, "wait.cpp"));  // SC_WAITN in waitFunIsTrue
141  sc_assert(thread->lineno == 60);
142  sc_core::sc_start(sc_time(95, SC_US));
143  sc_assert(endsWith(thread->file, "wait.cpp"));
144  sc_assert(thread->lineno == 60);
145  sc_core::sc_start(sc_time(910, SC_US));
146  sc_assert(endsWith(thread->file, "wait.cpp"));  // 2nd SC_WAIT_UNTIL
147  sc_assert(thread->lineno == lineno);
148  sc_core::sc_start(sc_time(1000, SC_US));
149  sc_assert(thread->file == NULL);  // done
150  sc_assert(thread->lineno == 0);
151
152  return 0;
153}
154