-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
228 lines (193 loc) · 7.23 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright 2017 Matthias Jung
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors:
* - Matthias Jung
*/
#include <iostream>
#include <iomanip>
#include <map>
#include <queue>
#include <systemc.h>
#include <tlm.h>
// Convenience Sockets:
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
#include "memory_manager.h"
#include "initiator.h"
#include "target.h"
#include "tlm2_base_protocol_checker.h"
using namespace std;
class routingExtension : public tlm::tlm_extension<routingExtension>
{
private:
int inputPortNumber;
int outputPortNumber;
public:
routingExtension(int i, int o) : inputPortNumber(i),
outputPortNumber(o)
{
cout << "\033[1;36m(E"
<< ") @" << setfill(' ') << setw(12) << sc_time_stamp()
<< ": Extension Created = "
<< " inPort = " << dec << setfill(' ') << setw(2) << i
<< " outPort = " << dec << setfill(' ') << setw(2) << o
<< "\033[0m" << endl;
}
tlm_extension_base *clone() const
{
return new routingExtension(inputPortNumber, outputPortNumber);
}
void copy_from(const tlm_extension_base &ext)
{
const routingExtension &cpyFrom =
static_cast<const routingExtension &>(ext);
inputPortNumber = cpyFrom.getInputPortNumber();
outputPortNumber = cpyFrom.getOutputPortNumber();
}
int getInputPortNumber() const
{
return inputPortNumber;
}
int getOutputPortNumber() const
{
return outputPortNumber;
}
};
SC_MODULE(Interconnect)
{
public:
tlm_utils::multi_passthrough_target_socket<Interconnect> tSocket;
tlm_utils::multi_passthrough_initiator_socket<Interconnect> iSocket;
SC_CTOR(Interconnect) : tSocket("tSocket"), iSocket("iSocket")
{
tSocket.register_b_transport(this, &Interconnect::b_transport);
tSocket.register_nb_transport_fw(this, &Interconnect::nb_transport_fw);
iSocket.register_nb_transport_bw(this, &Interconnect::nb_transport_bw);
}
private:
int routeFW(int inPort,
tlm::tlm_generic_payload &trans,
bool store)
{
int outPort = 0;
// Memory map implementation:
if (trans.get_address() < 512)
{
outPort = 0;
}
else if (trans.get_address() >= 512 && trans.get_address() < 1024)
{
// Correct Address:
trans.set_address(trans.get_address() - 512);
outPort = 1;
}
else
{
trans.set_response_status( tlm::TLM_ADDRESS_ERROR_RESPONSE );
}
if (store)
{
routingExtension *ext = new routingExtension(inPort, outPort);
trans.set_auto_extension(ext);
}
return outPort;
}
virtual void b_transport(int id,
tlm::tlm_generic_payload &trans,
sc_time &delay)
{
int outPort = routeFW(id, trans, false);
iSocket[outPort]->b_transport(trans, delay);
}
virtual tlm::tlm_sync_enum nb_transport_fw(int id,
tlm::tlm_generic_payload &trans,
tlm::tlm_phase &phase,
sc_time &delay)
{
int outPort = 0;
if (phase == tlm::BEGIN_REQ)
{
// In the case of nb_transport_fw the address attribute is valid
// immediately upon entering the function but only when the phase
// is BEGIN_REQ. Following the return from any forward path TLM-2.0
// interface method call, the address attribute will have the value
// set by the interconnect component lying furthest downstream, and
// so should be regarded as being undefined for the purposes of
// transaction routing.
trans.acquire();
// Modify address accoring to memory map:
outPort = routeFW(id, trans, true);
}
else if (phase == tlm::END_RESP)
{
// Adress was already modified in BEGIN_REQ phase:
routingExtension *ext = nullptr;
trans.get_extension(ext);
outPort = ext->getOutputPortNumber();
trans.release();
}
else
{
SC_REPORT_FATAL(name(),"Illegal phase received by initiator");
}
cout << "\033[1;37m("
<< name()
<< ")@" << setfill(' ') << setw(12) << sc_time_stamp()
<< ": Addr = " << setfill('0') << setw(8)
<< dec << trans.get_address()
<< " inPort = " << dec << setfill(' ') << setw(2) << id
<< " outPort = " << dec << setfill(' ') << setw(2) << outPort
<< " ptr = " << &trans
<< "\033[0m" << endl;
return iSocket[outPort]->nb_transport_fw(trans, phase, delay);
}
virtual tlm::tlm_sync_enum nb_transport_bw(int id,
tlm::tlm_generic_payload &trans,
tlm::tlm_phase &phase,
sc_time &delay)
{
routingExtension *ext = nullptr;
trans.get_extension(ext);
int inPort = ext->getInputPortNumber();
return tSocket[inPort]->nb_transport_bw(trans, phase, delay);
}
};
int sc_main (int, char **)
{
Initiator *cpu1 = new Initiator("C1");
Target *memory1 = new Target("M1", 8);
Target *memory2 = new Target("M2", 8);
Interconnect *bus = new Interconnect("B1");
cpu1->iSocket.bind(bus->tSocket);
bus->iSocket.bind(memory1->tSocket);
bus->iSocket.bind(memory2->tSocket);
sc_start();
return 0;
}