Velocity Reviews > VHDL > Re: division of two 4-bit vectors

# Re: division of two 4-bit vectors

kennheinrich@sympatico.ca
Guest
Posts: n/a

 02-12-2009
On Feb 12, 5:09*am, Jonathan Bromley <(E-Mail Removed)>
wrote:
> On Wed, 11 Feb 2009 15:02:31 -0800, My Name wrote:
>
> >I am sorry, i did not look for this topic on this forum, but i need
> >help, i am suposed to program a simple calculator in vhdl, and program
> >it on spartan 3 FPGA board... My assingment is to make a simple
> >calculator, i need to enter a two numbers (that are a 4 bit vectors) and
> >then enter a operand ( +, -, * or /). on my vhdl code everything is
> >working except division ( / ). and i dont know how to divide a two 4-bit
> >vectors..... please can anybody can help me... sorry for my bad
> >english.....

>
> For such tiny numbers, you could do "kindergarten division":
> repeatedly subtract the divisor from the dividend, counting
> how many times you do the subtraction, until the subtraction
> would give a negative result. *This could even be done
> as a purely combinational function - it would create truly
> horrible hardware, but it would work:
>
> * signal dividend, divisor, quotient, remainder:
> * * * * * * * * * * * * * * * * * *unsigned(3 downto 0);
> * .....
> * process (dividend, divisor)
> * * variable v: unsigned(3 downto 0);
> * * variable done: boolean;
> * begin
> * * v := dividend;
> * * done := false;
> * * for i in 0 to 15 loop
> * * * if not done then
> * * * * if v < divisor then
> * * * * * quotient <= to_unsigned(i, 4);
> * * * * * remainder <= v;
> * * * * * done := true;
> * * * * else
> * * * * * v := v - divisor;
> * * * * end if;
> * * * end if;
> * * end loop;
> * end process;
>
> For extra credit, explain why I used a "done" flag
> and a "for" loop, instead of a "while" loop.
>
> for anything serious.....? *And please promise that
> you will say something in your assignment report
> --
> 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
> (E-Mail Removed)://www.MYCOMPANY.com
>
> The contents of this message may contain personal views which
> are not the views of Doulos Ltd., unless specifically stated.

And in a slighly more tongue-in-cheek answer which is however
completely serious, try using the VHDL "&" operator. It will give you
a result as an unreduced fraction. This might not be what you thought
you wanted but has the obvious advantage of retaining full arithmetic
precision and cleanly handling division by zero.

signal numerator, denominator : bit_vector(3 downto 0);
result :bit_vector(7 downto 0);
result <= numerator & denominator;

Seriously, without defining what your output needs to be, there's very
little point asking how to do it. The division operator over Zmod16 is
not closed over any simple field representable by uniformly spaced
values (which is probably what you might be asking for) unless you use
fractions mod LCM([1...15]). If you just want to implement a random
arithmetic hack that approximates 256 distinct values, use a ROM or
the code above.

- Kenn

kennheinrich@sympatico.ca
Guest
Posts: n/a

 02-12-2009
On Feb 12, 12:49*pm, My Name <myemail@a_domain.com> wrote:
> i have another problem, if you can help me..... it is just a college
> project, it is not anything serious... my group an me, made a vhdl code
> for calculator that uses a State machine, and it can do a math
> operations and logical operations with two 4 bit vectors. Today we had a
> meeting with teacher, and he just said that this calculator is for small
> kids so hi said that we need to do a calculator, that will work with
> biger numbers, 4 digit numbers ( max. 9999 ) it needs to have only math
> and logical operations..... input comes from a keyboard..... so can you
> help me please, i am from Croatia, im 21 years old, and my name is
> will be very grateful.... thanks in advance....
>
> --
> goranbm
> ------------------------------------------------------------------------
> goranbm's Profile:http://www.fpgacentral.com/group/member.php?userid=11

Hi Goran,

There are three things you need to figure out. The first thing is to
understand how to divide numbers. The second thing is to understand
how to do that division in hardware. The last thing is to code that
hardware in VHDL. (Apparently, the philosophical issues regarding the
mapping of mathematical ideas to std_logic_vectors are inflammatory!)

The simplest way to understand the first thing is to see that ordinary
"schoolboy-method" long division will work just as well if you write
the numbers in binary as if you write them in decimal. Once you can do
long division of two numbers in binary with pencil and paper you are
halfway there.

The simplest way to understand the second thing is to see how your
long division can be created with shift registers, subtractor, and
adders, and what order you need to do all the individual steps. Now
you are 90% of the way there.

The last step is just VHDL coding. If you know what you want to build
you can usually see how to make it happen in code. Once you have a
more specific problem, let us know.

The completely opposite approach is to look for some VHDL code on
google and just play with it until you get it to work. Personally, I

- Kenn

Aiken
Guest
Posts: n/a

 02-12-2009
That's mean you need to have a decoder for Keyboard interface? or just

On Feb 12, 12:49*pm, My Name <myemail@a_domain.com> wrote:
> i have another problem, if you can help me..... it is just a college
> project, it is not anything serious... my group an me, made a vhdl code
> for calculator that uses a State machine, and it can do a math
> operations and logical operations with two 4 bit vectors. Today we had a
> meeting with teacher, and he just said that this calculator is for small
> kids so hi said that we need to do a calculator, that will work with
> biger numbers, 4 digit numbers ( max. 9999 ) it needs to have only math
> and logical operations..... input comes from a keyboard..... so can you
> help me please, i am from Croatia, im 21 years old, and my name is
> will be very grateful.... thanks in advance....
>
> --
> goranbm
> ------------------------------------------------------------------------
> goranbm's Profile:http://www.fpgacentral.com/group/member.php?userid=11

Dave
Guest
Posts: n/a

 02-13-2009
On Feb 12, 6:00*pm, My Name <myemail@a_domain.com> wrote:
> i just need to input a 4 digit number from a keyboard to simulation....
> if it is possible....
>
> --
> goranbm
> ------------------------------------------------------------------------
> goranbm's Profile:http://www.fpgacentral.com/group/member.php?userid=11

If you're just trying to get data into the simulation, and not have it
coded into the VHDL, maybe file I/O is what you want.

Dave

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Benjamin Couillard VHDL 2 02-13-2009 08:39 PM pauldepstein@att.net C++ 3 03-26-2008 06:22 PM krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM Ruby 0 09-14-2005 05:47 PM walala VHDL 12 09-14-2003 03:49 PM