1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20//====================================================================
21//  Nov 06, 2008
22//
23//  Updated by:
24//    Xiaopeng Qiu, JEDA Technologies, Inc
25//    Email:  qiuxp@jedatechnologies.net
26//
27//  To fix violations of TLM2.0 rules, which are detected by JEDA
28//  TLM2.0 checker.
29//
30//====================================================================
31
32#ifndef __SIMPLE_LT_INITIATOR3_H__
33#define __SIMPLE_LT_INITIATOR3_H__
34
35#include "tlm.h"
36#include "tlm_utils/simple_initiator_socket.h"
37//#include <systemc>
38#include <cassert>
39//#include <iostream>
40
41class SimpleLTInitiator3 : public sc_core::sc_module
42{
43public:
44  typedef tlm::tlm_generic_payload                  transaction_type;
45  typedef tlm::tlm_phase                            phase_type;
46  typedef tlm::tlm_sync_enum                        sync_enum_type;
47  typedef tlm_utils::simple_initiator_socket<SimpleLTInitiator3> initiator_socket_type;
48
49public:
50  initiator_socket_type socket;
51
52public:
53  SC_HAS_PROCESS(SimpleLTInitiator3);
54  SimpleLTInitiator3(sc_core::sc_module_name name,
55                  unsigned int nrOfTransactions = 0x5,
56                  unsigned int baseAddress = 0x0) :
57    sc_core::sc_module(name),
58    socket("socket"),
59    mNrOfTransactions(nrOfTransactions),
60    mBaseAddress(baseAddress),
61    mTransactionCount(0)
62  {
63    // Initiator thread
64    SC_THREAD(run);
65  }
66
67  bool initTransaction(transaction_type& trans)
68  {
69    if (mTransactionCount < mNrOfTransactions) {
70      trans.set_address(mBaseAddress + 4*mTransactionCount);
71      mData = mTransactionCount;
72      trans.set_command(tlm::TLM_WRITE_COMMAND);
73
74    } else if (mTransactionCount < 2 * mNrOfTransactions) {
75      trans.set_address(mBaseAddress + 4*(mTransactionCount-mNrOfTransactions));
76      mData = 0;
77      trans.set_command(tlm::TLM_READ_COMMAND);
78
79    } else {
80      return false;
81    }
82
83    trans.set_data_ptr(reinterpret_cast<unsigned char*>(&mData));
84    trans.set_data_length(4);
85    trans.set_streaming_width(4);
86    trans.set_dmi_allowed(false);
87    trans.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
88
89    ++mTransactionCount;
90    return true;
91  }
92
93  void logStartTransation(transaction_type& trans)
94  {
95    if (trans.get_command() == tlm::TLM_WRITE_COMMAND) {
96      std::cout << name() << ": Send write request: A = 0x"
97                << std::hex << (unsigned int)trans.get_address()
98                << ", D = 0x" << mData << std::dec
99                << " @ " << sc_core::sc_time_stamp() << std::endl;
100
101    } else {
102      std::cout << name() << ": Send read request: A = 0x"
103                << std::hex << (unsigned int)trans.get_address() << std::dec
104                << " @ " << sc_core::sc_time_stamp() << std::endl;
105    }
106  }
107
108  void logEndTransaction(transaction_type& trans)
109  {
110    if (trans.get_response_status() != tlm::TLM_OK_RESPONSE) {
111      std::cout << name() << ": Received error response @ "
112                << sc_core::sc_time_stamp() << std::endl;
113
114    } else {
115      std::cout << name() <<  ": Received ok response";
116      if (trans.get_command() == tlm::TLM_READ_COMMAND) {
117        std::cout << ": D = 0x" << std::hex << mData << std::dec;
118      }
119      std::cout << " @ " << sc_core::sc_time_stamp() << std::endl;
120    }
121  }
122
123  void run()
124  {
125    transaction_type trans;
126    sc_core::sc_time t;
127
128    while (initTransaction(trans)) {
129      // Create transaction and initialise t
130      t = sc_core::SC_ZERO_TIME;
131
132      logStartTransation(trans);
133
134      socket->b_transport(trans, t);
135      // Transaction Finished, wait for the returned delay
136      wait(t);
137
138      logEndTransaction(trans);
139    }
140    wait();
141
142  }
143
144private:
145  sc_core::sc_event mEndEvent;
146  unsigned int mNrOfTransactions;
147  unsigned int mBaseAddress;
148  unsigned int mTransactionCount;
149  unsigned int mData;
150};
151
152#endif
153