2

2 つの整数入力と 1 つの選択入力を持つ非常に単純なモジュールを作成しようとしています。selectが0の場合、出力は入力の合計であり、selectが1の場合、出力はそれらの差でなければなりません。GHDL を使用した簡単なテスト ベンチでモジュールを検証します。モジュールは合成可能である必要はありません。

私の最初の試みは次のとおりです。

entity alu is
  port (
    a   : in  integer;                  -- first input
    b   : in  integer;                  -- second input
    y   : out integer;                  -- result
    sel : in  bit);                     -- if sel = 0 y = a+b,  sel = 1 y = a-b

end entity alu;

architecture behav of alu is

begin  -- architecture behav

  -- purpose: calculate the output
  -- type   : combinational
  -- inputs : a, b, y
  -- outputs: y
  compute: process (sel, a, b) is
  begin  -- process compute
    if sel='0' then
      y <= a + b;
    else
      y <= a - b;
    end if;
  end process compute;

end architecture behav;

問題は、GHDL がオーバーフロー エラーを出すことです。私が理解している限り、2 つの整数の合計は別の整数に収まらないからです。

結果を保持するのに十分な範囲を持つ型を定義するにはどうすればよいですか? 私の最初の試みは次のとおりです。ただし、その場合、新しい型に対して「+」および「-」演算子を定義する必要があります。

type big_int is range 2 * integer'low  to 2 * integer'high;

必要な範囲が整数よりも広いため、サブタイプ定義を使用できません。サブタイプを定義できれば、整数に対して定義された「+」および「-」演算子を再定義せずに使用できます。

編集1:

テスト ベンチと正確なエラーが気になる方のために、EMACS vhdl-mode を使用して半自動生成されたテスト ベンチを示します。

library ieee;
use ieee.std_logic_1164.all;

-------------------------------------------------------------------------------

entity alu_tb is

end entity alu_tb;

-------------------------------------------------------------------------------

architecture test of alu_tb is

  -- component ports
  signal a   : integer;
  signal b   : integer;
  signal y   : integer;
  signal sel : bit;

  -- clock
  signal Clk : std_logic := '1';

begin  -- architecture test

  -- component instantiation
  DUT: entity work.alu
    port map (
      a   => a,
      b   => b,
      y   => y,
      sel => sel);

  -- clock generation
  Clk <= not Clk after 10 ns;

  -- waveform generation
  WaveGen_Proc: process
  begin
    a <= 24;
    b <= 46;
    sel <= '0';
    wait for 20 ns;

    sel <= '1';
    wait for 20 ns;

    wait;

  end process WaveGen_Proc;



end architecture test;

-------------------------------------------------------------------------------

configuration alu_tb_test_cfg of alu_tb is
  for test
  end for;
end alu_tb_test_cfg;

-------------------------------------------------------------------------------

GHDL からの正確なエラーは次のとおりです。

C:\GHDL\bin\ghdl.exe:error: overflow detected
  from: process work.alu(behav).compute at q9_alu.vhd:21
C:\GHDL\bin\ghdl.exe:error: simulation failed

21行目は

y <= a + b;

ソースファイルで。

編集2:

私のGHDLについて:

ghdl -v

GHDL 0.35 (tarball) [Dunoon edition]
 Compiled with GNAT Version: GPL 2017 (20170515-63)
 mcode code generator
Written by Tristan Gingold.

Copyright (C) 2003 - 2015 Tristan Gingold.
GHDL is free software, covered by the GNU General Public License.  There is
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4

2 に答える 2