FPGA Central - World's 1st FPGA / CPLD Portal

FPGA Central

World's 1st FPGA Portal

 

Go Back   FPGA Groups > NewsGroup > Verilog

Verilog comp.lang.verilog newsgroup / usenet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-22-2007, 07:51 PM
Mahurshi Akilla
Guest
 
Posts: n/a
Default 16 to 32 bit Sign Extention

I am doing 16 to 32 bit sign extention like below. I tested it out
and it works great. I know it is just 2 assign statements, but I am
wondering if there is a better way of doing this..

module sign_extender(in, out);
input [15:0] in;
output [31:0] out;
assign out[31:16] = {16{in[15]}};
assign out[15:0] = in[15:0];
endmodule

Mahurshi Akilla

Reply With Quote
  #2 (permalink)  
Old 04-22-2007, 11:15 PM
Jonathan Bromley
Guest
 
Posts: n/a
Default Re: 16 to 32 bit Sign Extention

On 22 Apr 2007 10:51:18 -0700, Mahurshi Akilla <[email protected]>
wrote:

>I am doing 16 to 32 bit sign extention like below. I tested it out
>and it works great. I know it is just 2 assign statements, but I am
>wondering if there is a better way of doing this..
>
>module sign_extender(in, out);
>input [15:0] in;
>output [31:0] out;
> assign out[31:16] = {16{in[15]}};
> assign out[15:0] = in[15:0];
>endmodule


Yes, there is. Use Verilog-2001 signed arithmetic.

module sign_extender (
input signed [15:0] in,
output signed [31:0] out);

assign out = in; // sign-extends

endmodule

Indeed, because of the Verilog-2001 signed arithmetic
features, you probably don't need the sign_extender
module at all. You can simply use signed arithmetic
wherever you need it.

NOTE VERY CAREFULLY, however, that sign extension
works only if EVERY operand in an arithmetic expression
is signed. If there is ANY unsigned operand in the
expression, then the WHOLE expression is evaluated in
traditional Verilog-95 unsigned fashion, without sign
extension. A big Gotcha!.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
[email protected]
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
Reply With Quote
  #3 (permalink)  
Old 04-23-2007, 06:07 PM
unfrostedpoptart
Guest
 
Posts: n/a
Default Re: 16 to 32 bit Sign Extention

On Apr 22, 10:51 am, Mahurshi Akilla <mahur...@gmail.com> wrote:
> I am doing 16 to 32 bit sign extention like below. I tested it out
> and it works great. I know it is just 2 assign statements, but I am
> wondering if there is a better way of doing this..
>
> module sign_extender(in, out);
> input [15:0] in;
> output [31:0] out;
> assign out[31:16] = {16{in[15]}};
> assign out[15:0] = in[15:0];
> endmodule
>
> Mahurshi Akilla


assign out = { {16{in[15]}, in[15:0] };

Reply With Quote
  #4 (permalink)  
Old 04-24-2007, 06:56 PM
mk
Guest
 
Posts: n/a
Default Re: 16 to 32 bit Sign Extention

On 22 Apr 2007 10:51:18 -0700, Mahurshi Akilla <[email protected]>
wrote:

>I am doing 16 to 32 bit sign extention like below. I tested it out
>and it works great. I know it is just 2 assign statements, but I am
>wondering if there is a better way of doing this..
>
>module sign_extender(in, out);
>input [15:0] in;
>output [31:0] out;
> assign out[31:16] = {16{in[15]}};
> assign out[15:0] = in[15:0];
>endmodule


assign out = {{16{in[15]}}, in[15:0]};
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:20 AM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Copyright 2008 @ FPGA Central. All rights reserved