Ask about finding maximum and second's maximum number in array is given.

Discussion in 'VHDL' started by phanhuyich, Jun 18, 2013.

  1. phanhuyich

    phanhuyich Guest

    I am starting to study VHDL. Now, I have to do an exercise with the following content:

    I have to define an array of 10 elements ( 8 bit range) ([3,4,2,8,9,0,1,5,7,6] for example). And 10 elements were imported to within 10 clock cycles. The question is find the maximum number and second maximum number in this array after 10 clock cycle.
    Anyone help to show me the method to solve it using VHDL ?

    Thank you.
    phanhuyich, Jun 18, 2013
    1. Advertisements

  2. phanhuyich

    valtih1978 Guest

    Here is the computation of the max

    variable max: integer := Integer.Minimum;
    for I in 1 to 10 loop
    wait until clk = '1';
    if (input > max)
    max := input;
    end if;
    end loop;
    report "max = " & integer'image(max);
    end process;

    I am sure, you can extend it to the pre-max variable by something like
    if (input > max)
    premax := max;
    max := input;
    end if;
    valtih1978, Jun 25, 2013
    1. Advertisements

  3. phanhuyich

    Andy Guest

    Your premax solution only works if the highest value occurs in the input stream after the 2nd highest value, and the 2nd highest value does not have the same value as the highest value.

    You need a separate comparison of input and premax. That means premax needs initialization too.

    Process variables are only initialized at time zero; when the implied process loop (not the loop statement) repeats, the variable is not re-initialized.

    In all fairness though, the OP did not state he needed a synthesizable solution.

    Andy, Jun 25, 2013
  4. phanhuyich

    Anssi Saari Guest

    Didn't he mention a clock cycle limit of 10 though? So it slightly hints
    towards synthesizable. Or at least synchronous.

    Another solution is of course sorting the array and taking the two
    largest values. I'd do that if this were a real problem and there were
    some doubt that maybe it's not the two largest but perhaps the fourth or
    seventh largest value I need... Or the median for that matter.
    Anssi Saari, Jun 28, 2013
  5. phanhuyich

    Andy Guest

    The OP said; "And 10 elements were imported to within 10 clock cycles". That is not the same as "only 10 clocks will be provided".

    What does the model do on the 11th and following clock cycles?

    Andy, Jun 28, 2013
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.