0

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

Kyri Lynx
  • 5
  • 1
  • also, what is the purpose of reading the file? You cannot synthesize it. However you should look into $readmem functions. For TB it might be better to switch to system verilog, there are more features which can put you in a familiar programming domain. In regular verilog testbench you are bound to static arrays (two-dimensions max). – Serge Nov 08 '22 at 15:16

1 Answers1

0

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.

dave_59
  • 39,096
  • 3
  • 24
  • 63
  • Thanks,actually i am trying to train a neural network to recognise hand gestures based on sensor data from thalmic's myo armband and because my text file separates each sensor value with a comma ,i need to able to identify the comma in order to separate each sensor signal to a different variable and feed it as an input to the network. – Kyri Lynx Nov 09 '22 at 12:11
  • I added an example. – dave_59 Nov 09 '22 at 16:29