How use value of string read from file

Viewed 260

I state that I am new in verilog.

I'm trying to make a testbench to test a design.

In the file I have for each line a set of <key, ciphertext, plaintext> values.

Values within the file look like this:

| 128'h2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A | 64'h2A2A2A2A2A2A2A2A | 64'hC30D63F15BA8B687 |

What I've done so far (probably wrong) is open the file and read it as if it were a string:

while (! $ feof (outfile0)) begin // read until an "end of file" is reached.
   @ (posedge clk);
   $ fgets (A, outfile0);
   ctxt = A[184:16];
   key= A[688:392];

What I would like to achieve is the assignment

key = 128'h2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A2A

but if I do

key = A [688: 392];

I can't get the expected value by printing the key

I think the problem is that I am assigning a string to the key and not the hexadecimal value read from the file, however I don't know how I can solve this problem.

Thanks everyone!

Edit - example code:

reg  [63:0] ctxt;
reg  [127:0] key;
wire [63:0] ptxt;

integer outfile0;
reg [728:0] A;

begin: READ_FILE
        outfile0=$fopen("test.txt","r");
        while (! $feof(outfile0)) begin
                    @(posedge clk);
                    $fgets(A, outfile0);
                    #10;
                    ctxt = A[184:16];
                    key=A[688:392];
        end
        $fclose(outfile0);

end

Test.txt example:

| 128'h00000000000000000000000000000000 | 64'h0000000000000008 | 64'h2FFCE6557D7DC86C |
| 128'h00000000000000000000000000000000 | 64'h0000000000000008 | 64'h2FFCE6557D7DC86C |
| 128'h00000000000000000000000000000000 | 64'h0000000000000008 | 64'h2FFCE6557D7DC86C |
.
.
.
etc
1 Answers

Use $fscanf to read formatted data instead of $fgets. Formatted data does not understand SystemVerilog number syntax so you can remove it, or make sure is gets skipped in your format string

code = $fscanf(outfile0, "| 128'h%h | 64'h%h | 64'h%h |", key, ctxt, ptxt);
Related