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

P

phanhuyich

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.
 
V

valtih1978

Here is the computation of the max

process
variable max: integer := Integer.Minimum;
begin
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;
 
A

Andy

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
 
A

Anssi Saari

Andy said:
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.

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.
 
A

Andy

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top