[2] | 1 | #ifndef XRAM_TRANSACTION_H_ |
---|
| 2 | #define XRAM_TRANSACTION_H_ |
---|
| 3 | |
---|
| 4 | #include <inttypes.h> |
---|
| 5 | #include <systemc> |
---|
| 6 | #include <cassert> |
---|
| 7 | #include "arithmetics.h" |
---|
| 8 | |
---|
| 9 | #define DEBUG_XRAM_TRANSACTION 0 |
---|
| 10 | |
---|
| 11 | //////////////////////////////////////////////////////////////////////// |
---|
| 12 | // A transaction tab entry |
---|
| 13 | //////////////////////////////////////////////////////////////////////// |
---|
| 14 | |
---|
| 15 | class TransactionTabEntry { |
---|
| 16 | typedef uint32_t size_t; |
---|
| 17 | typedef uint32_t data_t; |
---|
| 18 | typedef uint32_t be_t; |
---|
| 19 | |
---|
| 20 | public: |
---|
| 21 | bool valid; // entry valid |
---|
| 22 | bool xram_read; // read request to XRAM |
---|
| 23 | data_t nline; // index (zy) of the requested line |
---|
| 24 | size_t srcid; // processor requesting the transaction |
---|
| 25 | size_t trdid; // processor requesting the transaction |
---|
| 26 | size_t pktid; // processor requesting the transaction |
---|
| 27 | bool proc_read; // read request from processor |
---|
| 28 | bool single_word; // single word in case of processor read |
---|
| 29 | size_t word_index; // word index in case of single word read |
---|
| 30 | std::vector<data_t> wdata; // write buffer (one cache line) |
---|
| 31 | std::vector<be_t> wdata_be; // be for each data in the write buffer |
---|
| 32 | |
---|
| 33 | ///////////////////////////////////////////////////////////////////// |
---|
| 34 | // The init() function initializes the entry |
---|
| 35 | ///////////////////////////////////////////////////////////////////// |
---|
| 36 | void init() |
---|
| 37 | { |
---|
| 38 | valid = false; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | ///////////////////////////////////////////////////////////////////// |
---|
| 42 | // The alloc() function initializes the vectors of an entry |
---|
| 43 | // Its arguments are : |
---|
| 44 | // - n_words : number of words per line in the cache |
---|
| 45 | ///////////////////////////////////////////////////////////////////// |
---|
| 46 | void alloc(size_t n_words) |
---|
| 47 | { |
---|
| 48 | wdata_be.reserve( (int)n_words ); |
---|
| 49 | wdata.reserve( (int)n_words ); |
---|
| 50 | for(size_t i=0; i<n_words; i++){ |
---|
| 51 | wdata_be.push_back(false); |
---|
| 52 | wdata.push_back(0); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | //////////////////////////////////////////////////////////////////// |
---|
| 57 | // The copy() function copies an existing entry |
---|
| 58 | // Its arguments are : |
---|
| 59 | // - source : the transaction tab entry to copy |
---|
| 60 | //////////////////////////////////////////////////////////////////// |
---|
| 61 | void copy(const TransactionTabEntry &source) |
---|
| 62 | { |
---|
| 63 | valid = source.valid; |
---|
| 64 | xram_read = source.xram_read; |
---|
| 65 | nline = source.nline; |
---|
| 66 | srcid = source.srcid; |
---|
| 67 | trdid = source.trdid; |
---|
| 68 | pktid = source.pktid; |
---|
| 69 | proc_read = source.proc_read; |
---|
| 70 | single_word = source.single_word; |
---|
| 71 | word_index = source.word_index; |
---|
| 72 | wdata_be.assign(source.wdata_be.begin(),source.wdata_be.end()); |
---|
| 73 | wdata.assign(source.wdata.begin(),source.wdata.end()); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | //////////////////////////////////////////////////////////////////// |
---|
| 77 | // The print() function prints the entry |
---|
| 78 | //////////////////////////////////////////////////////////////////// |
---|
| 79 | void print(){ |
---|
| 80 | std::cout << "valid = " << valid << std::endl; |
---|
| 81 | std::cout << "xram_read = " << xram_read << std::endl; |
---|
| 82 | std::cout << "nline = " << nline << std::endl; |
---|
| 83 | std::cout << "srcid = " << srcid << std::endl; |
---|
| 84 | std::cout << "trdid = " << trdid << std::endl; |
---|
| 85 | std::cout << "pktid = " << pktid << std::endl; |
---|
| 86 | std::cout << "proc_read = " << proc_read << std::endl; |
---|
| 87 | std::cout << "single_word = " << single_word << std::endl; |
---|
| 88 | std::cout << "word_index = " << word_index << std::endl; |
---|
| 89 | for(size_t i=0; i<wdata_be.size() ; i++){ |
---|
| 90 | std::cout << "wdata_be [" << i <<"] = " << wdata_be[i] << std::endl; |
---|
| 91 | } |
---|
| 92 | for(size_t i=0; i<wdata.size() ; i++){ |
---|
| 93 | std::cout << "wdata [" << i <<"] = " << wdata[i] << std::endl; |
---|
| 94 | } |
---|
| 95 | std::cout << std::endl; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | ///////////////////////////////////////////////////////////////////// |
---|
| 99 | // Constructors |
---|
| 100 | ///////////////////////////////////////////////////////////////////// |
---|
| 101 | |
---|
| 102 | TransactionTabEntry() |
---|
| 103 | { |
---|
| 104 | wdata_be.clear(); |
---|
| 105 | wdata.clear(); |
---|
| 106 | valid=false; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | TransactionTabEntry(const TransactionTabEntry &source){ |
---|
| 110 | valid = source.valid; |
---|
| 111 | xram_read = source.xram_read; |
---|
| 112 | nline = source.nline; |
---|
| 113 | srcid = source.srcid; |
---|
| 114 | trdid = source.trdid; |
---|
| 115 | pktid = source.pktid; |
---|
| 116 | proc_read = source.proc_read; |
---|
| 117 | single_word = source.single_word; |
---|
| 118 | word_index = source.word_index; |
---|
| 119 | wdata_be.assign(source.wdata_be.begin(),source.wdata_be.end()); |
---|
| 120 | wdata.assign(source.wdata.begin(),source.wdata.end()); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | }; // end class TransactionTabEntry |
---|
| 124 | |
---|
| 125 | //////////////////////////////////////////////////////////////////////// |
---|
| 126 | // The transaction tab |
---|
| 127 | //////////////////////////////////////////////////////////////////////// |
---|
| 128 | class TransactionTab{ |
---|
| 129 | typedef uint32_t size_t; |
---|
| 130 | typedef uint32_t data_t; |
---|
| 131 | typedef uint32_t be_t; |
---|
| 132 | |
---|
| 133 | private: |
---|
| 134 | size_t size_tab; // The size of the tab |
---|
| 135 | |
---|
| 136 | data_t be_to_mask(be_t be) |
---|
| 137 | { |
---|
| 138 | data_t ret = 0; |
---|
| 139 | if ( be&0x1 ) { |
---|
| 140 | ret = ret | 0x000000FF; |
---|
| 141 | } |
---|
| 142 | if ( be&0x2 ) { |
---|
| 143 | ret = ret | 0x0000FF00; |
---|
| 144 | } |
---|
| 145 | if ( be&0x4 ) { |
---|
| 146 | ret = ret | 0x00FF0000; |
---|
| 147 | } |
---|
| 148 | if ( be&0x8 ) { |
---|
| 149 | ret = ret | 0xFF000000; |
---|
| 150 | } |
---|
| 151 | return ret; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | public: |
---|
| 155 | TransactionTabEntry *tab; // The transaction tab |
---|
| 156 | |
---|
| 157 | //////////////////////////////////////////////////////////////////// |
---|
| 158 | // Constructors |
---|
| 159 | //////////////////////////////////////////////////////////////////// |
---|
| 160 | TransactionTab() |
---|
| 161 | { |
---|
| 162 | size_tab=0; |
---|
| 163 | tab=NULL; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | TransactionTab(size_t n_entries, size_t n_words) |
---|
| 167 | { |
---|
| 168 | size_tab = n_entries; |
---|
| 169 | tab = new TransactionTabEntry[size_tab]; |
---|
| 170 | for ( size_t i=0; i<size_tab; i++) { |
---|
| 171 | tab[i].alloc(n_words); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | ~TransactionTab() |
---|
| 176 | { |
---|
| 177 | delete [] tab; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | ///////////////////////////////////////////////////////////////////// |
---|
| 181 | // The size() function returns the size of the tab |
---|
| 182 | ///////////////////////////////////////////////////////////////////// |
---|
| 183 | size_t size() |
---|
| 184 | { |
---|
| 185 | return size_tab; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | ///////////////////////////////////////////////////////////////////// |
---|
| 189 | // The init() function initializes the transaction tab entries |
---|
| 190 | ///////////////////////////////////////////////////////////////////// |
---|
| 191 | void init() |
---|
| 192 | { |
---|
| 193 | for ( size_t i=0; i<size_tab; i++) { |
---|
| 194 | tab[i].init(); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | ///////////////////////////////////////////////////////////////////// |
---|
| 199 | // The print() function prints a transaction tab entry |
---|
| 200 | // Arguments : |
---|
| 201 | // - index : the index of the entry to print |
---|
| 202 | ///////////////////////////////////////////////////////////////////// |
---|
| 203 | void print(const size_t index) |
---|
| 204 | { |
---|
| 205 | assert( (index < size_tab) |
---|
| 206 | && "Invalid Transaction Tab Entry"); |
---|
| 207 | tab[index].print(); |
---|
| 208 | return; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | ///////////////////////////////////////////////////////////////////// |
---|
| 212 | // The read() function returns a transaction tab entry. |
---|
| 213 | // Arguments : |
---|
| 214 | // - index : the index of the entry to read |
---|
| 215 | ///////////////////////////////////////////////////////////////////// |
---|
| 216 | TransactionTabEntry read(const size_t index) |
---|
| 217 | { |
---|
| 218 | assert( (index < size_tab) |
---|
| 219 | && "Invalid Transaction Tab Entry"); |
---|
| 220 | return tab[index]; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | ///////////////////////////////////////////////////////////////////// |
---|
| 224 | // The full() function returns the state of the transaction tab |
---|
| 225 | // Arguments : |
---|
| 226 | // - index : (return argument) the index of an empty entry |
---|
| 227 | // The function returns true if the transaction tab is full |
---|
| 228 | ///////////////////////////////////////////////////////////////////// |
---|
| 229 | bool full(size_t &index) |
---|
| 230 | { |
---|
| 231 | for(size_t i=0; i<size_tab; i++){ |
---|
| 232 | if(!tab[i].valid){ |
---|
| 233 | index=i; |
---|
| 234 | return false; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | return true; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | ///////////////////////////////////////////////////////////////////// |
---|
| 241 | // The hit_read() function checks if an XRAM read transaction exists |
---|
| 242 | // for a given cache line. |
---|
| 243 | // Arguments : |
---|
| 244 | // - index : (return argument) the index of the hit entry, if there is |
---|
| 245 | // - nline : the index (zy) of the requested line |
---|
| 246 | // The function returns true if a read request has already been sent |
---|
| 247 | ////////////////////////////////////////////////////////////////////// |
---|
| 248 | bool hit_read(const data_t nline,size_t &index) |
---|
| 249 | { |
---|
| 250 | for(size_t i=0; i<size_tab; i++){ |
---|
| 251 | if(tab[i].valid && (nline==tab[i].nline) && tab[i].xram_read) { |
---|
| 252 | index=i; |
---|
| 253 | return true; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | return false; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /////////////////////////////////////////////////////////////////////// |
---|
| 260 | // The hit_write() function looks if an XRAM write transaction exists |
---|
| 261 | // for a given line. |
---|
| 262 | // Arguments : |
---|
| 263 | // - nline : the index (zy) of the requested line |
---|
| 264 | // The function returns true if a write request has already been sent |
---|
| 265 | /////////////////////////////////////////////////////////////////////// |
---|
| 266 | bool hit_write(const data_t nline) |
---|
| 267 | { |
---|
| 268 | for(size_t i=0; i<size_tab; i++){ |
---|
| 269 | if(tab[i].valid && (nline==tab[i].nline) && !(tab[i].xram_read)) { |
---|
| 270 | return true; |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | return false; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | ///////////////////////////////////////////////////////////////////// |
---|
| 277 | // The write_data_mask() function writes a vector of data (a line). |
---|
| 278 | // The data is written only if the corresponding bits are set |
---|
| 279 | // in the be vector. |
---|
| 280 | // Arguments : |
---|
| 281 | // - index : the index of the request in the transaction tab |
---|
| 282 | // - be : vector of be |
---|
| 283 | // - data : vector of data |
---|
| 284 | ///////////////////////////////////////////////////////////////////// |
---|
| 285 | void write_data_mask(const size_t index, |
---|
| 286 | const std::vector<be_t> &be, |
---|
| 287 | const std::vector<data_t> &data) |
---|
| 288 | { |
---|
| 289 | assert( (index < size_tab) |
---|
| 290 | && "Invalid Transaction Tab Entry"); |
---|
| 291 | assert(be.size()==tab[index].wdata_be.size() |
---|
| 292 | && "Bad data mask in write_data_mask in TransactionTab"); |
---|
| 293 | assert(data.size()==tab[index].wdata.size() |
---|
| 294 | && "Bad data in write_data_mask in TransactionTab"); |
---|
| 295 | |
---|
| 296 | for(size_t i=0; i<tab[index].wdata_be.size() ; i++) { |
---|
| 297 | tab[index].wdata_be[i] = tab[index].wdata_be[i] | be[i]; |
---|
| 298 | data_t mask = be_to_mask(be[i]); |
---|
| 299 | tab[index].wdata[i] = (tab[index].wdata[i] & ~mask) | (data[i] & mask); |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | ///////////////////////////////////////////////////////////////////// |
---|
| 304 | // The set() function registers a transaction (read or write) |
---|
| 305 | // to the XRAM in the transaction tab. |
---|
| 306 | // Arguments : |
---|
| 307 | // - index : index in the transaction tab |
---|
| 308 | // - xram_read : transaction type (read or write a cache line) |
---|
| 309 | // - nline : the index (zy) of the cache line |
---|
| 310 | // - srcid : srcid of the initiator that caused the transaction |
---|
| 311 | // - trdid : trdid of the initiator that caused the transaction |
---|
| 312 | // - pktid : pktid of the initiator that caused the transaction |
---|
| 313 | // - proc_read : does the initiator want a copy |
---|
| 314 | // - single_word : single word read (in case of processor read) |
---|
| 315 | // - word_index : index in the line (in case of single word read) |
---|
| 316 | // - data : the data to write (in case of write) |
---|
| 317 | // - data_be : the mask of the data to write (in case of write) |
---|
| 318 | ///////////////////////////////////////////////////////////////////// |
---|
| 319 | void set(const size_t index, |
---|
| 320 | const bool xram_read, |
---|
| 321 | const data_t nline, |
---|
| 322 | const size_t srcid, |
---|
| 323 | const size_t trdid, |
---|
| 324 | const size_t pktid, |
---|
| 325 | const bool proc_read, |
---|
| 326 | const bool single_word, |
---|
| 327 | const size_t word_index, |
---|
| 328 | const std::vector<be_t> &data_be, |
---|
| 329 | const std::vector<data_t> &data) |
---|
| 330 | { |
---|
| 331 | assert( (index < size_tab) |
---|
| 332 | && "The selected entry is out of range in set() Transaction Tab"); |
---|
| 333 | assert(data_be.size()==tab[index].wdata_be.size() |
---|
| 334 | && "Bad data_be argument in set() TransactionTab"); |
---|
| 335 | assert(data.size()==tab[index].wdata.size() |
---|
| 336 | && "Bad data argument in set() TransactionTab"); |
---|
| 337 | |
---|
| 338 | tab[index].valid = true; |
---|
| 339 | tab[index].xram_read = xram_read; |
---|
| 340 | tab[index].nline = nline; |
---|
| 341 | tab[index].srcid = srcid; |
---|
| 342 | tab[index].trdid = trdid; |
---|
| 343 | tab[index].pktid = pktid; |
---|
| 344 | tab[index].proc_read = proc_read; |
---|
| 345 | tab[index].single_word = single_word; |
---|
| 346 | tab[index].word_index = word_index; |
---|
| 347 | for(size_t i=0; i<tab[index].wdata.size(); i++) { |
---|
| 348 | tab[index].wdata_be[i] = data_be[i]; |
---|
| 349 | tab[index].wdata[i] = data[i]; |
---|
| 350 | } |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | ///////////////////////////////////////////////////////////////////// |
---|
| 354 | // The write_rsp() function writes a word of the response to an |
---|
| 355 | // XRAM read transaction. |
---|
| 356 | // The data is only written when the corresponding BE field is Ox0. |
---|
| 357 | // Arguments : |
---|
| 358 | // - index : the index of the transaction in the transaction tab |
---|
| 359 | // - word_index : the index of the data in the line |
---|
| 360 | // - data : the data to write |
---|
| 361 | ///////////////////////////////////////////////////////////////////// |
---|
| 362 | void write_rsp(const size_t index, |
---|
| 363 | const size_t word, |
---|
| 364 | const data_t data) |
---|
| 365 | { |
---|
| 366 | assert( (index < size_tab) |
---|
| 367 | && "Selected entry out of range in write_rsp() Transaction Tab"); |
---|
| 368 | assert( (word <= tab[index].wdata_be.size()) |
---|
| 369 | && "Bad word_index in write_rsp() in TransactionTab"); |
---|
| 370 | assert( tab[index].valid |
---|
| 371 | && "Transaction Tab Entry invalid in write_rsp()"); |
---|
| 372 | assert( tab[index].xram_read |
---|
| 373 | && "Selected entry is not an XRAM read transaction in write_rsp()"); |
---|
| 374 | |
---|
| 375 | data_t mask = be_to_mask(tab[index].wdata_be[word]); |
---|
| 376 | tab[index].wdata[word] = (tab[index].wdata[word] & mask) | (data & ~mask); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | ///////////////////////////////////////////////////////////////////// |
---|
| 380 | // The erase() function erases an entry in the transaction tab. |
---|
| 381 | // Arguments : |
---|
| 382 | // - index : the index of the request in the transaction tab |
---|
| 383 | ///////////////////////////////////////////////////////////////////// |
---|
| 384 | void erase(const size_t index) |
---|
| 385 | { |
---|
| 386 | assert( (index < size_tab) |
---|
| 387 | && "The selected entry is out of range in erase() Transaction Tab"); |
---|
| 388 | tab[index].valid = false; |
---|
| 389 | } |
---|
| 390 | }; // end class TransactionTab |
---|
| 391 | |
---|
| 392 | #endif |
---|