Go Back   Velocity Reviews > Newsgroups > VHDL
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

VHDL - VHDL code for 2's complement

 
Thread Tools Search this Thread
Old 07-28-2003, 07:58 PM   #1
Default VHDL code for 2's complement


I am doing a project for a class and have virtually no VHDL
experience. I need help writing some code that will subtract 2 8-bit
numbers by using either 2's complement or 1's complement. Any help or
guidance would be greatly appreciated. Thanks


nick
  Reply With Quote
Old 07-29-2003, 02:04 AM   #2
Marc Guardiani
 
Posts: n/a
Default Re: VHDL code for 2's complement
ones_complement <= not(subtrahend);
twos_complement <= not(subtrahend) + 1;

I leave the practical application of this up to the student .


nick wrote:
> I am doing a project for a class and have virtually no VHDL
> experience. I need help writing some code that will subtract 2 8-bit
> numbers by using either 2's complement or 1's complement. Any help or
> guidance would be greatly appreciated. Thanks


--


Marc Guardiani

To reply directly to me, use the address given below. The domain name is
phonetic.
fpgaee81-at-eff-why-eye-dot-net



Marc Guardiani
  Reply With Quote
Old 07-29-2003, 09:15 AM   #3
Jonathan Bromley
 
Posts: n/a
Default Re: VHDL code for 2's complement
"nick" <> wrote in message
news: om...

> I am doing a project for a class and have virtually no VHDL
> experience. I need help writing some code that will subtract 2 8-bit
> numbers by using either 2's complement or 1's complement. Any help or
> guidance would be greatly appreciated.


Nick,
as someone else has pointed out, this isn't really a VHDL problem.
First you need to be sure you understand how 1s complement and
2s complement notations work, and how you would do a subtract.
Then you can start to worry about how to code it in VHDL,
which isn't hard. I'm guessing, from your lack of experience,
that this is a fairly elementary class and you are probably
expected to create most of the details of the hardware design,
so simply using the VHDL library subtract function is unlikely
to impress your prof

Here's the deal. You understand how binary numbers work, yes?
So let's play with some 4-bit binary numbers...

five 0101
three 0011
(subtract)----
two 0010

But just for grins, let's try something else...

five 0101
thirteen 1101
(add)----
two 0010 whoops, I threw away the carry-16

Is this a useful insight? Yes, because we already know how
to make an adder (you do, don't you?!) so if we can turn
3 into 13 in some simple way, we don't need to create
special hardware to make a subtracter. Of course, the
fake-subtraction I did above works because:
a) 13 = 16-3
b) I threw away the carry-16 out from the addition

So we can subtract using just an adder, if only we
can work out how to do the "16-n" calculation. It
turns out that this is really easy to do in binary,
if you rewrite it as "(15-n)+1"...

fifteen 1111
three 0011
(subtract)----
1100 Sheesh, all I had to do was invert every bit!

And the +1 usually comes for free, by utilising the carry-in
of the main adder. You don't need to do it at the same
time as the "15-n" operation.

So, to subtract Y = A - B, simply:

a) Ensure that A and B have the same number of bits
b) Invert every bit of B, to make ~B
c) Use your adder, with its carry-in active, to compute
Y = A + ~B + 1
d) Throw away the most significant (carry-out) bit of the result

And if this, together with your textbooks and class notes,
isn't a big enough hint, then....

Oh, the VHDL? Just a few component instances, I think.
Or a few concurrent assignments. I don't think I could
give you a hint without giving you most of the answer!

cheers
--
Jonathan Bromley, Consultant and ex-lecturer

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

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24 1AW, UK
Tel: +44 (0)1425 471223 mail:
Fax: +44 (0)1425 471573 Web: http://www.doulos.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.





Jonathan Bromley
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Phase locked loop VHDL code mreddy.a Hardware 0 09-16-2007 09:43 AM
VHDL code for SPI Master shah_satish2002 Hardware 2 07-22-2007 08:12 PM
Writing Register code in vhdl amirster Hardware 2 06-11-2007 03:22 PM
vhdl code amirster Hardware 0 05-10-2007 07:28 AM
Problem in VHDL code. caylakprogramci Hardware 2 05-07-2007 07:30 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46