pp.359-362
4個の4ビット値のなかから最小値、最大値を求める。procedureの書きかたがよく分からない。
subprograms_pkg.vhd
library ieee; use ieee.numeric_std.all; package subprograms_pkg is type unsigned_array is array (natural range <>) of unsigned; procedure find_min_and_max( input_array : in unsigned_array; ARRAY_SIZE, ELEM_SIZE: in positive; signal min, max: out unsigned ); end package; package body subprograms_pkg is procedure find_min_and_max( input_array : in unsigned_array; ARRAY_SIZE, ELEM_SIZE: in positive; signal min, max : out unsigned) is variable minimum, maximum: unsigned(ELEM_SIZE-1 downto 0); begin minimum := input_array(0); maximum := input_array(0); for i in 1 to ARRAY_SIZE-1 loop if input_array(i) < minimum then minimum := input_array(i); end if; if input_array(i) > maximum then maximum := input_array(i); end if; end loop; min <= minimum; max <= maximum; end procedure; end package body;
library ieee; use ieee.numeric_std.all; use work.subprograms_pkg.all; entity test_circuit is generic( NUM_INPUTS: positive := 4; NUM_BITS : positive := 4 ); port( x : in unsigned_array(0 to NUM_INPUTS-1)(NUM_BITS-1 downto 0); min, max: out unsigned(NUM_BITS-1 downto 0) ); end entity; architecture rtl of test_circuit is begin find_min_and_max(x, NUM_INPUTS, NUM_BITS, min, max); end architecture;
今、8,9,10,11のなかの最小値8 (0b1000)、最大値11 (0b1011)を見つけている。