|
|
1 #include "scv.h"
2
3 // Define a packet with a variable size payload
4 class packetT {
5 public:
6 packetT () {}
7 virtual ~packetT() {}
8 // Packet's data
9 sc_uint<16> src;
10 sc_uint<16> dest;
11 sc_uint<16> length; // Number of items in the payload
12 sc_uint<16> payload[1024];
13 // Define an equal operator
14 packetT& operator=(const packetT& rhs) {
15 src=rhs.src; dest=rhs.dest; length=rhs.length;
16 for(unsigned int i=0; (i<length) && (i<1024); i++)
17 payload[i]=rhs.payload[i];
18 return *this;
19 }
20 // Define a comparison operator
21 friend bool operator==(const packetT& a, const packetT& b) {
22 if (a.src ! = b.src) {return false;}
23 if (a.dest ! = b.dest) {return false;}
24 if (a.length ! = b.length) {return false;}
25 for(unsigned int i=0; (i<a.length) && (i<1024); i++)
26 if (a.payload[i] ! = b.payload[i]) {return false;}
27 return true;
28 }
29 // Define a not-equal operator (some compilers do not
30 // automatically create this from operator=)
31 friend bool operator ! =(const packetT& a, const packetT& b) {
32 if (a.src ! = b.src) {return true;}
33 if (a.dest ! = b.dest) {return true;}
34 if (a.length ! = b.length) {return true;}
35 for(unsigned int i=0; (i<a.length) && (i<1024); i++)
36 if (a.payload[i] ! = b.payload[i]) {return true;}
37 return false;
38 }
39 // Define ostream method to print data
40 friend ostream& operator<< (ostream& os, const packetT& p) {
41 os << " src: " << p.src
42 << " dest: " << p.dest
43 << " length: " << p.length
44 << " payload: " << p.payload[0]
45 << " .. " << p.payload[p.length-1];
46 return os;
47 }
48 };
49
50 // Extensions to packetT
51 template<>
52 class scv_extensions<packetT> : public scv_extensions_base<packetT> {
53 public:
54 scv_extensions< sc_uint<16> > src;
55 scv_extensions< sc_uint<16> > dest;
56 scv_extensions< sc_uint<16> > length;
57 scv_extensions< sc_uint<16>[1024] > payload;
58 SCV_EXTENSIONS_CTOR(packetT) {
59 //must be in order
60 SCV_FIELD(src);
61 SCV_FIELD(dest);
62 SCV_FIELD(length);
63 SCV_FIELD(payload);
64 }
65 };
66
67 SC_MODULE(sctop) {
68 SC_CTOR(sctop);
69 };
70
71 sctop::sctop(sc_module_name name) : sc_module(name)
72 {
73 // Create a sparse packet array
74 packetT defaultPacket;
75 sc_int<64> upper_bound;
76 upper_bound = ( ( (long long) 1 ) << 63 ) - 1;
77 const sc_int<64> lower_bound = -(upper_bound+1);
78 scv_sparse_array< sc_int<64>, packetT >
79 packets("packets", defaultPacket,
80 lower_bound, upper_bound);
81 // Create an index to the sparse packet array
82 scv_smart_ptr< sc_int<64> > index;
83 const sc_int<64> low = sc_int<64>(-1e3);
84 const sc_int<64> high = sc_int<64>(-1e2);
85 index->keep_only(low, high); // Only 901 possible values
86 // Create packets and copy into array
87 scv_smart_ptr<packetT> packet;
88 packet->length.keep_only(10,1024);
89 for (unsigned int i=0; i<10; ++i) {
90 packet->src.next();
91 packet->dest.next();
92 packet->length.next();
93 for(unsigned int j=0; j<packet->length; j++) {
94 packet->payload[j].next();
95 }
96 index->next();
97 packets[index->read()] = *packet; // Uses equal operator
98 }
99 // Iterate through the range looking for packets in the
100 // sparse array
101 const unsigned int packetsPerLine = 50;
102 unsigned int count = 0;
103 for (sc_int<64> i=low; i<=high; i++) {
104 if (count == 0) {
105 scv_out << i << ": ";
106 }
107 if (packets[i] == defaultPacket) { // uses packetT == operator
108 scv_out << ".";
109 } else {
110 scv_out << "*";
111 }
112 if (++count == packetsPerLine) {
113 scv_out << endl;
114 count = 0;
115 }
116 }
117 // Iterate through again, this time printing each packet
118 scv_out << endl << endl;
119 for (sc_int<64> i=low; i<=high; i++) {
120 if (packets[i] ! = defaultPacket) { // uses packetT == operator
121 // Uses output operator
122 scv_out << i << ": " << packets[i] << endl;
123 }
124 }
125 scv_out << endl;
126 }
127
128 int sc_main(int argc, char** argv) {
129 sctop top("top");
130 sc_start();
131 return 0;
132 }
You could download file scv_sparse_array.cpp here
|