Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sign extension in verilog

15,480 views
Skip to first unread message

rekz

unread,
Jan 27, 2010, 6:23:36 PM1/27/10
to
I would like to design a 16 to 32-bit sign extension unit. I would
like to preserve the number's sign (positive/negative). So I guess
after I append digits to the MSB then I would need to take care of the
sign's, how could I do this in verilog?

rekz

unread,
Jan 27, 2010, 6:30:27 PM1/27/10
to

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

David Rogoff

unread,
Jan 27, 2010, 7:07:30 PM1/27/10
to

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

rekz

unread,
Jan 27, 2010, 7:36:50 PM1/27/10
to

And doing this will preserve the sign of the numbers?? Why?

Cary R.

unread,
Jan 27, 2010, 7:44:28 PM1/27/10
to
David Rogoff wrote:
> On 2010-01-27 15:30:27 -0800, rekz said:
>
>> On Jan 27, 4:23 pm, rekz <aditya15...@gmail.com> wrote:
>> I would like to design a 16 to 32-bit sign extension unit. I would
>> like to preserve the number's sign (positive/negative). So I guess
>> after I append digits to the MSB then I would need to take care of the
>> sign's, how could I do this in verilog?
>>
>> 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.

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

Cary R.

unread,
Jan 27, 2010, 7:47:44 PM1/27/10
to

Read up on twos-complement numbers!

Cary

Jonathan Bromley

unread,
Jan 28, 2010, 6:11:59 AM1/28/10
to
On Wed, 27 Jan 2010 16:44:28 -0800, "Cary R." wrote:

>> 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

Jan Decaluwe

unread,
Jan 28, 2010, 10:23:57 AM1/28/10
to

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

Cary R.

unread,
Jan 28, 2010, 1:23:06 PM1/28/10
to
Jonathan Bromley wrote:

> 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

Cary R.

unread,
Jan 28, 2010, 1:33:23 PM1/28/10
to
rekz wrote:

> 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

Aswin

unread,
Jan 19, 2011, 8:49:56 PM1/19/11
to
am not getting the waveform in a proper way...it says zzzz and yyy something like this...so is there anything to do with the wire instructions???


0 new messages