Domanda

I am writing some code that requires me to read a file and then use it as input to the SystemC module. What this means is that I should read a file, say, abc.xxx, and then send its content in the binary form to a port (another way to interpret this: I wanna read a file into the buffer and send this buffer through a SystemC port). My questions are:

1) I could use a text file as my input. Read the text file, and store its text contents in a string (which would be enough for my purposes). But how would I send this string through a systemC port?

2) The above solution is only for text files, where I wanna use the text content of a text file. But like my original query, what would I do when I just want to transfer the file through the port?

I apologize if my query isn't completely clear. Thanks in advance!!

È stato utile?

Soluzione

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<bool> rdy to check if the receiving module is ready to receive new data.
  • a valid signal: sc_out<bool> vld to signal to the receiving module that new data is on the port.
  • and the data port sc_out<sc_int<8>> datap to send the bytes. Watch out with using protected word "data".

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top