Systemc and SCV
Based on SCV example of ex_07_seedfile, I created an example, which
generates
an ETHERNET packet
and randomizes some of its fields.
-
This is a simple exercise to generate a packet and controlling its field
randomization using SCV.
-
The example, from systemc installation, creates some types for randomization,
using SCV. The example also uses a read seed file, if one exists in the
Present Working Directory and so does my example.
Once the MAC addresses are randomized the packet length is randomized. The
resulted length is used to resize a vector for the payload. The payload can be
of constant data or random.
The SCV structures are shown below:
#define ADD_SZ 6
//enum for MAC addresses
struct byte_t {
unsigned char b;
};
struct pkt_mac_t {
unsigned char dst_add[ADD_SZ];
unsigned char src_add[ADD_SZ];
sc_uint<16> len;
};
Next comes the SCV extension. This is very much the same as the SCV example, so
I skip its description. The main test code has two parts. The first one
will be discussed
in detail. This part is the creation of
the packet class.
Its usage and the seed manipulation is quite the same as the SCV example.
class c_pkt_mac {
public:
unsigned char my_add[ADD_SZ];
std::vector<unsigned char> payload;
scv_smart_ptr<pkt_mac_t> data_p;
c_pkt_mac(unsigned char a[ADD_SZ], bool Rand, unsigned char val);
void prt_my_add();
void prt_pkt_mac();
void prt_payload(unsigned pr_len);
};
void c_pkt_mac::prt_my_add() {
scv_out << "packet my address:" << endl;
for(int i=0; i<ADD_SZ; i++) {
scv_out << std::hex << (unsigned)my_add[i] << " ";
}//for
scv_out << "-----------------" << endl;
};
...
c_pkt_mac::c_pkt_mac(unsigned char a[ADD_SZ], bool Rand, unsigned char val) {
//mac address
for(int i=0; i<ADD_SZ; i++) {
my_add[i]=a[i];
}//for
//generate packet's fields
data_p->len.keep_only(50, 1024); //what ever max payload is ?
data_p->next();
//generate packet payload
payload.resize(data_p->len, val);
if(Rand) {
scv_smart_ptr<byte_t> data_b("byte");
data_b->b.keep_only(0, 0x80);
for(unsigned i=0; i<data_p->len; i++) {
data_b->next();
payload[i]=data_b->b;
}
}
};
-
The print out of this program is:
Using seedfile.txt...
packet print:
packet my address:
1 2 3 4 5 6
-----------------
packet mac:
dst_add[0] = df
src_add[0] = 10
dst_add[1] = fa
src_add[1] = 6
dst_add[2] = 81
src_add[2] = 44
dst_add[3] = 19
src_add[3] = 31
dst_add[4] = 1f
src_add[4] = ac
dst_add[5] = 9b
src_add[5] = 89
len = 762 (value is decimal)
-----------------
packet payload (print only 16 bytes):
58 69 2f 6c 1a 63 22 60 d 6b 13 59 29 69 3c 7a
-----------------
-
The compilation can be done using the make file from the example.
If one wishes,
however, to do it by a script then the following can help. First the variable
LD_LIBRARY_PATH.
My machine is a 32 bit so path is to 32 library:
/mnt/Home/pini/Home_1/pini/systemc_2.2.0/lib-linux
#!/bin/bash
SYSTEMC="/mnt/Home/pini/Home_1/pini/systemc_2.2.0"
if [ -e test.o ] ; then
rm -f test.o
fi
if [ -e test.exe ] ; then
rm -f test.exe
fi
g++ -I$SYSTEMC/include -fPIC -c -g -Wall -O2 test.cc || echo "compilation failed"
ls test.o && g++ test.o -L$SYSTEMC/lib-linux -lsystemc -lscv -lsystemc -o test.exe
-
The code is available
from here.
|