Re: division of two 4-bit vectors

Discussion in 'VHDL' started by kennheinrich@sympatico.ca, Feb 12, 2009.

  1. Guest

    On Feb 12, 5:09 am, Jonathan Bromley <>
    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.....:confused:

    >
    > 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.
    >
    > Please, please promise you won't try to use that
    > for anything serious.....?  And please promise that
    > you will say something in your assignment report
    > about division by zero?
    > --
    > 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
    > ://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
     
    , Feb 12, 2009
    #1
    1. Advertising

  2. Guest

    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
    > Goran... This is my first project in vhdl language, so please help me, i
    > will be very grateful.... thanks in advance....
    >
    > --
    > goranbm
    > ------------------------------------------------------------------------
    > goranbm's Profile:http://www.fpgacentral.com/group/member.php?userid=11
    > View this thread:http://www.fpgacentral.com/group/showthread.php?t=87835


    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
    think you will learn more using the first approach.

    - Kenn
     
    , Feb 12, 2009
    #2
    1. Advertising

  3. Aiken Guest

    That's mean you need to have a decoder for Keyboard interface? or just
    input to your Simulation program?

    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
    > Goran... This is my first project in vhdl language, so please help me, i
    > will be very grateful.... thanks in advance....
    >
    > --
    > goranbm
    > ------------------------------------------------------------------------
    > goranbm's Profile:http://www.fpgacentral.com/group/member.php?userid=11
    > View this thread:http://www.fpgacentral.com/group/showthread.php?t=87835
     
    Aiken, Feb 12, 2009
    #3
  4. Dave Guest

    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
    > View this thread:http://www.fpgacentral.com/group/showthread.php?t=87835


    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
     
    Dave, Feb 13, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,801
    darshana
    Oct 26, 2008
  2. Replies:
    94
    Views:
    4,498
    ¬a\\/b
    Feb 9, 2007
  3. Replies:
    3
    Views:
    702
    Shadowman
    Mar 26, 2008
  4. Benjamin Couillard

    Re: division of two 4-bit vectors

    Benjamin Couillard, Feb 13, 2009, in forum: VHDL
    Replies:
    2
    Views:
    2,057
  5. Guest
    Replies:
    0
    Views:
    445
    Guest
    Sep 14, 2005
Loading...

Share This Page