is it just as simple as this:
module SignExtension(a, result);
input [15:0] a; // 16-bit input
output [31:0] result; // 32-bit output
assign result = { 16{a[31]}, a };
endmodule
This should be assign result = { 16{a[15]}, a };
Basically, you just take the MSB of the input and repeat it to fill to the left.
endmodule
And doing this will preserve the sign of the numbers?? Why?
Or use a signed right expression and the tools should take care of it
for you. There can be some subtlety with this, but it does work when you
understand the rules.
Cary
Read up on twos-complement numbers!
Cary
>> Basically, you just take the MSB of the input and
>> repeat it to fill to the left.
>
>Or use a signed right expression and the tools should take
>care of it for you. There can be some subtlety with this,
Nominated for Understatement Of The Year.
> but it does work when you understand the rules.
And spectacularly and mysteriously fails to work when you
don't, or when you do understand but you forget one of
the many Gotchas.
I would very strongly urge beginners to steer clear of
Verilog signed arithmetic - especially beginners like
the OP whose grasp of twos-complement seems shaky :-)
--
Jonathan Bromley
Correct advice, but now those poor beginners must be
really confused :-) They might intuitively think that
handling negative integers is a common task that should
be simple.
My advice to them: think outside the box for a while,
before attempting to become a Verilog guru. Perhaps your
intuition is right, and it's the HDL language designers
that have it all wrong. Perhaps there is a better way.
To start thinking outside the box, read this:
http://www.jandecaluwe.com/hdldesign/counting.html
--
Jan Decaluwe - Resources bvba - http://www.jandecaluwe.com
Python as a HDL: http://www.myhdl.org
VHDL development, the modern way: http://www.sigasi.com
Analog design automation: http://www.mephisto-da.com
World-class digital design: http://www.easics.com
> And spectacularly and mysteriously fails to work when you
> don't, or when you do understand but you forget one of
> the many Gotchas.
Been there seen that, but sometime not using it is even more painful.
For example in my current design I have a signed signal that represents
a correction value as 0.2dB per step. This needs to be expanded to
0.15dB per step with the correct rounding. Then because of a mistake in
the polarity of the analog system I need to negate the result. With
signed registers and arithmetic this isn't too bad to look at and
understand. Well the rounding of negative values is a bit complicated,
but that's completely independent and I added ample comments. ;-)
> I would very strongly urge beginners to steer clear of
> Verilog signed arithmetic - especially beginners like
> the OP whose grasp of twos-complement seems shaky :-)
I won't disagree with this, but they need to understand that
alternatives exist. Otherwise they won't understand perfectly valid and
working code.
Cary
> assign result = { 16{a[31]}, a };
As a side note this is invalid replication syntax. This should be
written as.
assign result = { {16{a[31]}}, a };
Notice the replication is enclosed in {}. cver does accept this invalid
syntax, but don't count on it being portable.
Cary