wait.cpp revision 12855
112855Sgabeblack@google.com/***************************************************************************** 212855Sgabeblack@google.com 312855Sgabeblack@google.com Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 412855Sgabeblack@google.com more contributor license agreements. See the NOTICE file distributed 512855Sgabeblack@google.com with this work for additional information regarding copyright ownership. 612855Sgabeblack@google.com Accellera licenses this file to you under the Apache License, Version 2.0 712855Sgabeblack@google.com (the "License"); you may not use this file except in compliance with the 812855Sgabeblack@google.com License. You may obtain a copy of the License at 912855Sgabeblack@google.com 1012855Sgabeblack@google.com http://www.apache.org/licenses/LICENSE-2.0 1112855Sgabeblack@google.com 1212855Sgabeblack@google.com Unless required by applicable law or agreed to in writing, software 1312855Sgabeblack@google.com distributed under the License is distributed on an "AS IS" BASIS, 1412855Sgabeblack@google.com WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 1512855Sgabeblack@google.com implied. See the License for the specific language governing 1612855Sgabeblack@google.com permissions and limitations under the License. 1712855Sgabeblack@google.com 1812855Sgabeblack@google.com *****************************************************************************/ 1912855Sgabeblack@google.com 2012855Sgabeblack@google.com/***************************************************************************** 2112855Sgabeblack@google.com 2212855Sgabeblack@google.com wait.cpp -- 2312855Sgabeblack@google.com 2412855Sgabeblack@google.com Original Author: Daniel Aarno, Intel, Corp 2015-07-23 2512855Sgabeblack@google.com 2612855Sgabeblack@google.com *****************************************************************************/ 2712855Sgabeblack@google.com 2812855Sgabeblack@google.com/***************************************************************************** 2912855Sgabeblack@google.com 3012855Sgabeblack@google.com MODIFICATION LOG - modifiers, enter your name, affiliation, date and 3112855Sgabeblack@google.com changes you are making here. 3212855Sgabeblack@google.com 3312855Sgabeblack@google.com Name, Affiliation, Date: 3412855Sgabeblack@google.com Description of Modification: 3512855Sgabeblack@google.com 3612855Sgabeblack@google.com *****************************************************************************/ 3712855Sgabeblack@google.com 3812855Sgabeblack@google.com#include <systemc> 3912855Sgabeblack@google.com 4012855Sgabeblack@google.comusing sc_core::sc_time; 4112855Sgabeblack@google.comusing sc_core::SC_US; 4212855Sgabeblack@google.com 4312855Sgabeblack@google.comnamespace { 4412855Sgabeblack@google.comint first_line = 0; 4512855Sgabeblack@google.com 4612855Sgabeblack@google.combool endsWith(const std::string &str, const std::string &ending) { 4712855Sgabeblack@google.com if (ending.size() > str.size()) { 4812855Sgabeblack@google.com return false; 4912855Sgabeblack@google.com } 5012855Sgabeblack@google.com return std::equal(ending.rbegin(), ending.rend(), str.rbegin()); 5112855Sgabeblack@google.com} 5212855Sgabeblack@google.com 5312855Sgabeblack@google.combool waitFunIsTrue() { 5412855Sgabeblack@google.com if (sc_core::sc_time_stamp() < sc_time(2000, SC_US)) { 5512855Sgabeblack@google.com return false; 5612855Sgabeblack@google.com } 5712855Sgabeblack@google.com 5812855Sgabeblack@google.com if (sc_core::sc_time_stamp() < sc_time(3000, SC_US)) { 5912855Sgabeblack@google.com sc_time delay(10, SC_US); 6012855Sgabeblack@google.com SC_WAITN(delay); 6112855Sgabeblack@google.com return false; 6212855Sgabeblack@google.com } 6312855Sgabeblack@google.com 6412855Sgabeblack@google.com if (sc_core::sc_time_stamp() < sc_time(4000, SC_US)) { 6512855Sgabeblack@google.com return false; 6612855Sgabeblack@google.com } 6712855Sgabeblack@google.com 6812855Sgabeblack@google.com return true; 6912855Sgabeblack@google.com} 7012855Sgabeblack@google.com 7112855Sgabeblack@google.comSC_MODULE(Wait) { 7212855Sgabeblack@google.com SC_CTOR(Wait) : clk( "clk", 10, SC_US, 0.5, 100, SC_US) { 7312855Sgabeblack@google.com SC_THREAD(thread); 7412855Sgabeblack@google.com sensitive << clk; 7512855Sgabeblack@google.com } 7612855Sgabeblack@google.com 7712855Sgabeblack@google.com void thread() { 7812855Sgabeblack@google.com sc_time delay(10, SC_US); 7912855Sgabeblack@google.com wait(delay); // 1st 8012855Sgabeblack@google.com 8112855Sgabeblack@google.com first_line = __LINE__ + 1; 8212855Sgabeblack@google.com SC_WAITN(delay); // Wait some time 8312855Sgabeblack@google.com wait(delay); // 2nd 8412855Sgabeblack@google.com 8512855Sgabeblack@google.com SC_WAIT(); // Wait for clk 8612855Sgabeblack@google.com wait(delay); // 3rd 8712855Sgabeblack@google.com 8812855Sgabeblack@google.com SC_WAIT_UNTIL(sc_core::sc_time_stamp() > sc_time(1000, SC_US)); 8912855Sgabeblack@google.com wait(delay); // 4th 9012855Sgabeblack@google.com 9112855Sgabeblack@google.com SC_WAIT_UNTIL(waitFunIsTrue()); 9212855Sgabeblack@google.com } 9312855Sgabeblack@google.com 9412855Sgabeblack@google.com sc_core::sc_clock clk; 9512855Sgabeblack@google.com}; 9612855Sgabeblack@google.com 9712855Sgabeblack@google.com} // namespace 9812855Sgabeblack@google.com 9912855Sgabeblack@google.comint sc_main(int argc, char ** argv) { 10012855Sgabeblack@google.com Wait w("dut"); 10112855Sgabeblack@google.com 10212855Sgabeblack@google.com sc_core::sc_process_handle hnd( sc_core::sc_find_object("dut.thread") ); 10312855Sgabeblack@google.com const sc_core::sc_process_b *thread 10412855Sgabeblack@google.com = dynamic_cast<sc_core::sc_process_b*>(hnd.get_process_object()); 10512855Sgabeblack@google.com sc_assert(hnd.valid() && thread); 10612855Sgabeblack@google.com 10712855Sgabeblack@google.com sc_assert(thread->file == NULL); // 1st wait(delay) 10812855Sgabeblack@google.com sc_assert(thread->lineno == 0); 10912855Sgabeblack@google.com sc_core::sc_start(sc_time(15, SC_US)); 11012855Sgabeblack@google.com 11112855Sgabeblack@google.com int lineno = first_line; 11212855Sgabeblack@google.com sc_assert(endsWith(thread->file, "wait.cpp")); // SC_WAITN 11312855Sgabeblack@google.com sc_assert(thread->lineno == lineno); 11412855Sgabeblack@google.com sc_core::sc_start(sc_time(10, SC_US)); 11512855Sgabeblack@google.com sc_assert(thread->file == NULL); // 2nd wait(delay) 11612855Sgabeblack@google.com sc_assert(thread->lineno == 0); 11712855Sgabeblack@google.com sc_core::sc_start(sc_time(10, SC_US)); 11812855Sgabeblack@google.com 11912855Sgabeblack@google.com lineno += 3; 12012855Sgabeblack@google.com sc_assert(endsWith(thread->file, "wait.cpp")); // SC_WAIT 12112855Sgabeblack@google.com sc_assert(thread->lineno == lineno); 12212855Sgabeblack@google.com sc_core::sc_start(sc_time(70, SC_US)); 12312855Sgabeblack@google.com sc_assert(thread->file == NULL); // 3rd wait(delay) 12412855Sgabeblack@google.com sc_assert(thread->lineno == 0); 12512855Sgabeblack@google.com sc_core::sc_start(sc_time(10, SC_US)); 12612855Sgabeblack@google.com 12712855Sgabeblack@google.com lineno += 3; 12812855Sgabeblack@google.com sc_assert(endsWith(thread->file, "wait.cpp")); // SC_WAIT_UNTIL 12912855Sgabeblack@google.com sc_assert(thread->lineno == lineno); 13012855Sgabeblack@google.com sc_core::sc_start(sc_time(900, SC_US)); 13112855Sgabeblack@google.com sc_assert(thread->file == NULL); // 4th wait(delay) 13212855Sgabeblack@google.com sc_assert(thread->lineno == 0); 13312855Sgabeblack@google.com 13412855Sgabeblack@google.com // Ensure that SC_WAIT_UNTIL can handle nested wait calls 13512855Sgabeblack@google.com sc_core::sc_start(sc_time(10, SC_US)); 13612855Sgabeblack@google.com lineno += 3; 13712855Sgabeblack@google.com sc_assert(endsWith(thread->file, "wait.cpp")); // 2nd SC_WAIT_UNTIL 13812855Sgabeblack@google.com sc_assert(thread->lineno == lineno); 13912855Sgabeblack@google.com sc_core::sc_start(sc_time(980, SC_US)); // time should be 2005 14012855Sgabeblack@google.com sc_assert(endsWith(thread->file, "wait.cpp")); // SC_WAITN in waitFunIsTrue 14112855Sgabeblack@google.com sc_assert(thread->lineno == 60); 14212855Sgabeblack@google.com sc_core::sc_start(sc_time(95, SC_US)); 14312855Sgabeblack@google.com sc_assert(endsWith(thread->file, "wait.cpp")); 14412855Sgabeblack@google.com sc_assert(thread->lineno == 60); 14512855Sgabeblack@google.com sc_core::sc_start(sc_time(910, SC_US)); 14612855Sgabeblack@google.com sc_assert(endsWith(thread->file, "wait.cpp")); // 2nd SC_WAIT_UNTIL 14712855Sgabeblack@google.com sc_assert(thread->lineno == lineno); 14812855Sgabeblack@google.com sc_core::sc_start(sc_time(1000, SC_US)); 14912855Sgabeblack@google.com sc_assert(thread->file == NULL); // done 15012855Sgabeblack@google.com sc_assert(thread->lineno == 0); 15112855Sgabeblack@google.com 15212855Sgabeblack@google.com return 0; 15312855Sgabeblack@google.com} 154