我正在尝试编写一个非常简单的模块,其中包含两个整数输入和一个选择输入。When select is 0 output should be sum of inputs and when select is 1 output should be difference of them. 我将使用 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 给出了溢出错误,因为据我所知,两个整数的总和不能适合另一个整数。
如何定义具有足够范围以保持结果的类型?我的第一次尝试如下。但是在这种情况下,我应该为新类型定义“+”和“-”运算符。
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.