OpenCores
URL https://opencores.org/ocsvn/async_sdm_noc/async_sdm_noc/trunk

Subversion Repositories async_sdm_noc

[/] [async_sdm_noc/] [branches/] [init/] [common/] [tb/] [pdu_def.h] - Blame information for rev 68

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 wsong0210
/*
2
 Asynchronous SDM NoC
3
 (C)2011 Wei Song
4
 Advanced Processor Technologies Group
5
 Computer Science, the Univ. of Manchester, UK
6
 
7
 Authors:
8
 Wei Song     wsong83@gmail.com
9
 
10
 License: LGPL 3.0 or later
11
 
12
 Package definition.
13
 
14
 History:
15
 19/08/2008  Initial version. <wsong83@gmail.com>
16
 04/08/2008  Add the check empty function. <wsong83@gmail.com>
17
 22/09/2008  Override the copy and = operations.  <wsong83@gmail.com>
18
 21/09/2010  Support VC and use templates. <wsong83@gmail.com>
19
 19/11/2010  Fixed to support the minimal 8bit VC. <wsong83@gmail.com>
20
 27/05/2011  Clean up for opensource. <wsong83@gmail.com>
21 32 wsong0210
 30/05/2011  Clear the addresses field when clear a flit. <wsong83@gmail.com>
22 30 wsong0210
 
23
*/
24
 
25
#ifndef PDU_DEF_H_
26
#define PDU_DEF_H_
27
 
28
#include <ostream>
29
#include <iomanip>
30
 
31
using namespace std;
32
 
33
// flit types: data, idle, head and tail
34
enum ftype_t {F_DAT, F_IDLE, F_HD, F_TL};
35
 
36
template<unsigned int BW>
37
class pdu_flit;
38
 
39
// override the method to stream out flits
40
template<unsigned int BW>
41
ostream& operator<< (ostream& os, const pdu_flit<BW>& dd) {
42
  switch(dd.ftype) {
43
  case F_DAT:
44
    os << hex << "<DATA:" << (unsigned int)(dd.vcn) << ":" << (unsigned int)(dd.prio) << ":";
45
    for(unsigned int i=0; i<BW; i++)
46
      os << setw(2) << setfill('0') << (unsigned int)(dd[i]);
47
    os << setw(0) << dec << ">";
48
    break;
49
  case F_HD:
50
    os << hex << "<HEAD:" << (unsigned int)(dd.vcn) << ":" << (unsigned int)(dd.prio) << ":" << (unsigned int)(dd.addrx) << "," << (unsigned int)(dd.addry) << ":";
51
    for(unsigned int i=0; i<BW-1; i++)
52
      os << setw(2) << setfill('0') << (unsigned int)(dd[i]);
53
    os << setw(0) << dec << ">";
54
    break;
55
  case F_TL:
56
    os << hex << "<TAIL:" << (unsigned int)(dd.vcn) << ":" << (unsigned int)(dd.prio) << ":";
57
    for(unsigned int i=0; i<BW; i++)
58
      os  << setw(2) << setfill('0') << (unsigned int)(dd[i]);
59
    os << setw(0) << dec << ">";
60
    break;
61
  case F_IDLE:
62
    os << "<IDLE>" ;
63
    break;
64
  default:
65
    os << "<ERR!>" ;
66
    break;
67
  }
68
 
69
  return os;
70
}
71
 
72
// flit used in NoC communication
73
template<unsigned int BW>
74
class pdu_flit {
75
 public:
76
    unsigned char vcn;
77
    unsigned char prio;
78
    ftype_t ftype;
79
    unsigned int addrx, addry;
80
 private:
81
    unsigned char data [BW];
82
 
83
     void copy(const pdu_flit<BW>& dd) {
84
      vcn = dd.vcn;
85
      prio = dd.prio;
86
      ftype = dd.ftype;
87
      addrx = dd.addrx;
88
      addry = dd.addry;
89
      for(unsigned int i=0; i<BW; i++) data[i] = dd.data[i];
90
    }
91
 
92
public:
93
    pdu_flit()
94
      : vcn(0), prio(0), ftype(F_IDLE)
95
    {
96
      for(unsigned int i=0; i<BW; i++) data[i] = 0;
97
    }
98
 
99
    void clear(){                    // clear the flit
100
      vcn = 0;
101
      prio = 0;
102
      ftype = F_IDLE;
103 32 wsong0210
      addrx = 0;
104
      addry = 0;
105 30 wsong0210
      for(unsigned int i=0; i<BW; i++) data[i] = 0;
106
    }
107
 
108
    unsigned char& operator[] (unsigned int index){         // read as a vector
109
      return data[index];
110
    }
111
 
112
    const unsigned char& operator[] (unsigned int index) const {         // read as a vector
113
      return data[index];
114
    }
115
 
116
    friend ostream& operator<< <BW> (ostream& os, const pdu_flit<BW>& dd);   // output to standard output stream
117
 
118
    pdu_flit(const pdu_flit<BW>& dd){                           // override the default copy operation
119
      copy(dd);
120
    }
121
 
122
    pdu_flit& operator=(const pdu_flit<BW>& dd){                // override the default eque with operation
123
      copy(dd);
124
      return(*this);
125
    }
126
};
127
 
128
//===============================================================================
129
// method to stream out frames
130
template<unsigned int BW>
131
class pdu_frame;
132
 
133
template<unsigned int BW>
134
ostream& operator<< (ostream& os, const pdu_frame<BW>& dd) {
135
  os << hex << "<FRAME:p" << (unsigned int)(dd.prio) << ":a" << (unsigned int)(dd.addrx) << "," << (unsigned int)(dd.addry) << ":s" << dd.fsize << ":";
136
  //  os << setw(2) << setfill('0');
137
  for(unsigned int i=0; i<dd.fsize; i++)
138
    os << setw(2) << setfill('0') << (unsigned int)(dd[i]);
139
  os << setw(0) << dec << ">";
140
  return os;
141
}
142
 
143
// frame definition
144
template<unsigned int BW>
145
class pdu_frame{
146
 
147
 public:
148
  unsigned int addrx, addry;
149
  unsigned char prio;
150
  unsigned int fsize;
151
  int rptr, wptr;
152
 
153
 private:
154
  unsigned char * data;                       // data field
155
 
156
 public:
157
  pdu_frame()
158
    : addrx(0), addry(0), prio(0), fsize(0), rptr(-1), wptr(0), data(NULL)
159
    {}
160
 
161
  pdu_frame(unsigned int fs)
162
    : addrx(0), addry(0), prio(0), fsize(fs), rptr(-1), wptr(0)
163
    {
164
      data = new unsigned char [fs];
165
    }
166
 
167
  ~pdu_frame() {
168
    if(data != NULL) {
169
      delete[] data;
170
      data = NULL;
171
    }
172
  }
173
 
174
  void clear() {
175
    rptr = -1;
176
    wptr = 0;
177
  }
178
 
179
  unsigned char&  operator[] (unsigned int index) {
180
    if(index > fsize)   // need to enlarge the buf
181
      resize(index);
182
 
183
    return data[index];
184
  }
185
 
186
  const unsigned char&  operator[] (unsigned int index) const {
187
    return data[index];
188
  }
189
 
190
  bool empty() {
191
    return ((rptr == wptr) || (wptr == 0));
192
  }
193
 
194
  void push(unsigned char dd) {
195
    if(wptr==fsize)
196
      resize(fsize+1);
197
 
198
    data[wptr++] = dd;
199
  }
200
 
201
  unsigned char pop() {
202
    if(empty())
203
      return 0;
204
 
205
    return data[rptr++];
206
  }
207
 
208
  pdu_frame& operator<< (const pdu_flit<BW>& dd) {
209
    switch(dd.ftype) {
210
    case F_DAT:
211
      for(unsigned int i=0; i<BW; i++)
212
        push(dd[i]);
213
      break;
214
    case F_HD:
215
      addrx = dd.addrx;
216
      addry = dd.addry;
217
      prio = dd.prio;
218
      for(unsigned int i=0; i<BW-1; i++)
219
        push(dd[i]);
220
      break;
221
    case F_TL:
222
      for(unsigned int i=0; i<BW; i++)
223
        push(dd[i]);
224
      resize(wptr);
225
      break;
226
    default:
227
      break;
228
    }
229
    return *this;
230
  }
231
 
232
  pdu_frame& operator>> (pdu_flit<BW>& dd) {
233
    if(rptr==-1) {
234
      dd.ftype = F_HD;
235
      rptr++;
236
      for(unsigned int i=0; i<BW-1; i++) {
237
        dd[i] = pop();
238
      }
239
      dd.addrx = addrx;
240
      dd.addry = addry;
241
    } else {
242
      dd.ftype = F_DAT;
243
      for(unsigned int i=0; i<BW; i++) {
244
        dd[i] = pop();
245
      }
246
    }
247
    if(empty())
248
      dd.ftype = F_TL;
249
 
250
    return *this;
251
  }
252
 
253
  //  friend ostream& operator<< <BW> (ostream& os, const pdu_frame<BW>& dd);
254
 
255
  pdu_frame(const pdu_frame<BW>& dd) {
256
    copy(dd);
257
  }
258
 
259
  pdu_frame<BW>& operator=(const pdu_frame<BW>& dd) {
260
    copy(dd);
261
    return (*this);
262
  }
263
 
264
  unsigned int psize() {
265
    unsigned int prac_size = fsize;
266
    while(1) {
267
      if(data[prac_size-1] == 0)
268
            prac_size--;
269
      else
270
            break;
271
    }
272
    return prac_size;
273
  }
274
 
275
  private:
276
  void resize(unsigned int fs) {
277
    // boundry check
278
    if(fs == fsize)
279
      return;
280
 
281
    // resize the buffer
282
    unsigned char * buf = new unsigned char [fs];
283
    for(unsigned int i=0; (i<fsize && i<fs); i++) {
284
      buf[i] = data[i];
285
    }
286
    fsize = fs;
287
    delete[] data;
288
    data = buf;
289
  }
290
 
291
  void copy(const pdu_frame<BW>& dd) {
292
    addrx = dd.addrx;
293
    addry = dd.addry;
294
    prio = dd.prio;
295
    resize(dd.fsize);
296
    rptr = dd.rptr;
297
    wptr = dd.wptr;
298
 
299
    for(unsigned int i=0; i<fsize; i++) {
300
      data[i] = dd.data[i];
301
    }
302
  }
303
 
304
};
305
 
306
 
307
#endif      /* PDU_DEF_H_ */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.