16 #ifndef ARBITRATED_SCRATCHPAD_H
17 #define ARBITRATED_SCRATCHPAD_H
19 #include <nvhls_int.h>
20 #include <nvhls_types.h>
21 #include <nvhls_message.h>
24 #include <mem_array.h>
25 #include <arbitrated_crossbar.h>
26 #include <ArbitratedScratchpad/ArbitratedScratchpadTypes.h>
55 template <
typename DataType,
unsigned int CapacityInBytes,
56 unsigned int NumInputs,
unsigned int NumBanks,
57 unsigned int InputQueueLen>
62 static const int kDebugLevel = 2;
64 static const int addr_width =
nvhls::nbits<CapacityInBytes - 1>::val;
68 static const bool is_nbanks_power_of_2 = (NumBanks & (NumBanks - 1)) == 0;
69 static const int bank_addr_width = (is_nbanks_power_of_2 && (NumBanks > 1)) ? (addr_width - log2_nbanks) : (addr_width - log2_nbanks + 1);
72 typedef NVUINTW(log2_nbanks) bank_sel_t;
73 typedef NVUINTW(bank_addr_width) bank_addr_t;
74 typedef NVUINTW(log2_inputs) input_sel_t;
80 input_sel_t input_chan;
81 static const int width = 1 + bank_addr_width + Wrapped<DataType>::width + log2_inputs;
83 template <
unsigned int Size>
84 void Marshall(Marshaller<Size>& m) {
94 static const int width = 1 + Wrapped<DataType>::width;
96 template <
unsigned int Size>
97 void Marshall(Marshaller<Size>& m) {
114 void compute_bank_request(
req_t &curr_cli_req,
bank_req_t bank_req[NumInputs],
115 bank_sel_t bank_sel[NumInputs],
116 bool bank_req_valid[NumInputs]) {
119 #pragma hls_unroll yes
120 for (
unsigned in_chan = 0; in_chan < NumInputs; in_chan++) {
123 if (NumInputs == 1) {
124 bank_sel[in_chan] = 0;
126 if (is_nbanks_power_of_2) {
127 bank_sel[in_chan] = nvhls::get_slc<log2_nbanks>(curr_cli_req.addr[in_chan], 0);
129 bank_sel[in_chan] = curr_cli_req.addr[in_chan] % NumBanks;
133 bank_req[in_chan].do_store = (curr_cli_req.valids[in_chan] ==
true) &&
134 (curr_cli_req.type.val == CLITYPE_T::STORE);
136 if (NumInputs == 1) {
137 bank_req[in_chan].addr = curr_cli_req.addr[in_chan];
139 if (is_nbanks_power_of_2) {
140 bank_req[in_chan].addr =
nvhls::get_slc<addr_width - log2_nbanks>(curr_cli_req.addr[in_chan], log2_nbanks);
142 bank_req[in_chan].addr = curr_cli_req.addr[in_chan] / NumBanks;
146 if (bank_req[in_chan].do_store) {
147 bank_req[in_chan].wdata = curr_cli_req.data[in_chan];
151 input_sel_t input_idx = in_chan;
152 bank_req[in_chan].input_chan = input_idx;
155 bank_req_valid[in_chan] = (curr_cli_req.valids[in_chan] ==
true);
159 void banks_load_store(bank_req_t bank_req[NumBanks],
160 bool bank_req_valid[NumBanks],
161 bank_rsp_t bank_rsp[NumBanks]) {
162 #pragma hls_unroll yes
163 for (
unsigned bank = 0; bank < NumBanks; bank++) {
164 if (bank_req_valid[bank] ==
true) {
165 if (!bank_req[bank].do_store) {
166 bank_rsp[bank].valid =
true;
167 bank_rsp[bank].rdata = banks.read(bank_req[bank].addr, bank);
169 banks.write(bank_req[bank].addr, bank, bank_req[bank].wdata);
170 bank_rsp[bank].valid =
false;
173 bank_rsp[bank].valid =
false;
181 void reset() { request_xbar.reset(); }
183 #ifdef HLS_ALGORITHMICC
184 void load_store(req_t &curr_cli_req, rsp_t &load_rsp,
185 bool input_ready[NumInputs]) {
186 bank_req_t bank_req[NumInputs];
187 bank_sel_t bank_sel[NumInputs];
188 bool bank_req_valid[NumInputs];
190 compute_bank_request(curr_cli_req, bank_req, bank_sel, bank_req_valid);
192 void load_store(bank_req_t bank_req[NumInputs],
193 bank_sel_t bank_sel[NumInputs],
194 bool bank_req_valid[NumInputs],
195 rsp_t &load_rsp,
bool input_ready[NumInputs]) {
197 CDCOUT(
"\tinputs:" << endl, kDebugLevel);
198 for (
unsigned i = 0; i < NumInputs; ++i) {
200 <<
" valid=" << bank_req_valid[i]
201 <<
" select=" << bank_sel[i]
202 <<
" addr=" << bank_req[i].addr
203 <<
" wdata=" << bank_req[i].wdata
204 <<
" load=" << !bank_req[i].do_store
205 <<
" store=" << bank_req[i].do_store
206 <<
" input=" << bank_req[i].input_chan << endl, kDebugLevel);
208 CDCOUT(
"\t------" << endl, kDebugLevel);
210 bank_req_t bank_req_winner[NumBanks];
211 bool bank_req_winner_valid[NumBanks];
212 request_xbar.
run(bank_req, bank_sel, bank_req_valid, bank_req_winner,
213 bank_req_winner_valid, input_ready);
215 CDCOUT(
"\t\tbank winner transactions:" << endl, kDebugLevel);
216 for (
unsigned i = 0; i < NumBanks; ++i) {
217 CDCOUT(
"\t\t" << i <<
" :"
218 <<
" valid=" << bank_req_winner_valid[i]
219 <<
" addr=" << bank_req_winner[i].addr
220 <<
" wdata=" << bank_req_winner[i].wdata
221 <<
" load=" << !bank_req_winner[i].do_store
222 <<
" store=" << bank_req_winner[i].do_store
223 <<
" input=" << bank_req_winner[i].input_chan << endl, kDebugLevel);
225 CDCOUT(
"\t\t------" << endl, kDebugLevel);
226 CDCOUT(
"\t\tinput_ready:" << endl, kDebugLevel);
227 for (
unsigned i = 0; i < NumInputs; ++i) {
228 CDCOUT(
"\t\t" << i <<
" : ready=" << input_ready[i] << endl, kDebugLevel);
230 CDCOUT(
"\t\t------" << endl, kDebugLevel);
232 bank_rsp_t bank_rsp[NumBanks];
233 banks_load_store(bank_req_winner, bank_req_winner_valid, bank_rsp);
236 DataType data_in[NumBanks];
237 bool valid_in[NumBanks];
238 bank_sel_t source[NumInputs];
239 bool valid_src[NumInputs];
240 DataType data_out[NumInputs];
241 bool valid_out[NumInputs];
243 #pragma hls_unroll yes
244 for (
unsigned out = 0; out < NumInputs; out++) {
245 valid_src[out] =
false;
248 #pragma hls_unroll yes
249 for (
unsigned bank = 0; bank < NumBanks; bank++) {
250 valid_in[bank] = bank_rsp[bank].valid;
251 data_in[bank] = bank_rsp[bank].rdata;
252 if (bank_rsp[bank].valid) {
253 source[bank_req_winner[bank].input_chan] = bank;
254 valid_src[bank_req_winner[bank].input_chan] =
true;
258 crossbar<DataType, NumBanks, NumInputs>(data_in, valid_in, source,
259 valid_src, data_out, valid_out);
261 #pragma hls_unroll yes
262 for (
unsigned out = 0; out < NumInputs; out++) {
263 load_rsp.valids[out] = valid_out[out];
264 load_rsp.data[out] = data_out[out];
Crossbar with conflict arbitration and input queuing.
Scratchpad Memories with arbitration and queuing.
void run(DataType data_in[NumInputs], OutputIdx dest_in[NumInputs], bool valid_in[NumInputs], DataType data_out[NumOutputs], bool valid_out[NumOutputs], bool ready[NumInputs], InputIdx source[NumOutputs])
Top-Level function for Arbitrated Crossbar.
nvhls_t< W >::nvuint_t get_slc(type X, const unsigned int i)
Function that returns slice of bits.
Compute number of bits to represent a constant.