-
Notifications
You must be signed in to change notification settings - Fork 7
/
target.h
273 lines (233 loc) · 8.42 KB
/
target.h
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef TARGET_H
#define TARGET_H
#include <iostream>
#include <iomanip>
#include <map>
#include <queue>
#include <systemc.h>
#include <tlm.h>
// Convenience Sockets:
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/simple_target_socket.h>
#include <tlm_utils/multi_passthrough_initiator_socket.h>
#include <tlm_utils/multi_passthrough_target_socket.h>
// PEQ:
#include <tlm_utils/peq_with_cb_and_phase.h>
// MM and tools:
#include "memory_manager.h"
#include "util.h"
// Internal Phase for transaction processing:
DECLARE_EXTENDED_PHASE(INTERNAL);
using namespace std;
SC_MODULE(Target)
{
public:
tlm_utils::simple_target_socket<Target> tSocket;
private:
unsigned char mem[512];
protected:
bool responseInProgress;
tlm::tlm_generic_payload* endRequestPending;
tlm_utils::peq_with_cb_and_phase<Target> peq;
unsigned int numberOfTransactions;
unsigned int bufferSize;
std::queue<tlm::tlm_generic_payload*> responseQueue;
public:
SC_HAS_PROCESS(Target);
Target(sc_module_name name, unsigned int bufferSize = 8) : sc_module(name),
tSocket("tSocket"),
responseInProgress(false),
endRequestPending(nullptr),
peq(this, &Target::peqCallback),
numberOfTransactions(0),
bufferSize(bufferSize)
{
tSocket.register_b_transport(this, &Target::b_transport);
tSocket.register_nb_transport_fw(this, &Target::nb_transport_fw);
}
void printBuffer(unsigned int max, unsigned int n)
{
std::cout << "\033[1;35m("
<< name()
<< ")@" << setfill(' ') << setw(12) << sc_time_stamp()
<< " Target Buffer: "
<< "[";
for (unsigned int i = 0; i < n; i++) {
std::cout << "█";
}
for (unsigned int i = 0; i < max-n; i++) {
std::cout << " ";
}
std::cout << "]"
<< " ( " << n << " / " << max << " ) "
<< "\033[0m"
<< std::endl;
std::cout.flush();
}
virtual void b_transport(tlm::tlm_generic_payload& trans,
sc_time& delay)
{
executeTransaction(trans);
}
// [1.0, 1.6]
virtual tlm::tlm_sync_enum nb_transport_fw(tlm::tlm_generic_payload& trans,
tlm::tlm_phase& phase,
sc_time& delay)
{
// Queue the transaction into the peq until
// the annotated time has elapsed
peq.notify( trans, phase, delay);
return tlm::TLM_ACCEPTED; // [1.1, 1.7, (1.8)]
}
void peqCallback(tlm::tlm_generic_payload& trans,
const tlm::tlm_phase& phase)
{
sc_time delay;
if(phase == tlm::BEGIN_REQ) // [1.0]
{
// Increment the transaction reference count
trans.acquire();
if (numberOfTransactions < bufferSize) // Input buffersize
{
sendEndRequest(trans); // [1.2]
// HINT: instead of [1.2] we can call also [4.1] (ie. [1.4])
}
else // If buffer is full we do backpressure
{
// Put back-pressure on initiator by deferring END_REQ
endRequestPending = &trans;
}
}
else if (phase == tlm::END_RESP) // [1.6]
{
// On receiving END_RESP, the target can release the transaction
// and allow other pending transactions to proceed
if (!responseInProgress)
{
SC_REPORT_FATAL(name(),
"Illegal transaction phase END_RESP received by target");
}
// Reduce number of transactions in target:
numberOfTransactions--;
printBuffer(bufferSize, numberOfTransactions);
// Target itself is now clear to issue the next BEGIN_RESP
responseInProgress = false;
if (responseQueue.size() > 0)
{
tlm::tlm_generic_payload * next = responseQueue.front();
responseQueue.pop();
sendResponse(*next);
}
// ... and to unblock the initiator by issuing END_REQ
if (endRequestPending)
{
sendEndRequest(*endRequestPending);
endRequestPending = nullptr;
}
}
else if(phase == INTERNAL)
{
// Execute the read or write commands
executeTransaction(trans);
// Target must honor BEGIN_RESP/END_RESP exclusion rule
// i.e. must not send BEGIN_RESP until receiving previous
// END_RESP or BEGIN_REQ
if(responseInProgress)
{
responseQueue.push(&trans);
}
else
{
sendResponse(trans);
}
}
else // tlm::END_REQ or tlm::BEGIN_RESP
{
SC_REPORT_FATAL(name(), "Illegal transaction phase received");
}
}
void sendEndRequest(tlm::tlm_generic_payload& trans)
{
tlm::tlm_phase bw_phase;
sc_time delay;
// Queue the acceptance and the response with the appropriate latency
bw_phase = tlm::END_REQ;
delay = sc_time(10, SC_NS); // Accept delay
tlm::tlm_sync_enum status;
status = tSocket->nb_transport_bw(trans, bw_phase, delay); // [1.2]
// Ignore return value (has to be TLM_ACCEPTED anyway)
// initiator cannot terminate transaction at this point
// Queue internal event to mark beginning of response
delay = delay + sc_time(40, SC_NS);//randomDelay(); // Latency
peq.notify(trans, INTERNAL, delay);
numberOfTransactions++;
printBuffer(bufferSize, numberOfTransactions);
}
// Common to b_transport and nb_transport
void executeTransaction(tlm::tlm_generic_payload& trans)
{
tlm::tlm_command cmd = trans.get_command();
sc_dt::uint64 adr = trans.get_address();
unsigned char* ptr = trans.get_data_ptr();
unsigned int len = trans.get_data_length();
unsigned char* byt = trans.get_byte_enable_ptr();
unsigned int wid = trans.get_streaming_width();
if (trans.get_address() >= 512) {
trans.set_response_status( tlm::TLM_ADDRESS_ERROR_RESPONSE );
return;
}
if (byt != nullptr) {
trans.set_response_status( tlm::TLM_BYTE_ENABLE_ERROR_RESPONSE );
return;
}
if (len > 4 || wid < len) {
trans.set_response_status( tlm::TLM_BURST_ERROR_RESPONSE );
return;
}
if (cmd == tlm::TLM_READ_COMMAND)
{
memcpy(&mem[trans.get_address()], // destination
trans.get_data_ptr(), // source
trans.get_data_length()); // size
}
else if (cmd == tlm::TLM_WRITE_COMMAND)
{
memcpy(trans.get_data_ptr(), // destination
&mem[trans.get_address()], // source
trans.get_data_length()); // size
}
cout << "\033[1;32m("
<< name()
<< ")@" << setfill(' ') << setw(12) << sc_time_stamp()
<< ": " << setw(12) << (cmd ? "Exec. Write " : "Exec. Read ")
<< "Addr = " << setfill('0') << setw(8) << dec << adr
<< " Data = " << "0x" << setfill('0') << setw(8) << hex
<< *reinterpret_cast<int*>(ptr)
<< "\033[0m" << endl;
trans.set_response_status( tlm::TLM_OK_RESPONSE );
}
void sendResponse(tlm::tlm_generic_payload& trans)
{
tlm::tlm_sync_enum status;
tlm::tlm_phase bw_phase;
sc_time delay;
sc_assert(responseInProgress == false);
responseInProgress = true;
bw_phase = tlm::BEGIN_RESP;
delay = SC_ZERO_TIME;
status = tSocket->nb_transport_bw( trans, bw_phase, delay ); // [1.4]
if (status == tlm::TLM_UPDATED) // [2.1]
{
// The timing annotation must be honored
peq.notify( trans, bw_phase, delay);
}
else if (status == tlm::TLM_COMPLETED)
{
SC_REPORT_FATAL(name(),
"This transition is deprecated since TLM2.0.1");
}
// In the case of TLM_ACCEPTED [1.5] we will recv. a FW call [1.6]
trans.release();
}
};
#endif // TARGET_H