112922Sgabeblack@google.com/*****************************************************************************
212922Sgabeblack@google.com
312922Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412922Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512922Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612922Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712922Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812922Sgabeblack@google.com  License.  You may obtain a copy of the License at
912922Sgabeblack@google.com
1012922Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112922Sgabeblack@google.com
1212922Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312922Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412922Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512922Sgabeblack@google.com  implied.  See the License for the specific language governing
1612922Sgabeblack@google.com  permissions and limitations under the License.
1712922Sgabeblack@google.com
1812922Sgabeblack@google.com *****************************************************************************/
1912922Sgabeblack@google.com
2012922Sgabeblack@google.com//====================================================================
2112922Sgabeblack@google.com//  Nov 06, 2008
2212922Sgabeblack@google.com//
2312922Sgabeblack@google.com//  Updated by:
2412922Sgabeblack@google.com//    Xiaopeng Qiu, JEDA Technologies, Inc
2512922Sgabeblack@google.com//    Email:  qiuxp@jedatechnologies.net
2612922Sgabeblack@google.com//
2712922Sgabeblack@google.com//  To fix violations of TLM2.0 rules, which are detected by JEDA
2812922Sgabeblack@google.com//  TLM2.0 checker.
2912922Sgabeblack@google.com//
3012922Sgabeblack@google.com//====================================================================
3112922Sgabeblack@google.com
3212922Sgabeblack@google.com#ifndef __CORE_DECOUPLING_LT_INITIATOR_H__
3312922Sgabeblack@google.com#define __CORE_DECOUPLING_LT_INITIATOR_H__
3412922Sgabeblack@google.com
3512922Sgabeblack@google.com#include "tlm.h"
3612922Sgabeblack@google.com#include "tlm_utils/simple_initiator_socket.h"
3712922Sgabeblack@google.com#include "tlm_utils/tlm_quantumkeeper.h"
3812922Sgabeblack@google.com//#include <systemc>
3912922Sgabeblack@google.com#include <cassert>
4012922Sgabeblack@google.com//#include <iostream>
4112922Sgabeblack@google.com
4212922Sgabeblack@google.comclass CoreDecouplingLTInitiator : public sc_core::sc_module
4312922Sgabeblack@google.com{
4412922Sgabeblack@google.compublic:
4512922Sgabeblack@google.com  typedef tlm::tlm_generic_payload                         transaction_type;
4612922Sgabeblack@google.com  typedef tlm_utils::simple_initiator_socket<CoreDecouplingLTInitiator> initiator_socket_type;
4712922Sgabeblack@google.com
4812922Sgabeblack@google.compublic:
4912922Sgabeblack@google.com  initiator_socket_type socket;
5012922Sgabeblack@google.com
5112922Sgabeblack@google.compublic:
5212922Sgabeblack@google.com  SC_HAS_PROCESS(CoreDecouplingLTInitiator);
5312922Sgabeblack@google.com  CoreDecouplingLTInitiator(sc_core::sc_module_name name,
5412922Sgabeblack@google.com                            unsigned int nrOfTransactions = 0x5,
5512922Sgabeblack@google.com                            unsigned int baseAddress = 0) :
5612922Sgabeblack@google.com    sc_core::sc_module(name),
5712922Sgabeblack@google.com    socket("socket"),
5812922Sgabeblack@google.com    mNrOfTransactions(nrOfTransactions),
5912922Sgabeblack@google.com    mBaseAddress(baseAddress),
6012922Sgabeblack@google.com    mTransactionCount(0)
6112922Sgabeblack@google.com  {
6212922Sgabeblack@google.com    tlm_utils::tlm_quantumkeeper::set_global_quantum(sc_core::sc_time(500, sc_core::SC_NS));
6312922Sgabeblack@google.com    mQuantumKeeper.reset();
6412922Sgabeblack@google.com
6512922Sgabeblack@google.com    // Initiator thread
6612922Sgabeblack@google.com    SC_THREAD(run);
6712922Sgabeblack@google.com  }
6812922Sgabeblack@google.com
6912922Sgabeblack@google.com  bool initTransaction(transaction_type& trans)
7012922Sgabeblack@google.com  {
7112922Sgabeblack@google.com    if (mTransactionCount < mNrOfTransactions) {
7212922Sgabeblack@google.com      trans.set_address(mBaseAddress + 4*mTransactionCount);
7312922Sgabeblack@google.com      mData = mTransactionCount;
7412922Sgabeblack@google.com      trans.set_command(tlm::TLM_WRITE_COMMAND);
7512922Sgabeblack@google.com
7612922Sgabeblack@google.com    } else if (mTransactionCount < 2 * mNrOfTransactions) {
7712922Sgabeblack@google.com      trans.set_address(mBaseAddress + 4*(mTransactionCount - mNrOfTransactions));
7812922Sgabeblack@google.com      mData = 0;
7912922Sgabeblack@google.com      trans.set_command(tlm::TLM_READ_COMMAND);
8012922Sgabeblack@google.com
8112922Sgabeblack@google.com    } else {
8212922Sgabeblack@google.com      return false;
8312922Sgabeblack@google.com    }
8412922Sgabeblack@google.com
8512922Sgabeblack@google.com    trans.set_data_ptr(reinterpret_cast<unsigned char*>(&mData));
8612922Sgabeblack@google.com    trans.set_data_length(4);
8712922Sgabeblack@google.com    trans.set_streaming_width(4);
8812922Sgabeblack@google.com    trans.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
8912922Sgabeblack@google.com
9012922Sgabeblack@google.com    ++mTransactionCount;
9112922Sgabeblack@google.com    return true;
9212922Sgabeblack@google.com  }
9312922Sgabeblack@google.com
9412922Sgabeblack@google.com  void logStartTransation(transaction_type& trans)
9512922Sgabeblack@google.com  {
9612922Sgabeblack@google.com    if (trans.get_command() == tlm::TLM_WRITE_COMMAND) {
9712922Sgabeblack@google.com      std::cout << name() << ": Send write request: A = 0x"
9812922Sgabeblack@google.com                << std::hex << (unsigned int)trans.get_address()
9912922Sgabeblack@google.com                << ", D = 0x" << mData << std::dec
10012922Sgabeblack@google.com                << " @ " << mQuantumKeeper.get_current_time()
10112922Sgabeblack@google.com                << " (" << sc_core::sc_time_stamp() << " + "
10212922Sgabeblack@google.com                << mQuantumKeeper.get_local_time() << ")"
10312922Sgabeblack@google.com                << std::endl;
10412922Sgabeblack@google.com
10512922Sgabeblack@google.com    } else {
10612922Sgabeblack@google.com      std::cout << name() << ": Send read request: A = 0x"
10712922Sgabeblack@google.com                << std::hex << (unsigned int)trans.get_address()
10812922Sgabeblack@google.com                << " @ " << mQuantumKeeper.get_current_time()
10912922Sgabeblack@google.com                << " (" << sc_core::sc_time_stamp() << " + "
11012922Sgabeblack@google.com                << mQuantumKeeper.get_local_time() << ")"
11112922Sgabeblack@google.com                << std::endl;
11212922Sgabeblack@google.com    }
11312922Sgabeblack@google.com  }
11412922Sgabeblack@google.com
11512922Sgabeblack@google.com  void logEndTransaction(transaction_type& trans)
11612922Sgabeblack@google.com  {
11712922Sgabeblack@google.com    if (trans.get_response_status() != tlm::TLM_OK_RESPONSE) {
11812922Sgabeblack@google.com      std::cout << name() << ": Received error response @ "
11912922Sgabeblack@google.com                << mQuantumKeeper.get_current_time()
12012922Sgabeblack@google.com                << " (" << sc_core::sc_time_stamp() << " + "
12112922Sgabeblack@google.com                << mQuantumKeeper.get_local_time() << ")"
12212922Sgabeblack@google.com                << std::endl;
12312922Sgabeblack@google.com
12412922Sgabeblack@google.com    } else {
12512922Sgabeblack@google.com      std::cout << name() <<  ": Received ok response";
12612922Sgabeblack@google.com      if (trans.get_command() == tlm::TLM_READ_COMMAND) {
12712922Sgabeblack@google.com          std::cout << ": D = 0x" << std::hex << mData << std::dec;
12812922Sgabeblack@google.com      }
12912922Sgabeblack@google.com      std::cout << " @ " << mQuantumKeeper.get_current_time()
13012922Sgabeblack@google.com                << " (" << sc_core::sc_time_stamp() << " + "
13112922Sgabeblack@google.com                << mQuantumKeeper.get_local_time() << ")"
13212922Sgabeblack@google.com                << std::endl;
13312922Sgabeblack@google.com    }
13412922Sgabeblack@google.com  }
13512922Sgabeblack@google.com
13612922Sgabeblack@google.com  void run()
13712922Sgabeblack@google.com  {
13812922Sgabeblack@google.com    transaction_type trans;
13912922Sgabeblack@google.com
14012922Sgabeblack@google.com    while (initTransaction(trans)) {
14112922Sgabeblack@google.com      logStartTransation(trans);
14212922Sgabeblack@google.com
14312922Sgabeblack@google.com      // exec instr
14412922Sgabeblack@google.com      sc_core::sc_time t = mQuantumKeeper.get_local_time();
14512922Sgabeblack@google.com      socket->b_transport(trans, t);
14612922Sgabeblack@google.com      mQuantumKeeper.set(t);
14712922Sgabeblack@google.com      // Target may have added a delay to the quantum -> sync if needed
14812922Sgabeblack@google.com      if (mQuantumKeeper.need_sync()) {
14912922Sgabeblack@google.com        std::cout << "Sync'ing..." << std::endl;
15012922Sgabeblack@google.com        mQuantumKeeper.sync();
15112922Sgabeblack@google.com      }
15212922Sgabeblack@google.com
15312922Sgabeblack@google.com      logEndTransaction(trans);
15412922Sgabeblack@google.com    }
15512922Sgabeblack@google.com    wait();
15612922Sgabeblack@google.com  }
15712922Sgabeblack@google.com
15812922Sgabeblack@google.comprivate:
15912922Sgabeblack@google.com  unsigned int mNrOfTransactions;
16012922Sgabeblack@google.com  unsigned int mBaseAddress;
16112922Sgabeblack@google.com  unsigned int mTransactionCount;
16212922Sgabeblack@google.com  unsigned int mData;
16312922Sgabeblack@google.com  tlm_utils::tlm_quantumkeeper mQuantumKeeper;
16412922Sgabeblack@google.com};
16512922Sgabeblack@google.com
16612922Sgabeblack@google.com#endif
167