"nick" <email@example.com> wrote in message
> 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.
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...
But just for grins, let's try something else...
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"...
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!
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: firstname.lastname@example.org
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.