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#ifndef __SIMPLE_LT_TARGET2_H__
2112855Sgabeblack@google.com#define __SIMPLE_LT_TARGET2_H__
2212855Sgabeblack@google.com
2312855Sgabeblack@google.com#include "tlm.h"
2412855Sgabeblack@google.com#include "tlm_utils/simple_target_socket.h"
2512855Sgabeblack@google.com#include "my_extension.h"
2612855Sgabeblack@google.com
2712855Sgabeblack@google.com//#include <systemc>
2812855Sgabeblack@google.com#include <cassert>
2912855Sgabeblack@google.com#include <vector>
3012855Sgabeblack@google.com//#include <iostream>
3112855Sgabeblack@google.com
3212855Sgabeblack@google.comclass SimpleLTTarget_ext : public sc_core::sc_module
3312855Sgabeblack@google.com{
3412855Sgabeblack@google.compublic:
3512855Sgabeblack@google.com  typedef tlm::tlm_generic_payload              transaction_type;
3612855Sgabeblack@google.com  typedef tlm::tlm_phase                        phase_type;
3712855Sgabeblack@google.com  typedef tlm::tlm_sync_enum                    sync_enum_type;
3812855Sgabeblack@google.com  typedef tlm_utils::simple_target_socket<SimpleLTTarget_ext, 32,
3912855Sgabeblack@google.com                             my_extended_payload_types> target_socket_type;
4012855Sgabeblack@google.com
4112855Sgabeblack@google.compublic:
4212855Sgabeblack@google.com  target_socket_type socket;
4312855Sgabeblack@google.com
4412855Sgabeblack@google.compublic:
4512855Sgabeblack@google.com  SC_HAS_PROCESS(SimpleLTTarget_ext);
4612855Sgabeblack@google.com  SimpleLTTarget_ext(sc_core::sc_module_name name,
4712855Sgabeblack@google.com                     sc_core::sc_time invalidate_dmi_time = sc_core::sc_time(25, sc_core::SC_NS)) :
4812855Sgabeblack@google.com    sc_core::sc_module(name),
4912855Sgabeblack@google.com    socket("socket")
5012855Sgabeblack@google.com  {
5112855Sgabeblack@google.com    // register nb_transport method
5212855Sgabeblack@google.com    socket.register_nb_transport_fw(this, &SimpleLTTarget_ext::myNBTransport);
5312855Sgabeblack@google.com    socket.register_get_direct_mem_ptr(this, &SimpleLTTarget_ext::myGetDMIPtr);
5412855Sgabeblack@google.com
5512855Sgabeblack@google.com    socket.register_transport_dbg(this, &SimpleLTTarget_ext::transport_dbg);
5612855Sgabeblack@google.com
5712855Sgabeblack@google.com    SC_METHOD(invalidate_dmi_method);
5812855Sgabeblack@google.com    sensitive << m_invalidate_dmi_event;
5912855Sgabeblack@google.com    dont_initialize();
6012855Sgabeblack@google.com    m_invalidate_dmi_time = invalidate_dmi_time;
6112855Sgabeblack@google.com  }
6212855Sgabeblack@google.com
6312855Sgabeblack@google.com  sync_enum_type myNBTransport(transaction_type& trans, phase_type& phase, sc_core::sc_time& t)
6412855Sgabeblack@google.com  {
6512855Sgabeblack@google.com    sc_assert(phase == tlm::BEGIN_REQ);
6612855Sgabeblack@google.com
6712855Sgabeblack@google.com    my_extension* tmp_ext;
6812855Sgabeblack@google.com    trans.get_extension(tmp_ext);
6912855Sgabeblack@google.com    if (!tmp_ext)
7012855Sgabeblack@google.com    {
7112855Sgabeblack@google.com        std::cout << name() << ": ERROR, extension not present" << std::endl;
7212855Sgabeblack@google.com    }
7312855Sgabeblack@google.com    else
7412855Sgabeblack@google.com    {
7512855Sgabeblack@google.com        std::cout << name() << ": OK, extension data = "
7612855Sgabeblack@google.com                  << tmp_ext->m_data << std::endl;
7712855Sgabeblack@google.com    }
7812855Sgabeblack@google.com    sc_dt::uint64 address = trans.get_address();
7912855Sgabeblack@google.com    sc_assert(address < 400);
8012855Sgabeblack@google.com
8112855Sgabeblack@google.com    unsigned int& data = *reinterpret_cast<unsigned int*>(trans.get_data_ptr());
8212855Sgabeblack@google.com    if (trans.get_command() == tlm::TLM_WRITE_COMMAND) {
8312855Sgabeblack@google.com      std::cout << name() << ": Received write request: A = 0x"
8412855Sgabeblack@google.com                << std::hex << (unsigned int)address
8512855Sgabeblack@google.com                << ", D = 0x" << data << std::dec
8612855Sgabeblack@google.com                << " @ " << sc_core::sc_time_stamp() << std::endl;
8712855Sgabeblack@google.com
8812855Sgabeblack@google.com      *reinterpret_cast<unsigned int*>(&mMem[address]) = data;
8912855Sgabeblack@google.com      t += sc_core::sc_time(10, sc_core::SC_NS);
9012855Sgabeblack@google.com
9112855Sgabeblack@google.com    } else {
9212855Sgabeblack@google.com      std::cout << name() << ": Received read request: A = 0x"
9312855Sgabeblack@google.com                << std::hex << (unsigned int)address << std::dec
9412855Sgabeblack@google.com                << " @ " << sc_core::sc_time_stamp() << std::endl;
9512855Sgabeblack@google.com
9612855Sgabeblack@google.com      data = *reinterpret_cast<unsigned int*>(&mMem[address]);
9712855Sgabeblack@google.com      t += sc_core::sc_time(100, sc_core::SC_NS);
9812855Sgabeblack@google.com    }
9912855Sgabeblack@google.com
10012855Sgabeblack@google.com    trans.set_response_status(tlm::TLM_OK_RESPONSE);
10112855Sgabeblack@google.com
10212855Sgabeblack@google.com    trans.set_dmi_allowed(true);
10312855Sgabeblack@google.com
10412855Sgabeblack@google.com    // LT target
10512855Sgabeblack@google.com    // - always return true
10612855Sgabeblack@google.com    // - not necessary to update phase (if true is returned)
10712855Sgabeblack@google.com    return tlm::TLM_COMPLETED;
10812855Sgabeblack@google.com  }
10912855Sgabeblack@google.com
11012855Sgabeblack@google.com  unsigned int transport_dbg(transaction_type& r)
11112855Sgabeblack@google.com  {
11212855Sgabeblack@google.com    if (r.get_address() >= 400) return 0;
11312855Sgabeblack@google.com
11412855Sgabeblack@google.com    unsigned int tmp = (int)r.get_address();
11512855Sgabeblack@google.com    unsigned int num_bytes;
11612855Sgabeblack@google.com    if (tmp + r.get_data_length() >= 400) {
11712855Sgabeblack@google.com      num_bytes = 400 - tmp;
11812855Sgabeblack@google.com
11912855Sgabeblack@google.com    } else {
12012855Sgabeblack@google.com      num_bytes = r.get_data_length();
12112855Sgabeblack@google.com    }
12212855Sgabeblack@google.com    if (r.is_read()) {
12312855Sgabeblack@google.com      for (unsigned int i = 0; i < num_bytes; ++i) {
12412855Sgabeblack@google.com        r.get_data_ptr()[i] = mMem[i + tmp];
12512855Sgabeblack@google.com      }
12612855Sgabeblack@google.com
12712855Sgabeblack@google.com    } else {
12812855Sgabeblack@google.com      for (unsigned int i = 0; i < num_bytes; ++i) {
12912855Sgabeblack@google.com        mMem[i + tmp] = r.get_data_ptr()[i];
13012855Sgabeblack@google.com      }
13112855Sgabeblack@google.com    }
13212855Sgabeblack@google.com    return num_bytes;
13312855Sgabeblack@google.com  }
13412855Sgabeblack@google.com
13512855Sgabeblack@google.com  bool myGetDMIPtr(transaction_type& trans,
13612855Sgabeblack@google.com                   tlm::tlm_dmi&  dmi_data)
13712855Sgabeblack@google.com  {
13812855Sgabeblack@google.com      // notify DMI invalidation, just to check if this reaches the
13912855Sgabeblack@google.com      // initiators properly
14012855Sgabeblack@google.com      m_invalidate_dmi_event.notify(m_invalidate_dmi_time);
14112855Sgabeblack@google.com
14212855Sgabeblack@google.com      // Check for DMI extension:
14312855Sgabeblack@google.com      my_extension * tmp_ext;
14412855Sgabeblack@google.com      trans.get_extension(tmp_ext);
14512855Sgabeblack@google.com      if (tmp_ext)
14612855Sgabeblack@google.com      {
14712855Sgabeblack@google.com        std::cout << name() << ": get_direct_mem_ptr OK, extension data = "
14812855Sgabeblack@google.com                  <<tmp_ext->m_data << std::endl;
14912855Sgabeblack@google.com      }
15012855Sgabeblack@google.com      else
15112855Sgabeblack@google.com      {
15212855Sgabeblack@google.com          std::cout << name() << ", get_direct_mem_ptr ERROR: "
15312855Sgabeblack@google.com                    << "didn't get pointer to extension"
15412855Sgabeblack@google.com                    << std::endl;
15512855Sgabeblack@google.com      }
15612855Sgabeblack@google.com      if (trans.get_address() < 400) {
15712855Sgabeblack@google.com        dmi_data.allow_read_write();
15812855Sgabeblack@google.com        dmi_data.set_start_address(0x0);
15912855Sgabeblack@google.com        dmi_data.set_end_address(399);
16012855Sgabeblack@google.com        dmi_data.set_dmi_ptr(mMem);
16112855Sgabeblack@google.com        dmi_data.set_read_latency(sc_core::sc_time(100, sc_core::SC_NS));
16212855Sgabeblack@google.com        dmi_data.set_write_latency(sc_core::sc_time(10, sc_core::SC_NS));
16312855Sgabeblack@google.com        return true;
16412855Sgabeblack@google.com
16512855Sgabeblack@google.com      } else {
16612855Sgabeblack@google.com        // should not happen
16712855Sgabeblack@google.com        dmi_data.set_start_address(trans.get_address());
16812855Sgabeblack@google.com        dmi_data.set_end_address(trans.get_address());
16912855Sgabeblack@google.com        return false;
17012855Sgabeblack@google.com
17112855Sgabeblack@google.com      }
17212855Sgabeblack@google.com  }
17312855Sgabeblack@google.com
17412855Sgabeblack@google.com  void invalidate_dmi_method()
17512855Sgabeblack@google.com  {
17612855Sgabeblack@google.com      sc_dt::uint64 start_address = 0x0;
17712855Sgabeblack@google.com      sc_dt::uint64 end_address = 399;
17812855Sgabeblack@google.com      socket->invalidate_direct_mem_ptr(start_address, end_address);
17912855Sgabeblack@google.com  }
18012855Sgabeblack@google.comprivate:
18112855Sgabeblack@google.com  unsigned char mMem[400];
18212855Sgabeblack@google.com  sc_core::sc_event m_invalidate_dmi_event;
18312855Sgabeblack@google.com  sc_core::sc_time  m_invalidate_dmi_time;
18412855Sgabeblack@google.com};
18512855Sgabeblack@google.com
18612855Sgabeblack@google.com#endif
187