If I am understanding it right you would like to take input from a file in one SC_MODULE
and send it to another SC_MODULE
. You give no constraints, so assuming you only want to send text using binary ASCII coding, you can use a sc_port unsigned integer with a 8 bit width: sc_uint<8>
to send the string character by character.
Procedure for the sending module.
For a correct hardware transmission you need some handshaking signals:
- a ready signal:
sc_in<
to check if the receiving module is ready to receive new data.bool
> rdy - a valid signal:
sc_out<
to signal to the receiving module that new data is on the port.bool
> vld - and the data port
sc_out<
to send the bytes. Watch out with using protected word "data".sc_int<8>
> datap
Now let's assume you want to send a textfile with 10 characters, I think a for-loop will be the most easy and intuitive way to do this. Put this code in a SC_THREAD because it contains control logic you only want to execute once. Assuming a clockperiod of 10 ns for this design, you will get something like this:
// open file for reading
std::ifstream fp_in;
fp_in.open("data.txt", ios::in | ios::binary);
if (fp_in.is_open())
{
cout << "file opened for reading" << endl;
fp_in.seekg(0, ios::beg ); // put file pointer to beginning of file
}
else {
cout << "Unable to open data.txt for reading";
}
// read data from file and send
const int DATALENGHT = 10;
char buffer;
for(int i=0; i<DATALENGHT; ++i)
{
while(rdy.read() != true) wait(10, SC_NS); // wait untill receiver is ready
fp_in.read(buffer, 1);
datap.write(buffer);
vld.write(true);
wait(10, SC_NS); // wait one clockcycle for the receiver to read it.
vld.write(false);
datap.write(0);
}
fp_in.close();