Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

writing values of a variable to a file using verilog HDL

Status
Not open for further replies.

dll_fpga

Full Member level 3
Full Member level 3
Joined
Mar 9, 2011
Messages
185
Helped
19
Reputation
38
Reaction score
21
Trophy points
1,298
Location
kerala,India
Activity points
2,416
how to write the values of a variable z to a text file in decimal format?
i'm using the following verilog code
integer fp1;
fp1=$fopen("ctq.txt");
$fwrite(fp1,"%d\n",z);
but the values of the variable z are still written to the file "ctq" in hexadecimal format
......but i need the values to be written to the file "ctq" in decimal format
how can this be done?
 

That would be really odd.... %d should really print decimal format. Just by way of sanity check...

Code:
integer fp1;
fp1=$fopen("ctq.txt");
$fwrite(fp1,"%d\n",z);
$fwrite(fp1,"Are we sure ... %d\n", 42);
$fclose(fp1);

So do you get 42 there, or 2A?
 
Last edited:
OK try this one

Code:
module fileop();
  integer fp1;
  reg [11:0]z;
 initial begin 
fp1=$fopen ("ctq.txt","w");
z=12'h F_C_F;
$fwrite(fp1,"%d\n",z);
$fclose(fp1);  
end 
endmodule
 
That would be really odd.... %d should really print decimal format. Just by way of sanity check...

Code:
integer fp1;
fp1=$fopen("ctq.txt");
$fwrite(fp1,"%d\n",z);
$fwrite(fp1,"Are we sure ... %d\n", 42);
$fclose(fp1);

So do you get 42 there, or 2A?

ok...fibble i got it .but i need the numbers to be written to the file in signed format .how can this be done?
integer fp1;
fp1=$fopen("ctq.txt");
$fwrite(fp1,"%d\n",z);

using %d is giving the results in unsigned format....but i need the values to be written in signed format....
 
Last edited:
That's a different question altogether. :p

AFAIK the moment your precious data resides in a reg or wire, you are boned. By the time the $fwrite occurs, the tool no longer knows about it being signed or unsigned.

The "integer" datatype however is signed, and will print negative numbers using %d. So as long as your data fits in a 32-bit signed, then what you could do is take your wire/reg, typecast it into an integer, and then $fwrite that integer.

---------- Post added at 20:39 ---------- Previous post was at 20:32 ----------

Did a quick test for a 32-bit wire, and works just fine.
 

That's a different question altogether. :p

AFAIK the moment your precious data resides in a reg or wire, you are boned. By the time the $fwrite occurs, the tool no longer knows about it being signed or unsigned.

The "integer" datatype however is signed, and will print negative numbers using %d. So as long as your data fits in a 32-bit signed, then what you could do is take your wire/reg, typecast it into an integer, and then $fwrite that integer.

thanks fibble.the variable z is wire type .
ie:wire [5:0] z;
and i need the signed value of z to be written to a text file....
currently i'm using the following code
wire [5:0] z;
integer fp1;
fp1=$fopen("ctq.txt");
$fwrite(fp1,"%d\n",z);
what is the change i need to make?
can you show how can this be done using typical codes?
 

add something like:

Code:
integer signed_z;

...

signed_z = (z);
$fwrite(fp1,"regular z: %d\n", z);
$fwrite(fp1,"signed z: %d\n", signed_z);


But note that I tested it with 32-bits wire on purpose, that being same size operands. The integer is 32-bits as well. I have not tested it with size different from 32-bits. That's your job. :p

---------- Post added at 20:49 ---------- Previous post was at 20:44 ----------

Okay, now I did test it. And it does work for 32 bit. And does NOT work for 6 bit like your case, without some extra work.

So what you need to do is take your signed 6-bit "z", and extend it to a signed 32-bit version. And apply the above typecast trick.
 

ok...fibble i got it .
but i need the numbers to be written to the file in signed format
.how can this be done?
integer fp1;
fp1=$fopen("ctq.txt");
$fwrite(fp1,"%d\n",z);

using %d is giving the results in unsigned format....but i need the values to be written in signed format....


h**p://www.sutherland-hdl.com/papers/2000-HDLCon-paper_Verilog-2000.pdf
page 4

Verilog-2001 adds two new system functions,
$signed and $unsigned. These system functions are used
to convert an unsigned value to signed

Once you declare the variable as integer .it should be signed

verify with this code just modified Mrflibbbles code to illustrate the fact
no separate format specifier is needed %d is enough if you declared variable as integer .


Code:
module trb();
integer fp1;
integer  i=32'h FFFFFFFF;
initial	begin
 
fp1=$fopen("ctq.txt"); 
$fwrite(fp1,"Are we sure ... %d\n",i); 	

i=32'h 7FFF_FFFF;
$fwrite(fp1,"Are we sure ... %d\n",i); 

$fclose(fp1);



end 
endmodule

Are we sure ... -1
Are we sure ... 2147483647


In first fwrite i is having negative sign as the MSB is 1 ,so the particular number is the 2's complement of another number (-1) .

In second one i is having Msb 0 that means a postive number.
 
Last edited:
add something like:

Code:
integer signed_z;

...

signed_z = (z);
$fwrite(fp1,"regular z: %d\n", z);
$fwrite(fp1,"signed z: %d\n", signed_z);


But note that I tested it with 32-bits wire on purpose, that being same size operands. The integer is 32-bits as well. I have not tested it with size different from 32-bits. That's your job. :p

---------- Post added at 20:49 ---------- Previous post was at 20:44 ----------

Okay, now I did test it. And it does work for 32 bit. And does NOT work for 6 bit like your case, without some extra work.

So what you need to do is take your signed 6-bit "z", and extend it to a signed 32-bit version. And apply the above typecast trick.

this line is giving me an error...
signed_z = (z);

---------- Post added at 09:54 ---------- Previous post was at 09:32 ----------

h**p://www.sutherland-hdl.com/papers/2000-HDLCon-paper_Verilog-2000.pdf
page 4

Verilog-2001 adds two new system functions,
$signed and $unsigned. These system functions are used
to convert an unsigned value to signed

Once you declare the variable as integer .it should be signed

verify with this code just modified Mrflibbbles code to illustrate the fact
no separate format specifier is needed %d is enough if you declared variable as integer .


Code:
module trb();
integer fp1;
integer  i=32'h FFFFFFFF;
initial	begin
 
fp1=$fopen("ctq.txt"); 
$fwrite(fp1,"Are we sure ... %d\n",i); 	

i=32'h 7FFF_FFFF;
$fwrite(fp1,"Are we sure ... %d\n",i); 

$fclose(fp1);



end 
endmodule

Are we sure ... -1
Are we sure ... 2147483647


In first fwrite i is having negative sign as the MSB is 1 ,so the particular number is the 2's complement of another number (-1) .

In second one i is having Msb 0 that means a postive number.

thanks blooz....i was able to solve it....
wire signed [5:0] z; //see page 4 of the pdf file given by blooz
integer fp1;
fp1=$fopen("ctq.txt");
$fwrite(fp1,"%d\n",z);

output
^^^^

37
-6
3
-1
1
-1
0
0
0
-1
0
-1
0
0
-1
0
-1
0
-1
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top