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.

[SOLVED] Reading negative hex values in Verilog!Plz HELP

Status
Not open for further replies.

UFK

Member level 3
Joined
Mar 8, 2010
Messages
60
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Pakistan
Activity points
1,728
Hi All

In my code im using $readmemh to read hex values from a text file in which alot of the values are negative. Verilog is treating them as positive and returning erroneous results. Can someone please help me deal with this problem? I really need to figure out a way to recognize these negative hex values.
Please help
 

fcfusion

Full Member level 4
Joined
Mar 6, 2010
Messages
207
Helped
43
Reputation
86
Reaction score
24
Trophy points
1,298
Activity points
2,826
When dealing with negative numbers in verilog (either decimal or hex) you must indicate explicitly that you are using signed numbers. To do that you just have to declare your variables as signed:

"reg signed [7:0] var"

Do the same for input or output signals:

"input signed [7:0] var"
 

UFK

Member level 3
Joined
Mar 8, 2010
Messages
60
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Pakistan
Activity points
1,728
Thankyou so much.

I tried what u suggested but it still wont work correctly for neg values.
I am posting my code below along with the text file its supposed to read. Perhaps you could run it to see what can be fixed in it.

1. This code should compare text file values with the constant T.
2. If greater than T, then move to LSP else do nothing

The code works fine if text file has positive hex values only, but with neg as in my case its not giving correct results.

My code is
module test();

reg [31:0] Signbit_ctr;
reg [31:0] Bit_ctr0;
reg [31:0] Bit_ctr1;

reg[31:0] T=32'h00001000;

integer i;
integer k;

reg signed [31:0] LIP [0:3];
reg signed [31:0] LSP [0:1023];


initial $readmemh("D16.txt",LIP);

initial
begin

Signbit_ctr = 0;
Bit_ctr0 = 0;
Bit_ctr1 = 0;

end


always @ (LIP)
begin
for(i=0;i<=3;i=i+1)

if (LIP >= T)

begin

LSP = LIP;

Bit_ctr1 = Bit_ctr1 + 1;
Signbit_ctr = Signbit_ctr + 1;

end
else
begin

Bit_ctr0 = Bit_ctr0 + 1;

$display("%d:%h",i,LSP);

end

end
endmodule





and the text file D16.txt is

12F2
FFFFFD3E
FFFFFF78
FF

Thanks alot in advance. And thankyou for all the previous help too. :)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top