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// test01.cpp -- test self suspends on processes 2312855Sgabeblack@google.com// 2412855Sgabeblack@google.com// Original Author: Andy Goodrich, Forte Design Systems, Inc. 2512855Sgabeblack@google.com// 2612855Sgabeblack@google.com// CVS MODIFICATION LOG - modifiers, enter your name, affiliation, date and 2712855Sgabeblack@google.com// changes you are making here. 2812855Sgabeblack@google.com// 2912855Sgabeblack@google.com// $Log: test1.cpp,v $ 3012855Sgabeblack@google.com// Revision 1.4 2011/04/02 00:07:44 acg 3112855Sgabeblack@google.com// Andy Goodrich: new message format. 3212855Sgabeblack@google.com// 3312855Sgabeblack@google.com// Revision 1.3 2011/03/07 19:32:07 acg 3412855Sgabeblack@google.com// Andy Goodrich: addition to set sc_core::sc_allow_process_control_corners 3512855Sgabeblack@google.com// to true so that this test avoids corner case error messages. 3612855Sgabeblack@google.com// 3712855Sgabeblack@google.com// Revision 1.2 2009/07/28 18:43:50 acg 3812855Sgabeblack@google.com// Andy Goodrich: new standard test bench version of this test. 3912855Sgabeblack@google.com// 4012855Sgabeblack@google.com// Revision 1.2 2009/07/28 01:09:48 acg 4112855Sgabeblack@google.com// Andy Goodrich: replacement test using standardized environment. 4212855Sgabeblack@google.com// 4312855Sgabeblack@google.com//***************************************************************************** 4412855Sgabeblack@google.com 4512855Sgabeblack@google.com#define SC_INCLUDE_DYNAMIC_PROCESSES 4612855Sgabeblack@google.com#include "systemc.h" 4712855Sgabeblack@google.com 4812855Sgabeblack@google.comenum my_process_states { 4912855Sgabeblack@google.com ST_DISABLED, 5012855Sgabeblack@google.com ST_NORMAL, 5112855Sgabeblack@google.com ST_SUSPENDED 5212855Sgabeblack@google.com}; 5312855Sgabeblack@google.com 5412855Sgabeblack@google.cominline ostream& time_stamp( ostream& os ) 5512855Sgabeblack@google.com{ 5612855Sgabeblack@google.com os << dec << sc_time_stamp() << "[" << sc_delta_count() << "]: "; 5712855Sgabeblack@google.com return os; 5812855Sgabeblack@google.com} 5912855Sgabeblack@google.com 6012855Sgabeblack@google.comSC_MODULE(top) { 6112855Sgabeblack@google.com // constructor: 6212855Sgabeblack@google.com 6312855Sgabeblack@google.com SC_CTOR(top) : 6412855Sgabeblack@google.com m_state_cthread0(ST_NORMAL), 6512855Sgabeblack@google.com m_state_method0(ST_NORMAL), 6612855Sgabeblack@google.com m_state_thread0(ST_NORMAL) 6712855Sgabeblack@google.com { 6812855Sgabeblack@google.com SC_THREAD(stimulator0); 6912855Sgabeblack@google.com 7012855Sgabeblack@google.com SC_CTHREAD( target_cthread0, m_clk.pos() ); 7112855Sgabeblack@google.com m_target_cthread0 = sc_get_current_process_handle(); 7212855Sgabeblack@google.com 7312855Sgabeblack@google.com SC_METHOD(target_method0); 7412855Sgabeblack@google.com sensitive << m_clk.pos(); 7512855Sgabeblack@google.com m_target_method0 = sc_get_current_process_handle(); 7612855Sgabeblack@google.com 7712855Sgabeblack@google.com SC_THREAD(target_thread0); 7812855Sgabeblack@google.com m_target_thread0 = sc_get_current_process_handle(); 7912855Sgabeblack@google.com } 8012855Sgabeblack@google.com 8112855Sgabeblack@google.com // processes: 8212855Sgabeblack@google.com 8312855Sgabeblack@google.com void stimulator0(); 8412855Sgabeblack@google.com void target_cthread0(); 8512855Sgabeblack@google.com void target_method0(); 8612855Sgabeblack@google.com void target_thread0(); 8712855Sgabeblack@google.com 8812855Sgabeblack@google.com // Storage: 8912855Sgabeblack@google.com 9012855Sgabeblack@google.com sc_in<bool> m_clk; 9112855Sgabeblack@google.com int m_state_cthread0; 9212855Sgabeblack@google.com int m_state_method0; 9312855Sgabeblack@google.com int m_state_thread0; 9412855Sgabeblack@google.com sc_process_handle m_target_cthread0; 9512855Sgabeblack@google.com sc_process_handle m_target_method0; 9612855Sgabeblack@google.com sc_process_handle m_target_thread0; 9712855Sgabeblack@google.com}; 9812855Sgabeblack@google.com 9912855Sgabeblack@google.comvoid top::stimulator0() 10012855Sgabeblack@google.com{ 10112855Sgabeblack@google.com const char* name = "stimulator"; 10212855Sgabeblack@google.com wait(10, SC_NS); 10312855Sgabeblack@google.com cout << endl; 10412855Sgabeblack@google.com time_stamp(cout) << name << ": resuming target_cthread0" << endl; 10512855Sgabeblack@google.com cout << endl; 10612855Sgabeblack@google.com m_state_cthread0 = ST_NORMAL; 10712855Sgabeblack@google.com m_target_cthread0.resume(); 10812855Sgabeblack@google.com wait(10, SC_NS); 10912855Sgabeblack@google.com 11012855Sgabeblack@google.com cout << endl; 11112855Sgabeblack@google.com time_stamp(cout) << name << ": resuming target_method0" << endl; 11212855Sgabeblack@google.com cout << endl; 11312855Sgabeblack@google.com m_state_method0 = ST_NORMAL; 11412855Sgabeblack@google.com m_target_method0.resume(); 11512855Sgabeblack@google.com wait(10, SC_NS); 11612855Sgabeblack@google.com 11712855Sgabeblack@google.com cout << endl; 11812855Sgabeblack@google.com time_stamp(cout) << name << ": resuming target_thread0" << endl; 11912855Sgabeblack@google.com cout << endl; 12012855Sgabeblack@google.com m_state_thread0 = ST_NORMAL; 12112855Sgabeblack@google.com m_target_thread0.resume(); 12212855Sgabeblack@google.com ::sc_core::wait(1000, SC_NS); 12312855Sgabeblack@google.com 12412855Sgabeblack@google.com cout << endl; 12512855Sgabeblack@google.com time_stamp(cout) << name << ": terminating" << endl; 12612855Sgabeblack@google.com sc_stop(); 12712855Sgabeblack@google.com} 12812855Sgabeblack@google.com 12912855Sgabeblack@google.comvoid top::target_cthread0() 13012855Sgabeblack@google.com{ 13112855Sgabeblack@google.com int i; 13212855Sgabeblack@google.com const char* name = "target_cthread0"; 13312855Sgabeblack@google.com 13412855Sgabeblack@google.com time_stamp(cout) << name << ": starting" << endl; 13512855Sgabeblack@google.com time_stamp(cout) << name << ": issuing self suspend" << endl; 13612855Sgabeblack@google.com cout << endl; 13712855Sgabeblack@google.com m_state_cthread0 = ST_SUSPENDED; 13812855Sgabeblack@google.com m_target_cthread0.suspend(); 13912855Sgabeblack@google.com time_stamp(cout) << name << ": back from self suspend" << endl; 14012855Sgabeblack@google.com for ( i = 0; i < 100; i++ ) 14112855Sgabeblack@google.com { 14212855Sgabeblack@google.com if ( m_state_cthread0 == ST_SUSPENDED ) 14312855Sgabeblack@google.com { 14412855Sgabeblack@google.com time_stamp(cout) << name << ": ERROR should not see this" << endl; 14512855Sgabeblack@google.com } 14612855Sgabeblack@google.com wait(); 14712855Sgabeblack@google.com } 14812855Sgabeblack@google.com time_stamp(cout) << name << ": terminating" << endl; 14912855Sgabeblack@google.com} 15012855Sgabeblack@google.com 15112855Sgabeblack@google.comvoid top::target_method0() 15212855Sgabeblack@google.com{ 15312855Sgabeblack@google.com const char* name = "target_method0"; 15412855Sgabeblack@google.com static int state = 0; 15512855Sgabeblack@google.com switch( state ) 15612855Sgabeblack@google.com { 15712855Sgabeblack@google.com case 0: 15812855Sgabeblack@google.com time_stamp(cout) << name << ": starting" << endl; 15912855Sgabeblack@google.com time_stamp(cout) << name << ": issuing self suspend" << endl; 16012855Sgabeblack@google.com m_state_method0 = ST_SUSPENDED; 16112855Sgabeblack@google.com m_target_method0.suspend(); 16212855Sgabeblack@google.com time_stamp(cout) << name << ": after issuing self suspend" << endl; 16312855Sgabeblack@google.com cout << endl; 16412855Sgabeblack@google.com break; 16512855Sgabeblack@google.com case 1: 16612855Sgabeblack@google.com time_stamp(cout) << name << ": back from self suspend" << endl; 16712855Sgabeblack@google.com // fall through 16812855Sgabeblack@google.com default: 16912855Sgabeblack@google.com if ( m_state_method0 == ST_SUSPENDED ) 17012855Sgabeblack@google.com { 17112855Sgabeblack@google.com time_stamp(cout) << name << ": ERROR should not see this" << endl; 17212855Sgabeblack@google.com } 17312855Sgabeblack@google.com break; 17412855Sgabeblack@google.com case 99: 17512855Sgabeblack@google.com time_stamp(cout) << name << ": terminating" << endl; 17612855Sgabeblack@google.com break; 17712855Sgabeblack@google.com } 17812855Sgabeblack@google.com state++; 17912855Sgabeblack@google.com} 18012855Sgabeblack@google.com 18112855Sgabeblack@google.comvoid top::target_thread0() 18212855Sgabeblack@google.com{ 18312855Sgabeblack@google.com const char* name = "target_thread0"; 18412855Sgabeblack@google.com 18512855Sgabeblack@google.com time_stamp(cout) << name << ": starting" << endl; 18612855Sgabeblack@google.com time_stamp(cout) << name << ": issuing self suspend" << endl; 18712855Sgabeblack@google.com cout << endl; 18812855Sgabeblack@google.com m_state_thread0 = ST_SUSPENDED; 18912855Sgabeblack@google.com m_target_thread0.suspend(); 19012855Sgabeblack@google.com time_stamp(cout) << name << ": back from self suspend" << endl; 19112855Sgabeblack@google.com 19212855Sgabeblack@google.com // We wait a long enough time that our event will not occur until 19312855Sgabeblack@google.com // after we are resumed. Otherwise this thread will just go away 19412855Sgabeblack@google.com // quietly when the suspend cancels the event. 19512855Sgabeblack@google.com 19612855Sgabeblack@google.com ::sc_core::wait(80, SC_NS); 19712855Sgabeblack@google.com if ( m_state_thread0 == ST_SUSPENDED ) 19812855Sgabeblack@google.com { 19912855Sgabeblack@google.com time_stamp(cout) << name << ": ERROR should not see this" << endl; 20012855Sgabeblack@google.com } 20112855Sgabeblack@google.com time_stamp(cout) << name << ": terminating" << endl; 20212855Sgabeblack@google.com} 20312855Sgabeblack@google.com 20412855Sgabeblack@google.comint sc_main (int argc, char *argv[]) 20512855Sgabeblack@google.com{ 20612855Sgabeblack@google.com sc_core::sc_allow_process_control_corners = true; 20712855Sgabeblack@google.com sc_clock clock( "clock", 1.0, SC_NS ); 20812855Sgabeblack@google.com 20912855Sgabeblack@google.com top* top_p = new top("top"); 21012855Sgabeblack@google.com top_p->m_clk(clock); 21112855Sgabeblack@google.com 21212855Sgabeblack@google.com sc_core::sc_allow_process_control_corners = true; 21312855Sgabeblack@google.com sc_start(); 21412855Sgabeblack@google.com return 0; 21512855Sgabeblack@google.com} 21612855Sgabeblack@google.com 217