VHDL code for 2's complement

N

nick

Guest
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
 
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
 
"nick" <flyerscrew05@aol.com> wrote in message
news:b047f94a.0307281058.429f7b8f@posting.google.com...

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: jonathan.bromley@doulos.com
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.
 

Welcome to EDABoard.com

Sponsor

Back
Top