VHDL: потолок и пол деления на две целочисленные константы - PullRequest
0 голосов
/ 16 июня 2019

В VHDL я ищу способ взять два целочисленных параметра сущности, разделить один против другого как число с плавающей запятой, а затем найти пол и потолок этого отношения с плавающей запятой, а затем сохранить его как целочисленную константу VHDL.

library ieee;
use     ieee.std_logic_1164.all;

entity something is
    generic(
        N: natural := 4;
        M: natural := 150
    );
    port(
        sys_clk     :in  std_logic;
        sys_rst     :in  std_logic;

        datai       :in  std_logic_vector(M-1 downto 0);
        datao       :out std_logic_vector(N-1 downto 0)
    );
end entity;


architecture rtl is something is
    --how to calculate ceiling of  M / N?
    constant ratio_ceiling :integer := integer(real(M)/real(N));

    --how to calculate floor of M / N?
    constant ratio_floor   :integer := integer(real(M)/real(N));

begin

end architecture;

Ответы [ 2 ]

1 голос
/ 16 июня 2019

Код:

library ieee;
use     ieee.std_logic_1164.all;
use     ieee.math_real.all;

entity something is
    generic(
        N: natural := 4;
        M: natural := 150
    );
    port(
        sys_clk     :in   std_logic;
        sys_rst     :in   std_logic;

        datai       :in   std_logic_vector(M-1 downto 0);
        datao       :out  std_logic_vector(N-1 downto 0)
    );
end entity;


architecture rtl of something is
    --how to calculate ceiling of  M / N
    constant ratio_ceiling :integer := integer(ceil(real(M)/real(N)));

    --how to calculate floor of M / N
    constant ratio_floor   :integer := integer(floor(real(M)/real(N)));

begin

    process
    begin
        report "ceil:  " & integer'image(ratio_ceiling);
        report "floor: " & integer'image(ratio_floor);
        wait;
    end process;
end architecture;

Выход:

C:\something> ghdl -a --std=08 --ieee=synopsys --work=work something.vhd

C:\something> ghdl --elab-run --std=08 --ieee=synopsys something --vcd=waves.vcd --ieee-asserts=disable

something.vhd:33:12:@0ms:(report note): ceil:  38
something.vhd:34:12:@0ms:(report note): floor: 37
0 голосов
/ 09 июля 2019

Не уверен, что ваш вопрос на самом деле, код кажется правильным. Если вы хотите избежать приведения типов и преобразований, вы можете использовать

constant ratio_ceiling : integer := (M + N - 1) / N;
constant ratio_floor   : integer := M / N;

Целые числа VHDL будут округляться, так что это прекрасно работает.

...