Search code examples
verilogfpgavivado

Verilog Data Casting


I know it might seems a bit dumb for a question but i had to learn verilog by my own and sometimes i get a little confused about the basic things, so my question is ,when i read a file in verilog with signed decimals inside and special characters like commas,when i store the line i am reading firstly does the type of data matter? Like if i store it as an integer will the data be converted as a string of ascii characters?And if i store it as a reg type will it be automatically converted to a binary string? Sorry if this looks dumb but i am a little confused about how verilog is processing external data Thanks!

I am trying to read a file with sensor data and to put it in a reg type ,the maximum total characters in a line of the file are 25 ,so i've assigned the width of my variable as 8*25 but because of my question above i am not sure how to progress with regarding the manipulation of my data


Solution

  • If you read the file using $fscanf, the file is treated as ASCII text and will be converted to a binary string if you use any of the integral formatting specifiers like %d for signed decimal and %h for hexadecimal. For example, suppose you have the file text:

    123,-456, 789
    -987, 654, -321
    

    And the code

    module top;
      
      integer A,B,C;
      integer file, code;
      initial begin
        file = $fopen("text","r");
        repeat(2) begin
          code = $fscanf(file,"%d,%d,%d",A,B,C);
          $display(code,,A,,B,,C);
        end
      end
    endmodule
    

    This displays

    # run -all
    #           3         123        -456         789
    #           3        -987         654        -321
    # exit
    

    If you need to read the file as an ASCII string (.i.e the ASCII character "0" is the binary string '00110000') then you would use the $s format specifier which would read the entire line. I doubt that is what you want to do.