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 __SIMPLE_LT_INITIATOR2_DMI_H__
3312922Sgabeblack@google.com#define __SIMPLE_LT_INITIATOR2_DMI_H__
3412922Sgabeblack@google.com
3512922Sgabeblack@google.com#include "tlm.h"
3612922Sgabeblack@google.com#include "tlm_utils/simple_initiator_socket.h"
3712922Sgabeblack@google.com#include <systemc>
3812922Sgabeblack@google.com#include <cassert>
3912922Sgabeblack@google.com#include <iostream>
4012922Sgabeblack@google.com#include <iomanip>
4112922Sgabeblack@google.com#include <map>
4212922Sgabeblack@google.com
4312922Sgabeblack@google.comclass SimpleLTInitiator2_dmi : public sc_core::sc_module
4412922Sgabeblack@google.com{
4512922Sgabeblack@google.compublic:
4612922Sgabeblack@google.com  typedef tlm::tlm_generic_payload                      transaction_type;
4712922Sgabeblack@google.com  typedef tlm::tlm_dmi                                  dmi_type;
4812922Sgabeblack@google.com  typedef tlm::tlm_phase                                phase_type;
4912922Sgabeblack@google.com  typedef tlm::tlm_sync_enum                            sync_enum_type;
5012922Sgabeblack@google.com  typedef tlm_utils::simple_initiator_socket<SimpleLTInitiator2_dmi> initiator_socket_type;
5112922Sgabeblack@google.com
5212922Sgabeblack@google.compublic:
5312922Sgabeblack@google.com  initiator_socket_type socket;
5412922Sgabeblack@google.com
5512922Sgabeblack@google.compublic:
5612922Sgabeblack@google.com  SC_HAS_PROCESS(SimpleLTInitiator2_dmi);
5712922Sgabeblack@google.com  SimpleLTInitiator2_dmi(sc_core::sc_module_name name,
5812922Sgabeblack@google.com                  unsigned int nrOfTransactions = 0x5,
5912922Sgabeblack@google.com                  unsigned int baseAddress = 0x0) :
6012922Sgabeblack@google.com    sc_core::sc_module(name),
6112922Sgabeblack@google.com    socket("socket"),
6212922Sgabeblack@google.com    mNrOfTransactions(nrOfTransactions),
6312922Sgabeblack@google.com    mBaseAddress(baseAddress),
6412922Sgabeblack@google.com    mTransactionCount(0)
6512922Sgabeblack@google.com  {
6612922Sgabeblack@google.com    mDMIDataReads.first.set_start_address(1);
6712922Sgabeblack@google.com    mDMIDataReads.first.set_end_address(0);
6812922Sgabeblack@google.com    mDMIDataWrites.first.set_start_address(1);
6912922Sgabeblack@google.com    mDMIDataWrites.first.set_end_address(0);
7012922Sgabeblack@google.com
7112922Sgabeblack@google.com    // register invalidate method
7212922Sgabeblack@google.com    socket.register_invalidate_direct_mem_ptr(this, &SimpleLTInitiator2_dmi::invalidate_direct_mem_ptr);
7312922Sgabeblack@google.com
7412922Sgabeblack@google.com    // Initiator thread
7512922Sgabeblack@google.com    SC_THREAD(run);
7612922Sgabeblack@google.com  }
7712922Sgabeblack@google.com
7812922Sgabeblack@google.com  bool initTransaction(transaction_type& trans)
7912922Sgabeblack@google.com  {
8012922Sgabeblack@google.com    if (mTransactionCount < mNrOfTransactions) {
8112922Sgabeblack@google.com      trans.set_address(mBaseAddress + 4*mTransactionCount);
8212922Sgabeblack@google.com      mData = mTransactionCount;
8312922Sgabeblack@google.com      trans.set_command(tlm::TLM_WRITE_COMMAND);
8412922Sgabeblack@google.com
8512922Sgabeblack@google.com    } else if (mTransactionCount < 2 * mNrOfTransactions) {
8612922Sgabeblack@google.com      trans.set_address(mBaseAddress + 4*(mTransactionCount-mNrOfTransactions));
8712922Sgabeblack@google.com      mData = 0;
8812922Sgabeblack@google.com      trans.set_command(tlm::TLM_READ_COMMAND);
8912922Sgabeblack@google.com
9012922Sgabeblack@google.com    } else {
9112922Sgabeblack@google.com      return false;
9212922Sgabeblack@google.com    }
9312922Sgabeblack@google.com
9412922Sgabeblack@google.com    trans.set_data_ptr(reinterpret_cast<unsigned char*>(&mData));
9512922Sgabeblack@google.com    trans.set_data_length(4);
9612922Sgabeblack@google.com    trans.set_streaming_width(4);
9712922Sgabeblack@google.com    trans.set_dmi_allowed(false);
9812922Sgabeblack@google.com    trans.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
9912922Sgabeblack@google.com
10012922Sgabeblack@google.com    ++mTransactionCount;
10112922Sgabeblack@google.com    return true;
10212922Sgabeblack@google.com  }
10312922Sgabeblack@google.com
10412922Sgabeblack@google.com  void logStartTransation(transaction_type& trans)
10512922Sgabeblack@google.com  {
10612922Sgabeblack@google.com    if (trans.get_command() == tlm::TLM_WRITE_COMMAND) {
10712922Sgabeblack@google.com      std::cout << name() << ": Send write request: A = 0x"
10812922Sgabeblack@google.com                << std::hex << (unsigned int)trans.get_address()
10912922Sgabeblack@google.com                << ", D = 0x" << mData << std::dec
11012922Sgabeblack@google.com                << " @ " << sc_core::sc_time_stamp() << std::endl;
11112922Sgabeblack@google.com
11212922Sgabeblack@google.com    } else {
11312922Sgabeblack@google.com      std::cout << name() << ": Send read request: A = 0x"
11412922Sgabeblack@google.com                << std::hex << (unsigned int)trans.get_address() << std::dec
11512922Sgabeblack@google.com                << " @ " << sc_core::sc_time_stamp() << std::endl;
11612922Sgabeblack@google.com    }
11712922Sgabeblack@google.com  }
11812922Sgabeblack@google.com
11912922Sgabeblack@google.com  void logEndTransaction(transaction_type& trans)
12012922Sgabeblack@google.com  {
12112922Sgabeblack@google.com    if (trans.get_response_status() != tlm::TLM_OK_RESPONSE) {
12212922Sgabeblack@google.com      std::cout << name() << ": Received error response @ "
12312922Sgabeblack@google.com                << sc_core::sc_time_stamp() << std::endl;
12412922Sgabeblack@google.com
12512922Sgabeblack@google.com    } else {
12612922Sgabeblack@google.com      std::cout << name() <<  ": Received ok response";
12712922Sgabeblack@google.com      if (trans.get_command() == tlm::TLM_READ_COMMAND) {
12812922Sgabeblack@google.com        std::cout << ": D = 0x" << std::hex << mData << std::dec;
12912922Sgabeblack@google.com      }
13012922Sgabeblack@google.com      std::cout << " @ " << sc_core::sc_time_stamp() << std::endl;
13112922Sgabeblack@google.com    }
13212922Sgabeblack@google.com  }
13312922Sgabeblack@google.com
13412922Sgabeblack@google.com  std::pair<dmi_type, bool>& getDMIData(const transaction_type& trans)
13512922Sgabeblack@google.com  {
13612922Sgabeblack@google.com    if (trans.get_command() == tlm::TLM_READ_COMMAND) {
13712922Sgabeblack@google.com      return mDMIDataReads;
13812922Sgabeblack@google.com
13912922Sgabeblack@google.com    } else { // WRITE
14012922Sgabeblack@google.com      return mDMIDataWrites;
14112922Sgabeblack@google.com    }
14212922Sgabeblack@google.com  }
14312922Sgabeblack@google.com
14412922Sgabeblack@google.com  void run()
14512922Sgabeblack@google.com  {
14612922Sgabeblack@google.com    transaction_type trans;
14712922Sgabeblack@google.com    sc_core::sc_time t;
14812922Sgabeblack@google.com
14912922Sgabeblack@google.com    while (initTransaction(trans)) {
15012922Sgabeblack@google.com      // Create transaction and initialise t
15112922Sgabeblack@google.com      t = sc_core::SC_ZERO_TIME;
15212922Sgabeblack@google.com
15312922Sgabeblack@google.com      logStartTransation(trans);
15412922Sgabeblack@google.com
15512922Sgabeblack@google.com      ///////////////////////////////////////////////////////////
15612922Sgabeblack@google.com      // DMI handling:
15712922Sgabeblack@google.com      // We do *not* use the DMI hint to check if it makes sense to ask for
15812922Sgabeblack@google.com      // DMI pointers. So the pattern is:
15912922Sgabeblack@google.com      // - if the address is not covered by a DMI region try to acquire DMI
16012922Sgabeblack@google.com      //   pointers
16112922Sgabeblack@google.com      // - if we have a DMI pointer, do the DMI "transaction"
16212922Sgabeblack@google.com      // - otherwise fall back to a normal transaction
16312922Sgabeblack@google.com      ///////////////////////////////////////////////////////////
16412922Sgabeblack@google.com
16512922Sgabeblack@google.com      std::pair<dmi_type, bool>& dmi_data = getDMIData(trans);
16612922Sgabeblack@google.com
16712922Sgabeblack@google.com      // Check if we need to acquire a DMI pointer
16812922Sgabeblack@google.com      if((trans.get_address() < dmi_data.first.get_start_address()) ||
16912922Sgabeblack@google.com         (trans.get_address() > dmi_data.first.get_end_address()) )
17012922Sgabeblack@google.com      {
17112922Sgabeblack@google.com          sc_dt::uint64 address = trans.get_address(); //save original address
17212922Sgabeblack@google.com          dmi_data.second =
17312922Sgabeblack@google.com            socket->get_direct_mem_ptr(trans,
17412922Sgabeblack@google.com                                       dmi_data.first);
17512922Sgabeblack@google.com          trans.set_address(address);
17612922Sgabeblack@google.com      }
17712922Sgabeblack@google.com      // Do DMI "transaction" if we have a valid region
17812922Sgabeblack@google.com      if (dmi_data.second &&
17912922Sgabeblack@google.com          (trans.get_address() >= dmi_data.first.get_start_address()) &&
18012922Sgabeblack@google.com          (trans.get_address() <= dmi_data.first.get_end_address()) )
18112922Sgabeblack@google.com      {
18212922Sgabeblack@google.com          // We can handle the data here. As the logEndTransaction is assuming
18312922Sgabeblack@google.com          // something to happen in the data structure, we really need to
18412922Sgabeblack@google.com          // do this:
18512922Sgabeblack@google.com          trans.set_response_status(tlm::TLM_OK_RESPONSE);
18612922Sgabeblack@google.com          sc_dt::uint64 tmp = trans.get_address() - dmi_data.first.get_start_address();
18712922Sgabeblack@google.com          if (trans.get_command() == tlm::TLM_WRITE_COMMAND)
18812922Sgabeblack@google.com          {
18912922Sgabeblack@google.com              *(unsigned int*)&dmi_data.first.get_dmi_ptr()[tmp] = mData;
19012922Sgabeblack@google.com          }
19112922Sgabeblack@google.com          else
19212922Sgabeblack@google.com          {
19312922Sgabeblack@google.com              mData = *(unsigned int*)&dmi_data.first.get_dmi_ptr()[tmp];
19412922Sgabeblack@google.com          }
19512922Sgabeblack@google.com
19612922Sgabeblack@google.com          // Do the wait immediately. Note that doing the wait here eats almost
19712922Sgabeblack@google.com          // all the performance anyway, so we only gain something if we're
19812922Sgabeblack@google.com          // using temporal decoupling.
19912922Sgabeblack@google.com          if (trans.get_command() == tlm::TLM_WRITE_COMMAND) {
20012922Sgabeblack@google.com            wait(dmi_data.first.get_write_latency());
20112922Sgabeblack@google.com
20212922Sgabeblack@google.com          } else {
20312922Sgabeblack@google.com            wait(dmi_data.first.get_read_latency());
20412922Sgabeblack@google.com          }
20512922Sgabeblack@google.com      }
20612922Sgabeblack@google.com      else // we need a full transaction
20712922Sgabeblack@google.com      {
20812922Sgabeblack@google.com          socket->b_transport(trans, t);
20912922Sgabeblack@google.com          wait(t);
21012922Sgabeblack@google.com      }
21112922Sgabeblack@google.com      logEndTransaction(trans);
21212922Sgabeblack@google.com    }
21312922Sgabeblack@google.com    wait();
21412922Sgabeblack@google.com
21512922Sgabeblack@google.com  }
21612922Sgabeblack@google.com
21712922Sgabeblack@google.com  // Invalidate DMI pointer(s)
21812922Sgabeblack@google.com  void invalidate_direct_mem_ptr(sc_dt::uint64 start_range,
21912922Sgabeblack@google.com                                 sc_dt::uint64 end_range)
22012922Sgabeblack@google.com  {
22112922Sgabeblack@google.com    // FIXME: probably faster to always invalidate everything?
22212922Sgabeblack@google.com    if (start_range <= mDMIDataReads.first.get_end_address ()&&
22312922Sgabeblack@google.com        end_range >= mDMIDataReads.first.get_start_address()) {
22412922Sgabeblack@google.com        mDMIDataReads.second = false;
22512922Sgabeblack@google.com    }
22612922Sgabeblack@google.com    if (start_range <= mDMIDataWrites.first.get_end_address ()&&
22712922Sgabeblack@google.com        end_range >= mDMIDataWrites.first.get_start_address()) {
22812922Sgabeblack@google.com      mDMIDataWrites.second = false;
22912922Sgabeblack@google.com    }
23012922Sgabeblack@google.com  }
23112922Sgabeblack@google.com
23212922Sgabeblack@google.com  // Test for transport_dbg, this one should fail in bus_dmi as we address
23312922Sgabeblack@google.com  // a target that doesn't support transport_dbg:
23412922Sgabeblack@google.com  // FIXME: use a configurable address
23512922Sgabeblack@google.com  void end_of_simulation()
23612922Sgabeblack@google.com  {
23712922Sgabeblack@google.com    std::cout <<  name() << ", <<SimpleLTInitiator1>>:" << std::endl
23812922Sgabeblack@google.com              << std::endl;
23912922Sgabeblack@google.com    unsigned char data[32];
24012922Sgabeblack@google.com
24112922Sgabeblack@google.com    transaction_type trans;
24212922Sgabeblack@google.com    trans.set_address(mBaseAddress);
24312922Sgabeblack@google.com    trans.set_data_length(32);
24412922Sgabeblack@google.com    trans.set_data_ptr(data);
24512922Sgabeblack@google.com    trans.set_read();
24612922Sgabeblack@google.com
24712922Sgabeblack@google.com    unsigned int n = socket->transport_dbg(trans);
24812922Sgabeblack@google.com
24912922Sgabeblack@google.com    std::cout << "Mem @" << std::hex << mBaseAddress << std::endl;
25012922Sgabeblack@google.com    unsigned int j = 0;
25112922Sgabeblack@google.com
25212922Sgabeblack@google.com    if (n > 0)
25312922Sgabeblack@google.com    {
25412922Sgabeblack@google.com        // always align endianness, so that we don't get a diff when
25512922Sgabeblack@google.com        // printing the raw data
25612922Sgabeblack@google.com        int e_start = 0;
25712922Sgabeblack@google.com        int e_end = 4;
25812922Sgabeblack@google.com        int e_increment = 1;
25912922Sgabeblack@google.com        if (!tlm::host_has_little_endianness())
26012922Sgabeblack@google.com        {
26112922Sgabeblack@google.com            e_start = 3;
26212922Sgabeblack@google.com            e_end = -1;
26312922Sgabeblack@google.com            e_increment = -1;
26412922Sgabeblack@google.com        }
26512922Sgabeblack@google.com
26612922Sgabeblack@google.com        for (unsigned int i=0; i<n; i+=4)
26712922Sgabeblack@google.com        {
26812922Sgabeblack@google.com            for (int k=e_start; k!=e_end; k+=e_increment)
26912922Sgabeblack@google.com            {
27012922Sgabeblack@google.com                std::cout << std::setw(2) << std::setfill('0')
27112922Sgabeblack@google.com                          << (int)data[i+k];
27212922Sgabeblack@google.com                j++;
27312922Sgabeblack@google.com                if (j==16) {
27412922Sgabeblack@google.com                    j=0;
27512922Sgabeblack@google.com                    std::cout << std::endl;
27612922Sgabeblack@google.com                } else {
27712922Sgabeblack@google.com                    std::cout << " ";
27812922Sgabeblack@google.com                }
27912922Sgabeblack@google.com            }
28012922Sgabeblack@google.com        }
28112922Sgabeblack@google.com    }
28212922Sgabeblack@google.com    else
28312922Sgabeblack@google.com    {
28412922Sgabeblack@google.com        std::cout << "OK: debug transaction didn't give data." << std::endl;
28512922Sgabeblack@google.com    }
28612922Sgabeblack@google.com    std::cout << std::dec << std::endl;
28712922Sgabeblack@google.com  }
28812922Sgabeblack@google.comprivate:
28912922Sgabeblack@google.com  std::pair<dmi_type, bool> mDMIDataReads;
29012922Sgabeblack@google.com  std::pair<dmi_type, bool> mDMIDataWrites;
29112922Sgabeblack@google.com
29212922Sgabeblack@google.com  sc_core::sc_event mEndEvent;
29312922Sgabeblack@google.com  unsigned int mNrOfTransactions;
29412922Sgabeblack@google.com  unsigned int mBaseAddress;
29512922Sgabeblack@google.com  unsigned int mTransactionCount;
29612922Sgabeblack@google.com  unsigned int mData;
29712922Sgabeblack@google.com};
29812922Sgabeblack@google.com
29912922Sgabeblack@google.com#endif
300