I am trying to write a very simple module with two integer inputs and one select input. When select is 0 output should be sum of inputs and when select is 1 output should be difference of them. I will verify the module with a simple test bench using GHDL. The module doesn't have to be synthesizable.
My first attempt is as follows.
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;
The problem is GHDL gives overflow error because as far as I understand sum of two integers can't fit into another integer.
How can I define a type which has range enough to keep results? My first try is as follows. However in that case I should define '+' and '-' operators for new type.
type big_int is range 2 * integer'low to 2 * integer'high;
Since the required range is wider than integer, I can't use subtype definition. If I were able to define a subtype, I could use '+' and '-' operators defined for integers without redefining them.
EDIT 1:
For those who wonder test bench and exact error, here is the test bench which is semi automatically generated using 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;
-------------------------------------------------------------------------------
Here is the exact error from 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
Line 21 corresponds to
y <= a + b;
in source file.
EDIT 2:
About my 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.